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 322 : void RestraintShortcut::registerKeywords(Keywords& keys) { 40 322 : ActionShortcut::registerKeywords( keys ); 41 644 : keys.addInputKeyword("numbered","ARG","scalar/vector","the values the harmonic restraint acts upon"); 42 322 : 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 322 : 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 322 : keys.add("compulsory","AT","the position of the restraint"); 45 322 : 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 644 : keys.addOutputComponent("bias","default","scalar","the instantaneous value of the bias potential"); 47 644 : keys.addOutputComponent("force2","default","scalar/vector","the instantaneous value of the squared force due to this bias potential"); 48 322 : keys.addActionNameSuffix("_SCALAR"); 49 322 : keys.needsAction("COMBINE"); 50 322 : keys.needsAction("SUM"); 51 322 : keys.needsAction("CUSTOM"); 52 322 : keys.needsAction("BIASVALUE"); 53 322 : } 54 : 55 291 : RestraintShortcut::RestraintShortcut(const ActionOptions&ao): 56 : Action(ao), 57 291 : ActionShortcut(ao) { 58 : // Read in the args 59 : std::vector<std::string> args; 60 582 : parseVector("ARG",args); 61 291 : if( args.size()==0 ) { 62 0 : error("found no input arguments"); 63 : } 64 : std::vector<Value*> vals; 65 291 : ActionWithArguments::interpretArgumentList( args, plumed.getActionSet(), this, vals ); 66 288 : if( vals.size()==0 ) { 67 0 : error("found no input arguments"); 68 : } 69 : 70 : // Find the rank 71 288 : unsigned rank=vals[0]->getRank(); 72 638 : for(unsigned i=0; i<vals.size(); ++i) { 73 350 : if( vals[i]->getRank()>0 && vals[i]->hasDerivatives() ) { 74 0 : error("argument should not be function on grid"); 75 : } 76 350 : if( vals[i]->getRank()!=rank ) { 77 0 : error("all arguments should have same rank"); 78 : } 79 : } 80 288 : if( rank==0 ) { 81 290 : std::vector<std::string> slope(args.size()); 82 287 : parseVector("SLOPE",slope); 83 287 : std::string slopestr=""; 84 287 : if( slope[0]!="0.0" ) { 85 64 : slopestr="SLOPE=" + slope[0]; 86 107 : for(unsigned i=1; i<slope.size(); ++i) { 87 86 : slopestr += "," + slope[i]; 88 : } 89 : } 90 287 : std::string allargs=args[0]; 91 349 : for(unsigned i=1; i<args.size(); ++i) { 92 124 : allargs += "," + args[i]; 93 : } 94 574 : readInputLine( getShortcutLabel() + ": RESTRAINT_SCALAR ARG=" + allargs + " " + slopestr + " " + convertInputLineToString() ); 95 : return; 96 287 : } 97 : 98 : std::string stride; 99 2 : parse("STRIDE",stride); 100 : std::vector<std::string> at; 101 2 : parseVector("AT",at); 102 1 : std::vector<std::string> slope(at.size()); 103 2 : parseVector("SLOPE",slope); 104 1 : std::vector<std::string> kappa(at.size()); 105 2 : parseVector("KAPPA",kappa); 106 : 107 : std::string biasargs, forceargs; 108 : bool non_constant_force=false; 109 2 : for(unsigned i=0; i<args.size(); ++i) { 110 : std::string argn; 111 1 : std::size_t dot=args[i].find_first_of("."); 112 1 : if(dot!=std::string::npos) { 113 0 : argn = args[i].substr(0,dot) + "_" + args[i].substr(dot+1); 114 : } else { 115 : argn = args[i]; 116 : } 117 2 : readInputLine( getShortcutLabel() + "_cv_" + argn + ": COMBINE PERIODIC=NO ARG=" + args[i] + " PARAMETERS=" + at[i] ); 118 : double kap; 119 1 : Tools::convert( kappa[i], kap ); 120 1 : if( fabs(kap)>0 ) { 121 : non_constant_force = true; 122 2 : readInputLine( getShortcutLabel() + "_harm_" + argn + ": CUSTOM PERIODIC=NO FUNC=0.5*" + kappa[i] + "*x^2 ARG=" + getShortcutLabel() + "_cv_" + argn ); 123 2 : readInputLine( getShortcutLabel() + "_kap_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_harm_" + argn ); 124 2 : readInputLine( getShortcutLabel() + "_f2_" + argn + ": CUSTOM PERIODIC=NO FUNC=" + kappa[i] + "*2*x+" + slope[i] + "*" + slope[i] + " ARG=" + getShortcutLabel() + "_harm_" + argn ); 125 1 : if( i==0 ) { 126 2 : biasargs = "ARG=" + getShortcutLabel() + "_kap_" + argn; 127 : } else { 128 0 : biasargs += "," + getShortcutLabel() + "_kap_" + argn; 129 : } 130 1 : if( i==0 ) { 131 2 : forceargs = "ARG=" + getShortcutLabel() + "_f2_" + argn; 132 : } else { 133 0 : forceargs += "," + getShortcutLabel() + "_f2_" + argn; 134 : } 135 : } 136 : double slo; 137 1 : Tools::convert( slope[i], slo ); 138 1 : if( fabs(slo)>0 ) { 139 0 : readInputLine( getShortcutLabel() + "_linear_" + argn + ": CUSTOM PERIODIC=NO FUNC=" + slope[i] + "*x ARG=" + getShortcutLabel() + "_cv_" + argn ); 140 0 : readInputLine( getShortcutLabel() + "_slope_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_linear_" + argn ); 141 0 : if( biasargs.length()==0 ) { 142 0 : biasargs = "ARG=" + getShortcutLabel() + "_slope_" + argn; 143 : } else { 144 0 : biasargs += "," + getShortcutLabel() + "_slope_" + argn; 145 : } 146 : } 147 : } 148 : // This is the bias 149 2 : readInputLine( getShortcutLabel() + "_bias: COMBINE PERIODIC=NO " + biasargs ); 150 2 : readInputLine( getShortcutLabel() + ": BIASVALUE ARG=" + getShortcutLabel() + "_bias STRIDE=" + stride ); 151 1 : if( non_constant_force ) { 152 2 : readInputLine( getShortcutLabel() + "_force2: COMBINE PERIODIC=NO " + forceargs ); 153 : } 154 298 : } 155 : 156 : } 157 : 158 : 159 : }