PSCF v1.1
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/MatrixParam.h>
12#include <util/global.h>
13
14#ifdef UTIL_MPI
16#endif
17
18#include <iomanip>
19
20namespace Util
21{
22
28 template <class Type>
29 class CArray2DParam : public MatrixParam<Type>
30 {
31
32 public:
33
50 CArray2DParam(const char *label, Type* ptr,
51 int m, int n, int np, bool isRequired = true);
52
58 void writeParam(std::ostream& out) const;
59
62 using MatrixParam<Type>::m;
63 using MatrixParam<Type>::n;
64
65 protected:
66
67 using MatrixParam<Type>::hasBrackets;
68
74 virtual void readValue(std::istream& in);
75
81 virtual void loadValue(Serializable::IArchive& ar);
82
88 virtual void saveValue(Serializable::OArchive& ar);
89
90 #ifdef UTIL_MPI
94 virtual void bcastValue();
95 #endif
96
97 private:
98
100 Type* ptr_;
101
103 int np_;
104
105 };
106
107
108 /*
109 * CArray2D constructor.
110 */
111 template <class Type>
112 CArray2DParam<Type>::CArray2DParam(const char* label, Type* ptr,
113 int m, int n, int np,
114 bool isRequired)
115 : MatrixParam<Type>(label, m, n, isRequired),
116 ptr_(ptr),
117 np_(np)
118 {
119 // Set matrix delimiter symbols to left and right square brackets
121 }
122
123 /*
124 * Read a DArray from isteam.
125 */
126 template <class Type>
127 void CArray2DParam<Type>::readValue(std::istream &in)
128 {
129 int i, j;
130 for (i = 0; i < m(); ++i) {
131 for (j = 0; j < n(); ++j) {
132 in >> ptr_[i*np_ + j];
133 }
134 }
136 }
137
138 /*
139 * Load a DArray from input archive.
140 */
141 template <class Type>
143 { ar.unpack(ptr_, m(), n(), np_); }
144
145 /*
146 * Save a DArray to an output archive.
147 */
148 template <class Type>
150 { ar.pack(ptr_, m(), n(), np_); }
151
152 #ifdef UTIL_MPI
153 /*
154 * Broadcast a DArray.
155 */
156 template <class Type>
158 { bcast<Type>(ioCommunicator(), ptr_, m()*np_, 0); }
159 #endif
160
161 /*
162 * Write a CArray2DParam.
163 */
164 template <class Type>
165 void CArray2DParam<Type>::writeParam(std::ostream &out) const
166 {
167
168 if (isActive()) {
169 Label space("");
170 int i, j;
171
172 if (hasBrackets()) {
173 out << indent() << Parameter::label_ << std::endl;
174 }
175 for (i = 0; i < m(); ++i) {
176 if (i == 0 && !hasBrackets()) {
177 out << indent() << Parameter::label_;
178 } else {
179 out << indent() << space;
180 }
181 for (j = 0; j < n(); ++j) {
182 out << std::right << std::scientific
183 << std::setprecision(Parameter::Precision)
184 << std::setw(Parameter::Width)
185 << ptr_[i*np_ + j];
186 }
187 out << std::endl;
188 }
190 }
191 }
192}
193#endif
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
Saving archive for binary istream.
void unpack(T &data)
Unpack a single T object.
Saving / output archive for binary ostream.
void pack(const T &data)
Pack one object of type T.
A Parameter associated with a 2D built-in C array.
Definition: CArray2DParam.h:30
virtual void readValue(std::istream &in)
Read 2D array parameter from an input stream.
virtual void loadValue(Serializable::IArchive &ar)
Load 2D array from an archive.
virtual void saveValue(Serializable::OArchive &ar)
Save 2D array to an archive.
virtual void bcastValue()
Broadcast 2D array within the ioCommunicator.
CArray2DParam(const char *label, Type *ptr, int m, int n, int np, bool isRequired=true)
Constructor.
void writeParam(std::ostream &out) const
Write 2D C array to file.
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.
Utility classes for scientific computation.
Definition: accumulators.mod:1