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

Add option to suppress unused varshks warning #62

Merged
merged 2 commits into from
May 14, 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
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
Loading