Private functions
CTModels
CTParser.CTParser — ModuleCTParser module.
Lists all the imported modules and packages:
BaseDocStringExtensionsMLStyleOrderedCollectionsParametersUnicode
List of all the exported names:
Index
CTParser.ParsingInfoCTParser.activate_backendCTParser.concatCTParser.constraint_typeCTParser.deactivate_backendCTParser.def_exaCTParser.def_funCTParser.expr_itCTParser.hasCTParser.hasCTParser.is_active_backendCTParser.parse!CTParser.parsingCTParser.replace_callCTParser.replace_callCTParser.subsCTParser.subs2CTParser.subs3CTParser.subs4CTParser.subs5CTParser.@def
Documentation
CTParser.ParsingInfo — Typemutable struct ParsingInfoFields
CTParser.activate_backend — Methodactivate_backend(backend)
Activate parsing backend. Possible choices: :exa.
CTParser.concat — Methodconcat(e1, e2) -> Expr
Concatenate two expressions without creating extra blocks (as Expr(:block, e1, e2) would do).
Example
julia> e1 = :(x = 1; y = 2)
quote
x = 1
#= REPL[3]:1 =#
y = 2
end
julia> e2 = :(z = 3)
:(z = 3)
julia> concat(e1, e2)
quote
x = 1
#= REPL[3]:1 =#
y = 2
z = 3
end
julia> concat(e1, e1)
quote
x = 1
#= REPL[3]:1 =#
y = 2
x = 1
#= REPL[3]:1 =#
y = 2
endCTParser.constraint_type — Methodconstraint_type(
e,
t,
t0,
tf,
x,
u,
v
) -> Union{Symbol, Tuple{Symbol, Any}}
Return the type constraint among :initial, :final, :boundary, :control_range, :control_fun, :state_range, :state_fun, :mixed, :variable_range, :variable_fun (:other otherwise), together with the appropriate value (range, updated expression...) Expressions like u(t0) where u is the control and t0 the initial time return :other.
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u; v = :v
julia> constraint_type(:( ẏ(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( ẋ(s) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( x(0)' ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x(t)' ), t, t0, tf, x, u, v)
:state_fun
julia> constraint_type(:( x(0) ), t, t0, tf, x, u, v)
(:initial, nothing)
julia> constraint_type(:( x[1:2:5](0) ), t, t0, tf, x, u, v)
(:initial, 1:2:5)
julia> constraint_type(:( x[1:2](0) ), t, t0, tf, x, u, v)
(:initial, 1:2)
julia> constraint_type(:( x[1](0) ), t, t0, tf, x, u, v)
(:initial, 1)
julia> constraint_type(:( 2x[1](0)^2 ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x(tf) ), t, t0, tf, x, u, v)
(:final, nothing)
j
julia> constraint_type(:( x[1:2:5](tf) ), t, t0, tf, x, u, v)
(:final, 1:2:5)
julia> constraint_type(:( x[1:2](tf) ), t, t0, tf, x, u, v)
(:final, 1:2)
julia> constraint_type(:( x[1](tf) ), t, t0, tf, x, u, v)
(:final, 1)
julia> constraint_type(:( 2x[1](tf)^2 ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( x[1](tf) - x[2](0) ), t, t0, tf, x, u, v)
:boundary
julia> constraint_type(:( u[1:2:5](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2:5)
julia> constraint_type(:( u[1:2](t) ), t, t0, tf, x, u, v)
(:control_range, 1:2)
julia> constraint_type(:( u[1](t) ), t, t0, tf, x, u, v)
(:control_range, 1)
julia> constraint_type(:( u(t) ), t, t0, tf, x, u, v)
(:control_range, nothing)
julia> constraint_type(:( 2u[1](t)^2 ), t, t0, tf, x, u, v)
:control_fun
julia> constraint_type(:( x[1:2:5](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2:5)
julia> constraint_type(:( x[1:2](t) ), t, t0, tf, x, u, v)
(:state_range, 1:2)
julia> constraint_type(:( x[1](t) ), t, t0, tf, x, u, v)
(:state_range, 1)
julia> constraint_type(:( x(t) ), t, t0, tf, x, u, v)
(:state_range, nothing)
julia> constraint_type(:( 2x[1](t)^2 ), t, t0, tf, x, u, v)
:state_fun
julia> constraint_type(:( 2u[1](t)^2 * x(t) ), t, t0, tf, x, u, v)
:mixed
julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( 2u[1](0)^2 * x(t) ), t, t0, tf, x, u, v)
:other
julia> constraint_type(:( 2u[1](t)^2 * x(t) + v ), t, t0, tf, x, u, v)
:mixed
julia> constraint_type(:( v[1:2:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:2:9)
julia> constraint_type(:( v[1:10] ), t, t0, tf, x, u, v)
(:variable_range, 1:10)
julia> constraint_type(:( v[2] ), t, t0, tf, x, u, v)
(:variable_range, 2)
julia> constraint_type(:( v ), t, t0, tf, x, u, v)
(:variable_range, nothing)
julia> constraint_type(:( v^2 + 1 ), t, t0, tf, x, u, v)
:variable_fun
julia> constraint_type(:( v[2]^2 + 1 ), t, t0, tf, x, u, v)
:variable_funCTParser.deactivate_backend — Methoddeactivate_backend(backend)
Deactivate parsing backend. Possible choices: :exa.
CTParser.def_exa — Methoddef_exa(e; log) -> Expr
Core computation used to discretise, parsing an expression towards an ExaModels.ExaModel.
CTParser.def_fun — Methoddef_fun(e; log) -> Expr
Core computation of @def macro, parsing an expression towards a CTModels.Model.
CTParser.expr_it — Methodexpr_it(e, _Expr, f) -> Any
Expr iterator: apply _Expr to nodes and f to leaves of the AST.
Example
julia> id(e) = expr_it(e, Expr, x -> x)CTParser.has — Methodhas(e, x, t) -> Union{Missing, Bool}
Return true if e contains a (...x...)(t) call.
Example
julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)
julia> has(e, :x, :t)
true
julia> has(e, :u, :t)
trueCTParser.has — Methodhas(e, e1) -> Union{Missing, Bool}
Return true if e contains e1.
Example
julia> e = :( ∫( x[1](t)^2 + 2*u(t) ) → min )
:(∫((x[1])(t) ^ 2 + 2 * u(t)) → min)
julia> has(e, 2)
true
julia> has(e, :x)
true
julia> has(e, :min)
true
julia> has(e, :( x[1](t)^2 ))
true
julia> !has(e, :( x[1](t)^3 ))
true
julia> !has(e, 3)
true
julia> !has(e, :max)
true
julia> has(:x, :x)
true
julia> !has(:x, 2)
true
julia> !has(:x, :y)
trueCTParser.is_active_backend — Methodis_active_backend(backend) -> Bool
Check whether backend is active or not.
CTParser.parse! — Methodparse!(p, p_ocp, e; log, backend) -> Any
Parse the expression e and update the ParsingInfo structure p.
Example
parse!(p, :p_ocp, :(v ∈ R, variable))CTParser.parsing — Methodparsing(s, backend) -> Function
Call the primitive associated with symbol s (:alias, etc.) for the associated backend.
CTParser.replace_call — Methodreplace_call(e, x::Symbol, t, y) -> Any
Replace calls in e of the form (...x...)(t) by (...y...).
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;
julia> e = :( x[1](0) * 2x(tf) - x[2](tf) * 2x(0) )
:((x[1])(0) * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))
julia> x0 = Symbol(x, 0); e = replace_call(e, x, t0, x0)
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2x0))
julia> xf = Symbol(x, "f"); replace_call(ans, x, tf, xf)
:(x0[1] * (2xf) - xf[2] * (2x0))
julia> e = :( A*x(t) + B*u(t) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(A * x + B * u)
julia> e = :( F0(x(t)) + u(t)*F1(x(t)) ); replace_call(replace_call(e, x, t, x), u, t, u)
:(F0(x) + u * F1(x))
julia> e = :( 0.5u(t)^2 ); replace_call(e, u, t, u)
:(0.5 * u ^ 2)CTParser.replace_call — Methodreplace_call(e, x::Vector{Symbol}, t, y) -> Any
Replace calls in e of the form (...x1...x2...)(t) by (...y1...y2...) for all symbols x1, x2... in the vector x.
Example
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;
julia> e = :( (x^2 + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])
julia> e = :( ((x^2)(t) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:(xx ^ 2 + uu[1])
julia> e = :( ((x^2)(t0) + u[1])(t) ); replace_call(e, [ x, u ], t , [ :xx, :uu ])
:((xx ^ 2)(t0) + uu[1])CTParser.subs — Methodsubs(e, e1::Union{Real, Symbol}, e2) -> Any
Substitute expression e1 by expression e2 in expression e.
Examples
julia> e = :( ∫( r(t)^2 + 2u₁(t)) → min )
:(∫(r(t) ^ 2 + 2 * u₁(t)) → min)
julia> subs(e, :r, :( x[1] ))
:(∫((x[1])(t) ^ 2 + 2 * u₁(t)) → min)
julia> e = :( ∫( u₁(t)^2 + 2u₂(t)) → min )
:(∫(u₁(t) ^ 2 + 2 * u₂(t)) → min)
julia> for i ∈ 1:2
e = subs(e, Symbol(:u, Char(8320+i)), :( u[$i] ))
end; e
:(∫((u[1])(t) ^ 2 + 2 * (u[2])(t)) → min)
julia> t = :t; t0 = 0; tf = :tf; x = :x; u = :u;
julia> e = :( x[1](0) * 2x(tf) - x[2](tf) * 2x(0) )
:((x[1])(0) * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))
julia> x0 = Symbol(x, 0); subs(e, :( $x[1]($(t0)) ), :( $x0[1] ))
:(x0[1] * (2 * x(tf)) - (x[2])(tf) * (2 * x(0)))CTParser.subs2 — Methodsubs2(e, x, y, j) -> Any
Substitute x[i] by y[i, j], whatever i, in e. See also: subs5.
Examples
julia> e = :(x0[1] * 2xf[3] - cos(xf[2]) * 2x0[2])
:(x0[1] * (2 * xf[3]) - cos(xf[2]) * (2 * x0[2]))
julia> subs2(subs2(e, :x0, :x, 0), :xf, :x, :N)
:(x[1, 0] * (2 * x[3, N]) - cos(x[2, N]) * (2 * x[2, 0]))
julia> e = :(x0 * 2xf[3] - cos(xf) * 2x0[2])
:(x0 * (2 * xf[3]) - cos(xf) * (2 * x0[2]))
julia> subs2(subs2(e, :x0, :x, 0), :xf, :x, :N)
:(x0 * (2 * x[3, N]) - cos(xf) * (2 * x[2, 0]))CTParser.subs3 — Methodsubs3(e, x, y, i, j) -> Any
Substitute x[rg] by y[i, j], whatever rg, in e.
Examples
julia> e = :(x0[1:2:d] * 2xf[1:3])
:(x0[1:2:d] * (2 * xf[1:3]))
julia> subs3(e, :x0, :x, :i, 0)
:(x[i, 0] * (2 * xf[1:3]))
julia> subs3(e, :xf, :x, 1, :N)
:(x0[1:2:d] * (2 * x[1, N]))CTParser.subs4 — Methodsubs4(e, x, y, i) -> Any
Substitute x[rg] by y[i], whatever rg, in e.
Examples
julia> e = :(v[1:2:d] * 2xf[1:3])
:(v[1:2:d] * (2 * xf[1:3]))
julia> subs4(e, :v, :v, :i)
:(v[i] * (2 * xf[1:3]))
julia> subs4(e, :xf, :xf, 1)
:(v[1:2:d] * (2 * xf[1]))CTParser.subs5 — Methodsubs5(e, x, y, j) -> Any
Substitute x[i] by (y[i, j] + y[i, j + 1]) / 2, whatever i, in e. See also: subs2.
Examples
julia> e = :(x0[1] * 2xf[3] - cos(xf[2]) * 2x0[2])
:(x0[1] * (2 * xf[3]) - cos(xf[2]) * (2 * x0[2]))
julia> subs5(subs5(e, :x0, :x, 0), :xf, :x, :N)
:(((x[1, 0] + x[1, 0 + 1]) / 2) * (2 * ((x[3, N] + x[3, N + 1]) / 2)) - cos((x[2, N] + x[2, N + 1]) / 2) * (2 * ((x[2, 0] + x[2, 0 + 1]) / 2)))
julia> e = :(x0 * 2xf[3] - cos(xf) * 2x0[2])
:(x0 * (2 * xf[3]) - cos(xf) * (2 * x0[2]))
julia> subs5(subs5(e, :x0, :x, 0), :xf, :x, :N)
:(x0 * (2 * ((x[3, N] + x[3, N + 1]) / 2)) - cos(xf) * (2 * ((x[2, 0] + x[2, 0 + 1]) / 2)))CTParser.@def — MacroDefine an optimal control problem. One pass parsing of the definition. Can be used writing either ocp = @def begin ... end or @def ocp begin ... end. In the second case, setting log to true will display the parsing steps.
Example
ocp = @def begin
tf ∈ R, variable
t ∈ [ 0, tf ], time
x ∈ R², state
u ∈ R, control
tf ≥ 0
-1 ≤ u(t) ≤ 1
q = x₁
v = x₂
q(0) == 1
v(0) == 2
q(tf) == 0
v(tf) == 0
0 ≤ q(t) ≤ 5, (1)
-2 ≤ v(t) ≤ 3, (2)
ẋ(t) == [ v(t), u(t) ]
tf → min
end
@def ocp begin
tf ∈ R, variable
t ∈ [ 0, tf ], time
x ∈ R², state
u ∈ R, control
tf ≥ 0
-1 ≤ u(t) ≤ 1
q = x₁
v = x₂
q(0) == 1
v(0) == 2
q(tf) == 0
v(tf) == 0
0 ≤ q(t) ≤ 5, (1)
-2 ≤ v(t) ≤ 3, (2)
ẋ(t) == [ v(t), u(t) ]
tf → min
end true # final boolean to show parsing log