LCOV - code coverage report
Current view: top level - bias - RestraintShortcut.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 48 51 94.1 %
Date: 2024-10-18 13:59:31 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2020 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 "core/ActionRegister.h"
      23             : #include "core/ActionShortcut.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "core/ActionSet.h"
      26             : #include "core/ActionWithArguments.h"
      27             : 
      28             : namespace PLMD {
      29             : namespace bias {
      30             : 
      31             : class RestraintShortcut : public ActionShortcut {
      32             : public:
      33             :   static void registerKeywords(Keywords& keys);
      34             :   explicit RestraintShortcut(const ActionOptions&);
      35             : };
      36             : 
      37             : PLUMED_REGISTER_ACTION(RestraintShortcut,"RESTRAINT")
      38             : 
      39         321 : void RestraintShortcut::registerKeywords(Keywords& keys) {
      40         321 :   ActionShortcut::registerKeywords( keys );
      41         642 :   keys.addInputKeyword("numbered","ARG","scalar/vector","the values the harmonic restraint acts upon");
      42         642 :   keys.add("compulsory","SLOPE","0.0","specifies that the restraint is linear and what the values of the force constants on each of the variables are");
      43         642 :   keys.add("compulsory","KAPPA","0.0","specifies that the restraint is harmonic and what the values of the force constants on each of the variables are");
      44         642 :   keys.add("compulsory","AT","the position of the restraint");
      45         642 :   keys.add("hidden","STRIDE","1","the frequency with which the forces due to the bias should be calculated.  This can be used to correctly set up multistep algorithms");
      46         642 :   keys.addOutputComponent("bias","default","scalar","the instantaneous value of the bias potential");
      47         642 :   keys.addOutputComponent("force2","default","scalar/vector","the instantaneous value of the squared force due to this bias potential");
      48        1605 :   keys.addActionNameSuffix("_SCALAR"); keys.needsAction("COMBINE"); keys.needsAction("SUM"); keys.needsAction("CUSTOM"); keys.needsAction("BIASVALUE");
      49         321 : }
      50             : 
      51         290 : RestraintShortcut::RestraintShortcut(const ActionOptions&ao):
      52             :   Action(ao),
      53         290 :   ActionShortcut(ao)
      54             : {
      55             :   // Read in the args
      56         580 :   std::vector<std::string> args; parseVector("ARG",args);
      57         290 :   if( args.size()==0 ) error("found no input arguments");
      58         290 :   std::vector<Value*> vals; ActionWithArguments::interpretArgumentList( args, plumed.getActionSet(), this, vals );
      59         287 :   if( vals.size()==0 ) error("found no input arguments");
      60             : 
      61             :   // Find the rank
      62         287 :   unsigned rank=vals[0]->getRank();
      63         630 :   for(unsigned i=0; i<vals.size(); ++i) {
      64         343 :     if( vals[i]->getRank()>0 && vals[i]->hasDerivatives() ) error("argument should not be function on grid");
      65         343 :     if( vals[i]->getRank()!=rank ) error("all arguments should have same rank");
      66             :   }
      67         287 :   if( rank==0 ) {
      68         575 :     std::vector<std::string> slope(args.size()); parseVector("SLOPE",slope); std::string slopestr="";
      69         386 :     if( slope[0]!="0.0" ) { slopestr="SLOPE=" + slope[0]; for(unsigned i=1; i<slope.size(); ++i) slopestr += "," + slope[i]; }
      70         342 :     std::string allargs=args[0]; for(unsigned i=1; i<args.size(); ++i) allargs += "," + args[i];
      71         572 :     readInputLine( getShortcutLabel() + ": RESTRAINT_SCALAR ARG=" + allargs + " " + slopestr + " " + convertInputLineToString() );
      72             :     return;
      73         286 :   }
      74             : 
      75           2 :   std::string stride; parse("STRIDE",stride);
      76           2 :   std::vector<std::string> at; parseVector("AT",at);
      77           2 :   std::vector<std::string> slope(at.size()); parseVector("SLOPE",slope);
      78           2 :   std::vector<std::string> kappa(at.size()); parseVector("KAPPA",kappa);
      79             : 
      80             :   std::string biasargs, forceargs; bool non_constant_force=false;
      81           2 :   for(unsigned i=0; i<args.size(); ++i) {
      82           1 :     std::string argn; std::size_t dot=args[i].find_first_of(".");
      83           1 :     if(dot!=std::string::npos) argn = args[i].substr(0,dot) + "_" + args[i].substr(dot+1);
      84             :     else argn = args[i];
      85           2 :     readInputLine( getShortcutLabel() + "_cv_" + argn + ": COMBINE PERIODIC=NO ARG=" + args[i] + " PARAMETERS=" + at[i] );
      86           1 :     double kap; Tools::convert(  kappa[i], kap );
      87           1 :     if( fabs(kap)>0 ) {
      88             :       non_constant_force = true;
      89           2 :       readInputLine( getShortcutLabel() + "_harm_" + argn + ": CUSTOM PERIODIC=NO FUNC=0.5*" + kappa[i] + "*x^2 ARG=" + getShortcutLabel() + "_cv_" + argn );
      90           2 :       readInputLine( getShortcutLabel() + "_kap_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_harm_" + argn );
      91           2 :       readInputLine( getShortcutLabel() + "_f2_" + argn + ": CUSTOM PERIODIC=NO FUNC=" + kappa[i] + "*2*x+" + slope[i] + "*" + slope[i] + " ARG=" + getShortcutLabel() + "_harm_" + argn );
      92           2 :       if( i==0 ) biasargs = "ARG=" + getShortcutLabel() + "_kap_" + argn; else biasargs += "," + getShortcutLabel() + "_kap_" + argn;
      93           2 :       if( i==0 ) forceargs = "ARG=" + getShortcutLabel() + "_f2_" + argn; else forceargs += "," + getShortcutLabel() + "_f2_" + argn;
      94             :     }
      95           1 :     double slo; Tools::convert( slope[i], slo );
      96           1 :     if( fabs(slo)>0 ) {
      97           0 :       readInputLine( getShortcutLabel() + "_linear_" + argn + ": CUSTOM PERIODIC=NO FUNC=" + slope[i] + "*x ARG=" + getShortcutLabel() + "_cv_" + argn );
      98           0 :       readInputLine( getShortcutLabel() + "_slope_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_linear_" + argn );
      99           0 :       if( biasargs.length()==0 ) biasargs = "ARG=" + getShortcutLabel() + "_slope_" + argn; else biasargs += "," + getShortcutLabel() + "_slope_" + argn;
     100             :     }
     101             :   }
     102             :   // This is the bias
     103           2 :   readInputLine( getShortcutLabel() + "_bias: COMBINE PERIODIC=NO " + biasargs );
     104           2 :   readInputLine( getShortcutLabel() + ": BIASVALUE ARG=" + getShortcutLabel() + "_bias STRIDE=" + stride );
     105           2 :   if( non_constant_force ) readInputLine( getShortcutLabel() + "_force2: COMBINE PERIODIC=NO " + forceargs );
     106         297 : }
     107             : 
     108             : }
     109             : 
     110             : 
     111             : }

Generated by: LCOV version 1.16