Conditions | 2 |
Total Lines | 113 |
Code Lines | 79 |
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 -*- |
||
31 | def test_gen_caes(): |
||
32 | # read sequence data |
||
33 | full_filename = os.path.join(os.path.dirname(__file__), "generic_caes.csv") |
||
34 | data = pd.read_csv(full_filename) |
||
35 | |||
36 | # select periods |
||
37 | periods = len(data) |
||
38 | |||
39 | # create an energy system |
||
40 | idx = pd.date_range("1/1/2017", periods=periods, freq="h") |
||
41 | es = EnergySystem(timeindex=idx, infer_last_interval=True) |
||
42 | |||
43 | # resources |
||
44 | bgas = Bus(label="bgas") |
||
45 | es.add(bgas) |
||
46 | |||
47 | es.add(Source(label="rgas", outputs={bgas: Flow(variable_costs=20)})) |
||
48 | |||
49 | # power |
||
50 | bel_source = Bus(label="bel_source") |
||
51 | es.add(bel_source) |
||
52 | es.add( |
||
53 | Source( |
||
54 | label="source_el", |
||
55 | outputs={bel_source: Flow(variable_costs=data["price_el_source"])}, |
||
56 | ) |
||
57 | ) |
||
58 | |||
59 | bel_sink = Bus(label="bel_sink") |
||
60 | es.add(bel_sink) |
||
61 | es.add( |
||
62 | Sink( |
||
63 | label="sink_el", |
||
64 | inputs={bel_sink: Flow(variable_costs=data["price_el_sink"])}, |
||
65 | ) |
||
66 | ) |
||
67 | |||
68 | # dictionary with parameters for a specific CAES plant |
||
69 | # based on thermal modelling and linearization techniques |
||
70 | concept = { |
||
71 | "cav_e_in_b": 0, |
||
72 | "cav_e_in_m": 0.6457267578, |
||
73 | "cav_e_out_b": 0, |
||
74 | "cav_e_out_m": 0.3739636077, |
||
75 | "cav_eta_temp": 1.0, |
||
76 | "cav_level_max": 211.11, |
||
77 | "cmp_p_max_b": 86.0918959849, |
||
78 | "cmp_p_max_m": 0.0679999932, |
||
79 | "cmp_p_min": 1, |
||
80 | "cmp_q_out_b": -19.3996965679, |
||
81 | "cmp_q_out_m": 1.1066036114, |
||
82 | "cmp_q_tes_share": 0, |
||
83 | "exp_p_max_b": 46.1294016678, |
||
84 | "exp_p_max_m": 0.2528340303, |
||
85 | "exp_p_min": 1, |
||
86 | "exp_q_in_b": -2.2073411014, |
||
87 | "exp_q_in_m": 1.129249765, |
||
88 | "exp_q_tes_share": 0, |
||
89 | "tes_eta_temp": 1.0, |
||
90 | "tes_level_max": 0.0, |
||
91 | } |
||
92 | |||
93 | # generic compressed air energy storage (caes) plant |
||
94 | es.add( |
||
95 | GenericCAES( |
||
96 | label="caes", |
||
97 | electrical_input={bel_source: Flow()}, |
||
98 | fuel_input={bgas: Flow()}, |
||
99 | electrical_output={bel_sink: Flow()}, |
||
100 | params=concept, |
||
101 | ) |
||
102 | ) |
||
103 | |||
104 | # create an optimization problem and solve it |
||
105 | om = Model(es) |
||
106 | |||
107 | # solve model |
||
108 | om.solve(solver="cbc") |
||
109 | |||
110 | # create result object |
||
111 | results = processing.results(om) |
||
112 | |||
113 | data = ( |
||
114 | views.node(results, "caes", keep_none_type=True)["sequences"] |
||
115 | .sum(axis=0) |
||
116 | .to_dict() |
||
117 | ) |
||
118 | |||
119 | test_dict = { |
||
120 | (("caes", None), "cav_level"): 25658.82964382, |
||
121 | (("caes", None), "exp_p"): 5020.801997000007, |
||
122 | (("caes", None), "exp_q_fuel_in"): 5170.880360999999, |
||
123 | (("caes", None), "tes_e_out"): 0.0, |
||
124 | (("caes", None), "exp_st"): 226.0, |
||
125 | (("bgas", "caes"), "flow"): 5170.880360999999, |
||
126 | (("caes", None), "cav_e_out"): 1877.5972265299995, |
||
127 | (("caes", None), "exp_p_max"): 17512.352336, |
||
128 | (("caes", None), "cmp_q_waste"): 2499.9125993000007, |
||
129 | (("caes", None), "cmp_p"): 2907.7271520000004, |
||
130 | (("caes", None), "exp_q_add_in"): 0.0, |
||
131 | (("caes", None), "cmp_st"): 37.0, |
||
132 | (("caes", None), "cmp_q_out_sum"): 2499.9125993000007, |
||
133 | (("caes", None), "tes_level"): 0.0, |
||
134 | (("caes", None), "tes_e_in"): 0.0, |
||
135 | (("caes", None), "exp_q_in_sum"): 5170.880360999999, |
||
136 | (("caes", None), "cmp_p_max"): 22320.76334300001, |
||
137 | (("caes", "bel_sink"), "flow"): 5020.801997000007, |
||
138 | (("bel_source", "caes"), "flow"): 2907.7271520000004, |
||
139 | (("caes", None), "cav_e_in"): 1877.597226, |
||
140 | } |
||
141 | |||
142 | for key in test_dict.keys(): |
||
143 | assert data[key] == pytest.approx(test_dict[key]) |
||
144 |