tests.test_eval_economy   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 143
Duplicated Lines 66.43 %

Importance

Changes 0
Metric Value
wmc 3
eloc 82
dl 95
loc 143
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A results_opex() 49 49 1
A results_capex() 46 46 1
A results_storage() 0 39 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
from oemof import solph
2
3
4 View Code Duplication
def results_opex():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
5
6
    date_time_index = solph.create_time_index(2025, number=4)
7
8
    # create the energysystem and assign the time index
9
    energysystem = solph.EnergySystem(
10
        timeindex=date_time_index, infer_last_interval=True
11
    )
12
13
    el_bus = solph.buses.Bus("el_bus")
14
    heat_bus = solph.buses.Bus("heat_bus")
15
16
    energysystem.add(el_bus, heat_bus)
17
18
    source = solph.components.Source(
19
        "source",
20
        outputs={
21
            el_bus: solph.flows.Flow(variable_costs=[10, 15, 20, 30, 15])
22
        },
23
    )
24
25
    demand = solph.components.Sink(
26
        "heat_demand",
27
        inputs={
28
            heat_bus: solph.flows.Flow(fix=[15, 42, 3, 9, 12], nominal_value=1)
29
        },
30
    )
31
32
    hp = solph.components.Converter(
33
        "heat_pump",
34
        inputs={el_bus: solph.flows.Flow()},
35
        outputs={heat_bus: solph.flows.Flow()},
36
        conversion_factors={el_bus: 1 / 3},
37
    )
38
39
    energysystem.add(source, demand, hp)
40
41
    energysystem_model = solph.Model(energysystem)
42
43
    energysystem_model.solve(solver="cbc", solve_kwargs={"tee": True})
44
45
    results = solph.Results(energysystem_model)
46
47
    assert results.to_df("variable_costs").iloc[:, 2].values == [
48
        50,
49
        210,
50
        20,
51
        90,
52
        60,
53
    ]
54
55
56 View Code Duplication
def results_capex():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
57
58
    date_time_index = solph.create_time_index(2025, number=4)
59
60
    # create the energysystem and assign the time index
61
    energysystem = solph.EnergySystem(
62
        timeindex=date_time_index, infer_last_interval=True
63
    )
64
65
    el_bus = solph.buses.Bus("el_bus")
66
    heat_bus = solph.buses.Bus("heat_bus")
67
68
    energysystem.add(el_bus, heat_bus)
69
70
    source = solph.components.Source(
71
        "source",
72
        outputs={el_bus: solph.flows.Flow()},
73
    )
74
75
    demand = solph.components.Sink(
76
        "heat_demand",
77
        inputs={
78
            heat_bus: solph.flows.Flow(fix=[15, 42, 3, 9, 12], nominal_value=1)
79
        },
80
    )
81
82
    hp = solph.components.Converter(
83
        "heat_pump",
84
        inputs={el_bus: solph.flows.Flow()},
85
        outputs={
86
            heat_bus: solph.flows.Flow(
87
                solph.Investment(ep_costs=100, fixed_costs=200)
88
            )
89
        },
90
        conversion_factors={el_bus: 1 / 3},
91
    )
92
93
    energysystem.add(source, demand, hp)
94
95
    energysystem_model = solph.Model(energysystem)
96
97
    energysystem_model.solve(solver="cbc", solve_kwargs={"tee": True})
98
99
    results = solph.Results(energysystem_model)
100
101
    assert results.to_df("investment_costs").iloc[0, 0] == 42 * 100 + 200
102
103
104
def results_storage():
105
    date_time_index = solph.create_time_index(2025, number=4)
106
107
    # create the energysystem and assign the time index
108
    energysystem = solph.EnergySystem(
109
        timeindex=date_time_index, infer_last_interval=True
110
    )
111
112
    bus = solph.buses.Bus("bus")
113
    energysystem.add(bus)
114
115
    source = solph.components.Source(
116
        "source",
117
        outputs={
118
            bus: solph.flows.Flow(fix=[20, 20, 10, 0, 0], nominal_value=1)
119
        },
120
    )
121
122
    demand = solph.components.Sink(
123
        "demand",
124
        inputs={bus: solph.flows.Flow(fix=10, nominal_value=1)},
125
    )
126
127
    storage = solph.components.GenericStorage(
128
        "storage",
129
        inputs={bus: solph.flows.Flow(solph.Investment(ep_costs=10))},
130
        outputs={bus: solph.flows.Flow()},
131
        nominal_capacity=solph.Investment(ep_costs=100),
132
    )
133
    energysystem.add(source, demand, storage)
134
135
    energysystem_model = solph.Model(energysystem)
136
137
    energysystem_model.solve(solver="cbc", solve_kwargs={"tee": True})
138
139
    results = solph.Results(energysystem_model)
140
141
    assert results.to_df("investment_costs").iloc[0, 0] == 100
142
    assert results.to_df("investment_costs").iloc[0, 1] == 2000
143