Passed
Pull Request — dev (#1208)
by Patrik
04:04 queued 02:13
created

tests.test_flows.test_non_convex_flow   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 135
Duplicated Lines 19.26 %

Importance

Changes 0
Metric Value
wmc 9
eloc 78
dl 26
loc 135
rs 10
c 0
b 0
f 0

9 Functions

Rating   Name   Duplication   Size   Complexity  
A test_activity_costs() 0 12 1
A test_maximum_shutdowns() 0 10 1
A test_startup_costs_start_off() 13 13 1
A test_shutdown_costs_start_on() 0 13 1
A test_startup_costs_start_on() 13 13 1
A test_initial_status_on() 0 11 1
A test_initial_status_off() 0 10 1
A test_maximum_startups() 0 10 1
A test_inactivity_costs() 0 11 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
# -*- coding: utf-8 -*-
2
3
"""Tests for Flows with NonConvex attribute
4
5
SPDX-FileCopyrightText: Deutsches Zentrum für Luft- und Raumfahrt e.V.
6
SPDX-FileCopyrightText: Patrik Schönfeldt
7
8
SPDX-License-Identifier: MIT
9
"""
10
11
from oemof import solph
12
13
from . import _run_flow_model
14
15
16
def test_initial_status_off():
17
    # negative costs but turned off initially
18
    flow = solph.flows.Flow(
19
        nominal_capacity=10,
20
        nonconvex=solph.NonConvex(initial_status=0, minimum_downtime=5),
21
        variable_costs=-1,
22
    )
23
    flow_result = _run_flow_model(flow)
24
25
    assert (flow_result["flow"][:-1] == 5 * [0] + 5 * [10]).all()
26
27
28
def test_maximum_shutdowns():
29
    flow = solph.flows.Flow(
30
        nominal_capacity=10,
31
        min=0.5,
32
        nonconvex=solph.NonConvex(maximum_shutdowns=1),
33
        variable_costs=[1, -2, 1, 1, 1, -5, 1, 1, 1, -2],
34
    )
35
    flow_result = _run_flow_model(flow)
36
37
    assert list(flow_result["status"][:-1]) == [0, 1, 1, 1, 1, 1, 0, 0, 0, 1]
38
39
40
def test_maximum_startups():
41
    flow = solph.flows.Flow(
42
        nominal_capacity=10,
43
        min=0.5,
44
        nonconvex=solph.NonConvex(maximum_startups=1),
45
        variable_costs=[1, -4, 1, 1, 1, -5, 1, 1, 5, -3],
46
    )
47
    flow_result = _run_flow_model(flow)
48
49
    assert list(flow_result["status"][:-1]) == [0, 1, 1, 1, 1, 1, 0, 0, 0, 0]
50
51
52
def test_initial_status_on():
53
    # positive costs but turned on initially
54
    flow = solph.flows.Flow(
55
        nominal_capacity=10,
56
        min=0.5,
57
        nonconvex=solph.NonConvex(initial_status=1, minimum_uptime=3),
58
        variable_costs=1,
59
    )
60
    flow_result = _run_flow_model(flow)
61
62
    assert (flow_result["flow"][:-1] == 3 * [5] + 7 * [0]).all()
63
64
65
def test_activity_costs():
66
    # activity costs higher then revenue for first time steps
67
    flow = solph.flows.Flow(
68
        nominal_capacity=10,
69
        min=0.1,
70
        max=[0.1] + [i * 0.1 for i in range(1, 10)],
71
        nonconvex=solph.NonConvex(activity_costs=9 * [1] + [10]),
72
        variable_costs=-0.45,
73
    )
74
    flow_result = _run_flow_model(flow)["flow"][:-1]
75
76
    assert (flow_result == [0, 0, 0, 3, 4, 5, 6, 7, 8, 0]).all()
77
78
79
def test_inactivity_costs():
80
    # inactivity costs lower then running costs for middle time steps
81
    flow = solph.flows.Flow(
82
        nominal_capacity=10,
83
        min=[i * 0.1 for i in range(10)],
84
        nonconvex=solph.NonConvex(inactivity_costs=9 * [1] + [10]),
85
        variable_costs=0.45,
86
    )
87
    flow_result = _run_flow_model(flow)["flow"][:-1]
88
89
    assert (flow_result == [0, 1, 2, 0, 0, 0, 0, 0, 0, 9]).all()
90
91
92 View Code Duplication
def test_startup_costs_start_off():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
93
    price_pattern = [1, 1, 1, -4, 1, 1, 1, -4, 1, 1]
94
95
    # startup costs higher then effect of shutting down
96
    flow = solph.flows.Flow(
97
        nominal_capacity=10,
98
        min=0.1,
99
        nonconvex=solph.NonConvex(startup_costs=5, initial_status=0),
100
        variable_costs=price_pattern,
101
    )
102
    flow_result = _run_flow_model(flow)
103
104
    assert (flow_result["flow"][:-1] == [0, 0, 0, 10, 1, 1, 1, 10, 0, 0]).all()
105
106
107 View Code Duplication
def test_startup_costs_start_on():
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
108
    price_pattern = [1, 1, 1, -4, 1, 1, 1, -4, 1, 1]
109
110
    # startup costs higher then effect of shutting down
111
    flow = solph.flows.Flow(
112
        nominal_capacity=10,
113
        min=0.1,
114
        nonconvex=solph.NonConvex(startup_costs=5, initial_status=1),
115
        variable_costs=price_pattern,
116
    )
117
    flow_result = _run_flow_model(flow)
118
119
    assert (flow_result["flow"][:-1] == [1, 1, 1, 10, 1, 1, 1, 10, 0, 0]).all()
120
121
122
def test_shutdown_costs_start_on():
123
    price_pattern = [1, 1, 1, -4, 1, 1, 1, -4, 1, 1]
124
125
    # shutdown costs higher then effect of shutting down
126
    flow = solph.flows.Flow(
127
        nominal_capacity=10,
128
        min=0.1,
129
        nonconvex=solph.NonConvex(shutdown_costs=5, initial_status=1),
130
        variable_costs=price_pattern,
131
    )
132
    flow_result = _run_flow_model(flow)
133
134
    assert (flow_result["flow"][:-1] == [1, 1, 1, 10, 1, 1, 1, 10, 1, 1]).all()
135