Modify simulation specifications
sim_specs.Rd
Simulation specifications are the settings that determine how the model is simulated, such as the integration method, 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",
saveat = dt,
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.
- saveat
Timestep at which to save computed values. Defaults to dt.
- 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".
- ...
Optional additional parameters
Examples
sfm = xmile("predator-prey") %>%
sim_specs(start = 1960, stop = 2010, dt = 0.1)
sim = simulate(sfm)
plot(sim)
# Change the simulation method to "rk4"
sfm = sfm %>% sim_specs(method = "rk4")
# Change the time units to "years", such that one time unit is one year
sfm = sfm %>% sim_specs(time_units = "years")
# To save storage but not affect accuracy, use saveat
sfm = sfm %>% sim_specs(saveat = 0.1, dt = 0.001)
sim = simulate(sfm)
head(as.data.frame(sim))
#> time predator prey predator_births predator_deaths prey_births
#> 1 1960.0 10.00000 50.00000 12.50000 5.000000 25.00000
#> 2 1960.1 10.77798 49.90401 13.44661 5.388992 24.95200
#> 3 1960.2 11.61083 49.60798 14.39975 5.805415 24.80399
#> 4 1960.3 12.49553 49.10233 15.33899 6.247763 24.55116
#> 5 1960.4 13.42704 48.38153 16.24052 6.713520 24.19077
#> 6 1960.5 14.39813 47.44494 17.07796 7.199065 23.72247
#> prey_deaths
#> 1 25.00000
#> 2 26.89323
#> 3 28.79949
#> 4 30.67797
#> 5 32.48104
#> 6 34.15592
# Specify seed for reproducibility
sfm = sfm %>% sim_specs(seed = 1) %>%
# Add stochastic initial condition
build(c("predator", "prey"), eqn = "runif(1, 20, 50)")
sim1 = simulate(sfm)
sim2 = simulate(sfm)
plot(sim1)
plot(sim2)
# Removing the seed yields variation
sfm = sfm %>% sim_specs(seed = NULL)
sim1 = simulate(sfm)
sim2 = simulate(sfm)
plot(sim1)
plot(sim2)
# Change the simulation language to Julia to use units and delay functions
sfm = sfm %>% sim_specs(language = "Julia")