LCOV - code coverage report
Current view: top level - bias - MovingRestraint.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 109 109 100.0 %
Date: 2024-10-18 14:00:25 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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             : #include "Bias.h"
      23             : #include "core/ActionRegister.h"
      24             : 
      25             : namespace PLMD {
      26             : namespace bias {
      27             : 
      28             : //+PLUMEDOC BIAS MOVINGRESTRAINT
      29             : /*
      30             : Add a time-dependent, harmonic restraint on one or more variables.
      31             : 
      32             : This form of bias can be used to performed steered MD \cite Grubmuller3
      33             : and Jarzynski sampling \cite jarzynski.
      34             : 
      35             : The harmonic restraint on your system is given by:
      36             : 
      37             : \f[
      38             : V(\vec{s},t) = \frac{1}{2} \kappa(t) ( \vec{s} - \vec{s}_0(t) )^2
      39             : \f]
      40             : 
      41             : The time dependence of \f$\kappa\f$ and \f$\vec{s}_0\f$ are specified by a list of
      42             : STEP, KAPPA and AT keywords.  These keywords tell plumed what values \f$\kappa\f$ and \f$\vec{s}_0\f$
      43             : should have at the time specified by the corresponding STEP keyword.  In between these times
      44             : the values of \f$\kappa\f$ and \f$\vec{s}_0\f$ are linearly interpolated.
      45             : 
      46             : Additional material and examples can be also found in the tutorial \ref belfast-5
      47             : 
      48             : \par Examples
      49             : 
      50             : The following input is dragging the distance between atoms 2 and 4
      51             : from 1 to 2 in the first 1000 steps, then back in the next 1000 steps.
      52             : In the following 500 steps the restraint is progressively switched off.
      53             : \plumedfile
      54             : DISTANCE ATOMS=2,4 LABEL=d
      55             : MOVINGRESTRAINT ...
      56             :   ARG=d
      57             :   STEP0=0    AT0=1.0 KAPPA0=100.0
      58             :   STEP1=1000 AT1=2.0
      59             :   STEP2=2000 AT2=1.0
      60             :   STEP3=2500         KAPPA3=0.0
      61             : ... MOVINGRESTRAINT
      62             : \endplumedfile
      63             : The following input is progressively building restraints
      64             : distances between atoms 1 and 5 and between atoms 2 and 4
      65             : in the first 1000 steps. Afterwards, the restraint is kept
      66             : static.
      67             : \plumedfile
      68             : DISTANCE ATOMS=1,5 LABEL=d1
      69             : DISTANCE ATOMS=2,4 LABEL=d2
      70             : MOVINGRESTRAINT ...
      71             :   ARG=d1,d2
      72             :   STEP0=0    AT0=1.0,1.5 KAPPA0=0.0,0.0
      73             :   STEP1=1000 AT1=1.0,1.5 KAPPA1=1.0,1.0
      74             : ... MOVINGRESTRAINT
      75             : \endplumedfile
      76             : The following input is progressively bringing atoms 1 and 2
      77             : close to each other with an upper wall
      78             : \plumedfile
      79             : DISTANCE ATOMS=1,2 LABEL=d1
      80             : MOVINGRESTRAINT ...
      81             :   ARG=d1
      82             :   VERSE=U
      83             :   STEP0=0    AT0=1.0 KAPPA0=10.0
      84             :   STEP1=1000 AT1=0.0
      85             : ... MOVINGRESTRAINT
      86             : \endplumedfile
      87             : 
      88             : By default the Action is issuing some values which are
      89             : the work on each degree of freedom, the center of the harmonic potential,
      90             : the total bias deposited
      91             : 
      92             : (See also \ref DISTANCE).
      93             : 
      94             : \attention Work is not computed properly when KAPPA is time dependent.
      95             : 
      96             : */
      97             : //+ENDPLUMEDOC
      98             : 
      99             : 
     100             : class MovingRestraint : public Bias {
     101             :   std::vector<std::vector<double> > at;
     102             :   std::vector<std::vector<double> > kappa;
     103             :   std::vector<long long int> step;
     104             :   std::vector<double> oldaa;
     105             :   std::vector<double> oldk;
     106             :   std::vector<double> olddpotdk;
     107             :   std::vector<double> oldf;
     108             :   std::vector<std::string> verse;
     109             :   std::vector<double> work;
     110             :   std::vector<double> kk,aa,f,dpotdk;
     111             :   double tot_work;
     112             :   std::vector<Value*> valueCntr;
     113             :   std::vector<Value*> valueWork;
     114             :   std::vector<Value*> valueKappa;
     115             :   Value* valueTotWork=nullptr;
     116             :   Value* valueForce2=nullptr;
     117             : public:
     118             :   explicit MovingRestraint(const ActionOptions&);
     119             :   void calculate() override;
     120             :   static void registerKeywords( Keywords& keys );
     121             : };
     122             : 
     123             : PLUMED_REGISTER_ACTION(MovingRestraint,"MOVINGRESTRAINT")
     124             : 
     125           6 : void MovingRestraint::registerKeywords( Keywords& keys ) {
     126           6 :   Bias::registerKeywords(keys);
     127           6 :   keys.use("ARG");
     128          12 :   keys.add("compulsory","VERSE","B","Tells plumed whether the restraint is only acting for CV larger (U) or smaller (L) than "
     129             :            "the restraint or whether it is acting on both sides (B)");
     130          12 :   keys.add("numbered","STEP","This keyword appears multiple times as STEPx with x=0,1,2,...,n. Each value given represents "
     131             :            "the MD step at which the restraint parameters take the values KAPPAx and ATx.");
     132          12 :   keys.reset_style("STEP","compulsory");
     133          12 :   keys.add("numbered","AT","ATx is equal to the position of the restraint at time STEPx. For intermediate times this parameter "
     134             :            "is linearly interpolated. If no ATx is specified for STEPx then the values of AT are kept constant "
     135             :            "during the interval of time between STEP(x-1) and STEPx.");
     136          12 :   keys.reset_style("AT","compulsory");
     137          12 :   keys.add("numbered","KAPPA","KAPPAx is equal to the value of the force constants at time STEPx. For intermediate times this "
     138             :            "parameter is linearly interpolated.  If no KAPPAx is specified for STEPx then the values of KAPPAx "
     139             :            "are kept constant during the interval of time between STEP(x-1) and STEPx.");
     140          12 :   keys.reset_style("KAPPA","compulsory");
     141          12 :   keys.addOutputComponent("work","default","the total work performed changing this restraint");
     142          12 :   keys.addOutputComponent("force2","default","the instantaneous value of the squared force due to this bias potential");
     143          12 :   keys.addOutputComponent("_cntr","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
     144             :                           "these quantities will named with  the arguments of the bias followed by "
     145             :                           "the character string _cntr. These quantities give the instantaneous position "
     146             :                           "of the center of the harmonic potential.");
     147          12 :   keys.addOutputComponent("_work","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
     148             :                           "These quantities will named with the arguments of the bias followed by "
     149             :                           "the character string _work. These quantities tell the user how much work has "
     150             :                           "been done by the potential in dragging the system along the various colvar axis.");
     151          12 :   keys.addOutputComponent("_kappa","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
     152             :                           "These quantities will named with the arguments of the bias followed by "
     153             :                           "the character string _kappa. These quantities tell the user the time dependent value of kappa.");
     154           6 : }
     155             : 
     156           4 : MovingRestraint::MovingRestraint(const ActionOptions&ao):
     157             :   PLUMED_BIAS_INIT(ao),
     158           4 :   verse(getNumberOfArguments()),
     159           4 :   kk(getNumberOfArguments()),
     160           4 :   aa(getNumberOfArguments()),
     161           4 :   f(getNumberOfArguments()),
     162           8 :   dpotdk(getNumberOfArguments())
     163             : {
     164           4 :   parseVector("VERSE",verse);
     165           4 :   std::vector<long long int> ss(1); ss[0]=-1;
     166           4 :   std::vector<double> kk( getNumberOfArguments() ), aa( getNumberOfArguments() );
     167          10 :   for(int i=0;; i++) {
     168             :     // Read in step
     169          28 :     if( !parseNumberedVector("STEP",i,ss) ) break;
     170          19 :     for(unsigned j=0; j<step.size(); j++) {
     171           9 :       if(ss[0]<step[j]) error("in moving restraint step number must always increase");
     172             :     }
     173          10 :     step.push_back(ss[0]);
     174             : 
     175             :     // Try to read kappa
     176          20 :     if( !parseNumberedVector("KAPPA",i,kk) ) kk=kappa[i-1];
     177          10 :     kappa.push_back(kk);
     178             : 
     179             :     // Now read AT
     180          20 :     if( !parseNumberedVector("AT",i,aa) ) aa=at[i-1];
     181          10 :     at.push_back(aa);
     182          10 :   }
     183           4 :   checkRead();
     184             : 
     185          14 :   for(unsigned i=0; i<step.size(); i++) {
     186          10 :     log.printf("  step%u %lld\n",i,step[i]);
     187          10 :     log.printf("  at");
     188          22 :     for(unsigned j=0; j<at[i].size(); j++) log.printf(" %f",at[i][j]);
     189          10 :     log.printf("\n");
     190          10 :     log.printf("  with force constant");
     191          22 :     for(unsigned j=0; j<kappa[i].size(); j++) log.printf(" %f",kappa[i][j]);
     192          10 :     log.printf("\n");
     193             :   };
     194             : 
     195           8 :   addComponent("force2"); componentIsNotPeriodic("force2");
     196           4 :   valueForce2=getPntrToComponent("force2");
     197             : 
     198             :   // add the centers of the restraint as additional components that can be retrieved (useful for debug)
     199             : 
     200             :   std::string comp;
     201           9 :   for(unsigned i=0; i< getNumberOfArguments() ; i++) {
     202          10 :     comp=getPntrToArgument(i)->getName()+"_cntr"; // each spring has its own center
     203          10 :     addComponent(comp); componentIsNotPeriodic(comp);
     204           5 :     valueCntr.push_back(getPntrToComponent(comp));
     205          10 :     comp=getPntrToArgument(i)->getName()+"_work"; // each spring has its own work
     206          10 :     addComponent(comp); componentIsNotPeriodic(comp);
     207           5 :     valueWork.push_back(getPntrToComponent(comp));
     208          10 :     comp=getPntrToArgument(i)->getName()+"_kappa"; // each spring has its own kappa
     209          10 :     addComponent(comp); componentIsNotPeriodic(comp);
     210           5 :     valueKappa.push_back(getPntrToComponent(comp));
     211           5 :     work.push_back(0.); // initialize the work value
     212             :   }
     213           8 :   addComponent("work"); componentIsNotPeriodic("work");
     214           4 :   valueTotWork=getPntrToComponent("work");
     215           4 :   tot_work=0.0;
     216             : 
     217           4 :   log<<"  Bibliography ";
     218           8 :   log<<cite("Grubmuller, Heymann, and Tavan, Science 271, 997 (1996)")<<"\n";
     219             : 
     220           4 : }
     221             : 
     222             : 
     223         566 : void MovingRestraint::calculate() {
     224             :   double ene=0.0;
     225             :   double totf2=0.0;
     226         566 :   unsigned narg=getNumberOfArguments();
     227         566 :   long long int now=getStep();
     228         566 :   if(now<=step[0]) {
     229           3 :     kk=kappa[0];
     230           3 :     aa=at[0];
     231           3 :     oldaa=at[0];
     232           3 :     oldk=kappa[0];
     233           3 :     olddpotdk.resize(narg);
     234           3 :     oldf.resize(narg);
     235         563 :   } else if(now>=step[step.size()-1]) {
     236          47 :     kk=kappa[step.size()-1];
     237          47 :     aa=at[step.size()-1];
     238             :   } else {
     239             :     unsigned i=0;
     240         521 :     for(i=1; i<step.size()-1; i++) if(now<step[i]) break;
     241         516 :     double c2=(now-step[i-1])/double(step[i]-step[i-1]);
     242         516 :     double c1=1.0-c2;
     243        1040 :     for(unsigned j=0; j<narg; j++) kk[j]=(c1*kappa[i-1][j]+c2*kappa[i][j]);
     244        1040 :     for(unsigned j=0; j<narg; j++) {
     245         524 :       const double a1=at[i-1][j];
     246         524 :       const double a2=at[i][j];
     247         524 :       aa[j]=(c1*a1+c2*(a1+difference(j,a1,a2)));
     248             :     }
     249             :   }
     250         566 :   tot_work=0.0;
     251        1142 :   for(unsigned i=0; i<narg; ++i) {
     252         576 :     const double cv=difference(i,aa[i],getArgument(i)); // this gives: getArgument(i) - aa[i]
     253         576 :     valueCntr[i]->set(aa[i]);
     254         576 :     const double k=kk[i];
     255         576 :     f[i]=-k*cv;
     256         576 :     if(verse[i]=="U" && cv<0) continue;
     257         576 :     if(verse[i]=="L" && cv>0) continue;
     258        1728 :     plumed_assert(verse[i]=="U" || verse[i]=="L" || verse[i]=="B");
     259         576 :     dpotdk[i]=0.5*cv*cv;
     260         576 :     if(oldaa.size()==aa.size() && oldf.size()==f.size()) work[i]+=0.5*(oldf[i]+f[i])*(aa[i]-oldaa[i]) + 0.5*( dpotdk[i]+olddpotdk[i] )*(kk[i]-oldk[i]);
     261         576 :     valueWork[i]->set(work[i]);
     262         576 :     valueKappa[i]->set(kk[i]);
     263         576 :     tot_work+=work[i];
     264         576 :     ene+=0.5*k*cv*cv;
     265         576 :     setOutputForce(i,f[i]);
     266         576 :     totf2+=f[i]*f[i];
     267             :   };
     268         566 :   valueTotWork->set(tot_work);
     269         566 :   oldf=f;
     270         566 :   oldaa=aa;
     271         566 :   oldk=kk;
     272         566 :   olddpotdk=dpotdk;
     273         566 :   setBias(ene);
     274         566 :   valueForce2->set(totf2);
     275         566 : }
     276             : 
     277             : }
     278             : }
     279             : 
     280             : 

Generated by: LCOV version 1.16