Example #1
0
		public b_rule (bool a, int rt1, int rs1,int rt2, int rs2,bool b, int pt1, int ps1,int pt2, int ps2, int idx, double rate)
		{
			type_state r1; 
			r1.state = rs1;
			r1.type = rt1;

			type_state r2; 
			r2.state = rs2;
			r2.type = rt2;
					
			type_state p1; 
			p1.state = ps1;
			p1.type = pt1;

			type_state p2; 
			p2.state = ps2;
			p2.type = pt2;

			reactionContact = a;
			productContact = b;
			reactants = new pair_st (r1, r2);
			products = new pair_st (p1, p2);				

			this.idx = idx;
			this.rate = rate;

		//	Console.WriteLine (this+" "+this.idx + " " + idx);
		}
Example #2
0
		public void TestPairST ()
		{
			int t = 0, s = 1;

			particle p = new particle (t, s);
			particle q = new particle (t, s);

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

		}
Example #3
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 #4
0
		public t_rule (bool a, int rt1, int rs1,int rt2, int rs2, int enz_t1, int enz_s1, bool b, int pt1, int ps1,int pt2, int ps2, int enz_t2, int enz_s2, bool c, bool d, int idx, double rate)
		{
			type_state r1; 
			r1.state = rs1;
			r1.type = rt1;

			type_state r2; 
			r2.state = rs2;
			r2.type = rt2;

			type_state p1; 
			p1.state = ps1;
			p1.type = pt1;

			type_state p2; 
			p2.state = ps2;
			p2.type = pt2;

			trireac.type = enz_t1;
			trireac.state = enz_s1;

			type_state e2;
			e2.type = enz_t2;
			e2.state = enz_s2;


			reactionContact = a;
			productContact = b;
			enzymeContact1 = c;
			enzymeContact2 = c;

			reactants = new pair_st (r1, r2);
			products1 = new pair_st (p1, p2);				
			products2 = new pair_st (p1, e2);
			products3 = new pair_st (p2, e2);

			this.idx = idx;
			this.rate = rate;							
		}
Example #5
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 #6
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 #7
0
		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);			
				}

			}
		}
Example #8
0
		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);
			}
		}
Example #9
0
		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);
		}
Example #10
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 #11
0
		public override double propensity(reactor rec){
			var pDict = rec.pDict;
			var stDict = rec.stDict;

			pair_st st = reactants;

			if (reactionContact == true) {

				// looking for linked reactants as in pair 

				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;

				// counting the number of reactants 
				int na = stDict.ContainsKey (trireac) ? stDict [trireac].Count : 0;

				// we need to remove already linked 
				pair_st a1 = new pair_st(reactants.a, trireac);

				int rem = 0;
				if (pDict.ContainsKey (a1)) {
					foreach (pair_part p in pDict[a1]) {
						particle p1 = rec.get_particle_id(p.id1);
						particle p2 = rec.get_particle_id(p.id2);
						if (p1.st == trireac) {
							foreach (int id in p1.linked) {
								particle o = rec.get_particle_id (id);
								if (o.st == reactants.b)
									rem += 1;
							}							
						} else {
							foreach (int id in p2.linked) {
								particle o = rec.get_particle_id (id);
								if (o.st == reactants.b)
									rem += 1;
								;
							}
						}
					}
				}

				
				return rate * (count * na - rem); 

			}
			else {
				type_state a = st.a;
				type_state b = st.b;
				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;
				int ntot = na * nb - nc;

				if (ntot < 0)
					return 0.0;
								

				return rate * ntot;
			}
		}