LCOV - code coverage report
Current view: top level - function - FuncPathMSD.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 75 81 92.6 %
Date: 2020-11-18 11:20:57 Functions: 11 12 91.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-2019 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             : #include <cmath>
      23             : 
      24             : #include "Function.h"
      25             : #include "ActionRegister.h"
      26             : 
      27             : #include <string>
      28             : #include <cstring>
      29             : #include <iostream>
      30             : 
      31             : using namespace std;
      32             : 
      33             : namespace PLMD {
      34             : namespace function {
      35             : 
      36             : //+PLUMEDOC FUNCTION FUNCPATHMSD
      37             : /*
      38             : This function calculates path collective variables.
      39             : 
      40             : This is the Path Collective Variables implementation
      41             : ( see \cite brand07 ).
      42             : This variable computes the progress along a given set of frames that is provided
      43             : in input ("s" component) and the distance from them ("z" component).
      44             : It is a function of MSD that are obtained by the joint use of MSD variable and SQUARED flag
      45             : (see below).
      46             : 
      47             : \par Examples
      48             : 
      49             : Here below is a case where you have defined three frames and you want to
      50             : calculate the progress alng the path and the distance from it in p1
      51             : 
      52             : \plumedfile
      53             : t1: RMSD REFERENCE=frame_1.dat TYPE=OPTIMAL SQUARED
      54             : t2: RMSD REFERENCE=frame_21.dat TYPE=OPTIMAL SQUARED
      55             : t3: RMSD REFERENCE=frame_42.dat TYPE=OPTIMAL SQUARED
      56             : p1: FUNCPATHMSD ARG=t1,t2,t3 LAMBDA=500.0
      57             : PRINT ARG=t1,t2,t3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
      58             : \endplumedfile
      59             : 
      60             : In this second example is shown how to define a PATH in the \ref CONTACTMAP space:
      61             : 
      62             : \plumedfile
      63             : CONTACTMAP ...
      64             : ATOMS1=1,2 REFERENCE1=0.1
      65             : ATOMS2=3,4 REFERENCE2=0.5
      66             : ATOMS3=4,5 REFERENCE3=0.25
      67             : ATOMS4=5,6 REFERENCE4=0.0
      68             : SWITCH={RATIONAL R_0=1.5}
      69             : LABEL=c1
      70             : CMDIST
      71             : ... CONTACTMAP
      72             : 
      73             : CONTACTMAP ...
      74             : ATOMS1=1,2 REFERENCE1=0.3
      75             : ATOMS2=3,4 REFERENCE2=0.9
      76             : ATOMS3=4,5 REFERENCE3=0.45
      77             : ATOMS4=5,6 REFERENCE4=0.1
      78             : SWITCH={RATIONAL R_0=1.5}
      79             : LABEL=c2
      80             : CMDIST
      81             : ... CONTACTMAP
      82             : 
      83             : CONTACTMAP ...
      84             : ATOMS1=1,2 REFERENCE1=1.0
      85             : ATOMS2=3,4 REFERENCE2=1.0
      86             : ATOMS3=4,5 REFERENCE3=1.0
      87             : ATOMS4=5,6 REFERENCE4=1.0
      88             : SWITCH={RATIONAL R_0=1.5}
      89             : LABEL=c3
      90             : CMDIST
      91             : ... CONTACTMAP
      92             : 
      93             : p1: FUNCPATHMSD ARG=c1,c2,c3 LAMBDA=500.0
      94             : PRINT ARG=c1,c2,c3,p1.s,p1.z STRIDE=1 FILE=colvar FMT=%8.4f
      95             : \endplumedfile
      96             : 
      97             : */
      98             : //+ENDPLUMEDOC
      99             : 
     100           6 : class FuncPathMSD : public Function {
     101             :   double lambda;
     102             :   int neigh_size;
     103             :   double neigh_stride;
     104             :   vector< pair<Value *,double> > neighpair;
     105             :   map<Value *,double > indexmap; // use double to allow isomaps
     106             :   vector <Value*> allArguments;
     107             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     108             : // this below is useful when one wants to sort a vector of double and have back the order
     109             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     110             : // create a custom sorter
     111             :   typedef vector<double>::const_iterator myiter;
     112             :   struct ordering {
     113             :     bool operator ()(pair<unsigned, myiter> const& a, pair<unsigned, myiter> const& b) {
     114             :       return *(a.second) < *(b.second);
     115             :     }
     116             :   };
     117             : // sorting utility
     118             :   vector<int> increasingOrder( vector<double> &v) {
     119             :     // make a pair
     120             :     vector< pair<unsigned, myiter> > order(v.size());
     121             :     unsigned n = 0;
     122             :     for (myiter it = v.begin(); it != v.end(); ++it, ++n) {
     123             :       order[n] = make_pair(n, it); // note: heere i do not put the values but the addresses that point to the value
     124             :     }
     125             :     // now sort according the second value
     126             :     sort(order.begin(), order.end(), ordering());
     127             :     vector<int> vv(v.size()); n=0;
     128             :     for (const auto & it : order) {
     129             :       vv[n]=it.first; n++;
     130             :     }
     131             :     return vv;
     132             :   }
     133             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     134             : // XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
     135             : 
     136             :   struct pairordering {
     137             :     bool operator ()(pair<Value *, double> const& a, pair<Value*, double> const& b) {
     138         437 :       return (a).second > (b).second;
     139             :     }
     140             :   };
     141             : 
     142             : public:
     143             :   explicit FuncPathMSD(const ActionOptions&);
     144             : // active methods:
     145             :   virtual void calculate();
     146             :   virtual void prepare();
     147             :   static void registerKeywords(Keywords& keys);
     148             : };
     149             : 
     150        6454 : PLUMED_REGISTER_ACTION(FuncPathMSD,"FUNCPATHMSD")
     151             : 
     152           3 : void FuncPathMSD::registerKeywords(Keywords& keys) {
     153           3 :   Function::registerKeywords(keys);
     154           6 :   keys.use("ARG");
     155          12 :   keys.add("compulsory","LAMBDA","the lambda parameter is needed for smoothing, is in the units of plumed");
     156          12 :   keys.add("optional","NEIGH_SIZE","size of the neighbor list");
     157          12 :   keys.add("optional","NEIGH_STRIDE","how often the neighbor list needs to be calculated in time units");
     158           3 :   componentsAreNotOptional(keys);
     159          12 :   keys.addOutputComponent("s","default","the position on the path");
     160          12 :   keys.addOutputComponent("z","default","the distance from the path");
     161           3 : }
     162           2 : FuncPathMSD::FuncPathMSD(const ActionOptions&ao):
     163             :   Action(ao),
     164             :   Function(ao),
     165             :   neigh_size(-1),
     166           6 :   neigh_stride(-1.)
     167             : {
     168             : 
     169           4 :   parse("LAMBDA",lambda);
     170           4 :   parse("NEIGH_SIZE",neigh_size);
     171           4 :   parse("NEIGH_STRIDE",neigh_stride);
     172           2 :   checkRead();
     173           2 :   log.printf("  lambda is %f\n",lambda);
     174             :   // list the action involved and check the type
     175           2 :   std::string myname=getPntrToArgument(0)->getPntrToAction()->getName();
     176           2 :   if(myname!="RMSD"&&myname!="CONTACTMAP"&&myname!="DISTANCE") error("One or more of your arguments is not of RMSD/CONTACTMAP/DISTANCE type!!!");
     177          10 :   for(unsigned i=1; i<getNumberOfArguments(); i++) {
     178             :     // for each value get the name and the label of the corresponding action
     179           8 :     if( getPntrToArgument(i)->getPntrToAction()->getName()!=myname ) error("mismatch between the types of arguments");
     180             :   }
     181           2 :   log.printf("  Consistency check completed! Your path cvs look good!\n");
     182             :   // do some neighbor printout
     183           2 :   if(neigh_stride>0. || neigh_size>0) {
     184           2 :     if(neigh_size>getNumberOfArguments()) {
     185           0 :       log.printf(" List size required ( %d ) is too large: resizing to the maximum number of arg required: %d  \n",neigh_size,getNumberOfArguments());
     186           0 :       neigh_size=getNumberOfArguments();
     187             :     }
     188           1 :     log.printf("  Neighbor list enabled: \n");
     189           1 :     log.printf("                size   :  %d elements\n",neigh_size);
     190           1 :     log.printf("                stride :  %f time \n",neigh_stride);
     191             :   } else {
     192           1 :     log.printf("  Neighbor list NOT enabled \n");
     193             :   }
     194             : 
     195           6 :   addComponentWithDerivatives("s"); componentIsNotPeriodic("s");
     196           6 :   addComponentWithDerivatives("z"); componentIsNotPeriodic("z");
     197             : 
     198             :   // now backup the arguments
     199          20 :   for(unsigned i=0; i<getNumberOfArguments(); i++)allArguments.push_back(getPntrToArgument(i));
     200             :   double i=1.;
     201           8 :   for(const auto & it : allArguments) {
     202           6 :     indexmap[it]=i; i+=1.;
     203             :   }
     204             : 
     205           2 : }
     206             : // calculator
     207        1092 : void FuncPathMSD::calculate() {
     208             : // log.printf("NOW CALCULATE! \n");
     209             :   double s_path=0.;
     210             :   double partition=0.;
     211        1092 :   if(neighpair.empty()) { // at first step, resize it
     212           0 :     neighpair.resize(allArguments.size());
     213           0 :     for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     214             :   }
     215             : 
     216        2184 :   Value* val_s_path=getPntrToComponent("s");
     217        2184 :   Value* val_z_path=getPntrToComponent("z");
     218             : 
     219        3959 :   for(auto & it : neighpair) {
     220        5734 :     it.second=exp(-lambda*(it.first->get()));
     221        2867 :     s_path+=(indexmap[it.first])*it.second;
     222        2867 :     partition+=it.second;
     223             :   }
     224        1092 :   s_path/=partition;
     225             :   val_s_path->set(s_path);
     226        1092 :   val_z_path->set(-(1./lambda)*std::log(partition));
     227             :   int n=0;
     228        3959 :   for(const auto & it : neighpair) {
     229        2867 :     double expval=it.second;
     230        2867 :     double tmp=lambda*expval*(s_path-(indexmap[it.first]))/partition;
     231             :     setDerivative(val_s_path,n,tmp);
     232        2867 :     setDerivative(val_z_path,n,expval/partition);
     233        2867 :     n++;
     234             :   }
     235             : 
     236             : //  log.printf("CALCULATION DONE! \n");
     237        1092 : }
     238             : ///
     239             : /// this function updates the needed argument list
     240             : ///
     241        1092 : void FuncPathMSD::prepare() {
     242             : 
     243             :   // neighbor list: rank and activate the chain for the next step
     244             : 
     245             :   // neighbor list: if neigh_size<0 never sort and keep the full vector
     246             :   // neighbor list: if neigh_size>0
     247             :   //                if the size is full -> sort the vector and decide the dependencies for next step
     248             :   //                if the size is not full -> check if next step will need the full dependency otherwise keep this dependencies
     249             : 
     250             :   // here just resize the neighpair. The real resizing of reinit will be done by the prepare stage that will modify the  list of arguments
     251        1092 :   if (neigh_size>0) {
     252         546 :     if(neighpair.size()==allArguments.size()) { // I just did the complete round: need to sort, shorten and give it a go
     253             :       // sort the values
     254             :       sort(neighpair.begin(),neighpair.end(),pairordering());
     255             :       // resize the effective list
     256         137 :       neighpair.resize(neigh_size);
     257         137 :       log.printf("  NEIGH LIST NOW INCLUDE INDEXES: ");
     258         685 :       for(int i=0; i<neigh_size; ++i)log.printf(" %f ",indexmap[neighpair[i].first]); log.printf(" \n");
     259             :     } else {
     260         409 :       if( int(getStep())%int(neigh_stride/getTimeStep())==0 ) {
     261         137 :         log.printf(" Time %f : recalculating full neighlist \n",getStep()*getTimeStep());
     262         274 :         neighpair.resize(allArguments.size());
     263        1507 :         for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     264             :       }
     265             :     }
     266             :   } else {
     267         546 :     if( int(getStep())==0) {
     268           2 :       neighpair.resize(allArguments.size());
     269          11 :       for(unsigned i=0; i<allArguments.size(); i++)neighpair[i].first=allArguments[i];
     270             :     }
     271             :   }
     272             :   vector<Value*> argstocall;
     273             : //log.printf("PREPARING \n");
     274             :   argstocall.clear();
     275        1092 :   if(!neighpair.empty()) {
     276        3959 :     for(const auto & it : neighpair) {
     277        2867 :       argstocall.push_back( it.first );
     278             :       //     log.printf("CALLING %p %f ",(*it).first ,indexmap[(*it).first] );
     279             :     }
     280             :   } else {
     281           0 :     for(unsigned i=0; i<allArguments.size(); i++) {
     282           0 :       argstocall.push_back(allArguments[i]);
     283             :     }
     284             :   }
     285             : // now the list of argument changes
     286        1092 :   requestArguments(argstocall);
     287             : //now resize the derivatives as well
     288             : //for each value in this action
     289        5460 :   for(int i=0; i< getNumberOfComponents(); i++) {
     290             :     //resize the derivative to the number   the
     291        2184 :     getPntrToComponent(i)->clearDerivatives();
     292        2184 :     getPntrToComponent(i)->resizeDerivatives(getNumberOfArguments());
     293             :   }
     294             : //log.printf("PREPARING DONE! \n");
     295        1092 : }
     296             : 
     297             : }
     298        4839 : }
     299             : 
     300             : 

Generated by: LCOV version 1.13