Simpatico  v1.10
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
11 #include <util/mpi/MpiSendRecv.h>
12 #endif
13 
14 #include <iomanip>
15 
16 namespace 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 a parameter.
35  */
36  void Parameter::readParam(std::istream &in)
37  {
38  if (isIoProcessor()) {
39 
40  // Read the label and attempt to match.
41  in >> label_;
42 
43  // If this parameter is required and the
44  // label does not match, an exception will
45  // be thrown by the >> operator.
46 
47  if (Label::isMatched()) {
48 
49  // If the label string matches
50  readValue(in);
51  isActive_ = true;
52  if (ParamComponent::echo()) {
54  }
55 
56  } else {
57 
58  // If label does not match.
59  // Note: Label must be optional or an
60  // Exception should have been thrown.
61  assert(!isRequired());
62  isActive_ = false;
63  if (ParamComponent::echo()) {
64  Log::file() << indent()
65  << label_ << std::right
66  << std::setw(Parameter::Width)
67  << "[ absent ]" << std::endl;
68  }
69 
70  }
71  } else {
72  #ifdef UTIL_MPI
73  if (!hasIoCommunicator()) {
74  UTIL_THROW("Error: not isIoProcessor and not hasIoCommunicator");
75  }
76  #else
77  UTIL_THROW("Error: not isIoProcessor and no MPI");
78  #endif
79  }
80  #ifdef UTIL_MPI
81  if (hasIoCommunicator()) {
82  if (isRequired()) {
83  bcastValue();
84  isActive_ = true;
85  } else {
87  if (isActive_) {
88  bcastValue();
89  }
90  }
91  }
92  #endif
93  }
94 
95  /*
96  * Load from an archive.
97  */
99  {
100  if (isIoProcessor()) {
101  if (isRequired()) {
102  isActive_ = true;
103  } else {
104  ar >> isActive_;
105  }
106  if (isActive_) {
107  loadValue(ar);
108  if (ParamComponent::echo()) {
110  }
111  } else {
112  if (ParamComponent::echo() && !isRequired()) {
113  Log::file() << indent()
114  << label_ << std::right
115  << std::setw(Parameter::Width)
116  << "[ absent ]" << std::endl;
117  }
118  }
119  } else {
120  #ifdef UTIL_MPI
121  if (!hasIoCommunicator()) {
122  UTIL_THROW("Error: not isIoProcessor and !hasIoCommunicator");
123  }
124  #else
125  UTIL_THROW("Error: not isIoProcessor and no MPI");
126  #endif
127  }
128  #ifdef UTIL_MPI
129  if (hasIoCommunicator()) {
130  if (isRequired()) {
131  isActive_ = true;
132  } else {
134  }
135  if (isActive_) {
136  bcastValue();
137  }
138  }
139  #endif
140  }
141 
142  /*
143  * Save to an archive.
144  */
146  {
147  if (!isRequired()) {
148  ar << isActive_;
149  }
150  if (isActive_) {
151  saveValue(ar);
152  }
153  }
154 
155  /*
156  * Return label string.
157  */
158  std::string Parameter::label() const
159  { return label_.string(); }
160 
161  /*
162  * Is this a required parameter?
163  */
165  { return label_.isRequired(); }
166 
167  /*
168  * Is this an active parameter?
169  */
170  bool Parameter::isActive() const
171  { return isActive_; }
172 
173 }
static bool isMatched()
Did the most recent attempt to match a Label succeed?
Definition: Label.h:203
virtual void writeParam(std::ostream &out)=0
Read parameter(s) to file.
std::string label() const
Return label string.
Definition: Parameter.cpp:158
Label label_
Label object that contains parameter label string.
Definition: Parameter.h:185
virtual void saveValue(Serializable::OArchive &ar)
Save parameter value to an archive.
Definition: Parameter.h:209
bool isRequired() const
Is this the label for a required component?
Definition: Label.h:197
void bcast< bool >(MPI::Intracomm &comm, bool &data, int root)
Explicit specialization of bcast for bool data.
Definition: MpiSendRecv.cpp:34
Parameter(const char *label, bool isRequired=true)
Constructor.
Definition: Parameter.cpp:22
Saving / output archive for binary ostream.
virtual ~Parameter()
Destructor.
Definition: Parameter.cpp:30
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition: global.h:51
std::string string() const
Return label string.
Definition: Label.cpp:89
MPI::Intracomm & ioCommunicator() const
Get the MPI communicator by reference.
Definition: MpiFileIo.h:105
virtual void bcastValue()
Broadcast parameter value within the ioCommunicator.
Definition: Parameter.h:215
Utility classes for scientific computation.
Definition: accumulators.mod:1
static bool echo()
Get echo parameter.
virtual void readValue(std::istream &in)
Read parameter value from an input stream.
Definition: Parameter.h:195
virtual void save(Serializable::OArchive &ar)
Save to an archive.
Definition: Parameter.cpp:145
bool isIoProcessor() const
Can this processor do file I/O ?
Definition: MpiFileIo.h:92
bool isActive() const
Is this parameter active?
Definition: Parameter.cpp:170
virtual void loadValue(Serializable::IArchive &ar)
Load bare parameter value from an archive.
Definition: Parameter.h:202
static std::ostream & file()
Get log ostream by reference.
Definition: Log.cpp:57
bool hasIoCommunicator() const
Does this object have an associated MPI communicator?
Definition: MpiFileIo.h:99
Saving archive for binary istream.
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:98
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
bool isActive_
Is this parameter active (always true if isRequired).
Definition: Parameter.h:188
bool isRequired() const
Is this an optional parameter?
Definition: Parameter.cpp:164
virtual void readParam(std::istream &in)
Read a label and (if the label matches) a parameter value.
Definition: Parameter.cpp:36
std::string indent() const
Return indent string for this object (string of spaces).