Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: MCSimMod
Title: Working with 'MCSim' Models
Version: 1.1
Version: 1.1.9000
Authors@R: c(
person("Dustin F.", "Kapraun", role=c("aut", "cre"), email = "kapraun.dustin@epa.gov", comment = c(ORCID = "0000-0001-5570-6383")),
person("Todd J.", "Zurlinden", role="aut", comment = c(ORCID = "0000-0003-1372-3913")),
Expand Down
29 changes: 23 additions & 6 deletions R/MCSim_model.R
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,24 @@ Model <- setRefClass("Model",

# Run script that defines initialization functions.
source(paths$inits_file, local = TRUE)
initParms <<- initParms
initStates <<- initStates

Outputs <<- Outputs
# Associate initParms for this model with the initParms function defined
# in inits_file.
r_command_string <- paste0("initParms <<- initParms_", mName)
r_expression <- parse(text = r_command_string)
eval(r_expression)

# Associate initStates for this model with the initStates function defined
# in inits_file.
r_command_string <- paste0("initStates <<- initStates_", mName)
r_expression <- parse(text = r_command_string)
eval(r_expression)
Comment on lines +118 to +120
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern of building strings, parsing, and evaluating is repeated three times. Consider extracting this into a helper function to reduce duplication and improve maintainability. For example: assign_model_function <- function(name, mName) { eval(parse(text = paste0(name, ' <<- ', name, '_', mName))) }

Copilot uses AI. Check for mistakes.

# Associate Outputs for this model with the Outputs variable defined in
# inits_file.
r_command_string <- paste0("Outputs <<- Outputs_", mName)
r_expression <- parse(text = r_command_string)
eval(r_expression)
Comment on lines +124 to +126
Copy link

Copilot AI Nov 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pattern of building strings, parsing, and evaluating is repeated three times. Consider extracting this into a helper function to reduce duplication and improve maintainability. For example: assign_model_function <- function(name, mName) { eval(parse(text = paste0(name, ' <<- ', name, '_', mName))) }

Copilot uses AI. Check for mistakes.

parms <<- initParms()
Y0 <<- initStates(parms)
Expand All @@ -125,10 +139,13 @@ Model <- setRefClass("Model",
runModel = function(times, ...) {
"Perform a simulation for the Model object using the \\code{deSolve} function \\code{ode} for the specified \\code{times}."
# Solve the ODE system using the "ode" function from the package "deSolve".
derivs_name <- paste0("derivs_", mName)
initforc_name <- paste0("initforc_", mName)
initmod_name <- paste0("initmod_", mName)
out <- ode(Y0, times,
func = "derivs", parms = parms, dllname = paths$dll_name,
initforc = "initforc", initfunc = "initmod", nout = length(Outputs),
outnames = Outputs, ...
func = derivs_name, parms = parms, dllname = paths$dll_name,
initforc = initforc_name, initfunc = initmod_name,
nout = length(Outputs), outnames = Outputs, ...
)

# Return the simulation output.
Expand Down
73 changes: 72 additions & 1 deletion R/compileModel.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@
#' @export
compileModel <- function(model_file, c_file, dll_name, dll_file, hash_file = NULL, verbose_output = FALSE) {
# Unload DLL if it has been loaded.
if (is.loaded("derivs", PACKAGE = dll_name)) {
mList <- .fixPath(model_file)
model_name <- mList$mName
model_path <- mList$mPath
derivs_name <- paste0("derivs_", model_name)
if (is.loaded(derivs_name, PACKAGE = dll_name)) {
dyn.unload(dll_file)
}

Expand Down Expand Up @@ -92,6 +96,73 @@ compileModel <- function(model_file, c_file, dll_name, dll_file, hash_file = NUL
}
}

# Code to update C source file using model-specific names for objects.

# Read the original C source file.
lines <- readLines(c_file)

# Find and replace C object names with model-specific names.
item_to_replace <- c(
"parms",
"forc",
"Nout",
"nr",
"ytau",
"yini",
"lagvalue",
"CalcDelay",
"initmod",
"initforc",
"initState",
"getParms",
"derivs",
"jac",
"event",
"root"
)
for (idx in seq(length(item_to_replace))) {
lines <- gsub(
paste0("\\b", item_to_replace[idx], "\\b"),
paste0(item_to_replace[idx], "_", model_name),
lines
)
}

# Overwrite the C source file with the updated text.
writeLines(lines, c_file)


# Code to update inits R source file using model-specific names for objects.

# Get inits R source file name.
inits_file <- file.path(
model_path,
paste0(model_name, "_model_inits.R")
)

# Read the original inits R source file.
lines <- readLines(inits_file)

# Find and replace R and C object names with model-specific names.
item_to_replace <- c(
"initParms",
"getParms",
"Outputs",
"initStates",
"initState"
)
for (idx in seq(length(item_to_replace))) {
lines <- gsub(
paste0("\\b", item_to_replace[idx], "\\b"),
paste0(item_to_replace[idx], "_", model_name),
lines
)
}

# Overwrite the R inits source file with the updated text.
writeLines(lines, inits_file)


# Compile the C model to obtain an object file (ending with ".o") and a
# machine code file (ending with ".dll" or ".so"). Write compiler output
# to a character string.
Expand Down
2 changes: 1 addition & 1 deletion man/Model-class.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.