| Conditions | 3 |
| Total Lines | 78 |
| Code Lines | 52 |
| 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 -*- |
||
| 26 | def test_pwltf(): |
||
| 27 | # Set timeindex and create data |
||
| 28 | periods = 20 |
||
| 29 | datetimeindex = pd.date_range("1/1/2019", periods=periods, freq="h") |
||
| 30 | step = 5 |
||
| 31 | demand = np.arange(0, step * periods, step) |
||
| 32 | |||
| 33 | # Set up EnergySystem, buses and sink |
||
| 34 | energysystem = EnergySystem( |
||
| 35 | timeindex=datetimeindex, infer_last_interval=True |
||
| 36 | ) |
||
| 37 | b_gas = Bus(label="biogas", balanced=False) |
||
| 38 | b_el = Bus(label="electricity") |
||
| 39 | demand_el = Sink( |
||
| 40 | label="demand", |
||
| 41 | inputs={b_el: Flow(nominal_capacity=1, fix=demand)}, |
||
| 42 | ) |
||
| 43 | |||
| 44 | energysystem.add(b_gas, b_el, demand_el) |
||
| 45 | |||
| 46 | # Define conversion function and breakpoints |
||
| 47 | def conv_func(x): |
||
| 48 | return 0.01 * x**2 |
||
| 49 | |||
| 50 | in_breakpoints = np.arange(0, 110, 25) |
||
| 51 | |||
| 52 | # Create and add PiecewiseLinearConverter |
||
| 53 | pwltf = solph.components.experimental.PiecewiseLinearConverter( |
||
| 54 | label="pwltf", |
||
| 55 | inputs={ |
||
| 56 | b_gas: solph.flows.Flow(nominal_capacity=100, variable_costs=1) |
||
| 57 | }, |
||
| 58 | outputs={b_el: solph.flows.Flow()}, |
||
| 59 | in_breakpoints=in_breakpoints, |
||
| 60 | conversion_function=conv_func, |
||
| 61 | pw_repn="CC", |
||
| 62 | ) |
||
| 63 | |||
| 64 | energysystem.add(pwltf) |
||
| 65 | |||
| 66 | # Create and solve the optimization model |
||
| 67 | optimization_model = Model(energysystem) |
||
| 68 | optimization_model.solve(solver="cbc") |
||
| 69 | |||
| 70 | # Get results |
||
| 71 | results = processing.results( |
||
| 72 | optimization_model, remove_last_time_point=True |
||
| 73 | ) |
||
| 74 | string_results = processing.convert_keys_to_strings(results) |
||
| 75 | sequences = {k: v["sequences"] for k, v in string_results.items()} |
||
| 76 | df = pd.concat(sequences, axis=1) |
||
| 77 | df[("efficiency", None, None)] = df[("pwltf", "electricity")].divide( |
||
| 78 | df[("biogas", "pwltf")] |
||
| 79 | ) |
||
| 80 | |||
| 81 | # Test: Compare results with piecewise linearized function |
||
| 82 | def linearized_func(func, x_break, x): |
||
| 83 | y_break = func(x_break) |
||
| 84 | condlist = [ |
||
| 85 | (x_l <= x) & (x < x_u) |
||
| 86 | for x_l, x_u in zip(x_break[:-1], x_break[1:]) |
||
| 87 | ] |
||
| 88 | funclist = [] |
||
| 89 | for i in range(len(x_break) - 1): |
||
| 90 | b = y_break[i] |
||
| 91 | a = ( |
||
| 92 | (y_break[i + 1] - y_break[i]) |
||
| 93 | * 1 |
||
| 94 | / (x_break[i + 1] - x_break[i]) |
||
| 95 | ) |
||
| 96 | funclist.append(lambda x, b=b, a=a, i=i: b + a * (x - x_break[i])) |
||
| 97 | return np.piecewise(x, condlist, funclist) |
||
| 98 | |||
| 99 | production_expected = linearized_func( |
||
| 100 | conv_func, in_breakpoints, df[("biogas", "pwltf")]["flow"].values |
||
| 101 | ) |
||
| 102 | production_modeled = df[("pwltf", "electricity")]["flow"].values |
||
| 103 | assert np.allclose(production_modeled, production_expected) |
||
| 104 |