GCBM
sawtoothmodule.h
Go to the documentation of this file.
1 #ifndef MOJA_MODULES_SAWTOOTH_SAWTOOTHMODULE_H_
2 #define MOJA_MODULES_SAWTOOTH_SAWTOOTHMODULE_H_
3 #include "moja/modules/sawtooth/_modules.sawtooth_exports.h"
6 #include "sawtooth/exports.h"
7 #include <moja/timeseries.h>
8 #include <unordered_map>
9 #include <unordered_set>
10 #include <string>
11 #include <random>
12 namespace moja {
13 namespace modules {
14 namespace cbm {
15 
17 
18  double tmean_ann;
19  double tmin_ann;
20  double tmean_gs;
21  double etp_gs;
22  double ws_gs;
23  double etp_gs_z;
24  double ws_gs_z;
25  double etp_gs_n;
26  double ws_gs_n;
27  double ca;
28  double ndep;
29  };
30 
31  struct Site_data {
32  int ID_Spc1;
33  int ID_Spc2;
34  int ID_Spc3;
35  int ID_Spc4;
36  double Frac_Spc1;
37  double Frac_Spc2;
38  double Frac_Spc3;
39  double Frac_Spc4;
40  int Slope;
41  int Aspect;
42  int TWI;
43  };
44 
45  template <class TMat, class TElem>
47  private:
48  std::shared_ptr<TMat> Mat;
49  std::shared_ptr<TElem> Values;
50 
51 
52  public:
54  SawtoothMatrixWrapper(size_t nrow, size_t ncol, TElem defaultValue = (TElem)0) {
55  Mat = std::make_shared<TMat>();
56  Values = std::shared_ptr<TElem>(new TElem[nrow*ncol]);
57  std::fill_n(Values.get(), nrow*ncol, defaultValue);
58  Mat->rows = nrow;
59  Mat->cols = ncol;
60  }
61 
62  TMat* Get() {
63  Mat->values = Values.get();
64  return Mat.get();
65  }
66 
67  double GetValue(size_t row, size_t col) {
68  return Get()->GetValue(row, col);
69  }
70 
71  void SetValue(size_t row, size_t col, TElem value) {
72  Get()->SetValue(row, col, value);
73  }
74  };
75 
76  class SAWTOOTH_API SawtoothModule : public CBMModuleBase {
77  public:
79  virtual ~SawtoothModule() = default;
80 
81  void configure(const DynamicObject& config) override;
82  void subscribe(NotificationCenter& notificationCenter) override;
83 
84  void doLocalDomainInit() override;
85  void doTimingInit() override;
86  void doTimingStep() override;
87  void doTimingShutdown() override;
88  void doSystemShutdown() override;
89  void onDisturbanceEvent(DynamicVar e) override;
90 
91  private:
92  void GetSiteData(Site_data& site);
93 
94  Environment_data GetEnvironmentData(int year);
95  void LoadEnvironmentData();
97  std::vector<Environment_data> environmentData;
98 
99 
101  std::default_random_engine generator;
102 
103  Sawtooth_Error sawtooth_error;
104  Sawtooth_Spatial_Variable spatialVar;
105  //SawtoothPlotVariables sawtoothVariables;
106 
107  Sawtooth_CBM_Variable cbmVariables;
108 
109  Sawtooth_StandLevelResult standLevelResult;
110  Sawtooth_CBMResult cbmResult;
111  std::shared_ptr<Sawtooth_CBMAnnualProcesses> annualProcess;
113  void Step(long plot_id, int year, int disturbance_type_id);
114 
115  flint::IVariable* PlotId;
116  int RCP_Id;
117  int GCM_Id;
118  std::unordered_set<const moja::flint::IPool*> bioPools;
119 
121 
139 
152 
157 
158  const flint::IPool* _softwoodMerch;
159  const flint::IPool* _softwoodOther;
160  const flint::IPool* _softwoodFoliage;
161  const flint::IPool* _softwoodCoarseRoots;
162  const flint::IPool* _softwoodFineRoots;
163 
164  const flint::IPool* _hardwoodMerch;
165  const flint::IPool* _hardwoodOther;
166  const flint::IPool* _hardwoodFoliage;
167  const flint::IPool* _hardwoodCoarseRoots;
168  const flint::IPool* _hardwoodFineRoots;
169 
170  const flint::IPool* _aboveGroundVeryFastSoil;
171  const flint::IPool* _aboveGroundFastSoil;
172  const flint::IPool* _belowGroundVeryFastSoil;
173  const flint::IPool* _belowGroundFastSoil;
174  const flint::IPool* _softwoodStemSnag;
175  const flint::IPool* _softwoodBranchSnag;
176  const flint::IPool* _hardwoodStemSnag;
177  const flint::IPool* _hardwoodBranchSnag;
178  const flint::IPool* _mediumSoil;
179  const flint::IPool* _atmosphere;
180 
181  flint::IVariable* _age;
182  flint::IVariable* _turnoverRates;
183  flint::IVariable* _regenDelay;
184 
185  flint::IVariable* _isForest;
186 
187  flint::IVariable* _standSPUID;
188 
201 
202  bool shouldRun() const;
203  Sawtooth_ModelMeta InitializeModelMeta(const DynamicObject& config);
204  void AllocateSpecies(int* species, size_t max_density, const Site_data& site_data);
205  void SawtoothModule::adjustPartialMatrix(DynamicVar e,
206  const Sawtooth_CBMBiomassPools& disturbanceLosses);
207  };
208 }}}
209 #endif
moja::modules::cbm::SawtoothModule::_hardwoodStemSnag
const flint::IPool * _hardwoodStemSnag
Definition: sawtoothmodule.h:176
moja::modules::cbm::SawtoothMatrixWrapper::Values
std::shared_ptr< TElem > Values
Definition: sawtoothmodule.h:49
moja::modules::cbm::SawtoothModule::environmentData
std::vector< Environment_data > environmentData
Definition: sawtoothmodule.h:97
moja::modules::cbm::SawtoothModule::speciesList
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > speciesList
Definition: sawtoothmodule.h:120
moja::modules::cbm::SawtoothModule::RegionId_mat
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > RegionId_mat
Definition: sawtoothmodule.h:156
moja::modules::cbm::SawtoothModule::ws_gs_z_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > ws_gs_z_mat
Definition: sawtoothmodule.h:131
moja::modules::cbm::SawtoothModule::_atmosphere
const flint::IPool * _atmosphere
Definition: sawtoothmodule.h:179
moja::modules::cbm::SawtoothModule::tmean_gs_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > tmean_gs_mat
Definition: sawtoothmodule.h:124
moja::modules::cbm::SawtoothModule::StumpParmeterId_mat
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > StumpParmeterId_mat
Definition: sawtoothmodule.h:153
moja::modules::cbm::SawtoothModule::_otherToBranchSnagSplit
double _otherToBranchSnagSplit
Definition: sawtoothmodule.h:194
moja::modules::cbm::SawtoothModule::_softwoodFoliageFallRate
double _softwoodFoliageFallRate
Definition: sawtoothmodule.h:189
moja::modules::cbm::SawtoothModule::_softwoodOther
const flint::IPool * _softwoodOther
Definition: sawtoothmodule.h:159
moja::modules::cbm::SawtoothModule::TotalBiomassCarbon_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > TotalBiomassCarbon_mat
Definition: sawtoothmodule.h:143
moja::modules::cbm
Definition: ageclasshelper.cpp:12
moja::modules::cbm::SawtoothModule::_standSPUID
flint::IVariable * _standSPUID
Definition: sawtoothmodule.h:187
moja::modules::cbm::Environment_data::ca
double ca
Definition: sawtoothmodule.h:27
moja::modules::cbm::SawtoothModule::_hardwoodMerch
const flint::IPool * _hardwoodMerch
Definition: sawtoothmodule.h:164
moja::modules::cbm::SawtoothModule::MeanBiomassCarbon_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > MeanBiomassCarbon_mat
Definition: sawtoothmodule.h:145
moja::modules::cbm::SawtoothModule::_coarseRootTurnProp
double _coarseRootTurnProp
Definition: sawtoothmodule.h:198
moja::modules::cbm::SawtoothModule::spatialVar
Sawtooth_Spatial_Variable spatialVar
Definition: sawtoothmodule.h:104
moja::modules::cbm::SawtoothModule::WasDisturbed
bool WasDisturbed
Definition: sawtoothmodule.h:112
moja::modules::cbm::SawtoothModule::_softwoodFineRoots
const flint::IPool * _softwoodFineRoots
Definition: sawtoothmodule.h:162
moja::modules::cbm::SawtoothModule::_stemSnagTurnoverRate
double _stemSnagTurnoverRate
Definition: sawtoothmodule.h:195
moja::modules::cbm::SawtoothModule::_fineRootTurnProp
double _fineRootTurnProp
Definition: sawtoothmodule.h:200
moja::modules::cbm::SawtoothModule::_hardwoodBranchSnag
const flint::IPool * _hardwoodBranchSnag
Definition: sawtoothmodule.h:177
moja::modules::cbm::SawtoothModule::_softwoodBranchTurnOverRate
double _softwoodBranchTurnOverRate
Definition: sawtoothmodule.h:192
moja::modules::cbm::SawtoothModule::TurnoverParameterId_mat
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > TurnoverParameterId_mat
Definition: sawtoothmodule.h:155
moja::modules::cbm::SawtoothModule::DisturbanceMortalityRate_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > DisturbanceMortalityRate_mat
Definition: sawtoothmodule.h:150
moja::modules::cbm::SawtoothModule::_age
flint::IVariable * _age
Definition: sawtoothmodule.h:181
moja::modules::cbm::SawtoothModule::vpd_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > vpd_mat
Definition: sawtoothmodule.h:125
moja::modules::cbm::CBMModuleBase
Definition: cbmmodulebase.h:22
moja::modules::cbm::SawtoothModule::_fineRootAGSplit
double _fineRootAGSplit
Definition: sawtoothmodule.h:199
moja::modules::cbm::SawtoothModule::environmentDataBaseYear
int environmentDataBaseYear
Definition: sawtoothmodule.h:96
moja::modules::cbm::SawtoothModule::eeq_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > eeq_mat
Definition: sawtoothmodule.h:127
moja::modules::cbm::Site_data::Frac_Spc1
double Frac_Spc1
Definition: sawtoothmodule.h:36
moja::modules::cbm::SawtoothModule::GCM_Id
int GCM_Id
Definition: sawtoothmodule.h:117
moja::modules::cbm::SawtoothModule::generator
std::default_random_engine generator
Definition: sawtoothmodule.h:101
moja::modules::cbm::SawtoothModule::aspect_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > aspect_mat
Definition: sawtoothmodule.h:138
moja::modules::cbm::Site_data
Definition: sawtoothmodule.h:31
moja::modules::cbm::SawtoothModule::ca_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > ca_mat
Definition: sawtoothmodule.h:129
moja::modules::cbm::SawtoothModule::disturbance_mat
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > disturbance_mat
Definition: sawtoothmodule.h:135
moja::modules::cbm::Environment_data::etp_gs
double etp_gs
Definition: sawtoothmodule.h:21
moja::modules::cbm::SawtoothModule::_regenDelay
flint::IVariable * _regenDelay
Definition: sawtoothmodule.h:183
moja::modules::cbm::SawtoothModule::_softwoodFoliage
const flint::IPool * _softwoodFoliage
Definition: sawtoothmodule.h:160
moja::modules::cbm::SawtoothModule::StandDensity_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > StandDensity_mat
Definition: sawtoothmodule.h:142
moja::modules::cbm::Environment_data
Definition: sawtoothmodule.h:16
moja::modules::cbm::Environment_data::tmean_ann
double tmean_ann
Definition: sawtoothmodule.h:18
moja::modules::cbm::SawtoothMatrixWrapper::Mat
std::shared_ptr< TMat > Mat
Definition: sawtoothmodule.h:48
moja::modules::cbm::SawtoothModule::_aboveGroundVeryFastSoil
const flint::IPool * _aboveGroundVeryFastSoil
Definition: sawtoothmodule.h:170
moja::modules::cbm::Environment_data::etp_gs_z
double etp_gs_z
Definition: sawtoothmodule.h:23
moja::modules::cbm::Site_data::Frac_Spc2
double Frac_Spc2
Definition: sawtoothmodule.h:37
moja::modules::cbm::SawtoothModule::DisturbanceMortalityCarbon_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > DisturbanceMortalityCarbon_mat
Definition: sawtoothmodule.h:151
moja::modules::cbm::SawtoothModule::ndep_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > ndep_mat
Definition: sawtoothmodule.h:130
moja::modules::cbm::SawtoothModule::twi_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > twi_mat
Definition: sawtoothmodule.h:137
moja::modules::cbm::Environment_data::tmean_gs
double tmean_gs
Definition: sawtoothmodule.h:20
moja::modules::cbm::SawtoothModule::annualProcess
std::shared_ptr< Sawtooth_CBMAnnualProcesses > annualProcess
Definition: sawtoothmodule.h:111
moja::modules::cbm::Environment_data::ws_gs_n
double ws_gs_n
Definition: sawtoothmodule.h:26
moja::modules::cbm::SawtoothModule::tmean_ann_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > tmean_ann_mat
Definition: sawtoothmodule.h:122
moja::modules::cbm::Site_data::Aspect
int Aspect
Definition: sawtoothmodule.h:41
moja::modules::cbm::Site_data::ID_Spc4
int ID_Spc4
Definition: sawtoothmodule.h:35
moja::modules::cbm::SawtoothModule::cbmResult
Sawtooth_CBMResult cbmResult
Definition: sawtoothmodule.h:110
moja::modules::cbm::SawtoothModule::_hardwoodFineRoots
const flint::IPool * _hardwoodFineRoots
Definition: sawtoothmodule.h:168
moja::modules::cbm::SawtoothModule::ws_gs_n_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > ws_gs_n_mat
Definition: sawtoothmodule.h:132
moja::modules::cbm::SawtoothModule
Definition: sawtoothmodule.h:76
moja::modules::cbm::SawtoothMatrixWrapper::SetValue
void SetValue(size_t row, size_t col, TElem value)
Definition: sawtoothmodule.h:71
moja::modules::cbm::Site_data::TWI
int TWI
Definition: sawtoothmodule.h:42
moja::modules::cbm::SawtoothModule::PlotId
flint::IVariable * PlotId
Definition: sawtoothmodule.h:115
moja::modules::cbm::SawtoothModule::slope_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > slope_mat
Definition: sawtoothmodule.h:136
moja::modules::cbm::Site_data::Frac_Spc4
double Frac_Spc4
Definition: sawtoothmodule.h:39
moja::modules::cbm::SawtoothModule::_branchSnagTurnoverRate
double _branchSnagTurnoverRate
Definition: sawtoothmodule.h:196
moja::modules::cbm::SawtoothModule::TotalBiomassCarbonGrowth_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > TotalBiomassCarbonGrowth_mat
Definition: sawtoothmodule.h:144
moja::modules::cbm::SawtoothModule::RootParameterId_mat
SawtoothMatrixWrapper< Sawtooth_Matrix_Int, int > RootParameterId_mat
Definition: sawtoothmodule.h:154
moja::modules::cbm::SawtoothModule::_mediumSoil
const flint::IPool * _mediumSoil
Definition: sawtoothmodule.h:178
moja::modules::cbm::SawtoothModule::bioPools
std::unordered_set< const moja::flint::IPool * > bioPools
Definition: sawtoothmodule.h:118
moja::modules::cbm::SawtoothModule::_hardwoodCoarseRoots
const flint::IPool * _hardwoodCoarseRoots
Definition: sawtoothmodule.h:167
moja::modules::cbm::SawtoothModule::ws_gs_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > ws_gs_mat
Definition: sawtoothmodule.h:128
moja::modules::cbm::SawtoothModule::MortalityCarbon_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > MortalityCarbon_mat
Definition: sawtoothmodule.h:148
moja::modules::cbm::Environment_data::ws_gs
double ws_gs
Definition: sawtoothmodule.h:22
moja::modules::cbm::SawtoothModule::_softwoodStemSnag
const flint::IPool * _softwoodStemSnag
Definition: sawtoothmodule.h:174
moja::modules::cbm::SawtoothModule::standLevelResult
Sawtooth_StandLevelResult standLevelResult
Definition: sawtoothmodule.h:109
moja::modules::cbm::SawtoothModule::_hardwoodOther
const flint::IPool * _hardwoodOther
Definition: sawtoothmodule.h:165
moja::modules::cbm::SawtoothModule::_hardwoodBranchTurnOverRate
double _hardwoodBranchTurnOverRate
Definition: sawtoothmodule.h:193
moja::modules::cbm::SawtoothModule::_softwoodCoarseRoots
const flint::IPool * _softwoodCoarseRoots
Definition: sawtoothmodule.h:161
moja::modules::cbm::SawtoothModule::_turnoverRates
flint::IVariable * _turnoverRates
Definition: sawtoothmodule.h:182
moja::modules::cbm::Environment_data::etp_gs_n
double etp_gs_n
Definition: sawtoothmodule.h:25
moja::modules::cbm::SawtoothMatrixWrapper
Definition: sawtoothmodule.h:46
moja::modules::cbm::SawtoothModule::Sawtooth_Max_Density
size_t Sawtooth_Max_Density
Definition: sawtoothmodule.h:100
moja::modules::cbm::SawtoothModule::tmin_ann_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > tmin_ann_mat
Definition: sawtoothmodule.h:123
moja::modules::cbm::SawtoothModule::MortalityRate_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > MortalityRate_mat
Definition: sawtoothmodule.h:147
moja::modules::cbm::Environment_data::ndep
double ndep
Definition: sawtoothmodule.h:28
moja::modules::cbm::SawtoothModule::DisturbanceType_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > DisturbanceType_mat
Definition: sawtoothmodule.h:149
moja::modules::cbm::SawtoothModule::SawtoothModule
SawtoothModule()
Definition: sawtoothmodule.h:78
moja::modules::cbm::SawtoothMatrixWrapper::Get
TMat * Get()
Definition: sawtoothmodule.h:62
moja::modules::cbm::SawtoothModule::_softwoodBranchSnag
const flint::IPool * _softwoodBranchSnag
Definition: sawtoothmodule.h:175
moja::modules::cbm::Site_data::ID_Spc3
int ID_Spc3
Definition: sawtoothmodule.h:34
moja::modules::cbm::SawtoothModule::etp_gs_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > etp_gs_mat
Definition: sawtoothmodule.h:126
moja::modules::cbm::SawtoothModule::_hardwoodFoliage
const flint::IPool * _hardwoodFoliage
Definition: sawtoothmodule.h:166
moja::modules::cbm::Site_data::ID_Spc2
int ID_Spc2
Definition: sawtoothmodule.h:33
moja::modules::cbm::SawtoothModule::sawtooth_error
Sawtooth_Error sawtooth_error
Definition: sawtoothmodule.h:103
moja::modules::cbm::SawtoothModule::RCP_Id
int RCP_Id
Definition: sawtoothmodule.h:116
moja::modules::cbm::SawtoothModule::_softwoodMerch
const flint::IPool * _softwoodMerch
Definition: sawtoothmodule.h:158
moja::modules::cbm::SawtoothModule::MeanHeight_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > MeanHeight_mat
Definition: sawtoothmodule.h:141
moja::modules::cbm::SawtoothModule::cbmVariables
Sawtooth_CBM_Variable cbmVariables
Definition: sawtoothmodule.h:107
moja::modules::cbm::SawtoothModule::_stemAnnualTurnOverRate
double _stemAnnualTurnOverRate
Definition: sawtoothmodule.h:191
moja::modules::cbm::Site_data::Slope
int Slope
Definition: sawtoothmodule.h:40
moja::modules::cbm::SawtoothModule::RecruitmentRate_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > RecruitmentRate_mat
Definition: sawtoothmodule.h:146
moja::modules::cbm::Site_data::Frac_Spc3
double Frac_Spc3
Definition: sawtoothmodule.h:38
moja::modules::cbm::SawtoothModule::_belowGroundVeryFastSoil
const flint::IPool * _belowGroundVeryFastSoil
Definition: sawtoothmodule.h:172
moja::modules::cbm::SawtoothModule::_aboveGroundFastSoil
const flint::IPool * _aboveGroundFastSoil
Definition: sawtoothmodule.h:171
moja::modules::cbm::SawtoothMatrixWrapper::SawtoothMatrixWrapper
SawtoothMatrixWrapper()
Definition: sawtoothmodule.h:53
moja::modules::cbm::SawtoothMatrixWrapper::SawtoothMatrixWrapper
SawtoothMatrixWrapper(size_t nrow, size_t ncol, TElem defaultValue=(TElem) 0)
Definition: sawtoothmodule.h:54
moja
Definition: ageclasshelper.cpp:10
moja::modules::cbm::SawtoothModule::_coarseRootSplit
double _coarseRootSplit
Definition: sawtoothmodule.h:197
moja::modules::cbm::Environment_data::ws_gs_z
double ws_gs_z
Definition: sawtoothmodule.h:24
moja::modules::cbm::SawtoothMatrixWrapper::GetValue
double GetValue(size_t row, size_t col)
Definition: sawtoothmodule.h:67
moja::modules::cbm::SawtoothModule::MeanAge_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > MeanAge_mat
Definition: sawtoothmodule.h:140
cbmmodulebase.h
moja::modules::cbm::SawtoothModule::etp_gs_z_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > etp_gs_z_mat
Definition: sawtoothmodule.h:133
moja::modules::cbm::SawtoothModule::_belowGroundFastSoil
const flint::IPool * _belowGroundFastSoil
Definition: sawtoothmodule.h:173
moja::modules::cbm::SawtoothModule::etp_gs_n_mat
SawtoothMatrixWrapper< Sawtooth_Matrix, double > etp_gs_n_mat
Definition: sawtoothmodule.h:134
rootbiomassequation.h
moja::modules::cbm::Site_data::ID_Spc1
int ID_Spc1
Definition: sawtoothmodule.h:32
moja::modules::cbm::SawtoothModule::_hardwoodFoliageFallRate
double _hardwoodFoliageFallRate
Definition: sawtoothmodule.h:190
moja::modules::cbm::SawtoothModule::_isForest
flint::IVariable * _isForest
Definition: sawtoothmodule.h:185
moja::modules::cbm::Environment_data::tmin_ann
double tmin_ann
Definition: sawtoothmodule.h:19