test_simple_dispatch_one   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 122
Duplicated Lines 77.87 %

Importance

Changes 0
Metric Value
wmc 2
eloc 63
dl 95
loc 122
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B test_dispatch_one_time_step() 95 95 2

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
# -*- coding: utf-8 -*-
2
3
"""This example shows how to create an energysystem with oemof objects and
4
solve it with the solph module.
5
6
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
7
by the contributors recorded in the version control history of the file,
8
available from its original location
9
oemof/tests/test_scripts/test_solph/test_simple_dispatch/test_simple_dispatch.py
10
11
SPDX-License-Identifier: MIT
12
"""
13
14
import pytest
15
16
from oemof.solph import EnergySystem
17
from oemof.solph import Model
18
from oemof.solph import processing
19
from oemof.solph import views
20
from oemof.solph.buses import Bus
21
from oemof.solph.components import Converter
22
from oemof.solph.components import Sink
23
from oemof.solph.components import Source
24
from oemof.solph.flows import Flow
25
26
27 View Code Duplication
def test_dispatch_one_time_step(solver="cbc"):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
28
    """Create an energy system and optimize the dispatch at least costs."""
29
30
    # ######################### create energysystem components ################
31
    # resource buses
32
    bgas = Bus(label="gas", balanced=False)
33
34
    # electricity and heat
35
    bel = Bus(label="b_el")
36
    bth = Bus(label="b_th")
37
38
    # an excess and a shortage variable can help to avoid infeasible problems
39
    excess_el = Sink(label="excess_el", inputs={bel: Flow()})
40
41
    # sources
42
    wind = Source(
43
        label="wind", outputs={bel: Flow(fix=0.5, nominal_capacity=66.3)}
44
    )
45
46
    # demands (electricity/heat)
47
    demand_el = Sink(
48
        label="demand_elec", inputs={bel: Flow(nominal_capacity=85, fix=0.3)}
49
    )
50
51
    demand_th = Sink(
52
        label="demand_therm", inputs={bth: Flow(nominal_capacity=40, fix=0.2)}
53
    )
54
55
    # combined heat and power plant (chp)
56
    pp_chp = Converter(
57
        label="pp_chp",
58
        inputs={bgas: Flow()},
59
        outputs={
60
            bel: Flow(nominal_capacity=30, variable_costs=42),
61
            bth: Flow(nominal_capacity=40),
62
        },
63
        conversion_factors={bel: 0.3, bth: 0.4},
64
    )
65
66
    # heatpump with a coefficient of performance (COP) of 3
67
    b_heat_source = Bus(label="b_heat_source")
68
69
    heat_source = Source(label="heat_source", outputs={b_heat_source: Flow()})
70
71
    cop = 3
72
    heat_pump = Converter(
73
        label="heat_pump",
74
        inputs={bel: Flow(), b_heat_source: Flow()},
75
        outputs={bth: Flow(nominal_capacity=10)},
76
        conversion_factors={bel: 1 / 3, b_heat_source: (cop - 1) / cop},
77
    )
78
79
    energysystem = EnergySystem(timeincrement=[1])
80
    energysystem.add(
81
        bgas,
82
        bel,
83
        bth,
84
        excess_el,
85
        wind,
86
        demand_el,
87
        demand_th,
88
        pp_chp,
89
        b_heat_source,
90
        heat_source,
91
        heat_pump,
92
    )
93
94
    # ################################ optimization ###########################
95
96
    # create optimization model based on energy_system
97
    optimization_model = Model(energysystem=energysystem)
98
99
    # solve problem
100
    optimization_model.solve(solver=solver)
101
102
    # write back results from optimization object to energysystem
103
    optimization_model.results()
104
105
    # ################################ results ################################
106
    data = views.node(processing.results(model=optimization_model), "b_el")
107
108
    # generate results to be evaluated in tests
109
    results = data["sequences"].sum(axis=0).to_dict()
110
111
    print("DateTimeIndex:", data["sequences"].index)
112
113
    test_results = {
114
        (("wind", "b_el"), "flow"): 33,
115
        (("b_el", "demand_elec"), "flow"): 26,
116
        (("b_el", "excess_el"), "flow"): 5,
117
        (("b_el", "heat_pump"), "flow"): 3,
118
    }
119
120
    for key in test_results.keys():
121
        assert results[key] == pytest.approx(test_results[key], abs=0.5)
122