QMCPACK
IO.h
Go to the documentation of this file.
1 //////////////////////////////////////////////////////////////////////////////////////
2 // This file is distributed under the University of Illinois/NCSA Open Source License.
3 // See LICENSE file in top directory for details.
4 //
5 // Copyright (c) 2016 Jeongnim Kim and QMCPACK developers.
6 //
7 // File developed by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
8 // Mark A. Berrill, berrillma@ornl.gov, Oak Ridge National Laboratory
9 //
10 // File created by: Paul R. C. Kent, kentpr@ornl.gov, Oak Ridge National Laboratory
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 // http://pathintegrals.info //
15 /////////////////////////////////////////////////////////////
16 
17 #ifndef IO_H
18 #define IO_H
19 
20 #include "IOBase.h"
21 #include "IOVar.h"
22 #include "IOASCII.h"
23 
24 namespace IO
25 {
26 // This output stream is for verbose output from programs. It is
27 // connected to stderr if SetVerbose(true) is called.
28 extern std::ostream verr;
29 void SetVerbose(bool verb);
30 
31 template<>
32 inline bool IOTreeClass::WriteVar(std::string name, const char* val)
33 {
34  return WriteVar(name, std::string(val));
35 }
36 template<typename T>
37 bool IOTreeClass::WriteVar(std::string name, T val)
38 {
39  if (GetFileType() == ASCII_TYPE)
40  {
41  throw std::logic_error{"this case is unimplement for rank 0"};
42 // VarList.push_back(new IOVarASCII<T, 0>(name, val));
43  }
44  else
45  {
46  std::cerr << "Unknown file type in WriteVar.\n";
47  abort();
48  }
49  return true;
50 }
51 
52 template<typename T, int LEN>
53 bool IOTreeClass::WriteVar(std::string name, const TinyVector<T, LEN>& val)
54 {
55  Array<T, 1> aVal(LEN);
56  for (int i = 0; i < LEN; i++)
57  aVal(i) = val[i];
58  WriteVar(name, aVal);
59  return true;
60 }
61 
62 
63 template<typename T, int RANK>
64 bool IOTreeClass::WriteVar(std::string name, const Array<T, RANK>& val)
65 {
66  if (GetFileType() == ASCII_TYPE)
67  {
68  VarList.push_back(new IOVarASCII<T, RANK>(name, val));
69  }
70  else
71  {
72  std::cerr << "Unknown file type in WriteVar.\n";
73  abort();
74  }
75  return true;
76 }
77 
78 template<typename T, int RANK, int LEN>
79 bool IOTreeClass::WriteVar(std::string name, const Array<TinyVector<T, LEN>, RANK>& val)
80 {
82  for (int dim = 0; dim < RANK; dim++)
83  shape[dim] = val.extent(dim);
84  shape[RANK] = LEN;
85 
86  Array<T, RANK + 1> aval((T*)&(val(0)[0]), shape, neverDeleteData);
87  return WriteVar(name, aval);
88 }
89 
90 
91 /// In the file name format name.extn, returns the extension.
92 /// Actually returns everything after the trailing.
93 inline std::string Extension(std::string fileName);
94 
95 
96 /// This function takes a filename, determines it extension, creates a
97 /// new IOTreeASCIIClass or IOTreeHDF5Class based on the
98 /// extension, and calls OpenFile on the new object.
99 /// Extensions:
100 /// .h5: HDF5
101 /// .xml: XML
102 /// .anything_else ASCII
103 IOTreeClass* ReadTree(std::string fileName, std::string myName, IOTreeClass* parent);
104 
105 IOTreeClass* NewTree(std::string fileName, std::string myName, IOTreeClass* parent);
106 
107 
108 /// Wrapper class for IOTreeClass that gives a nearly identical
109 /// interface as the OutputSectionClass.
111 {
112 private:
114 
115 public:
116  /// Opens the file reference by fileName and reads the contents into
117  /// the tree in CurrentSection. Creates a new object based on the
118  /// extnesion of the filename. For ".h5", it creates an
119  /// IOTreeHDF5Class. For ".xml" it creaes an IOTreeXMLClass.
120  /// After creating the object, it calls the objects virtual OpenFile
121  /// function, reading the contents of the file into the tree.
122  bool OpenFile(std::string fileName);
123  std::string GetName() { return CurrentSection->Name; }
124  std::string GetFileName();
125  std::string GetVarName(int num) { return GetVarPtr(num)->GetName(); }
126  /// Creates a file at the top level, choosing the appropriate type
127  /// based on the file extension.
128  bool NewFile(std::string fileName);
129 
130  /// Calls CurrentSections close file and then deletes the
131  /// CurrentSection.
132  void CloseFile();
133 
134  /// Flush all buffers to disk for safety
135  void FlushFile();
136 
137  /// Opens the num'th section with the given name. The default
138  /// value for num is 0.
139  bool OpenSection(std::string name, int num = 0);
140 
141  /// Opens the num'th section below CurrentSection.
142  bool OpenSection(int num);
143 
144  /// This mounts a file in the current tree under CurrentSection at
145  /// the end of CurrentsSection's SectionList. It does not change
146  /// what CurrentSection points to, ie. it does not descend to the
147  /// newly-opened section.
148  bool IncludeSection(std::string name, std::string fileName);
149 
150  /// Creates a new section of the same type as currentSection under
151  /// currentSection. Pushes the new section to the end of the
152  /// section list.
153  inline void NewSection(std::string name) { CurrentSection = CurrentSection->NewSection(name); }
154 
155  /// This function creates a new file of the appropriate type as
156  /// determined by the extension of fileName and mounts it at the end
157  /// of the list under CurrentSection. Returns false if the file
158  /// couldn't be created.
159  bool NewSection(std::string name, std::string fileName);
160 
161  /// Closes the current section. That is, CurrentSection becomes
162  /// CurrentSection's parent.
163  void CloseSection();
164 
165  /// Template function which reads a variable in the present section
166  /// into the passed-by-reference T variable.
167  template<class T>
168  bool ReadVar(std::string name, T& var)
169  {
170  return (CurrentSection->ReadVar(name, var));
171  }
172 
173  template<class T>
174  bool ReadVar(std::string name, T& var, T Default)
175  {
176  bool success = ReadVar(name, var);
177  if (!success)
178  var = Default;
179  return (success);
180  }
181 
182  /// Writes a variable under the current section.
183  template<typename T>
184  bool WriteVar(std::string name, T val)
185  {
186  return CurrentSection->WriteVar(name, val);
187  }
188 
189  template<typename T, int RANK>
190  bool WriteVar(std::string name, const Array<T, RANK>& val)
191  {
192  return CurrentSection->WriteVar(name, val);
193  }
194 
195  template<class T>
196  bool AppendVar(std::string name, T val)
197  {
198  return CurrentSection->AppendVar(name, val);
199  }
200 
201  template<typename T, int RANK>
202  bool AppendVar(std::string name, const Array<T, RANK>& val)
203  {
204  return CurrentSection->AppendVar(name, val);
205  }
206 
207  inline IOVarBase* GetVarPtr(std::string name) { return (CurrentSection->GetVarPtr(name)); }
208 
209  inline IOVarBase* GetVarPtr(int num) { return (CurrentSection->GetVarPtr(num)); }
210 
211  inline void SetUnderscores(bool use) { CurrentSection->SetUnderscores(use); }
212 
213  /// Returns the number of subsections within the present section
214  /// which have the name name. If called without a name, it returns
215  /// the total number of sections.
216  inline int CountSections(std::string name = "") { return (CurrentSection->CountSections(name)); }
217  inline int CountVars() { return (CurrentSection->CountVars()); }
218  /// Calls CurrentSections virtual PrintTree() function. This is for
219  /// debugging purposes. It spits out a hierarchy of the sections
220  /// and variable names.
222 
224 
226 };
227 
228 
229 } // namespace IO
230 
231 #endif
std::string Name
Definition: IOBase.h:56
IOSectionClass(IOSectionClass &io)
Definition: IO.h:223
bool WriteVar(std::string name, T val)
These create a new variable with the given name and value:
Definition: IO.h:37
IOTreeClass * NewTree(std::string fileName, std::string myName, IOTreeClass *parent)
void PrintTree()
Calls CurrentSections virtual PrintTree() function.
Definition: IO.h:221
bool OpenSection(std::string name, int num=0)
Opens the num&#39;th section with the given name.
int CountSections(std::string name)
Returns the number of subsections with the given name within the present section. ...
Definition: IOBase.h:177
bool AppendVar(std::string name, const Array< T, RANK > &val)
Definition: IO.h:202
bool NewFile(std::string fileName)
Creates a file at the top level, choosing the appropriate type based on the file extension.
std::string Extension(std::string fileName)
In the file name format name.extn, returns the extension.
void SetUnderscores(bool use)
Definition: IO.h:211
IOVarBase * GetVarPtr(std::string name)
Definition: IOBase.h:81
std::ostream verr
bool ReadVar(std::string name, T &var, T Default)
Definition: IO.h:174
int CountVars()
Definition: IO.h:217
bool WriteVar(std::string name, T val)
Writes a variable under the current section.
Definition: IO.h:184
This class stores a tree of input file sections.
Definition: IOBase.h:33
void CloseSection()
Closes the current section.
void CloseFile()
Calls CurrentSections close file and then deletes the CurrentSection.
void NewSection(std::string name)
Creates a new section of the same type as currentSection under currentSection.
Definition: IO.h:153
Definition: IO.h:24
IOVarBase * GetVarPtr(int num)
Definition: IO.h:209
std::list< IOVarBase * > VarList
Definition: IOBase.h:41
std::string GetName()
Definition: IO.h:123
void SetUnderscores(bool use)
Definition: IOBase.h:143
IOVarBase * GetVarPtr(std::string name)
Definition: IO.h:207
virtual IOTreeClass * NewSection(std::string name)=0
Write me!
constexpr class neverDeleteData_t neverDeleteData
void FlushFile()
Flush all buffers to disk for safety.
bool ReadVar(std::string name, T &var)
Definition: IOBase.h:63
virtual IOFileType GetFileType()=0
IOTreeClass * CurrentSection
Definition: IO.h:113
bool AppendVar(std::string name, T val)
Definition: IO.h:196
std::string GetVarName(int num)
Definition: IO.h:125
int CountVars()
Definition: IOBase.h:193
Wrapper class for IOTreeClass that gives a nearly identical interface as the OutputSectionClass.
Definition: IO.h:110
bool WriteVar(std::string name, const Array< T, RANK > &val)
Definition: IO.h:190
bool IncludeSection(std::string name, std::string fileName)
This mounts a file in the current tree under CurrentSection at the end of CurrentsSection&#39;s SectionLi...
std::string GetName() const
Definition: IOVarBase.h:75
std::string GetFileName()
IOSectionClass()
Definition: IO.h:225
bool OpenFile(std::string fileName)
Opens the file reference by fileName and reads the contents into the tree in CurrentSection.
IOTreeClass * ReadTree(std::string fileName, std::string myName, IOTreeClass *parent)
This function takes a filename, determines it extension, creates a new IOTreeASCIIClass or IOTreeHDF5...
int CountSections(std::string name="")
Returns the number of subsections within the present section which have the name name.
Definition: IO.h:216
void SetVerbose(bool verb)
virtual void PrintTree()=0
bool ReadVar(std::string name, T &var)
Template function which reads a variable in the present section into the passed-by-reference T variab...
Definition: IO.h:168
bool AppendVar(std::string name, T val)
Append a value to a variable of dimension of 1 higher than val.
Definition: IOBase.h:162