Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2013-2018 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 : \verbatim
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 : \endverbatim
53 : (See also \ref DISTANCE, \ref SORT, and \ref PRINT).
54 :
55 : In case you want to pass a single value you can use VALUE:
56 : \verbatim
57 : cn: CONSTANT VALUE=1.0
58 : dis: DISTANCE ATOMS=1
59 : sss: SORT ARG=cn,dis
60 : PRINT ARG=sss.1
61 : \endverbatim
62 :
63 : */
64 : //+ENDPLUMEDOC
65 :
66 : using namespace std;
67 :
68 12 : class Constant : public Colvar {
69 : vector<double> values;
70 : public:
71 : explicit Constant(const ActionOptions&);
72 : virtual void calculate();
73 : static void registerKeywords( Keywords& keys );
74 : };
75 :
76 2529 : PLUMED_REGISTER_ACTION(Constant,"CONSTANT")
77 :
78 6 : Constant::Constant(const ActionOptions&ao):
79 6 : PLUMED_COLVAR_INIT(ao)
80 : {
81 6 : bool noderiv=false;
82 6 : parseFlag("NODERIV",noderiv);
83 6 : parseVector("VALUES",values);
84 6 : if(values.size()==0) {
85 : double v;
86 2 : parse("VALUE",v);
87 : // this checks if v is different from NAN
88 2 : if(v*2!=v || v==0.0) {
89 2 : values.resize(1);
90 2 : values[0]=v;
91 : }
92 : }
93 6 : if(values.size()==0) error("Either VALUE or VALUES should be used with CONSTANT");
94 6 : checkRead();
95 6 : if(values.size()==1) {
96 5 : if(!noderiv) addValueWithDerivatives();
97 0 : else addValue();
98 5 : setNotPeriodic();
99 5 : setValue(values[0]);
100 1 : } else if(values.size()>1) {
101 3 : for(unsigned i=0; i<values.size(); i++) {
102 2 : std::string num; Tools::convert(i,num);
103 2 : if(!noderiv) addComponentWithDerivatives("v_"+num);
104 0 : else addComponent("v_"+num);
105 2 : componentIsNotPeriodic("v_"+num);
106 2 : Value* comp=getPntrToComponent("v_"+num);
107 2 : comp->set(values[i]);
108 2 : }
109 : }
110 : // fake request to avoid errors:
111 6 : std::vector<AtomNumber> atoms;
112 6 : requestAtoms(atoms);
113 6 : }
114 :
115 7 : void Constant::registerKeywords( Keywords& keys ) {
116 7 : Colvar::registerKeywords( keys );
117 7 : componentsAreNotOptional(keys);
118 7 : useCustomisableComponents(keys);
119 7 : keys.remove("NUMERICAL_DERIVATIVES");
120 7 : keys.add("compulsory","VALUES","NAN","The values of the constants");
121 7 : keys.add("compulsory","VALUE","NAN","The value of the constant");
122 7 : keys.addFlag("NODERIV",false,"Set to TRUE if you want values without derivatives.");
123 7 : keys.addOutputComponent("v","default","the # value");
124 7 : }
125 :
126 : // calculator
127 19 : void Constant::calculate() {
128 19 : if(values.size()==1) {
129 14 : setValue(values[0]);
130 33 : return;
131 : }
132 15 : for(unsigned i=0; i<values.size(); i++) {
133 10 : Value* comp=getPntrToComponent(i);
134 10 : comp->set(values[i]);
135 : }
136 : }
137 :
138 : }
139 2523 : }
140 :
141 :
142 :
|