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

Visio_grid_plot   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 211
Duplicated Lines 5.21 %

Importance

Changes 0
Metric Value
wmc 2
eloc 104
dl 11
loc 211
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
B main() 0 159 1
A plot_figures_for() 11 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
"""
4
General description
5
-------------------
6
This Code generates the picture of an energysystem used for the grid icons in
7
the docs
8
9
License
10
-------
11
`MIT license <https://github.com/oemof/oemof-solph/blob/dev/LICENSE>`_
12
"""
13
###########################################################################
14
# imports
15
###########################################################################
16
17
import logging
18
import os
19
20
import matplotlib.pyplot as plt
21
import pandas as pd
22
import plotly.io as pio
23
from oemof.tools import logger
24
from oemof.visio import ESGraphRenderer
25
26
from oemof.solph import EnergySystem
27
from oemof.solph import Model
28
from oemof.solph import buses
29
from oemof.solph import components
30
from oemof.solph import create_time_index
31
from oemof.solph import flows
32
from oemof.solph import processing
33
34
35 View Code Duplication
def plot_figures_for(element: dict) -> None:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
36
    figure, axes = plt.subplots(figsize=(10, 5))
37
    element["sequences"].plot(ax=axes, kind="line", drawstyle="steps-post")
38
    plt.legend(
39
        loc="upper center",
40
        prop={"size": 8},
41
        bbox_to_anchor=(0.5, 1.25),
42
        ncol=2,
43
    )
44
    figure.subplots_adjust(top=0.8)
45
    plt.show()
46
47
48
# uses data from the basic example
49
def main():
50
51
    # *************************************************************************
52
    # ********** PART 1 - Define and optimise the energy system ***************
53
    # *************************************************************************
54
55
    # Read data file
56
    file_name = os.path.realpath(
57
        os.path.join(
58
            __file__,
59
            "..",
60
            "..",
61
            "..",
62
            "..",
63
            r"examples\result_object\time_series.csv",
64
        )
65
    )
66
    data = pd.read_csv(file_name)
67
68
    solver = "cbc"  # 'glpk', 'gurobi',....
69
    number_of_time_steps = len(data)
70
    solver_verbose = False  # show/hide solver output
71
72
    # initiate the logger (see the API docs for more information)
73
    logger.define_logging(
74
        logfile="oemof_example.log",
75
        screen_level=logging.INFO,
76
        file_level=logging.INFO,
77
    )
78
79
    logging.info("Initialize the energy system")
80
    date_time_index = create_time_index(2012, number=number_of_time_steps)
81
82
    # create the energysystem and assign the time index
83
    energysystem = EnergySystem(
84
        timeindex=date_time_index, infer_last_interval=False
85
    )
86
87
    ##########################################################################
88
    # Create oemof objects
89
    ##########################################################################
90
91
    logging.info("Create oemof objects")
92
93
    # The bus objects were assigned to variables which makes it easier to
94
    # connect components to these buses (see below).
95
96
    # create natural gas bus
97
    bus_gas = buses.Bus(label="natural gas")
98
99
    # create electricity bus
100
    bus_electricity = buses.Bus(label="electricity")
101
102
    # create heat bus
103
    bus_heat = buses.Bus(label="heat")
104
105
    # adding the buses to the energy system
106
    energysystem.add(bus_gas, bus_electricity, bus_heat)
107
108
    # create sink for heat demand
109
    energysystem.add(
110
        components.Sink(
111
            label="Heat Demand",
112
            inputs={
113
                bus_heat: flows.Flow(fix=data["demand_el"], nominal_value=2)
114
            },
115
        )
116
    )
117
118
    # create source object representing the gas commodity
119
    energysystem.add(
120
        components.Source(
121
            label="Gas Grid",
122
            outputs={bus_gas: flows.Flow()},
123
        )
124
    )
125
126
    # create fixed source object representing power grid
127
    energysystem.add(
128
        components.Source(
129
            label="Power Grid",
130
            outputs={bus_electricity: flows.Flow()},
131
        )
132
    )
133
134
    # create fixed source object representing pv power plants
135
    energysystem.add(
136
        components.Source(
137
            label="Pv Plant",
138
            outputs={
139
                bus_electricity: flows.Flow(
140
                    fix=data["pv"], nominal_value=582000
141
                )
142
            },
143
        )
144
    )
145
146
    # create simple sink object representing the electrical demand
147
    # nominal_capacity is set to 1 because demand_el is not a normalised series
148
    energysystem.add(
149
        components.Sink(
150
            label="Electricity Demand",
151
            inputs={
152
                bus_electricity: flows.Flow(
153
                    fix=data["demand_el"], nominal_value=1
154
                )
155
            },
156
        )
157
    )
158
159
    # create simple converter object representing a gas boiler
160
    energysystem.add(
161
        components.Converter(
162
            label="Gas boiler",
163
            inputs={bus_gas: flows.Flow()},
164
            outputs={bus_heat: flows.Flow(variable_costs=50)},
165
            conversion_factors={bus_electricity: 0.58},
166
        )
167
    )
168
169
    # create simple converter object representing a heatpump
170
    energysystem.add(
171
        components.Converter(
172
            label="Heatpump",
173
            inputs={bus_electricity: flows.Flow()},
174
            outputs={bus_heat: flows.Flow(variable_costs=50)},
175
            conversion_factors={bus_electricity: 0.58},
176
        )
177
    )
178
179
    ##########################################################################
180
    # Optimise the energy system and plot the results
181
    ##########################################################################
182
183
    logging.info("Optimise the energy system")
184
185
    # initialise the operational model
186
    energysystem_model = Model(energysystem)
187
188
    esgr = ESGraphRenderer(
189
        energysystem_model,
190
        legend=False,
191
        filepath=os.path.realpath(
192
            os.path.join(__file__, "..", "..", "ES.svg")
193
        ),
194
        img_format="svg",
195
    )
196
    esgr.render()
197
198
    # if tee_switch is true solver messages will be displayed
199
    logging.info("Solve the optimization problem")
200
    energysystem_model.solve(
201
        solver=solver, solve_kwargs={"tee": solver_verbose}
202
    )
203
204
    # after the solve method of the model has been called
205
    results = processing.results(energysystem_model)
206
    fig_dict = esgr.sankey(results)
207
    pio.show(fig_dict)
208
209
210
main()
211