All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vessel.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_Vessel_h
23 #define __PLUMED_vesselbase_Vessel_h
24 
25 #include <string>
26 #include <cstring>
27 #include <vector>
28 #include <algorithm>
29 #include "tools/Exception.h"
30 #include "tools/Keywords.h"
31 #include "ActionWithVessel.h"
32 
33 namespace PLMD{
34 
35 class Communicator;
36 class Log;
37 
38 namespace vesselbase{
39 
40 /**
41 \ingroup TOOLBOX
42 Vessels are an important component of class PLMD::ActionWithVessel. This class
43 contains a large buffer array of doubles. The various elements of this array
44 can be accessed through vessels which are used to structure the elements of the
45 double array. As the buffer array is just a vector of doubles it can be easily
46 mpi gathered or passed to another node.
47 */
48 
49 //class ActionWithVessel;
50 class Vessel;
51 
52 /// This class is used to pass the input to Vessels
54  friend class Vessel;
55 private:
56 /// The name of the particular vessel
57  std::string myname;
58 /// The label for this particular vessel;
59  std::string mylabel;
60 /// The numerical label for this vessel
61  int numlab;
62 /// Pointer to ActionWithVessel that this if from
64 /// The keywords
67 public:
68 /// The parameters that are read into the function
69  std::string parameters;
70 /// The constructor
71  VesselOptions( const std::string& thisname, const std::string& thislab, const unsigned& nlab, const std::string& params, ActionWithVessel* aa );
72  VesselOptions(const VesselOptions& da, const Keywords& keys );
73 };
74 
75 class Vessel {
76 friend class ActionWithVessel;
77 private:
78 /// The keyword for the vessel in the input file
79  std::string myname;
80 /// The label for the vessel for referencing
81  std::string mylabel;
82 /// The numerical label for this object
83  const int numlab;
84 /// The action that this vessel is created within
86 /// Something to store the buffer if this is required
87  std::vector<double> stash;
88 /// The start of this Vessel's buffer in buffer in the underlying ActionWithVessel
89  unsigned bufstart;
90 /// The number of elements in this vessel's buffered data
91  unsigned bufsize;
92 /// Directive line.
93 /// This line is progressively erased during vessel construction
94 /// so as to check if all the present keywords are correct.
95  std::vector<std::string> line;
96 /// The keywords
98 /// This just checks we have done checkRead
100 protected:
101 /// A copy of the communicator
103 /// Return the numerical label
104  int getNumericalLabel() const ;
105 /// Report an error
106  void error(const std::string& errmsg);
107 /// Parse something from the input
108  template<class T>
109  void parse(const std::string&key, T&t);
110 /// Parse one keyword as std::vector
111  template<class T>
112  void parseVector(const std::string&key,std::vector<T>&t);
113 /// Parse one keyword as boolean flag
114  void parseFlag(const std::string&key,bool&t);
115 /// This returns the whole input line (it is used for less_than/more_than/between)
116  std::string getAllInput();
117 /// Return a pointer to the action we are working in
119 /// Return the value of the tolerance
120  double getTolerance() const ;
121 /// Return the value of the neighbor list tolerance
122  double getNLTolerance() const ;
123 /// Set the size of the data buffer
124  void resizeBuffer( const unsigned& n );
125 /// Set the value of the ith element in the buffer
126  void setBufferElement( const unsigned& i, const double& val);
127 /// Get the value in the ith element of the buffer
128  double getBufferElement( const unsigned& i ) const ;
129 /// Store everything that is the buffers
130  void stashBuffers();
131 /// Add the contents of the stash to the buffer
132  void setBufferFromStash();
133 public:
134 /// Reference to the log on which to output details
136 /// Reserve any keywords for this particular vessel
137  static void registerKeywords( Keywords& keys );
138 /// The constructor
139  Vessel( const VesselOptions& da );
140 /// Virtual destructor needed for proper inheritance
141  virtual ~Vessel(){}
142 /// Return the name
143  std::string getName() const ;
144 /// Return the label
145  std::string getLabel() const ;
146 /// Check that readin was fine
147  void checkRead();
148 /// Set all the buffer elements to zero
149  void zero();
150 /// Add something to the ith element in the buffer
151  void addToBufferElement( const unsigned& i, const double& val);
152 /// Return a description of the vessel contents
153  virtual std::string description()=0;
154 /// Do something before the loop
155  virtual void prepare(){}
156 /// Calculate the part of the vessel that is done in the loop
157  virtual bool calculate()=0;
158 /// Complete the calculation once the loop is finished
159  virtual void finish()=0;
160 /// Reset the size of the buffers
161  virtual void resize()=0;
162 /// Retrieve the forces on the quantities in the vessel
163  virtual bool applyForce( std::vector<double>& forces )=0;
164 /// Retrieve the number of terms we need to accumulate
165  virtual unsigned getNumberOfTerms()=0;
166 };
167 
168 template<class T>
169 void Vessel::parse(const std::string&key, T&t ){
170  plumed_massert(keywords.exists(key),"keyword " + key + " has not been registered");
171 
172  // Now try to read the keyword
173  bool found=Tools::parse(line,key,t); std::string def;
174  if ( !found && keywords.style(key,"compulsory") ){
175  if( keywords.getDefaultValue(key,def) ){
176  plumed_massert( def.length()!=0 && Tools::convert(def,t), "default value is dubious");
177  } else {
178  error("keyword " + key + " is comulsory for this vessel");
179  }
180  }
181 }
182 
183 template<class T>
184 void Vessel::parseVector(const std::string&key,std::vector<T>&t){
185  // Check keyword has been registered
186  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
187  unsigned size=t.size(); bool skipcheck=false;
188  if(size==0) skipcheck=true;
189 
190  // Now try to read the keyword
191  bool found; std::string def; T val;
192  found=Tools::parseVector(line,key,t);
193 
194  // Check vectors size is correct (not if this is atoms or ARG)
195  if( !keywords.style(key,"atoms") && found ){
196  if( !skipcheck && t.size()!=size ) error("vector read in for keyword " + key + " has the wrong size");
197  }
198 
199  // If it isn't read and it is compulsory see if a default value was specified
200  if ( !found && keywords.style(key,"compulsory") ){
201  if( keywords.getDefaultValue(key,def) ){
202  if( def.length()==0 || !Tools::convert(def,val) ){
203  plumed_merror("weird default value for keyword " + key );
204  } else {
205  for(unsigned i=0;i<t.size();++i) t[i]=val;
206  }
207  } else {
208  error("keyword " + key + " is compulsory");
209  }
210  } else if ( !found ){
211  t.resize(0);
212  }
213 }
214 
215 inline
217  return numlab;
218 }
219 
220 inline
221 void Vessel::resizeBuffer( const unsigned& n ){
222  bufsize=n; stash.resize(bufsize);
223 }
224 
225 inline
226 double Vessel::getTolerance() const {
227  return action->tolerance;
228 }
229 
230 inline
231 double Vessel::getNLTolerance() const {
232  return action->nl_tolerance;
233 }
234 
235 inline
237  return action;
238 }
239 
240 inline
242  for(unsigned i=0;i<bufsize;++i) setBufferElement( i, 0.0 );
243 }
244 
245 inline
246 void Vessel::setBufferElement( const unsigned& i, const double& val){
247  plumed_dbg_assert( i<bufsize );
248  action->buffer[bufstart+i]=val;
249 }
250 
251 inline
252 void Vessel::addToBufferElement( const unsigned& i, const double& val){
253  plumed_dbg_assert( i<bufsize );
254  action->buffer[bufstart+i]+=val;
255 }
256 
257 inline
258 double Vessel::getBufferElement( const unsigned& i ) const {
259  plumed_dbg_assert( i<bufsize );
260  return action->buffer[bufstart+i];
261 }
262 
263 }
264 }
265 #endif
virtual void resize()=0
Reset the size of the buffers.
void setBufferFromStash()
Add the contents of the stash to the buffer.
Definition: Vessel.cpp:133
void stashBuffers()
Store everything that is the buffers.
Definition: Vessel.cpp:128
void zero()
Set all the buffer elements to zero.
Definition: Vessel.h:241
std::string parameters
The parameters that are read into the function.
Definition: Vessel.h:69
static bool parseVector(std::vector< std::string > &line, const std::string &key, std::vector< T > &val)
Find a keyword on the input line, eventually deleting it, and saving its value to a vector...
Definition: Tools.h:135
virtual ~Vessel()
Virtual destructor needed for proper inheritance.
Definition: Vessel.h:141
double getTolerance() const
Return the value of the tolerance.
Definition: Vessel.h:226
void addToBufferElement(const unsigned &i, const double &val)
Add something to the ith element in the buffer.
Definition: Vessel.h:252
int numlab
The numerical label for this vessel.
Definition: Vessel.h:61
virtual std::string description()=0
Return a description of the vessel contents.
static void registerKeywords(Keywords &keys)
Reserve any keywords for this particular vessel.
Definition: Vessel.cpp:53
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
Communicator & comm
A copy of the communicator.
Definition: Vessel.h:102
double getBufferElement(const unsigned &i) const
Get the value in the ith element of the buffer.
Definition: Vessel.h:258
Class containing wrappers to MPI.
Definition: Communicator.h:44
static bool parse(std::vector< std::string > &line, const std::string &key, T &val)
Find a keyword on the input line, eventually deleting it, and saving its value to val...
Definition: Tools.h:127
static Keywords emptyKeys
Definition: Vessel.h:66
double nl_tolerance
Tolerance for quantities being put in neighbor lists.
virtual unsigned getNumberOfTerms()=0
Retrieve the number of terms we need to accumulate.
Class containing the log stream.
Definition: Log.h:35
void const char const char int * n
Definition: Matrix.h:42
bool getDefaultValue(std::string key, std::string &def) const
Get the value of the default for the keyword named key.
Definition: Keywords.cpp:543
std::string getLabel() const
Return the label.
Definition: Vessel.cpp:80
bool finished_read
This just checks we have done checkRead.
Definition: Vessel.h:99
std::vector< double > stash
Something to store the buffer if this is required.
Definition: Vessel.h:87
This class holds the keywords and their documentation.
Definition: Keywords.h:36
virtual void finish()=0
Complete the calculation once the loop is finished.
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Vessel.cpp:93
std::string mylabel
The label for the vessel for referencing.
Definition: Vessel.h:81
virtual bool calculate()=0
Calculate the part of the vessel that is done in the loop.
void resizeBuffer(const unsigned &n)
Set the size of the data buffer.
Definition: Vessel.h:221
const Keywords & keywords
The keywords.
Definition: Vessel.h:65
VesselOptions(const std::string &thisname, const std::string &thislab, const unsigned &nlab, const std::string &params, ActionWithVessel *aa)
The constructor.
Definition: Vessel.cpp:33
int getNumericalLabel() const
Return the numerical label.
Definition: Vessel.h:216
virtual bool applyForce(std::vector< double > &forces)=0
Retrieve the forces on the quantities in the vessel.
std::string getName() const
Return the name.
Definition: Vessel.cpp:76
void setBufferElement(const unsigned &i, const double &val)
Set the value of the ith element in the buffer.
Definition: Vessel.h:246
unsigned bufsize
The number of elements in this vessel's buffered data.
Definition: Vessel.h:91
ActionWithVessel * action
The action that this vessel is created within.
Definition: Vessel.h:85
Vessel(const VesselOptions &da)
The constructor.
Definition: Vessel.cpp:57
bool style(const std::string &k, const std::string &t) const
Check if the keyword with name k has style t.
Definition: Keywords.cpp:223
void error(const std::string &errmsg)
Report an error.
Definition: Vessel.cpp:122
std::string myname
The name of the particular vessel.
Definition: Vessel.h:57
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Vessel.h:184
std::string mylabel
The label for this particular vessel;.
Definition: Vessel.h:59
This class is used to pass the input to Vessels.
Definition: Vessel.h:53
std::vector< std::string > line
Directive line.
Definition: Vessel.h:95
const PLMD::Keywords & keywords
The keywords.
Definition: Vessel.h:97
ActionWithVessel * action
Pointer to ActionWithVessel that this if from.
Definition: Vessel.h:63
bool exists(const std::string &k) const
Check if there is a keyword with name k.
Definition: Keywords.cpp:239
void int double * da
Definition: Matrix.h:47
virtual void prepare()
Do something before the loop.
Definition: Vessel.h:155
unsigned bufstart
The start of this Vessel's buffer in buffer in the underlying ActionWithVessel.
Definition: Vessel.h:89
std::string myname
The keyword for the vessel in the input file.
Definition: Vessel.h:79
void parse(const std::string &key, T &t)
Parse something from the input.
Definition: Vessel.h:169
void checkRead()
Check that readin was fine.
Definition: Vessel.cpp:111
Log & log
Reference to the log on which to output details.
Definition: Vessel.h:135
double tolerance
The tolerance on the accumulators.
double getNLTolerance() const
Return the value of the neighbor list tolerance.
Definition: Vessel.h:231
std::string getAllInput()
This returns the whole input line (it is used for less_than/more_than/between)
Definition: Vessel.cpp:84
const int numlab
The numerical label for this object.
Definition: Vessel.h:83
ActionWithVessel * getAction()
Return a pointer to the action we are working in.
Definition: Vessel.h:236
This is used to create PLMD::Action objects that are computed by calculating the same function multip...