Conditions | 2 |
Total Lines | 95 |
Code Lines | 52 |
Lines | 95 |
Ratio | 100 % |
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 -*- |
||
27 | View Code Duplication | def test_dispatch_one_time_step(solver="cbc"): |
|
|
|||
28 | """Create an energy system and optimize the dispatch at least costs.""" |
||
29 | |||
30 | # ######################### create energysystem components ################ |
||
31 | # resource buses |
||
32 | bgas = Bus(label="gas", balanced=False) |
||
33 | |||
34 | # electricity and heat |
||
35 | bel = Bus(label="b_el") |
||
36 | bth = Bus(label="b_th") |
||
37 | |||
38 | # an excess and a shortage variable can help to avoid infeasible problems |
||
39 | excess_el = Sink(label="excess_el", inputs={bel: Flow()}) |
||
40 | |||
41 | # sources |
||
42 | wind = Source( |
||
43 | label="wind", outputs={bel: Flow(fix=0.5, nominal_capacity=66.3)} |
||
44 | ) |
||
45 | |||
46 | # demands (electricity/heat) |
||
47 | demand_el = Sink( |
||
48 | label="demand_elec", inputs={bel: Flow(nominal_capacity=85, fix=0.3)} |
||
49 | ) |
||
50 | |||
51 | demand_th = Sink( |
||
52 | label="demand_therm", inputs={bth: Flow(nominal_capacity=40, fix=0.2)} |
||
53 | ) |
||
54 | |||
55 | # combined heat and power plant (chp) |
||
56 | pp_chp = Converter( |
||
57 | label="pp_chp", |
||
58 | inputs={bgas: Flow()}, |
||
59 | outputs={ |
||
60 | bel: Flow(nominal_capacity=30, variable_costs=42), |
||
61 | bth: Flow(nominal_capacity=40), |
||
62 | }, |
||
63 | conversion_factors={bel: 0.3, bth: 0.4}, |
||
64 | ) |
||
65 | |||
66 | # heatpump with a coefficient of performance (COP) of 3 |
||
67 | b_heat_source = Bus(label="b_heat_source") |
||
68 | |||
69 | heat_source = Source(label="heat_source", outputs={b_heat_source: Flow()}) |
||
70 | |||
71 | cop = 3 |
||
72 | heat_pump = Converter( |
||
73 | label="heat_pump", |
||
74 | inputs={bel: Flow(), b_heat_source: Flow()}, |
||
75 | outputs={bth: Flow(nominal_capacity=10)}, |
||
76 | conversion_factors={bel: 1 / 3, b_heat_source: (cop - 1) / cop}, |
||
77 | ) |
||
78 | |||
79 | energysystem = EnergySystem(timeincrement=[1]) |
||
80 | energysystem.add( |
||
81 | bgas, |
||
82 | bel, |
||
83 | bth, |
||
84 | excess_el, |
||
85 | wind, |
||
86 | demand_el, |
||
87 | demand_th, |
||
88 | pp_chp, |
||
89 | b_heat_source, |
||
90 | heat_source, |
||
91 | heat_pump, |
||
92 | ) |
||
93 | |||
94 | # ################################ optimization ########################### |
||
95 | |||
96 | # create optimization model based on energy_system |
||
97 | optimization_model = Model(energysystem=energysystem) |
||
98 | |||
99 | # solve problem |
||
100 | optimization_model.solve(solver=solver) |
||
101 | |||
102 | # write back results from optimization object to energysystem |
||
103 | optimization_model.results() |
||
104 | |||
105 | # ################################ results ################################ |
||
106 | data = views.node(processing.results(model=optimization_model), "b_el") |
||
107 | |||
108 | # generate results to be evaluated in tests |
||
109 | results = data["sequences"].sum(axis=0).to_dict() |
||
110 | |||
111 | print("DateTimeIndex:", data["sequences"].index) |
||
112 | |||
113 | test_results = { |
||
114 | (("wind", "b_el"), "flow"): 33, |
||
115 | (("b_el", "demand_elec"), "flow"): 26, |
||
116 | (("b_el", "excess_el"), "flow"): 5, |
||
117 | (("b_el", "heat_pump"), "flow"): 3, |
||
118 | } |
||
119 | |||
120 | for key in test_results.keys(): |
||
121 | assert results[key] == pytest.approx(test_results[key], abs=0.5) |
||
122 |