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 435 : void RestraintShortcut::registerKeywords(Keywords& keys) { 40 435 : ActionShortcut::registerKeywords( keys ); 41 870 : keys.addInputKeyword("numbered","ARG","scalar/vector","the values the harmonic restraint acts upon"); 42 435 : 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 435 : 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 435 : keys.add("compulsory","AT","the position of the restraint"); 45 435 : 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 870 : keys.addOutputComponent("bias","default","scalar","the instantaneous value of the bias potential"); 47 870 : keys.addOutputComponent("force2","default","scalar/vector","the instantaneous value of the squared force due to this bias potential"); 48 435 : keys.addActionNameSuffix("_SCALAR"); 49 435 : keys.needsAction("COMBINE"); 50 435 : keys.needsAction("SUM"); 51 435 : keys.needsAction("CUSTOM"); 52 435 : keys.needsAction("BIASVALUE"); 53 435 : } 54 : 55 395 : RestraintShortcut::RestraintShortcut(const ActionOptions&ao): 56 : Action(ao), 57 395 : ActionShortcut(ao) { 58 : // Read in the args 59 395 : std::vector<std::string> args; 60 790 : parseVector("ARG",args); 61 395 : if( args.size()==0 ) { 62 0 : error("found no input arguments"); 63 : } 64 395 : std::vector<Value*> vals; 65 395 : ActionWithArguments::interpretArgumentList( args, plumed.getActionSet(), this, vals ); 66 392 : if( vals.size()==0 ) { 67 0 : error("found no input arguments"); 68 : } 69 : 70 : // Find the rank 71 392 : unsigned rank=vals[0]->getRank(); 72 904 : for(unsigned i=0; i<vals.size(); ++i) { 73 512 : if( vals[i]->getRank()>0 && vals[i]->hasDerivatives() ) { 74 0 : error("argument should not be function on grid"); 75 : } 76 512 : if( vals[i]->getRank()!=rank ) { 77 0 : error("all arguments should have same rank"); 78 : } 79 : } 80 392 : if( rank==0 ) { 81 391 : std::vector<std::string> slope(args.size()); 82 776 : parseVector("SLOPE",slope); 83 : // all the slopes are passed: checking only the first one dropped e.g. SLOPE=0.0,1.0 84 388 : std::string slopestr="SLOPE=" + slope[0]; 85 504 : for(unsigned i=1; i<slope.size(); ++i) { 86 232 : slopestr += "," + slope[i]; 87 : } 88 388 : std::string allargs=args[0]; 89 504 : for(unsigned i=1; i<args.size(); ++i) { 90 232 : allargs += "," + args[i]; 91 : } 92 1552 : readInputLine( getShortcutLabel() + ": RESTRAINT_SCALAR ARG=" + allargs + " " + slopestr + " " + convertInputLineToString() ); 93 : return; 94 388 : } 95 : 96 : std::string stride; 97 4 : parse("STRIDE",stride); 98 4 : std::vector<std::string> at; 99 8 : parseVector("AT",at); 100 4 : std::vector<std::string> slope(at.size()); 101 8 : parseVector("SLOPE",slope); 102 4 : std::vector<std::string> kappa(at.size()); 103 8 : parseVector("KAPPA",kappa); 104 : 105 : std::string biasargs, forceargs; 106 12 : for(unsigned i=0; i<args.size(); ++i) { 107 : std::string argn; 108 8 : std::size_t dot=args[i].find_first_of("."); 109 8 : if(dot!=std::string::npos) { 110 6 : argn = args[i].substr(0,dot) + "_" + args[i].substr(dot+1); 111 : } else { 112 : argn = args[i]; 113 : } 114 : double kap, slo; 115 8 : Tools::convert( kappa[i], kap ); 116 8 : Tools::convert( slope[i], slo ); 117 8 : if( kap==0 && slo==0 ) { 118 : continue; 119 : } 120 : // distance from AT: COMBINE applies the periodic boundary conditions; for a non periodic argument 121 : // the difference is computed directly in the CUSTOM functions, which saves one action 122 7 : std::vector<Value*> argvals; 123 7 : ActionWithArguments::interpretArgumentList( std::vector<std::string>(1,args[i]), plumed.getActionSet(), this, argvals ); 124 : bool periodic=false; 125 14 : for(const auto & v : argvals) { 126 7 : periodic = periodic || v->isPeriodic(); 127 : } 128 : std::string cvarg, cv; 129 7 : if( periodic ) { 130 0 : readInputLine( getShortcutLabel() + "_cv_" + argn + ": COMBINE PERIODIC=NO ARG=" + args[i] + " PARAMETERS=" + at[i] ); 131 0 : cvarg = getShortcutLabel() + "_cv_" + argn; 132 : cv = "x"; 133 : } else { 134 : cvarg = args[i]; 135 21 : cv = "(x-(" + at[i] + "))"; 136 : } 137 7 : if( kap!=0 ) { 138 28 : readInputLine( getShortcutLabel() + "_harm_" + argn + ": CUSTOM PERIODIC=NO FUNC=0.5*" + kappa[i] + "*" + cv + "^2 ARG=" + cvarg ); 139 21 : readInputLine( getShortcutLabel() + "_kap_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_harm_" + argn ); 140 24 : biasargs += (biasargs.length()==0 ? "ARG=" : ",") + getShortcutLabel() + "_kap_" + argn; 141 : } 142 7 : if( slo!=0 ) { 143 4 : readInputLine( getShortcutLabel() + "_linear_" + argn + ": CUSTOM PERIODIC=NO FUNC=" + slope[i] + "*" + cv + " ARG=" + cvarg ); 144 3 : readInputLine( getShortcutLabel() + "_slope_" + argn + ": SUM PERIODIC=NO ARG=" + getShortcutLabel() + "_linear_" + argn ); 145 4 : biasargs += (biasargs.length()==0 ? "ARG=" : ",") + getShortcutLabel() + "_slope_" + argn; 146 : } 147 : // squared force (k*x+m)^2, with x the distance from AT 148 35 : readInputLine( getShortcutLabel() + "_f2_" + argn + ": CUSTOM PERIODIC=NO FUNC=((" + kappa[i] + ")*" + cv + "+(" + slope[i] + "))^2 ARG=" + cvarg ); 149 24 : forceargs += (forceargs.length()==0 ? "ARG=" : ",") + getShortcutLabel() + "_f2_" + argn; 150 : } 151 4 : if( biasargs.length()==0 ) { 152 0 : error("all the values of KAPPA and SLOPE are zero"); 153 : } 154 : // This is the bias 155 4 : readInputLine( getShortcutLabel() + "_bias: COMBINE PERIODIC=NO " + biasargs ); 156 8 : readInputLine( getShortcutLabel() + ": BIASVALUE ARG=" + getShortcutLabel() + "_bias STRIDE=" + stride ); 157 8 : readInputLine( getShortcutLabel() + "_force2: COMBINE PERIODIC=NO " + forceargs ); 158 405 : } 159 : 160 : } 161 : 162 : 163 : }