ODESystem
Basic Operation of ODESystem
When an ODESystem is triggered, it reads its current time from its trigger link, reads its input, solves its differential equation and computes its output. Let us observe the basic operation of ODESystems with a simple example.
We first construct an ODESystem. Since an ODESystem is represented by its state equation and output equation, we need to define those equations.
julia> using Causal # hide
julia> sfunc(dx,x,u,t) = (dx .= -0.5x)
sfunc (generic function with 1 method)
julia> ofunc(x, u, t) = x
ofunc (generic function with 1 method)Let us construct the system
julia> ds = ODESystem(righthandside=sfunc, readout=ofunc, state=[1.], input=Inport(1), output=Outport(1))
ODESystem(righthandside:sfunc, readout:ofunc, state:[1.0], t:0.0, input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))Note that ds is a single input single output ODESystem with an initial state of [1.] and initial time 0.. To drive, i.e. trigger ds, we need to launch it.
julia> oport, iport, trg, hnd = Outport(1), Inport(1), Outpin(), Inpin{Bool}()
(Outport(numpins:1, eltype:Outpin{Float64}), Inport(numpins:1, eltype:Inpin{Float64}), Outpin(eltype:Float64, isbound:false), Inpin(eltype:Bool, isbound:false))
julia> connect!(oport, ds.input)
1-element Array{Link{Float64},1}:
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)
julia> connect!(ds.output, iport)
1-element Array{Link{Float64},1}:
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)
julia> connect!(trg, ds.trigger)
Link(state:open, eltype:Float64, isreadable:false, iswritable:false)
julia> connect!(ds.handshake, hnd)
Link(state:open, eltype:Bool, isreadable:false, iswritable:false)
julia> task = launch(ds)
Task (runnable) @0x00007fe0d444c9d0
julia> task2 = @async while true
all(take!(iport) .=== NaN) && break
end
Task (runnable) @0x00007fe0d61f8eb0When launched, ds is ready to driven. ds is driven from its trigger link. Note that the trigger link of ds is writable.
julia> ds.trigger.link
Link(state:open, eltype:Float64, isreadable:false, iswritable:true)Let us drive ds to the time of t of 1 second.
julia> put!(trg, 1.)When driven, ds reads current time of t from its trigger link, reads its input value from its input, solves its differential equation and computes its output values and writes its output. So, for the step to be continued, an input values must be written. Note that the input of ds is writable,
julia> ds.input[1].link
Link(state:open, eltype:Float64, isreadable:false, iswritable:true)Let us write some value.
julia> put!(oport, [5.])
1-element Array{Float64,1}:
5.0At this point, ds completed its step and put true to its handshake link to signal that its step is succeeded.
julia> hnd.link
Link(state:open, eltype:Bool, isreadable:true, iswritable:false)To complete the step and be ready for another step, we need to approve the step by reading its handshake.
julia> take!(hnd)
trueAt this point, ds can be driven further.
julia> for t in 2. : 10.
put!(trg, t)
put!(oport, [t * 10])
take!(hnd)
endNote that all the output value of ds is written to its outputbus,
julia> iport[1].link.buffer
64-element Buffer{Cyclic,Float64,1}:
0.00673796499594269
0.01110902273516018
0.018315676429473696
0.03019743608348212
0.04978714003339768
0.08208509196489885
0.13533539576281062
0.22313028059487142
0.36787951505627364
0.60653067653308
⋮
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0
0.0When we launched ds, we constructed a task and the task is still running.
julia> task
Task (runnable) @0x00007fe0d444c9d0
julia> task2
Task (runnable) @0x00007fe0d61f8eb0To terminate the task safely, we need to terminate ds safely.
julia> put!(trg, NaN)
julia> put!(ds.output, [NaN])
1-element Array{Float64,1}:
NaNNow, the state of the task is done.
julia> task
Task (done) @0x00007fe0d444c9d0
julia> task2
Task (done) @0x00007fe0d61f8eb0So, it is not possible to drive ds.
Mutation in State Function in ODESystem
Consider a system with the following ODE
where $x \in R^d, y \in R^m, u \in R^p$. To construct and ODESystem, The signature of the state function statefunc must be of the form
function statefunc(dx, x, u, t)
dx .= ... # Update dx
endNote that statefunc does not construct dx but updates dx and does not return anything. This is for performance reasons. On the contrary, the signature of the output function outputfunc must be of the form,
function outputfunc(x, u, t)
y = ... # Compute y
return y
endNote the output value y is computed and returned from outputfunc. y is not updated but generated in the outputfunc.
Full API
Causal.@def_ode_system — Macro@def_ode_system exwhere ex is the expression to define to define a new AbstractODESystem component type. The usage is as follows:
@def_ode_system mutable struct MyODESystem{T1,T2,T3,...,TN,OP,RH,RO,ST,IP,OP} <: AbstractODESystem
param1::T1 = param1_default # optional field
param2::T2 = param2_default # optional field
param3::T3 = param3_default # optional field
⋮
paramN::TN = paramN_default # optional field
righthandside::RH = righthandeside_function # mandatory field
readout::RO = readout_function # mandatory field
state::ST = state_default # mandatory field
input::IP = input_default # mandatory field
output::OP = output_default # mandatory field
endHere, MyODESystem has N parameters. MyODESystem is represented by the righthandside and readout function. state, input and output is the state, input port and output port of MyODESystem.
righthandside must have the signature
function righthandside(dx, x, u, t, args...; kwargs...)
dx .= .... # update dx
endand readout must have the signature
function readout(x, u, t)
y = ...
return y
endNew ODE system must be a subtype of AbstractODESystem to function properly.
New ODE system must be mutable type.
Example
julia> @def_ode_system mutable struct MyODESystem{RH, RO, IP, OP} <: AbstractODESystem
α::Float64 = 1.
β::Float64 = 2.
righthandside::RH = (dx, x, u, t, α=α) -> (dx[1] = α * x[1] + u[1](t))
readout::RO = (x, u, t) -> x
state::Vector{Float64} = [1.]
input::IP = Inport(1)
output::OP = Outport(1)
end
julia> ds = MyODESystem();
julia> ds.input
1-element Inport{Inpin{Float64}}:
Inpin(eltype:Float64, isbound:false)Causal.ODESystem — TypeODESystem(;righthandside, readout, state, input, output)Constructs a generic ODE system.
Example
julia> ds = ODESystem(righthandside=(dx,x,u,t)->(dx.=-x), readout=(x,u,t)->x, state=[1.],input=nothing, output=Outport(1));
julia> ds.state
1-element Array{Float64,1}:
1.0Causal.ContinuousLinearSystem — TypeContinuousLinearSystem(input, output, modelargs=(), solverargs=();
A=fill(-1, 1, 1), B=fill(0, 1, 1), C=fill(1, 1, 1), D=fill(0, 1, 1), state=rand(size(A,1)), t=0.,
alg=ODEAlg, modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a ContinuousLinearSystem with input and output. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
The ContinuousLinearSystem is represented by the following state and output equations.
where $x$ is state. solver is used to solve the above differential equation.
Causal.LorenzSystem — TypeLorenzSystem(input, output, modelargs=(), solverargs=();
sigma=10, beta=8/3, rho=28, gamma=1, outputfunc=allstates, state=rand(3), t=0.,
alg=ODEAlg, cplmat=diagm([1., 1., 1.]), modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a LorenzSystem with input and output. sigma, beta, rho and gamma is the system parameters. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
If input is nothing, the state equation of LorenzSystem is
where $x$ is state. solver is used to solve the above differential equation. If input is not nothing, then the state eqaution is
where $A = [\alpha_{ij}]$ is cplmat and $u = [u_{j}]$ is the value of the input. The output function is
where $t$ is time t, $y$ is the value of the output and $g$ is outputfunc.
Causal.ForcedLorenzSystem — TypeForcedLorenzSystem()Constructs a LorenzSystem that is driven by its inputs.
Causal.ChenSystem — TypeChenSystem(input, output, modelargs=(), solverargs=();
a=35, b=3, c=28, gamma=1, outputfunc=allstates, state=rand(3), t=0.,
alg=ODEAlg, cplmat=diagm([1., 1., 1.]), modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a ChenSystem with input and output. a, b, c and gamma is the system parameters. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
If input is nothing, the state equation of ChenSystem is
where $x$ is state. solver is used to solve the above differential equation. If input is not nothing, then the state eqaution is
where $A = [\alpha_{ij}]$ is cplmat and $u = [u_{j}]$ is the value of the input. The output function is
where $t$ is time t, $y$ is the value of the output and $g$ is outputfunc.
Causal.ForcedChenSystem — TypeForcedChenSystem()Constructs Chen system driven by its inputs.
Causal.ChuaSystem — TypeChuaSystem(input, output, modelargs=(), solverargs=();
diode=PiecewiseLinearDiode(), alpha=15.6, beta=28., gamma=1., outputfunc=allstates, state=rand(3), t=0.,
alg=ODEAlg, cplmat=diagm([1., 1., 1.]), modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a ChuaSystem with input and output. diode, alpha, beta and gamma is the system parameters. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
If input is nothing, the state equation of ChuaSystem is
where $x$ is state. solver is used to solve the above differential equation. If input is not nothing, then the state eqaution is
where $\Theta = [\theta_{ij}]$ is cplmat and $u = [u_{j}]$ is the value of the input. The output function is
where $t$ is time t, $y$ is the value of the output and $g$ is outputfunc.
Causal.ForcedChuaSystem — TypeForcedChuaSystem()Constructs a Chua system with inputs.
Causal.RosslerSystem — TypeRosslerSystem(input, output, modelargs=(), solverargs=();
a=0.38, b=0.3, c=4.82, gamma=1., outputfunc=allstates, state=rand(3), t=0.,
alg=ODEAlg, cplmat=diagm([1., 1., 1.]), modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a RosllerSystem with input and output. a, b, c and gamma is the system parameters. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
If input is nothing, the state equation of RosslerSystem is
where $x$ is state. solver is used to solve the above differential equation. If input is not nothing, then the state eqaution is
where $\Theta = [\theta_{ij}]$ is cplmat and $u = [u_{j}]$ is the value of the input. The output function is
where $t$ is time t, $y$ is the value of the output and $g$ is outputfunc.
Causal.ForcedRosslerSystem — TypeForcedRosslerSystem()Constructs a Rossler system driven by its input.
Causal.VanderpolSystem — TypeVanderpolSystem(input, output, modelargs=(), solverargs=();
mu=5., gamma=1., outputfunc=allstates, state=rand(2), t=0.,
alg=ODEAlg, cplmat=diagm([1., 1]), modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a VanderpolSystem with input and output. mu and gamma is the system parameters. state is the initial state and t is the time. modelargs and modelkwargs are passed into ODEProblem and solverargs and solverkwargs are passed into solve method of DifferentialEquations. alg is the algorithm to solve the differential equation of the system.
If input is nothing, the state equation of VanderpolSystem is
where $x$ is state. solver is used to solve the above differential equation. If input is not nothing, then the state eqaution is
where $\Theta = [\theta_{ij}]$ is cplmat and $u = [u_{j}]$ is the value of the input. The output function is
where $t$ is time t, $y$ is the value of the output and $g$ is outputfunc.
Causal.ForcedVanderpolSystem — TypeForcedVanderpolSystem()Constructs a Vanderpol system driven by its input.
Causal.Integrator — TypeIntegrator(state=zeros(0), t=0., modelargs=(), solverargs=();
alg=ODEAlg, modelkwargs=NamedTuple(), solverkwargs=NamedTuple(), numtaps=numtaps, callbacks=nothing,
name=Symbol())Constructs an integrator whose input output relation is given by
where $u(t)$ is the input, $y(t)$ is the output and $ki$ is the integration constant.