Simulations
EPHS.Simulations
— ModuleThe Simulations
module provides the simulate
function to compute the time evolution of composite systems, starting from an initial condition. As an argument, the function takes a numerical method, such as midpoint_rule
. Based on the DAESystem
obtained from the CompositeSystem
, this method generates Julia code, which is then called in a time-stepping loop. Further, the module provides functionality for post-processing and plotting of the SimulationResult
, see evolution
and plot_evolution
.
EPHS.Simulations.SimulationResult
— TypeSimulationResult(
sys::CompositeSystem,
dae::DAESystem,
method::Function,
h::Float64,
xs::Vector
)
Data structure returned by simulate
.
Fields
sys
:CompositeSystem
dae
: the resultingDAESystem
obtaind byassemble(sys)
method
: the numerical method (e.g.midpoint_rule
)h
: time step sizexs
: simulation result (xs[1]
is initial condition)
EPHS.Simulations.evolution
— Methodevolution(sim::SimulationResult, expr::SymExpr)
Evaluates the given SymExpr
at each time step, given that the expression depends only on the state variables.
EPHS.Simulations.evolution
— Methodevolution(sim::SimulationResult, xvar::Xvar)
Returns a Vector{Float64}
containing the values of the given state variable at each time instant.
EPHS.Simulations.midpoint_rule
— Methodmidpoint_rule(dae::DAESystem)
Generate a Julia function that performs a state update x₀ ↦ x₁
based on the implicit midpoint discretization. This is a symplectic, second-order Gauss method. For constrained systems, the state is augmented with the constraint variables.
EPHS.Simulations.nlsolve
— Methodnlsolve(residual::Function, x₀::AbstractVector)
Solves the system of nonlinear equations residual(x) ≈ 0
using the Newton-Raphson method with x = x₀
as the initial guess.
Example
julia> using StaticArrays
julia> residual(x) = SA[x[1]-x[2]+2, x[1]*x[1]-x[2]];
julia> x₀ = SA[0., 0.];
julia> nlsolve(residual, x₀) ≈ SA[-1., 1.]
true
EPHS.Simulations.simulate
— Methodsimulate(
sys::CompositeSystem,
method::Function,
ic::Union{Vector, AbstractDtry{Float64}},
h::Real,
tₑ::Real;
ps::AbstractDtry{Float64}=Dtry{Float64}()
) -> SimulationResult
Simulate the evolution of an isolated CompositeSystem
.
Arguments
sys
: the isolated systemmethod
: the numerical method used for simulationic
: directory of initial conditions for the state variables of all storage componentsh
: time step sizetₑ
: final time (duration of simulation)ps
: directory of parameters to update before simulation (optional keyword argument)
Returns a SimulationResult
.
EPHS.Simulations.timegrid
— Methodtimegrid(sim::SimulationResult)
Returns a Vector{Float64}
of time instants separated by the time step size sim.h
.
EPHS.Simulations.plot_convergence
— Methodplot_convergence(
sys::CompositeSystem,
ic::AbstractDtry{64},
hs::Vector{Float64},
t::Real,
expr::SymExpr,
error::Function,
method::Function,
ref::Vector{Int};
ps::AbstractDtry{Float64}=Dtry{Float64}()
)
Plot the convergence rate of a given integration method mthd
for a system sys
. SymExpr expr
defines a function over the simulation time t
. Function error
evaluates the results of eqn
over a set of time steps hs
. ref
is the exponent of convergence rates used as references lines. ps
overwrites the parameter set of sys
.
EPHS.Simulations.plot_evolution
— Methodplot_evolution(
sim::SimulationResult,
exprs_or_pairs::Vararg{Union{SymExpr,Pair{String,<:SymExpr}}}
)
Plot the time evolution of the given symbolic expressions, which define functions of the state variables. To use a custom label in the legend, supply a Pair{String,SymExpr}
. Keyword arguments are passed through to Plots.plot
.