| Conditions | 2 |
| Total Lines | 92 |
| Code Lines | 50 |
| 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 -*- |
||
| 39 | def main(optimize=True): |
||
| 40 | # create energy system |
||
| 41 | energysystem = solph.EnergySystem( |
||
| 42 | timeindex=pd.date_range("1/1/2012", periods=3, freq="h") |
||
| 43 | ) |
||
| 44 | |||
| 45 | # create gas bus |
||
| 46 | bgas = solph.Bus(label="gas") |
||
| 47 | |||
| 48 | # create electricity bus |
||
| 49 | bel = solph.Bus(label="electricity") |
||
| 50 | |||
| 51 | # adding the buses to the energy system |
||
| 52 | energysystem.add(bel, bgas) |
||
| 53 | |||
| 54 | # create fixed source object representing biomass plants |
||
| 55 | energysystem.add( |
||
| 56 | solph.components.Source( |
||
| 57 | label="biomass", |
||
| 58 | outputs={ |
||
| 59 | bel: solph.Flow( |
||
| 60 | nominal_capacity=100, |
||
| 61 | variable_costs=10, |
||
| 62 | fix=[0.1, 0.2, 0.3], |
||
| 63 | custom_attributes={"emission_factor": 0.01}, |
||
| 64 | ) |
||
| 65 | }, |
||
| 66 | ) |
||
| 67 | ) |
||
| 68 | |||
| 69 | # create source object representing the gas commodity |
||
| 70 | energysystem.add( |
||
| 71 | solph.components.Source( |
||
| 72 | label="gas-source", |
||
| 73 | outputs={ |
||
| 74 | bgas: solph.Flow( |
||
| 75 | variable_costs=10, |
||
| 76 | custom_attributes={"emission_factor": 0.2}, |
||
| 77 | ) |
||
| 78 | }, |
||
| 79 | ) |
||
| 80 | ) |
||
| 81 | |||
| 82 | energysystem.add( |
||
| 83 | solph.components.Sink( |
||
| 84 | label="demand", |
||
| 85 | inputs={ |
||
| 86 | bel: solph.Flow( |
||
| 87 | nominal_capacity=200, |
||
| 88 | variable_costs=10, |
||
| 89 | fix=[0.1, 0.2, 0.3], |
||
| 90 | ) |
||
| 91 | }, |
||
| 92 | ) |
||
| 93 | ) |
||
| 94 | |||
| 95 | # create simple converter object representing a gas power plant |
||
| 96 | energysystem.add( |
||
| 97 | solph.components.Converter( |
||
| 98 | label="pp_gas", |
||
| 99 | inputs={bgas: solph.Flow()}, |
||
| 100 | outputs={bel: solph.Flow(nominal_capacity=200)}, |
||
| 101 | conversion_factors={bel: 0.58}, |
||
| 102 | ) |
||
| 103 | ) |
||
| 104 | |||
| 105 | if optimize is False: |
||
| 106 | return energysystem |
||
| 107 | |||
| 108 | # initialise the operational model |
||
| 109 | model = solph.Model(energysystem) |
||
| 110 | |||
| 111 | # add the emission constraint |
||
| 112 | constraints.emission_limit(model, limit=100) |
||
| 113 | |||
| 114 | # print out the emission constraint |
||
| 115 | model.integral_limit_emission_factor_upper_limit.pprint() |
||
| 116 | model.integral_limit_emission_factor.pprint() |
||
| 117 | |||
| 118 | # solve the model |
||
| 119 | model.solve() |
||
| 120 | |||
| 121 | # print out the amount of emissions from the emission constraint |
||
| 122 | print(model.integral_limit_emission_factor()) |
||
| 123 | |||
| 124 | results = solph.processing.results(model) |
||
| 125 | |||
| 126 | data = solph.views.node(results, "electricity")["sequences"] |
||
| 127 | ax = data.plot(kind="line", grid=True) |
||
| 128 | ax.set_xlabel("Time (h)") |
||
| 129 | ax.set_ylabel("P (MW)") |
||
| 130 | plt.show() |
||
| 131 | |||
| 135 |