LCOV - code coverage report
Current view: top level - generic - Committor.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 73 73 100.0 %
Date: 2024-10-18 13:59:31 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2013-2023 The plumed team
       3             :    (see the PEOPLE file at the root of the distribution for a list of names)
       4             : 
       5             :    See http://www.plumed.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       8             : 
       9             :    plumed 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             :    plumed 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 plumed.  If not, see <http://www.gnu.org/licenses/>.
      21             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      22             : 
      23             : #include "core/ActionPilot.h"
      24             : #include "core/ActionWithArguments.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "core/PlumedMain.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace generic {
      30             : 
      31             : //+PLUMEDOC PRINTANALYSIS COMMITTOR
      32             : /*
      33             : Does a committor analysis.
      34             : 
      35             : \par Examples
      36             : 
      37             : The following input monitors two torsional angles during a simulation,
      38             : defines two basins (A and B) as a function of the two torsion angles and
      39             : stops the simulation when it falls in one of the two. In the log
      40             : file will be shown the latest values for the CVs and the basin reached.
      41             : \plumedfile
      42             : TORSION ATOMS=1,2,3,4 LABEL=r1
      43             : TORSION ATOMS=2,3,4,5 LABEL=r2
      44             : COMMITTOR ...
      45             :   ARG=r1,r2
      46             :   STRIDE=10
      47             :   BASIN_LL1=0.15,0.20
      48             :   BASIN_UL1=0.25,0.40
      49             :   BASIN_LL2=-0.25,-0.40
      50             :   BASIN_UL2=-0.15,-0.20
      51             : ... COMMITTOR
      52             : \endplumedfile
      53             : 
      54             : */
      55             : //+ENDPLUMEDOC
      56             : 
      57             : class Committor :
      58             :   public ActionPilot,
      59             :   public ActionWithArguments
      60             : {
      61             : private:
      62             :   std::string file;
      63             :   OFile ofile;
      64             :   std::string fmt;
      65             :   std::vector< std::vector<double> > lowerlimits;
      66             :   std::vector< std::vector<double> > upperlimits;
      67             :   unsigned nbasins;
      68             :   unsigned basin;
      69             :   bool doNotStop;
      70             : public:
      71             :   static void registerKeywords( Keywords& keys );
      72             :   explicit Committor(const ActionOptions&ao);
      73             :   void calculate() override;
      74          30 :   void apply() override {}
      75             : };
      76             : 
      77             : PLUMED_REGISTER_ACTION(Committor,"COMMITTOR")
      78             : 
      79          11 : void Committor::registerKeywords( Keywords& keys ) {
      80          11 :   Action::registerKeywords(keys);
      81          11 :   ActionPilot::registerKeywords(keys);
      82          11 :   ActionWithArguments::registerKeywords(keys);
      83          22 :   keys.addInputKeyword("compulsory","ARG","scalar","the labels of the values which is being used to define the committor surface");
      84          22 :   keys.add("numbered", "BASIN_LL","List of lower limits for basin #");
      85          22 :   keys.add("numbered", "BASIN_UL","List of upper limits for basin #");
      86          33 :   keys.reset_style("BASIN_LL","compulsory"); keys.reset_style("BASIN_UL","compulsory");
      87          22 :   keys.add("compulsory","STRIDE","1","the frequency with which the CVs are analyzed");
      88          22 :   keys.add("optional","FILE","the name of the file on which to output the reached basin");
      89          22 :   keys.add("optional","FMT","the format that should be used to output real numbers");
      90          22 :   keys.addFlag("NOSTOP",false,"if true do not stop the simulation when reaching a basin but just keep track of it");
      91          11 : }
      92             : 
      93           9 : Committor::Committor(const ActionOptions&ao):
      94             :   Action(ao),
      95             :   ActionPilot(ao),
      96             :   ActionWithArguments(ao),
      97           9 :   fmt("%f"),
      98             :   basin(0),
      99          18 :   doNotStop(false)
     100             : {
     101           9 :   ofile.link(*this);
     102          18 :   parse("FILE",file);
     103           9 :   if(file.length()>0) {
     104           2 :     ofile.open(file);
     105           2 :     log.printf("  on file %s\n",file.c_str());
     106             :   } else {
     107           7 :     log.printf("  on plumed log file\n");
     108           7 :     ofile.link(log);
     109             :   }
     110           9 :   parse("FMT",fmt);
     111           9 :   fmt=" "+fmt;
     112           9 :   log.printf("  with format %s\n",fmt.c_str());
     113             : 
     114          13 :   for(unsigned b=1;; ++b ) {
     115             :     std::vector<double> tmpl, tmpu;
     116          22 :     parseNumberedVector("BASIN_LL", b, tmpl );
     117          44 :     parseNumberedVector("BASIN_UL", b, tmpu );
     118          22 :     if( tmpl.empty() && tmpu.empty() ) break;
     119          13 :     if( tmpl.size()!=getNumberOfArguments()) error("Wrong number of values for BASIN_LL: they should be equal to the number of arguments");
     120          13 :     if( tmpu.size()!=getNumberOfArguments()) error("Wrong number of values for BASIN_UL: they should be equal to the number of arguments");
     121          13 :     lowerlimits.push_back(tmpl);
     122          13 :     upperlimits.push_back(tmpu);
     123          13 :     nbasins=b;
     124          13 :   }
     125             : 
     126           9 :   parseFlag("NOSTOP", doNotStop);
     127             : 
     128           9 :   checkRead();
     129             : 
     130             : 
     131          22 :   for(unsigned b=0; b<nbasins; b++) {
     132          13 :     log.printf("  BASIN %u definition:\n", b+1);
     133          32 :     for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     134          19 :       if(lowerlimits[b][i]>upperlimits[b][i]) error("COMMITTOR: UPPER bounds must always be greater than LOWER bounds");
     135          19 :       log.printf(" %f - %f\n", lowerlimits[b][i], upperlimits[b][i]);
     136             :     }
     137          13 :     if(doNotStop) log.printf(" COMMITOR will keep track of the visited basins without stopping the simulations\n");
     138             :   }
     139             : 
     140          20 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) ofile.setupPrintValue( getPntrToArgument(i) );
     141           9 : }
     142             : 
     143          30 : void Committor::calculate() {
     144             :   std::vector<unsigned> inbasin;
     145          30 :   inbasin.assign (nbasins,1);
     146             : 
     147             :   // check if current configuration belongs to a basin
     148         106 :   for(unsigned b=0; b<nbasins; ++b) {
     149         221 :     for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     150         145 :       if(getArgument(i)>lowerlimits[b][i]&&getArgument(i)<upperlimits[b][i]) {
     151             :         inbasin[b]*=1;
     152             :       } else {
     153          80 :         inbasin[b]*=0;
     154             :       }
     155             :     }
     156             :   }
     157             : 
     158             :   // check in which basin we are if any and if this is the same or a new one
     159             :   bool inonebasin = false;
     160          71 :   for(unsigned b=0; b<nbasins; ++b) {
     161          61 :     if(inbasin[b]==1) {
     162          20 :       if(basin!=(b+1)) {
     163          15 :         basin = b+1;
     164          15 :         ofile.fmtField(" %f");
     165          15 :         ofile.printField("time",getTime());
     166          38 :         for(unsigned i=0; i<getNumberOfArguments(); i++) {
     167          23 :           ofile.fmtField(fmt);
     168          23 :           ofile.printField( getPntrToArgument(i), getArgument(i) );
     169             :         }
     170          15 :         ofile.printField("basin", static_cast<int> (b+1));
     171          15 :         ofile.printField();
     172             :       }
     173             :       inonebasin = true;
     174             :       break;
     175             :     }
     176             :   }
     177          10 :   if(!inonebasin) basin = 0;
     178             : 
     179             :   // then check if the simulation should be stopped
     180          30 :   if(inonebasin&&(!doNotStop)) {
     181           8 :     std::string num; Tools::convert( basin, num );
     182           8 :     std::string str = "COMMITTED TO BASIN " + num;
     183           8 :     ofile.addConstantField(str);
     184           8 :     ofile.printField();
     185           8 :     ofile.flush();
     186           8 :     plumed.stop();
     187             :   }
     188          30 : }
     189             : 
     190             : }
     191             : }

Generated by: LCOV version 1.16