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 Jusdl # 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(sfunc, ofunc, [1.], 0., Inport(1), Outport(1))
ODESystem(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) @0x00007fcdecd4b5b0
julia> task2 = @async while true
all(take!(iport) .=== NaN) && break
end
Task (runnable) @0x00007fcdf4935600When 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) @0x00007fcdecd4b5b0
julia> task2
Task (runnable) @0x00007fcdf4935600To 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) @0x00007fcdecd4b5b0
julia> task2
Task (done) @0x00007fcdf4935600So, 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
Jusdl.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.
Jusdl.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.
Jusdl.LinearSystem — TypeLinearSystem(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 LinearSystem 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 LinearSystem is represented by the following state and output equations.
where $x$ is state. solver is used to solve the above differential equation.
Jusdl.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.
Jusdl.ODESystem — TypeODESystem(input, output, statefunc, outputfunc, state, t, modelargs=(), solverargs=();
alg=ODEAlg, modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs an ODESystem with input and output. statefunc is the state function and outputfunc is the output function. 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.
ODESystem is represented by the equations.
where $t$ is the time t, $x$ is state, $u$ is the value of input, $y$ is the value of output. $f$ is statefunc and $g$ is outputfunc. solver is used to solve the above differential equation.
The signature of statefunc must be of the form,
function statefunc(dx, x, u, t)
dx .= ... # Update dx
endand the signature of outputfunc must be of the form,
function outputfunc(x, u, t)
y = ... # Compute y
return y
endExample
julia> sfuncode(dx,x,u,t) = (dx[1] = 0.5x[1] + u[1](t));
julia> ofuncode(x, u, t) = x;
julia> ds = ODESystem(sfuncode, ofuncode, [1.], 0., Inport(), Outport())
ODESystem(state:[1.0], t:0.0, input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))
julia> ds = ODESystem(sfuncode, ofuncode, [1.], 0., Inport(), Outport(), solverkwargs=(dt=0.1, reltol=1e-6))
ODESystem(state:[1.0], t:0.0, input:Inport(numpins:1, eltype:Inpin{Float64}), output:Outport(numpins:1, eltype:Outpin{Float64}))See DifferentialEquations for more information about modelargs, modelkwargs, solverargs, solverkwargs and alg.
Jusdl.RODESystem — TypeRODESystem(input, output, statefunc, outputfunc, state, t, modelargs=(), solverargs=();
alg=RODEAlg, modelkwargs=NamedTuple(), solverkwargs=NamedTuple())Constructs a RODESystem with input and output. statefunc is the state function and outputfunc is the output function. 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 RODESystem is represented by the equations,
where $x$ is the state, $u$ is the value of input, $y$ the value of output, ant $t$ is the time t. $f$ is the statefunc and $g$ is the outputfunc. $W$ is the Wiene process. noise is the noise of the system and solver is used to solve the above differential equation.
The signature of statefunc must be of the form
function statefunc(dx, x, u, t, W)
dx .= ... # Update dx
endand the signature of outputfunc must be of the form
function outputfunc(x, u, t)
y = ... # Compute y
return y
endExample
julia> function sfuncrode(dx, x, u, t, W)
dx[1] = 2x[1]*sin(W[1] - W[2])
dx[2] = -2x[2]*cos(W[1] + W[2])
end;
julia> ofuncrode(x, u, t) = x;
julia> RODESystem(sfuncrode, ofuncrode, [1., 1.], 0., nothing, Outport(2), solverkwargs=(dt=0.01,))
RODESystem(state:[1.0, 1.0], t:0.0, input:nothing, output:Outport(numpins:2, eltype:Outpin{Float64}))See DifferentialEquations for more information about modelargs, modelkwargs, solverargs solverkwargs and alg.
Jusdl.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.
Jusdl.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.