Skip to contents

Simulation specifications are the settings that determine how the model is simulated, such as the integration method (i.e. solver), start and stop time, and timestep. Modify these specifications for an existing stock-and-flow model.

Usage

sim_specs(
  sfm,
  method = "euler",
  start = "0.0",
  stop = "100.0",
  dt = "0.01",
  save_at = dt,
  save_from = start,
  seed = NULL,
  time_units = "s",
  language = "R"
)

Arguments

sfm

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

method

Integration method. Defaults to "euler".

start

Start time of simulation. Defaults to 0.

stop

End time of simulation. Defaults to 100.

dt

Timestep of solver. Defaults to 0.01.

save_at

Timestep at which to save computed values. Defaults to dt.

save_from

Time to at which to start saving computed values. Defaults to start. Set to a larger time than start to store less data.

seed

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

time_units

Simulation time unit, e.g. 's' (second). Defaults to "s".

language

Coding language in which to simulate model. Either "R" or "Julia". Julia is necessary for using units or delay functions. Defaults to "R".

Value

Updated stock-and-flow model with new simulation specifications

Examples

sfm <- xmile("predator-prey") |>
  sim_specs(start = 0, stop = 10, dt = 0.01)
sim <- simulate(sfm)
plot(sim)
# Change the simulation method to "rk4" sfm <- sim_specs(sfm, method = "rk4") # Change the time units to "years", such that one time unit is one year sfm <- sim_specs(sfm, time_units = "years") # To save storage but not affect accuracy, use save_at and save_from sfm <- sim_specs(sfm, dt = 0.001, save_at = .1, save_from = 1) sim <- simulate(sfm) head(as.data.frame(sim)) #> time variable value #> 1 1.0 predator 19.44202 #> 2 1.1 predator 20.38485 #> 3 1.2 predator 21.26847 #> 4 1.3 predator 22.07841 #> 5 1.4 predator 22.80223 #> 6 1.5 predator 23.43008 # Add stochastic initial condition but specify seed to obtain same result sfm <- sim_specs(sfm, seed = 1) |> build(c("predator", "prey"), eqn = "runif(1, 20, 50)") sim1 <- simulate(sfm) sim2 <- simulate(sfm) plot(sim1)
plot(sim2)
# Change the simulation language to Julia to use units and delay functions sfm <- sim_specs(sfm, language = "Julia")