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