PSCF v1.1
DArrayParam.h
1#ifndef UTIL_D_ARRAY_PARAM_H
2#define UTIL_D_ARRAY_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/ArrayParam.h> // base class
12#include <util/containers/DArray.h> // member
13#include <util/global.h>
14
15#include <iomanip>
16
17namespace Util
18{
19
25 template <typename Type>
26 class DArrayParam : public ArrayParam<Type>
27 {
28
29 public:
30
39 DArrayParam(const char *label, DArray<Type>& array, int n,
40 bool isRequired = true);
41
47 void writeParam(std::ostream &out) const;
48
49 using ArrayParam<Type>::readParam;
50
51 protected:
52
56 Type& element(int i)
57 { return (*arrayPtr_)[i]; }
58
62 Type const & element(int i) const
63 { return (*arrayPtr_)[i]; }
64
70 virtual void readValue(std::istream& in);
71
77 virtual void loadValue(Serializable::IArchive& ar);
78
84 virtual void saveValue(Serializable::OArchive& ar);
85
86 #ifdef UTIL_MPI
90 virtual void bcastValue();
91 #endif
92
93 using ArrayParam<Type>::n;
94
95 private:
96
98 DArray<Type>* arrayPtr_;
99
100 };
101
102 /*
103 * DArrayParam<Type> constructor.
104 */
105 template <class Type>
107 DArray<Type>& array, int n,
108 bool isRequired)
109 : ArrayParam<Type>(label, n, isRequired),
110 arrayPtr_(&array)
111 {}
112
113 /*
114 * Write a DArray to a parameter file.
115 */
116 template <class Type>
117 void DArrayParam<Type>::writeParam(std::ostream &out) const
118 {
119 if (Parameter::isActive()) {
120 if (!(arrayPtr_->isAllocated())) {
121 UTIL_THROW("Cannot write unallocated DArray");
122 }
123 if (arrayPtr_->capacity() != n()) {
124 UTIL_THROW("Error: DArray capacity != n in writeParam");
125 }
127 }
128 }
129
130 /*
131 * Read array of values from isteam.
132 */
133 template <class Type>
134 void DArrayParam<Type>::readValue(std::istream &in)
135 {
136 if (!(arrayPtr_->isAllocated())) {
137 UTIL_THROW("Cannot read unallocated DArray");
138 }
139 if (arrayPtr_->capacity() != n()) {
140 UTIL_THROW("Error: DArray capacity < n");
141 }
143 }
144
145 /*
146 * Load a DArray from input archive.
147 */
148 template <class Type>
150 {
151 if (!(arrayPtr_->isAllocated())) {
152 arrayPtr_->allocate(n());
153 }
154 ar >> *arrayPtr_;
155 if (arrayPtr_->capacity() < n()) {
156 UTIL_THROW("Error: DArray capacity < n");
157 }
158 }
159
160 /*
161 * Save a DArray to an output archive.
162 */
163 template <class Type>
165 {
166 if (!(arrayPtr_->isAllocated())) {
167 UTIL_THROW("Cannot save unallocated DArray");
168 }
169 if (arrayPtr_->capacity() != n()) {
170 UTIL_THROW("Error: DArray capacity < n");
171 }
172 ar << *arrayPtr_;
173 }
174
175 #ifdef UTIL_MPI
176 /*
177 * Broadcast a DArray.
178 */
179 template <class Type>
181 { bcast<Type>(ioCommunicator(), *arrayPtr_, n(), 0); }
182 #endif
183
184}
185#endif
An array-valued parameter in a parameter file.
Definition: ArrayParam.h:56
int n() const
Get the logical array dimension.
Definition: ArrayParam.h:86
std::string label() const
Return label string.
Definition: Parameter.cpp:164
void writeParam(std::ostream &out) const
Write an array-valued parameter to stream.
Definition: ArrayParam.tpp:139
virtual void readValue(std::istream &in)
Read array of element values from an input stream.
Definition: ArrayParam.tpp:103
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:170
Saving archive for binary istream.
Saving / output archive for binary ostream.
A Parameter associated with a DArray container.
Definition: DArrayParam.h:27
Type & element(int i)
Return a reference to one element of the array.
Definition: DArrayParam.h:56
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: DArrayParam.h:149
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: DArrayParam.h:180
void writeParam(std::ostream &out) const
Write parameter to stream.
Definition: DArrayParam.h:117
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: DArrayParam.h:164
Type const & element(int i) const
Return a reference to one element of the array.
Definition: DArrayParam.h:62
DArrayParam(const char *label, DArray< Type > &array, int n, bool isRequired=true)
Constructor.
Definition: DArrayParam.h:106
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: DArrayParam.h:134
Dynamically allocatable contiguous array template.
Definition: DArray.h:32
virtual void readParam(std::istream &in)
Read a label and (if the label matches) a parameter value.
Definition: Parameter.cpp:42
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:176
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