Check for file-opening / existence errors (vs segfault). write_mod for general Hamilt...
authorDariusz Murakowski <murakdar@mit.edu>
Tue, 4 Feb 2014 20:01:52 +0000 (15:01 -0500)
committerDariusz Murakowski <murakdar@mit.edu>
Tue, 4 Feb 2014 20:01:52 +0000 (15:01 -0500)
hamiltonian.cpp
wf.cpp

index 593d9b0..b76615d 100644 (file)
@@ -18,7 +18,8 @@ Hamiltonian::Hamiltonian(std::string &FILENAME) {
     this->bJ=1.0;
     
        FILE* input;
-       input = fopen(FILENAME.c_str(),"r");
+       input = fopen(FILENAME.c_str(),"r");    // <paramfile>.j
+    if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); }
        
        typedef std::vector<int> Key;
        std::map<Key,double> JIndex;
@@ -111,7 +112,8 @@ EpitopeHamiltonian::EpitopeHamiltonian(std::string &FILENAME) {
     this->bJ=1.0;
     
        FILE* input;
-       input = fopen(FILENAME.c_str(),"r");
+       input = fopen(FILENAME.c_str(),"r");    // <paramfile>.j
+    if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); }
        
        typedef std::vector<int> Key;
        std::map<Key,double> JIndex;
@@ -325,6 +327,7 @@ TwoSiteHamiltonian::TwoSiteHamiltonian(std::string &FILENAME) {
     
        FILE* input;
        input = fopen(FILENAME.c_str(),"r");
+    if (input == NULL) { perror((std::string("ERROR: ") + FILENAME).c_str()); exit(1); }
        
        typedef std::vector<int> Key;
        std::map<Key,double> JIndex;
diff --git a/wf.cpp b/wf.cpp
index fa6e343..71ecb24 100644 (file)
--- a/wf.cpp
+++ b/wf.cpp
@@ -49,6 +49,8 @@ void run(RunParameters &r, unsigned seed) {
     r.setFiles();
     FILE *popout=fopen(r.trajectoryOutfile.c_str(),"w");        // <outfile>.dat
     FILE *supout=r.useTwoSite ? NULL : fopen(r.supplementaryOutfile.c_str(),"w");     // <outfile>.sum
+    if (popout == NULL) { perror("File error"); exit(1); }
+    if (r.useTwoSite && supout == NULL) { perror("File error"); exit(1); }
     
     if (r.importState) importState(r);      // <infile>.st
     
@@ -103,7 +105,10 @@ void run(RunParameters &r, unsigned seed) {
                 for (i=0; i<r.g; ++i) {
             
                     P.next_generation(H, rnd, r.useRelative, r.useVerbose);
-                    P.write_population(popout,i);
+
+                    if ((i+1) % r.write_mod == 0) {
+                        P.write_population(popout,i+1);
+                    }
                 
                     if (r.runUntilEscape && P.escaped(H)) break;
                 
@@ -161,7 +166,8 @@ void run(RunParameters &r, unsigned seed) {
 
 void importState(RunParameters &r) {
 
-    FILE *input=fopen(r.stateInfile.c_str(),"r");
+    FILE *input=fopen(r.stateInfile.c_str(),"r");   // <infile>.st
+    if (input == NULL) { perror((std::string("ERROR in importState: ") + r.stateInfile).c_str()); exit(1); }
     
     char o;
     double frac;
@@ -326,7 +332,7 @@ int main(int argc, char *argv[]) {
 
         else if (strcmp(argv[i],"-write_mod")==0) { if (++i==argc) break; else r.write_mod=(unsigned int)strtodouble(argv[i]); }
 
-        else if (strcmp(argv[i],"-h")==0)   { usage(); return 0;    }
+        else if (strcmp(argv[i],"-h")==0 || strcmp(argv[i],"--help")==0)   { usage(); return 0;    }
         
         else printf("Unrecognized command! '%s'\n",argv[i]);