Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove *_partitions from graph object #939

Merged
merged 1 commit into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 0 additions & 47 deletions src/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -236,53 +236,6 @@ function create_internal_structures(connection)
tmp_create_lowest_resolution_table(connection)
tmp_create_highest_resolution_table(connection)

df = TulipaIO.get_table(connection, "asset_time_resolution")
gdf = DataFrames.groupby(df, [:asset, :year, :rep_period])
for ((a, year, rp), _df) in pairs(gdf)
if !haskey(graph[a].rep_periods_partitions, year)
graph[a].rep_periods_partitions[year] = Dict{Int,Vector{TimestepsBlock}}()
end
graph[a].rep_periods_partitions[year][rp] =
map(r -> r[1]:r[2], zip(_df.time_block_start, _df.time_block_end))
end
df = TulipaIO.get_table(connection, "flow_time_resolution")
gdf = DataFrames.groupby(df, [:from_asset, :to_asset, :year, :rep_period])
for ((u, v, year, rp), _df) in pairs(gdf)
if !haskey(graph[u, v].rep_periods_partitions, year)
graph[u, v].rep_periods_partitions[year] = Dict{Int,Vector{TimestepsBlock}}()
end
graph[u, v].rep_periods_partitions[year][rp] =
map(r -> r[1]:r[2], zip(_df.time_block_start, _df.time_block_end))
P = graph[u, v].rep_periods_partitions[year][rp]
end

#=
For timeframe, This SQL query retrieves the names of assets from the `assets_data` table
along with their corresponding partition specifications from the `assets_timeframe_partitions` table,
if they exist. If a specification or partition is not available, it defaults to 'uniform' and '1' respectively.
The query only includes assets marked as seasonal (`is_seasonal` column) in the `assets_data` table.
=#
find_assets_partitions_query = """
SELECT asset_both.asset,
IFNULL(assets_timeframe_partitions.specification, 'uniform') AS specification,
IFNULL(assets_timeframe_partitions.partition, '1') AS partition
FROM asset_both
LEFT JOIN asset
ON asset.asset = asset_both.asset
LEFT JOIN assets_timeframe_partitions
ON asset_both.asset = assets_timeframe_partitions.asset
WHERE asset.is_seasonal
"""
for row in DuckDB.query(connection, find_assets_partitions_query)
for year in milestone_years
graph[row.asset].timeframe_partitions[year] = _parse_rp_partition(
Val(Symbol(row.specification)),
row.partition,
1:timeframe.num_periods,
)
end
end

