| Conditions | 2 |
| Total Lines | 142 |
| Code Lines | 85 |
| 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 -*- |
||
| 58 | def main(): |
||
| 59 | data = [0, 15, 30, 35, 20, 25, 27, 10, 5, 2, 15, 40, 20, 0, 0] |
||
| 60 | |||
| 61 | # create an energy system |
||
| 62 | idx = solph.create_time_index(2020, number=len(data)) |
||
| 63 | es = solph.EnergySystem(timeindex=idx, infer_last_interval=False) |
||
| 64 | |||
| 65 | # Parameter: costs for the sources |
||
| 66 | c_0 = 10 |
||
| 67 | c_1 = 100 |
||
| 68 | |||
| 69 | epc_invest = 500 |
||
| 70 | |||
| 71 | # commodity a |
||
| 72 | bus_a_0 = solph.Bus(label="bus_a_0") |
||
| 73 | bus_a_1 = solph.Bus(label="bus_a_1") |
||
| 74 | es.add(bus_a_0, bus_a_1) |
||
| 75 | |||
| 76 | es.add( |
||
| 77 | solph.components.Source( |
||
| 78 | label="source_a_0", |
||
| 79 | outputs={bus_a_0: solph.Flow(variable_costs=c_0)}, |
||
| 80 | ) |
||
| 81 | ) |
||
| 82 | |||
| 83 | es.add( |
||
| 84 | solph.components.Source( |
||
| 85 | label="source_a_1", |
||
| 86 | outputs={bus_a_1: solph.Flow(variable_costs=c_1)}, |
||
| 87 | ) |
||
| 88 | ) |
||
| 89 | |||
| 90 | es.add( |
||
| 91 | solph.components.Sink( |
||
| 92 | label="demand_a", |
||
| 93 | inputs={bus_a_1: solph.Flow(fix=data, nominal_value=1)}, |
||
| 94 | ) |
||
| 95 | ) |
||
| 96 | |||
| 97 | # commodity b |
||
| 98 | bus_b_0 = solph.Bus(label="bus_b_0") |
||
| 99 | bus_b_1 = solph.Bus(label="bus_b_1") |
||
| 100 | es.add(bus_b_0, bus_b_1) |
||
| 101 | es.add( |
||
| 102 | solph.components.Source( |
||
| 103 | label="source_b_0", |
||
| 104 | outputs={bus_b_0: solph.Flow(variable_costs=c_0)}, |
||
| 105 | ) |
||
| 106 | ) |
||
| 107 | |||
| 108 | es.add( |
||
| 109 | solph.components.Source( |
||
| 110 | label="source_b_1", |
||
| 111 | outputs={bus_b_1: solph.Flow(variable_costs=c_1)}, |
||
| 112 | ) |
||
| 113 | ) |
||
| 114 | |||
| 115 | es.add( |
||
| 116 | solph.components.Sink( |
||
| 117 | label="demand_b", |
||
| 118 | inputs={bus_b_1: solph.Flow(fix=data, nominal_value=1)}, |
||
| 119 | ) |
||
| 120 | ) |
||
| 121 | |||
| 122 | # transformer a |
||
| 123 | es.add( |
||
| 124 | solph.components.Transformer( |
||
| 125 | label="trafo_a", |
||
| 126 | inputs={bus_a_0: solph.Flow()}, |
||
| 127 | outputs={ |
||
| 128 | bus_a_1: solph.Flow( |
||
| 129 | nominal_value=None, |
||
| 130 | investment=solph.Investment( |
||
| 131 | ep_costs=epc_invest, |
||
| 132 | space=2, |
||
| 133 | ), |
||
| 134 | ) |
||
| 135 | }, |
||
| 136 | conversion_factors={bus_a_1: 0.8}, |
||
| 137 | ) |
||
| 138 | ) |
||
| 139 | |||
| 140 | # transformer b |
||
| 141 | es.add( |
||
| 142 | solph.components.Transformer( |
||
| 143 | label="trafo_b", |
||
| 144 | inputs={bus_b_0: solph.Flow()}, |
||
| 145 | outputs={ |
||
| 146 | bus_b_1: solph.Flow( |
||
| 147 | nominal_value=None, |
||
| 148 | investment=solph.Investment( |
||
| 149 | ep_costs=epc_invest, |
||
| 150 | space=1, |
||
| 151 | ), |
||
| 152 | ) |
||
| 153 | }, |
||
| 154 | conversion_factors={bus_a_1: 0.8}, |
||
| 155 | ) |
||
| 156 | ) |
||
| 157 | |||
| 158 | # create an optimization problem and solve it |
||
| 159 | om = solph.Model(es) |
||
| 160 | |||
| 161 | # add constraint for generic investment limit |
||
| 162 | om = solph.constraints.additional_investment_flow_limit( |
||
| 163 | om, "space", limit=24 |
||
| 164 | ) |
||
| 165 | |||
| 166 | # export lp file |
||
| 167 | filename = os.path.join( |
||
| 168 | solph.helpers.extend_basic_path("lp_files"), "GenericInvest.lp" |
||
| 169 | ) |
||
| 170 | logging.info("Store lp-file in {0}.".format(filename)) |
||
| 171 | om.write(filename, io_options={"symbolic_solver_labels": True}) |
||
| 172 | |||
| 173 | # solve model |
||
| 174 | om.solve(solver="cbc", solve_kwargs={"tee": True}) |
||
| 175 | |||
| 176 | # create result object |
||
| 177 | results = solph.processing.results(om) |
||
| 178 | |||
| 179 | bus1 = solph.views.node(results, "bus_a_1")["sequences"] |
||
| 180 | bus2 = solph.views.node(results, "bus_b_1")["sequences"] |
||
| 181 | |||
| 182 | # plot the time series (sequences) of a specific component/bus |
||
| 183 | if plt is not None: |
||
| 184 | bus1.plot(kind="line", drawstyle="steps-mid") |
||
| 185 | plt.legend() |
||
| 186 | plt.show() |
||
| 187 | bus2.plot(kind="line", drawstyle="steps-mid") |
||
| 188 | plt.legend() |
||
| 189 | plt.show() |
||
| 190 | |||
| 191 | space_used = om.invest_limit_space() |
||
| 192 | print("Space value: ", space_used) |
||
| 193 | print( |
||
| 194 | "Investment trafo_a: ", |
||
| 195 | solph.views.node(results, "trafo_a")["scalars"][0], |
||
| 196 | ) |
||
| 197 | print( |
||
| 198 | "Investment trafo_b: ", |
||
| 199 | solph.views.node(results, "trafo_b")["scalars"][0], |
||
| 200 | ) |
||
| 205 |