LCOV - code coverage report
Current view: top level - bias - ExtendedLagrangian.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 97 99 98.0 %
Date: 2026-03-30 13:16:06 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-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 "ActionRegister.h"
      24             : #include "tools/Random.h"
      25             : #include "core/PlumedMain.h"
      26             : #include "core/Atoms.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace bias {
      30             : 
      31             : //+PLUMEDOC BIAS EXTENDED_LAGRANGIAN
      32             : /*
      33             : Add extended Lagrangian.
      34             : 
      35             : This action can be used to create fictitious collective variables coupled to the real ones.
      36             : Given \f$x_i\f$ the \f$i\f$th argument of this bias potential, potential
      37             : and kinetic contributions are added to the energy of the system as
      38             : \f[
      39             :   V=\sum_i \frac{k_i}{2} (x_i-s_i)^2 + \sum_i \frac{\dot{s}_i^2}{2m_i}
      40             : \f].
      41             : 
      42             : The resulting potential is thus similar to a \ref RESTRAINT,
      43             : but the restraint center moved with time following Hamiltonian
      44             : dynamics with mass \f$m_i\f$.
      45             : 
      46             : This bias potential accepts thus vectorial keywords (one element per argument)
      47             : to define the coupling constant (KAPPA) and a relaxation time \f$tau\f$ (TAU).
      48             : The mass is them computed as \f$m=k(\frac{\tau}{2\pi})^2\f$.
      49             : 
      50             : Notice that this action creates several components.
      51             : The ones named XX_fict are the fictitious coordinates. It is possible
      52             : to add further forces on them by means of other bias potential,
      53             : e.g. to obtain an indirect \ref METAD as in \cite continua .
      54             : Also notice that the velocities of the fictitious coordinates
      55             : are reported (XX_vfict). However, printed velocities are the ones
      56             : at the previous step.
      57             : 
      58             : It is also possible to provide a non-zero friction (one value per component).
      59             : This is then used to implement a Langevin thermostat, so as to implement
      60             : TAMD/dAFED method \cite Maragliano2006 \cite AbramsJ2008 . Notice that
      61             : here a massive Langevin thermostat is used, whereas usually
      62             : TAMD employs an overamped Langevin dynamics and dAFED
      63             : a Gaussian thermostat.
      64             : 
      65             : \warning
      66             : The bias potential is reported in the component bias.
      67             : Notice that this bias potential, although formally compatible with
      68             : replica exchange framework, probably does not work as expected in that case.
      69             : Indeed, since fictitious coordinates are not swapped upon exchange,
      70             : acceptace can be expected to be extremely low unless (by chance) two neighboring
      71             : replicas have the fictitious variables located properly in space.
      72             : 
      73             : \warning
      74             : \ref RESTART is not properly supported by this action. Indeed,
      75             : at every start the position of the fictitious variable is reset to the value
      76             : of the real variable, and its velocity is set to zero.
      77             : This is not expected to introduce big errors, but certainly is
      78             : introducing a small inconsistency between a single long run
      79             : and many shorter runs.
      80             : 
      81             : \par Examples
      82             : 
      83             : The following input tells plumed to perform a metadynamics
      84             : with an extended Lagrangian on two torsional angles.
      85             : \plumedfile
      86             : phi: TORSION ATOMS=5,7,9,15
      87             : psi: TORSION ATOMS=7,9,15,17
      88             : ex: EXTENDED_LAGRANGIAN ARG=phi,psi KAPPA=20,20.0 TAU=0.1,0.1
      89             : METAD ARG=ex.phi_fict,ex.psi_fict PACE=100 SIGMA=0.35,0.35 HEIGHT=0.1
      90             : # monitor the two variables
      91             : PRINT STRIDE=10 ARG=phi,psi,ex.phi_fict,ex.psi_fict FILE=COLVAR
      92             : \endplumedfile
      93             : 
      94             : The following input tells plumed to perform a TAMD (or dAFED)
      95             : calculation on two torsional angles, keeping the two variables
      96             : at a fictitious temperature of 3000K with a Langevin thermostat
      97             : with friction 10
      98             : \plumedfile
      99             : phi: TORSION ATOMS=5,7,9,15
     100             : psi: TORSION ATOMS=7,9,15,17
     101             : ex: EXTENDED_LAGRANGIAN ARG=phi,psi KAPPA=20,20.0 TAU=0.1,0.1 FRICTION=10,10 TEMP=3000
     102             : # monitor the two variables
     103             : PRINT STRIDE=10 ARG=phi,psi,ex.phi_fict,ex.psi_fict FILE=COLVAR
     104             : \endplumedfile
     105             : 
     106             : */
     107             : //+ENDPLUMEDOC
     108             : 
     109             : class ExtendedLagrangian : public Bias {
     110             :   bool firsttime;
     111             :   std::vector<double> fict;
     112             :   std::vector<double> vfict;
     113             :   std::vector<double> vfict_laststep;
     114             :   std::vector<double> ffict;
     115             :   std::vector<double> kappa;
     116             :   std::vector<double> tau;
     117             :   std::vector<double> friction;
     118             :   std::vector<Value*> fictValue;
     119             :   std::vector<Value*> vfictValue;
     120             :   double kbt;
     121             :   Random rand;
     122             : public:
     123             :   explicit ExtendedLagrangian(const ActionOptions&);
     124             :   void calculate() override;
     125             :   void update() override;
     126             :   static void registerKeywords(Keywords& keys);
     127             : };
     128             : 
     129       13789 : PLUMED_REGISTER_ACTION(ExtendedLagrangian,"EXTENDED_LAGRANGIAN")
     130             : 
     131           6 : void ExtendedLagrangian::registerKeywords(Keywords& keys) {
     132           6 :   Bias::registerKeywords(keys);
     133           6 :   keys.use("ARG");
     134          12 :   keys.add("compulsory","KAPPA","specifies that the restraint is harmonic and what the values of the force constants on each of the variables are");
     135          12 :   keys.add("compulsory","TAU","specifies that the restraint is harmonic and what the values of the force constants on each of the variables are");
     136          12 :   keys.add("compulsory","FRICTION","0.0","add a friction to the variable");
     137          12 :   keys.add("optional","TEMP","the system temperature - needed when FRICTION is present. If not provided will be taken from MD code (if available)");
     138           6 :   componentsAreNotOptional(keys);
     139          12 :   keys.addOutputComponent("_fict","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
     140             :                           "These quantities will named with the arguments of the bias followed by "
     141             :                           "the character string _tilde. It is possible to add forces on these variable.");
     142          12 :   keys.addOutputComponent("_vfict","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
     143             :                           "These quantities will named with the arguments of the bias followed by "
     144             :                           "the character string _tilde. It is NOT possible to add forces on these variable.");
     145           6 : }
     146             : 
     147           2 : ExtendedLagrangian::ExtendedLagrangian(const ActionOptions&ao):
     148             :   PLUMED_BIAS_INIT(ao),
     149           2 :   firsttime(true),
     150           4 :   fict(getNumberOfArguments(),0.0),
     151           2 :   vfict(getNumberOfArguments(),0.0),
     152           2 :   vfict_laststep(getNumberOfArguments(),0.0),
     153           2 :   ffict(getNumberOfArguments(),0.0),
     154           2 :   kappa(getNumberOfArguments(),0.0),
     155           2 :   tau(getNumberOfArguments(),0.0),
     156           2 :   friction(getNumberOfArguments(),0.0),
     157           2 :   fictValue(getNumberOfArguments(),NULL),
     158           2 :   vfictValue(getNumberOfArguments(),NULL),
     159           4 :   kbt(0.0) {
     160           2 :   parseVector("TAU",tau);
     161           2 :   parseVector("FRICTION",friction);
     162           2 :   parseVector("KAPPA",kappa);
     163           2 :   double temp=-1.0;
     164           2 :   parse("TEMP",temp);
     165           2 :   if(temp>=0.0) {
     166           0 :     kbt=plumed.getAtoms().getKBoltzmann()*temp;
     167             :   } else {
     168           2 :     kbt=plumed.getAtoms().getKbT();
     169             :   }
     170           2 :   checkRead();
     171             : 
     172           2 :   log.printf("  with harmonic force constant");
     173           6 :   for(unsigned i=0; i<kappa.size(); i++) {
     174           4 :     log.printf(" %f",kappa[i]);
     175             :   }
     176           2 :   log.printf("\n");
     177             : 
     178           2 :   log.printf("  with relaxation time");
     179           6 :   for(unsigned i=0; i<tau.size(); i++) {
     180           4 :     log.printf(" %f",tau[i]);
     181             :   }
     182           2 :   log.printf("\n");
     183             : 
     184             :   bool hasFriction=false;
     185           6 :   for(unsigned i=0; i<getNumberOfArguments(); ++i)
     186           4 :     if(friction[i]>0.0) {
     187             :       hasFriction=true;
     188             :     }
     189             : 
     190           2 :   if(hasFriction) {
     191           2 :     log.printf("  with friction");
     192           6 :     for(unsigned i=0; i<friction.size(); i++) {
     193           4 :       log.printf(" %f",friction[i]);
     194             :     }
     195           2 :     log.printf("\n");
     196             :   }
     197             : 
     198           2 :   log.printf("  and kbt");
     199           2 :   log.printf(" %f",kbt);
     200           2 :   log.printf("\n");
     201             : 
     202           6 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     203           4 :     std::string comp=getPntrToArgument(i)->getName()+"_fict";
     204           4 :     addComponentWithDerivatives(comp);
     205           4 :     if(getPntrToArgument(i)->isPeriodic()) {
     206             :       std::string a,b;
     207           4 :       getPntrToArgument(i)->getDomain(a,b);
     208           4 :       componentIsPeriodic(comp,a,b);
     209             :     } else {
     210           0 :       componentIsNotPeriodic(comp);
     211             :     }
     212           4 :     fictValue[i]=getPntrToComponent(comp);
     213           4 :     comp=getPntrToArgument(i)->getName()+"_vfict";
     214           4 :     addComponent(comp);
     215           4 :     componentIsNotPeriodic(comp);
     216           4 :     vfictValue[i]=getPntrToComponent(comp);
     217             :   }
     218             : 
     219           4 :   log<<"  Bibliography "<<plumed.cite("Iannuzzi, Laio, and Parrinello, Phys. Rev. Lett. 90, 238302 (2003)");
     220           2 :   if(hasFriction) {
     221           4 :     log<<plumed.cite("Maragliano and Vanden-Eijnden, Chem. Phys. Lett. 426, 168 (2006)");
     222           4 :     log<<plumed.cite("Abrams and Tuckerman, J. Phys. Chem. B 112, 15742 (2008)");
     223             :   }
     224           2 :   log<<"\n";
     225           2 : }
     226             : 
     227             : 
     228          24 : void ExtendedLagrangian::calculate() {
     229             : 
     230          24 :   if(firsttime) {
     231           6 :     for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     232           4 :       fict[i]=getArgument(i);
     233             :     }
     234           2 :     firsttime=false;
     235             :   }
     236             :   double ene=0.0;
     237          72 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     238          48 :     const double cv=difference(i,fict[i],getArgument(i));
     239          48 :     const double k=kappa[i];
     240          48 :     const double f=-k*cv;
     241          48 :     ene+=0.5*k*cv*cv;
     242             :     setOutputForce(i,f);
     243          48 :     ffict[i]=-f;
     244             :   };
     245             :   setBias(ene);
     246          72 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     247          48 :     fict[i]=fictValue[i]->bringBackInPbc(fict[i]);
     248          48 :     fictValue[i]->set(fict[i]);
     249          48 :     vfictValue[i]->set(vfict_laststep[i]);
     250             :   }
     251          24 : }
     252             : 
     253          24 : void ExtendedLagrangian::update() {
     254          24 :   double dt=getTimeStep()*getStride();
     255          72 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     256          48 :     double mass=kappa[i]*tau[i]*tau[i]/(4*pi*pi); // should be k/omega**2
     257          48 :     double c1=std::exp(-0.5*friction[i]*dt);
     258          48 :     double c2=std::sqrt(kbt*(1.0-c1*c1)/mass);
     259             : // consider additional forces on the fictitious particle
     260             : // (e.g. MetaD stuff)
     261          48 :     ffict[i]+=fictValue[i]->getForce();
     262             : 
     263             : // update velocity (half step)
     264          48 :     vfict[i]+=ffict[i]*0.5*dt/mass;
     265             : // thermostat (half step)
     266          48 :     vfict[i]=c1*vfict[i]+c2*rand.Gaussian();
     267             : // save full step velocity to be dumped at next step
     268          48 :     vfict_laststep[i]=vfict[i];
     269             : // thermostat (half step)
     270          48 :     vfict[i]=c1*vfict[i]+c2*rand.Gaussian();
     271             : // update velocity (half step)
     272          48 :     vfict[i]+=ffict[i]*0.5*dt/mass;
     273             : // update position (full step)
     274          48 :     fict[i]+=vfict[i]*dt;
     275             :   }
     276          24 : }
     277             : 
     278             : }
     279             : 
     280             : }

Generated by: LCOV version 1.16