All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Read.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 "core/ActionPilot.h"
23 #include "core/ActionWithValue.h"
24 #include "core/ActionRegister.h"
25 #include "core/PlumedMain.h"
26 #include "core/ActionSet.h"
27 #include "core/Atoms.h"
28 #include "tools/IFile.h"
29 
30 namespace PLMD{
31 namespace generic{
32 
33 //+PLUMEDOC GENERIC READ
34 /*
35 Read quantities from a colvar file.
36 
37 This Action can be used with driver to read in a colvar file that was generated during
38 an MD simulation
39 
40 \par Description of components
41 
42 The READ command will read those fields that are labelled with the text string given to the
43 VALUE keyword. It will also read in any fields that are labelleled with the text string
44 given to the VALUE keyword followed by a dot and a further string. If a single Value is read in
45 this value can be referenced using the label of the Action. Alternatively, if multiple quanties
46 are read in, they can be referenced elsewhere in the input by using the label for the Action
47 followed by a dot and the character string that appeared after the dot in the title of the field.
48 
49 \par Examples
50 
51 This input reads in data from a file called input_colvar.data that was generated
52 in a calculation that involved PLUMED. The first command reads in the data from the
53 column headed phi1 while the second reads in the data from the column headed phi2.
54 
55 \verbatim
56 rphi1: READ FILE=input_colvar.data VALUES=phi1
57 rphi2: READ FILE=input_colvar.data VALUES=phi2
58 PRINT ARG=rphi1,rphi2 STRIDE=500 FILE=output_colvar.data
59 \endverbatim
60 
61 */
62 //+ENDPLUMEDOC
63 
64 class Read :
65 public ActionPilot,
66 public ActionWithValue
67 {
68 private:
70  unsigned nlinesPerStep;
71  std::string filename;
73  std::vector<Value*> readvals;
74 public:
75  static void registerKeywords( Keywords& keys );
76  Read(const ActionOptions&);
77  ~Read();
78  void prepare();
79  void apply(){}
80  void calculate();
81  void update();
82  std::string getFilename() const;
83  IFile* getFile();
84 };
85 
86 PLUMED_REGISTER_ACTION(Read,"READ")
87 
88 void Read::registerKeywords(Keywords& keys){
92  keys.add("compulsory","STRIDE","1","the frequency with which the file should be read.");
93  keys.add("compulsory","EVERY","1","only read every ith line of the colvar file. This should be used if the colvar was written more frequently than the trajectory.");
94  keys.add("compulsory","VALUES","the values to read from the file");
95  keys.add("compulsory","FILE","the name of the file from which to read these quantities");
96  keys.remove("NUMERICAL_DERIVATIVES");
98 }
99 
101 Action(ao),
102 ActionPilot(ao),
103 ActionWithValue(ao),
104 nlinesPerStep(1)
105 {
106  // Read the file name from the input line
107  parse("FILE",filename);
108  // Open the file if it is not already opened
109  cloned_file=false;
110  std::vector<Read*> other_reads=plumed.getActionSet().select<Read*>();
111  for(unsigned i=0;i<other_reads.size();++i){
112  if( other_reads[i]->getFilename()==filename ){
113  ifile=other_reads[i]->getFile();
114  cloned_file=true;
115  }
116  }
117  if( !cloned_file ){
118  ifile=new IFile();
119  if( !ifile->FileExist(filename) ) error("could not find file named " + filename);
120  ifile->link(*this);
121  ifile->open(filename);
123  }
124  parse("EVERY",nlinesPerStep);
125  if(nlinesPerStep>1) log.printf(" only reading every %uth line of file %s\n",nlinesPerStep,filename.c_str() );
126  else log.printf(" reading data from file %s\n",filename.c_str() );
127  // Find out what we are reading
128  std::vector<std::string> valread; parseVector("VALUES",valread);
129 
130  std::size_t dot=valread[0].find_first_of('.');
131  if( valread[0].find(".")!=std::string::npos ){
132  std::string label=valread[0].substr(0,dot);
133  std::string name=valread[0].substr(dot+1);
134  if( name=="*" ){
135  if( valread.size()>1 ) error("all values must be from the same Action when using READ");
136  std::vector<std::string> fieldnames;
137  ifile->scanFieldList( fieldnames );
138  for(unsigned i=0;i<fieldnames.size();++i){
139  if( fieldnames[i].substr(0,dot)==label ){
140  readvals.push_back(new Value(this, fieldnames[i], false) ); addComponent( fieldnames[i].substr(dot+1) );
141  if( ifile->FieldExist("min_" + fieldnames[i]) ) componentIsPeriodic( fieldnames[i].substr(dot+1), "-pi","pi" );
142  else componentIsNotPeriodic( fieldnames[i].substr(dot+1) );
143  }
144  }
145  } else {
146  readvals.push_back(new Value(this, valread[0], false) ); addComponent( name );
147  if( ifile->FieldExist("min_" + valread[0]) ) componentIsPeriodic( valread[0].substr(dot+1), "-pi", "pi" );
148  else componentIsNotPeriodic( valread[0].substr(dot+1) );
149  for(unsigned i=1;i<valread.size();++i) {
150  if( valread[i].substr(0,dot)!=label ) error("all values must be from the same Action when using READ");;
151  readvals.push_back(new Value(this, valread[i], false) ); addComponent( valread[i].substr(dot+1) );
152  if( ifile->FieldExist("min_" + valread[i]) ) componentIsPeriodic( valread[i].substr(dot+1), "-pi", "pi" );
153  else componentIsNotPeriodic( valread[i].substr(dot+1) );
154  }
155  }
156  } else {
157  if( valread.size()!=1 ) error("all values must be from the same Action when using READ");
158  readvals.push_back(new Value(this, valread[0], false) ); addValue();
159  if( ifile->FieldExist("min_" + valread[0]) ) setPeriodic( "-pi", "pi" );
160  else setNotPeriodic();
161  log.printf(" reading value %s and storing as %s\n",valread[0].c_str() ,getLabel().c_str() );
162  }
163  checkRead();
164 }
165 
167  if( !cloned_file ){ ifile->close(); delete ifile; }
168  for(unsigned i=0;i<readvals.size();++i) delete readvals[i];
169 }
170 
171 std::string Read::getFilename() const {
172  return filename;
173 }
174 
176  return ifile;
177 }
178 
180  if( !cloned_file ){
181  double du_time;
182  if( !ifile->scanField("time",du_time) ){
183  error("Reached end of file " + filename + " before end of trajectory");
184  } else if( fabs( du_time-getTime() )>plumed.getAtoms().getTimeStep() ){
185  std::string str_dutime,str_ptime; Tools::convert(du_time,str_dutime); Tools::convert(getTime(),str_ptime);
186  error("mismatched times in colvar files : colvar time=" + str_dutime + " plumed time=" + str_ptime );
187  }
188  }
189 }
190 
192  std::string smin, smax;
193  for(unsigned i=0;i<readvals.size();++i){
194  ifile->scanField( readvals[i] );
195  getPntrToComponent(i)->set( readvals[i]->get() );
196  if( readvals[i]->isPeriodic() ){
197  readvals[i]->getDomain( smin, smax );
198  getPntrToComponent(i)->setDomain( smin, smax );
199  }
200  }
201 }
202 
204  if( !cloned_file ){
205  for(unsigned i=0;i<nlinesPerStep;++i){
206  ifile->scanField(); double du_time;
207  if( plumed.getAtoms().getNatoms()==0 && !ifile->scanField("time",du_time) ) plumed.stop();
208  }
209  }
210 }
211 
212 }
213 }
bool FieldExist(const std::string &s)
Check if a field exist.
Definition: IFile.cpp:104
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
void setNotPeriodic()
Set your default value to have no periodicity.
Log & log
Reference to the log stream.
Definition: Action.h:93
static void registerKeywords(Keywords &keys)
Definition: Read.cpp:88
This is used to create PLMD::Action objects that are run with some set frequency. ...
Definition: ActionPilot.h:39
void componentIsNotPeriodic(const std::string &name)
Set your value component to have no periodicity.
std::vector< Value * > readvals
Definition: Read.cpp:73
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
static bool convert(const std::string &str, double &t)
Convert a string to a double, reading it.
Definition: Tools.cpp:74
Read(const ActionOptions &)
Definition: Read.cpp:100
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
const std::string & getLabel() const
Returns the label.
Definition: Action.h:263
void prepare()
Prepare an Action for calculation This can be used by Action if they need some special preparation be...
Definition: Read.cpp:179
FileBase & link(FILE *)
Link to an already open filed.
Definition: FileBase.cpp:64
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
void addComponent(const std::string &name)
Add a value with a name like label.name.
Provides the keyword READ
Definition: Read.cpp:64
void calculate()
Calculate an Action.
Definition: Read.cpp:191
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 parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
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
IFile & open(const std::string &name)
Opens the file.
Definition: IFile.cpp:90
IFile * ifile
Definition: Read.cpp:72
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void setPeriodic(const std::string &min, const std::string &max)
Set the value to be periodic with a particular domain.
double getTime() const
Return the present time.
Definition: Action.cpp:173
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
static void useCustomisableComponents(Keywords &keys)
The components in the action will depend on the user.
void close()
Closes the file Should be used only for explicitely opened files.
Definition: FileBase.cpp:140
Base class for all the input Actions.
Definition: Action.h:60
std::string label
Label of the Action, as set with LABEL= in the plumed.dat file.
Definition: Action.h:67
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
Definition: Action.cpp:49
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
static void registerKeywords(Keywords &keys)
Create the keywords for actionPilot.
Definition: ActionPilot.cpp:27
unsigned nlinesPerStep
Definition: Read.cpp:70
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::string getFilename() const
Definition: Read.cpp:171
void update()
Update.
Definition: Read.cpp:203
Class for input files.
Definition: IFile.h:40
void apply()
Apply an Action.
Definition: Read.cpp:79
IFile & scanField(const std::string &, double &)
Read a double field.
Definition: IFile.cpp:121
bool FileExist(const std::string &path)
Check if the file exists.
Definition: FileBase.cpp:118
Main plumed object.
Definition: Plumed.h:201
void addValue()
Add a value with the name label.
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
std::string filename
Definition: Read.cpp:71
IFile & scanFieldList(std::vector< std::string > &)
Gets the list of all fields.
Definition: IFile.cpp:95
IFile * getFile()
Definition: Read.cpp:175
void setDomain(const std::string &, const std::string &)
Set the domain of the function.
Definition: Value.cpp:91
void allowIgnoredFields()
Allow some of the fields in the input to be ignored.
Definition: IFile.cpp:199