LCOV - code coverage report
Current view: top level - bias - Bias.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 38 39 97.4 %
Date: 2025-11-25 13:55:50 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             : 
      24             : 
      25             : namespace PLMD {
      26             : namespace bias {
      27             : 
      28         996 : Bias::Bias(const ActionOptions&ao):
      29             :   Action(ao),
      30             :   ActionPilot(ao),
      31             :   ActionWithValue(ao),
      32             :   ActionWithArguments(ao),
      33         996 :   outputForces(getNumberOfArguments(),0.0) {
      34        1992 :   addComponentWithDerivatives("bias");
      35         996 :   componentIsNotPeriodic("bias");
      36         996 :   valueBias=getPntrToComponent("bias");
      37             : 
      38         996 :   if(getStride()>1) {
      39           3 :     log<<"  multiple time step "<<getStride()<<" ";
      40           6 :     log<<cite("Ferrarotti, Bottaro, Perez-Villa, and Bussi, J. Chem. Theory Comput. 11, 139 (2015)")<<"\n";
      41             :   }
      42        5256 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
      43        4260 :     (getPntrToArgument(i)->getPntrToAction())->turnOnDerivatives();
      44             :   }
      45             : 
      46         996 :   ActionWithValue::turnOnDerivatives();
      47         996 : }
      48             : 
      49        1357 : void Bias::registerKeywords( Keywords& keys ) {
      50        1357 :   Action::registerKeywords(keys);
      51        1357 :   ActionPilot::registerKeywords(keys);
      52        1357 :   ActionWithValue::registerKeywords(keys);
      53        1357 :   ActionWithArguments::registerKeywords(keys);
      54        2714 :   keys.add("hidden","STRIDE","the frequency with which the forces due to the bias should be calculated.  This can be used to correctly set up multistep algorithms");
      55        2714 :   keys.addOutputComponent("bias","default","the instantaneous value of the bias potential");
      56        1357 : }
      57             : 
      58      101634 : void Bias::apply() {
      59      101634 :   const unsigned noa=getNumberOfArguments();
      60      101634 :   const unsigned ncp=getNumberOfComponents();
      61             : 
      62      101634 :   if(onStep()) {
      63      101634 :     double gstr = static_cast<double>(getStride());
      64      280031 :     for(unsigned i=0; i<noa; ++i) {
      65      178397 :       getPntrToArgument(i)->addForce(gstr*outputForces[i]);
      66             :     }
      67             :   }
      68             : 
      69      101634 :   f.assign(noa,0.0);
      70      101634 :   forces.resize(noa);
      71             : 
      72             :   bool at_least_one_forced=false;
      73      486131 :   for(unsigned i=0; i<ncp; ++i) {
      74      384497 :     if(getPntrToComponent(i)->applyForce(forces)) {
      75             :       at_least_one_forced=true;
      76         913 :       for(unsigned j=0; j<noa; j++) {
      77         607 :         f[j]+=forces[j];
      78             :       }
      79             :     }
      80             :   }
      81             : 
      82      101634 :   if(at_least_one_forced && !onStep()) {
      83           0 :     error("you are biasing a bias with an inconsistent STRIDE");
      84             :   }
      85             : 
      86      101634 :   if(at_least_one_forced)
      87         841 :     for(unsigned i=0; i<noa; ++i) {
      88         559 :       getPntrToArgument(i)->addForce(f[i]);
      89             :     }
      90             : 
      91      101634 : }
      92             : 
      93             : }
      94             : }
      95             : 
      96             : 

Generated by: LCOV version 1.16