Conditions | 1 |
Total Lines | 108 |
Code Lines | 69 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | # -*- coding: utf-8 -*- |
||
43 | def main(optimize=True): |
||
44 | # create an energy system |
||
45 | idx = pd.date_range("1/1/2023", periods=13, freq="h") |
||
46 | es = solph.EnergySystem(timeindex=idx, infer_last_interval=False) |
||
47 | |||
48 | # power bus |
||
49 | bel = solph.Bus(label="bel") |
||
50 | es.add(bel) |
||
51 | |||
52 | es.add( |
||
53 | solph.components.Source( |
||
54 | label="source_el", |
||
55 | outputs={bel: solph.Flow()}, |
||
56 | ) |
||
57 | ) |
||
58 | |||
59 | es.add( |
||
60 | solph.components.Sink( |
||
61 | label="sink_el", |
||
62 | inputs={bel: solph.Flow()}, |
||
63 | ) |
||
64 | ) |
||
65 | |||
66 | electricity_price = np.array( |
||
67 | [ |
||
68 | 0.38, |
||
69 | 0.31, |
||
70 | 0.32, |
||
71 | 0.33, |
||
72 | 0.37, |
||
73 | 0.32, |
||
74 | 0.33, |
||
75 | 0.34, |
||
76 | 0.39, |
||
77 | 0.38, |
||
78 | 0.37, |
||
79 | 0.35, |
||
80 | 0.35, |
||
81 | ] |
||
82 | ) |
||
83 | |||
84 | # Electric Storage 1 |
||
85 | # Costs are designed in a way that storing energy is benificial until the |
||
86 | # last four time steps but emptying it is not a good option. |
||
87 | battery1 = solph.components.GenericStorage( |
||
88 | label="battery 1", |
||
89 | nominal_capacity=10, |
||
90 | inputs={ |
||
91 | bel: solph.Flow( |
||
92 | nominal_capacity=1, |
||
93 | variable_costs=electricity_price, |
||
94 | ) |
||
95 | }, |
||
96 | outputs={ |
||
97 | bel: solph.Flow( |
||
98 | nominal_capacity=1, |
||
99 | variable_costs=-electricity_price, |
||
100 | ) |
||
101 | }, |
||
102 | initial_storage_level=0.5, |
||
103 | balanced=False, |
||
104 | ) |
||
105 | es.add(battery1) |
||
106 | |||
107 | # storages that balance our fluctuating costs |
||
108 | # Electric Storage 2 |
||
109 | battery2 = solph.components.GenericStorage( |
||
110 | label="battery 2", |
||
111 | nominal_capacity=10, |
||
112 | inputs={ |
||
113 | bel: solph.Flow( |
||
114 | nominal_capacity=1, |
||
115 | variable_costs=electricity_price, |
||
116 | ) |
||
117 | }, |
||
118 | outputs={ |
||
119 | bel: solph.Flow( |
||
120 | nominal_capacity=1, |
||
121 | variable_costs=-electricity_price, |
||
122 | ) |
||
123 | }, |
||
124 | storage_costs=12 * [0] + [-np.mean(electricity_price)], |
||
125 | initial_storage_level=0.5, |
||
126 | balanced=False, |
||
127 | ) |
||
128 | es.add(battery2) |
||
129 | |||
130 | if optimize is False: |
||
131 | return es |
||
132 | |||
133 | # create an optimization problem and solve it |
||
134 | model = solph.Model(es) |
||
135 | |||
136 | # solve model |
||
137 | model.solve(solver="cbc") |
||
138 | |||
139 | # create result object |
||
140 | results = solph.processing.results(model) |
||
141 | |||
142 | plt.plot( |
||
143 | results[(battery1, None)]["sequences"], |
||
144 | label="content w/o storage costs", |
||
145 | ) |
||
146 | plt.plot( |
||
147 | results[(battery2, None)]["sequences"], |
||
148 | label="content w/ storage revenue", |
||
149 | ) |
||
150 | plt.legend() |
||
151 | plt.grid() |
||
158 |