Completed
Push — dev ( 68ddc7...49e927 )
by Patrik
58s queued 48s
created

test_connect_invest.test_connect_invest()   B

Complexity

Conditions 2

Size

Total Lines 124
Code Lines 83

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 83
dl 0
loc 124
rs 7.5563
c 0
b 0
f 0
cc 2
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
"""Connecting different investment variables.
4
5
This file is part of project oemof (github.com/oemof/oemof). It's copyrighted
6
by the contributors recorded in the version control history of the file,
7
available from its original location
8
oemof/tests/test_scripts/test_solph/test_connect_invest/test_connect_invest.py
9
10
SPDX-License-Identifier: MIT
11
"""
12
13
import logging
14
import os
15
16
import pandas as pd
17
import pytest
18
19
from oemof.solph import EnergySystem
20
from oemof.solph import Investment
21
from oemof.solph import Model
22
from oemof.solph import components as components
23
from oemof.solph import constraints
24
from oemof.solph import processing
25
from oemof.solph import views
26
from oemof.solph.buses import Bus
27
from oemof.solph.flows import Flow
28
29
30
def test_connect_invest():
31
    date_time_index = pd.date_range("1/1/2012", periods=24 * 7, freq="h")
32
33
    es = EnergySystem(timeindex=date_time_index, infer_last_interval=True)
34
35
    # Read data file
36
    full_filename = os.path.join(
37
        os.path.dirname(__file__), "connect_invest.csv"
38
    )
39
    data = pd.read_csv(full_filename, sep=",")
40
41
    logging.info("Create oemof objects")
42
43
    # create electricity bus
44
    bel1 = Bus(label="electricity1")
45
    bel2 = Bus(label="electricity2")
46
    es.add(bel1, bel2)
47
48
    # create excess component for the electricity bus to allow overproduction
49
    es.add(components.Sink(label="excess_bel", inputs={bel2: Flow()}))
50
    es.add(
51
        components.Source(
52
            label="shortage", outputs={bel2: Flow(variable_costs=50000)}
53
        )
54
    )
55
56
    # create fixed source object representing wind power plants
57
    es.add(
58
        components.Source(
59
            label="wind",
60
            outputs={bel1: Flow(fix=data["wind"], nominal_capacity=1000000)},
61
        )
62
    )
63
64
    # create simple sink object representing the electrical demand
65
    es.add(
66
        components.Sink(
67
            label="demand",
68
            inputs={bel1: Flow(fix=data["demand_el"], nominal_capacity=1)},
69
        )
70
    )
71
72
    storage = components.GenericStorage(
73
        label="storage",
74
        inputs={bel1: Flow(variable_costs=10e10)},
75
        outputs={bel1: Flow(variable_costs=10e10)},
76
        loss_rate=0.00,
77
        initial_storage_level=0,
78
        invest_relation_input_capacity=1 / 6,
79
        invest_relation_output_capacity=1 / 6,
80
        inflow_conversion_factor=1,
81
        outflow_conversion_factor=0.8,
82
        nominal_capacity=Investment(ep_costs=0.2),
83
    )
84
    es.add(storage)
85
86
    line12 = components.Converter(
87
        label="line12",
88
        inputs={bel1: Flow()},
89
        outputs={bel2: Flow(nominal_capacity=Investment(ep_costs=20))},
90
    )
91
    es.add(line12)
92
93
    line21 = components.Converter(
94
        label="line21",
95
        inputs={bel2: Flow()},
96
        outputs={bel1: Flow(nominal_capacity=Investment(ep_costs=20))},
97
    )
98
    es.add(line21)
99
100
    om = Model(es)
101
102
    constraints.equate_variables(
103
        om,
104
        om.InvestmentFlowBlock.invest[line12, bel2, 0],
105
        om.InvestmentFlowBlock.invest[line21, bel1, 0],
106
        2,
107
    )
108
    constraints.equate_variables(
109
        om,
110
        om.InvestmentFlowBlock.invest[line12, bel2, 0],
111
        om.GenericInvestmentStorageBlock.invest[storage, 0],
112
    )
113
114
    # if tee_switch is true solver messages will be displayed
115
    logging.info("Solve the optimization problem")
116
    om.solve(solver="cbc", tee=True)
117
118
    # check if the new result object is working for custom components
119
    results = processing.results(om)
120
121
    my_results = dict()
122
    my_results["line12"] = (
123
        views.node(results, "line12")["scalars"]
124
        .loc[[(("line12", "electricity2"), "invest")]]
125
        .iloc[0]
126
    )
127
128
    my_results["line21"] = (
129
        views.node(results, "line21")["scalars"]
130
        .loc[[(("line21", "electricity1"), "invest")]]
131
        .iloc[0]
132
    )
133
134
    stor_res = views.node(results, "storage")["scalars"]
135
    my_results["storage_in"] = stor_res[
136
        [(("electricity1", "storage"), "invest")]
137
    ].iloc[0]
138
    my_results["storage"] = stor_res[[(("storage", "None"), "invest")]].iloc[0]
139
    my_results["storage_out"] = stor_res[
140
        [(("storage", "electricity1"), "invest")]
141
    ].iloc[0]
142
143
    connect_invest_dict = {
144
        "line12": 814705,
145
        "line21": 1629410,
146
        "storage": 814705,
147
        "storage_in": 135784,
148
        "storage_out": 135784,
149
    }
150
151
    for key in connect_invest_dict.keys():
152
        assert my_results[key] == pytest.approx(
153
            connect_invest_dict[key], abs=0.5
154
        )
155