QMCPACK
RecordProperty.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: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
8 // Jeremy McMinnis, jmcminis@gmail.com, University of Illinois at Urbana-Champaign
9 //
10 // File created by: Jeongnim Kim, jeongnim.kim@gmail.com, University of Illinois at Urbana-Champaign
11 //////////////////////////////////////////////////////////////////////////////////////
12 
13 
14 #ifndef OHMMS_RECORDPROPERTYBASE_H__
15 #define OHMMS_RECORDPROPERTYBASE_H__
16 
18 #include <algorithm>
19 #include <optional>
20 #include <vector>
21 #include <string>
22 #include <fstream>
23 
24 /** abstract base class to record any properties
25  */
27 {
28  int stride;
29  std::string FileName;
30  RecordProperty() : stride(-1), FileName("default") {}
31 
32  virtual ~RecordProperty() {}
33  virtual void reset(const char* fileroot, bool append = false) = 0;
34  virtual void report(int i) = 0;
35  virtual void finalize() = 0;
36  virtual bool put(xmlNodePtr cur) = 0;
37 };
38 
39 
41 {
42 public:
43  using RecordList_t = std::vector<RecordProperty*>;
44 
46  virtual ~RecordPropertyList() { clear(); }
47 
48  inline void addRecord(RecordProperty* a) { Properties.push_back(a); }
49 
50  inline void setstride(int n)
51  {
52  RecordList_t::iterator it = Properties.begin();
53  while (it != Properties.end())
54  {
55  (*it)->stride = n;
56  it++;
57  }
58  }
59 
60  inline void report(int iter)
61  {
62  RecordList_t::iterator it = Properties.begin();
63  while (it != Properties.end())
64  {
65  (*it)->report(iter);
66  it++;
67  }
68  }
69 
70  inline void finalize()
71  {
72  RecordList_t::iterator it = Properties.begin();
73  while (it != Properties.end())
74  {
75  (*it)->finalize();
76  it++;
77  }
78  }
79  inline void clear()
80  {
81  std::vector<RecordProperty*>::iterator it = Properties.begin();
82  while (it != Properties.end())
83  {
84  delete (*it);
85  it++;
86  }
87  Properties.clear();
88  }
89 
90 protected:
91  std::vector<RecordProperty*> Properties;
92 };
93 
94 
95 /** Vectorized record engine for scalar properties
96  *
97  * A series of values with the name is recorded as a table of multiple columns.
98  */
99 template<class T>
101 {
102  std::optional<std::ofstream> OutStream;
103  std::vector<T> Values;
104  std::vector<std::string> Names;
105 
107  {
108  Values.reserve(20);
109  Names.reserve(20);
110  }
111 
112  explicit RecordNamedProperty(int n)
113  {
114  Values.resize(n, T());
115  Names.resize(n);
116  }
117 
119 
120  void clear()
121  {
122  Names.clear();
123  Values.clear();
124  }
125 
126  inline T operator[](int i) const { return Values[i]; }
127  inline T& operator[](int i) { return Values[i]; }
128 
129  ///iterators to use std algorithms
130  inline typename std::vector<T>::iterator begin() { return Values.begin(); }
131  inline typename std::vector<T>::iterator end() { return Values.end(); }
132  inline typename std::vector<T>::const_iterator begin() const { return Values.begin(); }
133  inline typename std::vector<T>::const_iterator end() const { return Values.end(); }
134 
135  //inline int add(const char* aname) {
136  inline int add(const std::string& aname)
137  {
138  int i = 0;
139  while (i < Names.size())
140  {
141  if (Names[i] == aname)
142  return i;
143  i++;
144  }
145  Names.push_back(aname);
146  Values.push_back(T());
147  return Names.size() - 1;
148  }
149 
150  /** add multiple items with the aroot
151  * @param aroot root
152  * @param first first index
153  * @param last last index
154  * @return the last valid index
155  */
156  inline int append(const std::string& aroot, int first, int last)
157  {
158  for (int i = first; i < last; ++i)
159  {
160  std::ostringstream o;
161  o << aroot << i;
162  Names.push_back(o.str());
163  Values.push_back(T());
164  }
165  return Values.size();
166  }
167 
168  inline int size() const { return Names.size(); }
169 
170  inline void setValues(T v)
171  {
172  for (int i = 0; i < Values.size(); i++)
173  Values[i] = v;
174  }
175 
176  inline void resize(int n)
177  {
178  std::vector<T> a = Values;
179  std::vector<std::string> b = Names;
180  Values.resize(n, T());
181  for (int i = 0; i < a.size(); i++)
182  Values[i] = a[i];
183  //std::copy_n(a.begin(), a.size(), Values.begin());
184  Names.resize(n);
185  for (int i = 0; i < a.size(); i++)
186  Names[i] = b[i];
187  //std::copy_n(b.begin(), b.size(), Name.begin());
188  }
189 
190  ///implement virtual functions
191  inline void reset(const char* fileroot, bool append = false) override
192  {
193  if (append)
194  {
195  OutStream = std::ofstream(fileroot, std::ios_base::app);
196  }
197  else
198  {
199  OutStream = std::ofstream(fileroot);
200  }
201  if (!append)
202  {
203  OutStream->setf(std::ios::left, std::ios::adjustfield);
204  *OutStream << "# ";
205  for (int i = 0; i < Names.size(); i++)
206  (*OutStream) << std::setw(15) << Names[i].c_str();
207  (*OutStream) << std::endl;
208  }
209  OutStream->setf(std::ios::scientific, std::ios::floatfield);
210  OutStream->setf(std::ios::right, std::ios::adjustfield);
211  }
212 
213  inline void report(int iter) override
214  {
215  if (stride && iter % stride == 0)
216  {
217  for (int i = 0; i < Values.size(); i++)
218  (*OutStream) << std::setw(15) << Values[i];
219  (*OutStream) << std::endl;
220  }
221  }
222 
223  void finalize() override {}
224  bool put(xmlNodePtr cur) override;
225 };
226 
227 template<class T>
228 bool RecordNamedProperty<T>::put(xmlNodePtr cur)
229 {
230  xmlAttrPtr att = cur->properties;
231  while (att != NULL)
232  {
233  std::string aname((const char*)(att->name));
234  if (aname == "stride")
235  {
236  stride = atoi((const char*)(att->children->content));
237  }
238  att = att->next;
239  }
240  return true;
241 }
242 #endif
std::vector< RecordProperty * > RecordList_t
virtual void finalize()=0
std::vector< T >::const_iterator end() const
void reset(const char *fileroot, bool append=false) override
implement virtual functions
bool put(xmlNodePtr cur) override
int append(const std::string &aroot, int first, int last)
add multiple items with the aroot
virtual void report(int i)=0
abstract base class to record any properties
std::string FileName
std::optional< std::ofstream > OutStream
std::vector< T >::iterator begin()
iterators to use std algorithms
std::vector< T >::iterator end()
Vectorized record engine for scalar properties.
Declaration of OhmmsElementBase and define xml-related macros.
RecordNamedProperty(const RecordNamedProperty< T > &a)
std::vector< std::string > Names
void setstride(int n)
virtual void reset(const char *fileroot, bool append=false)=0
int add(const std::string &aname)
std::vector< RecordProperty * > Properties
std::vector< T >::const_iterator begin() const
virtual bool put(xmlNodePtr cur)=0
void addRecord(RecordProperty *a)
void report(int iter) override
void finalize() override
T operator[](int i) const
void report(int iter)
virtual ~RecordPropertyList()
virtual ~RecordProperty()
std::vector< T > Values