All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Value.h
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 #ifndef __PLUMED_core_Value_h
23 #define __PLUMED_core_Value_h
24 
25 #include <vector>
26 #include <string>
27 #include <map>
28 #include "tools/Exception.h"
29 #include "tools/Tools.h"
30 #include "tools/AtomNumber.h"
31 #include "tools/Vector.h"
32 
33 namespace PLMD{
34 
35 class ActionWithValue;
36 
37 /// \ingroup TOOLBOX
38 /// A class for holding the value of a function together with its derivatives.
39 /// Typically, an object of type PLMD::ActionWithValue will contain one
40 /// object of type PLUMD::Value that will be named after the label. If the
41 /// PLMD::ActionWithValue is part of a class that calculates multiple components
42 /// then the class will contain multiple that will be called label.component-name
43 /// This class is used to pass information between different PLMD::Action
44 /// objects. However, if you find a use for a tempory PLMD::Value in some method
45 /// you are implementing please feel free to use it.
46 class Value{
47 friend class ActionWithValue;
48 /// This copies the contents of a value into a second value (just the derivatives and value)
49 friend void copy( const Value& val1, Value& val2 );
50 /// This copies the contents of a value into a second value (but second value is a pointer)
51 friend void copy( const Value& val, Value* val2 );
52 /// This adds some derivatives onto the value
53 friend void add( const Value& val1, Value* valout );
54 /// This calculates val1*val2 and sorts out the derivatives
55 friend void product( const Value& val1, const Value& val2, Value& valout );
56 /// This calculates va1/val2 and sorts out the derivatives
57 friend void quotient( const Value& val1, const Value& val2, Value* valout );
58 private:
59 /// The action in which this quantity is calculated
61 /// Had the value been set
62  bool value_set;
63 /// The value of the quantity
64  double value;
65 /// The force acting on this quantity
66  double inputForce;
67 /// A flag telling us we have a force acting on this quantity
68  bool hasForce;
69 /// The derivatives of the quantity stored in value
70  std::vector<double> derivatives;
71  std::map<AtomNumber,Vector> gradients;
72 /// The name of this quantiy
73  std::string name;
74 /// Does this quanity have derivatives
75  bool hasDeriv;
76 /// Is this quantity periodic
78 /// Various quantities that describe the domain of this value
79  std::string str_min, str_max;
80  double min,max;
81  double max_minus_min;
83 /// Complete the setup of the periodicity
84  void setupPeriodicity();
85 public:
86 /// A constructor that can be used to make Vectors of values
87  Value();
88 /// A constructor that is used throughout the code to setup the value poiters
89  Value(ActionWithValue* av, const std::string& name, const bool withderiv);
90 /// Set the value of the function
91  void set(double);
92 /// Add something to the value of the function
93  void add(double);
94 /// Get the value of the function
95  double get() const;
96 /// Find out if the value has been set
97  bool valueHasBeenSet() const;
98 /// Check if the value is periodic
99  bool isPeriodic() const;
100 /// Set the function not periodic
101  void setNotPeriodic();
102 /// Set the domain of the function
103  void setDomain(const std::string&, const std::string&);
104 /// Get the domain of the quantity
105  void getDomain(std::string&,std::string&) const;
106 /// Get the domain of the quantity
107  void getDomain(double&,double&) const;
108 /// Get the name of the quantity
109  const std::string& getName() const;
110 /// Check whether or not this particular quantity has derivatives
111  bool hasDerivatives()const;
112 /// Get the number of derivatives that this particular value has
113  unsigned getNumberOfDerivatives() const;
114 /// Set the number of derivatives
115  void resizeDerivatives(int n);
116 /// Set all the derivatives to zero
117  void clearDerivatives();
118 /// Add some derivative to the ith component of the derivatives array
119  void addDerivative(unsigned i,double d);
120 /// Apply the chain rule to the derivatives
121  void chainRule(double df);
122 /// Get the derivative with respect to component n
123  double getDerivative(const unsigned n) const;
124 /// Clear the input force on the variable
125  void clearInputForce();
126 /// Add some force on this value
127  void addForce(double f);
128 /// Get the value of the force on this colvar
129  double getForce() const ;
130 /// Apply the forces to the derivatives using the chain rule (if there are no forces this routine returns false
131  bool applyForce( std::vector<double>& forces ) const ;
132 /// Calculate the difference between the instantaneous value of the function and some other point: other_point-inst_val
133  double difference(double)const;
134 /// Calculate the difference between two values of this function: d2 -d1
135  double difference(double d1,double d2)const;
136 /// This returns the pointer to the action where this value is calculated
138 /// Bring back one value into the correct pbc if needed, else give back the value
139  double bringBackInPbc(double d1)const;
140 /// This sets up the gradients
141  void setGradients();
142  static double projection(const Value&,const Value&);
143 };
144 
145 void copy( const Value& val1, Value& val2 );
146 void copy( const Value& val1, Value* val2 );
147 void add( const Value& val1, Value* valout );
148 
149 inline
150 void product( const Value& val1, const Value& val2, Value& valout ){
151  plumed_assert( val1.derivatives.size()==val2.derivatives.size() );
152  if( valout.derivatives.size()!=val1.derivatives.size() ) valout.derivatives.resize( val1.derivatives.size() );
153  valout.value_set=false; valout.derivatives.assign(valout.derivatives.size(),0.0);
154  double u, v; u=val1.value; v=val2.value;
155  for(unsigned i=0;i<val1.derivatives.size();++i){
156  valout.addDerivative(i, u*val2.derivatives[i] + v*val1.derivatives[i] );
157  }
158  valout.set( u*v );
159 }
160 
161 inline
162 void quotient( const Value& val1, const Value& val2, Value* valout ){
163  plumed_assert( val1.derivatives.size()==val2.derivatives.size() );
164  if( valout->derivatives.size()!=val1.derivatives.size() ) valout->derivatives.resize( val1.derivatives.size() );
165  valout->value_set=false; valout->derivatives.assign(valout->derivatives.size(),0.0);
166  double u, v; u=val1.get(); v=val2.get();
167  for(unsigned i=0;i<val1.getNumberOfDerivatives();++i){
168  valout->addDerivative(i, v*val1.getDerivative(i) - u*val2.getDerivative(i) );
169  }
170  valout->chainRule( 1/(v*v) ); valout->set( u / v );
171 }
172 
173 inline
174 void Value::set(double v){
175  value_set=true;
176  value=v;
177 }
178 
179 inline
180 void Value::add(double v){
181  value_set=true;
182  value+=v;
183 }
184 
185 inline
186 double Value::get()const{
187  return value;
188 }
189 
190 inline
192  return value_set;
193 }
194 
195 inline
196 const std::string& Value::getName()const{
197  return name;
198 }
199 
200 inline
202  plumed_massert(hasDeriv,"the derivatives array for this value has zero size");
203  return derivatives.size();
204 }
205 
206 inline
207 double Value::getDerivative(const unsigned n) const {
208  plumed_dbg_massert(n<derivatives.size(),"you are asking for a derivative that is out of bounds");
209  return derivatives[n];
210 }
211 
212 inline
213 bool Value::hasDerivatives() const {
214  return (!derivatives.empty());
215 }
216 
217 inline
219  plumed_massert(hasDeriv,"cannot resize derivatives in values that have not got derivatives");
220  derivatives.resize(n);
221 }
222 
223 inline
224 void Value::addDerivative(unsigned i,double d){
225  plumed_dbg_massert(i<derivatives.size(),"derivative is out of bounds");
226  derivatives[i]+=d;
227 }
228 
229 inline
230 void Value::chainRule(double df){
231  for(unsigned i=0;i<derivatives.size();++i) derivatives[i]*=df;
232 }
233 
234 inline
236  hasForce=false;
237  inputForce=0.0;
238 }
239 
240 inline
242  value_set=false;
243  derivatives.assign(derivatives.size(),0.0);
244 }
245 
246 inline
247 void Value::addForce(double f){
248  plumed_massert(hasDerivatives(),"forces can only be added to values with derivatives");
249  hasForce=true;
250  inputForce+=f;
251 }
252 
253 inline
254 double Value::getForce() const {
255  return inputForce;
256 }
257 /// d2-d1
258 inline
259 double Value::difference(double d1,double d2)const{
261  return d2-d1;
262  }else if(periodicity==periodic){
263  double s=(d2-d1)*inv_max_minus_min;
264  // remember: pbc brings the difference in a range of -0.5:0.5
265  s=Tools::pbc(s);
266  return s*max_minus_min;
267  } else plumed_merror("periodicity should be set to compute differences");
268 }
269 
270 inline
271 double Value::bringBackInPbc(double d1)const{
272  return difference(min+max_minus_min/2. , d1);
273 }
274 
275 inline
276 double Value::difference(double d)const{
277  return difference(get(),d);
278 }
279 
280 }
281 
282 #endif
283 
ActionWithValue * action
The action in which this quantity is calculated.
Definition: Value.h:60
friend void copy(const Value &val1, Value &val2)
This copies the contents of a value into a second value (just the derivatives and value) ...
Definition: Value.cpp:159
const std::string & getName() const
Get the name of the quantity.
Definition: Value.h:196
friend void add(const Value &val1, Value *valout)
This adds some derivatives onto the value.
Definition: Value.cpp:175
Value()
A constructor that can be used to make Vectors of values.
Definition: Value.cpp:33
double difference(double) const
Calculate the difference between the instantaneous value of the function and some other point: other_...
Definition: Value.h:276
void addForce(double f)
Add some force on this value.
Definition: Value.h:247
void add(const Value &val1, Value *val2)
Definition: Value.cpp:175
void copy(const Value &val1, Value &val2)
Definition: Value.cpp:159
bool applyForce(std::vector< double > &forces) const
Apply the forces to the derivatives using the chain rule (if there are no forces this routine returns...
Definition: Value.cpp:80
double get() const
Get the value of the function.
Definition: Value.h:186
void clearInputForce()
Clear the input force on the variable.
Definition: Value.h:235
double inputForce
The force acting on this quantity.
Definition: Value.h:66
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
unsigned getNumberOfDerivatives() const
Get the number of derivatives that this particular value has.
Definition: Value.h:201
void clearDerivatives()
Set all the derivatives to zero.
Definition: Value.h:241
static double pbc(double)
Apply pbc for a unitary cell.
Definition: Tools.h:170
double max_minus_min
Definition: Value.h:81
double inv_max_minus_min
Definition: Value.h:82
void setGradients()
This sets up the gradients.
Definition: Value.cpp:111
void const char const char int * n
Definition: Matrix.h:42
bool value_set
Had the value been set.
Definition: Value.h:62
bool valueHasBeenSet() const
Find out if the value has been set.
Definition: Value.h:191
double max
Definition: Value.h:80
bool hasForce
A flag telling us we have a force acting on this quantity.
Definition: Value.h:68
void set(double)
Set the value of the function.
Definition: Value.h:174
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
void getDomain(std::string &, std::string &) const
Get the domain of the quantity.
Definition: Value.cpp:99
void setNotPeriodic()
Set the function not periodic.
Definition: Value.cpp:87
friend void quotient(const Value &val1, const Value &val2, Value *valout)
This calculates va1/val2 and sorts out the derivatives.
Definition: Value.h:162
double min
Definition: Value.h:80
bool hasDeriv
Does this quanity have derivatives.
Definition: Value.h:75
static double projection(const Value &, const Value &)
Definition: Value.cpp:140
void resizeDerivatives(int n)
Set the number of derivatives.
Definition: Value.h:218
ActionWithValue * getPntrToAction()
This returns the pointer to the action where this value is calculated.
Definition: Value.cpp:154
friend void product(const Value &val1, const Value &val2, Value &valout)
This calculates val1*val2 and sorts out the derivatives.
Definition: Value.h:150
double getForce() const
Get the value of the force on this colvar.
Definition: Value.h:254
std::vector< double > derivatives
The derivatives of the quantity stored in value.
Definition: Value.h:70
enum PLMD::Value::@1 periodicity
Is this quantity periodic.
void product(const Value &val1, const Value &val2, Value &valout)
Definition: Value.h:150
void setupPeriodicity()
Complete the setup of the periodicity.
Definition: Value.cpp:64
std::string str_max
Definition: Value.h:79
bool isPeriodic() const
Check if the value is periodic.
Definition: Value.cpp:75
double bringBackInPbc(double d1) const
Bring back one value into the correct pbc if needed, else give back the value.
Definition: Value.h:271
std::string name
The name of this quantiy.
Definition: Value.h:73
std::string str_min
Various quantities that describe the domain of this value.
Definition: Value.h:79
std::map< AtomNumber, Vector > gradients
Definition: Value.h:71
void chainRule(double df)
Apply the chain rule to the derivatives.
Definition: Value.h:230
void quotient(const Value &val1, const Value &val2, Value *valout)
Definition: Value.h:162
void addDerivative(unsigned i, double d)
Add some derivative to the ith component of the derivatives array.
Definition: Value.h:224
double getDerivative(const unsigned n) const
Get the derivative with respect to component n.
Definition: Value.h:207
void setDomain(const std::string &, const std::string &)
Set the domain of the function.
Definition: Value.cpp:91
bool hasDerivatives() const
Check whether or not this particular quantity has derivatives.
Definition: Value.h:213
double value
The value of the quantity.
Definition: Value.h:64