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