Simpatico  v1.10
CArrayParam.h
1 #ifndef UTIL_CARRAY_PARAM_H
2 #define UTIL_CARRAY_PARAM_H
3 
4 #include <util/param/Parameter.h>
5 #include <util/global.h>
6 
7 #ifdef UTIL_MPI
8 #include <util/mpi/MpiSendRecv.h>
9 #endif
10 
11 #include <iomanip>
12 
13 namespace Util
14 {
15 
21  template <class Type>
22  class CArrayParam : public Parameter
23  {
24 
25  public:
26 
30  CArrayParam(const char *label, Type *value, int n, bool isRequired = true);
31 
37  void writeParam(std::ostream &out);
38 
39  protected:
40 
46  virtual void readValue(std::istream& in);
47 
53  virtual void loadValue(Serializable::IArchive& ar);
54 
60  virtual void saveValue(Serializable::OArchive& ar);
61 
62  #ifdef UTIL_MPI
63 
66  virtual void bcastValue();
67  #endif
68 
69  private:
70 
72  Type* value_;
73 
75  int n_;
76 
77  };
78 
79  /*
80  * CArrayParam<Type> constructor.
81  */
82  template <class Type>
83  CArrayParam<Type>::CArrayParam(const char *label, Type* value, int n, bool isRequired)
84  : Parameter(label, isRequired),
85  value_(value),
86  n_(n)
87  {}
88 
89  /*
90  * Read C-array of n values from file.
91  */
92  template <class Type>
93  void CArrayParam<Type>::readValue(std::istream &in)
94  {
95  for (int i = 0; i < n_; ++i) {
96  in >> value_[i];
97  }
98  }
99 
100  /*
101  * Load C-array of n values from an input archive
102  */
103  template <class Type>
105  { ar.unpack(value_, n_); }
106 
107  /*
108  * Save C-array of n values to an output archive
109  */
110  template <class Type>
112  { ar.pack(value_, n_); }
113 
114  #ifdef UTIL_MPI
115  /*
116  * Broadcast an array of n values
117  */
118  template <class Type>
120  { bcast<Type>(ioCommunicator(), value_, n_, 0); }
121  #endif
122 
123  /*
124  * Write a C array
125  */
126  template <class Type>
127  void CArrayParam<Type>::writeParam(std::ostream &out)
128  {
129  if (isActive()) {
130  Label space("");
131  for (int i = 0; i < n_; ++i) {
132  if (i == 0) {
133  out << indent() << label_;
134  } else {
135  out << indent() << space;
136  }
137  out << std::right << std::scientific
138  << std::setprecision(Parameter::Precision)
139  << std::setw(Parameter::Width)
140  << value_[i]
141  << std::endl;
142  }
143  }
144  }
145 
146 }
147 #endif
std::string label() const
Return label string.
Definition: Parameter.cpp:158
CArrayParam(const char *label, Type *value, int n, bool isRequired=true)
Constructor.
Definition: CArrayParam.h:83
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
A single variable in a parameter file.
Definition: Parameter.h:45
File containing preprocessor macros for error handling.
Saving / output archive for binary ostream.
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: CArrayParam.h:93
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: CArrayParam.h:119
void pack(const T &data)
Pack one object of type T.
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: CArrayParam.h:111
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 Parameter associated with a 1D C array.
Definition: CArrayParam.h:22
void writeParam(std::ostream &out)
Write parameter to stream.
Definition: CArrayParam.h:127
A label string in a file format.
Definition: Label.h:36
void unpack(T &data)
Unpack a single T object.
Saving archive for binary istream.
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: CArrayParam.h:104
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
std::string indent() const
Return indent string for this object (string of spaces).