Simpatico  v1.10
DMatrixParam.h
1 #ifndef UTIL_DMATRIX_PARAM_H
2 #define UTIL_DMATRIX_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/DMatrix.h>
13 #ifdef UTIL_MPI
14 #include <util/mpi/MpiSendRecv.h>
15 #endif
16 #include <util/global.h>
17 
18 #include <iomanip>
19 
20 namespace Util
21 {
22 
28  template <class Type>
29  class DMatrixParam : public Parameter
30  {
31 
32  public:
33 
43  DMatrixParam(const char *label, DMatrix<Type>& matrix, int m, int n, bool isRequired = true);
44 
48  void writeParam(std::ostream &out);
49 
50  protected:
51 
57  virtual void readValue(std::istream& in);
58 
64  virtual void loadValue(Serializable::IArchive& ar);
65 
71  virtual void saveValue(Serializable::OArchive& ar);
72 
73  #ifdef UTIL_MPI
74 
77  virtual void bcastValue();
78  #endif
79 
80  private:
81 
83  DMatrix<Type>* matrixPtr_;
84 
86  int m_;
87 
89  int n_;
90 
91  };
92 
93  /*
94  * DMatrix constructor.
95  */
96  template <class Type>
97  DMatrixParam<Type>::DMatrixParam(const char* label, DMatrix<Type>& matrix, int m, int n, bool isRequired)
98  : Parameter(label, isRequired),
99  matrixPtr_(&matrix),
100  m_(m),
101  n_(n)
102  {}
103 
104  /*
105  * Read a DMatrix from isteam.
106  */
107  template <class Type>
108  void DMatrixParam<Type>::readValue(std::istream &in)
109  {
110  // Preconditions
111  if (!(matrixPtr_->isAllocated())) {
112  UTIL_THROW("Cannot read unallocated DMatrix");
113  }
114  if (m_ != matrixPtr_->capacity1()) {
115  UTIL_THROW("Error: Logical size m_ != DMatrix<Type>::capacity1()");
116  }
117  if (n_ != matrixPtr_->capacity2()) {
118  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
119  }
120 
121  int i, j;
122  for (i = 0; i < m_; ++i) {
123  for (j = 0; j < n_; ++j) {
124  in >> (*matrixPtr_)(i, j);
125  }
126  }
127  }
128 
129  /*
130  * Load a DMatrix from input archive.
131  */
132  template <class Type>
134  {
135  if (!(matrixPtr_->isAllocated())) {
136  matrixPtr_->allocate(m_, n_);
137  } else {
138  if (m_ != matrixPtr_->capacity1()) {
139  UTIL_THROW("Error: Logical size m_ != DMatrix<Type>::capacity1()");
140  }
141  if (n_ != matrixPtr_->capacity2()) {
142  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
143  }
144  }
145  ar >> *matrixPtr_;
146  }
147 
148  /*
149  * Save a DMatrix to an output archive.
150  */
151  template <class Type>
153  {
154  if (m_ != matrixPtr_->capacity1()) {
155  UTIL_THROW("Error: Logical size m_ != DMatrix<Type>::capacity1()");
156  }
157  if (n_ != matrixPtr_->capacity2()) {
158  UTIL_THROW("Error: Logical size n_ != DMatrix<Type>::capacity2()");
159  }
160  ar << *matrixPtr_;
161  }
162 
163  #ifdef UTIL_MPI
164  /*
165  * Broadcast a DMatrix.
166  */
167  template <class Type>
169  {
170  if (!(matrixPtr_->isAllocated())) {
171  matrixPtr_->allocate(m_, n_);
172  } else {
173  if (m_ != matrixPtr_->capacity1()) {
174  UTIL_THROW("Error: Logical size m_ > DMatrix<Type>::capacity1()");
175  }
176  if (n_ != matrixPtr_->capacity2()) {
177  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity2()");
178  }
179  }
180  bcast<Type>(ioCommunicator(), *matrixPtr_, m_, n_, 0);
181  }
182  #endif
183 
184  /*
185  * Write a DMatrixParam.
186  */
187  template <class Type>
188  void DMatrixParam<Type>::writeParam(std::ostream &out)
189  {
190  if (isActive()) {
191  // Preconditions
192  if (!(matrixPtr_->isAllocated())) {
193  UTIL_THROW("Cannot read unallocated DMatrix");
194  }
195  if (m_ > matrixPtr_->capacity1()) {
196  UTIL_THROW("Error: Logical size m_ > DMatrix<Type>::capacity1()");
197  }
198  if (n_ > matrixPtr_->capacity2()) {
199  UTIL_THROW("Error: Logical size n_ > DMatrix<Type>::capacity2()");
200  }
201 
202  Label space("");
203  int i, j;
204  for (i = 0; i < m_; ++i) {
205  if (i == 0) {
206  out << indent() << label_;
207  } else {
208  out << indent() << space;
209  }
210  for (j = 0; j < n_; ++j) {
211  out << std::right << std::scientific
212  << std::setprecision(Parameter::Precision)
213  << std::setw(Parameter::Width)
214  << (*matrixPtr_)(i, j);
215  }
216  out << std::endl;
217  }
218  }
219  }
220 
221 }
222 #endif
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition: DMatrix.h:170
std::string label() const
Return label string.
Definition: Parameter.cpp:158
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: DMatrixParam.h:133
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.
void writeParam(std::ostream &out)
Write DMatrix to file.
Definition: DMatrixParam.h:188
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
int capacity2() const
Get number of columns (range of the second array index).
Definition: Matrix.h:132
A Parameter associated with a 2D built-in C array.
Definition: DMatrixParam.h:29
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
int capacity1() const
Get number of rows (range of the first array index).
Definition: Matrix.h:125
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: DMatrixParam.h:152
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
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
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: DMatrixParam.h:168
std::string indent() const
Return indent string for this object (string of spaces).
bool isAllocated() const
Return true if the DMatrix has been allocated, false otherwise.
Definition: DMatrix.h:202
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: DMatrixParam.h:108
DMatrixParam(const char *label, DMatrix< Type > &matrix, int m, int n, bool isRequired=true)
Constructor.
Definition: DMatrixParam.h:97