Passed
Pull Request — dev (#1193)
by
unknown
01:54
created

temperature_level_facade.main()   B

Complexity

Conditions 1

Size

Total Lines 56
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 39
dl 0
loc 56
rs 8.9439
c 0
b 0
f 0
cc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
# -*- coding: utf-8 -*-
2
3
"""
4
This example gives a simplified idea how Facades and 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(solph.Facade):
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, facade_type=type(self)
41
        )
42
43
    def define_subnetwork(self):
44
        el_bus = self.subnode(
45
            solph.Bus,
46
            local_name="el",
47
            inputs={
48
                self.el_supply_bus: solph.Flow(
49
                    nominal_capacity=self.el_power_limit,
50
                ),
51
            },
52
        )
53
54
        for target, temperature in self.heat_demand_buses.items():
55
            cop = self.cpf * temperature / (temperature - self.temperature)
56
57
            self.subnode(
58
                solph.components.Converter,
59
                local_name=f"hp_{temperature}",
60
                inputs={el_bus: solph.Flow()},
61
                outputs={target: solph.Flow()},
62
                conversion_factors={el_bus: cop},
63
            )
64
65
66
def main():
67
68
    date_time_index = solph.create_time_index(2025, number=2)
69
70
    # create the energysystem and assign the time index
71
    es = solph.EnergySystem(
72
        timeindex=date_time_index, infer_last_interval=False
73
    )
74
75
    house = SubNetwork("house")
76
77
    el_bus = house.subnode(
78
        solph.Bus,
79
        local_name="el",
80
    )
81
    el_source = solph.components.Source(
82
        label="el_grid",
83
        outputs={el_bus: solph.Flow(variable_costs=0.3)},
84
    )
85
    es.add(house, el_bus, el_source)
86
87
    heat_demands = house.subnode(
88
        SubNetwork,
89
        local_name="heat demand",
90
    )
91
    demand_bus_dhw = heat_demands.subnode(solph.Bus, "b_dhw")
92
    demand_bus_sh = heat_demands.subnode(solph.Bus, "b_sh")
93
94
    heat_demands.subnode(
95
        solph.components.Sink,
96
        local_name="d_dhw",
97
        inputs={demand_bus_dhw: solph.Flow(nominal_capacity=1, fix=[0, 0.2])},
98
    )
99
    heat_demands.subnode(
100
        solph.components.Sink,
101
        local_name="d_sh",
102
        inputs={demand_bus_sh: solph.Flow(nominal_capacity=1, fix=[0.4, 2.1])},
103
    )
104
    es.add(heat_demands)
105
    hp = house.subnode(
106
        HeatPump,
107
        local_name="hp",
108
        el_supply=el_bus,
109
        heat_demand={demand_bus_dhw: 60.0, demand_bus_sh: 30},
110
        source_temperature=[3, 0],
111
        cpf=0.45,
112
        el_power_limit=3,
113
    )
114
    es.add(hp)
115
116
    model = solph.Model(es)
117
    model.solve()
118
119
    results = solph.Results(model)
120
121
    print(results.flow)
122
123
124
if __name__ == "__main__":
125
    main()
126