LCOV - code coverage report
Current view: top level - drr - drrtool.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 69 73 94.5 %
Date: 2026-03-30 13:16:06 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 "cltools/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);
      81           4 :   string description() const {
      82           4 :     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       13794 : PLUMED_REGISTER_CLTOOL(drrtool, "drr_tool")
      92             : 
      93        4595 : void drrtool::registerKeywords(Keywords &keys) {
      94        4595 :   CLTool::registerKeywords(keys);
      95        9190 :   keys.add("optional", "--extract", "Extract drrstate file(s)");
      96        9190 :   keys.add("optional", "--merge", "Merge eABF windows");
      97        9190 :   keys.add("optional", "--merge_output", "The output filename of the merged result");
      98        9190 :   keys.add("optional", "--divergence", "Calculate divergence of gradient field (experimental)");
      99        9190 :   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");
     100        9190 :   keys.addFlag("-v", false, "Verbose output");
     101        4595 : }
     102             : 
     103           9 : drrtool::drrtool(const CLToolOptions &co) : CLTool(co) {
     104           9 :   inputdata = commandline;
     105           9 :   verbosity = false;
     106           9 : }
     107             : 
     108           5 : int drrtool::main(FILE *in, FILE *out, Communicator &pc) {
     109          10 :   parseFlag("-v", verbosity);
     110             :   vector<string> stateFilesToExtract;
     111             :   string unitname;
     112           5 :   parse("--units",unitname);
     113           5 :   units.setEnergy( unitname );
     114           5 :   bool doextract = parseVector("--extract", stateFilesToExtract);
     115           5 :   if (doextract) {
     116           2 :     extractdrr(stateFilesToExtract);
     117             :   }
     118             :   vector<string> stateFilesToMerge;
     119           5 :   bool domerge = parseVector("--merge", stateFilesToMerge);
     120           5 :   if (domerge) {
     121             :     string merge_outputname;
     122           2 :     parse("--merge_output", merge_outputname);
     123           4 :     mergewindows(stateFilesToMerge, merge_outputname);
     124             :   }
     125             :   vector<string> stateFilesToDivergence;
     126           5 :   bool dodivergence = parseVector("--divergence", stateFilesToDivergence);
     127           5 :   if (dodivergence) {
     128           1 :     calcDivergence(stateFilesToDivergence);
     129             :   }
     130           5 :   return 0;
     131          10 : }
     132             : 
     133           2 : void drrtool::extractdrr(const vector<string> &filename) {
     134           2 :   #pragma omp parallel for
     135             :   for (size_t j = 0; j < filename.size(); ++j) {
     136             :     std::ifstream in;
     137             :     in.open(filename[j]);
     138             :     boost::archive::binary_iarchive ia(in);
     139             :     long long int step;
     140             :     vector<double> fict;
     141             :     vector<double> vfict;
     142             :     vector<double> vfict_laststep;
     143             :     vector<double> ffict;
     144             :     ABF abfgrid;
     145             :     CZAR czarestimator;
     146             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     147             :        czarestimator;
     148             :     in.close();
     149             :     abfgrid.setOutputUnit(units.getEnergy());
     150             :     czarestimator.setOutputUnit(units.getEnergy());
     151             :     if (verbosity) {
     152             :       std::cout << "Output units factor: " << units.getEnergy() << '\n';
     153             :       std::cout << "Dumping information of extended variables..." << '\n';
     154             :       std::cout << "Step: " << step << '\n';
     155             :       for (size_t i = 0; i < fict.size(); ++i) {
     156             :         std::cout << "Dimension[" << i + 1 << "]:\n"
     157             :                   << "  Coordinate: " << fict[i] << '\n'
     158             :                   << "  Velocity: " << vfict[i] << '\n'
     159             :                   << "  Velocity(laststep): " << vfict_laststep[i] << '\n'
     160             :                   << "  Force: " << ffict[i] << '\n';
     161             :       }
     162             :       std::cout << "Dumping counts and gradients from grids..." << '\n';
     163             :     }
     164             :     string outputname(filename[j]);
     165             :     outputname.resize(outputname.length() - suffix.length());
     166             :     if (verbosity) {
     167             :       std::cout << "Writing ABF(naive) estimator files..." << '\n';
     168             :     }
     169             :     abfgrid.writeAll(outputname);
     170             :     if (verbosity) {
     171             :       std::cout << "Writing CZAR estimator files..." << '\n';
     172             :     }
     173             :     czarestimator.writeAll(outputname);
     174             :     czarestimator.writeZCountZGrad(outputname);
     175             :   }
     176           2 : }
     177             : 
     178           2 : void drrtool::mergewindows(const vector<string> &filename, string outputname) {
     179           2 :   if (filename.size() < 2) {
     180           0 :     std::cerr << "ERROR! You need at least two .drrstate file to merge windows!" << std::endl;
     181           0 :     std::abort();
     182             :   }
     183             :   // Read grid into abfs and czars;
     184             :   vector<ABF> abfs;
     185             :   vector<CZAR> czars;
     186           6 :   for (auto it_fn = filename.begin(); it_fn != filename.end(); ++it_fn) {
     187           4 :     std::ifstream in;
     188           4 :     in.open((*it_fn));
     189           4 :     boost::archive::binary_iarchive ia(in);
     190             :     long long int step;
     191             :     vector<double> fict;
     192             :     vector<double> vfict;
     193             :     vector<double> vfict_laststep;
     194             :     vector<double> ffict;
     195             :     ABF abfgrid;
     196             :     CZAR czarestimator;
     197             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     198             :        czarestimator;
     199           4 :     abfgrid.setOutputUnit(units.getEnergy());
     200             :     czarestimator.setOutputUnit(units.getEnergy());
     201           4 :     abfs.push_back(abfgrid);
     202           4 :     czars.push_back(czarestimator);
     203           4 :     in.close();
     204           8 :   }
     205           2 :   CZAR cmerged = CZAR::mergewindow(czars[0], czars[1]);
     206           2 :   ABF amerged = ABF::mergewindow(abfs[0], abfs[1]);
     207           2 :   for (size_t i = 2; i < czars.size(); ++i) {
     208           0 :     cmerged = CZAR::mergewindow(cmerged, czars[i]);
     209           0 :     amerged = ABF::mergewindow(amerged, abfs[i]);
     210             :   }
     211           2 :   if (outputname.empty()) {
     212             :     // Generate new file name for merged grad and count
     213           1 :     vector<string> tmp_name = filename;
     214           1 :     std::transform(std::begin(tmp_name), std::end(tmp_name), std::begin(tmp_name),
     215           2 :     [&](string s) {
     216           2 :       return s.substr(0, s.find(suffix));
     217             :     });
     218           2 :     outputname = std::accumulate(std::begin(tmp_name), std::end(tmp_name), string(""),
     219           2 :     [](const string & a, const string & b) {
     220           4 :       return a + b + "+";
     221           1 :     });
     222           1 :     outputname.resize(outputname.size() - 1);
     223             :     std::cerr << "You have not specified an output filename for the merged"
     224           1 :               << " result, so the default name \"" + outputname
     225           2 :               << "\" is used here, which may yield unexpected behavior.\n";
     226           1 :   }
     227           2 :   cmerged.writeAll(outputname);
     228           2 :   cmerged.writeZCountZGrad(outputname);
     229           2 :   amerged.writeAll(outputname);
     230           4 : }
     231             : 
     232           1 : void drrtool::calcDivergence(const vector<string> &filename) {
     233           1 :   #pragma omp parallel for
     234             :   for (size_t j = 0; j < filename.size(); ++j) {
     235             :     std::ifstream in;
     236             :     in.open(filename[j]);
     237             :     boost::archive::binary_iarchive ia(in);
     238             :     long long int step;
     239             :     vector<double> fict;
     240             :     vector<double> vfict;
     241             :     vector<double> vfict_laststep;
     242             :     vector<double> ffict;
     243             :     ABF abfgrid;
     244             :     CZAR czarestimator;
     245             :     ia >> step >> fict >> vfict >> vfict_laststep >> ffict >> abfgrid >>
     246             :        czarestimator;
     247             :     in.close();
     248             :     abfgrid.setOutputUnit(units.getEnergy());
     249             :     czarestimator.setOutputUnit(units.getEnergy());
     250             :     if (verbosity) {
     251             :       std::cout << "Output units factor: " << units.getEnergy() << '\n';
     252             :       std::cout << "Dumping information of extended variables..." << '\n';
     253             :       std::cout << "Step: " << step << '\n';
     254             :       for (size_t i = 0; i < fict.size(); ++i) {
     255             :         std::cout << "Dimension[" << i + 1 << "]:\n"
     256             :                   << "  Coordinate: " << fict[i] << '\n'
     257             :                   << "  Velocity: " << vfict[i] << '\n'
     258             :                   << "  Velocity(laststep): " << vfict_laststep[i] << '\n'
     259             :                   << "  Force: " << ffict[i] << '\n';
     260             :       }
     261             :       std::cout << "Dumping counts and gradients from grids..." << '\n';
     262             :     }
     263             :     string outputname(filename[j]);
     264             :     outputname.resize(outputname.length() - suffix.length());
     265             :     abfgrid.writeDivergence(outputname);
     266             :     czarestimator.writeDivergence(outputname);
     267             :   }
     268           1 : }
     269             : 
     270             : } // End of namespace
     271             : }
     272             : 
     273             : #endif

Generated by: LCOV version 1.16