_df =
DuckDB.execute(
connection,
Expand Down
14 changes: 0 additions & 14 deletions src/structures.jl
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,6 @@ mutable struct GraphAssetData
timeframe_profiles::Dict{Int,Dict{Int,Dict{String,Vector{Float64}}}}
rep_periods_profiles::Dict{Int,Dict{Int,Dict{Tuple{String,Int},Vector{Float64}}}}

# partitions
timeframe_partitions::Dict{Int,Vector{PeriodsBlock}}
rep_periods_partitions::Dict{Int,Dict{Int,Vector{TimestepsBlock}}}

# Solution
investment::Dict{Int,Float64}
investment_energy::Dict{Int,Float64} # for storage assets with energy method
Expand All @@ -139,14 +135,10 @@ mutable struct GraphAssetData
function GraphAssetData(args...)
timeframe_profiles = Dict{Int,Dict{Int,Dict{String,Vector{Float64}}}}()
rep_periods_profiles = Dict{Int,Dict{Int,Dict{Tuple{String,Int},Vector{Float64}}}}()
timeframe_partitions = Dict{Int,Vector{TimestepsBlock}}()
rep_periods_partitions = Dict{Int,Dict{Int,Vector{TimestepsBlock}}}()
return new(
args...,
timeframe_profiles,
rep_periods_profiles,
timeframe_partitions,
rep_periods_partitions,
Dict{Int,Float64}(),
Dict{Int,Float64}(),
Dict{Tuple{Int,TimestepsBlock},Float64}(),
Expand Down Expand Up @@ -190,10 +182,6 @@ mutable struct GraphFlowData
timeframe_profiles::Dict{Int,Dict{String,Vector{Float64}}}
rep_periods_profiles::Dict{Int,Dict{Tuple{String,Int},Vector{Float64}}}

# partitions
timeframe_partitions::Dict{Int,Vector{PeriodsBlock}}
rep_periods_partitions::Dict{Int,Dict{Int,Vector{TimestepsBlock}}}

# Solution
flow::Dict{Tuple{Int,TimestepsBlock},Float64}
investment::Dict{Int,Float64}
Expand All @@ -204,8 +192,6 @@ function GraphFlowData(args...)
args...,
Dict{Int,Dict{String,Vector{Float64}}}(),
Dict{Int,Dict{Tuple{String,Int},Vector{Float64}}}(),
Dict{Int,Vector{PeriodsBlock}}(),
Dict{Int,Dict{Int,Vector{TimestepsBlock}}}(),
Dict{Int,Dict{Int,Vector{TimestepsBlock}}}(),
Dict{Int,Float64}(),
)
Expand Down
92 changes: 0 additions & 92 deletions test/test-io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -49,95 +49,3 @@ end
[Graphs.Edge(e) for e in [(1, 2), (3, 2), (4, 2), (5, 2), (6, 2)]]
end
end

@testset "Test parsing of partitions" begin
@testset "compute assets partitions" begin
representative_periods =
[RepresentativePeriod(1.0, 12, 1.0), RepresentativePeriod(1.0, 24, 1.0)]
df = DataFrame(
:asset => [1, 2, 2, 3],
:rep_period => [1, 1, 2, 2],
:specification => [:uniform, :explicit, :math, :math],
:partition => ["3", "4;4;4", "3x4+4x3", "2x2+2x3+2x4+1x6"],
)
assets = [1, 2, 3]
dummy = Dict(a => Dict() for a in assets)
for a in assets
compute_assets_partitions!(dummy[a], df, a, representative_periods)
end
expected = Dict(
(1, 1) => [1:3, 4:6, 7:9, 10:12],
(2, 1) => [1:4, 5:8, 9:12],
(3, 1) => [i:i for i in 1:12],
(1, 2) => [i:i for i in 1:24],
(2, 2) => [1:4, 5:8, 9:12, 13:15, 16:18, 19:21, 22:24],
(3, 2) => [1:2, 3:4, 5:7, 8:10, 11:14, 15:18, 19:24],
)
for a in 1:3, rp in 1:2
@test dummy[a][rp] == expected[(a, rp)]
end
end

@testset "compute flows partitions" begin
representative_periods =
[RepresentativePeriod(1.0, 12, 1.0), RepresentativePeriod(1.0, 24, 1.0)]
df = DataFrame(
:from_asset => [1, 2, 2, 3],
:to_asset => [2, 3, 3, 4],
:rep_period => [1, 1, 2, 2],
:specification => [:uniform, :explicit, :math, :math],
:partition => ["3", "4;4;4", "3x4+4x3", "2x2+2x3+2x4+1x6"],
)
flows = [(1, 2), (2, 3), (3, 4)]
dummy = Dict(f => Dict() for f in flows)
for (u, v) in flows
compute_flows_partitions!(dummy[(u, v)], df, u, v, representative_periods)
end
expected = Dict(
((1, 2), 1) => [1:3, 4:6, 7:9, 10:12],
((2, 3), 1) => [1:4, 5:8, 9:12],
((3, 4), 1) => [i:i for i in 1:12],
((1, 2), 2) => [i:i for i in 1:24],
((2, 3), 2) => [1:4, 5:8, 9:12, 13:15, 16:18, 19:21, 22:24],
((3, 4), 2) => [1:2, 3:4, 5:7, 8:10, 11:14, 15:18, 19:24],
)
for f in flows, rp in 1:2
@test dummy[f][rp] == expected[(f, rp)]
end
end

@testset "If the math doesn't match, raise exception" begin
TEM = TulipaEnergyModel
@test_throws AssertionError TEM._parse_rp_partition(Val(:uniform), "3", 1:13)
@test_throws AssertionError TEM._parse_rp_partition(Val(:uniform), "3", 1:14)
@test_throws AssertionError TEM._parse_rp_partition(Val(:explicit), "3;3;3;3", 1:11)
@test_throws AssertionError TEM._parse_rp_partition(Val(:explicit), "3;3;3;3", 1:13)
@test_throws AssertionError TEM._parse_rp_partition(Val(:explicit), "3;3;3;3", 1:14)
@test_throws AssertionError TEM._parse_rp_partition(Val(:math), "3x4", 1:11)
@test_throws AssertionError TEM._parse_rp_partition(Val(:math), "3x4", 1:13)
@test_throws AssertionError TEM._parse_rp_partition(Val(:math), "3x4", 1:14)
end
end

@testset "is_seasonal asset without entry in partitions file should use :uniform,1" begin
# Copy Norse and delete a row of the partitions file
dir = mktempdir()
for (root, _, files) in walkdir(joinpath(INPUT_FOLDER, "Norse"))
for file in files
cp(joinpath(root, file), joinpath(dir, file))
end
end
filename = joinpath(dir, "assets-timeframe-partitions.csv")
lines = readlines(filename)
open(filename, "w") do io
for line in lines[1:end-1]
println(io, line)
end
end
missing_asset = split(lines[end], ",")[1] # The asset that was not included

connection = DBInterface.connect(DuckDB.DB)
_read_csv_folder(connection, dir)
graph, rps, tf = create_internal_structures(connection)
@test graph[missing_asset].timeframe_partitions == Dict(2030 => [i:i for i in 1:tf.num_periods])
end
Loading