PSCF v1.1
Parameter.cpp
1/*
2* Util Package - C++ Utilities for Scientific Computation
3*
4* Copyright 2010 - 2017, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "Parameter.h"
9
10#ifdef UTIL_MPI
12#endif
13
14#include <iomanip>
15
16namespace Util
17{
18
19 /*
20 * Constructor.
21 */
22 Parameter::Parameter(const char *label, bool isRequired)
23 : label_(label, isRequired),
24 isActive_(isRequired)
25 {}
26
27 /*
28 * Destructor.
29 */
31 {}
32
33 /*
34 * Read label string and attempt to match.
35 */
36 void Parameter::readLabel(std::istream &in)
37 { in >> label_; }
38
39 /*
40 * Read a parameter.
41 */
42 void Parameter::readParam(std::istream &in)
43 {
44 if (isIoProcessor()) {
45
46 // Read the label and attempt to match.
47 readLabel(in);
48
49 // If this parameter is required and the
50 // label does not match, an exception will
51 // be thrown by the >> operator.
52
53 if (Label::isMatched()) {
54
55 // If the label string matches
56 readValue(in);
57 isActive_ = true;
60 }
61
62 } else {
63
64 // If label does not match.
65 // Note: Label must be optional or an
66 // Exception should have been thrown.
67 assert(!isRequired());
68 isActive_ = false;
70 Log::file() << indent()
71 << label_ << std::right
72 << std::setw(Parameter::Width)
73 << "[ absent ]" << std::endl;
74 }
75
76 }
77 } else {
78 #ifdef UTIL_MPI
79 if (!hasIoCommunicator()) {
80 UTIL_THROW("Error: not isIoProcessor and not hasIoCommunicator");
81 }
82 #else
83 UTIL_THROW("Error: not isIoProcessor and no MPI");
84 #endif
85 }
86 #ifdef UTIL_MPI
87 if (hasIoCommunicator()) {
88 if (isRequired()) {
89 bcastValue();
90 isActive_ = true;
91 } else {
93 if (isActive_) {
94 bcastValue();
95 }
96 }
97 }
98 #endif
99 }
100
101 /*
102 * Load from an archive.
103 */
105 {
106 if (isIoProcessor()) {
107 if (isRequired()) {
108 isActive_ = true;
109 } else {
110 ar >> isActive_;
111 }
112 if (isActive_) {
113 loadValue(ar);
114 if (ParamComponent::echo()) {
116 }
117 } else {
118 if (ParamComponent::echo() && !isRequired()) {
119 Log::file() << indent()
120 << label_ << std::right
121 << std::setw(Parameter::Width)
122 << "[ absent ]" << std::endl;
123 }
124 }
125 } else {
126 #ifdef UTIL_MPI
127 if (!hasIoCommunicator()) {
128 UTIL_THROW("Error: not isIoProcessor and !hasIoCommunicator");
129 }
130 #else
131 UTIL_THROW("Error: not isIoProcessor and no MPI");
132 #endif
133 }
134 #ifdef UTIL_MPI
135 if (hasIoCommunicator()) {
136 if (isRequired()) {
137 isActive_ = true;
138 } else {
140 }
141 if (isActive_) {
142 bcastValue();
143 }
144 }
145 #endif
146 }
147
148 /*
149 * Save to an archive.
150 */
152 {
153 if (!isRequired()) {
154 ar << isActive_;
155 }
156 if (isActive_) {
157 saveValue(ar);
158 }
159 }
160
161 /*
162 * Return label string.
163 */
164 std::string Parameter::label() const
165 { return label_.string(); }
166
167 /*
168 * Is this a required parameter?
169 */
171 { return label_.isRequired(); }
172
173 /*
174 * Is this an active parameter?
175 */
177 { return isActive_; }
178
179}
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.
std::string string() const
Return label string.
Definition: Label.cpp:111
static bool isMatched()
Did the most recent attempt to match a Label succeed?
Definition: Label.h:274
bool isRequired() const
Is this the label for a required component?
Definition: Label.h:261
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
bool isIoProcessor() const
Can this processor do file I/O ?
Definition: MpiFileIo.h:92
bool hasIoCommunicator() const
Does this object have an associated MPI communicator?
Definition: MpiFileIo.h:99
std::string indent() const
Return indent string for this object (string of spaces).
virtual void writeParam(std::ostream &out) const =0
Read parameter(s) to file.
static bool echo()
Get echo parameter.
virtual ~Parameter()
Destructor.
Definition: Parameter.cpp:30
virtual void readLabel(std::istream &in)
Read and attempt to match label string.
Definition: Parameter.cpp:36
std::string label() const
Return label string.
Definition: Parameter.cpp:164
Parameter(const char *label, bool isRequired=true)
Constructor.
Definition: Parameter.cpp:22
virtual void save(Serializable::OArchive &ar)
Save to an archive.
Definition: Parameter.cpp:151
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: Parameter.h:202
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: Parameter.h:216
bool isActive_
Is this parameter active (always true if isRequired).
Definition: Parameter.h:188
virtual void readParam(std::istream &in)
Read a label and (if the label matches) a parameter value.
Definition: Parameter.cpp:42
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: Parameter.h:209
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:170
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: Parameter.h:222
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
virtual void load(Serializable::IArchive &ar)
Load from an archive.
Definition: Parameter.cpp:104
#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
void bcast< bool >(MPI::Intracomm &comm, bool &data, int root)
Explicit specialization of bcast for bool data.
Definition: MpiSendRecv.cpp:34