LCOV - code coverage report
Current view: top level - bias - UWalls.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 55 55 100.0 %
Date: 2024-10-11 08:09:47 Functions: 6 7 85.7 %

          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             : #include "Bias.h"
      23             : #include "ActionRegister.h"
      24             : 
      25             : namespace PLMD {
      26             : namespace bias {
      27             : 
      28             : //+PLUMEDOC BIAS UPPER_WALLS
      29             : /*
      30             : Defines a wall for the value of one or more collective variables,
      31             :  which limits the region of the phase space accessible during the simulation.
      32             : 
      33             : The restraining potential starts acting on the system when the value of the CV is greater
      34             : (in the case of UPPER_WALLS) or lower (in the case of LOWER_WALLS) than a certain limit \f$a_i\f$ (AT)
      35             : minus an offset \f$o_i\f$ (OFFSET).
      36             : The expression for the bias due to the wall is given by:
      37             : 
      38             : \f$
      39             :   \sum_i {k_i}((x_i-a_i+o_i)/s_i)^e_i
      40             : \f$
      41             : 
      42             : \f$k_i\f$ (KAPPA) is an energy constant in internal unit of the code, \f$s_i\f$ (EPS) a rescaling factor and
      43             : \f$e_i\f$ (EXP) the exponent determining the power law. By default: EXP = 2, EPS = 1.0, OFFSET = 0.
      44             : 
      45             : 
      46             : \par Examples
      47             : 
      48             : The following input tells plumed to add both a lower and an upper walls on the distance
      49             : between atoms 3 and 5 and the distance between atoms 2 and 4. The lower and upper limits
      50             : are defined at different values. The strength of the walls is the same for the four cases.
      51             : It also tells plumed to print the energy of the walls.
      52             : \plumedfile
      53             : DISTANCE ATOMS=3,5 LABEL=d1
      54             : DISTANCE ATOMS=2,4 LABEL=d2
      55             : UPPER_WALLS ARG=d1,d2 AT=1.0,1.5 KAPPA=150.0,150.0 EXP=2,2 EPS=1,1 OFFSET=0,0 LABEL=uwall
      56             : LOWER_WALLS ARG=d1,d2 AT=0.0,1.0 KAPPA=150.0,150.0 EXP=2,2 EPS=1,1 OFFSET=0,0 LABEL=lwall
      57             : PRINT ARG=uwall.bias,lwall.bias
      58             : \endplumedfile
      59             : 
      60             : */
      61             : //+ENDPLUMEDOC
      62             : 
      63             : class UWalls : public Bias {
      64             :   std::vector<double> at;
      65             :   std::vector<double> kappa;
      66             :   std::vector<double> exp;
      67             :   std::vector<double> eps;
      68             :   std::vector<double> offset;
      69             : public:
      70             :   explicit UWalls(const ActionOptions&);
      71             :   void calculate() override;
      72             :   static void registerKeywords(Keywords& keys);
      73             : };
      74             : 
      75       10453 : PLUMED_REGISTER_ACTION(UWalls,"UPPER_WALLS")
      76             : 
      77          18 : void UWalls::registerKeywords(Keywords& keys) {
      78          18 :   Bias::registerKeywords(keys);
      79          18 :   keys.use("ARG");
      80          36 :   keys.add("compulsory","AT","the positions of the wall. The a_i in the expression for a wall.");
      81          36 :   keys.add("compulsory","KAPPA","the force constant for the wall.  The k_i in the expression for a wall.");
      82          36 :   keys.add("compulsory","OFFSET","0.0","the offset for the start of the wall.  The o_i in the expression for a wall.");
      83          36 :   keys.add("compulsory","EXP","2.0","the powers for the walls.  The e_i in the expression for a wall.");
      84          36 :   keys.add("compulsory","EPS","1.0","the values for s_i in the expression for a wall");
      85          36 :   keys.addOutputComponent("force2","default","the instantaneous value of the squared force due to this bias potential");
      86          18 : }
      87             : 
      88          17 : UWalls::UWalls(const ActionOptions&ao):
      89             :   PLUMED_BIAS_INIT(ao),
      90          34 :   at(getNumberOfArguments(),0),
      91          17 :   kappa(getNumberOfArguments(),0.0),
      92          17 :   exp(getNumberOfArguments(),2.0),
      93          17 :   eps(getNumberOfArguments(),1.0),
      94          34 :   offset(getNumberOfArguments(),0.0)
      95             : {
      96             :   // Note : the sizes of these vectors are checked automatically by parseVector
      97          17 :   parseVector("OFFSET",offset);
      98          17 :   parseVector("EPS",eps);
      99          17 :   parseVector("EXP",exp);
     100          17 :   parseVector("KAPPA",kappa);
     101          17 :   parseVector("AT",at);
     102          17 :   checkRead();
     103             : 
     104          17 :   log.printf("  at");
     105          34 :   for(unsigned i=0; i<at.size(); i++) log.printf(" %f",at[i]);
     106          17 :   log.printf("\n");
     107          17 :   log.printf("  with an offset");
     108          34 :   for(unsigned i=0; i<offset.size(); i++) log.printf(" %f",offset[i]);
     109          17 :   log.printf("\n");
     110          17 :   log.printf("  with force constant");
     111          34 :   for(unsigned i=0; i<kappa.size(); i++) log.printf(" %f",kappa[i]);
     112          17 :   log.printf("\n");
     113          17 :   log.printf("  and exponent");
     114          34 :   for(unsigned i=0; i<exp.size(); i++) log.printf(" %f",exp[i]);
     115          17 :   log.printf("\n");
     116          17 :   log.printf("  rescaled");
     117          34 :   for(unsigned i=0; i<eps.size(); i++) log.printf(" %f",eps[i]);
     118          17 :   log.printf("\n");
     119             : 
     120          34 :   addComponent("force2"); componentIsNotPeriodic("force2");
     121          17 : }
     122             : 
     123       28015 : void UWalls::calculate() {
     124             :   double ene=0.0;
     125             :   double totf2=0.0;
     126       56030 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     127             :     double f = 0.0;
     128       28015 :     const double cv=difference(i,at[i],getArgument(i));
     129       28015 :     const double off=offset[i];
     130       28015 :     const double epsilon=eps[i];
     131       28015 :     const double uscale = (cv+off)/epsilon;
     132       28015 :     if( uscale > 0.) {
     133           6 :       const double k=kappa[i];
     134           6 :       const double exponent=exp[i];
     135           6 :       double power = pow( uscale, exponent );
     136           6 :       f = -( k / epsilon ) * exponent * power / uscale;
     137           6 :       ene += k * power;
     138           6 :       totf2 += f * f;
     139             :     }
     140             :     setOutputForce(i,f);
     141             :   }
     142             :   setBias(ene);
     143       28015 :   getPntrToComponent("force2")->set(totf2);
     144       28015 : }
     145             : 
     146             : }
     147             : }

Generated by: LCOV version 1.15