Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix two problems with if-statements in equations #71

Merged
merged 1 commit into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading