|
| 1 | +using CSV |
| 2 | +using DataFrames |
| 3 | +using DuckDB |
| 4 | +using TulipaEnergyModel |
| 5 | +using TulipaIO |
| 6 | +using Plots |
| 7 | + |
| 8 | +function _validate_one_rep_period(connection) |
| 9 | + for row in DuckDB.query( |
| 10 | + connection, |
| 11 | + "SELECT year, max(rep_period) as num_rep_periods |
| 12 | + FROM rep_periods_data |
| 13 | + GROUP BY year |
| 14 | + ", |
| 15 | + ) |
| 16 | + if row.num_rep_periods > 1 |
| 17 | + error("We should have only 1 rep period for rolling horizon") |
| 18 | + end |
| 19 | + end |
| 20 | +end |
| 21 | + |
| 22 | +connection = DBInterface.connect(DuckDB.DB) |
| 23 | +schemas = TulipaEnergyModel.schema_per_table_name |
| 24 | +TulipaIO.read_csv_folder( |
| 25 | + connection, |
| 26 | + joinpath(@__DIR__, "..", "test", "inputs", "Rolling Horizon"); |
| 27 | + schemas, |
| 28 | +) |
| 29 | + |
| 30 | +_q(s) = DataFrame(DuckDB.query(connection, s)) |
| 31 | + |
| 32 | +# Manually run rolling horizon simulation |
| 33 | +try |
| 34 | + # MAKE SURE THAT num_rep_periods = 1, otherwise we don't know what to do yet |
| 35 | + # TODO: Create issue to add num_rep_periods to year_data |
| 36 | + # TODO: This should go to validation |
| 37 | + _validate_one_rep_period(connection) |
| 38 | + |
| 39 | + move_forward = 24 |
| 40 | + maximum_window_length = 48 |
| 41 | + global energy_problem = run_rolling_horizon( |
| 42 | + connection, |
| 43 | + move_forward, |
| 44 | + maximum_window_length; |
| 45 | + show_log = false, |
| 46 | + model_file_name = "jump-test.lp", |
| 47 | + ) |
| 48 | + |
| 49 | + @info "Full run" energy_problem |
| 50 | + if energy_problem.solved |
| 51 | + # @info "Asset investment" _q("FROM var_assets_investment") |
| 52 | + @info "Storage" count(_q("FROM var_storage_level_rep_period").solution .> 0) |
| 53 | + @assert any(_q("FROM var_storage_level_rep_period").solution .> 0) |
| 54 | + # @info "Flow from solar" count(_q("FROM var_flow WHERE from_asset='Solar'").solution .> 0) |
| 55 | + # @info "Positive flows in the first 3 hours" _q( |
| 56 | + # "FROM var_flow WHERE solution > 0 AND time_block_start in (11, 12)", |
| 57 | + # ) |
| 58 | + @info energy_problem |
| 59 | + else |
| 60 | + @warn "Infeasible" |
| 61 | + error("Infeasible") |
| 62 | + end |
| 63 | + |
| 64 | +catch ex |
| 65 | + rethrow(ex) |
| 66 | +finally |
| 67 | + # close(connection) |
| 68 | +end |
| 69 | + |
| 70 | +# Plotting |
| 71 | +df_sql(s) = DataFrame(DuckDB.query(connection, s)) |
| 72 | +big_table = df_sql(""" |
| 73 | + WITH cte_outgoing AS ( |
| 74 | + SELECT |
| 75 | + rolsol.window_id, |
| 76 | + var.from_asset AS asset, |
| 77 | + var.time_block_start AS timestep, |
| 78 | + sum(rolsol.solution) AS solution |
| 79 | + FROM rolling_solution_var_flow AS rolsol |
| 80 | + LEFT JOIN full_var_flow AS var |
| 81 | + ON rolsol.var_id = var.id |
| 82 | + GROUP BY window_id, asset, timestep |
| 83 | + ), cte_incoming AS ( |
| 84 | + SELECT |
| 85 | + rolsol.window_id, |
| 86 | + var.to_asset AS asset, |
| 87 | + var.time_block_start AS timestep, |
| 88 | + sum(rolsol.solution) AS solution |
| 89 | + FROM rolling_solution_var_flow AS rolsol |
| 90 | + LEFT JOIN full_var_flow AS var |
| 91 | + ON rolsol.var_id = var.id |
| 92 | + GROUP BY window_id, asset, timestep |
| 93 | + ), cte_unified AS ( |
| 94 | + SELECT |
| 95 | + cte_outgoing.window_id, |
| 96 | + cte_outgoing.asset, |
| 97 | + cte_outgoing.timestep, |
| 98 | + coalesce(cte_outgoing.solution) AS outgoing, |
| 99 | + coalesce(cte_incoming.solution) AS incoming, |
| 100 | + FROM cte_outgoing |
| 101 | + LEFT JOIN cte_incoming |
| 102 | + ON cte_outgoing.window_id = cte_incoming.window_id |
| 103 | + AND cte_outgoing.asset = cte_incoming.asset |
| 104 | + AND cte_outgoing.timestep = cte_incoming.timestep |
| 105 | + ), cte_full_asset_data AS ( |
| 106 | + SELECT |
| 107 | + cte_unified.*, |
| 108 | + asset.type, |
| 109 | + FROM cte_unified |
| 110 | + LEFT JOIN asset |
| 111 | + ON cte_unified.asset = asset.asset |
| 112 | + ) |
| 113 | + FROM cte_full_asset_data |
| 114 | + """) |
| 115 | + |
| 116 | +num_windows = TulipaEnergyModel.get_num_rows(connection, "rolling_horizon_window") |
| 117 | +horizon_length = maximum(big_table.timestep) |
| 118 | + |
| 119 | +big_table_grouped_per_window = groupby(big_table, :window_id) |
| 120 | +plt_vec = Plots.Plot[] |
| 121 | +for ((window_id,), window_table) in pairs(big_table_grouped_per_window) |
| 122 | + local timestep = range(extrema(window_table.timestep)...) |
| 123 | + |
| 124 | + thermal = sort(window_table[window_table.asset.=="thermal", :], :timestep).outgoing |
| 125 | + solar = sort(window_table[window_table.asset.=="solar", :], :timestep).outgoing |
| 126 | + discharge = sort(window_table[window_table.asset.=="battery", :], :timestep).outgoing |
| 127 | + charge = sort(window_table[window_table.asset.=="battery", :], :timestep).incoming |
| 128 | + |
| 129 | + y = hcat(thermal, solar, discharge) |
| 130 | + local plt = plot(; ylabel = "MW", xlims = (1, horizon_length), xticks = 1:12:horizon_length) |
| 131 | + label = window_id == 1 ? ["thermal" "solar" "discharge"] : false |
| 132 | + areaplot!(timestep, y; lab = label) |
| 133 | + label = window_id == 1 ? "charge" : false |
| 134 | + areaplot!(timestep, -charge; lab = label) |
| 135 | + push!(plt_vec, plt) |
| 136 | +end |
| 137 | +Plots.plot(plt_vec...; layout = (length(plt_vec), 1), size = (800, 150 * num_windows)) |
| 138 | +plot!() |
| 139 | + |
| 140 | +# TODO: fix naming of opt_window (just move_forward is enough) |
0 commit comments