Passed
Pull Request — dev (#1215)
by Patrik
01:55
created

subnetwork_example   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 125
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 77
dl 0
loc 125
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A HeatPump.__init__() 0 40 2

1 Function

Rating   Name   Duplication   Size   Complexity  
B main() 0 56 1
1
# -*- coding: utf-8 -*-
2
3
"""
4
This example gives a simplified idea how SubNetworks
5
might be used to work with discretised temperatures.
6
7
SPDX-FileCopyrightText: Patrik Schönfeldt <[email protected]>
8
SPDX-FileCopyrightText: Deutsches Zentrum für Luft- und Raumfahrt (DLR)
9
10
SPDX-License-Identifier: MIT
11
"""
12
13
import numpy as np
14
15
from oemof.network import SubNetwork
16
17
from oemof import solph
18
19
20
class HeatPump(SubNetwork):
21
22
    def __init__(
23
        self,
24
        label: str,
25
        el_supply: solph.Bus,
26
        heat_demand: dict[solph.Bus, float],
27
        source_temperature: float | list[float],
28
        cpf: float = 0.5,
29
        el_power_limit: float = None,
30
        parent_node=None,
31
    ):
32
        self.el_supply_bus = el_supply
33
        self.heat_demand_buses = heat_demand
34
        self.temperature = np.array(source_temperature)
35
36
        self.cpf = cpf
37
        self.el_power_limit = el_power_limit
38
39
        super().__init__(
40
            label=label, parent_node=parent_node
41
        )
42
43
        el_bus = self.subnode(
44
            solph.Bus,
45
            local_name="el",
46
            inputs={
47
                self.el_supply_bus: solph.Flow(
48
                    nominal_capacity=self.el_power_limit,
49
                ),
50
            },
51
        )
52
53
        for target, temperature in self.heat_demand_buses.items():
54
            cop = self.cpf * temperature / (temperature - self.temperature)
55
56
            self.subnode(
57
                solph.components.Converter,
58
                local_name=f"hp_{temperature}",
59
                inputs={el_bus: solph.Flow()},
60
                outputs={target: solph.Flow()},
61
                conversion_factors={el_bus: cop},
62
            )
63
64
65
def main():
66
67
    date_time_index = solph.create_time_index(2025, number=2)
68
69
    # create the energysystem and assign the time index
70
    es = solph.EnergySystem(
71
        timeindex=date_time_index, infer_last_interval=False
72
    )
73
74
    house = SubNetwork("house")
75
76
    el_bus = house.subnode(
77
        solph.Bus,
78
        local_name="el",
79
    )
80
    el_source = solph.components.Source(
81
        label="el_grid",
82
        outputs={el_bus: solph.Flow(variable_costs=0.3)},
83
    )
84
    es.add(house, el_bus, el_source)
85
86
    heat_demands = house.subnode(
87
        SubNetwork,
88
        local_name="heat demand",
89
    )
90
    demand_bus_dhw = heat_demands.subnode(solph.Bus, "b_dhw")
91
    demand_bus_sh = heat_demands.subnode(solph.Bus, "b_sh")
92
93
    heat_demands.subnode(
94
        solph.components.Sink,
95
        local_name="d_dhw",
96
        inputs={demand_bus_dhw: solph.Flow(nominal_capacity=1, fix=[0, 0.2])},
97
    )
98
    heat_demands.subnode(
99
        solph.components.Sink,
100
        local_name="d_sh",
101
        inputs={demand_bus_sh: solph.Flow(nominal_capacity=1, fix=[0.4, 2.1])},
102
    )
103
    es.add(heat_demands)
104
    hp = house.subnode(
105
        HeatPump,
106
        local_name="hp",
107
        el_supply=el_bus,
108
        heat_demand={demand_bus_dhw: 60.0, demand_bus_sh: 30},
109
        source_temperature=[3, 0],
110
        cpf=0.45,
111
        el_power_limit=3,
112
    )
113
    es.add(hp)
114
115
    model = solph.Model(es)
116
    model.solve()
117
118
    results = solph.Results(model)
119
120
    print(results.flow)
121
122
123
if __name__ == "__main__":
124
    main()
125