Skip to content

Commit 7c32dff

Browse files
authored
Fixed a bug in link resources (#66)
* input and output was switched * test was added to avoid the problem in the future
1 parent 01127e4 commit 7c32dff

3 files changed

Lines changed: 40 additions & 5 deletions

File tree

NEWS.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## Unversioned
44

5+
### Bugfixes
6+
7+
* Fixed a bug when a `Link` did not have the same `input` and `output` `Resource`.
8+
59
### Changes to checks
610

711
* Add check that `stor_res` is included in both `input` and `output`.

src/model.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -594,14 +594,14 @@ function constraints_couple(m, 𝒩::Vector{<:Node}, ℒ::Vector{<:Link}, 𝒫,
594594
if has_output(n)
595595
@constraint(m, [t 𝒯, p outputs(n)],
596596
m[:flow_out][n, t, p] ==
597-
sum(m[:link_in][l, t, p] for l ℒᶠʳᵒᵐ if p outputs(l))
597+
sum(m[:link_in][l, t, p] for l ℒᶠʳᵒᵐ if p inputs(l))
598598
)
599599
end
600600
# Constraint for input flowrate and output links.
601601
if has_input(n)
602602
@constraint(m, [t 𝒯, p inputs(n)],
603603
m[:flow_in][n, t, p] ==
604-
sum(m[:link_out][l, t, p] for l ℒᵗᵒ if p inputs(l))
604+
sum(m[:link_out][l, t, p] for l ℒᵗᵒ if p outputs(l))
605605
)
606606
end
607607
end

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)
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+
= get_links(case)
171+
𝒩 = get_nodes(case)
172+
𝒯 = get_time_struct(case)
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)