Skip to content

Commit

Permalink
Updated link arguments and checks for DHPipe (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
JulStraus authored Feb 10, 2025
1 parent 8a80edc commit 5e6c9e8
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 42 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
*.jl.mem
Manifest.toml
docs/build
docs/src/manual/NEWS.md
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
* Increased version nubmer for EMB.
* Model worked without adjustments.
* Adjustments only required for simple understanding of changes.
* Included updated `DHPipe`:
* `DHPIpe` now follow a check.
* The arguments for `create_link` were updated.

## Version 0.1.0 (2024-12-18)

Expand Down
6 changes: 6 additions & 0 deletions docs/src/library/internals/methods-EMB.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,13 @@ EnergyModelsBase.constraints_level_iterate
EnergyModelsBase.constraints_capacity
EnergyModelsBase.constraints_flow_in
EnergyModelsBase.constraints_flow_out
```

## [Check methods](@id lib-int-met_emb-check)

```@docs
EnergyModelsBase.check_node
EnergyModelsBase.check_link
```

## [Field extraction methods](@id lib-int-met_emb-field)
Expand Down
27 changes: 0 additions & 27 deletions docs/src/manual/NEWS.md

This file was deleted.

35 changes: 29 additions & 6 deletions src/checks.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
"""
check_node(
n::DirectHeatUpgrade{A, T},
𝒯,
modeltype::EnergyModel,
check_timeprofiles::Bool,
) where {A, T}
n::DirectHeatUpgrade{A, T},
𝒯,
modeltype::EnergyModel,
check_timeprofiles::Bool,
) where {A, T}
Check if a `DirectHeatUpgrade` node has reasonable values for the return/supply temperatures and error if the upgrade is ≥ 1 (should only happen with data errors).
Check if a `DirectHeatUpgrade` node has reasonable values for the return/supply temperatures
and error if the upgrade is ≥ 1 (should only happen with data errors).
"""
function EMB.check_node(
n::DirectHeatUpgrade{A,T},
Expand Down Expand Up @@ -115,3 +116,25 @@ function EMB.check_node(
"The heat_loss_factor field must be less or equal to 1."
)
end

"""
EMB.check_link(l::DHPipe, 𝒯, modeltype::EnergyModel, check_timeprofiles::Bool)
This method checks that the *[`DHPipe`](@ref)* link is valid.
## Checks
- The field `cap` is required to be non-negative.
- The field `pipe_length` is required to be non-negative.
- The field `pipe_loss_factor` is required to be non-negative.
"""
function EMB.check_link(l::DHPipe, 𝒯, modeltype::EnergyModel, check_timeprofiles::Bool)
@assert_or_log(
all(capacity(l, t) 0 for t 𝒯),
"The capacity must be non-negative."
)
@assert_or_log(pipe_length(l) 0, "The pipeline length must be non-negative.")
@assert_or_log(
pipe_loss_factor(l) 0,
"The pipeline loss factor must be non-negative."
)
end
5 changes: 2 additions & 3 deletions src/model.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ function EMB.variables_link(m, ℒˢᵘᵇ::Vector{<:DHPipe}, 𝒯, modeltype::E
end

"""
create_link(m, 𝒯, 𝒫, l::DHPipe, modeltype::EnergyModel, formulation::EMB.Formulation)
create_link(m, l::DHPipe, 𝒯, 𝒫, modeltype::EnergyModel)
When the link is a [`DHPipe`](@ref), the constraints for a link include a loss based on the
difference in the temperature of the district heating resource and the ground.
Expand All @@ -18,11 +18,10 @@ In addition, a [`DHPipe`](@ref) includes a capacity with the potential for inves
"""
function EMB.create_link(
m,
l::DHPipe,
𝒯,
𝒫,
l::DHPipe,
modeltype::EnergyModel,
formulation::EMB.Formulation,
)

# DH pipe in which each output corresponds to the input minus heat losses
Expand Down
4 changes: 2 additions & 2 deletions src/structures/link.jl
Original file line number Diff line number Diff line change
Expand Up @@ -159,15 +159,15 @@ t_supply(l::DHPipe, t) = t_supply(resource_heat(l), t)
"""
EMB.inputs(l::DHPipe)
Return the resources transported into a given DHPipe `l`.
Return the resources transported into a given DHPipe `l`.
This resource is in a standard [`DHPipe`](@ref) given by the function [`resource_heat`](@ref).
"""
EMB.inputs(l::DHPipe) = [resource_heat(l)]

"""
EMB.outputs(l::DHPipe)
Return the resources transported out from a given DHPipe `l`.
Return the resources transported out from a given DHPipe `l`.
This resource is in a standard [`DHPipe`](@ref) given by the function [`resource_heat`](@ref).
"""
EMB.outputs(l::DHPipe) = [resource_heat(l)]
82 changes: 78 additions & 4 deletions test/checks.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@testitem "DirectHeatUpgrade checks" setup = [TestData] begin
@testitem "Checks - DirectHeatUpgrade" setup = [TestData] begin
using EnergyModelsBase
using EnergyModelsHeat
using TimeStruct
Expand Down Expand Up @@ -31,7 +31,7 @@ end

# Test that the fields of a HeatPump are correctly checked
# - check_node(n::HeatPump, 𝒯, modeltype::EnergyModel)
@testitem "HeatPump" begin
@testitem "Checks - HeatPump" begin
using EnergyModelsBase
using TimeStruct
const EMB = EnergyModelsBase
Expand Down Expand Up @@ -90,7 +90,7 @@ end
"heat_demand",
FixedProfile(10),
Dict(:surplus => FixedProfile(0), :deficit => StrategicProfile([1e3, 2e2])),
Dict(Power => 1),
Dict(Heat => 1),
)

nodes = [heat_pump, heat_source, power_source, heat_demand]
Expand Down Expand Up @@ -140,7 +140,7 @@ end

# Test that the fields of a ThermalEnergyStorage are correctly checked
# - check_node(n::ThermalEnergyStorage, 𝒯, modeltype::EnergyModel)
@testitem "ThermalEnergyStorage" begin
@testitem "Checks - ThermalEnergyStorage" begin
using EnergyModelsBase
using TimeStruct

Expand Down Expand Up @@ -231,3 +231,77 @@ end
# Set the global again to false
EMB.TEST_ENV = false
end

# Test that the fields of a DHPipe are correctly checked
# - EMB.check_link(l::DHPipe, 𝒯, modeltype::EnergyModel, check_timeprofiles::Bool)
@testitem "Checks - DHPipe" begin
using EnergyModelsBase
using TimeStruct
const EMB = EnergyModelsBase

# Set the global to true to suppress the error message
EMB.TEST_ENV = true

# Resources used in the analysis
Heat = ResourceHeat("Heat", FixedProfile(90), FixedProfile(60))
CO2 = ResourceEmit("CO2", 1.0)

# Function for setting up the system for testing a `DHPipe` link
function check_graph(;
cap = FixedProfile(20),
pipe_length = 100.0,
pipe_loss_factor = 0.5,
)
products = [Heat, CO2]
heat_source = RefSource(
"heat_source",
FixedProfile(20),
FixedProfile(0.1),
FixedProfile(0),
Dict(Heat => 1),
)
heat_demand = RefSink(
"heat_demand",
FixedProfile(10),
Dict(:surplus => FixedProfile(0), :deficit => StrategicProfile([1e3, 2e2])),
Dict(Heat => 1),
)

nodes = [heat_source, heat_demand]
links = [
DHPipe(
"src-sink",
nodes[1],
nodes[2],
cap,
pipe_length,
pipe_loss_factor,
FixedProfile(10),
Heat,
),
]

# Creation of the time structure and the used global data
op_per_strat = 8760.0
T = TwoLevel(2, 2, SimpleTimes(10, 1); op_per_strat)
modeltype = OperationalModel(
Dict(CO2 => FixedProfile(10)),
Dict(CO2 => FixedProfile(0)),
CO2,
)

# Input data structure
case = Case(T, products, [nodes, links], [[get_nodes, get_links]])
return create_model(case, modeltype), case, modeltype
end

# Test that a wrong capacity is caught by the checks
@test_throws AssertionError check_graph(; cap = FixedProfile(-25))

# Test that a wrong lower capacity bound is caught by the checks
@test_throws AssertionError check_graph(; pipe_length = -0.4)
@test_throws AssertionError check_graph(; pipe_loss_factor = -0.4)

# Set the global again to false
EMB.TEST_ENV = false
end

0 comments on commit 5e6c9e8

Please sign in to comment.