All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Vessel.cpp
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 #include "ActionWithVessel.h"
23 #include "Vessel.h"
24 #include "tools/Exception.h"
25 #include "tools/Communicator.h"
26 #include "tools/Log.h"
27 
28 namespace PLMD {
29 namespace vesselbase{
30 
32 
33 VesselOptions::VesselOptions(const std::string& thisname, const std::string& thislab, const unsigned& nlab, const std::string& params, ActionWithVessel* aa ):
34 myname(thisname),
35 mylabel(thislab),
36 numlab(nlab),
37 action(aa),
38 keywords(emptyKeys),
39 parameters(params)
40 {
41 }
42 
44 myname(da.myname),
45 mylabel(da.mylabel),
46 numlab(da.numlab),
47 action(da.action),
48 keywords(keys),
49 parameters(da.parameters)
50 {
51 }
52 
54  plumed_assert( keys.size()==0 );
55 }
56 
58 myname(da.myname),
59 numlab(da.numlab),
60 action(da.action),
61 keywords(da.keywords),
62 finished_read(false),
63 comm(da.action->comm),
64 log((da.action)->log)
65 {
67  if( da.mylabel.length()==0 && numlab>=0 ){
68  mylabel=myname; std::string nn;
69  std::transform( mylabel.begin(), mylabel.end(), mylabel.begin(), tolower );
70  if(numlab>0){ Tools::convert( numlab, nn ); mylabel = mylabel + "-" + nn; }
71  } else if( numlab==0 ) {
72  mylabel=da.mylabel;
73  }
74 }
75 
76 std::string Vessel::getName() const {
77  return myname;
78 }
79 
80 std::string Vessel::getLabel() const {
81  return mylabel;
82 }
83 
84 std::string Vessel::getAllInput(){
85  std::string fullstring;
86  for(unsigned i=0;i<line.size();++i){
87  fullstring = fullstring + " " + line[i];
88  }
89  line.clear(); line.resize(0);
90  return fullstring;
91 }
92 
93 void Vessel::parseFlag(const std::string&key, bool & t){
94  // Check keyword has been registered
95  plumed_massert(keywords.exists(key), "keyword " + key + " has not been registered");
96  // Check keyword is a flag
97  if(!keywords.style(key,"nohtml")){
98  plumed_massert(keywords.style(key,"flag"), "keyword " + key + " is not a flag");
99  }
100 
101  // Read in the flag otherwise get the default value from the keywords object
102  if(!Tools::parseFlag(line,key,t)){
103  if( keywords.style(key,"nohtml") ){
104  t=false;
105  } else if ( !keywords.getLogicalDefault(key,t) ){
106  plumed_merror("there is a flag with no logical default in a vessel - weird");
107  }
108  }
109 }
110 
112  if(!line.empty()){
113  std::string msg="cannot understand the following words from input : ";
114  for(unsigned i=0;i<line.size();i++) msg = msg + line[i] + ", ";
115  error(msg);
116  }
117  finished_read=true;
118  std::string describe=description();
119  if( describe.length()>0 ) log.printf(" %s\n", describe.c_str() );
120 }
121 
122 void Vessel::error( const std::string& msg ){
123  action->log.printf("ERROR for keyword %s in action %s with label %s : %s \n \n",myname.c_str(), (action->getName()).c_str(), (action->getLabel()).c_str(), msg.c_str() );
125  plumed_merror("ERROR for keyword " + myname + " in action " + action->getName() + " with label " + action->getLabel() + " : " + msg );
126 }
127 
129  unsigned stride=comm.Get_size(); unsigned rank=comm.Get_rank();
130  unsigned n=0; for(unsigned i=rank;i<bufsize;i+=stride){ stash[n]=getBufferElement(i); n++; }
131 }
132 
134  unsigned stride=comm.Get_size(); unsigned rank=comm.Get_rank();
135  unsigned n=0; for(unsigned i=rank;i<bufsize;i+=stride){ addToBufferElement( i, stash[n]); n++; }
136 }
137 
138 }
139 }
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
std::string parameters
The parameters that are read into the function.
Definition: Vessel.h:69
Log & log
Reference to the log stream.
Definition: Action.h:93
void addToBufferElement(const unsigned &i, const double &val)
Add something to the ith element in the buffer.
Definition: Vessel.h:252
static bool parseFlag(std::vector< std::string > &line, const std::string &key, bool &val)
Find a keyword without arguments on the input line.
Definition: Tools.h:157
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
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
static Keywords emptyKeys
Definition: Vessel.h:66
void const char const char int * n
Definition: Matrix.h:42
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
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Vessel.cpp:93
int Get_rank() const
Obtain the rank of the present process.
std::string mylabel
The label for the vessel for referencing.
Definition: Vessel.h:81
VesselOptions(const std::string &thisname, const std::string &thislab, const unsigned &nlab, const std::string &params, ActionWithVessel *aa)
The constructor.
Definition: Vessel.cpp:33
static std::vector< std::string > getWords(const std::string &line, const char *sep=NULL, int *parlevel=NULL, const char *parenthesis="{")
Split the line in words using separators.
Definition: Tools.cpp:112
void print(Log &log) const
Print the documentation to the log file (used by PLMD::Action::error)
Definition: Keywords.cpp:461
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
const std::string & getName() const
Returns the name.
Definition: Action.h:268
std::string getName() const
Return the name.
Definition: Vessel.cpp:76
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 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
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
std::string myname
The keyword for the vessel in the input file.
Definition: Vessel.h:79
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
int Get_size() const
Obtain the number of processes.
bool getLogicalDefault(std::string key, bool &def) const
find out whether flag key is on or off by default.
Definition: Keywords.cpp:534
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
unsigned size() const
Return the number of defined keywords.
Definition: Keywords.cpp:230
This is used to create PLMD::Action objects that are computed by calculating the same function multip...