From 73ed75ec4da02041b51a1c5f6405f14b785d14b8 Mon Sep 17 00:00:00 2001
From: Gareth Tribello <gt@eider.asg>
Date: Wed, 24 Jul 2013 10:09:42 +0100
Subject: [PATCH] Fixed small things in analysis

Made it so that virtual routine is not called from constructor.  If the calculation
is terminated during the analysis phase then the analysis is now restarted when
the calculation reaches update on the first loop.  I also removed the duplicated
backup code that was in analysis.  Backup is now done by the routines in OFile.
This routine is now a little more flexible in that you can specify the string
in the backup name.
---
 ...1.reference => analysis.0.histo.reference} |  0
 src/analysis/Analysis.cpp                     | 27 +++----------------
 src/analysis/Analysis.h                       |  2 --
 src/analysis/Histogram.cpp                    | 11 ++++----
 src/tools/OFile.cpp                           |  4 +--
 src/tools/OFile.h                             |  2 +-
 6 files changed, 12 insertions(+), 34 deletions(-)
 rename regtest/analysis/rt43/{histo.1.reference => analysis.0.histo.reference} (100%)

diff --git a/regtest/analysis/rt43/histo.1.reference b/regtest/analysis/rt43/analysis.0.histo.reference
similarity index 100%
rename from regtest/analysis/rt43/histo.1.reference
rename to regtest/analysis/rt43/analysis.0.histo.reference
diff --git a/src/analysis/Analysis.cpp b/src/analysis/Analysis.cpp
index 606992bcf..e4ab30dc8 100644
--- a/src/analysis/Analysis.cpp
+++ b/src/analysis/Analysis.cpp
@@ -158,7 +158,6 @@ old_norm(0.0)
           // Setup the restart file (append mode)
           if( write_chq ) rfile.open( filename.c_str() );  // In append mode automatically because of restart
           // Run the analysis if we stoped in the middle of it last time
