Skip to content

Commit fd08566

Browse files
committed
Default values for ModvegeParameters, closes #26
1 parent a09548a commit fd08566

File tree

11 files changed

+283
-80
lines changed

11 files changed

+283
-80
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: growR
22
Type: Package
3-
Version: 1.0.0.9005
3+
Version: 1.0.0.9006
44
Date: 2023-09-27
55
Authors@R: person(
66
given = "Kevin",

NEWS.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
* Input data CSV files are now actual CSV files, instead of
1414
"semicolon-separated value" files.
1515

16-
* `ParameterData` now only throws a warning on missing parameters instead of
17-
an error.
16+
* `ParameterData` now assumes default values for most parameters.
17+
`check_parameters` now only throws an error if any of the really
18+
*required* parameters are missing.
1819

1920
## Removed
2021

R/parameter_scan.R

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,8 @@ run_parameter_scan = function(environment, param_values, force = FALSE,
7676
}
7777

7878
# Ensure all supplied parameters are valid
79-
environment$parameters$check_parameters(names(param_values))
79+
environment$parameters$check_parameters(names(param_values),
80+
check_for_completeness = FALSE)
8081
# Create all parameter combinations
8182
parameter_sets = create_combinations(param_values)
8283
n_combinations = length(parameter_sets)

R/parameters.R

Lines changed: 73 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,65 @@
11
# Define the names of the variables in the model. This is also used for a
22
# 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
1818
)
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
4244
)
4345
# 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()
5255
# Similar procedure for initial_conditions:
5356
# For each value that needs to be stored as an initial condition, create a
5457
# copy of the variable with an appended "0" to the variable name.
5558
initial_condition_values = list()
5659
for (i_param in 1:length(initial_condition_names)) {
5760
old_name = initial_condition_names[i_param]
5861
new_name = paste0(old_name, "0")
59-
initial_condition_values[[new_name]] = NA
62+
initial_condition_values[[new_name]] = initial_conditions[[old_name]]
6063
}
6164
for (name in c("WR0", "OMDGV0", "OMDGR0")) {
6265
initial_condition_values[[name]] = NA
@@ -101,10 +104,14 @@ ModvegeParameters = R6Class(
101104

102105
list(
103106
#-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.
105112
parameter_names = all_parameter_names,
106113
#' @field n_parameters Number of total parameters.
107-
n_parameters = length(parameter_names),
114+
n_parameters = length(all_parameter_names),
108115
#' @field functional_group The [FunctionalGroup] instance holding the
109116
#' vegetation parameters.
110117
functional_group = NULL,
@@ -233,30 +240,36 @@ ModvegeParameters = R6Class(
233240
#'
234241
#' @param param_names A list of parameter names to be checked.
235242
#' @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.
239247
#' @return not_known The list of unrecognized parameter names.
240248
#'
241249
#' @md
242-
check_parameters = function(param_names, check_for_completeness = FALSE) {
250+
check_parameters = function(param_names, check_for_completeness = TRUE) {
243251
not_known = setdiff(param_names, self$parameter_names)
252+
param_file = self$param_file
244253
# Warn if some arguments were not recognized.
245254
if (length(not_known) != 0) {
246255
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)
250259
}
251260
if (check_for_completeness) {
252261
# 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)
254267
if (length(not_present) != 0 ) {
255268
# Construct the error message.
256269
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)
260273
}
261274
}
262275
return(not_known)

man/ModvegeParameters.Rd

Lines changed: 10 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/ModvegeSite.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/WeatherData.Rd

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/analyze_parameter_scan.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/_snaps/modvegesite/modvege_reference_output.dat

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,6 @@
1515
#LAT;46.77
1616
#LON;7.11
1717
#ELV;650
18-
#WHC;130
19-
#NI;0.7
20-
#w_FGA;0.7
21-
#w_FGB;0.3
22-
#w_FGC;0
23-
#w_FGD;0
2418
#RUEmax;3
2519
#sigmaGV;0.4
2620
#sigmaGR;0.2
@@ -34,6 +28,12 @@
3428
#OMDDV;0.45
3529
#OMDDR;0.4
3630
#CO2_growth_factor;0.5
31+
#WHC;130
32+
#NI;0.7
33+
#w_FGA;0.7
34+
#w_FGB;0.3
35+
#w_FGC;0
36+
#w_FGD;0
3737
#year;2013
3838
#site_name;-
3939
#run_name;-

0 commit comments

Comments
 (0)