diff --git a/test/runtests.jl b/test/runtests.jl index e388f402..38cd117d 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1,16 +1,16 @@ -using CSV -using Cbc -using DataFrames -using DuckDB -using GLPK -using Graphs -using HiGHS -using JuMP -using MathOptInterface -using Test -using TOML -using TulipaEnergyModel -using TulipaIO +using CSV: CSV +using Cbc: Cbc +using DataFrames: DataFrames, DataFrame +using DuckDB: DuckDB, DBInterface +using GLPK: GLPK +using Graphs: Graphs +using HiGHS: HiGHS +using JuMP: JuMP +using MathOptInterface: MathOptInterface +using Test: Test, @test, @testset, @test_throws, @test_logs +using TOML: TOML +using TulipaEnergyModel: TulipaEnergyModel +using TulipaIO: TulipaIO # Folders names const INPUT_FOLDER = joinpath(@__DIR__, "inputs") @@ -49,5 +49,5 @@ end @testset "Ensuring data can be read and create the internal structures" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(@__DIR__, "../benchmark/EU/")) - create_internal_structures(connection) + TulipaEnergyModel.create_internal_structures(connection) end diff --git a/test/test-case-studies.jl b/test/test-case-studies.jl index b00b2d01..2a1f8e11 100644 --- a/test/test-case-studies.jl +++ b/test/test-case-studies.jl @@ -12,7 +12,7 @@ for (optimizer, parameters) in parameters_dict connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario(connection; optimizer, parameters) + energy_problem = TulipaEnergyModel.run_scenario(connection; optimizer, parameters) @test JuMP.is_solved_and_feasible(energy_problem.model) end end @@ -26,7 +26,7 @@ end for optimizer in optimizer_list connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario(connection; optimizer) + energy_problem = TulipaEnergyModel.run_scenario(connection; optimizer) @test energy_problem.objective_value ≈ 269238.43825 rtol = 1e-8 end end @@ -35,7 +35,7 @@ end dir = joinpath(INPUT_FOLDER, "Norse") connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario( + energy_problem = TulipaEnergyModel.run_scenario( connection; output_folder = OUTPUT_FOLDER, write_lp_file = true, @@ -47,7 +47,7 @@ end dir = joinpath(INPUT_FOLDER, "Storage") connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario(connection) + energy_problem = TulipaEnergyModel.run_scenario(connection) @test energy_problem.objective_value ≈ 2409.384029 atol = 1e-5 end @@ -58,7 +58,8 @@ end Dict("output_flag" => false, "mip_rel_gap" => 0.0, "mip_feasibility_tolerance" => 1e-5) connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario(connection; optimizer = optimizer, parameters = parameters) + energy_problem = + TulipaEnergyModel.run_scenario(connection; optimizer = optimizer, parameters = parameters) @test energy_problem.objective_value ≈ 293074.923309 atol = 1e-5 end @@ -66,7 +67,7 @@ end dir = joinpath(INPUT_FOLDER, "Variable Resolution") connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario(connection) + energy_problem = TulipaEnergyModel.run_scenario(connection) @test energy_problem.objective_value ≈ 28.45872 atol = 1e-5 end @@ -74,7 +75,7 @@ end dir = joinpath(INPUT_FOLDER, "Multi-year Investments") connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = run_scenario( + energy_problem = TulipaEnergyModel.run_scenario( connection; model_parameters_file = joinpath(@__DIR__, "inputs", "model-parameters-example.toml"), ) @@ -94,9 +95,11 @@ end AND milestone_year = 2030 ", ) - energy_problem = EnergyProblem(connection) - create_model!(energy_problem) - @test_logs (:warn, "Model status different from optimal") solve_model!(energy_problem) + energy_problem = TulipaEnergyModel.EnergyProblem(connection) + TulipaEnergyModel.create_model!(energy_problem) + @test_logs (:warn, "Model status different from optimal") TulipaEnergyModel.solve_model!( + energy_problem, + ) @test energy_problem.termination_status == JuMP.INFEASIBLE print(energy_problem) end diff --git a/test/test-economic-parameters.jl b/test/test-economic-parameters.jl index 3fa79b41..98d7561f 100644 --- a/test/test-economic-parameters.jl +++ b/test/test-economic-parameters.jl @@ -19,7 +19,7 @@ (2022, "asset1") => 135.6714595, ) - result = calculate_annualized_cost( + result = TulipaEnergyModel.calculate_annualized_cost( discount_rate, economic_lifetime, investment_cost, @@ -53,7 +53,7 @@ end (2022, "asset1") => 964.3285406, ) - result = calculate_salvage_value( + result = TulipaEnergyModel.calculate_salvage_value( discount_rate, economic_lifetime, annualized_cost, @@ -93,7 +93,7 @@ end (2022, "asset1") => 0.0797796, ) - result = calculate_weight_for_investment_discounts( + result = TulipaEnergyModel.calculate_weight_for_investment_discounts( social_rate, discount_year, salvage_value, diff --git a/test/test-io.jl b/test/test-io.jl index 0ece21ab..e4966718 100644 --- a/test/test-io.jl +++ b/test/test-io.jl @@ -2,7 +2,7 @@ @testset "Check missing asset partition if strict" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Norse")) - @test_throws Exception EnergyProblem(connection, strict = true) + @test_throws Exception TulipaEnergyModel.EnergyProblem(connection, strict = true) end end @@ -10,13 +10,13 @@ end @testset "Make sure that saving an unsolved energy problem fails" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - energy_problem = EnergyProblem(connection) + energy_problem = TulipaEnergyModel.EnergyProblem(connection) output_dir = mktempdir() - @test_throws Exception save_solution_to_file(output_dir, energy_problem) - create_model!(energy_problem) - @test_throws Exception save_solution_to_file(output_dir, energy_problem) - solve_model!(energy_problem) - @test save_solution_to_file(output_dir, energy_problem) === nothing + @test_throws Exception TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem) + TulipaEnergyModel.create_model!(energy_problem) + @test_throws Exception TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem) + TulipaEnergyModel.solve_model!(energy_problem) + @test TulipaEnergyModel.save_solution_to_file(output_dir, energy_problem) === nothing end end @@ -24,11 +24,11 @@ end @testset "Check the missing cases of printing the EnergyProblem" begin # model infeasible is covered in testset "Infeasible Case Study". connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - energy_problem = EnergyProblem(connection) + energy_problem = TulipaEnergyModel.EnergyProblem(connection) print(energy_problem) - create_model!(energy_problem) + TulipaEnergyModel.create_model!(energy_problem) print(energy_problem) - solve_model!(energy_problem) + TulipaEnergyModel.solve_model!(energy_problem) print(energy_problem) end end @@ -37,7 +37,7 @@ end @testset "Graph structure is correct" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - graph, _, _ = create_internal_structures(connection) + graph, _, _ = TulipaEnergyModel.create_internal_structures(connection) @test Graphs.nv(graph) == 6 @test Graphs.ne(graph) == 5 diff --git a/test/test-model-parameters.jl b/test/test-model-parameters.jl index 71ba6034..bee985b9 100644 --- a/test/test-model-parameters.jl +++ b/test/test-model-parameters.jl @@ -2,40 +2,40 @@ path = joinpath(@__DIR__, "inputs", "model-parameters-example.toml") @testset "Basic usage" begin - mp = ModelParameters(; discount_rate = 0.1, discount_year = 2018) + mp = TulipaEnergyModel.ModelParameters(; discount_rate = 0.1, discount_year = 2018) @test mp.discount_rate == 0.1 @test mp.discount_year == 2018 end @testset "Errors when missing required parameters" begin - @test_throws UndefKeywordError ModelParameters() + @test_throws UndefKeywordError TulipaEnergyModel.ModelParameters() end @testset "Read from file" begin - mp = ModelParameters(path) + mp = TulipaEnergyModel.ModelParameters(path) data = TOML.parsefile(path) for (key, value) in data @test value == getfield(mp, Symbol(key)) end @testset "explicit keywords take precedence" begin - mp = ModelParameters(path; discount_year = 2019) + mp = TulipaEnergyModel.ModelParameters(path; discount_year = 2019) @test mp.discount_year == 2019 end @testset "Errors if path does not exist" begin - @test_throws ArgumentError ModelParameters("nonexistent.toml") + @test_throws ArgumentError TulipaEnergyModel.ModelParameters("nonexistent.toml") end end @testset "Read from DuckDB" begin connection = DBInterface.connect(DuckDB.DB) - read_csv_folder(connection, joinpath(@__DIR__, "inputs", "Norse")) - mp = ModelParameters(connection) + TulipaIO.read_csv_folder(connection, joinpath(@__DIR__, "inputs", "Norse")) + mp = TulipaEnergyModel.ModelParameters(connection) @test mp.discount_year == 2030 @testset "path has precedence" begin - mp = ModelParameters(connection, path) + mp = TulipaEnergyModel.ModelParameters(connection, path) data = TOML.parsefile(path) for (key, value) in data @test value == getfield(mp, Symbol(key)) @@ -43,7 +43,7 @@ end @testset "explicit keywords take precedence" begin - mp = ModelParameters(connection, path; discount_year = 2019) + mp = TulipaEnergyModel.ModelParameters(connection, path; discount_year = 2019) @test mp.discount_year == 2019 end end diff --git a/test/test-model.jl b/test/test-model.jl index 46dc9ac3..046b5ad3 100644 --- a/test/test-model.jl +++ b/test/test-model.jl @@ -1,11 +1,11 @@ @testset "Test that solve_model! throws if model is not created but works otherwise" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - energy_problem = EnergyProblem(connection) - @test_throws Exception solve_model!(energy_problem) + energy_problem = TulipaEnergyModel.EnergyProblem(connection) + @test_throws Exception TulipaEnergyModel.solve_model!(energy_problem) @test !energy_problem.solved - create_model!(energy_problem) + TulipaEnergyModel.create_model!(energy_problem) @test !energy_problem.solved - solution = solve_model!(energy_problem) + solution = TulipaEnergyModel.solve_model!(energy_problem) @test energy_problem.solved end diff --git a/test/test-options.jl b/test/test-options.jl index 299eebec..1ad8a9dc 100644 --- a/test/test-options.jl +++ b/test/test-options.jl @@ -1,7 +1,7 @@ @testset "Test some HiGHS options" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - energy_problem = run_scenario( + energy_problem = TulipaEnergyModel.run_scenario( connection; output_folder = OUTPUT_FOLDER, optimizer = HiGHS.Optimizer, @@ -19,19 +19,19 @@ end @testset "Test default_parameters usage" begin @testset "HiGHS" begin expected = Dict{String,Any}("output_flag" => false) - @test default_parameters(Val(:HiGHS)) == expected - @test default_parameters(HiGHS.Optimizer) == expected - @test default_parameters(:HiGHS) == expected - @test default_parameters("HiGHS") == expected + @test TulipaEnergyModel.default_parameters(Val(:HiGHS)) == expected + @test TulipaEnergyModel.default_parameters(HiGHS.Optimizer) == expected + @test TulipaEnergyModel.default_parameters(:HiGHS) == expected + @test TulipaEnergyModel.default_parameters("HiGHS") == expected end @testset "Undefined values" begin expected = Dict{String,Any}() - @test default_parameters(Val(:blah)) == expected - @test default_parameters(:blah) == expected - @test default_parameters("blah") == expected + @test TulipaEnergyModel.default_parameters(Val(:blah)) == expected + @test TulipaEnergyModel.default_parameters(:blah) == expected + @test TulipaEnergyModel.default_parameters("blah") == expected struct DummySolver <: MathOptInterface.AbstractOptimizer end - @test default_parameters(Val(:DummySolver)) == expected + @test TulipaEnergyModel.default_parameters(Val(:DummySolver)) == expected end @testset "New definition" begin @@ -59,7 +59,7 @@ end ) close(io) - @test read_parameters_from_file(filepath) == Dict{String,Any}( + @test TulipaEnergyModel.read_parameters_from_file(filepath) == Dict{String,Any}( "string" => "something", "integer_number" => 5, "small_number" => 1.0e-8, @@ -68,16 +68,17 @@ end "big_number" => 6.66e6, ) - @test_throws ArgumentError read_parameters_from_file("badfile") + @test_throws ArgumentError TulipaEnergyModel.read_parameters_from_file("badfile") end @testset "Test that bad options throw errors" begin connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, joinpath(INPUT_FOLDER, "Tiny")) - @test_throws MathOptInterface.UnsupportedAttribute energy_problem = run_scenario( - connection; - output_folder = OUTPUT_FOLDER, - optimizer = HiGHS.Optimizer, - parameters = Dict("bad_param" => 1.0), - ) + @test_throws MathOptInterface.UnsupportedAttribute energy_problem = + TulipaEnergyModel.run_scenario( + connection; + output_folder = OUTPUT_FOLDER, + optimizer = HiGHS.Optimizer, + parameters = Dict("bad_param" => 1.0), + ) end diff --git a/test/test-pipeline.jl b/test/test-pipeline.jl index 780029cd..5efc14b4 100644 --- a/test/test-pipeline.jl +++ b/test/test-pipeline.jl @@ -2,10 +2,10 @@ dir = joinpath(INPUT_FOLDER, "Tiny") connection = DBInterface.connect(DuckDB.DB) _read_csv_folder(connection, dir) - energy_problem = EnergyProblem(connection) - create_model!(energy_problem) - solve_model!(energy_problem, HiGHS.Optimizer) - save_solution_to_file(mktempdir(), energy_problem) + energy_problem = TulipaEnergyModel.EnergyProblem(connection) + TulipaEnergyModel.create_model!(energy_problem) + TulipaEnergyModel.solve_model!(energy_problem, HiGHS.Optimizer) + TulipaEnergyModel.save_solution_to_file(mktempdir(), energy_problem) end @testset "Test that everything works for $input from beginning to end without EnergyProblem struct" for input in @@ -21,15 +21,16 @@ end _read_csv_folder(connection, dir) # Internal data and structures pre-model - graph, representative_periods, timeframe, years = create_internal_structures(connection) - model_parameters = ModelParameters(connection) - sets = create_sets(graph, years) - variables = compute_variables_indices(connection) - constraints = compute_constraints_indices(connection) - profiles = prepare_profiles_structure(connection) + graph, representative_periods, timeframe, years = + TulipaEnergyModel.create_internal_structures(connection) + model_parameters = TulipaEnergyModel.ModelParameters(connection) + sets = TulipaEnergyModel.create_sets(graph, years) + variables = TulipaEnergyModel.compute_variables_indices(connection) + constraints = TulipaEnergyModel.compute_constraints_indices(connection) + profiles = TulipaEnergyModel.prepare_profiles_structure(connection) # Create model - model = create_model( + model = TulipaEnergyModel.create_model( connection, graph, sets, @@ -43,6 +44,6 @@ end ) # Solve model - solution = solve_model(model, variables, HiGHS.Optimizer) - save_solution_to_file(mktempdir(), graph, solution) + solution = TulipaEnergyModel.solve_model(model, variables, HiGHS.Optimizer) + TulipaEnergyModel.save_solution_to_file(mktempdir(), graph, solution) end