PSCF v1.1
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/MatrixParam.h>
12#include <util/containers/DMatrix.h>
13#ifdef UTIL_MPI
15#endif
16#include <util/global.h>
17
18#include <iomanip>
19
20namespace Util
21{
22
28 template <class Type>
29 class DMatrixParam : public MatrixParam<Type>
30 {
31
32 public:
33
43 DMatrixParam(const char *label, DMatrix<Type>& matrix, int m, int n,
44 bool isRequired = true);
45
49 void writeParam(std::ostream &out) const;
50
53 using MatrixParam<Type>::m;
54 using MatrixParam<Type>::n;
55
56 protected:
57
58 using MatrixParam<Type>::hasBrackets;
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
85 virtual void bcastValue();
86 #endif
87
88 private:
89
91 DMatrix<Type>* matrixPtr_;
92
93 };
94
95 /*
96 * DMatrix constructor.
97 */
98 template <class Type>
100 int m, int n, bool isRequired)
101 : MatrixParam<Type>(label, m, n, isRequired),
102 matrixPtr_(&matrix)
103 {
104 // Set delimiter strings to left and right square brackets.
106 }
107
108 /*
109 * Read a DMatrix from isteam.
110 */
111 template <class Type>
112 void DMatrixParam<Type>::readValue(std::istream &in)
113 {
114 // Preconditions
115 if (!(matrixPtr_->isAllocated())) {
116 UTIL_THROW("Cannot read unallocated DMatrix");
117 }
118 if (m() != matrixPtr_->capacity1()) {
119 UTIL_THROW("Error: Logical size m() != DMatrix<Type>::capacity1()");
120 }
121 if (n() != matrixPtr_->capacity2()) {
122 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
123 }
124
125 int i, j;
126 for (i = 0; i < m(); ++i) {
127 for (j = 0; j < n(); ++j) {
128 in >> (*matrixPtr_)(i, j);
129 }
130 }
131
133 }
134
135 /*
136 * Load a DMatrix from input archive.
137 */
138 template <class Type>
140 {
141 if (!(matrixPtr_->isAllocated())) {
142 matrixPtr_->allocate(m(), n());
143 } else {
144 if (m() != matrixPtr_->capacity1()) {
145 UTIL_THROW("Error: Logical size m() != DMatrix<Type>::capacity1()");
146 }
147 if (n() != matrixPtr_->capacity2()) {
148 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
149 }
150 }
151 ar >> *matrixPtr_;
152 }
153
154 /*
155 * Save a DMatrix to an output archive.
156 */
157 template <class Type>
159 {
160 if (m() != matrixPtr_->capacity1()) {
161 UTIL_THROW("Error: Logical size m() != DMatrix<Type>::capacity1()");
162 }
163 if (n() != matrixPtr_->capacity2()) {
164 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
165 }
166 ar << *matrixPtr_;
167 }
168
169 #ifdef UTIL_MPI
170 /*
171 * Broadcast a DMatrix.
172 */
173 template <class Type>
175 {
176 if (!(matrixPtr_->isAllocated())) {
177 matrixPtr_->allocate(m(), n());
178 } else {
179 if (m() != matrixPtr_->capacity1()) {
180 UTIL_THROW("Error: Logical size m() > DMatrix<Type>::capacity1()");
181 }
182 if (n() != matrixPtr_->capacity2()) {
183 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
184 }
185 }
186 bcast<Type>(ioCommunicator(), *matrixPtr_, m(), n(), 0);
187 }
188 #endif
189
190 /*
191 * Write a DMatrixParam.
192 */
193 template <class Type>
194 void DMatrixParam<Type>::writeParam(std::ostream &out) const
195 {
196 if (isActive()) {
197 // Preconditions
198 if (!(matrixPtr_->isAllocated())) {
199 UTIL_THROW("Cannot read unallocated DMatrix");
200 }
201 if (m() > matrixPtr_->capacity1()) {
202 UTIL_THROW("Error: Logical size m() > DMatrix<Type>::capacity1()");
203 }
204 if (n() > matrixPtr_->capacity2()) {
205 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
206 }
207
208 if (hasBrackets()) {
209 out << indent() << Parameter::label_ << std::endl;
210 }
211 Label space("");
212 int i, j;
213 for (i = 0; i < m(); ++i) {
214 if (i == 0 && !hasBrackets()) {
215 out << indent() << Parameter::label_;
216 } else {
217 out << indent() << space;
218 }
219 for (j = 0; j < n(); ++j) {
220 out << std::right << std::scientific
221 << std::setprecision(Parameter::Precision)
222 << std::setw(Parameter::Width)
223 << (*matrixPtr_)(i, j);
224 }
225 out << std::endl;
226 }
228 }
229 }
230
231}
232#endif
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
Saving archive for binary istream.
Saving / output archive for binary ostream.
A Parameter associated with a 2D built-in C array.
Definition: DMatrixParam.h:30
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: DMatrixParam.h:112
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: DMatrixParam.h:174
void writeParam(std::ostream &out) const
Write DMatrix to file.
Definition: DMatrixParam.h:194
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: DMatrixParam.h:139
DMatrixParam(const char *label, DMatrix< Type > &matrix, int m, int n, bool isRequired=true)
Constructor.
Definition: DMatrixParam.h:99
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: DMatrixParam.h:158
Dynamically allocated Matrix.
Definition: DMatrix.h:25
A label string in a file format.
Definition: Label.h:73
An array-valued parameter in a parameter file.
Definition: MatrixParam.h:62
int m() const
Get the logical array dimension.
Definition: MatrixParam.h:84
std::string label() const
Return label string.
Definition: Parameter.cpp:164
void setBrackets(std::string lBracket, std::string rBracket)
Set left and right bracket / delimiter strings.
Definition: MatrixParam.tpp:54
void readEndBracket(std::istream &in)
Read the closing delimiter, if any.
void writeEndBracket(std::ostream &out) const
Write the end bracket delimiter, if any.
bool hasBrackets() const
Are brackets being used as delimiters?
Definition: MatrixParam.h:131
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:170
int n() const
Get the logical array dimension.
Definition: MatrixParam.h:90
std::string indent() const
Return indent string for this object (string of spaces).
static const int Precision
Precision for io of floating point data field.
Definition: Parameter.h:56
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:176
static const int Width
Width of output field for a scalar variable.
Definition: Parameter.h:53
File containing preprocessor macros for error handling.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
Utility classes for scientific computation.
Definition: accumulators.mod:1