SimulationModule.h
Go to the documentation of this file.
1/*!
2 * \file SimulationModule.h
3 * \brief Parent class for all modules in SEIMS
4 *
5 * Changelog:
6 * - 1. 2010-07-31 - jz - Initial implementation.
7 * - 2. 2016-06-14 - lj - Add SetScenario etc. functions.
8 * - 3. 2018-03-03 - lj - Add CHECK_XXX series macros for data checking.
9 * - 4. 2020-09-18 - lj - Using Easyloggingpp
10 * - 5. 2021-10-29 - ss,lj - Add InitialIntermediates to initialize intermediate params.
11 *
12 * \author Junzhi Liu, Liangjun Zhu
13 */
14#ifndef SIMULATION_MOUDULE_BASE
15#define SIMULATION_MOUDULE_BASE
16
17#include "basic.h"
18#include "utils_time.h"
19#include "Scenario.h"
20#include "clsReach.h"
21#include "clsSubbasin.h"
22
23#include <string>
24#include <ctime>
25
26using namespace ccgl;
27using namespace utils_time;
28using namespace bmps;
29
30/*!
31 * \enum TimeStepType
32 * \ingroup module_setting
33 * \brief Time step types.
34 */
36 TIMESTEP_HILLSLOPE, ///< Hillslope scale
37 TIMESTEP_CHANNEL, ///< Channel scale
38 TIMESTEP_SIMULATION ///< Whole simulation scale
39};
40
41/*!
42 * \ingroup module_setting
43 * \class SimulationModule
44 * \brief Base module for all simulation modules in SEIMS
45 */
47public:
48 //! Constructor
50
51 //! Execute the simulation. Return 0 for success.
52 virtual int Execute() { return -1; }
53
54 //! Set date time, as well as the sequence number of the entire simulation. Added by LJ for statistics convenient.
55 virtual void SetDate(time_t t, int year_idx);
56
57 //! Set thread number for OpenMP
58 virtual void SetTheadNumber(const int thread_num) {
59 SetOpenMPThread(thread_num);
60 }
61
62 //! Set climate data type, P, M, PET etc.
63 virtual void SetClimateDataType(int data_type) {
64 }
65
66 //! Set data, DT_Single, integer
67 virtual void SetValue(const char* key, int value) {
68 throw ModelException("SimulationModule", "SetValue",
69 "Set function of parameter " + string(key) + " is not implemented.");
70 }
71
72 //! Set data, DT_Single, float point number (float or double)
73 virtual void SetValue(const char* key, FLTPT value) {
74 throw ModelException("SimulationModule", "SetValue",
75 "Set function of parameter " + string(key) + " is not implemented.");
76 }
77
78 //! Set single value to array1D by index, used in MPI version for passing values of subbasins, integer
79 virtual void SetValueByIndex(const char* key, int index, int value) {
80 throw ModelException("SimulationModule", "SetValueByIndex",
81 "Set function of parameter " + string(key) + " is not implemented.");
82 }
83
84 //! Set single value to array1D by index, used in MPI version for passing values of subbasins, float
85 virtual void SetValueByIndex(const char* key, int index, FLTPT value) {
86 throw ModelException("SimulationModule", "SetValueByIndex",
87 "Set function of parameter " + string(key) + " is not implemented.");
88 }
89
90 //! Set 1D data, by default, DT_Raster1D, integer
91 virtual void Set1DData(const char* key, int n, int* data) {
92 throw ModelException("SimulationModule", "Set1DData",
93 "Set function of parameter " + string(key) + " is not implemented.");
94 }
95
96 //! Set 1D data, by default, DT_Raster1D, float
97 virtual void Set1DData(const char* key, int n, FLTPT* data) {
98 throw ModelException("SimulationModule", "Set1DData",
99 "Set function of parameter " + string(key) + " is not implemented.");
100 }
101
102 //! Set 2D data, by default, DT_Raster2D, integer
103 virtual void Set2DData(const char* key, int nrows, int ncols, int** data) {
104 throw ModelException("SimulationModule", "Set2DData",
105 "Set function of parameter " + string(key) + " is not implemented.");
106 }
107
108 //! Set 2D data, by default, DT_Raster2D, float
109 virtual void Set2DData(const char* key, int nrows, int ncols, FLTPT** data) {
110 throw ModelException("SimulationModule", "Set2DData",
111 "Set function of parameter " + string(key) + " is not implemented.");
112 }
113
114 //! Get value, DT_Single, integer
115 virtual void GetValue(const char* key, int* value) {
116 throw ModelException("SimulationModule", "GetValue",
117 "Get function of parameter " + string(key) + " is not implemented.");
118 }
119
120 //! Get value, DT_Single, float
121 virtual void GetValue(const char* key, FLTPT* value) {
122 throw ModelException("SimulationModule", "GetValue",
123 "Get function of parameter " + string(key) + " is not implemented.");
124 }
125
126 //! Get 1D data, by default, DT_Raster1D, integer
127 virtual void Get1DData(const char* key, int* n, int** data) {
128 throw ModelException("SimulationModule", "Get1DData",
129 "Get function of parameter " + string(key) + " is not implemented.");
130 }
131
132 //! Get 1D data, by default, DT_Raster1D, float
133 virtual void Get1DData(const char* key, int* n, FLTPT** data) {
134 throw ModelException("SimulationModule", "Get1DData",
135 "Get function of parameter " + string(key) + " is not implemented.");
136 }
137
138 //! Get 2D data, by default, DT_Raster2D, integer
139 virtual void Get2DData(const char* key, int* nrows, int* ncols, int*** data) {
140 throw ModelException("SimulationModule", "Get2DData",
141 "Get function of parameter " + string(key) + " is not implemented.");
142 }
143
144 //! Get 2D data, by default, DT_Raster2D, float
145 virtual void Get2DData(const char* key, int* nrows, int* ncols, FLTPT*** data) {
146 throw ModelException("SimulationModule", "Get2DData",
147 "Get function of parameter " + string(key) + " is not implemented.");
148 }
149
150 //! Set pointer of Scenario class which contains all BMP information. Added by LJ, 2016-6-14
151 virtual void SetScenario(Scenario* sce) {
152 throw ModelException("SimulationModule", "SetScenario", "Set scenario function is not implemented.");
153 }
154
155 //! Set pointer of clsReaches class which contains all reaches information. Added by LJ, 2016-7-2
156 virtual void SetReaches(clsReaches* rches) {
157 throw ModelException("SimulationModule", "SetReaches", "Set reaches function is not implemented.");
158 }
159
160 //! Set pointer of clsSubbasins class which contains all subbasins information. Added by LJ, 2016-7-28
161 virtual void SetSubbasins(clsSubbasins* subbsns) {
162 throw ModelException("SimulationModule", "SetSubbasins", "Set subbasins function is not implemented.");
163 }
164
165 /*!
166 * \brief Check the input data. Make sure all the input data is available.
167 *
168 * This function is optional to be overridden.
169 *
170 * \return bool The validity of the input data.
171 */
172 virtual bool CheckInputData() { return true; }
173
174 /*!
175 * \brief Check data length of the first dimension (i.e., nRows) of the input array-based data
176 *
177 * \param[in] module_id Module ID used to print exception message
178 * \param[in] key the key to identify the requested data
179 * \param[in] nrows size of the first dimension
180 * \param[out] m_nrows the expected size, if m_nrows less or equal to 0, then m_nrows = mrows
181 */
182 virtual bool CheckInputSize(const char* module_id, const char* key, int nrows, int& m_nrows);
183
184 /*!
185 * \brief Check data length of the two dimensions (i.e., nRows and nCols) of the input array-based data
186 *
187 * \param[in] module_id Module ID used to print exception message
188 * \param[in] key the key to identify the requested data
189 * \param[in] nrows size of the first dimension
190 * \param[in] ncols size of the second dimension
191 * \param[out] m_nrows the expected rows size, if m_nrows less or equal to 0, then m_nrows = mrows
192 * \param[out] m_ncols the expected cols size, if m_ncols less or equal to 0, then m_ncols = ncols
193 */
194 virtual bool CheckInputSize2D(const char* module_id, const char* key, int nrows, int ncols, int& m_nrows, int& m_ncols);
195
196 /*!
197 * \brief Initialize output variables.
198 *
199 * This function is optional to be overridden.
200 * Only allocate memory address and initialize outputs.
201 */
202 virtual void InitialOutputs() {}
203
204 /*!
205 * \brief Initialize intermediate parameters for reducing computing amount.
206 *
207 * This function is optional to be overridden.
208 * Intermediate parameters only need to be calculated once
209 * and will not change during simulation.
210 * This function must be separated with InitialOutputs().
211 *
212 * For example, K*P*LS*11.8*exp(ROCK) in the equation of MUSLE can be
213 * considered as an intermediate parameter.
214 *
215 */
216 virtual void InitialIntermediates() {}
217
218 /*!
219 * \brief Get time step type, default is hillslope process.
220 *
221 * Remember to OVERRIDE this function to return other time step type for
222 * routing modules and others if necessary.
223 */
225 return TIMESTEP_HILLSLOPE;
226 }
227
228 //! Reset subtime step
229 virtual void ResetSubTimeStep() {
230 m_tsCounter = 1;
231 }
232
233 //! Whether the inputs (i.e., inputs derived from other modules) have been set.
235
236 //! Change the status of setting inputs parameters
237 void SetInputsDone(const bool set_done) { m_inputsSetDone = set_done; }
238
239 //! set whether intermediate parameters need to recalculated
240 void SetReCalIntermediates(const bool recal) { m_reCalIntermediates = recal; }
241
242 // set 1D Array which contains position data of raster
243 virtual void SetRasterPositionDataPointer(const char* key, int** positions) {
244 throw ModelException("SimulationModule", "SetRasterPositionDataPointer",
245 "Set function of parameter " + string(key) + " is not implemented.");
246 }
247
248 //virtual void SetRasterRows( int rows) {
249 // throw ModelException("SimulationModule", "SetRasterRows",
250 // "Set function of parameter rows is not implemented.");
251 //}
252
253 //virtual void SetRasterCols( int cols) {
254 // throw ModelException("SimulationModule", "SetRasterCols",
255 // "Set function of parameter cols is not implemented.");
256 //}
257
258 // set 1D Array which stores reach depth data in each cell of raster
259 //virtual void SetReachDepthData(FloatRaster* ch_depth) {
260 // throw ModelException("SimulationModule", "SetReachDepthData",
261 // "Set SetReachDepthData function is not implemented.");
262 //}
263
264
265protected:
266 /// date time
267 time_t m_date;
268 /// index of current year of simulation, e.g., the simulation period from 2010 to 2015, m_yearIdx is 2 when simulate 2012.
270 /// year
272 /// month since January - [1,12]
274 /// day of the month - [1,31]
275 int m_day;
276 /// day of year - [1, 366]
278 /// sub-timestep counter
280 /// Whether the inputs parameters (i.e., parameters derived from other modules) have been set.
282 /// need to recalculate intermediate parameters?
284};
285
286/*!
287 * Macros for CheckInputData function
288 * BE REMEMBER OF SEMICOLON!
289 */
290//! CHECK_DATA is used for the unforeseen situation
291#define CHECK_DATA(moduleID, expression, desc) if ((expression)) \
292 throw ModelException(moduleID, "CheckInputData", string(desc))
293//! CHECK_POINTER is used for 1D or 2D raster and other pointer of data
294#define CHECK_POINTER(moduleID, param) if (nullptr == (param)) \
295 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST NOT be NULL!"))
296//! CHECK_POSITIVE is used for single value that must be positive
297#define CHECK_POSITIVE(moduleID, param) if ((param) <= 0) \
298 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST be positive!"))
299//! CHECK_NONNEGATIVE is used for single value that must be greater or equal than zero
300#define CHECK_NONNEGATIVE(moduleID, param) if ((param) < 0) \
301 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST be greater or equal than zero!"))
302//! CHECK_NEGATIVE is used for single value that must be negative
303#define CHECK_NEGATIVE(moduleID, param) if ((param) >= 0) \
304 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST be negative!"))
305//! CHECK_ZERO is used for single value that must not be ZERO
306#define CHECK_ZERO(moduleID, param) if (FloatEqual(CVT_DBL(param), 0.)) \
307 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST NOT be zero!"))
308//! CHECK_NODATA is used for single value that must not be NODATA_VALUE
309#define CHECK_NODATA(moduleID, param) if (FloatEqual(CVT_DBL(param), NODATA_VALUE)) \
310 throw ModelException(moduleID, "CheckInputData", string(#param) + string(" MUST NOT be NODATA_VALUE!"))
311
312#endif /* SIMULATION_MOUDULE_BASE */
Scenario class in BMP database.
@ TIMESTEP_CHANNEL
Channel scale.
Definition: SimulationModule.h:37
@ TIMESTEP_SIMULATION
Whole simulation scale.
Definition: SimulationModule.h:38
@ TIMESTEP_HILLSLOPE
Hillslope scale.
Definition: SimulationModule.h:36
Basic definitions.
Main class of scenario in BMP database.
Definition: Scenario.h:47
Base type of all interfaces.
Definition: basic.h:407
Print the exception message.
Definition: basic.h:416
Class to store reach related parameters from REACHES table.
Class for managing subbasin data.
Read and store all reaches information as input parameters.
Definition: clsReach.h:88
Manager all Subbasin related parameters and methods.
Definition: clsSubbasin.h:214
virtual void SetClimateDataType(int data_type)
Set climate data type, P, M, PET etc.
Definition: SimulationModule.h:63
int m_month
month since January - [1,12]
Definition: SimulationModule.h:273
bool m_reCalIntermediates
need to recalculate intermediate parameters?
Definition: SimulationModule.h:283
virtual void ResetSubTimeStep()
Reset subtime step.
Definition: SimulationModule.h:229
bool IsInputsSetDone()
Whether the inputs (i.e., inputs derived from other modules) have been set.
Definition: SimulationModule.h:234
virtual TimeStepType GetTimeStepType()
Get time step type, default is hillslope process.
Definition: SimulationModule.h:224
virtual void SetDate(time_t t, int year_idx)
Set date time, as well as the sequence number of the entire simulation. Added by LJ for statistics co...
virtual void Set2DData(const char *key, int nrows, int ncols, FLTPT **data)
Set 2D data, by default, DT_Raster2D, float.
Definition: SimulationModule.h:109
virtual bool CheckInputSize2D(const char *module_id, const char *key, int nrows, int ncols, int &m_nrows, int &m_ncols)
Check data length of the two dimensions (i.e., nRows and nCols) of the input array-based data.
virtual bool CheckInputSize(const char *module_id, const char *key, int nrows, int &m_nrows)
Check data length of the first dimension (i.e., nRows) of the input array-based data.
virtual void Set1DData(const char *key, int n, int *data)
Set 1D data, by default, DT_Raster1D, integer.
Definition: SimulationModule.h:91
virtual void Set2DData(const char *key, int nrows, int ncols, int **data)
Set 2D data, by default, DT_Raster2D, integer.
Definition: SimulationModule.h:103
virtual void SetValueByIndex(const char *key, int index, int value)
Set single value to array1D by index, used in MPI version for passing values of subbasins,...
Definition: SimulationModule.h:79
virtual void SetScenario(Scenario *sce)
Set pointer of Scenario class which contains all BMP information. Added by LJ, 2016-6-14.
Definition: SimulationModule.h:151
bool m_inputsSetDone
Whether the inputs parameters (i.e., parameters derived from other modules) have been set.
Definition: SimulationModule.h:281
virtual int Execute()
Execute the simulation. Return 0 for success.
Definition: SimulationModule.h:52
void SetReCalIntermediates(const bool recal)
set whether intermediate parameters need to recalculated
Definition: SimulationModule.h:240
virtual void InitialIntermediates()
Initialize intermediate parameters for reducing computing amount.
Definition: SimulationModule.h:216
virtual void Get1DData(const char *key, int *n, int **data)
Get 1D data, by default, DT_Raster1D, integer.
Definition: SimulationModule.h:127
virtual void SetReaches(clsReaches *rches)
Set pointer of clsReaches class which contains all reaches information. Added by LJ,...
Definition: SimulationModule.h:156
time_t m_date
date time
Definition: SimulationModule.h:267
void SetInputsDone(const bool set_done)
Change the status of setting inputs parameters.
Definition: SimulationModule.h:237
virtual void InitialOutputs()
Initialize output variables.
Definition: SimulationModule.h:202
int m_day
day of the month - [1,31]
Definition: SimulationModule.h:275
virtual bool CheckInputData()
Check the input data.
Definition: SimulationModule.h:172
virtual void Get2DData(const char *key, int *nrows, int *ncols, FLTPT ***data)
Get 2D data, by default, DT_Raster2D, float.
Definition: SimulationModule.h:145
virtual void Get2DData(const char *key, int *nrows, int *ncols, int ***data)
Get 2D data, by default, DT_Raster2D, integer.
Definition: SimulationModule.h:139
virtual void GetValue(const char *key, FLTPT *value)
Get value, DT_Single, float.
Definition: SimulationModule.h:121
int m_dayOfYear
day of year - [1, 366]
Definition: SimulationModule.h:277
int m_year
year
Definition: SimulationModule.h:271
virtual void SetTheadNumber(const int thread_num)
Set thread number for OpenMP.
Definition: SimulationModule.h:58
virtual void SetValue(const char *key, int value)
Set data, DT_Single, integer.
Definition: SimulationModule.h:67
virtual void SetSubbasins(clsSubbasins *subbsns)
Set pointer of clsSubbasins class which contains all subbasins information. Added by LJ,...
Definition: SimulationModule.h:161
virtual void Get1DData(const char *key, int *n, FLTPT **data)
Get 1D data, by default, DT_Raster1D, float.
Definition: SimulationModule.h:133
int m_yearIdx
index of current year of simulation, e.g., the simulation period from 2010 to 2015,...
Definition: SimulationModule.h:269
int m_tsCounter
sub-timestep counter
Definition: SimulationModule.h:279
virtual void GetValue(const char *key, int *value)
Get value, DT_Single, integer.
Definition: SimulationModule.h:115
virtual void SetValueByIndex(const char *key, int index, FLTPT value)
Set single value to array1D by index, used in MPI version for passing values of subbasins,...
Definition: SimulationModule.h:85
virtual void SetValue(const char *key, FLTPT value)
Set data, DT_Single, float point number (float or double)
Definition: SimulationModule.h:73
virtual void Set1DData(const char *key, int n, FLTPT *data)
Set 1D data, by default, DT_Raster1D, float.
Definition: SimulationModule.h:97
SimulationModule()
Constructor.
Base module for all simulation modules in SEIMS.
Definition: SimulationModule.h:46
TimeStepType
Time step types.
Definition: SimulationModule.h:35
All BMPs scenario related data, classes, and functions.
Definition: BMPArealSourceFactory.h:22
Common Cross-platform Geographic Library (CCGL)
void SetOpenMPThread(int n)
Set the omp thread number by given thread number.
Time and datetime related functions in CCGL.