コード例 #1
0
ファイル: replicator.cs プロジェクト: hsoula/staarc
		public static void graph(reactor rec, int seed, double lambda, int nb){
			rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + ".dot");
			// rec.export_particles_list ("part.rec");

			var g = rec.create_graph ();

			IDictionary<int,int> components = new Dictionary<int,int> ();
			int count = g.WeaklyConnectedComponents (components);

			Console.Write (count + " " + rec.npart + " " + ((double)count) / (rec.npart));
			List<particle>[] u = new List<particle>[count];
			for (int i = 0; i < count; i++)
				u [i] = new List<particle> ();

			foreach (KeyValuePair<int,int> kv in components) {			
				u [kv.Value].Add (rec.get_particle_id (kv.Key));				
			}

			int tk = 0;
			for (int i = 0; i < count; i++)
				tk += u [i].Count;
			Console.WriteLine (" " + ((float)tk) / count);
			for (int i = 0; i < count; i++) {
				List<particle> l = u [i];
				particle p = l.Find (x => x.st.type == 5);
				if (p != null) {

					particle current = p;

					bool done = true;
					while (done) {
						Console.Write (current.st);
						l.Remove (current);
						if (current.linked.Count > 0) {
							int nc = current.linked.Count;
							int z = 0;
							while (z < nc && !l.Contains (rec.get_particle_id (current.linked [z]))) {
								z++;
							}
							if (z == nc)
								done = false;
							else {
								current = rec.get_particle_id (current.linked [z]);							
							}
						}				
					}
					Console.WriteLine ("");
				} else {
					foreach (particle r in l) {
						Console.Write (r.st);
					}
					Console.WriteLine ("");
				}
			}


		}
コード例 #2
0
ファイル: d_rule.cs プロジェクト: hsoula/staarc
		public  bool apply(reactor rec,int pa){


			particle a = rec.get_particle_id(pa);
			//Console.WriteLine("a: " + a);
			rec.remove_particle (a);
			return true;
		}
コード例 #3
0
ファイル: m_rule.cs プロジェクト: hsoula/staarc
		public  bool apply(reactor rec,int pa){


			particle a = rec.get_particle_id(pa);

			type_state old_a = a.st;
			type_state new_a = end;
			if (old_a != new_a)
				rec.update_new_state (a, old_a, new_a);
		

			if (unlink)
				rec.all_unlink (a.id);

			return true;
		}
