Skip to content

Commit 292fd10

Browse files
authored
Merge pull request #39 from control-toolbox/36-bug-variable-time-of-wrong-dimension
36 bug variable time of wrong dimension
2 parents abb871c + 1701cd9 commit 292fd10

2 files changed

Lines changed: 17 additions & 27 deletions

File tree

src/onepass.jl

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ $(TYPEDEF)
3636
tf::Union{Real,Symbol,Expr,Nothing} = nothing
3737
x::Union{Symbol,Nothing} = nothing
3838
u::Union{Symbol,Nothing} = nothing
39+
is_scalar_v::Bool = false
3940
is_scalar_x::Bool = false # todo: remove in future, when allowing componentwise declaration of dynamics
4041
aliases::OrderedDict{Union{Symbol,Expr},Union{Real,Symbol,Expr}} = __init_aliases() # Dict ordered by Symbols *and Expr* just for scalar variable / state / control
4142
lnum::Int = 0
@@ -247,6 +248,7 @@ function p_variable!(p, p_ocp, v, q; components_names=nothing, log=false)
247248
p.aliases[Symbol(v, CTBase.ctindices(1))] = :($vg[1])
248249
p.aliases[Symbol(v, 1)] = :($vg[1])
249250
v = vg
251+
p.is_scalar_v = true
250252
end
251253
p.v = v
252254
qq = q isa Int ? q : 9
@@ -282,27 +284,15 @@ function p_time!(p, p_ocp, t, t0, tf; log=false)
282284
code = @match (has(t0, p.v), has(tf, p.v)) begin
283285
(false, false) => :($prefix.time!($p_ocp; t0=$t0, tf=$tf, time_name=$tt))
284286
(true, false) => @match t0 begin
285-
:($v1[$i]) && if (v1 == p.v)
286-
end => :($prefix.time!($p_ocp; ind0=$i, tf=$tf, time_name=$tt))
287-
:($v1) && if (v1 == p.v)
288-
end => quote
289-
($p_ocp.variable_dimension 1) && throw( # todo: add info (dim of var) in PreModel
290-
$e_prefix.ParsingError("variable must be of dimension one for a time"),
291-
)
292-
$prefix.time!($p_ocp; ind0=1, tf=$tf, time_name=$tt)
293-
end
287+
:($v1[$i]) && if (v1 == p.v) end => :($prefix.time!($p_ocp; ind0=$i, tf=$tf, time_name=$tt))
288+
:($v1) && if (v1 == p.v) && p.is_scalar_v end => :( $prefix.time!($p_ocp; ind0=1, tf=$tf, time_name=$tt) )
289+
:($v1) && if (v1 == p.v) && !p.is_scalar_v end => return __throw("variable must be of dimension one for a time", p.lnum, p.line)
294290
_ => return __throw("bad time declaration", p.lnum, p.line)
295291
end
296292
(false, true) => @match tf begin
297-
:($v1[$i]) && if (v1 == p.v)
298-
end => :($prefix.time!($p_ocp; t0=$t0, indf=$i, time_name=$tt))
299-
:($v1) && if (v1 == p.v)
300-
end => quote
301-
($p_ocp.variable_dimension 1) && throw( # todo: add info (dim of var) in PreModel
302-
$e_prefix.ParsingError("variable must be of dimension one for a time"),
303-
)
304-
$prefix.time!($p_ocp; t0=$t0, indf=1, time_name=$tt)
305-
end
293+
:($v1[$i]) && if (v1 == p.v) end => :($prefix.time!($p_ocp; t0=$t0, indf=$i, time_name=$tt))
294+
:($v1) && if (v1 == p.v) && p.is_scalar_v end => :( $prefix.time!($p_ocp; t0=$t0, indf=1, time_name=$tt) )
295+
:($v1) && if (v1 == p.v) && !p.is_scalar_v end => return __throw("variable must be of dimension one for a time", p.lnum, p.line)
306296
_ => return __throw("bad time declaration", p.lnum, p.line)
307297
end
308298
_ => @match (t0, tf) begin

test/test_onepass.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -369,15 +369,15 @@ function test_onepass()
369369
@test initial_time(o, [1, 2]) == 1
370370
@test final_time(o, [1, 2]) == 2
371371

372-
## @test_throws ParsingError @def o begin # debug: check time! in onepass, then uncomment
373-
## t0 ∈ R², variable
374-
## t ∈ [t0, 1], time
375-
## end
376-
##
377-
## @test_throws ParsingError @def o begin # debug: check time! in onepass, then uncomment
378-
## tf ∈ R², variable
379-
## t ∈ [0, tf], time
380-
## end
372+
@test_throws ParsingError @def o begin # debug: check time! in onepass, then uncomment
373+
t0 R², variable
374+
t [t0, 1], time
375+
end
376+
377+
@test_throws ParsingError @def o begin # debug: check time! in onepass, then uncomment
378+
tf R², variable
379+
t [0, tf], time
380+
end
381381

382382
@test_throws ParsingError @def o begin
383383
v, variable

0 commit comments

Comments
 (0)