Canoe
Comprehensive Atmosphere N' Ocean Engine
virtual_groups.hpp
Go to the documentation of this file.
1 #ifndef SRC_VIRTUAL_GROUPS_HPP_
2 #define SRC_VIRTUAL_GROUPS_HPP_
3 
4 // C/C++
5 #include <map>
6 #include <string>
7 #include <unordered_map>
8 
9 // external
10 #include <yaml-cpp/yaml.h> // YAML::Node
11 
12 // athena
13 #include <athena/athena.hpp> // Real
14 
15 // canoe
16 #include <configure.hpp>
17 #include <index_map.hpp>
18 
19 class Mesh;
20 class MeshBlock;
21 class OutputType;
22 class OutputParameters;
23 
24 class NamedGroup {
25  public:
26  explicit NamedGroup(std::string name) : myname_(name) {}
27  virtual ~NamedGroup() {}
28  std::string GetName() const { return myname_; }
29 
30  private:
31  std::string myname_;
32 };
33 
34 class FlagGroup {
35  public:
36  explicit FlagGroup(uint64_t flags = 0LL) : myflags_(flags) {}
37  virtual ~FlagGroup() {}
38 
39  int TestFlag(uint64_t flag) const { return myflags_ & flag; }
40  void SetFlag(uint64_t flag) { myflags_ |= flag; }
41 
42  private:
44  uint64_t myflags_;
45 };
46 
48  public:
49  virtual ~ParameterGroup() {}
50 
51  void SetRealsFrom(YAML::Node const &node) {
52  for (auto it = node.begin(); it != node.end(); ++it) {
53  params_real_[it->first.as<std::string>()] = it->second.as<Real>();
54  }
55  }
56 
58  void SetPar(std::string const &name, Real value) {
59  params_real_[name] = value;
60  }
61 
63  void SetPar(std::string const &name, int value) { params_int_[name] = value; }
64 
66  void SetPar(std::string const &name, std::string const &value) {
67  params_str_[name] = value;
68  }
69 
71  template <typename T>
72  T GetPar(std::string const &name) const;
73 
75  bool HasPar(std::string const &name) const {
76  if (params_real_.count(name) > 0) return true;
77  if (params_int_.count(name) > 0) return true;
78  if (params_str_.count(name) > 0) return true;
79  return false;
80  }
81 
82  private:
84  std::unordered_map<std::string, Real> params_real_;
85 
87  std::unordered_map<std::string, int> params_int_;
88 
90  std::unordered_map<std::string, std::string> params_str_;
91 };
92 
93 // specialization
94 template <>
95 inline int ParameterGroup::GetPar<int>(std::string const &name) const {
96  return params_int_.at(name);
97 }
98 
99 template <>
100 inline Real ParameterGroup::GetPar<Real>(std::string const &name) const {
101  return params_real_.at(name);
102 }
103 
104 template <>
105 inline std::string ParameterGroup::GetPar<std::string>(
106  std::string const &name) const {
107  return params_str_.at(name);
108 }
109 
111  public:
112  virtual ~SpeciesIndexGroup() {}
113 
115  void SetSpeciesIndex(std::vector<std::string> const &species_names) {
116  auto pindex = IndexMap::GetInstance();
117 
118  for (auto const &name : species_names) {
119  species_index_.push_back(pindex->GetSpeciesId(name));
120  cloud_index_.push_back(species_index_.back() - NHYDRO);
121  chemistry_index_.push_back(species_index_.back() - NHYDRO - NCLOUD);
122  }
123  }
124 
126  std::vector<int> const &GetSpeciesIndexArray() const {
127  return species_index_;
128  }
129 
131  int GetSpeciesIndex(int n) const { return species_index_[n]; }
132 
134  std::vector<int> const &GetCloudIndexArray() const { return cloud_index_; }
135 
137  int GetCloudIndex(int n) const { return cloud_index_[n]; }
138 
140  std::vector<int> const &GetChemistryIndexArray() const {
141  return chemistry_index_;
142  }
143 
145  int GetChemistryIndex(int n) const { return chemistry_index_[n]; }
146 
147  private:
149  std::vector<int> species_index_;
150 
152  std::vector<int> cloud_index_;
153 
155  std::vector<int> chemistry_index_;
156 };
157 
159  public:
160  virtual ~CounterGroup() {}
162  void DecrementCounter(Real dt) { current_ -= dt; }
163  Real GetCounter() const { return current_; }
164  void SetCooldownTime(Real cooldown) { cooldown_ = cooldown; }
165 
166  private:
167  Real cooldown_, current_ = 0.;
168 };
169 
170 class CheckGroup {
171  public:
172  virtual ~CheckGroup() {}
173 
175  virtual void CheckFail() const {}
176 
179  virtual bool CheckWarn() const { return true; }
180 };
181 
183  public:
184  virtual ~RestartGroup() {}
185  virtual size_t RestartDataSizeInBytes(Mesh const *pm) const = 0;
186  virtual void DumpRestartData(char *pdst) const = 0;
187  virtual size_t LoadRestartData(char *psrt) = 0;
188 };
189 
191  public:
192  virtual ~ASCIIOutputGroup() {}
193  virtual void WriteAsciiHeader(OutputParameters const *) const = 0;
194  virtual void WriteAsciiData(OutputParameters const *) const = 0;
195 };
196 
198  public:
199  virtual ~BinaryOutputGroup() {}
200  virtual void WriteBinaryHeader(std::ofstream &os) const = 0;
201  virtual void WriteBinaryData(std::ofstream &os) const = 0;
202 };
203 
205  public:
206  virtual ~MeshOutputGroup() {}
207  virtual bool ShouldMeshOutput(std::string variable_name) const = 0;
208  virtual void LoadMeshOutputData(OutputType *out, int *num_vars) const = 0;
209 };
210 
212  public:
213  virtual ~FITSOutputGroup() {}
214  virtual bool ShouldFITSOutput(std::string variable_name) const = 0;
215  virtual void LoadFITSOutputData(OutputType *out, int *num_vars) const = 0;
216 };
217 
218 #endif // SRC_VIRTUAL_GROUPS_HPP_
virtual ~ASCIIOutputGroup()
virtual void WriteAsciiHeader(OutputParameters const *) const =0
virtual void WriteAsciiData(OutputParameters const *) const =0
virtual void WriteBinaryData(std::ofstream &os) const =0
virtual void WriteBinaryHeader(std::ofstream &os) const =0
virtual ~BinaryOutputGroup()
virtual bool CheckWarn() const
virtual ~CheckGroup()
virtual void CheckFail() const
This function fails if the check fails.
virtual ~CounterGroup()
void SetCooldownTime(Real cooldown)
void DecrementCounter(Real dt)
Real GetCounter() const
virtual bool ShouldFITSOutput(std::string variable_name) const =0
virtual void LoadFITSOutputData(OutputType *out, int *num_vars) const =0
virtual ~FITSOutputGroup()
void SetFlag(uint64_t flag)
int TestFlag(uint64_t flag) const
FlagGroup(uint64_t flags=0LL)
uint64_t myflags_
internal flags
virtual ~FlagGroup()
static IndexMap const * GetInstance()
Definition: index_map.cpp:29
virtual void LoadMeshOutputData(OutputType *out, int *num_vars) const =0
virtual ~MeshOutputGroup()
virtual bool ShouldMeshOutput(std::string variable_name) const =0
std::string myname_
virtual ~NamedGroup()
NamedGroup(std::string name)
std::string GetName() const
T GetPar(std::string const &name) const
Get parameter.
virtual ~ParameterGroup()
void SetPar(std::string const &name, Real value)
Set real parameter.
void SetPar(std::string const &name, int value)
Set int parameter.
std::unordered_map< std::string, int > params_int_
int parameters
bool HasPar(std::string const &name) const
Check if a parameter exists.
std::unordered_map< std::string, std::string > params_str_
string parameters
void SetRealsFrom(YAML::Node const &node)
std::unordered_map< std::string, Real > params_real_
real parameters
void SetPar(std::string const &name, std::string const &value)
Set string parameter.
virtual ~RestartGroup()
virtual size_t RestartDataSizeInBytes(Mesh const *pm) const =0
virtual size_t LoadRestartData(char *psrt)=0
virtual void DumpRestartData(char *pdst) const =0
std::vector< int > cloud_index_
indices of clouds
int GetSpeciesIndex(int n) const
virtual ~SpeciesIndexGroup()
void SetSpeciesIndex(std::vector< std::string > const &species_names)
Set species index based on species names.
std::vector< int > const & GetCloudIndexArray() const
std::vector< int > chemistry_index_
indices of chemical species
int GetChemistryIndex(int n) const
int GetCloudIndex(int n) const
std::vector< int > const & GetSpeciesIndexArray() const
std::vector< int > species_index_
indices of species
std::vector< int > const & GetChemistryIndexArray() const