|
1 | 1 | # Define the names of the variables in the model. This is also used for a
|
2 | 2 | # sanity check of the supplied input.
|
3 |
| -initial_condition_names = c( |
4 |
| - "AgeGV", |
5 |
| - "AgeGR", |
6 |
| - "AgeDV", |
7 |
| - "AgeDR", |
8 |
| - "BMGV", |
9 |
| - "BMGR", |
10 |
| - "BMDV", |
11 |
| - "BMDR", |
12 |
| - "SENGV", |
13 |
| - "SENGR", |
14 |
| - "ABSDV", |
15 |
| - "ABSDR", |
16 |
| - "ST", |
17 |
| - "cBM" |
| 3 | +initial_conditions = list( |
| 4 | + AgeGV = 100, |
| 5 | + AgeGR = 2000, |
| 6 | + AgeDV = 500, |
| 7 | + AgeDR = 500, |
| 8 | + BMGV = 420, |
| 9 | + BMGR = 0, |
| 10 | + BMDV = 300, |
| 11 | + BMDR = 30, |
| 12 | + SENGV = 0, |
| 13 | + SENGR = 0, |
| 14 | + ABSDV = 0, |
| 15 | + ABSDR = 0, |
| 16 | + ST = 0, |
| 17 | + cBM = 0 |
18 | 18 | )
|
19 |
| -parameter_names = c( |
20 |
| - "LAT", |
21 |
| - "LON", |
22 |
| - "ELV", |
23 |
| - "WHC", |
24 |
| - "NI", |
25 |
| - "w_FGA", |
26 |
| - "w_FGB", |
27 |
| - "w_FGC", |
28 |
| - "w_FGD", |
29 |
| - "RUEmax", |
30 |
| - "sigmaGV", |
31 |
| - "sigmaGR", |
32 |
| - "T0", |
33 |
| - "T1", |
34 |
| - "T2", |
35 |
| - "KGV", |
36 |
| - "KGR", |
37 |
| - "KlDV", |
38 |
| - "KlDR", |
39 |
| - "OMDDV", |
40 |
| - "OMDDR", |
41 |
| - "CO2_growth_factor" |
| 19 | +required_parameters = list( |
| 20 | + WHC = NA, |
| 21 | + NI = NA, |
| 22 | + w_FGA = NA, |
| 23 | + w_FGB = NA, |
| 24 | + w_FGC = NA, |
| 25 | + w_FGD = NA |
| 26 | +) |
| 27 | +parameter_defaults = list( |
| 28 | + LAT = NA, |
| 29 | + LON = NA, |
| 30 | + ELV = NA, |
| 31 | + RUEmax = 3, |
| 32 | + sigmaGV = 0.4, |
| 33 | + sigmaGR = 0.2, |
| 34 | + T0 = 5, |
| 35 | + T1 = 10, |
| 36 | + T2 = 20, |
| 37 | + KGV = 0.002, |
| 38 | + KGR = 0.001, |
| 39 | + KlDV = 0.001, |
| 40 | + KlDR = 0.0005, |
| 41 | + OMDDV = 0.45, |
| 42 | + OMDDR = 0.4, |
| 43 | + CO2_growth_factor = 0.5 |
42 | 44 | )
|
43 | 45 | # Create a list that can be inserted into R6 class to create many fields
|
44 |
| -all_parameter_names = c(initial_condition_names, parameter_names) |
45 |
| -parameters = as.list(all_parameter_names) |
46 |
| -names(parameters) = all_parameter_names |
47 |
| -sapply(all_parameter_names, function(p) {parameters[[p]] = NA}) |
48 |
| -# Same procedure for functional group parameters |
49 |
| -fg_parameters = as.list(FG_A$fg_parameter_names) |
50 |
| -names(fg_parameters) = FG_A$fg_parameter_names |
51 |
| -sapply(FG_A$fg_parameter_names, function(p) {fg_parameters[[p]] = NA}) |
| 46 | +initial_condition_names = names(initial_conditions) |
| 47 | +parameter_names = names(parameter_defaults) |
| 48 | +required_parameter_names = names(required_parameters) |
| 49 | +all_parameter_names = c(initial_condition_names, parameter_names, |
| 50 | + required_parameter_names) |
| 51 | +parameters = c(initial_conditions, parameter_defaults, required_parameters) |
| 52 | +# Similar procedure for functional group parameters, using the values of FG_A as |
| 53 | +# defaults. |
| 54 | +fg_parameters = FG_A$get_parameters() |
52 | 55 | # Similar procedure for initial_conditions:
|
53 | 56 | # For each value that needs to be stored as an initial condition, create a
|
54 | 57 | # copy of the variable with an appended "0" to the variable name.
|
55 | 58 | initial_condition_values = list()
|
56 | 59 | for (i_param in 1:length(initial_condition_names)) {
|
57 | 60 | old_name = initial_condition_names[i_param]
|
58 | 61 | new_name = paste0(old_name, "0")
|
59 |
| - initial_condition_values[[new_name]] = NA |
| 62 | + initial_condition_values[[new_name]] = initial_conditions[[old_name]] |
60 | 63 | }
|
61 | 64 | for (name in c("WR0", "OMDGV0", "OMDGR0")) {
|
62 | 65 | initial_condition_values[[name]] = NA
|
@@ -101,10 +104,14 @@ ModvegeParameters = R6Class(
|
101 | 104 |
|
102 | 105 | list(
|
103 | 106 | #-Public-attributes-----------------------------------------------------------
|
104 |
| -#' @field parameter_names Names of all parameters and state variables. |
| 107 | +#' @field required_parameter_names Names of parameters that do not have a |
| 108 | +#' default value and are therefore strictly required. |
| 109 | + required_parameter_names = required_parameter_names, |
| 110 | +#' @field parameter_names Names of all required and optional parameters and |
| 111 | +#' state variables. |
105 | 112 | parameter_names = all_parameter_names,
|
106 | 113 | #' @field n_parameters Number of total parameters.
|
107 |
| - n_parameters = length(parameter_names), |
| 114 | + n_parameters = length(all_parameter_names), |
108 | 115 | #' @field functional_group The [FunctionalGroup] instance holding the
|
109 | 116 | #' vegetation parameters.
|
110 | 117 | functional_group = NULL,
|
@@ -233,30 +240,36 @@ ModvegeParameters = R6Class(
|
233 | 240 | #'
|
234 | 241 | #' @param param_names A list of parameter names to be checked.
|
235 | 242 | #' @param check_for_completeness Boolean Toggle whether only the
|
236 |
| - #' validity of supplied *param_names* is checked (default) or whether we |
237 |
| - #' require all parameters to be present. In the latter case, if any |
238 |
| - #' parameter is missing, an error is thrown. |
| 243 | + #' validity of supplied *param_names* is checked or whether we |
| 244 | + #' want to check that all required parameters to be present |
| 245 | + #' (default). In the latter case, if any required parameter is |
| 246 | + #' missing, an error is thrown. |
239 | 247 | #' @return not_known The list of unrecognized parameter names.
|
240 | 248 | #'
|
241 | 249 | #' @md
|
242 |
| - check_parameters = function(param_names, check_for_completeness = FALSE) { |
| 250 | + check_parameters = function(param_names, check_for_completeness = TRUE) { |
243 | 251 | not_known = setdiff(param_names, self$parameter_names)
|
| 252 | + param_file = self$param_file |
244 | 253 | # Warn if some arguments were not recognized.
|
245 | 254 | if (length(not_known) != 0) {
|
246 | 255 | unknown_args = paste(not_known, collapse = "\n")
|
247 |
| - message = paste("The following unrecognized parameters were present", |
248 |
| - "in the supplied input file (%s):\n%s") |
249 |
| - logger(sprintf(message, param_file, unknown_args), level = WARNING) |
| 256 | + msg = paste("The following unrecognized parameters were present", |
| 257 | + "in the supplied input file (%s):\n%s") |
| 258 | + logger(sprintf(msg, param_file, unknown_args), level = WARNING) |
250 | 259 | }
|
251 | 260 | if (check_for_completeness) {
|
252 | 261 | # Give error if an argument is missing.
|
253 |
| - not_present = setdiff(self$parameter_names, param_names) |
| 262 | + print("===============================================================") |
| 263 | + print(self$required_parameter_names) |
| 264 | + print(param_names) |
| 265 | + print("===============================================================") |
| 266 | + not_present = setdiff(self$required_parameter_names, param_names) |
254 | 267 | if (length(not_present) != 0 ) {
|
255 | 268 | # Construct the error message.
|
256 | 269 | missing_args = paste(not_present, collapse = "\n")
|
257 |
| - message = paste("The following parameters were missing", |
258 |
| - "from the supplied input file (%s):\n%s") |
259 |
| - logger(sprintf(message, param_file, missing_args), level = WRNING) |
| 270 | + msg = paste("The following parameters were missing", |
| 271 | + "from the supplied input file (%s):\n%s") |
| 272 | + logger(sprintf(msg, param_file, missing_args), level = ERROR) |
260 | 273 | }
|
261 | 274 | }
|
262 | 275 | return(not_known)
|
|
0 commit comments