Simpatico  v1.10
FileMaster.cpp
1 /*
2 * Util Package - C++ Utilities for Scientific Computation
3 *
4 * Copyright 2010 - 2017, The Regents of the University of Minnesota
5 * Distributed under the terms of the GNU General Public License.
6 */
7 
8 #include <util/global.h>
9 
10 #include "FileMaster.h"
11 #ifdef UTIL_MPI
12 #include <util/mpi/MpiLoader.h>
13 #endif
14 
15 #include <sstream>
16 
17 namespace Util
18 {
19 
20  /*
21  * Default constructor.
22  */
24  : paramFileName_(),
25  commandFileName_(),
26  inputPrefix_(),
27  outputPrefix_(),
28  directoryIdPrefix_(),
29  rootPrefix_(),
30  paramFilePtr_(0),
31  commandFilePtr_(0),
32  hasDirectoryId_(false),
33  isCommonControl_(false)
34  { setClassName("FileMaster"); }
35 
36  /*
37  * Copy constructor.
38  */
40  : paramFileName_(),
41  commandFileName_(),
42  inputPrefix_(other.inputPrefix_),
43  outputPrefix_(other.outputPrefix_),
44  directoryIdPrefix_(other.directoryIdPrefix_),
45  rootPrefix_(other.rootPrefix_),
46  paramFilePtr_(0),
47  commandFilePtr_(0),
48  hasDirectoryId_(other.hasDirectoryId_),
49  isCommonControl_(other.isCommonControl_)
50  {}
51 
52  /*
53  * Destructor.
54  */
56  {
57  if (paramFilePtr_) {
58  paramFilePtr_->close();
59  delete paramFilePtr_;
60  }
61  if (commandFilePtr_) {
62  commandFilePtr_->close();
63  delete commandFilePtr_;
64  }
65  }
66 
67  /*
68  * Set root prefix for all path names.
69  */
70  void FileMaster::setRootPrefix(const std::string& rootPrefix)
71  { rootPrefix_ = rootPrefix; }
72 
73  /*
74  * Set a directory Id prefix.
75  */
77  {
78  std::stringstream sMyId;
79  sMyId << rank;
80  directoryIdPrefix_ = sMyId.str();
81  directoryIdPrefix_ += "/";
82  hasDirectoryId_ = true;
83  }
84 
85  /*
86  * Set to use a common parameter file even if a directory id is set.
87  */
89  { isCommonControl_ = true; }
90 
91  /*
92  * Set the input prefix.
93  */
94  void FileMaster::setInputPrefix(const std::string& inputPrefix)
95  { inputPrefix_ = inputPrefix; }
96 
97  /*
98  * Set the output prefix.
99  */
100  void FileMaster::setOutputPrefix(const std::string& outputPrefix)
101  { outputPrefix_ = outputPrefix; }
102 
103  /*
104  * Set the parameter file name.
105  */
107  { paramFileName_ = paramFileName; }
108 
109  /*
110  * Set the command file name.
111  */
113  { commandFileName_ = commandFileName; }
114 
115  /*
116  * Read parameters from file.
117  */
118  void FileMaster::readParameters(std::istream &in)
119  {
120  if (commandFileName_.empty()) {
121  readOptional<std::string>(in, "commandFileName", commandFileName_);
122  }
123  readOptional<std::string>(in, "inputPrefix", inputPrefix_);
124  readOptional<std::string>(in, "outputPrefix", outputPrefix_);
125  }
126 
127  /*
128  * Get the default parameter stream.
129  */
131  {
132  loadParameter<std::string>(ar, "inputPrefix", inputPrefix_);
133  loadParameter<std::string>(ar, "outputPrefix", outputPrefix_);
134  #ifdef UTIL_MPI
135  MpiLoader<Serializable::IArchive> loader(*this, ar);
136  loader.load(isCommonControl_);
137  #else
138  ar >> isCommonControl_;
139  #endif
140  }
141 
142  /*
143  * Save internal state to file.
144  */
146  {
147  ar << inputPrefix_;
148  ar << outputPrefix_;
149  ar << isCommonControl_;
150  }
151 
152  /*
153  * Get the default parameter stream.
154  */
155  std::istream& FileMaster::paramFile()
156  {
157  if (paramFilePtr_) {
158  return *paramFilePtr_;
159  } else {
160  if (paramFileName_.empty()) {
161  if (!hasDirectoryId_ || isCommonControl_) {
162  return std::cin;
163  } else {
164  paramFileName_ = "param";
165  }
166  }
167  paramFilePtr_ = new std::ifstream();
168  // Log::file() << "Opening parameter file "
169  // << paramFileName_ << std::endl;
170  openControlFile(paramFileName_, *paramFilePtr_);
171  return *paramFilePtr_;
172  }
173  }
174 
175  /*
176  * Get the command input stream by reference.
177  */
178  std::istream& FileMaster::commandFile()
179  {
180  if (commandFilePtr_) {
181  return *commandFilePtr_;
182  } else {
183  if (commandFileName_.empty()) {
184  commandFileName_ = "commands";
185  }
186  commandFilePtr_ = new std::ifstream();
187  //Log::file() << "Opening command file "
188  // << paramFileName_ << std::endl;
189  openControlFile(commandFileName_, *commandFilePtr_);
190  return *commandFilePtr_;
191  }
192  }
193 
194  /*
195  * Open an ifstream with fully specified path and mode.
196  */
197  void
198  FileMaster::open(const std::string& name, std::ifstream& in,
199  std::ios_base::openmode mode) const
200  {
201  in.open(name.c_str(), mode);
202  if (in.fail()) {
203  std::string message = "Error opening input file. Filename: ";
204  message += name;
205  UTIL_THROW(message.c_str());
206  }
207  }
208 
209  /*
210  * Open an ofstream with fully specified path and mode.
211  */
212  void
213  FileMaster::open(const std::string& name, std::ofstream& out,
214  std::ios_base::openmode mode) const
215  {
216  out.open(name.c_str(), mode);
217  if (out.fail()) {
218  std::string message = "Error opening output file. Filename: ";
219  message += name;
220  UTIL_THROW(message.c_str());
221  }
222  }
223 
224  /*
225  * Open an input parameter or command control file.
226  */
227  void FileMaster::openControlFile(const std::string& name,
228  std::ifstream& in) const
229  {
230  std::string filename(rootPrefix_);
231  if (hasDirectoryId_ && !isCommonControl_) {
232  filename += directoryIdPrefix_;
233  }
234  filename += name;
235  open(filename, in);
236  }
237 
238  /*
239  * Open an input restart file for reading.
240  */
241  void
242  FileMaster::openRestartIFile(const std::string& name, std::ifstream& in,
243  std::ios_base::openmode mode) const
244  {
245  std::string filename(rootPrefix_);
246  if (hasDirectoryId_) {
247  filename += directoryIdPrefix_;
248  }
249  filename += name;
250  open(filename, in, mode);
251  }
252 
253  /*
254  * Open an output restart file for writing.
255  */
256  void
257  FileMaster::openRestartOFile(const std::string& name, std::ofstream& out,
258  std::ios_base::openmode mode) const
259  {
260  // Construct filename = outputPrefix_ + name
261  std::string filename(rootPrefix_);
262  if (hasDirectoryId_) {
263  filename = directoryIdPrefix_;
264  }
265  filename += name;
266  open(filename, out, mode);
267  }
268 
269  /*
270  * Open an input dat file with specified mode.
271  */
272  void
273  FileMaster::openInputFile(const std::string& name, std::ifstream& in,
274  std::ios_base::openmode mode) const
275  {
276  // Construct filename = inputPrefix_ + name
277  std::string filename(rootPrefix_);
278  if (hasDirectoryId_) {
279  filename += directoryIdPrefix_;
280  }
281  filename += inputPrefix_;
282  filename += name;
283  open(filename, in, mode);
284  }
285 
286  /*
287  * Open an output data file in specified mode.
288  */
289  void
290  FileMaster::openOutputFile(const std::string& name,
291  std::ofstream& out,
292  std::ios_base::openmode mode) const
293  {
294  // Construct path
295  std::string filename(rootPrefix_);
296  if (hasDirectoryId_) {
297  filename += directoryIdPrefix_;
298  }
299  filename += outputPrefix_;
300  filename += name;
301 
302  open(filename, out, mode);
303  }
304 
305  /*
306  * Will paramFile() return std::cin ?
307  */
309  { return ((!hasDirectoryId_) || isCommonControl_); }
310 
311 }
std::istream & commandFile()
Get the command input stream by reference.
Definition: FileMaster.cpp:178
void setDirectoryId(int directoryId)
Set an integer directory identifier for this processor.
Definition: FileMaster.cpp:76
void setCommandFileName(const std::string &commandFileName)
Set the command file name.
Definition: FileMaster.cpp:112
std::string paramFileName() const
Return the param file name, if any.
Definition: FileMaster.h:466
void openRestartIFile(const std::string &name, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input restart dump file for reading.
Definition: FileMaster.cpp:242
void openOutputFile(const std::string &filename, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output file.
Definition: FileMaster.cpp:290
virtual void loadParameters(Serializable::IArchive &ar)
Load internal state from file.
Definition: FileMaster.cpp:130
void openInputFile(const std::string &filename, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input file.
Definition: FileMaster.cpp:273
File containing preprocessor macros for error handling.
bool isCommonControl() const
Is set for common param and command files?
Definition: FileMaster.cpp:308
std::istream & paramFile()
Get a default parameter stream by reference.
Definition: FileMaster.cpp:155
Saving / output archive for binary ostream.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
void openControlFile(const std::string &name, std::ifstream &in) const
Open an input parameter or command file.
Definition: FileMaster.cpp:227
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void readParameters(std::istream &in)
Read parameter file.
Definition: FileMaster.cpp:118
virtual ~FileMaster()
Destructor.
Definition: FileMaster.cpp:55
std::string commandFileName() const
Return the command file name.
Definition: FileMaster.h:472
virtual void save(Serializable::OArchive &ar)
Save internal state to file.
Definition: FileMaster.cpp:145
void setRootPrefix(const std::string &rootPrefix)
Set the path from current directory to root directory.
Definition: FileMaster.cpp:70
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:142
void setCommonControl()
Enable "replicated" mode in multi-system simulations.
Definition: FileMaster.cpp:88
void setOutputPrefix(const std::string &outputPrefix)
Set the output file prefix string.
Definition: FileMaster.cpp:100
void open(const std::string &name, std::ifstream &in, std::ios_base::openmode mode=std::ios_base::in) const
Open an input file with a known path and open mode.
Definition: FileMaster.cpp:198
Saving archive for binary istream.
void setInputPrefix(const std::string &inputPrefix)
Set the input file prefix string.
Definition: FileMaster.cpp:94
Provides methods for MPI-aware loading of data from input archive.
Definition: MpiLoader.h:43
void setClassName(const char *className)
Set class name string.
void setParamFileName(const std::string &paramFileName)
Set the parameter file name.
Definition: FileMaster.cpp:106
void openRestartOFile(const std::string &name, std::ofstream &out, std::ios_base::openmode mode=std::ios_base::out) const
Open an output restart file for writing.
Definition: FileMaster.cpp:257
FileMaster()
Constructor.
Definition: FileMaster.cpp:23