Conditions | 3 |
Total Lines | 182 |
Code Lines | 105 |
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 -*- |
||
25 | def test_variable_chp(filename="variable_chp.csv", solver="cbc"): |
||
26 | logging.info("Initialize the energy system") |
||
27 | |||
28 | # create time index for 192 hours in May. |
||
29 | date_time_index = pd.date_range("5/5/2012", periods=5, freq="h") |
||
30 | energysystem = solph.EnergySystem( |
||
31 | timeindex=date_time_index, infer_last_interval=True |
||
32 | ) |
||
33 | |||
34 | # Read data file with heat and electrical demand (192 hours) |
||
35 | full_filename = os.path.join(os.path.dirname(__file__), filename) |
||
36 | data = pd.read_csv(full_filename, sep=",") |
||
37 | |||
38 | ########################################################################## |
||
39 | # Create oemof.solph objects |
||
40 | ########################################################################## |
||
41 | |||
42 | logging.info("Create oemof.solph objects") |
||
43 | |||
44 | # create natural gas bus |
||
45 | bgas = solph.buses.Bus(label=("natural", "gas")) |
||
46 | energysystem.add(bgas) |
||
47 | |||
48 | # create commodity object for gas resource |
||
49 | energysystem.add( |
||
50 | solph.components.Source( |
||
51 | label=("commodity", "gas"), |
||
52 | outputs={bgas: solph.flows.Flow(variable_costs=50)}, |
||
53 | ) |
||
54 | ) |
||
55 | |||
56 | # create two electricity buses and two heat buses |
||
57 | bel = solph.buses.Bus(label=("electricity", 1)) |
||
58 | bel2 = solph.buses.Bus(label=("electricity", 2)) |
||
59 | bth = solph.buses.Bus(label=("heat", 1)) |
||
60 | bth2 = solph.buses.Bus(label=("heat", 2)) |
||
61 | energysystem.add(bel, bel2, bth, bth2) |
||
62 | |||
63 | # create excess components for the elec/heat bus to allow overproduction |
||
64 | energysystem.add( |
||
65 | solph.components.Sink( |
||
66 | label=("excess", "bth_2"), inputs={bth2: solph.flows.Flow()} |
||
67 | ) |
||
68 | ) |
||
69 | energysystem.add( |
||
70 | solph.components.Sink( |
||
71 | label=("excess", "bth_1"), inputs={bth: solph.flows.Flow()} |
||
72 | ) |
||
73 | ) |
||
74 | energysystem.add( |
||
75 | solph.components.Sink( |
||
76 | label=("excess", "bel_2"), inputs={bel2: solph.flows.Flow()} |
||
77 | ) |
||
78 | ) |
||
79 | energysystem.add( |
||
80 | solph.components.Sink( |
||
81 | label=("excess", "bel_1"), inputs={bel: solph.flows.Flow()} |
||
82 | ) |
||
83 | ) |
||
84 | |||
85 | # create simple sink object for electrical demand for each electrical bus |
||
86 | energysystem.add( |
||
87 | solph.components.Sink( |
||
88 | label=("demand", "elec1"), |
||
89 | inputs={ |
||
90 | bel: solph.flows.Flow( |
||
91 | fix=data["demand_el"], nominal_capacity=1 |
||
92 | ) |
||
93 | }, |
||
94 | ) |
||
95 | ) |
||
96 | energysystem.add( |
||
97 | solph.components.Sink( |
||
98 | label=("demand", "elec2"), |
||
99 | inputs={ |
||
100 | bel2: solph.flows.Flow( |
||
101 | fix=data["demand_el"], nominal_capacity=1 |
||
102 | ) |
||
103 | }, |
||
104 | ) |
||
105 | ) |
||
106 | |||
107 | # create simple sink object for heat demand for each thermal bus |
||
108 | energysystem.add( |
||
109 | solph.components.Sink( |
||
110 | label=("demand", "therm1"), |
||
111 | inputs={ |
||
112 | bth: solph.flows.Flow( |
||
113 | fix=data["demand_th"], nominal_capacity=741000 |
||
114 | ) |
||
115 | }, |
||
116 | ) |
||
117 | ) |
||
118 | energysystem.add( |
||
119 | solph.components.Sink( |
||
120 | label=("demand", "therm2"), |
||
121 | inputs={ |
||
122 | bth2: solph.flows.Flow( |
||
123 | fix=data["demand_th"], nominal_capacity=741000 |
||
124 | ) |
||
125 | }, |
||
126 | ) |
||
127 | ) |
||
128 | |||
129 | # create a fixed converter to distribute to the heat_2 and elec_2 buses |
||
130 | energysystem.add( |
||
131 | solph.components.Converter( |
||
132 | label=("fixed_chp", "gas"), |
||
133 | inputs={bgas: solph.flows.Flow(nominal_capacity=1e11)}, |
||
134 | outputs={bel2: solph.flows.Flow(), bth2: solph.flows.Flow()}, |
||
135 | conversion_factors={bel2: 0.3, bth2: 0.5}, |
||
136 | ) |
||
137 | ) |
||
138 | |||
139 | # create a fixed converter to distribute to the heat and elec buses |
||
140 | energysystem.add( |
||
141 | solph.components.ExtractionTurbineCHP( |
||
142 | label=("variable_chp", "gas"), |
||
143 | inputs={bgas: solph.flows.Flow(nominal_capacity=1e11)}, |
||
144 | outputs={bel: solph.flows.Flow(), bth: solph.flows.Flow()}, |
||
145 | conversion_factors={bel: 0.3, bth: 0.5}, |
||
146 | conversion_factor_full_condensation={bel: 0.5}, |
||
147 | ) |
||
148 | ) |
||
149 | |||
150 | ########################################################################## |
||
151 | # Optimise the energy system and plot the results |
||
152 | ########################################################################## |
||
153 | |||
154 | logging.info("Optimise the energy system") |
||
155 | |||
156 | om = solph.Model(energysystem) |
||
157 | |||
158 | logging.info("Solve the optimization problem") |
||
159 | om.solve(solver=solver) |
||
160 | |||
161 | optimisation_results = solph.processing.results(om) |
||
162 | parameter = solph.processing.parameter_as_dict(energysystem) |
||
163 | |||
164 | myresults = views.node(optimisation_results, "('natural', 'gas')") |
||
165 | sumresults = myresults["sequences"].sum(axis=0) |
||
166 | maxresults = myresults["sequences"].max(axis=0) |
||
167 | |||
168 | variable_chp_dict_sum = { |
||
169 | (("('natural', 'gas')", "('variable_chp', 'gas')"), "flow"): 2823024, |
||
170 | (("('natural', 'gas')", "('fixed_chp', 'gas')"), "flow"): 3710208, |
||
171 | (("('commodity', 'gas')", "('natural', 'gas')"), "flow"): 6533232, |
||
172 | } |
||
173 | |||
174 | variable_chp_dict_max = { |
||
175 | (("('natural', 'gas')", "('variable_chp', 'gas')"), "flow"): 630332, |
||
176 | (("('natural', 'gas')", "('fixed_chp', 'gas')"), "flow"): 785934, |
||
177 | (("('commodity', 'gas')", "('natural', 'gas')"), "flow"): 1416266, |
||
178 | } |
||
179 | |||
180 | for key in variable_chp_dict_max.keys(): |
||
181 | logging.debug("Test the maximum value of {0}".format(key)) |
||
182 | assert maxresults[[key]].iloc[0] == pytest.approx( |
||
183 | variable_chp_dict_max[key] |
||
184 | ) |
||
185 | |||
186 | for key in variable_chp_dict_sum.keys(): |
||
187 | logging.debug("Test the summed up value of {0}".format(key)) |
||
188 | assert sumresults[[key]].iloc[0] == pytest.approx( |
||
189 | variable_chp_dict_sum[key] |
||
190 | ) |
||
191 | |||
192 | assert ( |
||
193 | parameter[(energysystem.groups["('fixed_chp', 'gas')"], None)][ |
||
194 | "scalars" |
||
195 | ]["label"] |
||
196 | == "('fixed_chp', 'gas')" |
||
197 | ) |
||
198 | assert ( |
||
199 | parameter[(energysystem.groups["('fixed_chp', 'gas')"], None)][ |
||
200 | "scalars" |
||
201 | ]["conversion_factors_('electricity', 2)"] |
||
202 | ) == pytest.approx(0.3) |
||
203 | |||
204 | # objective function |
||
205 | assert solph.processing.meta_results(om)["objective"] == pytest.approx( |
||
206 | 326661590, abs=0.5 |
||
207 | ) |
||
208 |