PSCF v1.1
DSymmMatrixParam.h
1#ifndef UTIL_D_SYMM_MATRIX_PARAM_H
2#define UTIL_D_SYMM_MATRIX_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#include <util/format/Int.h>
14#include <util/format/Dbl.h>
15#ifdef UTIL_MPI
17#endif
18#include <util/global.h>
19
20#include <iomanip>
21#include <sstream>
22
23namespace Util
24{
25
47 template <class Type>
48 class DSymmMatrixParam : public MatrixParam<Type>
49 {
50
51 public:
52
61 DSymmMatrixParam(const char *label, DMatrix<Type>& matrix, int n,
62 bool isRequired = true);
63
69 void writeParam(std::ostream &out) const;
70
71 protected:
72
78 virtual void readValue(std::istream& in);
79
85 virtual void loadValue(Serializable::IArchive& ar);
86
92 virtual void saveValue(Serializable::OArchive& ar);
93
94 #ifdef UTIL_MPI
98 virtual void bcastValue();
99 #endif
100
103 using MatrixParam<Type>::m;
104 using MatrixParam<Type>::n;
105
106 protected:
107
108 using Parameter::label_;
109 using MatrixParam<Type>::hasBrackets;
110 using MatrixParam<Type>::setBrackets;
111 using MatrixParam<Type>::readEndBracket;
113
114 private:
115
117 DMatrix<Type>* matrixPtr_;
118
120 bool defaultZero_;
121
122 };
123
124 /*
125 * Constructor.
126 */
127 template <class Type>
129 DMatrix<Type>& matrix,
130 int n, bool isRequired)
131 : MatrixParam<Type>(label, n, n, isRequired),
132 matrixPtr_(&matrix),
133 defaultZero_(true)
134 {
135 // Set left and right brackets to parentheses
136 setBrackets("(",")");
137 }
138
139 /*
140 * Read a DMatrix from isteam.
141 */
142 template <class Type>
144 {
145 // Preconditions
146 if (!(matrixPtr_->isAllocated())) {
147 UTIL_THROW("Cannot read unallocated DMatrix");
148 }
149 if (n() != matrixPtr_->capacity1()) {
150 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
151 }
152 if (n() != matrixPtr_->capacity2()) {
153 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
154 }
155
156 // Create matrix of int/bool flags for error checking
157 // Initialize all elements to zero
158 DMatrix<int> flags;
159 flags.allocate(n(), n());
160 int i, j;
161 for (i = 0; i < n(); ++i) {
162 for (j = 0; j < n(); ++j) {
163 flags(i, j) = 0;
164 (*matrixPtr_)(i, j) = 0.0;
165 }
166 }
167
168 double value;
169 int k = 0;
170 bool open = true ;
171 while (open) {
172 std::string string;
173 in >> string;
174 std::stringstream stream;
175 stream << string;
176 if (string == ")") {
177 UTIL_CHECK(BracketPolicy::get() != BracketPolicy::Forbidden);
178 UTIL_CHECK(hasBrackets());
179 open = false;
180 readEndBracket(stream);
181 } else {
182 stream >> i;
183 in >> j >> value;
184 UTIL_CHECK(flags(i,j) == 0);
185 (*matrixPtr_)(i, j) = value;
186 flags(i, j) = 1;
187 if (i != j) {
188 UTIL_CHECK(flags(j,i) == 0);
189 (*matrixPtr_)(j, i) = value;
190 flags(j, i) = 1;
191 }
192 ++k;
193 if (k == (n()+1)*n()/2) {
194 open = false;
195 readEndBracket(in); // Only reads if hasBrackets
196 }
197 }
198 }
199
200 }
201
202 /*
203 * Load a DMatrix from input archive.
204 */
205 template <class Type>
207 {
208 if (!(matrixPtr_->isAllocated())) {
209 matrixPtr_->allocate(n(), n());
210 } else {
211 if (n() != matrixPtr_->capacity1()) {
212 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
213 }
214 if (n() != matrixPtr_->capacity2()) {
215 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
216 }
217 }
218 ar >> *matrixPtr_;
219 }
220
221 /*
222 * Save a DMatrix to an output archive.
223 */
224 template <class Type>
226 {
227 if (n() != matrixPtr_->capacity1()) {
228 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
229 }
230 if (n() != matrixPtr_->capacity2()) {
231 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
232 }
233 ar << *matrixPtr_;
234 }
235
236 #ifdef UTIL_MPI
237 /*
238 * Broadcast a DMatrix.
239 */
240 template <class Type>
242 {
243 if (!(matrixPtr_->isAllocated())) {
244 matrixPtr_->allocate(n(), n());
245 } else {
246 if (n() != matrixPtr_->capacity1()) {
247 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity1()");
248 }
249 if (n() != matrixPtr_->capacity2()) {
250 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
251 }
252 }
253 bcast<Type>(ioCommunicator(), *matrixPtr_, n(), n(), 0);
254 }
255 #endif
256
257 /*
258 * Write a DSymmMatrixParam.
259 */
260 template <class Type>
261 void DSymmMatrixParam<Type>::writeParam(std::ostream &out) const
262 {
263 if (isActive()) {
264 // Preconditions
265 if (!(matrixPtr_->isAllocated())) {
266 UTIL_THROW("Cannot read unallocated DMatrix");
267 }
268 if (n() > matrixPtr_->capacity1()) {
269 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity1()");
270 }
271 if (n() > matrixPtr_->capacity2()) {
272 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
273 }
274
275 if (hasBrackets()) {
276 out << indent() << label_ << std::endl;
277 }
278 Label space("");
279 int i, j;
280 for (i = 0; i < n(); ++i) {
281 for (j = 0; j <= i; ++j) {
282 if (i == 0 && j == 0 && !hasBrackets()) {
283 out << indent() << label_;
284 } else {
285 out << indent() << space;
286 }
287 out << Int(i, 4) << " " << Int(j, 4) << " "
288 << std::right << std::scientific
289 << std::setprecision(Parameter::Precision)
290 << std::setw(Parameter::Width)
291 << (*matrixPtr_)(i, j)
292 << std::endl;
293 }
294 }
295 writeEndBracket(out);
296 }
297 }
298
299}
300#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.
Dynamically allocated Matrix.
Definition: DMatrix.h:25
void allocate(int capacity1, int capacity2)
Allocate memory for a matrix.
Definition: DMatrix.h:170
A Parameter associated with a square symmetric DMatrix.
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
void writeParam(std::ostream &out) const
Write symmetric DMatrix to file.
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
DSymmMatrixParam(const char *label, DMatrix< Type > &matrix, int n, bool isRequired=true)
Constructor.
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Wrapper for an int, for formatted ostream output.
Definition: Int.h:37
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_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition: global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
BracketPolicy::Type get()
Get value of bracket policy.
Utility classes for scientific computation.
Definition: accumulators.mod:1