All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Print.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/ActionWithArguments.h"
24 #include "core/ActionRegister.h"
25 
26 using namespace std;
27 
28 namespace PLMD{
29 namespace generic{
30 
31 //+PLUMEDOC ANALYSIS PRINT
32 /*
33 Print quantities to a file.
34 
35 This directive can be used multiple times
36 in the input so you can print files with different strides or print different quantities
37 to different files. You can control the buffering of output using the \subpage FLUSH keyword.
38 
39 \par Examples
40 The following input instructs plumed to print the distance between atoms 3 and 5 on a file
41 called COLVAR every 10 steps, and the distance and total energy on a file called COLVAR_ALL
42 every 1000 steps.
43 \verbatim
44 DISTANCE ATOMS=2,5 LABEL=distance
45 ENERGY LABEL=energy
46 PRINT ARG=distance STRIDE=10 FILE=COLVAR
47 PRINT ARG=distance,energy STRIDE=1000 FILE=COLVAR_ALL
48 \endverbatim
49 (See also \ref DISTANCE and \ref ENERGY).
50 
51 */
52 //+ENDPLUMEDOC
53 
54 class Print :
55 public ActionPilot,
57 {
58  string file;
60  string fmt;
61 // small internal utility
62 /////////////////////////////////////////
63 // these are crazy things just for debug:
64 // they allow to change regularly the
65 // printed argument
66  int rotate;
69  vector<Value*> rotateArguments;
70 /////////////////////////////////////////
71 public:
72  void calculate(){}
73  void prepare();
74  Print(const ActionOptions&);
75  static void registerKeywords(Keywords& keys);
76  void apply(){}
77  void update();
78  ~Print();
79 };
80 
81 PLUMED_REGISTER_ACTION(Print,"PRINT")
82 
83 void Print::registerKeywords(Keywords& keys){
84  Action::registerKeywords(keys);
85  ActionPilot::registerKeywords(keys);
86  ActionWithArguments::registerKeywords(keys);
87  keys.use("ARG");
88  keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be output");
89  keys.add("optional","FILE","the name of the file on which to output these quantities");
90  keys.add("optional","FMT","the format that should be used to output real numbers");
91  keys.add("hidden","_ROTATE","some funky thing implemented by GBussi");
92 }
93 
94 Print::Print(const ActionOptions&ao):
95 Action(ao),
96 ActionPilot(ao),
98 fmt("%f"),
99 rotate(0)
100 {
101  ofile.link(*this);
102  parse("FILE",file);
103  if(file.length()>0){
104  ofile.open(file);
105  log.printf(" on file %s\n",file.c_str());
106  } else {
107  log.printf(" on plumed log file\n");
108  ofile.link(log);
109  }
110  parse("FMT",fmt);
111  fmt=" "+fmt;
112  log.printf(" with format %s\n",fmt.c_str());
113  for(unsigned i=0;i<getNumberOfArguments();++i) ofile.setupPrintValue( getPntrToArgument(i) );
114 /////////////////////////////////////////
115 // these are crazy things just for debug:
116 // they allow to change regularly the
117 // printed argument
118  parse("_ROTATE",rotate);
119  if(rotate>0){
121  for(unsigned i=0;i<getNumberOfArguments();++i) rotateArguments.push_back( getPntrToArgument(i) );
122  vector<Value*> a(1,rotateArguments[0]);
123  requestArguments(vector<Value*>(1,rotateArguments[0]));
124  rotateLast=0;
125  }
126 /////////////////////////////////////////
127  checkRead();
128 }
129 
131 /////////////////////////////////////////
132 // these are crazy things just for debug:
133 // they allow to change regularly the
134 // printed argument
135  if(rotate>0){
136  rotateCountdown--;
137  if(rotateCountdown==0){
139  rotateLast++;
140  rotateLast%=rotateArguments.size();
141  requestArguments(vector<Value*>(1,rotateArguments[rotateLast]));
142  }
143  }
144 /////////////////////////////////////////
145 }
146 
148  ofile.fmtField(" %f");
149  ofile.printField("time",getTime());
150  for(unsigned i=0;i<getNumberOfArguments();i++){
151  ofile.fmtField(fmt);
153  //ofile.printField(getPntrToArgument(i)->getName(),getArgument(i));
154  }
155  ofile.printField();
156 }
157 
159 }
160 
161 }
162 
163 
164 }
Log & log
Reference to the log stream.
Definition: Action.h:93
This is used to create PLMD::Action objects that are run with some set frequency. ...
Definition: ActionPilot.h:39
void requestArguments(const std::vector< Value * > &arg)
Setup the dependencies.
OFile & fmtField(const std::string &)
Set the format for writing double precision fields.
Definition: OFile.cpp:135
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
Value * getPntrToArgument(const unsigned n)
Return a pointer to specific argument.
This is used to create PLMD::Action objects that take the output from some other Action as input...
void calculate()
Calculate an Action.
Definition: Print.cpp:72
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
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
OFile & link(OFile &)
Allows linking this OFile to another one.
Definition: OFile.cpp:71
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
Base class for all the input Actions.
Definition: Action.h:60
double getArgument(const unsigned n) const
Returns the value of an argument.
void apply()
Apply an Action.
Definition: Print.cpp:76
Provides the keyword PRINT
Definition: Print.cpp:54
Class for output files.
Definition: OFile.h:84
OFile & printField(const std::string &, double)
Set the value of a double precision field.
Definition: OFile.cpp:145
void update()
Update.
Definition: Print.cpp:147
void const char const char int double * a
Definition: Matrix.h:42
unsigned getNumberOfArguments() const
Returns the number of arguments.
OFile & open(const std::string &name)
Opens the file using automatic append/backup.
Definition: OFile.cpp:264
OFile & setupPrintValue(Value *val)
Used to setup printing of values.
Definition: OFile.cpp:172
void prepare()
Prepare an Action for calculation This can be used by Action if they need some special preparation be...
Definition: Print.cpp:130
vector< Value * > rotateArguments
Definition: Print.cpp:69