All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ActionWithVessel.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_vesselbase_ActionWithVessel_h
23 #define __PLUMED_vesselbase_ActionWithVessel_h
24 
25 #include "core/ActionWithValue.h"
26 #include "core/ActionAtomistic.h"
27 #include "tools/Exception.h"
28 #include "tools/DynamicList.h"
29 #include <vector>
30 
31 namespace PLMD{
32 class Value;
33 
34 namespace vesselbase{
35 
36 class Vessel;
37 class BridgeVessel;
38 
39 /**
40 \ingroup MULTIINHERIT
41 This is used to create PLMD::Action objects that are computed by calculating the same function multiple
42 times. This is used in PLMD::MultiColvar.
43 */
44 
45 class ActionWithVessel : public virtual Action {
46 friend class Vessel;
47 friend class ShortcutVessel;
48 friend class FunctionVessel;
49 friend class BridgeVessel;
50 private:
51 /// This is used to ensure that we have properly read the action
52  bool read;
53 /// Do all calculations in serial
54  bool serial;
55 /// The tolerance on the accumulators
56  double tolerance;
57 /// Tolerance for quantities being put in neighbor lists
58  double nl_tolerance;
59 /// The value of the current element in the sum
60  std::vector<double> thisval;
61 /// A boolean that makes sure we don't accumulate very wrong derivatives
62  std::vector<bool> thisval_wasset;
63 /// Vector of derivatives for the object
64  std::vector<double> derivatives;
65 /// The buffers we use for mpi summing DistributionFunction objects
68  std::vector<double> buffer;
69 /// Pointers to the functions we are using on each value
70  std::vector<Vessel*> functions;
71 /// Tempory storage for forces
72  std::vector<double> tmpforces;
73 protected:
74 /// The terms in the series are locked
76 /// Does the weight have derivatives
78 /// The numerical index of the task we are curently performing
79  unsigned current;
80 /// This is used for numerical derivatives of bridge variables
81  unsigned bridgeVariable;
82 /// The list of tasks we have to perform
84 /// Add a vessel to the list of vessels
85  void addVessel( const std::string& name, const std::string& input, const int numlab=0, const std::string thislab="" );
86  void addVessel( Vessel* vv );
87 /// Add a bridging vessel to the list of vessels
89 /// Complete the setup of this object (this routine must be called after construction of ActionWithValue)
90  void readVesselKeywords();
91 /// Return the value of the tolerance
92  double getTolerance() const ;
93 /// Return the value for the neighbor list tolerance
94  double getNLTolerance() const ;
95 /// Get the number of vessels
96  unsigned getNumberOfVessels() const;
97 /// Get a pointer to the ith vessel
98  Vessel* getPntrToVessel( const unsigned& i );
99 /// Calculate the values of all the vessels
100  void runAllTasks();
101 /// Finish running all the calculations
102  void finishComputations();
103 /// Resize all the functions when the number of derivatives change
104  void resizeFunctions();
105 /// Set the derivative of the jth element wrt to a numbered element
106  void setElementDerivative( const unsigned&, const double& );
107 /// This loops over all the vessels calculating them and also
108 /// sets all the element derivatives equal to zero
109  bool calculateAllVessels();
110 /// Retrieve the forces from all the vessels (used in apply)
111  bool getForcesFromVessels( std::vector<double>& forcesToApply );
112 /// This is used to accumulate the derivatives when we merge using chainRuleForElementDerivatives
113  void accumulateDerivative( const unsigned& ider, const double& df );
114 /// Clear tempory data that is calculated for each task
115  void clearAfterTask();
116 public:
117  static void registerKeywords(Keywords& keys);
120 /// Used to make sure we are calculating everything during neighbor list update step
121  virtual void unlockContributors();
122  virtual void lockContributors();
123 /// Activate the jth colvar
124 /// Deactivate the current task in future loops
125  virtual void deactivate_task();
126 /// Merge the derivatives
127  void chainRuleForElementDerivatives( const unsigned&, const unsigned&, const double& , Vessel* );
128  void chainRuleForElementDerivatives( const unsigned&, const unsigned& , const unsigned& , const unsigned& , const double& , Vessel* );
129  virtual void mergeDerivatives( const unsigned& ider, const double& df );
130  virtual void clearDerivativesAfterTask( const unsigned& );
131 /// Are the base quantities periodic
132  virtual bool isPeriodic()=0;
133 /// What are the domains of the base quantities
134  virtual void retrieveDomain( std::string& min, std::string& max);
135 /// Get the number of derivatives for final calculated quantity
136  virtual unsigned getNumberOfDerivatives()=0;
137 /// Do any jobs that are required before the task list is undertaken
138  virtual void doJobsRequiredBeforeTaskList();
139 /// Calculate one of the functions in the distribution
140  virtual void performTask( const unsigned& j )=0;
141 /// Return a pointer to the field
142  Vessel* getVessel( const std::string& name );
143 /// Add some derivative of the quantity in the sum wrt to a numbered element
144  void addElementDerivative( const unsigned&, const double& );
145 /// Set the value of the element
146  void setElementValue( const unsigned& , const double& );
147 /// Get the value of this element
148  double getElementValue( const unsigned& ival ) const ;
149 /// Retrieve the derivative of the quantity in the sum wrt to a numbered element
150  double getElementDerivative( const unsigned& ) const ;
151 /// Apply forces from bridge vessel - this is rarely used - currently only in ActionVolume
152  virtual void applyBridgeForces( const std::vector<double>& bb ){ plumed_error(); }
153 };
154 
155 inline
157  return tolerance;
158 }
159 
160 inline
162  return nl_tolerance;
163 }
164 
165 inline
167  return functions.size();
168 }
169 
170 inline
172  plumed_dbg_assert( i<functions.size() );
173  return functions[i];
174 }
175 
176 inline
177 double ActionWithVessel::getElementValue(const unsigned& ival) const {
178  return thisval[ival];
179 }
180 
181 inline
182 void ActionWithVessel::setElementValue( const unsigned& ival, const double& val ){
183  // Element 0 is reserved for the value we are accumulating
184  // Element 1 is reserved for the normalization constant for calculating AVERAGES, normalized HISTOGRAMS
185  plumed_dbg_massert( !thisval_wasset[ival], "In action named " + getName() + " with label " + getLabel() );
186  thisval[ival]=val;
187  thisval_wasset[ival]=true;
188 }
189 
190 inline
191 double ActionWithVessel::getElementDerivative( const unsigned& ider ) const {
192  plumed_dbg_assert( ider<derivatives.size() );
193  return derivatives[ider];
194 }
195 
196 inline
197 void ActionWithVessel::addElementDerivative( const unsigned& ider, const double& der ){
198 #ifndef NDEBUG
199  unsigned ndertmp=getNumberOfDerivatives();
200  if( ider>=ndertmp && ider<2*ndertmp ) plumed_dbg_massert( weightHasDerivatives, "In " + getLabel() );
201 #endif
202  plumed_dbg_assert( ider<derivatives.size() );
203  derivatives[ider] += der;
204 }
205 
206 inline
207 void ActionWithVessel::setElementDerivative( const unsigned& ider, const double& der ){
208  plumed_dbg_assert( ider<derivatives.size() );
209  derivatives[ider] = der;
210 }
211 
212 inline
213 void ActionWithVessel::accumulateDerivative( const unsigned& ider, const double& der ){
214  plumed_dbg_assert( ider<getNumberOfDerivatives() );
216 }
217 
218 inline
220  plumed_dbg_assert( taskList.getNumberActive()==taskList.fullSize() );
222 }
223 
224 inline
227 }
228 
229 }
230 }
231 #endif
BridgeVessel * addBridgingVessel(ActionWithVessel *tome)
Add a bridging vessel to the list of vessels.
Vessel * getVessel(const std::string &name)
Return a pointer to the field.
unsigned current
The numerical index of the task we are curently performing.
unsigned current_buffer_start
The buffers we use for mpi summing DistributionFunction objects.
std::vector< Vessel * > functions
Pointers to the functions we are using on each value.
static void registerKeywords(Keywords &keys)
void chainRuleForElementDerivatives(const unsigned &, const unsigned &, const double &, Vessel *)
Merge the derivatives.
std::vector< bool > thisval_wasset
A boolean that makes sure we don't accumulate very wrong derivatives.
double getNLTolerance() const
Return the value for the neighbor list tolerance.
bool read
This is used to ensure that we have properly read the action.
void setElementDerivative(const unsigned &, const double &)
Set the derivative of the jth element wrt to a numbered element.
virtual unsigned getNumberOfDerivatives()=0
Get the number of derivatives for final calculated quantity.
void addElementDerivative(const unsigned &, const double &)
Add some derivative of the quantity in the sum wrt to a numbered element.
std::vector< double > tmpforces
Tempory storage for forces.
virtual void mergeDerivatives(const unsigned &ider, const double &df)
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
Objects that inherit from FunctionVessel can be used (in tandem with PLMD::ActionWithVessel) to calcu...
double getElementDerivative(const unsigned &) const
Retrieve the derivative of the quantity in the sum wrt to a numbered element.
void addVessel(const std::string &name, const std::string &input, const int numlab=0, const std::string thislab="")
Add a vessel to the list of vessels.
double nl_tolerance
Tolerance for quantities being put in neighbor lists.
void runAllTasks()
Calculate the values of all the vessels.
unsigned bridgeVariable
This is used for numerical derivatives of bridge variables.
bool getForcesFromVessels(std::vector< double > &forcesToApply)
Retrieve the forces from all the vessels (used in apply)
void readVesselKeywords()
Complete the setup of this object (this routine must be called after construction of ActionWithValue)...
std::vector< double > thisval
The value of the current element in the sum.
bool weightHasDerivatives
Does the weight have derivatives.
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
This class allows you to calculate the vessel in one ActionWithVessel.
Definition: BridgeVessel.h:40
virtual void retrieveDomain(std::string &min, std::string &max)
What are the domains of the base quantities.
void clearAfterTask()
Clear tempory data that is calculated for each task.
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void setElementValue(const unsigned &, const double &)
Set the value of the element.
const std::string & getName() const
Returns the name.
Definition: Action.h:268
Base class for all the input Actions.
Definition: Action.h:60
virtual void unlockContributors()
Used to make sure we are calculating everything during neighbor list update step. ...
void accumulateDerivative(const unsigned &ider, const double &df)
This is used to accumulate the derivatives when we merge using chainRuleForElementDerivatives.
bool calculateAllVessels()
This loops over all the vessels calculating them and also sets all the element derivatives equal to z...
virtual void deactivate_task()
Activate the jth colvar Deactivate the current task in future loops.
virtual void performTask(const unsigned &j)=0
Calculate one of the functions in the distribution.
DynamicList< unsigned > taskList
The list of tasks we have to perform.
unsigned getNumberOfVessels() const
Get the number of vessels.
double getTolerance() const
Return the value of the tolerance.
virtual bool isPeriodic()=0
Are the base quantities periodic.
virtual void doJobsRequiredBeforeTaskList()
Do any jobs that are required before the task list is undertaken.
bool serial
Do all calculations in serial.
unsigned fullSize() const
Return the total number of elements in the list.
Definition: DynamicList.h:225
virtual void applyBridgeForces(const std::vector< double > &bb)
Apply forces from bridge vessel - this is rarely used - currently only in ActionVolume.
ActionWithVessel(const ActionOptions &ao)
std::vector< double > derivatives
Vector of derivatives for the object.
virtual void clearDerivativesAfterTask(const unsigned &)
double tolerance
The tolerance on the accumulators.
bool contributorsAreUnlocked
The terms in the series are locked.
void resizeFunctions()
Resize all the functions when the number of derivatives change.
unsigned getNumberActive() const
Return the number of elements that are currently active.
Definition: DynamicList.h:230
void finishComputations()
Finish running all the calculations.
Vessel * getPntrToVessel(const unsigned &i)
Get a pointer to the ith vessel.
double getElementValue(const unsigned &ival) const
Get the value of this element.
This is used to create PLMD::Action objects that are computed by calculating the same function multip...