public bool apply(reactor rec,pair_part ret){ particle a = rec.get_particle_id(ret.id1); particle b = rec.get_particle_id(ret.id2); type_state old_a = a.st; type_state old_b = b.st; switch (function) { case 0: default: return false; case 1: //link rec.link (a, b); break; case 2: // split rec.unlink (a, b); break; case 3: // add type_state new_a = new type_state(a.st.type,a.st.state+1); type_state new_b = new type_state(b.st.type,b.st.state+1); rec.update_new_state (a, old_a,new_a); rec.update_new_state (b, old_b,new_b); break; case 4: // swap rec.update_new_state (a, old_a,old_b); rec.update_new_state (b, old_b,old_a); break; } return true; }
public pair_part pick_reactants(Dictionary<type_state,List<int>> stDict, Dictionary<pair_st,List<pair_part>> pDict,Random rd){ type_state a = new type_state(target_type,p1); type_state b = new type_state(target_type,p2); pair_st st = new pair_st(a,b); switch (function) { case 0: default: return new pair_part (-1, -1); case 1: // link var la = stDict [a]; var lb = stDict [b]; bool done = false; int tmx = 0; while (!done) { tmx++; int na = (int)(rd.NextDouble () * la.Count); int nb = (int)(rd.NextDouble () * lb.Count); int pa = la [na]; int pb = lb [nb]; if (pa != pb){ if (!pDict.ContainsKey (st)) { var ret = new pair_part (pa, pb); return ret; } else { pair_part pp = new pair_part (pa, pb); if (!pDict [st].Contains (pp) && !pDict [st].Contains (pp.swap ())) { var ret = new pair_part (pa, pb); return ret; } } } if (tmx > 100) done = true; } for (int xa = 0; xa < la.Count; xa++) for (int xb = 0; xb < lb.Count; xb++) { int pa = la [xa]; int pb = lb [xb]; pair_part pp = new pair_part (pa, pb); //Console.WriteLine (pp.id1+"-"+pp.id2 + " " + xa + " " + xb + " " + pDict [st].Contains (pp) + pDict [st].Contains (pp.swap ())); if (!pDict [st].Contains (pp) && !pDict [st].Contains (pp.swap ())) { var ret = new pair_part (pa, pb); return ret; } } int ra = stDict.ContainsKey (a) ? stDict [a].Count : 0; int rb = stDict.ContainsKey (b) ? stDict [b].Count : 0; int nc = pDict.ContainsKey (st) ? pDict [st].Count : 0; nc += pDict.ContainsKey (st.swap()) ? pDict [st.swap()].Count : 0; if (a.state == b.state && a.type ==b.type ) rb += -1; // Console.WriteLine ("early stop :"+a +" "+b+" " + ra + " " + rb + " " + nc + " " + (ra*rb -nc) ); return new pair_part (-1, -1); case 2: // split case 3: // add case 4: // swap double ur = rd.NextDouble (); int n = 0, u = 0, v = 0; if (pDict.ContainsKey (st)) { u = pDict [st].Count; n += u; } if (pDict.ContainsKey (st.swap ())) { v = pDict [st.swap ()].Count; n += v; } if (ur * n < u) return pDict [st] [(int)(ur * n)]; else { var m = pDict [st.swap ()]; return m [(int)(ur * n - u)].swap (); } } }
public void add_particle(particle o){ if (!particles.Contains (o)) { particles.Add (o); ids.Add (o.id, o); if (!stDict.ContainsKey (o.st)) stDict.Add (o.st, new List<int> ()); stDict [o.st].Add (o.id); foreach (int pid in o.linked) { particle p = get_particle_id (pid); pair_st s = new pair_st (o, p); if (!pDict.ContainsKey (s)) { pDict.Add (s, new List<pair_part> ()); } var par = new pair_part (o.id, p.id); pDict [s].Add (par); } } }
public void unlink(particle a, particle b){ if (a == null) return; if (b == null) return; pair_st s = new pair_st (a, b); if (pDict.ContainsKey (s)) { pair_part n = new pair_part (-1, -1); foreach (pair_part l in pDict[s]) { if ((l.id1 == a.id) && (l.id2 == b.id)) { n = l; break; } if ((l.id2 == a.id) && (l.id1 == b.id)) { n = l; break; } } if (n.id1 != -1) pDict [s].Remove(n); } }
public void link(particle a, particle b){ if (a == null) return; if (b == null) return; a.link (b); pair_st s = new pair_st (a, b); if (!pDict.ContainsKey (s)) { pDict.Add (s, new List<pair_part> ()); } var par = new pair_part(a.id,b.id); pDict [s].Add (par); }
public void update_new_state(particle a,type_state old_state, type_state new_state){ foreach (int pid in a.linked) { particle p = get_particle_id (pid); pair_st st1 = new pair_st (old_state, p.st); List<pair_part> l; if (pDict.ContainsKey (st1)) { l = pDict [st1]; // Console.WriteLine (st1); l.RemoveAll (x => x.id1 == a.id && x.id2 == p.id); l.RemoveAll (x => x.id2 == a.id && x.id1 == p.id); } pair_st st2 = new pair_st (p.st,old_state); if (pDict.ContainsKey(st2)) { l = pDict [st2]; // Console.WriteLine ("swap:"+st2); l.RemoveAll (x => x.id1 == a.id && x.id2 == p.id); l.RemoveAll (x => x.id2 == a.id && x.id1 == p.id); } // Console.WriteLine ("update :" + a + "O:" + old_state + "N:" + new_state + " " + p); pair_st pst = new pair_st (new_state, p.st); if(!pDict.ContainsKey(pst)) pDict.Add(pst,new List<pair_part>()); var ol = new pair_part (a.id, p.id); pDict [pst].Add (ol); //if (pDict.ContainsKey(ol.swap()) } // Console.WriteLine ("#-" + a.id + " " + stDict [old_state].Count+ " " + old_state); stDict [old_state].Remove (a.id); // Console.WriteLine ("#-" + a.id + " " + stDict [old_state].Count+ " " + old_state); a.st = new_state; if (!stDict.ContainsKey (new_state)) stDict [new_state] = new List<int> (); stDict [new_state].Add (a.id); }
public bool apply(reactor rec,pair_part ret){ particle a = rec.get_particle_id(ret.id1); particle b = rec.get_particle_id(ret.id2); type_state old_a = a.st; type_state old_b = b.st; // Console.WriteLine ("A:" + (old_a.state == r.reactants.b.state)); pair_st st = products; type_state new_a = st.a; type_state new_b = st.b; if (old_a != new_a) rec.update_new_state (a, old_a, new_a); if (old_b!=new_b) rec.update_new_state (b , old_b, new_b); if (productContact) rec.link (a, b); else { a.unlink (b); rec.unlink (a, b); } return true; }