Simpatico  v1.10
FArrayParam.h
1 #ifndef UTIL_F_ARRAY_PARAM_H
2 #define UTIL_F_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>
12 #include <util/containers/FArray.h>
13 #include <util/global.h>
14 
15 #include <iomanip>
16 
17 namespace Util
18 {
19 
25  template <class Type, int N>
26  class FArrayParam : public Parameter
27  {
28 
29  public:
30 
38  FArrayParam(const char *label, FArray<Type, N>& array, bool isRequired = true);
39 
45  void writeParam(std::ostream &out);
46 
47  protected:
48 
54  virtual void readValue(std::istream& in);
55 
61  virtual void loadValue(Serializable::IArchive& ar);
62 
68  virtual void saveValue(Serializable::OArchive& ar);
69 
70  #ifdef UTIL_MPI
71 
74  virtual void bcastValue();
75  #endif
76 
77  private:
78 
80  FArray<Type, N>* arrayPtr_;
81 
82  };
83 
84  /*
85  * FArrayParam<Type, N> constructor.
86  */
87  template <class Type, int N>
89  : Parameter(label, isRequired),
90  arrayPtr_(&array)
91  {}
92 
93  /*
94  * Read a FArray from isteam.
95  */
96  template <class Type, int N>
97  void FArrayParam<Type, N>::readValue(std::istream &in)
98  {
99  for (int i = 0; i < N; ++i) {
100  in >> (*arrayPtr_)[i];
101  }
102  }
103 
104  /*
105  * Load a FArray from input archive.
106  */
107  template <class Type, int N>
109  { ar >> *arrayPtr_; }
110 
111  /*
112  * Save a FArray to an output archive.
113  */
114  template <class Type, int N>
116  { ar << *arrayPtr_; }
117 
118  #ifdef UTIL_MPI
119  /*
120  * Broadcast a FArray.
121  */
122  template <class Type, int N>
124  { bcast<Type>(ioCommunicator(), &((*arrayPtr_)[0]), N, 0); }
125  #endif
126 
127  /*
128  * Write a FArray parameter.
129  */
130  template <class Type, int N>
131  void FArrayParam<Type, N>::writeParam(std::ostream &out)
132  {
133  if (isActive()) {
134  Label space("");
135  for (int i = 0; i < N; ++i) {
136  if (i == 0) {
137  out << indent() << label_;
138  } else {
139  out << indent() << space;
140  }
141  out << std::right << std::scientific
142  << std::setprecision(Parameter::Precision)
143  << std::setw(Parameter::Width)
144  << (*arrayPtr_)[i]
145  << std::endl;
146  }
147  }
148  }
149 
150 }
151 #endif
std::string label() const
Return label string.
Definition: Parameter.cpp:158
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: FArrayParam.h:115
FArrayParam(const char *label, FArray< Type, N > &array, bool isRequired=true)
Constructor.
Definition: FArrayParam.h:88
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.
void writeParam(std::ostream &out)
Write FArray parameter to stream.
Definition: FArrayParam.h:131
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
Utility classes for scientific computation.
Definition: accumulators.mod:1
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
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: FArrayParam.h:108
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: FArrayParam.h:123
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: FArrayParam.h:97
std::string indent() const
Return indent string for this object (string of spaces).
A Parameter associated with a FArray container.
Definition: FArrayParam.h:26