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 722 : Bias::Bias(const ActionOptions&ao): 29 : Action(ao), 30 : ActionPilot(ao), 31 : ActionWithValue(ao), 32 : ActionWithArguments(ao), 33 722 : outputForces(getNumberOfArguments(),0.0) { 34 719 : addComponentWithDerivatives("bias"); 35 719 : componentIsNotPeriodic("bias"); 36 719 : valueBias=getPntrToComponent("bias"); 37 : 38 719 : 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 4666 : for(unsigned i=0; i<getNumberOfArguments(); ++i) { 43 3947 : (getPntrToArgument(i)->getPntrToAction())->turnOnDerivatives(); 44 : } 45 : 46 719 : ActionWithValue::turnOnDerivatives(); 47 722 : } 48 : 49 823 : void Bias::registerKeywords( Keywords& keys ) { 50 823 : Action::registerKeywords(keys); 51 823 : ActionPilot::registerKeywords(keys); 52 823 : ActionWithValue::registerKeywords(keys); 53 823 : ActionWithArguments::registerKeywords(keys); 54 1646 : 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 823 : componentsAreNotOptional(keys); 56 1646 : keys.addOutputComponent("bias","default","the instantaneous value of the bias potential"); 57 823 : } 58 : 59 81061 : void Bias::apply() { 60 81061 : const unsigned noa=getNumberOfArguments(); 61 81061 : const unsigned ncp=getNumberOfComponents(); 62 : 63 81061 : if(onStep()) { 64 81061 : double gstr = static_cast<double>(getStride()); 65 231175 : for(unsigned i=0; i<noa; ++i) { 66 150114 : getPntrToArgument(i)->addForce(gstr*outputForces[i]); 67 : } 68 : } 69 : 70 : // additional forces on the bias component 71 81061 : std::vector<double> f(noa,0.0); 72 81061 : std::vector<double> forces(noa); 73 : 74 : bool at_least_one_forced=false; 75 416703 : for(unsigned i=0; i<ncp; ++i) { 76 335642 : if(getPntrToComponent(i)->applyForce(forces)) { 77 : at_least_one_forced=true; 78 913 : for(unsigned j=0; j<noa; j++) { 79 607 : f[j]+=forces[j]; 80 : } 81 : } 82 : } 83 : 84 81061 : if(at_least_one_forced && !onStep()) { 85 0 : error("you are biasing a bias with an inconsistent STRIDE"); 86 : } 87 : 88 81061 : if(at_least_one_forced) 89 841 : for(unsigned i=0; i<noa; ++i) { 90 559 : getPntrToArgument(i)->addForce(f[i]); 91 : } 92 : 93 81061 : } 94 : 95 : } 96 : } 97 : 98 :