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