LCOV - code coverage report
Current view: top level - function - FuncPathMSD.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 73 79 92.4 %
Date: 2024-10-18 14:00:25 Functions: 4 5 80.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-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 "Function.h"
      24             : #include "core/ActionRegister.h"
      25             : 
      26             : namespace PLMD {
      27             : namespace function {
      28             : 
      29             : //+PLUMEDOC FUNCTION FUNCPATHMSD
      30             : /*
      31             : This function calculates path collective variables.
      32             : 
      33             : This is the Path Collective Variables implementation
      34             : ( see \cite brand07 ).
      35             : This variable computes the progress along a given set of frames that is provided
      36             : in input ("s" component) and the distance from them ("z" component).
      37             : It is a function of mean squared displacement that are obtained by the joint use of mean squared displacement variables with the SQUARED flag
      38             : (see below).
      39             : 
      40             : \par Examples
      41             : 
      42             : Here below is a case where you have defined three frames and you want to
      43             : calculate the progress along the path and the distance from it in p1
      44             : 
      45             : \plumedfile
      46             : t1: RMSD REFERENCE=frame_1.pdb TYPE=OPTIMAL SQUARED
      47             : t2: RMSD REFERENCE=frame_21.pdb TYPE=OPTIMAL SQUARED
      48             : t3: RMSD REFERENCE=frame_42.pdb TYPE=OPTIMAL SQUARED
      49             : p1: FUNCPATHMSD ARG=t1,t2,t3 LAMBDA=500.0
      50             : PRINT ARG=t1,t2,t3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
      51             : \endplumedfile
      52             : 
      53             : For this input you would then define the position of the reference coordinates in three separate pdb files.  The contents of the
      54             : file frame_1.pdb are shown below:
      55             : 
      56             : \auxfile{frame_1.pdb}
      57             : ATOM      1  CL  ALA     1      -3.171   0.295   2.045  1.00  1.00
      58             : ATOM      5  CLP ALA     1      -1.819  -0.143   1.679  1.00  1.00
      59             : ATOM      6  OL  ALA     1      -1.177  -0.889   2.401  1.00  1.00
      60             : ATOM      7  NL  ALA     1      -1.313   0.341   0.529  1.00  1.00
      61             : ATOM      8  HL  ALA     1      -1.845   0.961  -0.011  1.00  1.00
      62             : END
      63             : \endauxfile
      64             : 
      65             : This is then frame.21.pdb:
      66             : 
      67             : \auxfile{frame_21.pdb}
      68             : ATOM      1  CL  ALA     1      -3.089   1.850   1.546  1.00  1.00
      69             : ATOM      5  CLP ALA     1      -1.667   1.457   1.629  1.00  1.00
      70             : ATOM      6  OL  ALA     1      -0.974   1.868   2.533  1.00  1.00
      71             : ATOM      7  NL  ALA     1      -1.204   0.683   0.642  1.00  1.00
      72             : ATOM      8  HL  ALA     1      -1.844   0.360  -0.021  1.00  1.00
      73             : END
      74             : \endauxfile
      75             : 
      76             : and finally this is frame_42.pdb:
      77             : 
      78             : \auxfile{frame_42.pdb}
      79             : ATOM      1  CL  ALA     1      -3.257   1.605   1.105  1.00  1.00
      80             : ATOM      5  CLP ALA     1      -1.941   1.459   0.447  1.00  1.00
      81             : ATOM      6  OL  ALA     1      -1.481   2.369  -0.223  1.00  1.00
      82             : ATOM      7  NL  ALA     1      -1.303   0.291   0.647  1.00  1.00
      83             : ATOM      8  HL  ALA     1      -1.743  -0.379   1.229  1.00  1.00
      84             : END
      85             : \endauxfile
      86             : 
      87             : This second example shows how to define a PATH in \ref CONTACTMAP space:
      88             : 
      89             : \plumedfile
      90             : CONTACTMAP ...
      91             : ATOMS1=1,2 REFERENCE1=0.1
      92             : ATOMS2=3,4 REFERENCE2=0.5
      93             : ATOMS3=4,5 REFERENCE3=0.25
      94             : ATOMS4=5,6 REFERENCE4=0.0
      95             : SWITCH={RATIONAL R_0=1.5}
      96             : LABEL=c1
      97             : CMDIST
      98             : ... CONTACTMAP
      99             : 
     100             : CONTACTMAP ...
     101             : ATOMS1=1,2 REFERENCE1=0.3
     102             : ATOMS2=3,4 REFERENCE2=0.9
     103             : ATOMS3=4,5 REFERENCE3=0.45
     104             : ATOMS4=5,6 REFERENCE4=0.1
     105             : SWITCH={RATIONAL R_0=1.5}
     106             : LABEL=c2
     107             : CMDIST
     108             : ... CONTACTMAP
     109             : 
     110             : CONTACTMAP ...
     111             : ATOMS1=1,2 REFERENCE1=1.0
     112             : ATOMS2=3,4 REFERENCE2=1.0
     113             : ATOMS3=4,5 REFERENCE3=1.0
     114             : ATOMS4=5,6 REFERENCE4=1.0
     115             : SWITCH={RATIONAL R_0=1.5}
     116             : LABEL=c3
     117             : CMDIST
     118             : ... CONTACTMAP
     119             : 
     120             : p1: FUNCPATHMSD ARG=c1,c2,c3 LAMBDA=500.0
     121             : PRINT ARG=c1,c2,c3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
     122             : \endplumedfile
     123             : 
     124             : This third example shows how to define a PATH in \ref PIV space:
     125             : 
     126             : \plumedfile
     127             : PIV ...
     128             : LABEL=c1
     129             : PRECISION=1000
     130             : NLIST
     131             : REF_FILE=Ref1.pdb
     132             : PIVATOMS=2
     133             : ATOMTYPES=A,B
     134             : ONLYDIRECT
     135             : SFACTOR=1.0,0.2
     136             : SORT=1,1
     137             : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
     138             : SWITCH2={RATIONAL R_0=0.5 MM=10 NN=5}
     139             : NL_CUTOFF=1.2,1.2
     140             : NL_STRIDE=10,10
     141             : NL_SKIN=0.1,0.1
     142             : ... PIV
     143             : PIV ...
     144             : LABEL=c2
     145             : PRECISION=1000
     146             : NLIST
     147             : REF_FILE=Ref2.pdb
     148             : PIVATOMS=2
     149             : ATOMTYPES=A,B
     150             : ONLYDIRECT
     151             : SFACTOR=1.0,0.2
     152             : SORT=1,1
     153             : SWITCH1={RATIONAL R_0=0.6 MM=12 NN=4}
     154             : SWITCH2={RATIONAL R_0=0.4 MM=10 NN=5}
     155             : NL_CUTOFF=1.2,1.2
     156             : NL_STRIDE=10,10
     157             : NL_SKIN=0.1,0.1
     158             : ... PIV
     159             : 
     160             : p1: FUNCPATHMSD ARG=c1,c2 LAMBDA=0.180338
     161             : METAD ARG=p1.s,p1.z SIGMA=0.01,0.2 HEIGHT=0.8 PACE=500   LABEL=res
     162             : PRINT ARG=c1,c2,p1.s,p1.z,res.bias STRIDE=500  FILE=colvar FMT=%15.6f
     163             : \endplumedfile
     164             : 
     165             : */
     166             : //+ENDPLUMEDOC
     167             : 
     168             : class FuncPathMSD : public Function {
     169             :   double lambda;
     170             :   int neigh_size;
     171             :   double neigh_stride;
     172             :   std::vector< std::pair<Value *,double> > neighpair;
     173             :   std::map<Value *,double > indexmap; // use double to allow isomaps
     174             :   std::vector <Value*> allArguments;
     175             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     176             : // this below is useful when one wants to sort a vector of double and have back the order
     177             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     178             : // create a custom sorter
     179             :   typedef std::vector<double>::const_iterator myiter;
     180             :   struct ordering {
     181             :     bool operator ()(std::pair<unsigned, myiter> const& a, std::pair<unsigned, myiter> const& b) {
     182             :       return *(a.second) < *(b.second);
     183             :     }
     184             :   };
     185             : // sorting utility
     186             :   std::vector<int> increasingOrder( std::vector<double> &v) {
     187             :     // make a pair
     188             :     std::vector< std::pair<unsigned, myiter> > order(v.size());
     189             :     unsigned n = 0;
     190             :     for (myiter it = v.begin(); it != v.end(); ++it, ++n) {
     191             :       order[n] = make_pair(n, it); // note: heere i do not put the values but the addresses that point to the value
     192             :     }
     193             :     // now sort according the second value
     194             :     std::sort(order.begin(), order.end(), ordering());
     195             :     std::vector<int> vv(v.size()); n=0;
     196             :     for (const auto & it : order) {
     197             :       vv[n]=it.first; n++;
     198             :     }
     199             :     return vv;
     200             :   }
     201             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     202             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     203             : 
     204             :   struct pairordering {
     205             :     bool operator ()(std::pair<Value *, double> const& a, std::pair<Value*, double> const& b) {
     206         437 :       return (a).second > (b).second;
     207             :     }
     208             :   };
     209             : 
     210             : public:
     211             :   explicit FuncPathMSD(const ActionOptions&);
     212             : // active methods:
     213             :   void calculate() override;
     214             :   void prepare() override;
     215             :   static void registerKeywords(Keywords& keys);
     216             : };
     217             : 
     218             : PLUMED_REGISTER_ACTION(FuncPathMSD,"FUNCPATHMSD")
     219             : 
     220           4 : void FuncPathMSD::registerKeywords(Keywords& keys) {
     221           4 :   Function::registerKeywords(keys);
     222           4 :   keys.use("ARG");
     223           8 :   keys.add("compulsory","LAMBDA","the lambda parameter is needed for smoothing, is in the units of plumed");
     224           8 :   keys.add("optional","NEIGH_SIZE","size of the neighbor list");
     225           8 :   keys.add("optional","NEIGH_STRIDE","how often the neighbor list needs to be calculated in time units");
     226           8 :   keys.addOutputComponent("s","default","the position on the path");
     227           8 :   keys.addOutputComponent("z","default","the distance from the path");
     228           4 : }
     229           2 : FuncPathMSD::FuncPathMSD(const ActionOptions&ao):
     230             :   Action(ao),
     231             :   Function(ao),
     232           2 :   neigh_size(-1),
     233           2 :   neigh_stride(-1.)
     234             : {
     235             : 
     236           2 :   parse("LAMBDA",lambda);
     237           2 :   parse("NEIGH_SIZE",neigh_size);
     238           2 :   parse("NEIGH_STRIDE",neigh_stride);
     239           2 :   checkRead();
     240           2 :   log.printf("  lambda is %f\n",lambda);
     241             :   // list the action involved and check the type
     242           2 :   std::string myname=getPntrToArgument(0)->getPntrToAction()->getName();
     243           2 :   if(myname!="RMSD_SCALAR"&&myname!="CONTACTMAP"&&myname!="DISTANCE"&&myname!="PIV") error("One or more of your arguments is not of RMSD/CONTACTMAP/DISTANCE/PIV type!!!");
     244           6 :   for(unsigned i=1; i<getNumberOfArguments(); i++) {
     245             :     // for each value get the name and the label of the corresponding action
     246           4 :     if( getPntrToArgument(i)->getPntrToAction()->getName()!=myname ) error("mismatch between the types of arguments");
     247             :   }
     248           2 :   log.printf("  Consistency check completed! Your path cvs look good!\n");
     249             :   // do some neighbor printout
     250           2 :   if(neigh_stride>0. || neigh_size>0) {
     251           1 :     if(neigh_size>static_cast<int>(getNumberOfArguments())) {
     252           0 :       log.printf(" List size required ( %d ) is too large: resizing to the maximum number of arg required: %d  \n",neigh_size,getNumberOfArguments());
     253           0 :       neigh_size=getNumberOfArguments();
     254             :     }
     255           1 :     log.printf("  Neighbor list enabled: \n");
     256           1 :     log.printf("                size   :  %d elements\n",neigh_size);
     257           1 :     log.printf("                stride :  %f time \n",neigh_stride);
     258             :   } else {
     259           1 :     log.printf("  Neighbor list NOT enabled \n");
     260             :   }
     261             : 
     262           4 :   addComponentWithDerivatives("s"); componentIsNotPeriodic("s");
     263           4 :   addComponentWithDerivatives("z"); componentIsNotPeriodic("z");
     264             : 
     265             :   // now backup the arguments
     266           8 :   for(unsigned i=0; i<getNumberOfArguments(); i++)allArguments.push_back(getPntrToArgument(i));
     267             :   double i=1.;
     268           8 :   for(const auto & it : allArguments) {
     269           6 :     indexmap[it]=i; i+=1.;
     270             :   }
     271             : 
     272           2 : }
     273             : // calculator
     274        1092 : void FuncPathMSD::calculate() {
     275             : // log.printf("NOW CALCULATE! \n");
     276             :   double s_path=0.;
     277             :   double partition=0.;
     278        1092 :   if(neighpair.empty()) { // at first step, resize it
     279           0 :     neighpair.resize(allArguments.size());
     280           0 :     for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     281             :   }
     282             : 
     283        1092 :   Value* val_s_path=getPntrToComponent("s");
     284        2184 :   Value* val_z_path=getPntrToComponent("z");
     285             : 
     286        3959 :   for(auto & it : neighpair) {
     287        2867 :     it.second=std::exp(-lambda*(it.first->get()));
     288        2867 :     s_path+=(indexmap[it.first])*it.second;
     289        2867 :     partition+=it.second;
     290             :   }
     291        1092 :   s_path/=partition;
     292             :   val_s_path->set(s_path);
     293        1092 :   val_z_path->set(-(1./lambda)*std::log(partition));
     294             :   int n=0;
     295        3959 :   for(const auto & it : neighpair) {
     296        2867 :     double expval=it.second;
     297        2867 :     double tmp=lambda*expval*(s_path-(indexmap[it.first]))/partition;
     298             :     setDerivative(val_s_path,n,tmp);
     299        2867 :     setDerivative(val_z_path,n,expval/partition);
     300        2867 :     n++;
     301             :   }
     302             : 
     303             : //  log.printf("CALCULATION DONE! \n");
     304        1092 : }
     305             : ///
     306             : /// this function updates the needed argument list
     307             : ///
     308        1092 : void FuncPathMSD::prepare() {
     309             : 
     310             :   // neighbor list: rank and activate the chain for the next step
     311             : 
     312             :   // neighbor list: if neigh_size<0 never sort and keep the full vector
     313             :   // neighbor list: if neigh_size>0
     314             :   //                if the size is full -> sort the vector and decide the dependencies for next step
     315             :   //                if the size is not full -> check if next step will need the full dependency otherwise keep this dependencies
     316             : 
     317             :   // here just resize the neighpair. The real resizing of reinit will be done by the prepare stage that will modify the  list of arguments
     318        1092 :   if (neigh_size>0) {
     319         546 :     if(neighpair.size()==allArguments.size()) { // I just did the complete round: need to sort, shorten and give it a go
     320             :       // sort the values
     321         137 :       std::sort(neighpair.begin(),neighpair.end(),pairordering());
     322             :       // resize the effective list
     323         137 :       neighpair.resize(neigh_size);
     324         137 :       log.printf("  NEIGH LIST NOW INCLUDE INDEXES: ");
     325         411 :       for(int i=0; i<neigh_size; ++i) {log.printf(" %f ",indexmap[neighpair[i].first]);} log.printf(" \n");
     326             :     } else {
     327         409 :       if( int(getStep())%int(neigh_stride/getTimeStep())==0 ) {
     328         137 :         log.printf(" Time %f : recalculating full neighlist \n",getStep()*getTimeStep());
     329         137 :         neighpair.resize(allArguments.size());
     330         548 :         for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     331             :       }
     332             :     }
     333             :   } else {
     334         546 :     if( int(getStep())==0) {
     335           1 :       neighpair.resize(allArguments.size());
     336           4 :       for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     337             :     }
     338             :   }
     339             :   std::vector<Value*> argstocall;
     340             : //log.printf("PREPARING \n");
     341             :   argstocall.clear();
     342        1092 :   if(!neighpair.empty()) {
     343        3959 :     for(const auto & it : neighpair) {
     344        2867 :       argstocall.push_back( it.first );
     345             :       //     log.printf("CALLING %p %f ",(*it).first ,indexmap[(*it).first] );
     346             :     }
     347             :   } else {
     348           0 :     for(unsigned i=0; i<allArguments.size(); i++) {
     349           0 :       argstocall.push_back(allArguments[i]);
     350             :     }
     351             :   }
     352             : // now the list of argument changes
     353        1092 :   requestArguments(argstocall);
     354             : //now resize the derivatives as well
     355             : //for each value in this action
     356        3276 :   for(int i=0; i< getNumberOfComponents(); i++) {
     357             :     //resize the derivative to the number   the
     358        2184 :     getPntrToComponent(i)->clearDerivatives();
     359        2184 :     getPntrToComponent(i)->resizeDerivatives(getNumberOfArguments());
     360             :   }
     361             : //log.printf("PREPARING DONE! \n");
     362        1092 : }
     363             : 
     364             : }
     365             : }
     366             : 
     367             : 

Generated by: LCOV version 1.16