-          if( idata==ndata ) runAnalysis();
           log.printf("  restarting analysis with %u points read from restart file\n",idata);
       } else if( write_chq ){
           // Setup the restart file (delete any old one)
@@ -200,6 +199,9 @@ void Analysis::prepare(){
 void Analysis::calculate(){
   // Don't store the first step (also don't store if we are getting data from elsewhere)
   if( getStep()==0 || reusing_data ) return;
+  // This is used when we have a full quota of data from the first run
+  if( idata==logweights.size() ) return; 
+
   // Retrieve the bias
   double bias=0.0; for(unsigned i=0;i<biases.size();++i) bias+=biases[i]->get();
 
@@ -327,28 +329,5 @@ void Analysis::runFinalJobs() {
   runAnalysis(); 
 }
 
-std::string Analysis::saveResultsFromPreviousAnalyses( const std::string & filename ){
-  FILE* ff=std::fopen( filename.c_str() ,"r");
-  // Perhaps replace this with an warning and a backup at some stage
-  if(ff && !firstAnalysisDone) error("found file named " + filename + " from previous calculation");
-  FILE* fff=NULL; std::string num, backup;
-  if( ff ){
-     FILE* fff=NULL;
-     for(unsigned i=1;;i++){
-         Tools::convert(i,num);
-         backup=filename + "." + num;
-         fff=std::fopen(backup.c_str(),"r");
-         if(!fff) break;
-     }   
-     int check=rename(filename.c_str(), backup.c_str() );
-     plumed_massert(check==0,"renaming " + filename + " to " + backup + " failed");
-  } else {
-     return "";
-  }
-  if(ff) fclose(ff);
-  if(fff) fclose(fff); 
-  return backup; 
-} 
-
 }
 }
diff --git a/src/analysis/Analysis.h b/src/analysis/Analysis.h
index 39b27668e..6358c1adb 100644
--- a/src/analysis/Analysis.h
+++ b/src/analysis/Analysis.h
@@ -100,8 +100,6 @@ protected:
   double getNormalization() const;
 /// Are we analyzing each data block separately (if we are not this also returns the old normalization )
   bool usingMemory() const; 
-/// Save the results in files from previous runs of the analysis algorithm
-  std::string saveResultsFromPreviousAnalyses( const std::string & filename );
 /// Convert the stored log weights to proper weights
   void finalizeWeights( const bool& ignore_weights );
 /// Overwrite ActionWithArguments getArguments() so that we don't return
diff --git a/src/analysis/Histogram.cpp b/src/analysis/Histogram.cpp
index 16b909bfe..14a672d2c 100644
--- a/src/analysis/Histogram.cpp
+++ b/src/analysis/Histogram.cpp
@@ -145,7 +145,7 @@ gbin(getNumberOfArguments())
 
 void Histogram::performAnalysis(){
   // Back up old histogram files
-  std::string oldfname=saveResultsFromPreviousAnalyses( gridfname );
+//  std::string oldfname=saveResultsFromPreviousAnalyses( gridfname );
 
   // Get pbc stuff for grid
   std::vector<bool> pbc; std::string dmin,dmax;
@@ -153,9 +153,10 @@ void Histogram::performAnalysis(){
      pbc.push_back( getPeriodicityInformation(i,dmin,dmax) );
      if(pbc[i]){ Tools::convert(dmin,gmin[i]); Tools::convert(dmax,gmax[i]); }
   }
-  Grid* gg; 
-  if( oldfname.length()>0 && usingMemory() ){
-      IFile oldf; oldf.link(*this); oldf.open(oldfname);
+
+  Grid* gg; IFile oldf; oldf.link(*this); 
+  if( usingMemory() && oldf.FileExist(gridfname) ){
+      oldf.open(gridfname);
       gg = Grid::create( "probs", getArguments(), oldf, gmin, gmax, gbin, false, false, false );
       oldf.close();
   } else {
@@ -175,7 +176,7 @@ void Histogram::performAnalysis(){
 
   // Write the grid to a file
   OFile gridfile; gridfile.link(*this); 
-  gridfile.open( gridfname ); gg->writeToFile( gridfile );
+  gridfile.open( gridfname, "analysis" ); gg->writeToFile( gridfile );
   // Close the file 
   gridfile.close(); delete gg;
 }
diff --git a/src/tools/OFile.cpp b/src/tools/OFile.cpp
index 28caee7c6..569395d69 100644
--- a/src/tools/OFile.cpp
+++ b/src/tools/OFile.cpp
@@ -210,7 +210,7 @@ OFile& OFile::printField(){
   return *this;
 }
 
-OFile& OFile::open(const std::string&path){
+OFile& OFile::open(const std::string&path, const std::string& backstring ){
   plumed_assert(!cloned);
   eof=false;
   err=false;
@@ -236,7 +236,7 @@ OFile& OFile::open(const std::string&path){
            std::string num;
            Tools::convert(i,num);
            if(i>maxbackup) plumed_merror("cannot backup file "+file+" maximum number of backup is "+num+"\n");
-           backup=directory+"bck."+num+"."+file;
+           backup=directory+backstring +"."+num+"."+file;
            fff=std::fopen(backup.c_str(),"r");
            if(!fff) break;
          }
diff --git a/src/tools/OFile.h b/src/tools/OFile.h
index a1cab21d8..c3611e56e 100644
--- a/src/tools/OFile.h
+++ b/src/tools/OFile.h
@@ -131,7 +131,7 @@ public:
 /// be either opened explicitly, linked to a FILE or linked to a OFile
   OFile& link(OFile&);
 /// Opens the file using automatic append/backup
-  OFile& open(const std::string&name);
+  OFile& open(const std::string&name, const std::string& backstring="bck");
 /// Set the prefix for output.
 /// Typically "PLUMED: ". Notice that lines with a prefix cannot
 /// be parsed using fields in a IFile.
-- 
GitLab