Add command-line option/flag to flush each output line when printing to stdout.
authorDariusz Murakowski <murakdar@mit.edu>
Fri, 26 Jun 2015 20:15:24 +0000 (16:15 -0400)
committerDariusz Murakowski <murakdar@mit.edu>
Fri, 26 Jun 2015 20:15:24 +0000 (16:15 -0400)
ss.cpp
ss.h

diff --git a/ss.cpp b/ss.cpp
index ea4696a..2839ce9 100644 (file)
--- a/ss.cpp
+++ b/ss.cpp
@@ -337,7 +337,7 @@ void run(RunParameters_SS &r, unsigned seed) {
     else
         initialize_Ising(r,species,reactions,print_spec);
 
-    simulate(reactions, species, r.t_end, r.max_steps, r.sample_interval, r.step_sample_interval, print_spec, r.useVerbose);
+    simulate(reactions, species, r.t_end, r.max_steps, r.sample_interval, r.step_sample_interval, print_spec, r.useVerbose, r.flushOften);
 
     // clean up reaction array
     for (Rxn_parray::iterator  it = reactions.begin(),
@@ -376,7 +376,7 @@ void print_species_header(const Species_parray &print_spec)
     std::cout << '\n';
 }
 
-void print_species_traj(double t, long step, const Species_parray &print_spec, bool verbose)
+void print_species_traj(double t, long step, const Species_parray &print_spec, bool verbose, bool flushOften)
 {
     std::cout << t;
     for (Species_parray::const_iterator spec = print_spec.begin(),
@@ -420,10 +420,12 @@ void print_species_traj(double t, long step, const Species_parray &print_spec, b
 
     }
     std::cout << '\n';
+    if (flushOften)
+        std::cout << std::flush;
 }
 
 
-void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long max_steps, double sample_interval, unsigned step_sample_interval, const Species_parray &print_spec, bool verbose)
+void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long max_steps, double sample_interval, unsigned step_sample_interval, const Species_parray &print_spec, bool verbose, bool flushOften)
 {
     long step = 0;
 
@@ -436,7 +438,7 @@ void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long
     double total_propensity = 0.0;
 
     print_species_header(print_spec);
-    print_species_traj(t,step,print_spec,verbose);   // XXX
+    print_species_traj(t,step,print_spec,verbose,flushOften);   // XXX
 
     Rxn_parray::iterator rxn, end;
     Rxn_parray::iterator rxn2, end2;
@@ -496,18 +498,18 @@ void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long
         // print copy numbers
         if (t_next_sample <= t) {
             t_next_sample += sample_interval;
-            print_species_traj(t,step,print_spec,verbose);
+            print_species_traj(t,step,print_spec,verbose,flushOften);
         }
 
         if ((step_sample_interval != 0) && (step_next_sample <= step)) {
             step_next_sample += step_sample_interval;
-            print_species_traj(t,step,print_spec,verbose);
+            print_species_traj(t,step,print_spec,verbose,flushOften);
         }
 
     }
 
     if (t_end == HUGE_VAL)
-        print_species_traj(t,step,print_spec,verbose);
+        print_species_traj(t,step,print_spec,verbose,flushOften);
 
 }
 
@@ -1043,6 +1045,7 @@ void usage()
 " -const_ND (int)     number of effector CTL divisions (generations) [default=9]\n"
 " -vol (double)       volume scaling factor; affects rate constants, not initial values [default=1.0]\n"
 " -track_esc          flag to count escaped viruses (in Iesc species)\n"
+" -flush_each         flag to flush after each line of output (vs waiting for buffering)\n"
 ;   std::cout.flush();
 }
 
@@ -1122,6 +1125,8 @@ int main(int argc, char *argv[]) {
 
         else if (strcmp(argv[i],"-track_esc")==0) { r.trackEscaped = true; }
 
+        else if (strcmp(argv[i],"-flush_each")==0) { r.flushOften = true; }
+
         else if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0)   { usage(); return 0;    }
 
         else printf("Unrecognized argument! '%s'\n",argv[i]);
diff --git a/ss.h b/ss.h
index df9e0a3..937009d 100644 (file)
--- a/ss.h
+++ b/ss.h
@@ -142,6 +142,8 @@ public:
                                 // If true, use a dummy T cell species "Iesc" with special reactions,
                                 // updating amount when viruses aren't recognized.
 
+    bool flushOften;            // flush output each line?
+
 
     std::vector<std::set<unsigned int> > initPop;   // Initial population sequences
     std::vector<double>                  initFrac;  // Initial population fraction
@@ -232,6 +234,8 @@ public:
 
         trackEscaped = false;
 
+        flushOften = false;
+
     }
 
 
@@ -282,7 +286,7 @@ void importState(RunParameters_SS &r);
 void importState_Potts(RunParameters_SS &r);
 void importEpitope(RunParameters_SS &r, Species_parray &species, Rxn_parray &reactions, VirusSpecies *V, Species_parray &print_spec);
 void importEpitope_Potts(RunParameters_SS &r, Species_parray &species, Rxn_parray &reactions, NTVirusSpecies *V, Species_parray &print_spec);
-void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long max_steps, double sample_interval, unsigned step_sample_interval, const Species_parray &print_spec, bool verbose);
+void simulate(Rxn_parray &reactions, Species_parray &species, double t_end, long max_steps, double sample_interval, unsigned step_sample_interval, const Species_parray &print_spec, bool verbose, bool flushOften);
 void initialize_Ising(RunParameters_SS &r, Species_parray &species, Rxn_parray &reactions, Species_parray &print_spec);
 void initialize_Potts(RunParameters_SS &r, Species_parray &species, Rxn_parray &reactions, Species_parray &print_spec);