Simulations

EPHS.SimulationsModule

The 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.

source
EPHS.Simulations.SimulationResultType
SimulationResult(
    sys::CompositeSystem,
    dae::DAESystem,
    method::Function,
    h::Float64,
    xs::Vector
)

Data structure returned by simulate.

Fields

  • sys: CompositeSystem
  • dae: the resulting DAESystem obtaind by assemble(sys)
  • method: the numerical method (e.g. midpoint_rule)
  • h: time step size
  • xs: simulation result (xs[1] is initial condition)
source
EPHS.Simulations.evolutionMethod
evolution(sim::SimulationResult, expr::SymExpr)

Evaluates the given SymExpr at each time step, given that the expression depends only on the state variables.

source
EPHS.Simulations.evolutionMethod
evolution(sim::SimulationResult, xvar::Xvar)

Returns a Vector{Float64} containing the values of the given state variable at each time instant.

source
EPHS.Simulations.midpoint_ruleMethod
midpoint_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.

source
EPHS.Simulations.nlsolveMethod
nlsolve(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
source
EPHS.Simulations.simulateMethod
simulate(
  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 system
  • method: the numerical method used for simulation
  • ic: directory of initial conditions for the state variables of all storage components
  • h: time step size
  • tₑ: final time (duration of simulation)
  • ps: directory of parameters to update before simulation (optional keyword argument)

Returns a SimulationResult.

source
EPHS.Simulations.plot_convergenceMethod
plot_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.

source
EPHS.Simulations.plot_evolutionMethod
plot_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.

source