Skip to content

Commit a02c729

Browse files
authored
Fixed a bug in link resources (#68)
* input and output was switched * test was added to avoid the problem in the future
1 parent 58a97e7 commit a02c729

3 files changed

Lines changed: 42 additions & 5 deletions

File tree

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Release notes
22

3+
## Unversioned
4+
5+
### Bugfixes
6+
7+
* Fixed a bug when a `Link` did not have the same `input` and `output` `Resource`.
8+
39
## Version 0.8.3 (2024-11-29)
410

511
### Reference checks possible to be called

src/model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -468,14 +468,14 @@ function constraints_node(m, 𝒩, 𝒯, 𝒫, ℒ, modeltype::EnergyModel)
468468
if has_output(n)
469469
@constraint(m, [t 𝒯, p outputs(n)],
470470
m[:flow_out][n, t, p] ==
471-
sum(m[:link_in][l, t, p] for l ℒᶠʳᵒᵐ if p outputs(l))
471+
sum(m[:link_in][l, t, p] for l ℒᶠʳᵒᵐ if p inputs(l))
472472
)
473473
end
474474
# Constraint for input flowrate and output links.
475475
if has_input(n)
476476
@constraint(m, [t 𝒯, p inputs(n)],
477477
m[:flow_in][n, t, p] ==
478-
sum(m[:link_out][l, t, p] for l ℒᵗᵒ if p inputs(l))
478+
sum(m[:link_out][l, t, p] for l ℒᵗᵒ if p outputs(l))
479479
)
480480
end
481481
# Call of function for individual node constraints.

test/test_links.jl

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -115,20 +115,20 @@ Power = ResourceCarrier("Power", 0.0)
115115
CO2 = ResourceEmit("CO2", 1.0)
116116

117117
# Function for setting up the system
118-
function link_graph(LinkType::Vector{DataType})
118+
function link_graph(LinkType::Vector{DataType}; res_in=Power, res_out=Power)
119119
# Used source, network, and sink
120120
source = RefSource(
121121
"source",
122122
FixedProfile(4),
123123
FixedProfile(10),
124124
FixedProfile(0),
125-
Dict(Power => 1),
125+
Dict(res_in => 1),
126126
)
127127
sink = RefSink(
128128
"sink",
129129
FixedProfile(3),
130130
Dict(:surplus => FixedProfile(4), :deficit => FixedProfile(100)),
131-
Dict(Power => 1),
131+
Dict(res_out => 1),
132132
)
133133

134134
resources = [Power, CO2]
@@ -147,6 +147,37 @@ function link_graph(LinkType::Vector{DataType})
147147
return run_model(case, model, HiGHS.Optimizer), case, model
148148
end
149149

150+
@testset "Link - different resources" begin
151+
# Creation of a new link type with associated emissions in each operational period
152+
struct ResourceLink <: Link
153+
id::Any
154+
from::EMB.Node
155+
to::EMB.Node
156+
formulation::EMB.Formulation
157+
end
158+
function EMB.create_link(m, 𝒯, 𝒫, l::ResourceLink, modeltype::EnergyModel, formulation::EMB.Formulation)
159+
# Generic link in which each output corresponds to the input
160+
@constraint(m, [t 𝒯],
161+
sum(m[:link_out][l, t, p_out] for p_out outputs(l)) ==
162+
sum(m[:link_in][l, t, p_in] for p_in inputs(l))
163+
)
164+
end
165+
EMB.inputs(l::ResourceLink) = [Power]
166+
EMB.outputs(l::ResourceLink) = [CO2]
167+
168+
# Create and solve the system
169+
m, case, model = link_graph([ResourceLink]; res_out=CO2)
170+
= case[:links]
171+
𝒩 = case[:nodes]
172+
𝒯 = case[:T]
173+
174+
# Test that the coupling is working correctly and resources are transported
175+
@test all(value.(m[:flow_out][𝒩[1], t, Power]) value.(m[:link_in][ℒ[1], t, Power]) for t 𝒯)
176+
@test all(value.(m[:flow_in][𝒩[2], t, CO2]) value.(m[:link_out][ℒ[1], t, CO2]) for t 𝒯)
177+
@test all(value.(m[:flow_out][𝒩[1], t, Power]) value.(m[:flow_in][𝒩[2], t, CO2]) for t 𝒯)
178+
@test all(value.(m[:flow_out][𝒩[1], t, Power]) 3 for t 𝒯)
179+
end
180+
150181
@testset "Link - emissions" begin
151182
# Creation of a new link type with associated emissions in each operational period
152183
struct EmissionDirect <: Link

0 commit comments

Comments
 (0)