GCBM
growthmultipliermodule.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_CBM_CBMGROWTHMULTIPLIERMODULE_H_
2 #define MOJA_MODULES_CBM_CBMGROWTHMULTIPLIERMODULE_H_
3 
5 #include "moja/hash.h"
6 
7 #include <boost/format.hpp>
8 #include <boost/algorithm/string/join.hpp>
9 #include <boost/algorithm/string.hpp>
10 
11 #include <unordered_map>
12 
13 namespace moja {
14 namespace modules {
15 namespace cbm {
16 
18  public:
20  pos = multipliersByTimestep.begin();
21  }
22 
25  pos = multipliersByTimestep.begin();
26  }
27 
28  void add(int timestep, double multiplier) {
29  multipliersByTimestep[timestep] = multiplier;
30  }
31 
32  double next() {
33  return end() ? 1.0 : pos++->second;
34  }
35 
36  bool end() {
37  return pos == multipliersByTimestep.end();
38  }
39 
40  std::string toString() {
41  std::vector<std::string> multipliers;
42  for (auto& growthMultiplier : multipliersByTimestep) {
43  multipliers.push_back((
44  boost::format("%1%: %2%") % growthMultiplier.first % growthMultiplier.second
45  ).str());
46  }
47 
48  return boost::algorithm::join(multipliers, ", ");
49  }
50 
51  private:
52  std::map<int, double> multipliersByTimestep;
53  std::map<int, double>::iterator pos;
54  };
55 
57  public:
58  void add(std::string forestType, int timestep, double multiplier) {
59  auto it = seriesByForestType.find(forestType);
60  if (it != seriesByForestType.end()) {
61  it->second.add(timestep, multiplier);
62  } else {
64  seriesByForestType[forestType].add(timestep, multiplier);
65  }
66  }
67 
68  std::unordered_map<std::string, double> next() {
69  std::unordered_map<std::string, double> currentMultipliers;
70  for (auto& series : seriesByForestType) {
71  currentMultipliers[series.first] = series.second.next();
72  }
73 
74  return currentMultipliers;
75  }
76 
77  bool end() {
78  for (auto& series : seriesByForestType) {
79  if (!series.second.end()) {
80  // End of the set when the last forest type has run out of multipliers.
81  return false;
82  }
83  }
84 
85  return true;
86  }
87 
88  std::string toString() {
89  std::vector<std::string> multiplierComponents;
90  for (auto& series : seriesByForestType) {
91  multiplierComponents.push_back((
92  boost::format("[%1%: %2%]") % series.first % series.second.toString()
93  ).str());
94  }
95 
96  return boost::algorithm::join(multiplierComponents, ", ");
97  }
98 
99  private:
100  std::unordered_map<std::string, GrowthMultiplierSeries> seriesByForestType;
101  };
102 
104  public:
106  virtual ~GrowthMultiplierModule() = default;
107 
108  void configure(const DynamicObject& config) override;
109  void subscribe(NotificationCenter& notificationCenter) override;
110 
111  flint::ModuleTypes moduleType() { return flint::ModuleTypes::DisturbanceEvent; };
112 
113  virtual void doDisturbanceEvent(DynamicVar) override;
114  virtual void doLocalDomainInit() override;
115  virtual void doTimingInit() override;
116  virtual void doTimingStep() override;
117  virtual void doTimingShutdown() override;
118 
119  private:
120  void advanceMultipliers();
121  void clearMultipliers();
122 
124  bool _debuggingEnabled = false;
125  flint::IVariable* _currentGrowthMultipliers;
126  std::unordered_map<std::string, GrowthMultiplierSet> _growthMultiplierSets;
128  };
129 
130 }}} // namespace moja::modules::cbm
131 #endif // MOJA_MODULES_CBM_CBMGROWTHMULTIPLIERMODULE_H_
moja::modules::cbm::GrowthMultiplierModule::advanceMultipliers
void advanceMultipliers()
Definition: growthmultipliermodule.cpp:153
moja::modules::cbm::GrowthMultiplierSeries::toString
std::string toString()
Definition: growthmultipliermodule.h:40
moja::modules::cbm::GrowthMultiplierModule::_debuggingEnabled
bool _debuggingEnabled
Definition: growthmultipliermodule.h:124
moja::modules::cbm::GrowthMultiplierSeries::next
double next()
Definition: growthmultipliermodule.h:32
moja::modules::cbm
Definition: ageclasshelper.cpp:12
moja::modules::cbm::GrowthMultiplierModule
Definition: growthmultipliermodule.h:103
moja::modules::cbm::CBMModuleBase
Definition: cbmmodulebase.h:22
moja::modules::cbm::GrowthMultiplierSeries::multipliersByTimestep
std::map< int, double > multipliersByTimestep
Definition: growthmultipliermodule.h:52
moja::modules::cbm::GrowthMultiplierModule::subscribe
void subscribe(NotificationCenter &notificationCenter) override
Definition: growthmultipliermodule.cpp:39
moja::modules::cbm::GrowthMultiplierSet::add
void add(std::string forestType, int timestep, double multiplier)
Definition: growthmultipliermodule.h:58
moja::modules::cbm::GrowthMultiplierSet
Definition: growthmultipliermodule.h:56
moja::modules::cbm::GrowthMultiplierModule::moduleType
flint::ModuleTypes moduleType()
Definition: growthmultipliermodule.h:111
moja::modules::cbm::GrowthMultiplierSeries::pos
std::map< int, double >::iterator pos
Definition: growthmultipliermodule.h:53
moja::modules::cbm::GrowthMultiplierModule::GrowthMultiplierModule
GrowthMultiplierModule()
Definition: growthmultipliermodule.h:105
moja::modules::cbm::GrowthMultiplierModule::doLocalDomainInit
virtual void doLocalDomainInit() override
Definition: growthmultipliermodule.cpp:57
moja::modules::cbm::GrowthMultiplierModule::doTimingStep
virtual void doTimingStep() override
Definition: growthmultipliermodule.cpp:136
moja::modules::cbm::GrowthMultiplierSeries::GrowthMultiplierSeries
GrowthMultiplierSeries()
Definition: growthmultipliermodule.h:19
moja::modules::cbm::GrowthMultiplierModule::configure
void configure(const DynamicObject &config) override
Definition: growthmultipliermodule.cpp:27
moja::modules::cbm::GrowthMultiplierModule::clearMultipliers
void clearMultipliers()
Definition: growthmultipliermodule.cpp:121
moja::modules::cbm::GrowthMultiplierSeries::add
void add(int timestep, double multiplier)
Definition: growthmultipliermodule.h:28
moja::modules::cbm::GrowthMultiplierModule::_currentGrowthMultipliers
flint::IVariable * _currentGrowthMultipliers
Definition: growthmultipliermodule.h:125
moja::modules::cbm::GrowthMultiplierModule::doDisturbanceEvent
virtual void doDisturbanceEvent(DynamicVar) override
Definition: growthmultipliermodule.cpp:173
moja::modules::cbm::GrowthMultiplierModule::_moduleEnabled
bool _moduleEnabled
Definition: growthmultipliermodule.h:123
moja::modules::cbm::GrowthMultiplierSet::toString
std::string toString()
Definition: growthmultipliermodule.h:88
moja::modules::cbm::GrowthMultiplierModule::~GrowthMultiplierModule
virtual ~GrowthMultiplierModule()=default
moja::modules::cbm::GrowthMultiplierModule::doTimingShutdown
virtual void doTimingShutdown() override
Definition: growthmultipliermodule.cpp:111
moja::modules::cbm::GrowthMultiplierSeries::GrowthMultiplierSeries
GrowthMultiplierSeries(const GrowthMultiplierSeries &rhs)
Definition: growthmultipliermodule.h:23
moja::modules::cbm::GrowthMultiplierModule::_activeMultiplierSet
GrowthMultiplierSet _activeMultiplierSet
Definition: growthmultipliermodule.h:127
moja::modules::cbm::GrowthMultiplierSeries::end
bool end()
Definition: growthmultipliermodule.h:36
moja::modules::cbm::GrowthMultiplierSet::end
bool end()
Definition: growthmultipliermodule.h:77
moja::modules::cbm::GrowthMultiplierSet::next
std::unordered_map< std::string, double > next()
Definition: growthmultipliermodule.h:68
moja::modules::cbm::GrowthMultiplierSet::seriesByForestType
std::unordered_map< std::string, GrowthMultiplierSeries > seriesByForestType
Definition: growthmultipliermodule.h:100
moja::modules::cbm::GrowthMultiplierSeries
Definition: growthmultipliermodule.h:17
moja
Definition: ageclasshelper.cpp:10
cbmmodulebase.h
moja::modules::cbm::GrowthMultiplierModule::doTimingInit
virtual void doTimingInit() override
Definition: growthmultipliermodule.cpp:102
moja::modules::cbm::GrowthMultiplierModule::_growthMultiplierSets
std::unordered_map< std::string, GrowthMultiplierSet > _growthMultiplierSets
Definition: growthmultipliermodule.h:126