| Conditions | 2 |
| Total Lines | 95 |
| Code Lines | 56 |
| 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 -*- |
||
| 34 | def main(): |
||
| 35 | energy_system = solph.EnergySystem( |
||
| 36 | timeindex=pd.date_range("1/1/2012", periods=4, freq="H") |
||
| 37 | ) |
||
| 38 | Node.registry = energy_system |
||
| 39 | |||
| 40 | bel = solph.Bus(label="bel") |
||
| 41 | |||
| 42 | # There are a sink and a source, both creating a revenue (negative cost), |
||
| 43 | # so it would be optimal to use both at the same time. To suppress this, |
||
| 44 | # the constraint "limit_active_flow_count" is used. |
||
| 45 | # You might define any keyword (here "my_keyword") like: |
||
| 46 | # > Flow(nonconvex=solph.NonConvex(), |
||
| 47 | # > my_keyword=True, |
||
| 48 | # ...) |
||
| 49 | # But also any existing one (e.g. "emission_factor") can be used. |
||
| 50 | |||
| 51 | solph.components.Source( |
||
| 52 | label="source1", |
||
| 53 | outputs={ |
||
| 54 | bel: solph.Flow( |
||
| 55 | nonconvex=solph.NonConvex(), |
||
| 56 | nominal_value=210, |
||
| 57 | variable_costs=[-1, -5, -1, -1], |
||
| 58 | max=[1, 1, 1, 0], |
||
| 59 | my_keyword=True, |
||
| 60 | ) |
||
| 61 | }, |
||
| 62 | ) |
||
| 63 | |||
| 64 | # Note: The keyword is also defined when set to False. |
||
| 65 | solph.components.Sink( |
||
| 66 | label="sink1", |
||
| 67 | inputs={ |
||
| 68 | bel: solph.Flow( |
||
| 69 | nonconvex=solph.NonConvex(), |
||
| 70 | variable_costs=[-2, -1, -2, -2], |
||
| 71 | nominal_value=250, |
||
| 72 | max=[1, 1, 1, 0], |
||
| 73 | my_keyword=False, |
||
| 74 | ) |
||
| 75 | }, |
||
| 76 | ) |
||
| 77 | |||
| 78 | # Should be ignored because my_keyword is not defined. |
||
| 79 | solph.components.Source( |
||
| 80 | label="source2", |
||
| 81 | outputs={ |
||
| 82 | bel: solph.Flow( |
||
| 83 | variable_costs=1, |
||
| 84 | nonconvex=solph.NonConvex(), |
||
| 85 | max=[1, 1, 1, 0], |
||
| 86 | nominal_value=145, |
||
| 87 | ) |
||
| 88 | }, |
||
| 89 | ) |
||
| 90 | |||
| 91 | # Should be ignored because it is not NonConvex. |
||
| 92 | solph.components.Sink( |
||
| 93 | label="sink2", |
||
| 94 | inputs={ |
||
| 95 | bel: solph.Flow( |
||
| 96 | my_keyword=True, fix=[0, 1, 1, 0], nominal_value=130 |
||
| 97 | ) |
||
| 98 | }, |
||
| 99 | ) |
||
| 100 | |||
| 101 | model = solph.Model(energy_system) |
||
| 102 | |||
| 103 | # only one of the two flows may be active at a time |
||
| 104 | solph.constraints.limit_active_flow_count_by_keyword( |
||
| 105 | model, "my_keyword", lower_limit=0, upper_limit=1 |
||
| 106 | ) |
||
| 107 | |||
| 108 | model.solve() |
||
| 109 | |||
| 110 | results = processing.results(model) |
||
| 111 | |||
| 112 | if plt is not None: |
||
| 113 | data = views.node(results, "bel")["sequences"] |
||
| 114 | ax = data.plot(kind="line", grid=True) |
||
| 115 | ax.set_xlabel("Time (h)") |
||
| 116 | ax.set_ylabel("P (MW)") |
||
| 117 | |||
| 118 | plt.figure() |
||
| 119 | ax = plt.gca() |
||
| 120 | plt.plot( |
||
| 121 | results[("my_keyword", "my_keyword")]["sequences"], |
||
| 122 | label="my_keyword_count", |
||
| 123 | ) |
||
| 124 | ax.set_xlabel("Time (h)") |
||
| 125 | ax.set_ylabel("Count (1)") |
||
| 126 | plt.grid() |
||
| 127 | plt.legend() |
||
| 128 | plt.show() |
||
| 129 | |||
| 133 |