LCOV - code coverage report
Current view: top level - drr - drrtool.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 72 76 94.7 %
Date: 2025-03-25 09:33:27 Functions: 12 12 100.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :     Copyright (c) 2017 of Haochuan Chen (excluding colvar_UIestimator.h)
       3             :     Copyright (c) 2017 of Haohao Fu (colvar_UIestimator.h)
       4             : 
       5             :     This program is free software: you can redistribute it and/or modify
       6             :     it under the terms of the GNU Lesser General Public License as published
       7             :     by the Free Software Foundation, either version 3 of the License, or
       8             :     (at your option) any later version.
       9             : 
      10             :     This program is distributed in the hope that it will be useful,
      11             :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             :     GNU Lesser General Public License for more details.
      14             : 
      15             :     You should have received a copy of the GNU Lesser General Public License
      16             :     along with this program.  If not, see <http://www.gnu.org/licenses/>.
      17             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      18             : #ifdef __PLUMED_HAS_BOOST_SERIALIZATION
      19             : #include "cltools/CLTool.h"
      20             : #include "core/CLToolRegister.h"
      21             : #include "config/Config.h"
      22             : #include "core/ActionRegister.h"
      23             : #include "DRR.h"
      24             : #include "tools/Tools.h"
      25             : #include "tools/Units.h"
      26             : #include <boost/archive/binary_iarchive.hpp>
      27             : #include <boost/archive/binary_oarchive.hpp>
      28             : #include <boost/serialization/vector.hpp>
      29             : #include <cstdlib>
      30             : #include <fstream>
      31             : #include <iostream>
      32             : #include <string>
      33             : 
      34             : using namespace PLMD;
      35             : using namespace cltools;
      36             : 
      37             : namespace PLMD {
      38             : namespace drr {
      39             : 
      40             : //+PLUMEDOC EABFMOD_TOOLS drr_tool
      41             : /*
      42             :  - Extract .grad and .count files from the binary output .drrstate
      43             :  - Merge windows
      44             : 
      45             : \par Examples
      46             : 
      47             : The following command will extract .grad and .count files.
      48             : \verbatim
      49             : plumed drr_tool --extract eabf.drrstate
      50             : \endverbatim
      51             : 
      52             : The following command will merge windows of two .drrstate file, and output the
      53             : .grad and .count files.
      54             : \verbatim
      55             : plumed drr_tool --merge win1.drrstate,win2.drrstate
      56             : \endverbatim
      57             : 
      58             : After getting the .grad and .count file, you can do numerical integration by
      59             : using abf_integrate tool from
      60             : https://github.com/Colvars/colvars/tree/master/colvartools
      61             : \verbatim
      62             : abf_integrate eabf.czar.grad
      63             : \endverbatim
      64             : \note
      65             : The abf_integrate in colvartools is in kcal/mol, so it may be better to use --units kcal/mol when running drr_tool
      66             : 
      67             : */
      68             : //+ENDPLUMEDOC
      69             : 
      70             : using std::vector;
      71             : using std::string;
      72             : 
      73             : class drrtool : public CLTool {
      74             : public:
      75             :   static void registerKeywords(Keywords &keys);
      76             :   explicit drrtool(const CLToolOptions &co);
      77             :   int main(FILE *in, FILE *out, Communicator &pc);
      78             :   void extractdrr(const vector<string> &filename);
      79             :   void mergewindows(const vector<string> &filename, string outputname);
      80             :   void calcDivergence(const vector<string> &filename, const string &fmt);
      81           5 :   string description() const {
      82           5 :     return "Extract or merge the drrstate files.";
      83             :   }
      84             : 
      85             : private:
      86             :   bool verbosity;
      87             :   Units units;
      88             :   const string suffix{".drrstate"};
      89             : };
      90             : 
      91       16264 : PLUMED_REGISTER_CLTOOL(drrtool, "drr_tool")
      92             : 
      93        5418 : void drrtool::registerKeywords(Keywords &keys) {
      94        5418 :   CLTool::registerKeywords(keys);
      95        5418 :   keys.add("optional", "--extract", "Extract drrstate file(s)");
      96        5418 :   keys.add("optional", "--merge", "Merge eABF windows");
      97        5418 :   keys.add("optional", "--merge_output", "The output filename of the merged result");
      98        5418 :   keys.add("optional", "--divergence", "Calculate divergence of gradient field (experimental)");
      99        5418 :   keys.add("optional","--dump-fmt","( default=%%f ) the format to use to dump the output");
     100        5418 :   keys.add("compulsory","--units","kj/mol","the units of energy can be kj/mol, kcal/mol, j/mol, eV or the conversion factor from kj/mol");
     101        5418 :   keys.addFlag("-v", false, "Verbose output");
     102        5418 : }
     103             : 
     104          10 : drrtool::drrtool(const CLToolOptions &co) : CLTool(co) {
     105          10 :   inputdata = commandline;
     106          10 :   verbosity = false;
     107          10 : }
     108             : 
     109           5 : int drrtool::main(FILE *in, FILE *out, Communicator &pc) {
     110          10 :   parseFlag("-v", verbosity);
     111             :   vector<string> stateFilesToExtract;
     112             :   string unitname;
     113           5 :   parse("--units",unitname);
     114           5 :   units.setEnergy( unitname );
     115           5 :   std::string dumpFmt("%.9f");;
     116           5 :   parse("--dump-fmt", dumpFmt);
     117           5 :   bool doextract = parseVector("--extract", stateFilesToExtract);
     118           5 :   if (doextract) {
     119           2 :     extractdrr(stateFilesToExtract);
     120             :   }
     121             :   vector<string> stateFilesToMerge;
     122           5 :   bool domerge = parseVector("--merge", stateFilesToMerge);
     123           5 :   if (domerge) {
     124             :     string merge_outputname;
     125           2 :     parse("--merge_output", merge_outputname);
     126           4 :     mergewindows(stateFilesToMerge, merge_outputname);
     127             :   }
     128             :   vector<string> stateFilesToDivergence;
     129           5 :   bool dodivergence = parseVector("--divergence", stateFilesToDivergence);
     130           5 :   if (dodivergence) {
     131           1 :     calcDivergence(stateFilesToDivergence, dumpFmt);
     132             :   }
     133           5 :   return 0;
     134          10 : }
     135             : 
     136           2 : void drrtool::extractdrr(const vector<string> &filename) {
     137           2 :   #pragma omp parallel for
     138             :   for (size_t j = 0; j < filename.size(); ++j) {
     139             :     std::ifstream in;
     140             :     in.open(filename[j]);
     141             :     boost::archive::binary_iarchive ia(in);
     142             :     long long int step;
     143             :     vector<double> fict;
     144             :     vector<double> vfict;
     145             :     vector<double> vfict_laststep;
     146             :     vector<double> ffict;
     147             :     ABF abfgrid;
     148             :     CZAR czarestimator;
     149             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     150             :        czarestimator;
     151             :     in.close();
     152             :     abfgrid.setOutputUnit(units.getEnergy());
     153             :     czarestimator.setOutputUnit(units.getEnergy());
     154             :     if (verbosity) {
     155             :       std::cout << "Output units factor: " << units.getEnergy() << '\n';
     156             :       std::cout << "Dumping information of extended variables..." << '\n';
     157             :       std::cout << "Step: " << step << '\n';
     158             :       for (size_t i = 0; i < fict.size(); ++i) {
     159             :         std::cout << "Dimension[" << i + 1 << "]:\n"
     160             :                   << "  Coordinate: " << fict[i] << '\n'
     161             :                   << "  Velocity: " << vfict[i] << '\n'
     162             :                   << "  Velocity(laststep): " << vfict_laststep[i] << '\n'
     163             :                   << "  Force: " << ffict[i] << '\n';
     164             :       }
     165             :       std::cout << "Dumping counts and gradients from grids..." << '\n';
     166             :     }
     167             :     string outputname(filename[j]);
     168             :     outputname.resize(outputname.length() - suffix.length());
     169             :     if (verbosity) {
     170             :       std::cout << "Writing ABF(naive) estimator files..." << '\n';
     171             :     }
     172             :     abfgrid.writeAll(outputname);
     173             :     if (verbosity) {
     174             :       std::cout << "Writing CZAR estimator files..." << '\n';
     175             :     }
     176             :     czarestimator.writeAll(outputname);
     177             :     czarestimator.writeZCountZGrad(outputname);
     178             :   }
     179           2 : }
     180             : 
     181           2 : void drrtool::mergewindows(const vector<string> &filename, string outputname) {
     182           2 :   if (filename.size() < 2) {
     183           0 :     std::cerr << "ERROR! You need at least two .drrstate file to merge windows!" << std::endl;
     184           0 :     std::abort();
     185             :   }
     186             :   // Read grid into abfs and czars;
     187             :   vector<ABF> abfs;
     188             :   vector<CZAR> czars;
     189           6 :   for (auto it_fn = filename.begin(); it_fn != filename.end(); ++it_fn) {
     190           4 :     std::ifstream in;
     191           4 :     in.open((*it_fn));
     192           4 :     boost::archive::binary_iarchive ia(in);
     193             :     long long int step;
     194             :     vector<double> fict;
     195             :     vector<double> vfict;
     196             :     vector<double> vfict_laststep;
     197             :     vector<double> ffict;
     198             :     ABF abfgrid;
     199             :     CZAR czarestimator;
     200             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     201             :        czarestimator;
     202           4 :     abfgrid.setOutputUnit(units.getEnergy());
     203             :     czarestimator.setOutputUnit(units.getEnergy());
     204           4 :     abfs.push_back(abfgrid);
     205           4 :     czars.push_back(czarestimator);
     206           4 :     in.close();
     207           8 :   }
     208           2 :   CZAR cmerged = CZAR::mergewindow(czars[0], czars[1]);
     209           2 :   ABF amerged = ABF::mergewindow(abfs[0], abfs[1]);
     210           2 :   for (size_t i = 2; i < czars.size(); ++i) {
     211           0 :     cmerged = CZAR::mergewindow(cmerged, czars[i]);
     212           0 :     amerged = ABF::mergewindow(amerged, abfs[i]);
     213             :   }
     214           2 :   if (outputname.empty()) {
     215             :     // Generate new file name for merged grad and count
     216           1 :     vector<string> tmp_name = filename;
     217           1 :     std::transform(std::begin(tmp_name), std::end(tmp_name), std::begin(tmp_name),
     218           2 :     [&](const string & s) {
     219           2 :       return s.substr(0, s.find(suffix));
     220             :     });
     221           2 :     outputname = std::accumulate(std::begin(tmp_name), std::end(tmp_name), string(""),
     222           2 :     [](const string & a, const string & b) {
     223           4 :       return a + b + "+";
     224           1 :     });
     225           1 :     outputname.resize(outputname.size() - 1);
     226             :     std::cerr << "You have not specified an output filename for the merged"
     227           1 :               << " result, so the default name \"" + outputname
     228           2 :               << "\" is used here, which may yield unexpected behavior.\n";
     229           1 :   }
     230           2 :   cmerged.writeAll(outputname);
     231           2 :   cmerged.writeZCountZGrad(outputname);
     232           2 :   amerged.writeAll(outputname);
     233           4 : }
     234             : 
     235           1 : void drrtool::calcDivergence(const vector<string> &filename, const string &dumpFmt) {
     236           1 :   #pragma omp parallel for
     237             :   for (size_t j = 0; j < filename.size(); ++j) {
     238             :     std::ifstream in;
     239             :     in.open(filename[j]);
     240             :     boost::archive::binary_iarchive ia(in);
     241             :     long long int step;
     242             :     vector<double> fict;
     243             :     vector<double> vfict;
     244             :     vector<double> vfict_laststep;
     245             :     vector<double> ffict;
     246             :     ABF abfgrid;
     247             :     CZAR czarestimator;
     248             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     249             :        czarestimator;
     250             :     in.close();
     251             :     abfgrid.setOutputUnit(units.getEnergy());
     252             :     czarestimator.setOutputUnit(units.getEnergy());
     253             :     if (verbosity) {
     254             :       std::cout << "Output units factor: " << units.getEnergy() << '\n';
     255             :       std::cout << "Dumping information of extended variables..." << '\n';
     256             :       std::cout << "Step: " << step << '\n';
     257             :       for (size_t i = 0; i < fict.size(); ++i) {
     258             :         std::cout << "Dimension[" << i + 1 << "]:\n"
     259             :                   << "  Coordinate: " << fict[i] << '\n'
     260             :                   << "  Velocity: " << vfict[i] << '\n'
     261             :                   << "  Velocity(laststep): " << vfict_laststep[i] << '\n'
     262             :                   << "  Force: " << ffict[i] << '\n';
     263             :       }
     264             :       std::cout << "Dumping counts and gradients from grids..." << '\n';
     265             :     }
     266             :     string outputname(filename[j]);
     267             :     outputname.resize(outputname.length() - suffix.length());
     268             :     abfgrid.writeDivergence(outputname, dumpFmt);
     269             :     czarestimator.writeDivergence(outputname, dumpFmt);
     270             :   }
     271           1 : }
     272             : 
     273             : } // End of namespace
     274             : }
     275             : 
     276             : #endif

Generated by: LCOV version 1.16