You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This guide shows how to extend resource functionality by adding a custom resource
8
-
type and connecting it to custom variables and constraints through
9
-
resource-dispatch functions. This is useful for modelling more complex
10
-
resource behavior that cannot be captured by the default resource types where the standard
11
-
behavior is built around energy or mass flow.
7
+
## [Concept](@id how_to-res_funct-concept)
12
8
13
-
The pattern follows the same structure as the resource dispatch test in
14
-
`test/test_resource.jl`:
9
+
This guide shows how to extend resource functionality by adding a custom resource type and connecting it to custom variables and constraints through resource-dispatch functions.
10
+
This is useful for modelling more complex resource behavior that cannot be captured by the default resource types where the standard behavior is built around energy or mass flow.
11
+
12
+
The pattern follows the same structure as the resource dispatch test in `test/test_resource.jl`:
15
13
16
14
1. Define a resource subtype with extra parameters.
17
-
2. Optionally create a custom node subtype that uses the resource.
18
-
3. Add resource-specific variables with `variables_flow_resource`.
19
-
4. Add resource-specific constraints with `constraints_resource`.
20
-
5. Couple node and link resource variables with `constraints_couple_resource`.
15
+
2. (Optionally) create a custom node subtype that uses the resource.
16
+
3. Add resource-specific variables with [`variables_flow_resource`](@ref).
17
+
4. Add resource-specific constraints with [`constraints_resource`](@ref).
18
+
5. Couple node and link resource variables with [`constraints_couple_resource`](@ref).
19
+
20
+
## [Example](@id how_to-res_funct-example)
21
21
22
-
The example in the test suite defines a `PotentialPower` resource that has a potential,
23
-
with upper and lower bounds, in addition to energy flow. The flow of this potential
24
-
in and out of junctions follow equality constraints, as opposed to the energy and mass flow
25
-
which follow sum constraints.
22
+
The following example illustrates the different steps that are required for creating a new resource with additional properties.
23
+
It defines a `PotentialPower` resource which has as property a potential with upper and lower bounds in addition to its energy flow.
24
+
The flow of this potential in and out of junctions follows equality constraints, as opposed to the energy and mass flow which follow sum constraints.
26
25
27
26
The notation below follows the same conventions as the implementation and tests:
28
27
29
-
-`𝒩` for nodes
30
-
-`ℒ` for links
31
-
-`𝒫` for resources
32
-
-`𝒯` for the time structure
33
-
-`ℒᶠʳᵒᵐ`, `ℒᵗᵒ` for outgoing and incoming links of a node
34
-
-`𝒫ᵒᵘᵗ`, `𝒫ⁱⁿ`, `𝒫ˡⁱⁿᵏ` for resource subsets on outputs, inputs, and links
28
+
-`𝒩` for nodes,
29
+
-`ℒ` for links,
30
+
-`𝒫` for resources,
31
+
-`𝒯` for the time structure,
32
+
-`ℒᶠʳᵒᵐ`, `ℒᵗᵒ` for outgoing and incoming links of a node, and
33
+
-`𝒫ᵒᵘᵗ`, `𝒫ⁱⁿ`, `𝒫ˡⁱⁿᵏ` for resource subsets on outputs, inputs, and links.
35
34
36
-
## 1. Define a special resource
35
+
###1. Define a special resource
37
36
38
-
Create a subtype of [`Resource`](@ref) and keep `co2_int` as the second field for
39
-
consistency with existing resource structures.
37
+
Create a subtype of [`Resource`](@ref) and keep `co2_int` as the second field for consistency with existing resource structures.
38
+
Alternatively, you can create a new method for the internal function [`co2_int`](@ref).
Use [`variables_flow_resource`](@ref) to create resource variables.
100
97
101
98
Important:
99
+
102
100
- Declare each variable name once.
103
101
- Filter `𝒩` and `ℒ` down to the subsets that actually use the special resource.
104
-
-Keep bounds in `constraints_resource` when they depend on dispatch logic.
102
+
-You can create resource dependent bounds as well.
105
103
106
104
```julia
107
105
function EMB.variables_flow_resource(
@@ -111,19 +109,18 @@ function EMB.variables_flow_resource(
111
109
𝒯,
112
110
modeltype::EnergyModel,
113
111
)
114
-
output_nodes=filter(n ->any(p ∈ 𝒫 for p ∈outputs(n)), 𝒩)
115
-
input_nodes=filter(n ->any(p ∈ 𝒫 for p ∈inputs(n)), 𝒩)
112
+
𝒩ᵒᵘᵗ=filter(n ->any(p ∈ 𝒫 for p ∈outputs(n)), 𝒩)
113
+
𝒩ⁱⁿ=filter(n ->any(p ∈ 𝒫 for p ∈inputs(n)), 𝒩)
116
114
117
-
@variable(
118
-
m, energy_potential_node_out[
119
-
n ∈output_nodes, t ∈𝒯, p ∈𝒫; p ∈outputs(n)
120
-
]
115
+
@variable(m,
116
+
lower_limit(p) ≤
117
+
energy_potential_node_out[n ∈𝒩ᵒᵘᵗ, 𝒯, p ∈intersect(outputs(n), 𝒫)] ≤
118
+
upper_limit(p)
121
119
)
122
-
123
-
@variable(
124
-
m, energy_potential_node_in[
125
-
n ∈ input_nodes, t ∈ 𝒯, p ∈intersect(inputs(n), 𝒫)
126
-
]
120
+
@variable(m,
121
+
lower_limit(p) ≤
122
+
energy_potential_node_in[n ∈ 𝒩ⁱⁿ, 𝒯, p ∈intersect(inputs(n), 𝒫)] ≤
123
+
upper_limit(p)
127
124
)
128
125
end
129
126
@@ -140,9 +137,11 @@ function EMB.variables_flow_resource(
140
137
end
141
138
```
142
139
143
-
## 4. Add resource-specific constraints
140
+
###4. Add resource-specific constraints
144
141
145
-
Use [`constraints_resource`](@ref) for custom node or link behavior.
142
+
Create a new method [`constraints_resource`](@ref) for custom node or link behavior.
143
+
These methods can be either for the complete set of [`Node`](@ref EnergyModelsBase.Node) and [`Link`](@ref)s or alternatively for only a specified subset of nodes.
144
+
If you only specify it for a subset of nodes, it is important that the new resource is only an `input` or `output` of this subset.
146
145
147
146
```julia
148
147
function EMB.constraints_resource(
@@ -159,48 +158,6 @@ function EMB.constraints_resource(
0 commit comments