Skip to contents

Simulate a stock-and-flow model with simulation specifications defined by sim_settings(). If sim_settings(language = "julia"), the Julia environment will first be set up with use_julia(). If any problems are detected by summary(), the model cannot be simulated.

Usage

# S3 method for class 'stockflow'
simulate(object, nsim = 1, seed = NULL, ...)

Arguments

object

Stock-and-flow model, object of class stockflow.

nsim

Number of simulations to run (unused; see ensemble() for running multiple simulations).

seed

Seed number to ensure reproducibility across runs in case of random elements. Must be an integer. Defaults to NULL (no seed).

...

Optional arguments passed to sim_settings(); these can be used to override the simulation specifications set in the model object.

Value

Object of class simulate_stockflow, a list containing:

object

Stock-and-flow model object of class stockflow

df

Data frame: simulation results (time, variable, value)

init

Named vector: initial stock values

constants

Named vector: constant parameters

script

Character: generated simulation code (R or Julia)

duration

Numeric: simulation time in seconds

success

Logical: TRUE if completed without errors

error_message

NULL if completed without errors

Use as.data.frame() to extract results, plot() to visualize.

Examples

sfm <- stockflow("SIR")
sim <- simulate(sfm)
plot(sim)
# Obtain all model variables sim <- simulate(sim_settings(sfm, only_stocks = FALSE)) plot(sim, show_constants = TRUE)