home_pv_3   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 101
dl 0
loc 182
rs 10
c 0
b 0
f 0
1
# -*- coding: utf-8 -*-
2
3
"""
4
SPDX-FileCopyrightText: Patrik Schönfeldt
5
SPDX-FileCopyrightText: Daniel Niederhöfer
6
SPDX-FileCopyrightText: DLR e.V.
7
8
SPDX-License-Identifier: MIT
9
"""
10
# %%[imports]
11
import os
12
import matplotlib.pyplot as plt
13
import networkx as nx
14
import numpy as np
15
from oemof.network.graph import create_nx_graph
16
import pandas as pd
17
18
from oemof import solph
19
20
# %%[input_data]
21
22
file_path = os.path.dirname(__file__)
23
filename = os.path.join(file_path, "pv_example_data.csv")
24
input_data = pd.read_csv(
25
    filename, index_col="timestep", parse_dates=["timestep"]
26
)
27
28
# %%[energy_system]
29
30
# parse_dates does not set the freq attribute.
31
# However, we want to use it for the EnergySystem.
32
input_data.index.freq = pd.infer_freq(input_data.index)
33
34
energy_system = solph.EnergySystem(
35
    timeindex=input_data.index,
36
    infer_last_interval=True,
37
)
38
39
# %%[dispatch_model]
40
41
el_bus = solph.Bus(label="electricity")
42
43
demand = solph.components.Sink(
44
    label="demand",
45
    inputs={
46
        el_bus: solph.Flow(
47
            nominal_capacity=1,
48
            fix=input_data["electricity demand (kW)"],
49
        )
50
    },
51
)
52
53
energy_system.add(el_bus, demand)
54
55
56
# %%[grid_conection]
57
58
grid = solph.Bus(
59
    label="grid",
60
    inputs={el_bus: solph.Flow(variable_costs=-0.06)},
61
    outputs={el_bus: solph.Flow(variable_costs=0.3)},
62
    balanced=False,
63
)
64
65
# %%[add_grid]
66
67
energy_system.add(grid)
68
69
# %%[pv_system]
70
71
pv_specific_costs = 1500  # €/kW
72
pv_lifetime = 20  # years
73
pv_epc = pv_specific_costs / pv_lifetime
74
75
pv_system = solph.components.Source(
76
    label="PV",
77
    outputs={
78
        el_bus: solph.Flow(
79
            nominal_capacity=solph.Investment(ep_costs=pv_epc, maximum=10),
80
            max=input_data["pv yield (kW/kW)"],
81
        )
82
    },
83
)
84
85
energy_system.add(pv_system)
86
87
# %%[graph_plotting]
88
plt.figure()
89
graph = create_nx_graph(energy_system)
90
nx.drawing.nx_pydot.write_dot(graph, "home_pv_graph_3.dot")
91
nx.draw(graph, with_labels=True, font_size=8)
92
# %%[model_optimisation]
93
model = solph.Model(energy_system)
94
95
model.solve(solver="cbc", solve_kwargs={"tee": True})
96
results = solph.processing.results(model)
97
meta_results = solph.processing.meta_results(model)
98
99
# %%[result_pv]
100
101
pv_size = results[(pv_system, el_bus)]["scalars"]["invest"]
102
103
# %%[results]
104
105
pv_annuity = pv_epc * pv_size
106
annual_grid_supply = results[(grid, el_bus)]["sequences"]["flow"].sum()
107
el_costs = 0.3 * annual_grid_supply
108
el_revenue = 0.1 * results[(el_bus, grid)]["sequences"]["flow"].sum()
109
110
tce = meta_results["objective"]
111
112
# %%[result_plotting]
113
114
print(f"The optimal PV size is {pv_size:.2f} kW.")
115
116
print(f"The annual costs for grid electricity are {el_costs:.2f} €.")
117
print(f"The annual revenue from feed-in is {el_revenue:.2f} €.")
118
print(f"The annuity for the PV system is {pv_annuity:.2f} €.")
119
print(f"The total annual costs are {tce:.2f} €.")
120
121
annual_demand = input_data["electricity demand (kW)"].sum()
122
123
print(
124
    f"Autarky is 1 - {annual_grid_supply:.2f} kWh / {annual_demand:.2f} kWh"
125
    + f" = {100 - 100 * annual_grid_supply / annual_demand:.2f} %."
126
)
127
128
electricity_fows = solph.views.node(results, "electricity")["sequences"]
129
130
baseline = np.zeros(len(electricity_fows))
131
132
plt.figure()
133
134
mode = "light"
135
# mode = "dark"
136
if mode == "dark":
137
    plt.style.use("dark_background")
138
139
plt.fill_between(
140
    electricity_fows.index,
141
    baseline,
142
    baseline + electricity_fows[(("grid", "electricity"), "flow")],
143
    step="pre",
144
    label="Grid supply",
145
)
146
147
baseline += electricity_fows[(("grid", "electricity"), "flow")]
148
149
plt.fill_between(
150
    electricity_fows.index,
151
    baseline,
152
    baseline + electricity_fows[(("PV", "electricity"), "flow")],
153
    step="pre",
154
    label="PV supply",
155
)
156
157
plt.step(
158
    electricity_fows.index,
159
    electricity_fows[(("electricity", "demand"), "flow")],
160
    "-",
161
    color="darkgrey",
162
    label="Electricity demand",
163
)
164
165
plt.step(
166
    electricity_fows.index,
167
    electricity_fows[(("electricity", "demand"), "flow")]
168
    + electricity_fows[(("electricity", "grid"), "flow")],
169
    ":",
170
    color="darkgrey",
171
    label="Feed-In",
172
)
173
174
plt.legend()
175
plt.ylabel("Power (kW)")
176
plt.xlim(pd.Timestamp("2020-02-21 00:00"), pd.Timestamp("2020-02-28 00:00"))
177
plt.gcf().autofmt_xdate()
178
179
plt.savefig(f"home_pv_result-3_{mode}.svg")
180
181
plt.show()
182