diff --git a/src/TulipaEnergyModel.jl b/src/TulipaEnergyModel.jl index 3d06c879..29afa60f 100644 --- a/src/TulipaEnergyModel.jl +++ b/src/TulipaEnergyModel.jl @@ -7,7 +7,7 @@ using Graphs using HiGHS using JuMP -include("input_tables.jl") +include("input-tables.jl") include("io.jl") include("model.jl") include("time-resolution.jl") diff --git a/src/input_tables.jl b/src/input-tables.jl similarity index 100% rename from src/input_tables.jl rename to src/input-tables.jl diff --git a/test/inputs/tiny/assets-data.csv b/test/inputs/Tiny/assets-data.csv similarity index 100% rename from test/inputs/tiny/assets-data.csv rename to test/inputs/Tiny/assets-data.csv diff --git a/test/inputs/tiny/assets-profiles.csv b/test/inputs/Tiny/assets-profiles.csv similarity index 100% rename from test/inputs/tiny/assets-profiles.csv rename to test/inputs/Tiny/assets-profiles.csv diff --git a/test/inputs/tiny/bad-assets-data.csv b/test/inputs/Tiny/bad-assets-data.csv similarity index 100% rename from test/inputs/tiny/bad-assets-data.csv rename to test/inputs/Tiny/bad-assets-data.csv diff --git a/test/inputs/tiny/flows-data.csv b/test/inputs/Tiny/flows-data.csv similarity index 100% rename from test/inputs/tiny/flows-data.csv rename to test/inputs/Tiny/flows-data.csv diff --git a/test/inputs/tiny/flows-profiles.csv b/test/inputs/Tiny/flows-profiles.csv similarity index 100% rename from test/inputs/tiny/flows-profiles.csv rename to test/inputs/Tiny/flows-profiles.csv diff --git a/test/inputs/tiny/rep-periods-data.csv b/test/inputs/Tiny/rep-periods-data.csv similarity index 100% rename from test/inputs/tiny/rep-periods-data.csv rename to test/inputs/Tiny/rep-periods-data.csv diff --git a/test/runtests.jl b/test/runtests.jl index 8e03e887..aafb892c 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -8,116 +8,12 @@ using Test const INPUT_FOLDER = joinpath(@__DIR__, "inputs") const OUTPUT_FOLDER = joinpath(@__DIR__, "outputs") -@testset "TulipaEnergyModel.jl" begin - dir = joinpath(INPUT_FOLDER, "Norse") - parameters, sets = create_parameters_and_sets_from_file(dir) - graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) - model = create_model(graph, parameters, sets) - solution = solve_model(model) - @test solution.objective_value ≈ 183696778.98322 atol = 1e-5 - save_solution_to_file( - OUTPUT_FOLDER, - sets.assets_investment, - solution.assets_investment, - parameters.assets_unit_capacity, - ) -end - -@testset "Write lp file" begin - dir = joinpath(INPUT_FOLDER, "tiny") - parameters, sets = create_parameters_and_sets_from_file(dir) - graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) - model = create_model(graph, parameters, sets; write_lp_file = true) - solution = solve_model(model) - @test solution.objective_value ≈ 269238.43825 atol = 1e-5 - save_solution_to_file( - OUTPUT_FOLDER, - sets.assets_investment, - solution.assets_investment, - parameters.assets_unit_capacity, - ) -end - -@testset "Infeasible run" begin - dir = joinpath(INPUT_FOLDER, "tiny") - parameters, sets = create_parameters_and_sets_from_file(dir) - parameters.peak_demand["demand"] = -1 # make it infeasible - graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) - model = create_model(graph, parameters, sets) - solution = solve_model(model) - @test solution === nothing -end - -@testset "Tiny graph" begin - @testset "Graph structure is correct" begin - dir = joinpath(INPUT_FOLDER, "tiny") - graph = - create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) - - @test Graphs.nv(graph) == 6 - @test Graphs.ne(graph) == 5 - @test collect(Graphs.edges(graph)) == - [Graphs.Edge(e) for e in [(1, 6), (2, 6), (3, 6), (4, 6), (5, 6)]] - end -end - -@testset "Input validation" begin - @testset "Make sure that input validation fails for bad files" begin - dir = joinpath(INPUT_FOLDER, "tiny") - @test_throws ArgumentError TulipaEnergyModel.read_csv_with_schema( - joinpath(dir, "bad-assets-data.csv"), - TulipaEnergyModel.AssetData, - ) - end -end - -@testset "Time resolution" begin - @testset "resolution_matrix" begin - rp_periods = [1:4, 5:8, 9:12] - time_steps = [1:4, 5:8, 9:12] - expected = [ - 1.0 0.0 0.0 - 0.0 1.0 0.0 - 0.0 0.0 1.0 - ] - @test resolution_matrix(rp_periods, time_steps) == expected - - time_steps = [1:3, 4:6, 7:9, 10:12] - expected = [ - 1.0 1/3 0.0 0.0 - 0.0 2/3 2/3 0.0 - 0.0 0.0 1/3 1.0 - ] - @test resolution_matrix(rp_periods, time_steps) == expected - - time_steps = [1:6, 7:9, 10:10, 11:11, 12:12] - expected = [ - 2/3 0.0 0.0 0.0 0.0 - 1/3 2/3 0.0 0.0 0.0 - 0.0 1/3 1.0 1.0 1.0 - ] - @test resolution_matrix(rp_periods, time_steps) == expected - end - - @testset "compute_rp_periods" begin - # regular - time_steps1 = [1:4, 5:8, 9:12] # every 4 hours - time_steps2 = [1:3, 4:6, 7:9, 10:12] # every 3 hours - time_steps3 = [i:i for i = 1:12] # hourly - - @test compute_rp_periods([time_steps1, time_steps2]) == time_steps1 - @test compute_rp_periods([time_steps1, time_steps2, time_steps3]) == time_steps1 - @test compute_rp_periods([time_steps2, time_steps3]) == time_steps2 - - # Irregular - time_steps4 = [1:6, 7:9, 10:11, 12:12] - time_steps5 = [1:2, 3:4, 5:12] - @test compute_rp_periods([time_steps1, time_steps4]) == [1:6, 7:9, 10:12] - @test compute_rp_periods([time_steps1, time_steps5]) == [1:4, 5:12] - @test compute_rp_periods([time_steps4, time_steps5]) == [1:6, 7:12] - end -end +# Add run all test files in test folder +include("test-io.jl") +include("test-time-resolution.jl") +include("test-case-studies.jl") +# Other general tests that don't need their own file @testset "Ensuring benchmark loads" begin include(joinpath(@__DIR__, "..", "benchmark", "benchmarks.jl")) @test SUITE !== nothing diff --git a/test/test-case-studies.jl b/test/test-case-studies.jl new file mode 100644 index 00000000..60d9bd0b --- /dev/null +++ b/test/test-case-studies.jl @@ -0,0 +1,39 @@ +@testset "Norse Case Study" begin + dir = joinpath(INPUT_FOLDER, "Norse") + parameters, sets = create_parameters_and_sets_from_file(dir) + graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) + model = create_model(graph, parameters, sets) + solution = solve_model(model) + @test solution.objective_value ≈ 183696778.98322 atol = 1e-5 + save_solution_to_file( + OUTPUT_FOLDER, + sets.assets_investment, + solution.assets_investment, + parameters.assets_unit_capacity, + ) +end + +@testset "Tiny Case Study" begin + dir = joinpath(INPUT_FOLDER, "Tiny") + parameters, sets = create_parameters_and_sets_from_file(dir) + graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) + model = create_model(graph, parameters, sets; write_lp_file = true) + solution = solve_model(model) + @test solution.objective_value ≈ 269238.43825 atol = 1e-5 + save_solution_to_file( + OUTPUT_FOLDER, + sets.assets_investment, + solution.assets_investment, + parameters.assets_unit_capacity, + ) +end + +@testset "Infeasible Case Study" begin + dir = joinpath(INPUT_FOLDER, "Tiny") + parameters, sets = create_parameters_and_sets_from_file(dir) + parameters.peak_demand["demand"] = -1 # make it infeasible + graph = create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) + model = create_model(graph, parameters, sets) + solution = solve_model(model) + @test solution === nothing +end diff --git a/test/test-io.jl b/test/test-io.jl new file mode 100644 index 00000000..4cab0884 --- /dev/null +++ b/test/test-io.jl @@ -0,0 +1,22 @@ +@testset "Input validation" begin + @testset "Make sure that input validation fails for bad files" begin + dir = joinpath(INPUT_FOLDER, "Tiny") + @test_throws ArgumentError TulipaEnergyModel.read_csv_with_schema( + joinpath(dir, "bad-assets-data.csv"), + TulipaEnergyModel.AssetData, + ) + end +end + +@testset "Graph structure" begin + @testset "Graph structure is correct" begin + dir = joinpath(INPUT_FOLDER, "Tiny") + graph = + create_graph(joinpath(dir, "assets-data.csv"), joinpath(dir, "flows-data.csv")) + + @test Graphs.nv(graph) == 6 + @test Graphs.ne(graph) == 5 + @test collect(Graphs.edges(graph)) == + [Graphs.Edge(e) for e in [(1, 6), (2, 6), (3, 6), (4, 6), (5, 6)]] + end +end diff --git a/test/test-time-resolution.jl b/test/test-time-resolution.jl new file mode 100644 index 00000000..7c5a1e06 --- /dev/null +++ b/test/test-time-resolution.jl @@ -0,0 +1,46 @@ +@testset "Time resolution" begin + @testset "resolution_matrix" begin + rp_periods = [1:4, 5:8, 9:12] + time_steps = [1:4, 5:8, 9:12] + expected = [ + 1.0 0.0 0.0 + 0.0 1.0 0.0 + 0.0 0.0 1.0 + ] + @test resolution_matrix(rp_periods, time_steps) == expected + + time_steps = [1:3, 4:6, 7:9, 10:12] + expected = [ + 1.0 1/3 0.0 0.0 + 0.0 2/3 2/3 0.0 + 0.0 0.0 1/3 1.0 + ] + @test resolution_matrix(rp_periods, time_steps) == expected + + time_steps = [1:6, 7:9, 10:10, 11:11, 12:12] + expected = [ + 2/3 0.0 0.0 0.0 0.0 + 1/3 2/3 0.0 0.0 0.0 + 0.0 1/3 1.0 1.0 1.0 + ] + @test resolution_matrix(rp_periods, time_steps) == expected + end + + @testset "compute_rp_periods" begin + # regular + time_steps1 = [1:4, 5:8, 9:12] # every 4 hours + time_steps2 = [1:3, 4:6, 7:9, 10:12] # every 3 hours + time_steps3 = [i:i for i ∈ 1:12] # hourly + + @test compute_rp_periods([time_steps1, time_steps2]) == time_steps1 + @test compute_rp_periods([time_steps1, time_steps2, time_steps3]) == time_steps1 + @test compute_rp_periods([time_steps2, time_steps3]) == time_steps2 + + # Irregular + time_steps4 = [1:6, 7:9, 10:11, 12:12] + time_steps5 = [1:2, 3:4, 5:12] + @test compute_rp_periods([time_steps1, time_steps4]) == [1:6, 7:9, 10:12] + @test compute_rp_periods([time_steps1, time_steps5]) == [1:4, 5:12] + @test compute_rp_periods([time_steps4, time_steps5]) == [1:6, 7:12] + end +end