Passed
Pull Request — dev (#817)
by Uwe
04:43 queued 03:19
created

test_simple_dispatch_one_explicit_timemode   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 125
Duplicated Lines 77.6 %

Importance

Changes 0
Metric Value
wmc 2
eloc 65
dl 97
loc 125
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B test_dispatch_one_time_step() 97 97 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
from nose.tools import eq_
15
from oemof.network.network import Node
16
17
from oemof.solph import EnergySystem
18
from oemof.solph import Model
19
from oemof.solph import processing
20
from oemof.solph import views
21
from oemof.solph.buses import Bus
22
from oemof.solph.components import Sink
23
from oemof.solph.components import Source
24
from oemof.solph.components import Transformer
25
from oemof.solph.flows import Flow
26
27
28 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...
29
    """Create an energy system and optimize the dispatch at least costs."""
30
31
    # ######################### create energysystem components ################
32
    Node.registry = None
33
34
    # resource buses
35
    bgas = Bus(label="gas", balanced=False)
36
37
    # electricity and heat
38
    bel = Bus(label="b_el")
39
    bth = Bus(label="b_th")
40
41
    # an excess and a shortage variable can help to avoid infeasible problems
42
    excess_el = Sink(label="excess_el", inputs={bel: Flow()})
43
44
    # sources
45
    wind = Source(
46
        label="wind", outputs={bel: Flow(fix=0.5, nominal_value=66.3)}
47
    )
48
49
    # demands (electricity/heat)
50
    demand_el = Sink(
51
        label="demand_elec", inputs={bel: Flow(nominal_value=85, fix=0.3)}
52
    )
53
54
    demand_th = Sink(
55
        label="demand_therm", inputs={bth: Flow(nominal_value=40, fix=0.2)}
56
    )
57
58
    # combined heat and power plant (chp)
59
    pp_chp = Transformer(
60
        label="pp_chp",
61
        inputs={bgas: Flow()},
62
        outputs={
63
            bel: Flow(nominal_value=30, variable_costs=42),
64
            bth: Flow(nominal_value=40),
65
        },
66
        conversion_factors={bel: 0.3, bth: 0.4},
67
    )
68
69
    # heatpump with a coefficient of performance (COP) of 3
70
    b_heat_source = Bus(label="b_heat_source")
71
72
    heat_source = Source(label="heat_source", outputs={b_heat_source: Flow()})
73
74
    cop = 3
75
    heat_pump = Transformer(
76
        label="heat_pump",
77
        inputs={bel: Flow(), b_heat_source: Flow()},
78
        outputs={bth: Flow(nominal_value=10)},
79
        conversion_factors={bel: 1 / 3, b_heat_source: (cop - 1) / cop},
80
    )
81
82
    energysystem = EnergySystem(timeincrement=[1], timemode="explicit")
83
    energysystem.add(
84
        bgas,
85
        bel,
86
        bth,
87
        excess_el,
88
        wind,
89
        demand_el,
90
        demand_th,
91
        pp_chp,
92
        b_heat_source,
93
        heat_source,
94
        heat_pump,
95
    )
96
97
    # ################################ optimization ###########################
98
99
    # create optimization model based on energy_system
100
    optimization_model = Model(energysystem=energysystem)
101
102
    # solve problem
103
    optimization_model.solve(solver=solver)
104
105
    # write back results from optimization object to energysystem
106
    optimization_model.results()
107
108
    # ################################ results ################################
109
    data = views.node(processing.results(model=optimization_model), "b_el")
110
111
    # generate results to be evaluated in tests
112
    results = data["sequences"].sum(axis=0).to_dict()
113
114
    print("DateTimeIndex:", data["sequences"].index)
115
116
    test_results = {
117
        (("wind", "b_el"), "flow"): 33,
118
        (("b_el", "demand_elec"), "flow"): 26,
119
        (("b_el", "excess_el"), "flow"): 5,
120
        (("b_el", "heat_pump"), "flow"): 3,
121
    }
122
123
    for key in test_results.keys():
124
        eq_(int(round(results[key])), int(round(test_results[key])))
125