Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2013-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 "Colvar.h" 23 : #include "ActionRegister.h" 24 : #include "core/PlumedMain.h" 25 : #include "core/Atoms.h" 26 : 27 : namespace PLMD { 28 : namespace colvar { 29 : 30 : //+PLUMEDOC COLVAR CONSTANT 31 : /* 32 : Return one or more constant quantities with or without derivatives. 33 : 34 : Useful in combination with functions that 35 : takes in input constants or parameters. 36 : 37 : \par Examples 38 : 39 : The following input instructs plumed to compute the distance 40 : between atoms 1 and 2. If this distance is between 1.0 and 2.0, it is 41 : printed. If it is lower than 1.0 (larger than 2.0), 1.0 (2.0) is printed 42 : 43 : \plumedfile 44 : cn: CONSTANT VALUES=1.0,2.0 45 : dis: DISTANCE ATOMS=1,2 46 : sss: SORT ARG=cn.v-0,dis,cn.v-1 47 : PRINT ARG=sss.2 48 : \endplumedfile 49 : 50 : In case you want to pass a single value you can use VALUE: 51 : \plumedfile 52 : cn: CONSTANT VALUE=1.0 53 : dis: DISTANCE ATOMS=1,2 54 : sss: SORT ARG=cn,dis 55 : PRINT ARG=sss.1 56 : \endplumedfile 57 : 58 : */ 59 : //+ENDPLUMEDOC 60 : 61 : class Constant : public Colvar { 62 : std::vector<double> values; 63 : public: 64 : explicit Constant(const ActionOptions&); 65 : void calculate() override; 66 : static void registerKeywords( Keywords& keys ); 67 : }; 68 : 69 13863 : PLUMED_REGISTER_ACTION(Constant,"CONSTANT") 70 : 71 39 : Constant::Constant(const ActionOptions&ao): 72 39 : PLUMED_COLVAR_INIT(ao) { 73 39 : bool noderiv=false; 74 39 : parseFlag("NODERIV",noderiv); 75 78 : parseVector("VALUES",values); 76 : std::vector<double> value; 77 78 : parseVector("VALUE",value); 78 39 : if(values.size()==0&&value.size()==0) { 79 0 : error("One should use either VALUE or VALUES"); 80 : } 81 39 : if(values.size()!=0&&value.size()!=0) { 82 0 : error("One should use either VALUE or VALUES"); 83 : } 84 39 : if(value.size()>1) { 85 0 : error("VALUE cannot take more than one number"); 86 : } 87 39 : if(values.size()==0) { 88 11 : values.resize(1); 89 11 : values[0]=value[0]; 90 : } 91 39 : checkRead(); 92 39 : if(values.size()==1) { 93 38 : if(!noderiv) { 94 38 : addValueWithDerivatives(); 95 : } else { 96 0 : addValue(); 97 : } 98 38 : setNotPeriodic(); 99 38 : setValue(values[0]); 100 1 : } else if(values.size()>1) { 101 3 : for(unsigned i=0; i<values.size(); i++) { 102 : std::string num; 103 2 : Tools::convert(i,num); 104 2 : if(!noderiv) { 105 4 : addComponentWithDerivatives("v-"+num); 106 : } else { 107 0 : addComponent("v-"+num); 108 : } 109 2 : componentIsNotPeriodic("v-"+num); 110 2 : Value* comp=getPntrToComponent("v-"+num); 111 2 : comp->set(values[i]); 112 : } 113 : } 114 : // fake request to avoid errors: 115 : std::vector<AtomNumber> atoms; 116 39 : requestAtoms(atoms); 117 39 : } 118 : 119 43 : void Constant::registerKeywords( Keywords& keys ) { 120 43 : Colvar::registerKeywords( keys ); 121 43 : componentsAreNotOptional(keys); 122 43 : keys.remove("NUMERICAL_DERIVATIVES"); 123 86 : keys.add("optional","VALUES","The values of the constants"); 124 86 : keys.add("optional","VALUE","The value of the constant"); 125 86 : keys.addFlag("NODERIV",false,"Set to TRUE if you want values without derivatives."); 126 86 : keys.addOutputComponent("v","default","the # value"); 127 43 : } 128 : 129 : // calculator 130 3401 : void Constant::calculate() { 131 3401 : if(values.size()==1) { 132 3396 : setValue(values[0]); 133 3396 : return; 134 : } 135 15 : for(unsigned i=0; i<values.size(); i++) { 136 10 : Value* comp=getPntrToComponent(i); 137 10 : comp->set(values[i]); 138 : } 139 : } 140 : 141 : } 142 : } 143 : 144 : 145 :