LCOV - code coverage report
Current view: top level - bias - BiasValue.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 19 19 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 BIASVALUE
      29             : /*
      30             : Takes the value of one variable and use it as a bias
      31             : 
      32             : This is the simplest possible bias: the bias potential is equal to a collective variable.
      33             : It is useful to create custom biasing potential, e.g. applying a function (see \ref Function)
      34             : to some collective variable then using the value of this function directly as a bias.
      35             : 
      36             : \par Examples
      37             : 
      38             : The following input tells plumed to use the value of the distance between atoms 3 and 5
      39             : and the value of the distance between atoms 2 and 4 as biases.
      40             : It then tells plumed to print the energy of the restraint
      41             : \plumedfile
      42             : DISTANCE ATOMS=3,5 LABEL=d1
      43             : DISTANCE ATOMS=3,6 LABEL=d2
      44             : BIASVALUE ARG=d1,d2 LABEL=b
      45             : PRINT ARG=d1,d2,b.d1_bias,b.d2_bias
      46             : \endplumedfile
      47             : 
      48             : Another thing one can do is asking one system to follow
      49             : a circle in sin/cos according a  time dependence
      50             : 
      51             : \plumedfile
      52             : t: TIME
      53             : # this just print cos and sin of time
      54             : cos: MATHEVAL ARG=t VAR=t FUNC=cos(t) PERIODIC=NO
      55             : sin: MATHEVAL ARG=t VAR=t FUNC=sin(t) PERIODIC=NO
      56             : c1: COM ATOMS=1,2
      57             : c2: COM ATOMS=3,4
      58             : d: DISTANCE COMPONENTS ATOMS=c1,c2
      59             : PRINT ARG=t,cos,sin,d.x,d.y,d.z STRIDE=1 FILE=colvar FMT=%8.4f
      60             : # this calculates sine and cosine of a projected component of distance
      61             : mycos:  MATHEVAL ARG=d.x,d.y  VAR=x,y   FUNC=x/sqrt(x*x+y*y) PERIODIC=NO
      62             : mysin:  MATHEVAL ARG=d.x,d.y  VAR=x,y   FUNC=y/sqrt(x*x+y*y) PERIODIC=NO
      63             : # this creates a moving spring so that the system follows a circle-like dynamics
      64             : # but it is not a bias, it is a simple value now
      65             : vv1:  MATHEVAL ARG=mycos,mysin,cos,sin VAR=mc,ms,c,s  FUNC=100*((mc-c)^2+(ms-s)^2) PERIODIC=NO
      66             : # this takes the value calculated with matheval and uses as a bias
      67             : cc: BIASVALUE ARG=vv1
      68             : # some printout
      69             : PRINT ARG=t,cos,sin,d.x,d.y,d.z,mycos,mysin,cc.vv1_bias STRIDE=1 FILE=colvar FMT=%8.4f
      70             : \endplumedfile
      71             : 
      72             : */
      73             : //+ENDPLUMEDOC
      74             : 
      75             : class BiasValue : public Bias {
      76             : public:
      77             :   explicit BiasValue(const ActionOptions&);
      78             :   void calculate() override;
      79             :   static void registerKeywords(Keywords& keys);
      80             : };
      81             : 
      82       10483 : PLUMED_REGISTER_ACTION(BiasValue,"BIASVALUE")
      83             : 
      84          33 : void BiasValue::registerKeywords(Keywords& keys) {
      85          33 :   Bias::registerKeywords(keys);
      86          33 :   keys.use("ARG");
      87             :   // Should be _bias below
      88          66 :   keys.addOutputComponent("_bias","default","one or multiple instances of this quantity can be referenced elsewhere in the input file. "
      89             :                           "these quantities will named with  the arguments of the bias followed by "
      90             :                           "the character string _bias. These quantities tell the user how much the bias is "
      91             :                           "due to each of the colvars.");
      92          33 : }
      93             : 
      94          32 : BiasValue::BiasValue(const ActionOptions&ao):
      95          32 :   PLUMED_BIAS_INIT(ao)
      96             : {
      97          32 :   checkRead();
      98             :   // add one bias for each argument
      99          65 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     100          33 :     std::string ss=getPntrToArgument(i)->getName()+"_bias";
     101          33 :     addComponent(ss); componentIsNotPeriodic(ss);
     102             :   }
     103          32 : }
     104             : 
     105         472 : void BiasValue::calculate() {
     106             :   double bias=0.0;
     107         950 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
     108             :     double val; val=getArgument(i);
     109         478 :     getPntrToComponent(i+1)->set(val);
     110         478 :     setOutputForce(i,-1.);
     111         478 :     bias+=val;
     112             :   }
     113             :   setBias(bias);
     114         472 : }
     115             : 
     116             : }
     117             : }

Generated by: LCOV version 1.15