LCOV - code coverage report
Current view: top level - bias - UWalls.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 69 70 98.6 %
Date: 2026-11-12 10:48:31 Functions: 3 4 75.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             : #include "Bias.h"
      23             : #include "core/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             : for UPPER_WALLS:
      39             : \f$
      40             :   \sum_i {k_i}((x_i-a_i+o_i)/s_i)^e_i
      41             : \f$
      42             : 
      43             : for LOWER_WALLS:
      44             : \f$
      45             :   \sum_i {k_i}|(x_i-a_i-o_i)/s_i|^e_i
      46             : \f$
      47             : 
      48             : \f$k_i\f$ (KAPPA) is an energy constant in internal unit of the code, \f$s_i\f$ (EPS) a rescaling factor and
      49             : \f$e_i\f$ (EXP) the exponent determining the power law. By default: EXP = 2, EPS = 1.0, OFFSET = 0.
      50             : 
      51             : 
      52             : \par Examples
      53             : 
      54             : The following input tells plumed to add both a lower and an upper walls on the distance
      55             : between atoms 3 and 5 and the distance between atoms 2 and 4. The lower and upper limits
      56             : are defined at different values. The strength of the walls is the same for the four cases.
      57             : It also tells plumed to print the energy of the walls.
      58             : \plumedfile
      59             : DISTANCE ATOMS=3,5 LABEL=d1
      60             : DISTANCE ATOMS=2,4 LABEL=d2
      61             : 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
      62             : 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
      63             : PRINT ARG=uwall.bias,lwall.bias
      64             : \endplumedfile
      65             : 
      66             : 
      67             : \par Periodic arguments
      68             : 
      69             : If an argument is periodic (e.g. a \ref TORSION), the wall acts on the value of the argument in its periodic domain
      70             : and periodic boundary conditions are __not__ applied to the difference \f$x_i-a_i\f$. The value of AT must thus be within the
      71             : periodic domain of the argument. This is the case both when the arguments are scalars and when they are vectors.
      72             : The following input restrains a torsion within the interval \f$[-2,2]\f$:
      73             : \plumedfile
      74             : t: TORSION ATOMS=1,2,3,4
      75             : uwall: UPPER_WALLS ARG=t AT=2.0 KAPPA=150.0
      76             : lwall: LOWER_WALLS ARG=t AT=-2.0 KAPPA=150.0
      77             : PRINT ARG=t,uwall.bias,lwall.bias
      78             : \endplumedfile
      79             : 
      80             : Notice that the bias is discontinuous at the boundary of the periodic domain (here at \f$\pm\pi\f$). You should thus make sure that
      81             : the walls keep the argument far from this boundary, e.g. by using both an upper and a lower wall as in the input above.
      82             : 
      83             : Versions of PLUMED before 2.9.7 applied periodic boundary conditions to the difference \f$x_i-a_i\f$. With that choice, each wall acts on half of the
      84             : periodic domain, and the region left free by an upper and a lower wall is smaller than the one between AT values
      85             : (see \issue{658}).
      86             : 
      87             : */
      88             : //+ENDPLUMEDOC
      89             : 
      90             : //+PLUMEDOC BIAS UPPER_WALLS_SCALAR
      91             : /*
      92             : Defines a wall for the value of one or more collective variables,
      93             :  which limits the region of the phase space accessible during the simulation.
      94             : 
      95             : \par Examples
      96             : 
      97             : */
      98             : //+ENDPLUMEDOC
      99             : 
     100             : class UWalls : public Bias {
     101             :   std::vector<double> at;
     102             :   std::vector<double> kappa;
     103             :   std::vector<double> exp;
     104             :   std::vector<double> eps;
     105             :   std::vector<double> offset;
     106             : public:
     107             :   explicit UWalls(const ActionOptions&);
     108             :   void calculate() override;
     109             :   static void registerKeywords(Keywords& keys);
     110             : };
     111             : 
     112             : PLUMED_REGISTER_ACTION(UWalls,"UPPER_WALLS_SCALAR")
     113             : 
     114          38 : void UWalls::registerKeywords(Keywords& keys) {
     115          38 :   Bias::registerKeywords(keys);
     116          38 :   keys.setDisplayName("UPPER_WALLS");
     117          38 :   keys.use("ARG");
     118          76 :   keys.add("hidden","NO_ACTION_LOG","suppresses printing from action on the log");
     119          76 :   keys.add("compulsory","AT","the positions of the wall. The a_i in the expression for a wall.");
     120          76 :   keys.add("compulsory","KAPPA","the force constant for the wall.  The k_i in the expression for a wall.");
     121          76 :   keys.add("compulsory","OFFSET","0.0","the offset for the start of the wall.  The o_i in the expression for a wall.");
     122          76 :   keys.add("compulsory","EXP","2.0","the powers for the walls.  The e_i in the expression for a wall.");
     123          76 :   keys.add("compulsory","EPS","1.0","the values for s_i in the expression for a wall");
     124          76 :   keys.addOutputComponent("force2","default","the instantaneous value of the squared force due to this bias potential");
     125          38 : }
     126             : 
     127          17 : UWalls::UWalls(const ActionOptions&ao):
     128             :   PLUMED_BIAS_INIT(ao),
     129          34 :   at(getNumberOfArguments(),0),
     130          17 :   kappa(getNumberOfArguments(),0.0),
     131          17 :   exp(getNumberOfArguments(),2.0),
     132          17 :   eps(getNumberOfArguments(),1.0),
     133          34 :   offset(getNumberOfArguments(),0.0) {
     134             :   // Note : the sizes of these vectors are checked automatically by parseVector
     135          17 :   parseVector("OFFSET",offset);
     136          17 :   parseVector("EPS",eps);
     137          17 :   parseVector("EXP",exp);
     138          17 :   parseVector("KAPPA",kappa);
     139          17 :   parseVector("AT",at);
     140          17 :   checkRead();
     141             : 
     142          17 :   log.printf("  at");
     143          36 :   for(unsigned i=0; i<at.size(); i++) {
     144          19 :     log.printf(" %f",at[i]);
     145             :   }
     146          17 :   log.printf("\n");
     147          17 :   log.printf("  with an offset");
     148          36 :   for(unsigned i=0; i<offset.size(); i++) {
     149          19 :     log.printf(" %f",offset[i]);
     150             :   }
     151          17 :   log.printf("\n");
     152          17 :   log.printf("  with force constant");
     153          36 :   for(unsigned i=0; i<kappa.size(); i++) {
     154          19 :     log.printf(" %f",kappa[i]);
     155             :   }
     156          17 :   log.printf("\n");
     157          17 :   log.printf("  and exponent");
     158          36 :   for(unsigned i=0; i<exp.size(); i++) {
     159          19 :     log.printf(" %f",exp[i]);
     160             :   }
     161          17 :   log.printf("\n");
     162          17 :   log.printf("  rescaled");
     163          36 :   for(unsigned i=0; i<eps.size(); i++) {
     164          19 :     log.printf(" %f",eps[i]);
     165             :   }
     166          17 :   log.printf("\n");
     167             : 
     168             :   // The wall acts on the value of the argument in its domain, without periodic images
     169             :   // (see https://github.com/plumed/plumed2/issues/658).
     170          36 :   for(unsigned i=0; i<getNumberOfArguments(); i++) {
     171             :     Value* arg=getPntrToArgument(i);
     172          19 :     if( arg->isPeriodic() ) {
     173             :       double min, max;
     174           4 :       arg->getDomain( min, max );
     175           4 :       if( at[i]<min || at[i]>max ) {
     176           0 :         error("the position of the wall (AT) for periodic argument " + arg->getName() + " should be within its periodic domain");
     177             :       }
     178           4 :       log.printf("  WARNING: argument %s is periodic. The wall acts on its value in the domain [%f,%f]\n"
     179             :                  "    without periodic images, so the bias is discontinuous at the boundary of the domain.\n"
     180             :                  "    Make sure that the walls keep the argument far from it (e.g. use both an upper and a lower wall).\n",
     181             :                  arg->getName().c_str(), min, max );
     182             :     }
     183             :   }
     184             : 
     185          34 :   addComponent("force2");
     186          17 :   componentIsNotPeriodic("force2");
     187          17 : }
     188             : 
     189       14127 : void UWalls::calculate() {
     190             :   double ene=0.0;
     191             :   double totf2=0.0;
     192       28318 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     193             :     double f = 0.0;
     194             :     // no periodic boundary conditions here, see the documentation and issue #658
     195       14191 :     const double cv=getArgument(i)-at[i];
     196       14191 :     const double off=offset[i];
     197       14191 :     const double epsilon=eps[i];
     198       14191 :     const double uscale = (cv+off)/epsilon;
     199       14191 :     if( uscale > 0.) {
     200          57 :       const double k=kappa[i];
     201          57 :       const double exponent=exp[i];
     202          57 :       double power = pow( uscale, exponent );
     203          57 :       f = -( k / epsilon ) * exponent * power / uscale;
     204          57 :       ene += k * power;
     205          57 :       totf2 += f * f;
     206             :     }
     207       14191 :     setOutputForce(i,f);
     208             :   }
     209       14127 :   setBias(ene);
     210       14127 :   getPntrToComponent("force2")->set(totf2);
     211       14127 : }
     212             : 
     213             : }
     214             : }

Generated by: LCOV version 1.16