Skip to content

Commit

Permalink
Move timing from run scenario (#670)
Browse files Browse the repository at this point in the history
  • Loading branch information
datejada authored Jun 26, 2024
1 parent 1c63605 commit 5d25cb9
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 98 deletions.
43 changes: 24 additions & 19 deletions src/create-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -349,25 +349,30 @@ end
Create the internal model of an [`TulipaEnergyModel.EnergyProblem`](@ref).
"""
function create_model!(energy_problem; kwargs...)
graph = energy_problem.graph
representative_periods = energy_problem.representative_periods
constraints_partitions = energy_problem.constraints_partitions
timeframe = energy_problem.timeframe
energy_problem.dataframes = @timeit to "construct_dataframes" construct_dataframes(
graph,
representative_periods,
constraints_partitions,
)
energy_problem.model = @timeit to "create_model" create_model(
graph,
representative_periods,
energy_problem.dataframes,
timeframe;
kwargs...,
)
energy_problem.termination_status = JuMP.OPTIMIZE_NOT_CALLED
energy_problem.solved = false
energy_problem.objective_value = NaN
elapsed_time_create_model = @elapsed begin
graph = energy_problem.graph
representative_periods = energy_problem.representative_periods
constraints_partitions = energy_problem.constraints_partitions
timeframe = energy_problem.timeframe
energy_problem.dataframes = @timeit to "construct_dataframes" construct_dataframes(
graph,
representative_periods,
constraints_partitions,
)
energy_problem.model = @timeit to "create_model" create_model(
graph,
representative_periods,
energy_problem.dataframes,
timeframe;
kwargs...,
)
energy_problem.termination_status = JuMP.OPTIMIZE_NOT_CALLED
energy_problem.solved = false
energy_problem.objective_value = NaN
end

energy_problem.time_create_model = elapsed_time_create_model

return energy_problem
end

Expand Down
8 changes: 6 additions & 2 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ the `EnergyProblem` structure.
Set `strict = true` to error if assets are missing from partition data.
"""
function create_energy_problem_from_csv_folder(input_folder::AbstractString; strict = false)
connection = create_connection_and_import_from_csv_folder(input_folder)
return EnergyProblem(connection; strict = strict)
elapsed_time_read_data = @elapsed begin
connection = create_connection_and_import_from_csv_folder(input_folder)
energy_problem = EnergyProblem(connection; strict = strict)
end
energy_problem.time_read_data = elapsed_time_read_data
return energy_problem
end

"""
Expand Down
30 changes: 10 additions & 20 deletions src/run-scenario.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,28 +18,18 @@ function run_scenario(
log_file = "",
show_log = true,
)
elapsed_time_read_data = @elapsed begin
energy_problem =
@timeit to "create_energy_problem_from_csv_folder" create_energy_problem_from_csv_folder(
input_folder,
)
end

elapsed_time_create_model = @elapsed begin
@timeit to "create_model!" create_model!(energy_problem; write_lp_file = write_lp_file)
end

elapsed_time_solve_model = @elapsed begin
@timeit to "solve and store solution" solve_model!(
energy_problem,
optimizer;
parameters = parameters,
energy_problem =
@timeit to "create_energy_problem_from_csv_folder" create_energy_problem_from_csv_folder(
input_folder,
)
end

energy_problem.time_read_data = elapsed_time_read_data
energy_problem.time_create_model = elapsed_time_create_model
energy_problem.time_solve_model = elapsed_time_solve_model
@timeit to "create_model!" create_model!(energy_problem; write_lp_file = write_lp_file)

@timeit to "solve and store solution" solve_model!(
energy_problem,
optimizer;
parameters = parameters,
)

if output_folder != ""
@timeit to "save_solution_to_file" save_solution_to_file(output_folder, energy_problem)
Expand Down
120 changes: 63 additions & 57 deletions src/solve-model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,78 +11,84 @@ function solve_model!(
optimizer = HiGHS.Optimizer;
parameters = default_parameters(optimizer),
)
model = energy_problem.model
if model === nothing
error("Model is not created, run create_model(energy_problem) first.")
end
elapsed_time_solve_model = @elapsed begin
model = energy_problem.model
if model === nothing
error("Model is not created, run create_model(energy_problem) first.")
end

energy_problem.solution =
solve_model!(energy_problem.dataframes, model, optimizer; parameters = parameters)
energy_problem.termination_status = JuMP.termination_status(model)
if energy_problem.solution === nothing
# Warning has been given at internal function
return
end
energy_problem.solved = true
energy_problem.objective_value = JuMP.objective_value(model)

graph = energy_problem.graph
for a in MetaGraphsNext.labels(graph)
asset = graph[a]
if asset.investable
if asset.investment_integer
asset.investment = round(Int, energy_problem.solution.assets_investment[a])
else
asset.investment = energy_problem.solution.assets_investment[a]
end
if asset.storage_method_energy
if asset.investment_integer_storage_energy
asset.investment_energy =
round(Int, energy_problem.solution.assets_investment_energy[a])
energy_problem.solution =
solve_model!(energy_problem.dataframes, model, optimizer; parameters = parameters)
energy_problem.termination_status = JuMP.termination_status(model)
if energy_problem.solution === nothing
# Warning has been given at internal function
return
end
energy_problem.solved = true
energy_problem.objective_value = JuMP.objective_value(model)

graph = energy_problem.graph
for a in MetaGraphsNext.labels(graph)
asset = graph[a]
if asset.investable
if asset.investment_integer
asset.investment = round(Int, energy_problem.solution.assets_investment[a])
else
asset.investment_energy = energy_problem.solution.assets_investment_energy[a]
asset.investment = energy_problem.solution.assets_investment[a]
end
if asset.storage_method_energy
if asset.investment_integer_storage_energy
asset.investment_energy =
round(Int, energy_problem.solution.assets_investment_energy[a])
else
asset.investment_energy =
energy_problem.solution.assets_investment_energy[a]
end
end
end
end
end

for row in eachrow(energy_problem.dataframes[:lowest_storage_level_intra_rp])
a, rp, timesteps_block, value = row.asset, row.rep_period, row.timesteps_block, row.solution
graph[a].storage_level_intra_rp[(rp, timesteps_block)] = value
end
for row in eachrow(energy_problem.dataframes[:lowest_storage_level_intra_rp])
a, rp, timesteps_block, value =
row.asset, row.rep_period, row.timesteps_block, row.solution
graph[a].storage_level_intra_rp[(rp, timesteps_block)] = value
end

for row in eachrow(energy_problem.dataframes[:storage_level_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].storage_level_inter_rp[pb] = value
end
for row in eachrow(energy_problem.dataframes[:storage_level_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].storage_level_inter_rp[pb] = value
end

for row in eachrow(energy_problem.dataframes[:max_energy_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].max_energy_inter_rp[pb] = value
end
for row in eachrow(energy_problem.dataframes[:max_energy_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].max_energy_inter_rp[pb] = value
end

for row in eachrow(energy_problem.dataframes[:min_energy_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].min_energy_inter_rp[pb] = value
end
for row in eachrow(energy_problem.dataframes[:min_energy_inter_rp])
a, pb, value = row.asset, row.periods_block, row.solution
graph[a].min_energy_inter_rp[pb] = value
end

for (u, v) in MetaGraphsNext.edge_labels(graph)
if graph[u, v].investable
if graph[u, v].investment_integer
graph[u, v].investment =
round(Int, energy_problem.solution.flows_investment[(u, v)])
else
graph[u, v].investment = energy_problem.solution.flows_investment[(u, v)]
for (u, v) in MetaGraphsNext.edge_labels(graph)
if graph[u, v].investable
if graph[u, v].investment_integer
graph[u, v].investment =
round(Int, energy_problem.solution.flows_investment[(u, v)])
else
graph[u, v].investment = energy_problem.solution.flows_investment[(u, v)]
end
end
end
end

for row in eachrow(energy_problem.dataframes[:flows])
u, v, rp, timesteps_block, value =
row.from, row.to, row.rep_period, row.timesteps_block, row.solution
graph[u, v].flow[(rp, timesteps_block)] = value
for row in eachrow(energy_problem.dataframes[:flows])
u, v, rp, timesteps_block, value =
row.from, row.to, row.rep_period, row.timesteps_block, row.solution
graph[u, v].flow[(rp, timesteps_block)] = value
end
end

energy_problem.time_solve_model = elapsed_time_solve_model

return energy_problem.solution
end

Expand Down

0 comments on commit 5d25cb9

Please sign in to comment.