Simpatico  v1.10
CArray2DParam.h
1 #ifndef UTIL_CARRAY_2D_PARAM_H
2 #define UTIL_CARRAY_2D_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/global.h>
13 
14 #ifdef UTIL_MPI
15 #include <util/mpi/MpiSendRecv.h>
16 #endif
17 
18 #include <iomanip>
19 
20 namespace Util
21 {
22 
28  template <class Type>
29  class CArray2DParam : public Parameter
30  {
31 
32  public:
33 
50  CArray2DParam(const char *label, Type* ptr,
51  int m, int n, int np, bool isRequired = true);
52 
56  void writeParam(std::ostream &out);
57 
58  protected:
59 
65  virtual void readValue(std::istream& in);
66 
72  virtual void loadValue(Serializable::IArchive& ar);
73 
79  virtual void saveValue(Serializable::OArchive& ar);
80 
81  #ifdef UTIL_MPI
82 
85  virtual void bcastValue();
86  #endif
87 
88  private:
89 
91  Type* ptr_;
92 
94  int m_;
95 
97  int n_;
98 
100  int np_;
101 
102  };
103 
104 
105  /*
106  * CArray2D constructor.
107  */
108  template <class Type>
109  CArray2DParam<Type>::CArray2DParam(const char* label, Type* ptr, int m, int n, int np, bool isRequired)
110  : Parameter(label, isRequired),
111  ptr_(ptr),
112  m_(m),
113  n_(n),
114  np_(np)
115  {}
116 
117  /*
118  * Read a DArray from isteam.
119  */
120  template <class Type>
121  void CArray2DParam<Type>::readValue(std::istream &in)
122  {
123  int i, j;
124  for (i = 0; i < m_; ++i) {
125  for (j = 0; j < n_; ++j) {
126  in >> ptr_[i*np_ + j];
127  }
128  }
129  }
130 
131  /*
132  * Load a DArray from input archive.
133  */
134  template <class Type>
136  { ar.unpack(ptr_, m_, n_, np_); }
137 
138  /*
139  * Save a DArray to an output archive.
140  */
141  template <class Type>
143  { ar.pack(ptr_, m_, n_, np_); }
144 
145  #ifdef UTIL_MPI
146  /*
147  * Broadcast a DArray.
148  */
149  template <class Type>
151  { bcast<Type>(ioCommunicator(), ptr_, m_*np_, 0); }
152  #endif
153 
154  /*
155  * Write a CArray2DParam.
156  */
157  template <class Type>
158  void CArray2DParam<Type>::writeParam(std::ostream &out)
159  {
160  if (isActive()) {
161  Label space("");
162  int i, j;
163 
164  for (i = 0; i < m_; ++i) {
165  if (i == 0) {
166  out << indent() << label_;
167  } else {
168  out << indent() << space;
169  }
170  for (j = 0; j < n_; ++j) {
171  out << std::right << std::scientific
172  << std::setprecision(Parameter::Precision)
173  << std::setw(Parameter::Width)
174  << ptr_[i*np_ + j];
175  }
176  out << std::endl;
177  }
178  }
179  }
180 }
181 #endif
std::string label() const
Return label string.
Definition: Parameter.cpp:158
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 2D array to an archive.
virtual void readValue(std::istream &in)
Read 2D array parameter from an input stream.
File containing preprocessor macros for error handling.
A Parameter associated with a 2D built-in C array.
Definition: CArray2DParam.h:29
Saving / output archive for binary ostream.
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
virtual void loadValue(Serializable::IArchive &ar)
Load 2D array from an archive.
Utility classes for scientific computation.
Definition: accumulators.mod:1
virtual void bcastValue()
Broadcast 2D array within the ioCommunicator.
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
CArray2DParam(const char *label, Type *ptr, int m, int n, int np, bool isRequired=true)
Constructor.
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>.
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
void writeParam(std::ostream &out)
Write 2D C array to file.
std::string indent() const
Return indent string for this object (string of spaces).