PSCF v1.5.0
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, David Morse (morse012@um.edu)
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include <util/misc/Log.h>
12#include <util/param/MatrixParam.h>
13#include <util/containers/DMatrix.h>
14#include <util/format/Int.h>
15#include <util/format/Dbl.h>
16#ifdef UTIL_MPI
17#include <util/mpi/MpiSendRecv.h>
18#endif
19#include <util/global.h>
20
21#include <iomanip>
22#include <sstream>
23
24namespace Util
25{
26
48 template <class Type>
49 class DSymmMatrixParam : public MatrixParam<Type>
50 {
51
52 public:
53
62 DSymmMatrixParam(const char *label, DMatrix<Type>& matrix, int n,
63 bool isRequired = true);
64
70 void writeParam(std::ostream &out) const;
71
72 protected:
73
79 virtual void readValue(std::istream& in);
80
86 virtual void loadValue(Serializable::IArchive& ar);
87
93 virtual void saveValue(Serializable::OArchive& ar);
94
95 #ifdef UTIL_MPI
99 virtual void bcastValue();
100 #endif
101
104 using MatrixParam<Type>::m;
105 using MatrixParam<Type>::n;
106
107 protected:
108
109 using Parameter::label_;
110 using MatrixParam<Type>::hasBrackets;
111 using MatrixParam<Type>::setBrackets;
112 using MatrixParam<Type>::readEndBracket;
114
115 private:
116
118 DMatrix<Type>* matrixPtr_;
119
121 bool defaultZero_;
122
123 };
124
125 /*
126 * Constructor.
127 */
128 template <class Type>
130 DMatrix<Type>& matrix,
131 int n, bool isRequired)
132 : MatrixParam<Type>(label, n, n, isRequired),
133 matrixPtr_(&matrix),
134 defaultZero_(true)
135 {
136 // Set left and right brackets to parentheses
137 setBrackets("(",")");
138 }
139
140 /*
141 * Read a DMatrix from isteam.
142 */
143 template <class Type>
145 {
146 // Preconditions
147 if (!(matrixPtr_->isAllocated())) {
148 UTIL_THROW("Cannot read unallocated DMatrix");
149 }
150 if (n() != matrixPtr_->capacity1()) {
151 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
152 }
153 if (n() != matrixPtr_->capacity2()) {
154 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
155 }
156
157 // Create matrix of int/bool flags for error checking
158 // Initialize all elements to zero
159 DMatrix<int> flags;
160 flags.allocate(n(), n());
161 int i, j;
162 for (i = 0; i < n(); ++i) {
163 for (j = 0; j < n(); ++j) {
164 flags(i, j) = 0;
165 (*matrixPtr_)(i, j) = 0.0;
166 }
167 }
168
169 double value;
170 int k = 0;
171 bool open = true ;
172 while (open) {
173 std::string string;
174 in >> string;
175 std::stringstream stream;
176 stream << string;
177 if (string == ")") {
178 UTIL_CHECK(BracketPolicy::get() != BracketPolicy::Forbidden);
180 open = false;
181 readEndBracket(stream);
182 } else {
183 stream >> i;
184 in >> j >> value;
185 // Check the input before i and j are used as indices: the
186 // container bounds checks are compiled out unless UTIL_DEBUG
187 if (stream.fail() || in.fail()) {
188 Log::file() << "Error reading an element of matrix "
189 << this->label() << "\n";
190 UTIL_THROW("Error reading a symmetric matrix element");
191 }
192 if (i < 0 || i >= n() || j < 0 || j >= n()) {
193 Log::file() << "Element (" << i << ", " << j
194 << ") of matrix " << this->label() << " with "
195 << n() << " rows (indices start at 0)\n";
196 UTIL_THROW("Symmetric matrix element index out of range");
197 }
198 UTIL_CHECK(flags(i,j) == 0);
199 (*matrixPtr_)(i, j) = value;
200 flags(i, j) = 1;
201 if (i != j) {
202 UTIL_CHECK(flags(j,i) == 0);
203 (*matrixPtr_)(j, i) = value;
204 flags(j, i) = 1;
205 }
206 ++k;
207 if (k == (n()+1)*n()/2) {
208 open = false;
209 readEndBracket(in); // Only reads if hasBrackets
210 }
211 }
212 }
213
214 }
215
216 /*
217 * Load a DMatrix from input archive.
218 */
219 template <class Type>
221 {
222 if (!(matrixPtr_->isAllocated())) {
223 matrixPtr_->allocate(n(), n());
224 } else {
225 if (n() != matrixPtr_->capacity1()) {
226 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
227 }
228 if (n() != matrixPtr_->capacity2()) {
229 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
230 }
231 }
232 ar >> *matrixPtr_;
233 }
234
235 /*
236 * Save a DMatrix to an output archive.
237 */
238 template <class Type>
240 {
241 if (n() != matrixPtr_->capacity1()) {
242 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity1()");
243 }
244 if (n() != matrixPtr_->capacity2()) {
245 UTIL_THROW("Error: Logical size n() != DMatrix<Type>::capacity2()");
246 }
247 ar << *matrixPtr_;
248 }
249
250 #ifdef UTIL_MPI
251 /*
252 * Broadcast a DMatrix.
253 */
254 template <class Type>
256 {
257 if (!(matrixPtr_->isAllocated())) {
258 matrixPtr_->allocate(n(), n());
259 } else {
260 if (n() != matrixPtr_->capacity1()) {
261 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity1()");
262 }
263 if (n() != matrixPtr_->capacity2()) {
264 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
265 }
266 }
267 bcast<Type>(this->ioCommunicator(), *matrixPtr_, n(), n(), 0);
268 }
269 #endif
270
271 /*
272 * Write a DSymmMatrixParam.
273 */
274 template <class Type>
275 void DSymmMatrixParam<Type>::writeParam(std::ostream &out) const
276 {
277 if (isActive()) {
278 // Preconditions
279 if (!(matrixPtr_->isAllocated())) {
280 UTIL_THROW("Cannot read unallocated DMatrix");
281 }
282 if (n() > matrixPtr_->capacity1()) {
283 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity1()");
284 }
285 if (n() > matrixPtr_->capacity2()) {
286 UTIL_THROW("Error: Logical size n() > DMatrix<Type>::capacity2()");
287 }
288
289 if (hasBrackets()) {
290 out << indent() << label_ << std::endl;
291 }
292 Label space("");
293 int i, j;
294 for (i = 0; i < n(); ++i) {
295 for (j = 0; j <= i; ++j) {
296 if (i == 0 && j == 0 && !hasBrackets()) {
297 out << indent() << label_;
298 } else {
299 out << indent() << space;
300 }
301 out << Int(i, 4) << " " << Int(j, 4) << " "
302 << std::right << std::scientific
303 << std::setprecision(Parameter::Precision)
304 << std::setw(Parameter::Width)
305 << (*matrixPtr_)(i, j)
306 << std::endl;
307 }
308 }
309 writeEndBracket(out);
310 }
311 }
312
313}
314#endif
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.
std::string indent() const
Return indent string for this object (string of spaces).
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.
Label label_
Label object that contains parameter label string.
Definition Parameter.h:185
bool isActive() const
Is this parameter active?
Int()
Default constructor.
Definition Int.cpp:19
A label string in a file format.
Definition Label.h:73
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
int m() const
Get the logical array dimension.
Definition MatrixParam.h:84
std::string label() const
Return label string.
void setBrackets(std::string lBracket, std::string rBracket)
Set left and right bracket / delimiter strings.
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?
bool isRequired() const
Is this an optional parameter?
MatrixParam(const char *label, int m, int n, bool isRequired=true)
Constructor.
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?
static const int Width
Width of output field for a scalar variable.
Definition Parameter.h:53
BinaryFileIArchive IArchive
Type of input archive used by load method.
BinaryFileOArchive OArchive
Type of output archive used by save method.
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:49
BracketPolicy::Type get()
Get value of bracket policy.
Utility classes for scientific computation.