Skip to content

Commit

Permalink
Fix two problems with if-statements in equations (#71)
Browse files Browse the repository at this point in the history
fix #70
  • Loading branch information
bbejanov authored Jul 8, 2024
1 parent 9b066c2 commit aab0a16
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
4 changes: 2 additions & 2 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1079,12 +1079,12 @@ function process_equation(model::Model, expr::Expr;
filter!(!isnothing, args)
if ex.head == :if
if length(args) == 3
return Expr(:call, :if, args...)
return Expr(:if, args...)
else
error_process("Unable to process an `if` statement with a single branch. Use function `ifelse` instead.", expr, modelmodule)
end
end
if ex.head (:call, :(&&), :(||))
if ex.head (:call, :comparison, :(&&), :(||))
return Expr(ex.head, args...)
end
if ex.head == :block && length(args) == 1
Expand Down
26 changes: 25 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,30 @@ end
end), eqn_name=:_EQ2) isa Equation
end

@testset "ifelse_eval" begin
# this addresses issue #70
@test let model = Model()
@variables model a
@parameters model cond = true
@equations model begin
:A => a[t] = cond ? 1.0 : -1.0
end
@initialize model
r, j = eval_RJ(zeros(1,1), model)
r == [-1.0] && j == [1.0;;]
end
@test let model = Model()
@variables model b
@parameters model p = 0.5
@equations model begin
:B => b[t] = (0.0 <= p <= 1.0)
end
@initialize model
r, j = eval_RJ(zeros(1,1), model)
r == [-1.0] && j == [1.0;;]
end
end

@testset "Meta" begin
mod = Model()
@parameters mod a = 0.1 b = @link(1.0 - a)
Expand Down Expand Up @@ -1526,7 +1550,7 @@ end

@using_example E2sat
m2_for_sattelite_tests = E2sat.newmodel()
@testset "sattelite models" begin
@testset "satellite models" begin
m1 = E2.newmodel()

m_sattelite = Model()
Expand Down

0 comments on commit aab0a16

Please sign in to comment.