Skip to content

Commit

Permalink
add some relevant model checks to test_utils from PSI
Browse files Browse the repository at this point in the history
  • Loading branch information
jerrypotts committed Oct 31, 2024
1 parent 53291c7 commit 962d9ef
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const PSINV = PowerSystemsInvestments
const PSY = PowerSystems

include("test_utils/test_data.jl")
include("test_utils/model_checks.jl")

import Aqua
Aqua.test_unbound_args(PowerSystemsInvestments)
Expand Down
103 changes: 103 additions & 0 deletions test/test_utils/model_checks.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
const GAEVF = JuMP.GenericAffExpr{Float64, VariableRef}
const GQEVF = JuMP.GenericQuadExpr{Float64, VariableRef}

function moi_tests(
model::InvestmentModel,
vars::Int,
interval::Int,
lessthan::Int,
greaterthan::Int,
equalto::Int,
binary::Bool,
)
JuMPmodel = PSIN.get_jump_model(model)
@test JuMP.num_variables(JuMPmodel) == vars
@test JuMP.num_constraints(JuMPmodel, GAEVF, MOI.Interval{Float64}) == interval
@test JuMP.num_constraints(JuMPmodel, GAEVF, MOI.LessThan{Float64}) == lessthan
@test JuMP.num_constraints(JuMPmodel, GAEVF, MOI.GreaterThan{Float64}) == greaterthan
@test JuMP.num_constraints(JuMPmodel, GAEVF, MOI.EqualTo{Float64}) == equalto
@test ((JuMP.VariableRef, MOI.ZeroOne) in JuMP.list_of_constraint_types(JuMPmodel)) ==
binary

return
end

function psin_constraint_test(
model::InvestmentModel,
constraint_keys::Vector{<:PSIN.ConstraintKey},
)
constraints = PSIN.get_constraints(model)
for con in constraint_keys
if get(constraints, con, nothing) !== nothing
@test true
else
@error con
@test false
end
end
return
end

function psin_checksolve_test(model::InvestmentModel, status, expected_result, tol = 0.0)
res = solve!(model)
model = PSIN.get_jump_model(model)
@test termination_status(model) in status
obj_value = JuMP.objective_value(model)
@test isapprox(obj_value, expected_result, atol = tol)
end

function check_variable_unbounded(
model::InvestmentModel,
::Type{T},
::Type{U},
) where {T <: PSIN.VariableType, U <: PSIP.Technology}
return check_variable_unbounded(model::InvestmentModel, PSIN.VariableKey(T, U))
end

function check_variable_unbounded(model::InvestmentModel, var_key::PSIN.VariableKey)
psi_cont = PSIN.get_optimization_container(model)
variable = PSIN.get_variable(psi_cont, var_key)
for var in variable
if JuMP.has_lower_bound(var) || JuMP.has_upper_bound(var)
return false
end
end
return true
end

function check_variable_bounded(
model::InvestmentModel,
::Type{T},
::Type{U},
) where {T <: PSIN.VariableType, U <: PSIP.Technology}
return check_variable_bounded(model, PSIN.VariableKey(T, U))
end

function check_variable_bounded(model::InvestmentModel, var_key::PSIN.VariableKey)
psi_cont = PSIN.get_optimization_container(model)
variable = PSIN.get_variable(psi_cont, var_key)
for var in variable
if !JuMP.has_lower_bound(var) || !JuMP.has_upper_bound(var)
return false
end
end
return true
end

function check_flow_variable_values(
model::InvestmentModel,
::Type{T},
::Type{U},
device_name::String,
limit::Float64,
) where {T <: PSIN.VariableType, U <: PSIP.Technology}
psi_cont = PSIN.get_optimization_container(model)
variable = PSIN.get_variable(psi_cont, T(), U)
for var in variable[device_name, :]
if !(PSIN.jump_value(var) <= (limit + 1e-2))
@error "$device_name out of bounds $(PSIN.jump_value(var))"
return false
end
end
return true
end

0 comments on commit 962d9ef

Please sign in to comment.