Skip to content

Commit

Permalink
Add documentation how to vary RHS params in SimulationBatch (#1398)
Browse files Browse the repository at this point in the history
  • Loading branch information
PavelBal authored May 6, 2024
1 parent 03e8f5a commit 84db2e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
17 changes: 10 additions & 7 deletions R/utilities-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -124,11 +124,13 @@ saveSimulation <- function(simulation, filePath) {
#' popPath <- system.file("extdata", "pop.csv", package = "ospsuite")
#' population <- loadPopulation(popPath)
#' results <- runSimulation(sim, population, simulationRunOptions = simRunOptions)
#'}
#' }
runSimulation <- function(simulation, population = NULL, agingData = NULL, simulationRunOptions = NULL) {
lifecycle::deprecate_soft(when = "12.0.0",
what = "runSimulation()",
with = "runSimulations()")
lifecycle::deprecate_soft(
when = "12.0.0",
what = "runSimulation()",
with = "runSimulations()"
)

# Check that only one simulation is passed
simulation <- c(simulation)
Expand Down Expand Up @@ -202,7 +204,6 @@ runSimulations <- function(simulations, population = NULL, agingData = NULL, sim
outputList <- list()
outputList[[simulations[[1]]$id]] <- results
} else {

# more than one simulation? This is a concurrent run.

# We do not allow population variation
Expand Down Expand Up @@ -338,7 +339,7 @@ runSimulations <- function(simulations, population = NULL, agingData = NULL, sim
createSimulationBatch <- function(simulation, parametersOrPaths = NULL, moleculesOrPaths = NULL) {
validateIsOfType(simulation, "Simulation")
validateIsOfType(parametersOrPaths, c("Parameter", "character"), nullAllowed = TRUE)
validateIsOfType(moleculesOrPaths, c("Molecule", "character"), nullAllowed = TRUE)
validateIsOfType(moleculesOrPaths, c("Quantity", "character"), nullAllowed = TRUE)

if (length(parametersOrPaths) == 0 && length(moleculesOrPaths) == 0) {
stop(messages$errorSimulationBatchNothingToVary)
Expand All @@ -352,7 +353,9 @@ createSimulationBatch <- function(simulation, parametersOrPaths = NULL, molecule

variableMolecules <- c(moleculesOrPaths)

if (isOfType(variableMolecules, "Molecule")) {
# Checking for Quantity instead of Molecule because state variable parameters must
# be added as molecules
if (isOfType(variableMolecules, "Quantity")) {
variableMolecules <- unlist(lapply(variableMolecules, function(x) x$path), use.names = FALSE)
}

Expand Down
3 changes: 2 additions & 1 deletion tests/testthat/test-utilities-simulation.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ test_that("Two sims not from cache and third from cache", {

test_that("It throws an exception if the pkml loaded is not a valid simulation file", {
expect_error(loadTestSimulation("molecules"),
regexp = "Could not load simulation")
regexp = "Could not load simulation"
)
})

test_that("It can remove simulation from cache", {
Expand Down
32 changes: 30 additions & 2 deletions vignettes/efficient-calculations.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,6 @@ results <- runSimulationBatches(simulationBatches)
print(names(unlist(results)))
```


The enqueued run values are cleared after calling `runSimulationBatches()`, so executing the run
again would result in an empty results list. We can now set more values to the batches and run them again.
Notes:
Expand All @@ -204,4 +203,33 @@ rm(simBatch2)

Usage of `SimulationBatch` is recommended for advanced scenarios where simulations
expected to be run hundreds of times and where each second that can be spared
will impact the performance significantly.
will impact the performance significantly.

### Varying state variable parameters with SimulationBatch

State variable parameters, i.e., those defined by a right hand side (RHS), are treated as molecules internally. Trying to create and run a simulation batch with a state variable parameter set as a variable parameter
will result in an error:

```{r BatchRunStateVarParam, eval=TRUE, error = TRUE, purl = FALSE}
stateVariableParam <- getParameter(path = "Organism|Lumen|Stomach|Liquid", container = sim1)
print(stateVariableParam)
# Create simulation batch with state variable parameter set as a variable parameter
simBatch <- createSimulationBatch(simulation = sim1, parametersOrPaths = stateVariableParam)
# Add run values
resId <- simBatch$addRunValues(parameterValues = 0.5)
# Try to run batch
results <- runSimulationBatches(simBatch)
```

Instead, the state variable parameter should be treated as a species and set as a variable molecule start value.

```{r BatchRunStateVarMolecule, eval=TRUE}
# Create simulation batch with state variable parameter set as a variable molecule
simBatch <- createSimulationBatch(simulation = sim1, moleculesOrPaths = stateVariableParam)
# Add run values
resId <- simBatch$addRunValues(initialValues = 0.5)
# Try to run batch
results <- runSimulationBatches(simBatch)
```

0 comments on commit 84db2e9

Please sign in to comment.