ev_charging_1   A
last analyzed

Complexity

Total Complexity 0

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 0
eloc 57
dl 0
loc 93
rs 10
c 0
b 0
f 0
1
# %%[imports_start]
2
3
import matplotlib.pyplot as plt
4
import networkx as nx
5
import numpy as np
6
from oemof.network.graph import create_nx_graph
7
import pandas as pd
8
from helpers import plot_results
9
10
# %%[imports_end]
11
# %%[create_time_index_set_up_energysystem_start]
12
import oemof.solph as solph
13
14
time_index = pd.date_range(
15
    start="2025-01-01",
16
    end="2025-01-02",
17
    freq="5min",
18
    inclusive="both",
19
)
20
21
ev_energy_system = solph.EnergySystem(
22
    timeindex=time_index,
23
    infer_last_interval=False,
24
)
25
# %%[create_time_index_set_up_energysystem_end]
26
# %%[trip_data_start]
27
ev_demand = pd.Series(0, index=time_index[:-1])
28
29
driving_start_morning = pd.Timestamp("2025-01-01 07:10")
30
driving_end_morning = pd.Timestamp("2025-01-01 08:10")
31
ev_demand.loc[driving_start_morning:driving_end_morning] = 10  # kW
32
33
34
driving_start_evening = pd.Timestamp("2025-01-01 16:13:37")
35
driving_end_evening = pd.Timestamp("2025-01-01 17:45:11")
36
ev_demand.loc[driving_start_evening:driving_end_evening] = 9  # kW
37
# %%[trip_data_end]
38
## %%[plot_trip_data_start]
39
plt.figure()
40
# plt.style.use("dark_background")
41
plt.title("Driving pattern")
42
plt.plot(ev_demand)
43
plt.ylabel("Power (kW)")
44
plt.gcf().autofmt_xdate()
45
## %%[plot_trip_data_end]
46
# %%[energysystem_and_bus_start]
47
bus_car = solph.Bus(label="Car Electricity")
48
49
ev_energy_system.add(bus_car)
50
# %%[energysystem_and_bus_end]
51
# %%[car_start]
52
demand_driving = solph.components.Sink(
53
    label="Driving Demand",
54
    inputs={bus_car: solph.Flow(nominal_capacity=1, fix=ev_demand)},
55
)
56
57
ev_energy_system.add(demand_driving)
58
59
storage_revenue = np.zeros(len(time_index) - 1)
60
storage_revenue[-1] = -0.6  # 60 ct/kWh in the last time step
61
62
car_battery = solph.components.GenericStorage(
63
    label="Car Battery",
64
    nominal_capacity=50,  # kWh
65
    inputs={bus_car: solph.Flow()},
66
    outputs={bus_car: solph.Flow()},
67
    initial_storage_level=1,  # full in the beginning
68
    loss_rate=0.001,  # 0.1 % / hr
69
    inflow_conversion_factor=0.9,  # 90 % charging efficiency
70
    balanced=False,  # True: content at beginning and end need to be equal
71
    storage_costs=storage_revenue,  # Only has an effect on charging.
72
)
73
74
ev_energy_system.add(car_battery)
75
# %%[car_end]
76
# %%[graph_start]
77
plt.figure()
78
graph = create_nx_graph(ev_energy_system)
79
nx.drawing.nx_pydot.write_dot(graph, "ev_carging_graph_1.dot")
80
nx.draw(graph, with_labels=True, font_size=8)
81
82
# %%[graph_end]
83
# %%[solve_start]
84
model = solph.Model(ev_energy_system)
85
model.solve(solver="cbc", solve_kwargs={"tee": True})
86
results = solph.processing.results(model)
87
# %%[solve_end]
88
# %%[plot_results_start]
89
plot_results(
90
    results=results, plot_title="Driving demand only", dark_mode=False
91
)
92
plt.show()
93
# %%[plot_results_end]
94