Skip to contents

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

Usage

simulate(
  sfm,
  keep_nonnegative_flow = TRUE,
  keep_nonnegative_stock = FALSE,
  keep_unit = TRUE,
  only_stocks = TRUE,
  verbose = FALSE,
  ...
)

Arguments

sfm

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

keep_nonnegative_flow

If TRUE, keeps original non-negativity setting of flows. Defaults to TRUE.

keep_nonnegative_stock

If TRUE, keeps original non-negativity setting of stocks Defaults to FALSE.

keep_unit

If TRUE, keeps units of variables. Defaults to TRUE.

only_stocks

If TRUE, only return stocks in output, discarding flows and auxiliaries. If FALSE, flows and auxiliaries are saved, which slows down the simulation. Defaults to FALSE.

verbose

If TRUE, print duration of simulation. Defaults to FALSE.

...

Optional arguments

Value

Object of class sdbuildR_sim, a list containing:

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

...

Other parameters passed to simulate

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

Examples

sfm <- xmile("SIR")
sim <- simulate(sfm)
plot(sim)
# Obtain all model variables sim <- simulate(sfm, only_stocks = FALSE) plot(sim, add_constants = TRUE)
# Use Julia for models with units sfm <- sim_specs(xmile("coffee_cup"), language = "Julia") sim <- simulate(sfm) #> Starting Julia ... #> Connecting to Julia TCP server at localhost:11980 ... #> Setting up Julia environment for sdbuildR... plot(sim)
# Close Julia session use_julia(stop = TRUE) #> Julia session closed.