All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ActionWithValue.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_ActionWithValue_h
23 #define __PLUMED_core_ActionWithValue_h
24 
25 #include "Action.h"
26 #include "Value.h"
27 #include "tools/Exception.h"
28 #include <vector>
29 
30 namespace PLMD{
31 
32 /**
33 \ingroup MULTIINHERIT
34 Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some derivatives.
35 This is used for PLMD::Bias, PLMD::Colvar and PLMD::Function
36 
37 The vast majority of the PLMD::Action objects that are implemented in
38 plumed calculate some quantity or a set of quantities. This could be
39 the value of a CV, the value of a function or the potential due to a bias.
40 PLMD::ActionWithValue provides the functionality for storing these quantities
41 and (in tandem with PLMD::ActionWithArguments) the functionality for passing
42 quantities between PLMD::Actions. When you are deciding what quantities
43 your new PLMD::Action will need to store using PLMD::ActionWithValue you must
44 ask yourself the following two questions:
45 
46 - Do I need to differentiate my output quantities
47 - Is my PLMD::Action calculating a single thing or does the output have multiple components
48 
49 If the answer to the first of these questions is yes then you must setup your values
50 you using either PLMD::ActionWithValue::addValueWithDerivatives() or
51 PLMD::ActionWithValue::addComponentWithDerivatives. If the answer is no you
52 can set up values using PLMD::ActionWithValue::addValue() or PLMD::ActionWithValue::addComponent().
53 The precise routine you use to setup your values will depend on your answer to the
54 second question. As you are probably aware if the output of your PLMD::Action is a
55 single quantity you can reference that quantity in the input file using the label of the
56 PLMD::Action it was calculated in. If your action <b> outputs only one quantity </b>
57 we call that quantity the <b> value </b> of the Action. To set the <b> value </b> and get pointers to it
58 you should <b> use the set of routines that have the word value in the name </b>. If, by contrast,
59 your PLMD::Action calculates multiple quantities then these quantities are referenced in input using the
60 label.component syntax. We refer to these <b> multiple quantities </b> the <b> components </b>
61 of the PLMD::Action. Perhaps unsurprisingly, when you manipulate the <b> components </b> of an
62 PLMD::Action you should use <b> the routines with the word component in the name. </b>
63 */
64 
66  public virtual Action
67 {
68 private:
69 /// An array containing the values for this action
70  std::vector<Value*> values;
71 /// Are we using numerical derivatives to differentiate
73 /// Return the index for the component named name
74  int getComponent( const std::string& name ) const;
75 public:
76 
77 // -------- The action has one value only ---------------- //
78 
79 /// Add a value with the name label
80  void addValue();
81 /// Add a value with the name label that has derivatives
83 /// Set your default value to have no periodicity
84  void setNotPeriodic();
85 /// Set the value to be periodic with a particular domain
86  void setPeriodic( const std::string& min, const std::string& max );
87 protected:
88 /// Get a pointer to the default value
90 /// Set the default value (the one without name)
91  void setValue(const double& d);
92 
93 // -------- The action has multiple components ---------- //
94 
95 public:
96 /// Add a value with a name like label.name
97  void addComponent( const std::string& name );
98 /// Add a value with a name like label.name that has derivatives
99  void addComponentWithDerivatives( const std::string& name );
100 /// Set your value component to have no periodicity
101  void componentIsNotPeriodic( const std::string& name );
102 /// Set the value to be periodic with a particular domain
103  void componentIsPeriodic( const std::string& name, const std::string& min, const std::string& max );
104 protected:
105 /// Return a pointer to the component by index
106  Value* getPntrToComponent(int i);
107 /// Return a pointer to the value by name
108  Value* getPntrToComponent(const std::string& name);
109 public:
110  ActionWithValue(const ActionOptions&ao);
112 
113 /// Register all the relevant keywords for the action
114  static void registerKeywords( Keywords& keys );
115 /// Insist that numerical derivatives should always be used for an action and make this fact appear in the manual
116  static void noAnalyticalDerivatives(Keywords& keys);
117 /// Puts a message into the manual that the components always output
118  static void componentsAreNotOptional(Keywords& keys);
119 /// The components in the action will depend on the user
120  static void useCustomisableComponents(Keywords& keys);
121 /// Get the value of one of the components of the PLMD::Action
122  double getOutputQuantity( const unsigned j ) const ;
123 /// Get the value with a specific name (N.B. if there is no such value this returns zero)
124  double getOutputQuantity( const std::string& name ) const ;
125 
126 // --- Routines for passing stuff to ActionWithArguments -- //
127 
128 /// Check if a value with a particular name is present. This is only used in PLMD::ActionWithArguments.
129 /// You should not use it when manipulating components.
130  bool exists( const std::string& name ) const;
131 /// Return a pointer to the value with name (this is used to retrieve values in other PLMD::Actions)
132 /// You should NEVER use this routine to refer to the components of your PLMD::Action. Use
133 /// getPntrToComponent instead.
134  Value* copyOutput( const std::string&name ) const;
135 /// Return a pointer to the value with this number (this is used to retrieve values in other PLMD::Actions)
136 /// You should NEVER use this routine to refer to the components of your PLMD::Action. Use
137 /// getPntrToComponent instead.
138  Value* copyOutput( const unsigned& n ) const;
139 /// get a string that contains all the available components
140  std::string getComponentsList( ) const ;
141 /// get a vector that contains the label for all the components
142  std::vector<std::string> getComponentsVector( ) const ;
143 
144 
145 // -- Routines for everything else -- //
146 
147 /// Returns the number of values defined
148  int getNumberOfComponents() const ;
149 /// Clear the forces on the values
150  void clearInputForces();
151 /// Clear the derivatives of values wrt parameters
152  virtual void clearDerivatives();
153 /// Calculate the gradients and store them for all the values (need for projections)
154  void setGradientsIfNeeded();
155 /// Set the value
156  void setValue(Value*,double);
157 /// Check if numerical derivatives should be used
158  bool checkNumericalDerivatives() const ;
159 /// This forces the class to use numerical derivatives
161 // These are things for using vectors of values as fields
162  virtual void checkFieldsAllowed(){ error("cannot use this action as a field"); }
163  virtual unsigned getNumberOfDerivatives();
164  virtual void mergeFieldDerivatives( const std::vector<double>& derivatives, Value* val_out );
165 };
166 
167 inline
168 double ActionWithValue::getOutputQuantity(const unsigned j) const {
169  plumed_massert(j<values.size(),"index requested is out of bounds");
170  return values[j]->get();
171 }
172 
173 inline
174 double ActionWithValue::getOutputQuantity( const std::string& name ) const {
175  std::string thename; thename=getLabel() + "." + name;
176  for(unsigned i=0;i<values.size();++i){
177  if( values[i]->name==thename ) return values[i]->value;
178  }
179  return 0.0;
180 }
181 
182 inline
183 void ActionWithValue::setValue(const double& d){
184  plumed_massert(values.size()==1, "cannot use setValue in multi-component actions");
185  plumed_massert(values[0]->name==getLabel(), "The value you are trying to set is not the default");
186  values[0]->set(d);
187 }
188 
189 inline
191  return values.size();
192 }
193 
194 inline
197 }
198 
199 inline
201  return numericalDerivatives;
202 }
203 
204 inline
206  plumed_assert( !values.empty() );
207  return values[0]->getNumberOfDerivatives();
208 }
209 
210 
211 
212 }
213 
214 #endif
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
void setNotPeriodic()
Set your default value to have no periodicity.
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
virtual void checkFieldsAllowed()
int getComponent(const std::string &name) const
Return the index for the component named name.
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
virtual unsigned getNumberOfDerivatives()
std::vector< Value * > values
An array containing the values for this action.
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
static void componentsAreNotOptional(Keywords &keys)
Puts a message into the manual that the components always output.
Value * getPntrToValue()
Get a pointer to the default value.
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
void addValueWithDerivatives()
Add a value with the name label that has derivatives.
void addComponent(const std::string &name)
Add a value with a name like label.name.
void const char const char int * n
Definition: Matrix.h:42
void setGradientsIfNeeded()
Calculate the gradients and store them for all the values (need for projections)
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
This class holds the keywords and their documentation.
Definition: Keywords.h:36
const std::string name
Name of the directive in the plumed.dat file.
Definition: Action.h:64
std::string getComponentsList() const
get a string that contains all the available components
bool exists(const std::string &name) const
Check if a value with a particular name is present.
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void clearInputForces()
Clear the forces on the values.
ActionWithValue(const ActionOptions &ao)
void setPeriodic(const std::string &min, const std::string &max)
Set the value to be periodic with a particular domain.
static void useCustomisableComponents(Keywords &keys)
The components in the action will depend on the user.
Base class for all the input Actions.
Definition: Action.h:60
Value * copyOutput(const std::string &name) const
Return a pointer to the value with name (this is used to retrieve values in other PLMD::Actions) You ...
void setValue(const double &d)
Set the default value (the one without name)
void componentIsPeriodic(const std::string &name, const std::string &min, const std::string &max)
Set the value to be periodic with a particular domain.
std::vector< std::string > getComponentsVector() const
get a vector that contains the label for all the components
bool checkNumericalDerivatives() const
Check if numerical derivatives should be used.
virtual void mergeFieldDerivatives(const std::vector< double > &derivatives, Value *val_out)
double getOutputQuantity(const unsigned j) const
Get the value of one of the components of the PLMD::Action.
void addComponentWithDerivatives(const std::string &name)
Add a value with a name like label.name that has derivatives.
virtual void clearDerivatives()
Clear the derivatives of values wrt parameters.
int getNumberOfComponents() const
Returns the number of values defined.
void useNumericalDerivatives()
This forces the class to use numerical derivatives.
void addValue()
Add a value with the name label.
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
bool numericalDerivatives
Are we using numerical derivatives to differentiate.
static void noAnalyticalDerivatives(Keywords &keys)
Insist that numerical derivatives should always be used for an action and make this fact appear in th...