LCOV - code coverage report
Current view: top level - ves - OutputFesBias.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 60 68 88.2 %
Date: 2024-10-18 13:59:31 Functions: 2 5 40.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2016-2021 The VES code team
       3             :    (see the PEOPLE-VES file at the root of this folder for a list of names)
       4             : 
       5             :    See http://www.ves-code.org for more information.
       6             : 
       7             :    This file is part of VES code module.
       8             : 
       9             :    The VES code module is free software: you can redistribute it and/or modify
      10             :    it under the terms of the GNU Lesser General Public License as published by
      11             :    the Free Software Foundation, either version 3 of the License, or
      12             :    (at your option) any later version.
      13             : 
      14             :    The VES code module is distributed in the hope that it will be useful,
      15             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      16             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17             :    GNU Lesser General Public License for more details.
      18             : 
      19             :    You should have received a copy of the GNU Lesser General Public License
      20             :    along with the VES code module.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : 
      23             : #include "CoeffsVector.h"
      24             : #include "VesTools.h"
      25             : #include "VesBias.h"
      26             : 
      27             : 
      28             : #include "tools/File.h"
      29             : #include "core/ActionRegister.h"
      30             : #include "core/PlumedMain.h"
      31             : 
      32             : 
      33             : namespace PLMD {
      34             : namespace ves {
      35             : 
      36             : //+PLUMEDOC VES_UTILS VES_OUTPUT_FES
      37             : /*
      38             : Tool to output biases and free energy surfaces for VES biases from previously obtained coefficients.
      39             : 
      40             : This action can be used to output to file biases and free energy surfaces for VES biases from
      41             : previously obtained coefficients. It should be used through the \ref driver and
      42             : can only be used in post processing. The VES bias needs to be defined in the
      43             : exact same way as during the simulation. At the current moment this action does
      44             : not support dynamic target distributions (e.g. well-tempered).
      45             : 
      46             : \par Examples
      47             : 
      48             : In the following input we define a VES bias and then read in the coefficient
      49             : file coeffs.input.data and output the FES and bias every 500 iterations.
      50             : 
      51             : \plumedfile
      52             : phi:   TORSION ATOMS=5,7,9,15
      53             : psi:   TORSION ATOMS=7,9,15,17
      54             : 
      55             : bf1: BF_FOURIER ORDER=5 MINIMUM=-pi MAXIMUM=pi
      56             : bf2: BF_FOURIER ORDER=5 MINIMUM=-pi MAXIMUM=pi
      57             : 
      58             : VES_LINEAR_EXPANSION ...
      59             :  ARG=phi,psi
      60             :  BASIS_FUNCTIONS=bf1,bf2
      61             :  LABEL=ves1
      62             :  GRID_BINS=100,100
      63             :  PROJ_ARG1=phi
      64             :  PROJ_ARG2=psi
      65             : ... VES_LINEAR_EXPANSION
      66             : 
      67             : VES_OUTPUT_FES ...
      68             :   BIAS=ves1
      69             :   FES_OUTPUT=500
      70             :   FES_PROJ_OUTPUT=500
      71             :   BIAS_OUTPUT=500
      72             :   COEFFS_INPUT=coeffs.input.data
      73             : ... VES_OUTPUT_FES
      74             : \endplumedfile
      75             : 
      76             : The header of coeffs.input.data should look like the following:
      77             : 
      78             : \auxfile{coeffs.input.data}
      79             : #! FIELDS idx_phi idx_psi ves1.coeffs ves1.aux_coeffs index
      80             : #! SET time 100.000000
      81             : #! SET iteration  10
      82             : #! SET type LinearBasisSet
      83             : #! SET ndimensions  2
      84             : #! SET ncoeffs_total  121
      85             : #! SET shape_phi  11
      86             : #! SET shape_psi  11
      87             : \endauxfile
      88             : 
      89             : This input should be run through the driver by using a command similar to the
      90             : following one where the trajectory/configuration file configuration.gro is needed to
      91             : correctly define the CVs
      92             : \verbatim
      93             : plumed driver --plumed plumed.dat --igro configuration.gro
      94             : \endverbatim
      95             : 
      96             : */
      97             : //+ENDPLUMEDOC
      98             : 
      99             : class OutputFesBias : public Action {
     100             : 
     101             : public:
     102             :   static void registerKeywords(Keywords&);
     103             :   explicit OutputFesBias(const ActionOptions&);
     104           0 :   void update() override {}
     105           0 :   void calculate() override {}
     106           0 :   void apply() override {}
     107             : };
     108             : 
     109             : 
     110             : PLUMED_REGISTER_ACTION(OutputFesBias,"VES_OUTPUT_FES")
     111             : 
     112             : 
     113           5 : void OutputFesBias::registerKeywords(Keywords& keys) {
     114          10 :   keys.add("compulsory","BIAS","the label of the VES bias for to output the free energy surfaces and the bias files");
     115          10 :   keys.add("compulsory","COEFFS_INPUT","the name of input coefficient file");
     116          10 :   keys.add("optional","BIAS_OUTPUT","how often the bias(es) should be written out to file. Note that the value is given in terms of coefficient iterations.");
     117          10 :   keys.add("optional","FES_OUTPUT","how often the FES(s) should be written out to file. Note that the value is given in terms of coefficient iterations.");
     118          10 :   keys.add("optional","FES_PROJ_OUTPUT","how often the projections of the FES(s) should be written out to file. Note that the value is given in terms of coefficient iterations.");
     119             :   //
     120           5 : }
     121             : 
     122             : 
     123           3 : OutputFesBias::OutputFesBias(const ActionOptions&ao):
     124           3 :   Action(ao)
     125             : {
     126             : 
     127             :   std::vector<std::string> bias_labels;
     128           6 :   parseVector("BIAS",bias_labels);
     129           3 :   if(bias_labels.size()>1) {
     130           0 :     plumed_merror(getName()+" only support one VES bias");
     131             :   }
     132             : 
     133           3 :   std::string error_msg = "";
     134           3 :   std::vector<VesBias*> bias_pntrs = VesTools::getPointersFromLabels<VesBias*>(bias_labels,plumed.getActionSet(),error_msg);
     135           3 :   if(error_msg.size()>0) {plumed_merror("Error in keyword BIAS of "+getName()+": "+error_msg);}
     136             : 
     137           6 :   for(unsigned int i=0; i<bias_pntrs.size(); i++) {
     138           3 :     if(bias_pntrs[i]->numberOfCoeffsSets()>1) {
     139           0 :       plumed_merror(getName()+" at the moment supports only VES biases with a single coefficient set");
     140             :     }
     141             :   }
     142             : 
     143             :   std::vector<std::string> coeffs_fnames;
     144           6 :   parseVector("COEFFS_INPUT",coeffs_fnames);
     145           3 :   if(coeffs_fnames.size()!=bias_pntrs.size()) {
     146           0 :     plumed_merror(getName()+": there have to be as many coefficient file given in COEFFS_INPUT as VES biases given in BIAS");
     147             :   }
     148             : 
     149           3 :   unsigned int bias_output_stride = 0;
     150           3 :   parse("BIAS_OUTPUT",bias_output_stride);
     151             : 
     152           3 :   unsigned int fes_output_stride = 0;
     153           3 :   parse("FES_OUTPUT",fes_output_stride);
     154             : 
     155           3 :   unsigned int fesproj_output_stride = 0;
     156           3 :   parse("FES_PROJ_OUTPUT",fesproj_output_stride);
     157             : 
     158           3 :   if(bias_output_stride == 0 && fes_output_stride == 0 && fesproj_output_stride == 0) {
     159           0 :     plumed_merror(getName()+": you are not telling the action to do anything, you need to use one of the keywords BIAS_OUTPUT, FES_OUTPUT, or FES_PROJ_OUTPUT");
     160             :   }
     161             : 
     162           3 :   checkRead();
     163             : 
     164           6 :   for(unsigned int i=0; i<bias_pntrs.size(); i++) {
     165             : 
     166           3 :     if(bias_pntrs[i]->dynamicTargetDistribution()) {
     167           0 :       plumed_merror(getName()+" does not support dynamic target distributions at the moment");
     168             :     }
     169             : 
     170           3 :     if(bias_pntrs[i]->isStaticTargetDistFileOutputActive()) {
     171           2 :       bias_pntrs[i]->setupTargetDistFileOutput();
     172           2 :       bias_pntrs[i]->writeTargetDistToFile();
     173           2 :       bias_pntrs[i]->setupTargetDistProjFileOutput();
     174           2 :       bias_pntrs[i]->writeTargetDistProjToFile();
     175             :     }
     176             : 
     177             : 
     178           3 :     if(bias_output_stride>0) {
     179           3 :       bias_pntrs[i]->enableBiasFileOutput();
     180           3 :       bias_pntrs[i]->setupBiasFileOutput();
     181             :     }
     182             : 
     183           3 :     if(fes_output_stride>0) {
     184           3 :       bias_pntrs[i]->enableFesFileOutput();
     185           3 :       bias_pntrs[i]->setupFesFileOutput();
     186             :     }
     187             : 
     188           3 :     if(fesproj_output_stride>0) {
     189           1 :       bias_pntrs[i]->enableFesProjFileOutput();
     190           1 :       bias_pntrs[i]->setupFesProjFileOutput();
     191             :     }
     192             : 
     193           3 :     bias_pntrs[i]->enableIterationNumberInFilenames();
     194             : 
     195           3 :     IFile ifile;
     196           3 :     ifile.open(coeffs_fnames[i]);
     197             : 
     198          39 :     while(ifile) {
     199             : 
     200          36 :       bias_pntrs[i]->resetBiasFileOutput();
     201          36 :       bias_pntrs[i]->resetFesFileOutput();
     202             : 
     203          72 :       if(bias_pntrs[i]->getCoeffsPntrs()[0]->readOneSetFromFile(ifile)>0) {
     204          33 :         unsigned int iteration = bias_pntrs[i]->getCoeffsPntrs()[0]->getIterationCounter();
     205             : 
     206          33 :         if(bias_output_stride>0 && iteration%bias_output_stride==0) {
     207           8 :           bias_pntrs[i]->writeBiasToFile();
     208             :         }
     209             : 
     210          33 :         if(fes_output_stride>0 && iteration%fes_output_stride==0) {
     211           8 :           bias_pntrs[i]->writeFesToFile();
     212             :         }
     213             : 
     214          33 :         if(fesproj_output_stride>0 && iteration%fesproj_output_stride==0) {
     215           3 :           bias_pntrs[i]->writeFesProjToFile();
     216             :         }
     217             : 
     218             :       }
     219             : 
     220             :     }
     221             : 
     222           3 :   }
     223             : 
     224           3 :   log.printf("Stopping");
     225           3 :   plumed.stop();
     226           6 : }
     227             : 
     228             : 
     229             : }
     230             : }

Generated by: LCOV version 1.16