Example #1
0
		public m_rule (int sT, int sS, int eT, int eS, bool u, double rate, int idx)
		{
			start = new type_state (sT, sS);
			end = new type_state (eT, eS);
			unlink = u;		
			this.rate = rate;
			this.idx = idx;
		}
Example #2
0
		public ensemble (int s, type_state[] p, int[,] l)
		{
			size = s;
			parts = new type_state[s];
			for (int i = 0; i < s; i++) {
				parts [i] = p [i];
			}
			links = new int[s, s];
			for (int i = 0; i < s; i++) {
				for (int j = 0; j < s; j++) {
					links [i, j] = l [i, j];
				}
			}
		}
Example #3
0
		public pair_st (type_state a,type_state b)
		{
		/*	if (a.type <= b.type) {
				if (a.state <= b.state) {*/
					this.a = a;
					this.b = b;
			/*
				} else {
					this.a = b;
					this.b = a;
				}
			} else {
				this.a = b;
				this.b = a;

			}*/
		}
Example #4
0
		public override double propensity(Dictionary<type_state,List<int>> stDict, Dictionary<pair_st,List<pair_part>> pDict){

			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 0.0;


			case 1: // link 
				int na = stDict.ContainsKey (a) ? stDict [a].Count : 0;
				int nb = 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)
					nb += -1;

				Console.WriteLine (na + " " + nb + " " + nc +  " " + rate * (na * nb - nc));
				if (nc > na * nb)
					return 0.0;
				return rate * (na * nb - nc);
				
			case 2: // split
			case 3: // add
			case 4: // swap 				
				int count = 0;
				if (pDict.ContainsKey (st))
					count += pDict [st].Count;
				pair_st st_swap = st.swap ();
				if (pDict.ContainsKey (st_swap))
					count += pDict [st_swap].Count;
				Console.WriteLine (count);
				return rate * count;
				
			}
			
		}
Example #5
0
		public pair_st(particle a, particle b){

			this.a = a.st;
			this.b = b.st;
		}
Example #6
0
		public void TestReactor ()
		{
			
			reactor rec = new reactor ();

			/// new configuration tests
			Assert.AreEqual (0, rec.particles.Count);
			Assert.AreEqual (0, rec.rules.Count);

			int t = 0, s = 1;
			type_state key = new type_state(t,s);
			particle p = new particle (t, s);
			rec.add_particle (p);

			// test add particles 
			Assert.AreEqual (1, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (1, rec.stDict [key].Count);

			// check link -> no links 
			Assert.AreEqual (0, rec.pDict.Count);

			// double add 
			rec.add_particle (p);
			Assert.AreEqual (1, rec.particles.Count);


			// two particles 
			particle q = new particle (t, s);


			// test add 
			rec.add_particle (q);
			Assert.AreEqual (2, rec.particles.Count);

			// test dictionnaries 
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			// test remove 

			rec.remove_particle (q);

			Assert.AreEqual (1, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (1, rec.stDict [key].Count);


			rec.remove_particle (p);

			Assert.AreEqual (0, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (0, rec.stDict [key].Count);


			// test link 
			rec.add_particle (q);
			rec.add_particle (p);

			Assert.AreEqual (2, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			// create pair part 

			pair_st pst = new pair_st (p, q);
			pair_st pstn = new pair_st (p.st, q.st);

			Assert.AreEqual (pst.a, pstn.a);
			Assert.AreEqual (pst.b, pstn.b);


			rec.link (p, q);

			Assert.AreEqual (2, rec.particles.Count);
			Assert.IsTrue(rec.stDict.ContainsKey(key));
			Assert.AreEqual (2, rec.stDict [key].Count);

			Assert.IsTrue (rec.pDict.ContainsKey (pst));
			Assert.AreEqual (1, rec.pDict[pst].Count);

		}
Example #7
0
		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 ();
				}
					

			}			
		}
Example #8
0
		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;
		}
Example #9
0
		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);


		}
Example #10
0
		public d_rule (int sT, int sS, double rate, int idx)
		{
			start = new type_state (sT, sS);		
			this.rate = rate;
			this.idx = idx;
		}