Skip to content

Commit 245c99e

Browse files
authored
Merge pull request #12 from biocro/improve-write-model
Improve write_model
2 parents 92e73da + 69f5b57 commit 245c99e

4 files changed

Lines changed: 45 additions & 16 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,10 @@ be directly added to this file to describe the related changes.
4747
(`minimal`) only prints info to the terminal when an issue with a simulation
4848
occurs
4949

50+
- Improved formatting of output from `write_module`, so that initial value and
51+
parameter lists are alphabetized, equals signs are aligned, and module names
52+
are preserved
53+
5054
# Changes in BioCroValidation Version 0.2.0 (2025-05-23)
5155

5256
- Added 2002 and 2005 SoyFACE biomass and standard deviation data.

R/write_model.R

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,39 @@ write_model <- function(
77
ode_solver
88
)
99
{
10+
# Get the longest name among the initial values and parameters
11+
all_names <- c(
12+
names(initial_values),
13+
names(parameters)
14+
)
15+
16+
name_width <- max(sapply(all_names, nchar))
17+
18+
# Alphabetize the initial values and parameters
19+
initial_values <- initial_values[order(tolower(names(initial_values)))]
20+
parameters <- parameters[order(tolower(names(parameters)))]
21+
22+
# Prepare the module list strings
23+
dir_module_string <- paste(paste0(' ', names(direct_modules), ' = "', direct_modules, '"'), collapse = ',\n')
24+
diff_module_string <- paste(paste0(' ', names(differential_modules), ' = "', differential_modules, '"'), collapse = ',\n')
25+
26+
# Remove any lines without names
27+
dir_module_string <- gsub(' = ', ' ', dir_module_string)
28+
diff_module_string <- gsub(' = ', ' ', diff_module_string)
29+
1030
# Fill in the module definition template (defined below)
1131
model_text <- sprintf(
1232
model_definition_template,
1333
name,
14-
paste(paste0(' "', direct_modules, '"'), collapse = ',\n'),
15-
paste(paste0(' "', differential_modules, '"'), collapse = ',\n'),
34+
dir_module_string,
35+
diff_module_string,
1636
ode_solver[['type']],
1737
ode_solver[['output_step_size']],
1838
ode_solver[['adaptive_rel_error_tol']],
1939
ode_solver[['adaptive_abs_error_tol']],
2040
ode_solver[['adaptive_max_steps']],
21-
paste(paste0(' "', names(initial_values), '" = ', initial_values), collapse = ',\n'),
22-
paste(paste0(' "', names(parameters), '" = ', parameters), collapse = ',\n')
41+
paste(paste0(' ', format(names(initial_values), width = name_width), ' = ', initial_values), collapse = ',\n'),
42+
paste(paste0(' ', format(names(parameters), width = name_width), ' = ', parameters), collapse = ',\n')
2343
)
2444
}
2545

@@ -31,11 +51,11 @@ model_definition_template <- '%s <- list(
3151
%s
3252
),
3353
ode_solver = list(
34-
type = "%s",
35-
output_step_size = %f,
54+
type = "%s",
55+
output_step_size = %f,
3656
adaptive_rel_error_tol = %e,
3757
adaptive_abs_error_tol = %e,
38-
adaptive_max_steps = %i
58+
adaptive_max_steps = %i
3959
),
4060
initial_values = list(
4161
%s

man/write_model.Rd

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@
6262
output. See examples below. Note that it is customary to name the R script
6363
file as \code{name.R}, where \code{name} is the value provided to the function
6464
itself.
65+
66+
The \code{initial_values} and \code{parameters} will be alphabetized
67+
(case-insensitive). The \code{ode_solver} elements will be printed in the
68+
customary BioCro order. The \code{direct_modules} and
69+
\code{differential_modules} will not be reordered.
6570
}
6671

6772
\value{
@@ -85,7 +90,7 @@ if (require(BioCro)) {
8590

8691
if (interactive()) {
8792
# Use writeLines to save as a `.R` file
88-
writeLines(out, "./test_soybean_model.h")
93+
writeLines(out, "./test_soybean_model.R")
8994
}
9095
}
9196
}

tests/test_data/test_model.R

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ test_model <- list(
66
"BioCro:harmonic_oscillator"
77
),
88
ode_solver = list(
9-
type = "boost_rkck54",
10-
output_step_size = 1.000000,
9+
type = "boost_rkck54",
10+
output_step_size = 1.000000,
1111
adaptive_rel_error_tol = 1.000000e-04,
1212
adaptive_abs_error_tol = 1.000000e-04,
13-
adaptive_max_steps = 200
13+
adaptive_max_steps = 200
1414
),
1515
initial_values = list(
16-
"position" = 1,
17-
"velocity" = 0
16+
position = 1,
17+
velocity = 0
1818
),
1919
parameters = list(
20-
"timestep" = 1,
21-
"mass" = 1,
22-
"spring_constant" = 0.5
20+
mass = 1,
21+
spring_constant = 0.5,
22+
timestep = 1
2323
)
2424
)

0 commit comments

Comments
 (0)