Skip to content

Commit

Permalink
Merge remote-tracking branch 'Luca/develop' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
lbonaldo committed Oct 15, 2023
2 parents 8abbe4a + 5aa4c57 commit 415afb2
Show file tree
Hide file tree
Showing 12 changed files with 142 additions and 109 deletions.
28 changes: 15 additions & 13 deletions test/expression_manipulation_test.jl
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
using JuMP
using HiGHS

let
let
EP = Model(HiGHS.Optimizer)

# Test fill_with_zeros!
small_zeros_expr = Array{AffExpr,2}(undef,(2,3))
small_zeros_expr = Array{AffExpr,2}(undef, (2, 3))
GenX.fill_with_zeros!(small_zeros_expr)
@test small_zeros_expr == AffExpr.([0.0 0.0 0.0; 0.0 0.0 0.0])

# Test fill_with_const!
small_const_expr = Array{AffExpr,2}(undef,(3,2))
small_const_expr = Array{AffExpr,2}(undef, (3, 2))
GenX.fill_with_const!(small_const_expr, 6.0)
@test small_const_expr == AffExpr.([6.0 6.0; 6.0 6.0; 6.0 6.0])

# Test create_empty_expression! with fill_with_const!
large_dims = (2,10,20)
large_dims = (2, 10, 20)
GenX.create_empty_expression!(EP, :large_expr, large_dims)
@test all(EP[:large_expr] .== 0.0)

Expand All @@ -33,8 +33,11 @@ let
@test all(EP[:large_expr][:] .== 18.0)

# Test add_similar_to_expression! returns an error if the dimensions don't match
GenX.create_empty_expression!(EP, :small_expr, (2,3))
@test_throws ErrorException GenX.add_similar_to_expression!(EP[:large_expr], EP[:small_expr])
GenX.create_empty_expression!(EP, :small_expr, (2, 3))
@test_throws ErrorException GenX.add_similar_to_expression!(
EP[:large_expr],
EP[:small_expr],
)

# Test we can add variables to an expression using add_similar_to_expression!
@variable(EP, test_var[1:large_dims[1], 1:large_dims[2], 1:large_dims[3]] >= 0)
Expand All @@ -55,12 +58,12 @@ let
@test EP[:large_expr][100] == test_var[100] + 22.0 + single_var
end

###### ###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ###### ######
# Performance tests we can perhaps add later
# These require the BenchmarkTests.jl package
###### ###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ###### ######
# Performance tests we can perhaps add later
# These require the BenchmarkTests.jl package
###### ###### ###### ###### ###### ###### ######
###### ###### ###### ###### ###### ###### ######

# function test_performance(expr_dims)
# EP = Model(HiGHS.Optimizer)
Expand All @@ -76,4 +79,3 @@ end
# small_benchmark = test_performance((2,3))
# medium_benchmark = test_performance((2,10,20))
# large_benchmark = test_performance((2,20,40))

138 changes: 76 additions & 62 deletions test/resource_test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,68 +2,82 @@ let
Resource = GenX.GenXResource
check_resource = GenX.check_resource

therm = Resource(:Resource => "my_therm",
:THERM => 1,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0)

stor_lds = Resource(:Resource => "stor_lds",
:THERM => 0,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 1,
:LDS => 1)

hydro_lds = Resource(:Resource => "hydro_lds",
:THERM => 0,
:FLEX => 0,
:HYDRO => 1,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1)

bad_lds = Resource(:Resource => "bad lds combo",
:THERM => 0,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1)

bad_none = Resource(:Resource => "none",
:THERM => 0,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0)

bad_twotypes = Resource(:Resource => "too many",
:THERM => 1,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0)

bad_multiple = Resource(:Resource => "multiple_bad",
:THERM => 1,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1)
therm = Resource(
:Resource => "my_therm",
:THERM => 1,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0,
)

stor_lds = Resource(
:Resource => "stor_lds",
:THERM => 0,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 1,
:LDS => 1,
)

hydro_lds = Resource(
:Resource => "hydro_lds",
:THERM => 0,
:FLEX => 0,
:HYDRO => 1,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1,
)

bad_lds = Resource(
:Resource => "bad lds combo",
:THERM => 0,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1,
)

bad_none = Resource(
:Resource => "none",
:THERM => 0,
:FLEX => 0,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0,
)

bad_twotypes = Resource(
:Resource => "too many",
:THERM => 1,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 0,
)

bad_multiple = Resource(
:Resource => "multiple_bad",
:THERM => 1,
:FLEX => 1,
:HYDRO => 0,
:VRE => 0,
:MUST_RUN => 0,
:STOR => 0,
:LDS => 1,
)

function check_okay(resource)
e = check_resource(resource)
Expand Down
6 changes: 3 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ end
end

# Test GenX modules
@testset verbose=true "GenX modules" begin
@testset verbose = true "GenX modules" begin
@testset "Three zones" begin
include("test_threezones.jl")
end
Expand All @@ -33,7 +33,7 @@ end

@testset "PiecewiseFuel CO2" begin
include("test_piecewisefuel_CO2.jl")
end
end

