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 10497 : PLUMED_REGISTER_ACTION(Constant,"CONSTANT") 70 : 71 39 : Constant::Constant(const ActionOptions&ao): 72 39 : PLUMED_COLVAR_INIT(ao) 73 : { 74 39 : bool noderiv=false; 75 39 : parseFlag("NODERIV",noderiv); 76 78 : parseVector("VALUES",values); 77 : std::vector<double> value; 78 78 : parseVector("VALUE",value); 79 39 : if(values.size()==0&&value.size()==0) error("One should use either VALUE or VALUES"); 80 39 : if(values.size()!=0&&value.size()!=0) error("One should use either VALUE or VALUES"); 81 39 : if(value.size()>1) error("VALUE cannot take more than one number"); 82 39 : if(values.size()==0) { 83 11 : values.resize(1); 84 11 : values[0]=value[0]; 85 : } 86 39 : checkRead(); 87 39 : if(values.size()==1) { 88 38 : if(!noderiv) addValueWithDerivatives(); 89 0 : else addValue(); 90 38 : setNotPeriodic(); 91 38 : setValue(values[0]); 92 1 : } else if(values.size()>1) { 93 3 : for(unsigned i=0; i<values.size(); i++) { 94 2 : std::string num; Tools::convert(i,num); 95 4 : if(!noderiv) addComponentWithDerivatives("v-"+num); 96 0 : else addComponent("v-"+num); 97 2 : componentIsNotPeriodic("v-"+num); 98 2 : Value* comp=getPntrToComponent("v-"+num); 99 2 : comp->set(values[i]); 100 : } 101 : } 102 : // fake request to avoid errors: 103 : std::vector<AtomNumber> atoms; 104 39 : requestAtoms(atoms); 105 39 : } 106 : 107 40 : void Constant::registerKeywords( Keywords& keys ) { 108 40 : Colvar::registerKeywords( keys ); 109 40 : componentsAreNotOptional(keys); 110 40 : keys.remove("NUMERICAL_DERIVATIVES"); 111 80 : keys.add("optional","VALUES","The values of the constants"); 112 80 : keys.add("optional","VALUE","The value of the constant"); 113 80 : keys.addFlag("NODERIV",false,"Set to TRUE if you want values without derivatives."); 114 80 : keys.addOutputComponent("v","default","the # value"); 115 40 : } 116 : 117 : // calculator 118 3401 : void Constant::calculate() { 119 3401 : if(values.size()==1) { 120 3396 : setValue(values[0]); 121 3396 : return; 122 : } 123 15 : for(unsigned i=0; i<values.size(); i++) { 124 10 : Value* comp=getPntrToComponent(i); 125 10 : comp->set(values[i]); 126 : } 127 : } 128 : 129 : } 130 : } 131 : 132 : 133 :