dasslsim
Sims.dasslsim
— Function.The solver that uses DASKR, a variant of DASSL.
See sim for the interface.
sunsim
Sims.sunsim
— Function.The solver that uses Sundials.
See sim for the interface.
Simulations
Various functions for simulations and building simulation objects from models.
sim
Sims.sim
— Function.sim
is the name of the default solver used to simulate Sims models and also shows the generic simulation API for available solvers (currently dasslsim
and sunsim
). The default solver is currently dasslsim
if DASSL is available.
sim
has many method definitions to accomodate solutions based on intermediate model representations. Also, both positional and keyword arguments are supported (use one or the other after the first argument).
sim(m::Model, tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
sim(m::Model; tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
sim(m::Sim, tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
sim(m::Sim; tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
sim(m::SimState, tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
sim(m::SimState; tstop::Float64=1.0, Nsteps::Int=500, reltol::Float64=1e-4, abstol::Float64=1e-4, init::Symbol=:Ya_Ydp, alg::Bool=true)
Arguments
m::Model
: a Modelsm::Sim
: a simulation objectss::SimState
: a simulation objecttstop::Float64
: the simulation stopping time [secs], default = 1.0Nsteps::Int
: the number of simulation steps, default = 500reltol::Float64
: the relative tolerance, default = 1e-4abstol::Float64
: the absolute tolerance, default = 1e-4init
: initialization of the model; options include::none
: no initialization:Ya_Ydp
: givenY_d
, calculateY_a
andY'_d
(the default):Y
: givenY'
, calculateY
alg
: indicates whether algebraic variables should be included in the error estimate (default is true)
Returns
::SimResult
: the simulation result
The result can be plotted with Plots.jl
.
Details
The main steps in converting to a model and doing a simulation are:
eqs::EquationSet = elaborate(m::Model) # flatten the model
sm::Sim = create_sim(eqs::EquationSet) # prepare for simulation
sm::SimState = create_simstate(sm::Sim) # prepare for simulation II
y::SimResult = sim(ss::SimState) # simulate
The following are equivalent:
y = sim(create_simstate(create_sim(elaborate(m))))
y = sim(m)
Example
using Sims
function Vanderpol()
y = Unknown(1.0, "y") # The 1.0 is the initial value. "y" is for plotting.
x = Unknown("x") # The initial value is zero if not given.
# The following gives the return value which is a list of equations.
# Expressions with Unknowns are kept as expressions. Expressions of
# regular variables are evaluated immediately (like normal).
@equations begin
# The -1.0 in der(x, -1.0) is the initial value for the derivative
der(x, -1.0) = (1 - y^2) * x - y
der(y) = x
end
end
v = Vanderpol() # returns the hierarchical model
y = sunsim(v, 50.0)
plot(y)
EquationSet
Sims.EquationSet
— Type.A representation of a flattened model, normally created with elaborate(model)
. sim
uses an elaborated model for simulations.
Contains the hierarchical equations, flattened equations, flattened initial equations, events, event response functions, and a map of Unknown nodes.
elaborate
Sims.elaborate
— Function.elaborate
is the main elaboration function that returns a flattened model representation that can be used by sim
.
elaborate(a::Model)
Arguments
a::Model
: the input model
Returns
::EquationSet
: the flattened model
Details
The main steps in flattening are:
Replace fixed initial values.
Flatten models and populate
eq.equations
.Pull out InitialEquations and populate
eq.initialequations
Pull out Events and populate
eq.events
.Handle StructuralEvents.
Collect nodes and populate
eq.nodeMap
.Strip out MExpr's from expressions.
Remove empty equations.
There is currently no real symbolic processing (sorting, index reduction, or any of the other stuff a fancy modeling tool would do).
In EquationSet, model
contains equations and StructuralEvents. When a StructuralEvent triggers, the entire model is elaborated again. The first step is to replace StructuralEvents that have activated with their new_relation in model. Then, the rest of the EquationSet is reflattened using model
as the starting point.
SimFunctions
Sims.SimFunctions
— Type.The set of functions used in the DAE solution. Includes an initial set of equations, a residual function, and several functions for detecting and responding to events.
All functions take (t,y,yp) as arguments. {TODO: is this still right?}
Sim
Sims.Sim
— Type.A type for holding several simulation objects needed for simulation, normally created with create_sim(eqs)
.
SimState
Sims.SimState
— Type.The top level type for holding all simulation objects needed for simulation, including a Sim. Normally created with create_simstate(sim)
.
create_sim
Sims.create_sim
— Function.create_sim
converts a model to a Sim.
create_sim(m::Model)
create_sim(eq::EquationSet)
Arguments
m::Model
: a Modeleq::EquationSet
: a flattened model
Returns
::Sim
: a simulation object
create_simstate
Sims.create_simstate
— Function.create_simstate
converts a Sim is the main conversion function that returns a SimState, a simulation object with state history.
create_simstate(m::Model)
create_simstate(eq::EquationSet)
create_simstate(sm::Sim)
Arguments
m::Model
: a Modeleq::EquationSet
: a flattened modelsm::Sim
: a simulation object
Returns
::Sim
: a simulation object
SimResult
Sims.SimResult
— Type.A type holding simulation results from sim
, dasslsim
, or sunsim
. Includes a matrix of results and a vector of column names.