コード例 #4
0
ファイル: replicator.cs プロジェクト: hsoula/staarc
		public static void Main (string[] args)
		{


			int seed = Int32.Parse (args [0]);
			double lambda = double.Parse (args [1]);
			int nb = Int32.Parse (args [2]);
			//double kappa = double.Parse (args [3]);

			string js_name = args [3];
			string json;
			using (System.IO.StreamReader r = new System.IO.StreamReader (js_name)) {
				json = r.ReadToEnd ();
			}
			
			ensemble e = JsonConvert.DeserializeObject<ensemble> (json);


			reactor rec = new reactor ();
			for(int x = 0; x < nb; x++)
				rec.insert_ensemble (e);


			var lr0 = b_rule.generate_rules (7, false, 5, 8, 5, 0, true, 4, 3,1,lambda);
			var lr1 = b_rule.generate_rules (7, true, -1, 4, -2, 1, true, 2, 5,2,lambda);
			var lr2 = b_rule.generate_rules (7, false, -1, 5, -1, 0, true, 7, 6,3,lambda);
			var lr3 = b_rule.generate_rules (7, false, -1, 3, -2, 6, true, 2, 13, 4,lambda);
			var lr4 = b_rule.generate_rules (7, true, -1, 7, -2, 13, true, 4, 3,5,lambda);
			var lr5 = b_rule.generate_rules (7, true, 6, 4, 6, 3, false, 8, 8,6,lambda);
			var lr6 = b_rule.generate_rules (7, true, -1, 2, -2, 8, true, 9, 1,7,lambda);
			var lr7 = b_rule.generate_rules (7, true, -1, 9, -2, 9, false, 8, 8, 8,lambda);

			foreach(b_rule r in lr0) rec.add_rule(r);
			foreach(b_rule r in lr1) rec.add_rule(r);		
			foreach(b_rule r in lr2) rec.add_rule(r);
			foreach(b_rule r in lr3) rec.add_rule(r);
			foreach(b_rule r in lr4) rec.add_rule(r);
			foreach(b_rule r in lr5) rec.add_rule(r);
			foreach(b_rule r in lr6) rec.add_rule(r);
			foreach(b_rule r in lr7) rec.add_rule(r);

			double lambda_p = 1e-6;
			for (int i = 1; i < 7; i++) {
				var er0 = new o_rule (i, 0, lambda_p); 
				rec.add_rule (er0);
			}

			double lambda_d = 1e-7;
			for (int i = 1; i < 7; i++) {
				for (int j = 0; j < 1; j++) {
					
					var er0 = new d_rule (i, j, lambda_d, 0);
					rec.add_rule (er0);
				}
			}
		

			for (int i = 0; i < 10; i++) {

				rec.add_particle (new particle (1, 0));
				rec.add_particle (new particle (2, 0));
				rec.add_particle (new particle (3, 0));
				rec.add_particle (new particle (4, 0)); 
				rec.add_particle (new particle (5, 0));
				rec.add_particle (new particle (6, 0));
			}

			double tau;
			double total_time = 0.0;
			Random rd = new Random (seed);
			int step = 0;
			int idx;
			int ndiv = 0;

			while (rec.gillespie_step (rd,out tau, out idx)) {

				total_time +=tau;
				step++;
				int npart = rec.particles.Count;
				if (npart>100000)
					break;

				Console.WriteLine (step+ " " + npart + " " + ndiv + " " + total_time + " " + idx + " " + rec.get_rule(idx).idx + " " +  rec.get_rule(idx));
				if (idx == 245)
					ndiv ++;

				if (step % 1000 == 0) {
					var g = rec.create_graph ();
					IDictionary<int,int> components = new Dictionary<int,int> ();
					int count = g.WeaklyConnectedComponents (components);
					List<particle>[] u = new List<particle>[count];
					for (int i = 0; i < count; i++)
						u [i] = new List<particle> ();

					foreach (KeyValuePair<int,int> kv in components) {			
						u [kv.Value].Add (rec.get_particle_id (kv.Key));				
					}

					using(System.IO.StreamWriter str = new System.IO.StreamWriter("out"+step+".out")){				
						for (int i = 0; i < count; i++)					
							str.Write(u [i].Count + " ");
					}
					rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + "_" + step + ".dot");
				}

				
				
			}						
		/*	var g = rec.create_graph ();

			IDictionary<int,int> components = new Dictionary<int,int> ();
			int count = g.WeaklyConnectedComponents (components);
			List<particle>[] u = new List<particle>[count];
			for (int i = 0; i < count; i++)
				u [i] = new List<particle> ();

			foreach (KeyValuePair<int,int> kv in components) {			
				u [kv.Value].Add (rec.get_particle_id (kv.Key));				
			}

			using(System.IO.StreamWriter str = new System.IO.StreamWriter("out.out")){				
				for (int i = 0; i < count; i++)					
					str.Write(u [i].Count + " ");
			}

			int tk = 0;
			for (int i = 0; i < count; i++)
				tk += u [i].Count;
				*/

/*			Console.Write(lambda + " " + nb + " " + kappa + " " + seed + " " + step+" " + total_time+ " " + count + " " + rec.npart + " " + ((double)count) / (rec.npart));
			Console.Write (" " + ndiv);
			Console.WriteLine (" " + ((float)tk) / count);
*/
			rec.export_graphviz ("out_" + seed + "_" + lambda + "_" + nb + ".dot");
		}
コード例 #5
0
ファイル: g_rule.cs プロジェクト: hsoula/staarc
		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;
		}
コード例 #6
0
ファイル: t_rule.cs プロジェクト: hsoula/staarc
		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;
			}
		}
コード例 #7
0
ファイル: b_rule.cs プロジェクト: hsoula/staarc
		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;
		}