24 #include "tools/Tools.h"
25 #include "core/Action.h"
26 #include "core/ActionRegister.h"
27 #include "core/PlumedMain.h"
28 #include "tools/Communicator.h"
29 #include "tools/Random.h"
34 #include "tools/File.h"
35 #include "core/Value.h"
36 #include "tools/Matrix.h"
191 static void registerKeywords(
Keywords& keys );
194 string description()
const;
197 static bool findCvsAndPeriodic(std::string filename, std::vector< std::vector <std::string> > &cvs,std::vector<std::string> &pmin,std::vector<std::string> &pmax,
bool &multivariate);
200 void CLToolSumHills::registerKeywords(
Keywords& keys ){
201 CLTool::registerKeywords( keys );
202 keys.
addFlag(
"--help-debug",
false,
"print special options that can be used to create regtests");
203 keys.
add(
"optional",
"--hills",
"specify the name of the hills file");
204 keys.
add(
"optional",
"--histo",
"specify the name of the file for histogram a colvar/hills file is good");
205 keys.
add(
"optional",
"--stride",
"specify the stride for integrating hills file (default 0=never)");
206 keys.
add(
"optional",
"--min",
"the lower bounds for the grid");
207 keys.
add(
"optional",
"--max",
"the upper bounds for the grid");
208 keys.
add(
"optional",
"--bin",
"the number of bins for the grid");
209 keys.
add(
"optional",
"--idw",
"specify the variables to be integrated (default is all)");
210 keys.
add(
"optional",
"--outfile",
"specify the outputfile for sumhills");
211 keys.
add(
"optional",
"--outhisto",
"specify the outputfile for the histogram");
212 keys.
add(
"optional",
"--kt",
"specify temperature for integrating out variables");
213 keys.
add(
"optional",
"--sigma",
" a vector that specify the sigma for binning (only needed when doing histogram ");
214 keys.
addFlag(
"--negbias",
false,
" print the negative bias instead of the free energy (only needed with welltempered runs and flexible hills) ");
215 keys.
addFlag(
"--nohistory",
false,
" to be used with --stride: it splits the bias/histogram in pieces without previous history ");
216 keys.
add(
"optional",
"--fmt",
"specify the output format");
230 vector<string> hillsFiles;
234 vector<string> histoFiles;
238 plumed_massert(dohisto || dohills,
"you should use --histo or/and --hills command");
240 vector< vector<string> > vcvs;
241 vector<string> vpmin;
242 vector<string> vpmax;
249 vector< vector<string> > hcvs;
250 vector<string> hpmin;
251 vector<string> hpmax;
254 vector<std::string> sigma;
259 if(sigma.size()!=hcvs.size())plumed_merror(
"you should define --sigma vector when using histogram");
262 if(dohisto && dohills){
263 plumed_massert(vcvs==hcvs,
"variables for histogram and bias should have the same labels");
264 plumed_massert(hpmin==vpmin,
"variables for histogram and bias should have the same min for periodicity");
265 plumed_massert(hpmax==vpmax,
"variables for histogram and bias should have the same max for periodicity");
270 vector< vector<string> > cvs;
287 unsigned grid_check=0;
288 vector<std::string> gmin(cvs.size());
290 if(gmin.size()!=cvs.size() && gmin.size()!=0) plumed_merror(
"not enough values for --min");
293 vector<std::string> gmax(cvs.size() );
295 if(gmax.size()!=cvs.size() && gmax.size()!=0) plumed_merror(
"not enough values for --max");
298 vector<std::string> gbin(cvs.size());
299 bool grid_has_bin; grid_has_bin=
false;
301 if(gbin.size()!=cvs.size() && gbin.size()!=0) plumed_merror(
"not enough values for --bin");
306 plumed_massert( gmin.size()==gmax.size() && (gmin.size()==0 || gmin.size()==cvs.size() ) ,
"you should specify --min and --max together with same number of components");
316 plumed.
cmd(
"init",&nn);
317 vector <bool> isdone(cvs.size(),
false);
318 for(
int i=0;i<cvs.size();i++){
321 std::vector<std::string> actioninput;
322 std::vector <unsigned> inds;
323 actioninput.push_back(
"FAKE");
324 actioninput.push_back(
"ATOMS=1");
325 actioninput.push_back(
"LABEL="+cvs[i][0]);
326 std::vector<std::string> comps, periods;
327 if(cvs[i].size()>1){comps.push_back(cvs[i][1]);inds.push_back(i);}
328 periods.push_back(pmin[i]);periods.push_back(pmax[i]);
329 for(
unsigned j=i+1;j<cvs.size();j++){
330 if(cvs[i][0]==cvs[j][0] && !isdone[j]){
331 if(cvs[i].size()==1 || cvs[j].size()==1 )plumed_merror(
"you cannot have twice the same label and no components ");
333 comps.push_back(cvs[j][1]);
334 periods.push_back(pmin[j]);periods.push_back(pmax[j]);
335 isdone[j]=
true; inds.push_back(j);
344 for(
unsigned i=0;i<comps.size()-1;i++)addme+=comps[i]+
",";
346 actioninput.push_back(addme);
350 for(
unsigned j=0;j<periods.size()-1;j++){
351 addme+=periods[j]+
",";
353 addme+=periods.back();
354 actioninput.push_back(addme);
355 for(
unsigned j=0;j<inds.size();j++){
356 unsigned jj;jj=inds[j];
360 if(pmin[jj]!=
"none"){
364 plumed_merror(
"Periodicity issue : GRID_MIN value ( "+gmin[jj]+
" ) is less than periodicity in HILLS file in "+cvs[jj][0]+
" ( "+pmin[jj]+
" ) ");
367 if(pmax[jj]!=
"none"){
371 plumed_merror(
"Periodicity issue : GRID_MAX value ( "+gmax[jj]+
" ) is more than periodicity in HILLS file in "+cvs[jj][0]+
" ( "+pmax[jj]+
" ) ");
384 unsigned ncv=cvs.size();
385 std::vector<std::string> actioninput;
386 vector<std::string> idw;
389 for(
unsigned i=0;i<idw.size();i++){
391 for(
unsigned j=0;j<cvs.size();j++){
393 if(idw[i]==cvs[j][0]+
"."+cvs[j][1])found=
true;
395 if(idw[i]==cvs[j][0])found=
true;
398 if(!found)plumed_merror(
"variable "+idw[i]+
" is not found in the bunch of cvs: revise your --idw option" );
400 plumed_massert( idw.size()<=cvs.size() ,
"the number of variables to be integrated should be at most equal to the total number of cvs ");
404 std::string kt; kt=std::string(
"1.");
405 if ( dohisto || idw.size()!=0 ) {
406 plumed_massert(
parse(
"--kt",kt),
"if you make a dimensionality reduction (--idw) or a histogram (--histo) then you need to define --kt ");
411 actioninput.push_back(
"FUNCSUMHILLS");
412 actioninput.push_back(
"ISCLTOOL");
416 if(
parse(
"--outfile",outfile)){
417 actioninput.push_back(
"OUTHILLS="+outfile);
419 std::string outhisto;
420 if(
parse(
"--outhisto",outhisto)){
421 actioninput.push_back(
"OUTHISTO="+outhisto);
426 for(
unsigned i=0;i<(ncv-1);i++){
427 if(cvs[i].size()==1){
428 addme+=std::string(cvs[i][0])+
",";
430 addme+=std::string(cvs[i][0])+
"."+std::string(cvs[i][1])+
",";
433 if(cvs[ncv-1].size()==1){
434 addme+=std::string(cvs[ncv-1][0]);
436 addme+=std::string(cvs[ncv-1][0])+
"."+std::string(cvs[ncv-1][1]);
438 actioninput.push_back(addme);
443 addme=
"HILLSFILES=";
for(
unsigned i=0;i<hillsFiles.size()-1;i++)addme+=hillsFiles[i]+
","; addme+=hillsFiles[hillsFiles.size()-1];
444 actioninput.push_back(addme);
448 addme=
"GRID_MAX=";
for(
unsigned i=0;i<(ncv-1);i++)addme+=gmax[i]+
","; addme+=gmax[ncv-1];
449 actioninput.push_back(addme);
450 addme=
"GRID_MIN=";
for(
unsigned i=0;i<(ncv-1);i++)addme+=gmin[i]+
","; addme+=gmin[ncv-1];
451 actioninput.push_back(addme);
454 addme=
"GRID_BIN=";
for(
unsigned i=0;i<(ncv-1);i++)addme+=gbin[i]+
","; addme+=gbin[ncv-1];
455 actioninput.push_back(addme);
461 std::string stride; stride=
"";
462 if(
parse(
"--stride",stride)){
463 actioninput.push_back(
"INITSTRIDE="+stride);
467 actioninput.push_back(
"NOHISTORY");
472 for(
unsigned i=0;i<idw.size()-1;i++){addme+=idw[i]+
",";}
474 actioninput.push_back(addme);
476 if(idw.size()!=0 || dohisto){
477 actioninput.push_back(
"KT="+kt);
480 addme=
"HISTOFILES=";
for(
unsigned i=0;i<histoFiles.size()-1;i++){addme+=histoFiles[i]+
",";}addme+=histoFiles[histoFiles.size()-1];
481 actioninput.push_back(addme);
484 for(
unsigned i=0;i<sigma.size()-1;i++){addme+=sigma[i]+
",";}
486 actioninput.push_back(addme);
492 actioninput.push_back(
"NEGBIAS");
496 std::string fmt;fmt=
"";
498 if(fmt!=
"")actioninput.push_back(
"FMT="+fmt);
509 bool CLToolSumHills::findCvsAndPeriodic(std::string filename, std::vector< std::vector<std::string> > &cvs, std::vector<std::string> &pmin,std::vector<std::string> &pmax,
bool &multivariate){
512 std::vector<std::string> fields;
514 cvs.clear(); pmin.clear(); pmax.clear();
515 ifile.
open(filename);
517 size_t founds,foundm,foundp;
518 bool before_sigma=
true;
519 for(
int i=0;i<fields.size();i++){
522 founds=fields[i].find(
"sigma_", pos) ;
523 foundm=fields[i].find(
"min_", pos) ;
524 foundp=fields[i].find(
"max_", pos) ;
525 if (founds!=std::string::npos || foundm!=std::string::npos || foundp!=std::string::npos )before_sigma=
false;
528 found=fields[i].find(
"time", pos);
529 if( found==std::string::npos && before_sigma){
531 size_t dot=fields[i].find_first_of(
'.');
532 std::vector<std::string> ss;
534 if(dot!=std::string::npos){
535 std::string
a=fields[i].substr(0,dot);
536 std::string
name=fields[i].substr(dot+1);
541 std::vector<std::string> ss;
542 ss.push_back(fields[i]);
550 pmin.push_back(
"none");
551 pmax.push_back(
"none");
552 std::string mm;
if((cvs.back()).size()>1){mm=cvs.back()[0]+
"."+cvs.back()[1];}
else{mm=cvs.back()[0];}
556 pmin[pmin.size()-1]=val;
563 pmax[pmax.size()-1]=val;
574 if(sss==
"true"){ multivariate=
true;}
575 else if(sss==
"false"){ multivariate=
false;}
bool FieldExist(const std::string &s)
Check if a field exist.
static bool initialized()
Tests if MPI library is initialized.
int main(int argc, char **argv)
This main uses only the interface published in Plumed.h.
MPI_Comm & Get_comm()
Reference to MPI communicator.
Class containing wrappers to MPI.
void add(const std::string &t, const std::string &k, const std::string &d)
Add a new keyword of type t with name k and description d.
This class holds the keywords and their documentation.
IFile & open(const std::string &name)
Opens the file.
void readInputWords(const std::vector< std::string > &str)
Read an input string.
void close()
Closes the file Should be used only for explicitely opened files.
IFile & scanField(const std::string &, double &)
Read a double field.
bool FileExist(const std::string &path)
Check if the file exists.
void cmd(const std::string &key, void *val=NULL)
cmd method, accessible with standard Plumed.h interface.
IFile & scanFieldList(std::vector< std::string > &)
Gets the list of all fields.
void addFlag(const std::string &k, const bool def, const std::string &d)
Add a falg with name k that is by default on if def is true and off if def is false. d should provide a description of the flag.
void allowIgnoredFields()
Allow some of the fields in the input to be ignored.