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