Skip to content

Commit

Permalink
Add option to suppress unused varshks warning (#62)
Browse files Browse the repository at this point in the history
* Add option to suppress unused varshks warning
  • Loading branch information
jasonjensen authored May 14, 2024
1 parent 114c5d6 commit f281071
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
17 changes: 13 additions & 4 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1405,7 +1405,7 @@ function initialize!(model::Model, modelmodule::Module)
# if dynss is true, then we need the steady state even for the standard MED
nothing
end
unused = get_unused_symbols(model)
unused = get_unused_symbols(model; filter_known_unused=true)
if length(unused[:variables]) > 0
@warn "Model contains unused variables: $(unused[:variables])"
end
Expand Down Expand Up @@ -1458,7 +1458,7 @@ function reinitialize!(model::Model, modelmodule::Module=moduleof(model))
# if dynss is true, then we need the steady state even for the standard MED
nothing
end
unused = get_unused_symbols(model)
unused = get_unused_symbols(model; filter_known_unused=true)
if length(unused[:variables]) > 0
@warn "Model contains unused variables: $(unused[:variables])"
end
Expand Down Expand Up @@ -1867,17 +1867,26 @@ function replace_in_expr(e::Expr, old::Model, new::Union{Symbol,Expr}, params::P
end

"""
get_unused_symbols(model::Model)
get_unused_symbols(model::Model; filter_known_unused=false)
Returns a dictionary with vectors of the unused variables, shocks, and parameters.
Keyword arguments:
* filter_known_unused::Bool - When `true`, the results will exclude variables present in model.option.unused_varshks.
The default is `false`.
"""
function get_unused_symbols(model::Model)
function get_unused_symbols(model::Model; filter_known_unused::Bool=false)
eqmap = equation_map(model)
unused = Dict(
:variables => filter(x -> !haskey(eqmap, x), [x.name for x in model.variables]),
:shocks => filter(x -> !haskey(eqmap, x), [x.name for x in model.shocks]),
:parameters => filter(x -> !haskey(eqmap, x), collect(keys(model.parameters)))
)
if filter_known_unused && :unused_varshks model.options
for k in (:variables, :shocks)
unused[k] = filter(x -> x model.options.unused_varshks, unused[k])
end
end
return unused
end
export get_unused_symbols
33 changes: 24 additions & 9 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1435,15 +1435,6 @@ end
@test length(m.equations) == 3
@test collect(keys(m.equations)) == [:_EQ1, :_EQ3, :_EQ4]

m = S1.newmodel()
@equations m begin
@delete _EQ1
end
@steadystate m begin
@delete _SSEQ1
end
@test_logs (:warn, "Model contains unused variables: [:a]") @reinitialize m


maux = deepcopy(AUX.model)
@test length(maux.equations) == 2
Expand All @@ -1458,6 +1449,30 @@ end
end
@test length(maux.equations) == 2
@test length(maux.alleqns) == 4

# option to not show a warning
m = S1.newmodel()

@equations m begin
@delete _EQ2
end

@test length(m.equations) == 2
@test collect(keys(m.equations)) == [:_EQ1, :_EQ3]
m.options.unused_varshks = [:b_shk]

@test_logs @reinitialize m

# option to not show a warning
m = S1.newmodel()
@equations m begin
@delete _EQ1
end
@steadystate m begin
@delete _SSEQ1
end
m.options.unused_varshks = [:a]
@test_logs @reinitialize m
end

@using_example E2sat
Expand Down

0 comments on commit f281071

Please sign in to comment.