@testset "VRE and storage" begin
include("test_VREStor.jl")
Expand All @@ -50,4 +50,4 @@ end
@testset "Multi Stage" begin
include("test_multistage.jl")
end
end
end
6 changes: 3 additions & 3 deletions test/simple_op_test.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@test simple_operation(2.0, 3.0) == 5
@test simple_operation(2.1, 3.1)==5.2
@test simple_operation(21.0, 31.0)== 52.0
@test simple_operation(73.0, 35.0)== 108.0
@test simple_operation(2.1, 3.1) == 5.2
@test simple_operation(21.0, 31.0) == 52.0
@test simple_operation(73.0, 35.0) == 108.0
5 changes: 2 additions & 3 deletions test/test_VREStor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ genx_setup = Dict(
"ParameterScale" => 1,
"Trans_Loss_Segments" => 1,
"CapacityReserveMargin" => 1,
"ModelingtoGenerateAlternativeSlack: 0" =>1,
"ModelingtoGenerateAlternativeSlack: 0" => 1,
"Solver" => "HiGHS",
"ModelingToGenerateAlternatives" => 0,
"WriteShadowPrices" => 1,
Expand All @@ -45,10 +45,9 @@ optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")
obj_test = round_objfromtol!(obj_test, optimal_tol)

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestVREStor

2 changes: 1 addition & 1 deletion test/test_electrolyzer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")
obj_test = round_objfromtol!(obj_test, optimal_tol)

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
write_testlog(test_path, obj_test, optimal_tol, test_result)
Expand Down
8 changes: 4 additions & 4 deletions test/test_multistage.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ test_path = "MultiStage"

# Define test inputs
multistage_setup = Dict(
"NumStages" => 3 ,
"StageLengths" => [10,10,10],
"NumStages" => 3,
"StageLengths" => [10, 10, 10],
"WACC" => 0.045,
"ConvergenceTolerance" => 0.01,
"Myopic" => 0,
Expand Down Expand Up @@ -48,8 +48,8 @@ EP, _, _ = redirect_stdout(devnull) do
run_genx_case_testing(test_path, genx_setup)
end

obj_test = objective_value.([EP[i] for i in 1:3])
optimal_tol = get_attribute.([EP[i] for i in 1:3], "dual_feasibility_tolerance")
obj_test = objective_value.([EP[i] for i = 1:3])
optimal_tol = get_attribute.([EP[i] for i = 1:3], "dual_feasibility_tolerance")

# Round the objective value to the same number of digits as the tolerance
obj_test = round_objfromtol!.(obj_test, optimal_tol)
Expand Down
3 changes: 1 addition & 2 deletions test/test_piecewisefuel_CO2.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,9 @@ optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")
obj_test = round_objfromtol!(obj_test, optimal_tol)

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
write_testlog(test_path, obj_test, optimal_tol, test_result)

end # module TestPiecewiseFuelCO2

6 changes: 3 additions & 3 deletions test/test_threezones.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ genx_setup = Dict(
"CapacityReserveMargin" => 0,
"CO2Cap" => 2,
"StorageLosses" => 1,
"MinCapReq" => 1 ,
"MaxCapReq" => 0 ,
"MinCapReq" => 1,
"MaxCapReq" => 0,
"Solver" => "HiGHS",
"ParameterScale" => 1,
"WriteShadowPrices" => 1,
Expand Down Expand Up @@ -47,7 +47,7 @@ optimal_tol = get_attribute(EP, "dual_feasibility_tolerance")
obj_test = round_objfromtol!(obj_test, optimal_tol)

# Test the objective value
test_result = @test obj_test obj_true atol=optimal_tol
test_result = @test obj_test obj_true atol = optimal_tol

# Add the results to the test log
write_testlog(test_path, obj_test, optimal_tol, test_result)
Expand Down
15 changes: 8 additions & 7 deletions test/test_time_domain_reduction.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module TestTDR


import GenX
import Test
import Test
import JLD2, Clustering

# suppress printing
Expand All @@ -12,7 +12,7 @@ redirect_stdout(devnull)
test_folder = settings_path = "TDR"

if isdir(joinpath(test_folder, "TDR_Results"))
rm(joinpath(test_folder, "TDR_Results"), recursive=true)
rm(joinpath(test_folder, "TDR_Results"), recursive = true)
end

# Inputs for cluster_inputs function
Expand All @@ -30,10 +30,11 @@ genx_setup = Dict(
"CO2Cap" => 2,
)

clustering_test = GenX.cluster_inputs(test_folder, settings_path, genx_setup)["ClusterObject"]
clustering_test =
GenX.cluster_inputs(test_folder, settings_path, genx_setup)["ClusterObject"]

# Load true clustering
clustering_true = JLD2.load(joinpath(test_folder,"clusters_true.jld2"))["ClusterObject"]
clustering_true = JLD2.load(joinpath(test_folder, "clusters_true.jld2"))["ClusterObject"]

# Clustering validation
R = Clustering.randindex(clustering_test, clustering_true)[2]
Expand All @@ -42,7 +43,7 @@ I = Clustering.mutualinfo(clustering_test, clustering_true)
# restore printing
redirect_stdout(console_out)

Test.@test round(R, digits=1) 0.9 # Rand index should be close to 1
Test.@test round(I, digits=1) 0.7 # Mutual information should be close to 1
Test.@test round(R, digits = 1) 0.9 # Rand index should be close to 1
Test.@test round(I, digits = 1) 0.7 # Mutual information should be close to 1

end # module TestTDR
end # module TestTDR
2 changes: 1 addition & 1 deletion test/test_utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ end
end


end # module TestUtilities
end # module TestUtilities
Loading

0 comments on commit 415afb2

Please sign in to comment.