Simpatico  v1.10
DArrayParam.h
1 #ifndef UTIL_D_ARRAY_PARAM_H
2 #define UTIL_D_ARRAY_PARAM_H
3 
4 /*
5 * Util Package - C++ Utilities for Scientific Computation
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include <util/param/Parameter.h> // base class
12 #include <util/containers/DArray.h> // member
13 #include <util/global.h>
14 
15 #include <iomanip>
16 
17 namespace Util
18 {
19 
25  template <class Type>
26  class DArrayParam : public Parameter
27  {
28 
29  public:
30 
31  /*
32  * Constructor.
33  */
34  DArrayParam(const char *label, DArray<Type>& array, int n, bool isRequired = true);
35 
41  void writeParam(std::ostream &out);
42 
43  protected:
44 
50  virtual void readValue(std::istream& in);
51 
57  virtual void loadValue(Serializable::IArchive& ar);
58 
64  virtual void saveValue(Serializable::OArchive& ar);
65 
66  #ifdef UTIL_MPI
67 
70  virtual void bcastValue();
71  #endif
72 
73  private:
74 
76  DArray<Type>* arrayPtr_;
77 
79  int n_;
80 
81  };
82 
83  /*
84  * DArrayParam<Type> constructor.
85  */
86  template <class Type>
87  DArrayParam<Type>::DArrayParam(const char *label, DArray<Type>& array, int n, bool isRequired)
88  : Parameter(label, isRequired),
89  arrayPtr_(&array),
90  n_(n)
91  {}
92 
93  /*
94  * Read array of values from isteam.
95  */
96  template <class Type>
97  void DArrayParam<Type>::readValue(std::istream &in)
98  {
99  if (!(arrayPtr_->isAllocated())) {
100  UTIL_THROW("Cannot read unallocated DArray");
101  }
102  if (arrayPtr_->capacity() != n_) {
103  UTIL_THROW("Error: DArray capacity < n");
104  }
105  for (int i = 0; i < n_; ++i) {
106  in >> (*arrayPtr_)[i];
107  }
108  }
109 
110  /*
111  * Load a DArray from input archive.
112  */
113  template <class Type>
115  {
116  if (!(arrayPtr_->isAllocated())) {
117  arrayPtr_->allocate(n_);
118  }
119  ar >> *arrayPtr_;
120  if (arrayPtr_->capacity() < n_) {
121  UTIL_THROW("Error: DArray capacity < n");
122  }
123  }
124 
125  /*
126  * Save a DArray to an output archive.
127  */
128  template <class Type>
130  {
131  if (!(arrayPtr_->isAllocated())) {
132  UTIL_THROW("Cannot save unallocated DArray");
133  }
134  if (arrayPtr_->capacity() != n_) {
135  UTIL_THROW("Error: DArray capacity < n");
136  }
137  ar << *arrayPtr_;
138  }
139 
140  #ifdef UTIL_MPI
141  /*
142  * Broadcast a DArray.
143  */
144  template <class Type>
146  { bcast<Type>(ioCommunicator(), *arrayPtr_, n_, 0); }
147  #endif
148 
149  /*
150  * Write a DArray parameter.
151  */
152  template <class Type>
153  void DArrayParam<Type>::writeParam(std::ostream &out)
154  {
155  if (isActive()) {
156 
157  if (!(arrayPtr_->isAllocated())) {
158  UTIL_THROW("Cannot write unallocated DArray");
159  }
160  if (arrayPtr_->capacity() != n_) {
161  UTIL_THROW("Error: DArray capacity != n in writeParam");
162  }
163 
164  Label space("");
165  int i;
166  for (i = 0; i < n_; ++i) {
167  if (i == 0) {
168  out << indent() << label_;
169  } else {
170  out << indent() << space;
171  }
172  out << std::right << std::scientific
173  << std::setprecision(Parameter::Precision)
174  << std::setw(Parameter::Width)
175  << (*arrayPtr_)[i]
176  << std::endl;
177  }
178 
179  } // if isActive
180  }
181 
182 }
183 #endif
std::string label() const
Return label string.
Definition: Parameter.cpp:158
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: DArrayParam.h:145
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
A single variable in a parameter file.
Definition: Parameter.h:45
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: DArrayParam.h:129
File containing preprocessor macros for error handling.
Parameter(const char *label, bool isRequired=true)
Constructor.
Definition: Parameter.cpp:22
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
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: DArrayParam.h:97
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Utility classes for scientific computation.
Definition: accumulators.mod:1
void writeParam(std::ostream &out)
Write parameter to stream.
Definition: DArrayParam.h:153
bool isAllocated() const
Return true if the DArray has been allocated, false otherwise.
Definition: DArray.h:222
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:170
A label string in a file format.
Definition: Label.h:36
Saving archive for binary istream.
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
A Parameter associated with a DArray container.
Definition: DArrayParam.h:26
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
int capacity() const
Return allocated size.
Definition: Array.h:153
void allocate(int capacity)
Allocate the underlying C array.
Definition: DArray.h:191
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: DArrayParam.h:114
std::string indent() const
Return indent string for this object (string of spaces).