Simpatico  v1.10
Begin.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 "Begin.h"
9 #ifdef UTIL_MPI
10 #include <util/mpi/MpiSendRecv.h>
11 #endif
12 
13 #include <util/global.h>
14 
15 namespace Util
16 {
17 
18  /*
19  * Begin constructor
20  */
21  Begin::Begin(const char *label, bool isRequired)
22  : label_(isRequired),
23  isActive_(false)
24  {
25  std::string expected = label;
26  expected += "{";
27  label_.setString(expected);
28  }
29 
30  /*
31  * Read label, check against expected value
32  */
33  void Begin::readParam(std::istream &in)
34  {
35  if (isIoProcessor()) {
36  in >> label_;
37 
38  // If this parameter is required and the label string
39  // doesn't match, an Exception will be thrown by the
40  // operator >> for a Label, terminating this function.
41 
42  if (Label::isClear()) {
43  // If the label string matches
44  isActive_ = true;
45  if (ParamComponent::echo()) {
47  }
48  } else {
49  // If label does not match and this isOptional
50  isActive_ = false;
51  if (ParamComponent::echo()) {
52  Log::file() << indent()
53  << label_.string() << " [absent] }"
54  << std::endl;
55  }
56  }
57  } else {
58  #ifdef UTIL_MPI
59  if (!hasIoCommunicator()) {
60  UTIL_THROW("Error: not isIoProcessor and not hasIoCommunicator");
61  }
62  #else
63  UTIL_THROW("Error: not isIoProcessor and no MPI");
64  #endif
65  }
66  #ifdef UTIL_MPI
67  if (hasIoCommunicator()) {
68  if (isRequired()) {
69  isActive_ = true;
70  } else {
71  bcast<bool>(ioCommunicator(), isActive_, 0);
72  }
73  }
74  #endif
75  }
76 
77  /*
78  * Begin::writeParam() template
79  */
80  void Begin::writeParam(std::ostream &out)
81  { out << indent() << label_.string() << std::endl; }
82 
83  /*
84  * Do-nothing implementation of virtual resetIo function.
85  */
87  {}
88 
89 }
Begin(const char *label, bool isRequired=true)
Constructor.
Definition: Begin.cpp:21
File containing preprocessor macros for error handling.
void bcast< bool >(MPI::Intracomm &comm, bool &data, int root)
Explicit specialization of bcast for bool data.
Definition: MpiSendRecv.cpp:34
static bool isClear()
Is the input buffer clear?
Definition: Label.cpp:37
virtual void resetParam()
Do-nothing implementation of virtual resetParam function.
Definition: Begin.cpp:86
#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
Utility classes for scientific computation.
Definition: accumulators.mod:1
static bool echo()
Get echo parameter.
void setString(std::string string)
Set the label string.
Definition: Label.cpp:83
virtual void writeParam(std::ostream &out)
Write the opening line.
Definition: Begin.cpp:80
bool isIoProcessor() const
Can this processor do file I/O ?
Definition: MpiFileIo.h:92
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
bool isRequired() const
Is this the beginning line for a required element?
Definition: Begin.h:78
This file contains templates for global functions send<T>, recv<T> and bcast<T>.
virtual void readParam(std::istream &in)
Read the opening line.
Definition: Begin.cpp:33
std::string indent() const
Return indent string for this object (string of spaces).