GCBM
cbmspinupsequencer.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_CBM_CBMSPINUPSEQUENCER_H_
2 #define MOJA_MODULES_CBM_CBMSPINUPSEQUENCER_H_
3 
4 #include "moja/modules/cbm/_modules.cbm_exports.h"
5 #include "moja/datetime.h"
6 #include "moja/flint/itiming.h"
7 #include "moja/flint/sequencermodulebase.h"
8 #include "moja/notificationcenter.h"
9 #include "moja/hash.h"
10 #include "moja/pocojsonutils.h"
11 
12 #include <string>
13 #include <unordered_map>
14 
15 namespace moja {
16  namespace modules {
17  namespace cbm {
18 
19  class CBM_API CBMSpinupSequencer : public flint::SequencerModuleBase {
20  public:
21  CBMSpinupSequencer() : _standAge(0) {};
22  virtual ~CBMSpinupSequencer() {};
23 
24  const std::string returnInverval = "return_interval";
25  const std::string maxRotation = "max_rotations";
26  const std::string historicDistType = "historic_disturbance_type";
27  const std::string lastDistType = "last_pass_disturbance_type";
28  const std::string delay = "delay";
29  const std::string inventoryDelay = "inventory_delay";
30 
31  void configure(const DynamicObject& config) override {
32  if (config.contains("ramp_start_date")) {
33  _rampStartDate = moja::parseSimpleDate(
34  config["ramp_start_date"].extract<std::string>());
35  }
36  };
37 
38  void configure(flint::ITiming& timing) override {
39  timing.setStepLengthInYears(1);
40  };
41 
42  bool Run(NotificationCenter& _notificationCenter, flint::ILandUnitController& luc) override;
43 
44  private:
45  const flint::IPool* _aboveGroundSlowSoil;
46  const flint::IPool* _belowGroundSlowSoil;
47  const flint::IPool* _featherMossSlow;
48  const flint::IPool* _sphagnumMossSlow;
49 
50  const std::set<std::string> _biomassPools{
51  "SoftwoodMerch",
52  "SoftwoodFoliage",
53  "SoftwoodOther",
54  "SoftwoodCoarseRoots",
55  "SoftwoodFineRoots",
56  "HardwoodMerch",
57  "HardwoodFoliage",
58  "HardwoodOther",
59  "HardwoodCoarseRoots",
60  "HardwoodFineRoots"
61  };
62 
63  flint::IVariable* _age;
64  flint::IVariable* _shrubAge;
65  flint::IVariable* _smallTreeAge;
66  flint::IVariable* _delay;
67  flint::IVariable* _mat;
68  flint::IVariable* _spu;
69  flint::IVariable* _isDecaying;
70  flint::IVariable* _lastPassDisturbanceTimeseries = nullptr;
71  flint::IVariable* _spinupMossOnly;
72  flint::IVariable* _regenDelay;
73 
74  int _maxRotationValue{ 0 }; // maximum rotations to do the spinup, 30, each rotation is 125 years
75  int _minimumRotation{ 0 }; // minimum rotation to do the spinup, 3
76  int _ageReturnInterval{ 0 }; // age interval to fire a historic disturbance, 125 years
77  int _standAge{ 0 }; // stand age to grow after the last disturbance
78  int _standRegenDelay{ 0 }; // regen delay for the post-spinup period (i.e. imported into main simulation)
79  int _standDelay{ 0 }; // years to delay, during delay period, only turnover and decay processes
80  int _spinupGrowthCurveID{ -1 }; // spinup growth curve ID
81  std::string _historicDistType; // historic disturbance type happened at each age interval
82  std::string _lastPassDistType; // last disturance type happened when the slow pool is stable and minimum rotations are done
83  std::unordered_map<std::string, int> _disturbanceOrder;
84 
85  // Optional ramp to use at the end of the spinup period; used when, for example, spinup uses a
86  // value of 10 for a variable, and the rest of the simulation uses a value of 20, and the values
87  // need to blend smoothly together, so the user prepares a 10-value ramp which is used for the last
88  // 10 timesteps of the spinup period: 10, 11, 12, 13, ...
89  Poco::Nullable<DateTime> _rampStartDate;
90 
91  // SPU, historic disturbance type, GC ID, return interval, mean annual temperature
92  typedef std::tuple<int, std::string, int, int, double> CacheKey;
93  std::unordered_map<CacheKey, std::vector<double>, moja::Hash> _cache;
94 
95  // Get spinup parameters for this land unit
96  bool getSpinupParameters(flint::ILandUnitDataWrapper& landUnitData);
97 
98  // Run the standard spinup procedure for most stands.
99  void runRegularSpinup(NotificationCenter& notificationCenter, flint::ILandUnitController& luc, bool runMoss);
100 
101  // Run the alternate spinup procedure for peatland.
102  void runPeatlandSpinup(NotificationCenter& notificationCenter, flint::ILandUnitController& luc);
103 
104  // Check if the slow pool is stable
105  bool isSlowPoolStable(double lastSlowPoolValue, double currentSlowPoolValue);
106 
107  // Check if to run peatland module
108  bool isPeatlandApplicable();
109 
110  // Check if to run moss module
111  bool isMossApplicable(bool runPeatland);
112 
113  // Fire timing events
114  void fireSpinupSequenceEvent(NotificationCenter& notificationCenter,
115  flint::ILandUnitController& luc,
116  int maximumSteps,
117  bool incrementStep);
118 
119  // Fire historical and last disturbance
120  void fireHistoricalLastDisturbanceEvent(NotificationCenter& notificationCenter,
121  flint::ILandUnitController& luc,
122  std::string disturbanceName);
123  };
124 
125  }
126  }
127 } // namespace moja::Modules::CBM
128 
129 #endif // MOJA_MODULES_CBM_CBMSPINUPSEQUENCER_H_
moja::modules::cbm::CBMSpinupSequencer::configure
void configure(flint::ITiming &timing) override
Definition: cbmspinupsequencer.h:38
moja::modules::cbm::CBMSpinupSequencer::_aboveGroundSlowSoil
const flint::IPool * _aboveGroundSlowSoil
Definition: cbmspinupsequencer.h:45
moja::modules::cbm::CBMSpinupSequencer::_sphagnumMossSlow
const flint::IPool * _sphagnumMossSlow
Definition: cbmspinupsequencer.h:48
moja::modules::cbm::CBMSpinupSequencer::CBMSpinupSequencer
CBMSpinupSequencer()
Definition: cbmspinupsequencer.h:21
moja::modules::cbm::CBMSpinupSequencer::_regenDelay
flint::IVariable * _regenDelay
Definition: cbmspinupsequencer.h:72
moja::modules::cbm
Definition: ageclasshelper.cpp:12
moja::modules::cbm::CBMSpinupSequencer::_spu
flint::IVariable * _spu
Definition: cbmspinupsequencer.h:68
moja::modules::cbm::CBMSpinupSequencer::_spinupMossOnly
flint::IVariable * _spinupMossOnly
Definition: cbmspinupsequencer.h:71
moja::modules::cbm::CBMSpinupSequencer::_mat
flint::IVariable * _mat
Definition: cbmspinupsequencer.h:67
moja::modules::cbm::CBMSpinupSequencer::~CBMSpinupSequencer
virtual ~CBMSpinupSequencer()
Definition: cbmspinupsequencer.h:22
moja::modules::cbm::CBMSpinupSequencer::_featherMossSlow
const flint::IPool * _featherMossSlow
Definition: cbmspinupsequencer.h:47
moja::modules::cbm::CBMSpinupSequencer::_delay
flint::IVariable * _delay
Definition: cbmspinupsequencer.h:66
moja::modules::cbm::CBMSpinupSequencer::_shrubAge
flint::IVariable * _shrubAge
Definition: cbmspinupsequencer.h:64
moja::modules::cbm::CBMSpinupSequencer::_disturbanceOrder
std::unordered_map< std::string, int > _disturbanceOrder
Definition: cbmspinupsequencer.h:83
moja::modules::cbm::CBMSpinupSequencer::_historicDistType
std::string _historicDistType
Definition: cbmspinupsequencer.h:81
moja::modules::cbm::CBMSpinupSequencer::_age
flint::IVariable * _age
Definition: cbmspinupsequencer.h:63
moja::modules::cbm::CBMSpinupSequencer::_isDecaying
flint::IVariable * _isDecaying
Definition: cbmspinupsequencer.h:69
moja::modules::cbm::CBMSpinupSequencer::_lastPassDistType
std::string _lastPassDistType
Definition: cbmspinupsequencer.h:82
moja::modules::cbm::CBMSpinupSequencer::CacheKey
std::tuple< int, std::string, int, int, double > CacheKey
Definition: cbmspinupsequencer.h:92
moja::modules::cbm::CBMSpinupSequencer::_rampStartDate
Poco::Nullable< DateTime > _rampStartDate
Definition: cbmspinupsequencer.h:89
moja::modules::cbm::CBMSpinupSequencer::configure
void configure(const DynamicObject &config) override
Definition: cbmspinupsequencer.h:31
moja::modules::cbm::CBMSpinupSequencer
Definition: cbmspinupsequencer.h:19
moja
Definition: ageclasshelper.cpp:10
moja::modules::cbm::CBMSpinupSequencer::_cache
std::unordered_map< CacheKey, std::vector< double >, moja::Hash > _cache
Definition: cbmspinupsequencer.h:93
moja::modules::cbm::CBMSpinupSequencer::_belowGroundSlowSoil
const flint::IPool * _belowGroundSlowSoil
Definition: cbmspinupsequencer.h:46
moja::modules::cbm::CBMSpinupSequencer::_smallTreeAge
flint::IVariable * _smallTreeAge
Definition: cbmspinupsequencer.h:65