TulipaEnergyModel.jl
TulipaEnergyModel.jl copied to clipboard
Use functions for saving flow solutions
What and Why
The current implementation for saving flow solutions (see code below) in the save_solution_to_file
function uses a block for DataFrames.flatten
and DataFrames.transform
.
output_file = joinpath(output_folder, "flows.csv")
output_table =
DataFrames.select(dataframes[:flows], :from, :to, :rp, :timesteps_block => :timestep)
output_table.value = solution.flow
output_table = DataFrames.flatten(
DataFrames.transform(
output_table,
[:timestep, :value] =>
DataFrames.ByRow(
(timesteps_block, value) -> begin # transform each row using these two columns
n = length(timesteps_block)
(timesteps_block, Iterators.repeated(value, n)) # e.g., (3:5, [30, 30, 30])
end,
) => [:timestep, :value],
),
[:timestep, :value], # flatten, e.g., [(3, 30), (4, 30), (5, 30)]
)
output_table |> CSV.write(output_file)
After #564, there is a similar function defined for storage (see code below).
function _interpolate_storage_level!(df, time_column)
DataFrames.flatten(
DataFrames.transform(
df,
[time_column, :value, :processed_value] =>
DataFrames.ByRow(
(period, value, start_value) -> begin
n = length(period)
interpolated_values = range(start_value; stop = value, length = n + 1)
(period, value, interpolated_values[2:end])
end,
) => [time_column, :value, :processed_value],
),
[time_column, :processed_value],
)
end
Since this function are added for the storage, the first block code can be simplified further by also using this function. For that purpose, the function may need to be modified a bit.
Possible Drawbacks
It will make the code cleaner, but that may not save many codes (since some conditions would need to be added to the functions and currently there is only this part that needs to be refactored).
It may lose or improve readability.
Related Issues
No response
Maybe obsolete if we use dataframes for outputs #115
Also see comments on #688