Conditions | 6 |
Total Lines | 125 |
Code Lines | 33 |
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 -*- |
||
53 | def generic_integral_limit( |
||
54 | om, keyword, flows=None, upper_limit=None, lower_limit=None, limit=None |
||
55 | ): |
||
56 | r"""Set a global limit for flows weighted by attribute named keyword. |
||
57 | The attribute named keyword has to be added |
||
58 | to every flow you want to take into account. |
||
59 | |||
60 | Total value of keyword attributes after optimization can be retrieved |
||
61 | calling the |
||
62 | `om.oemof.solph._models.Model.integral_limit_${keyword}()`. |
||
63 | |||
64 | Parameters |
||
65 | ---------- |
||
66 | om : oemof.solph.Model |
||
67 | Model to which constraints are added. |
||
68 | flows : dict |
||
69 | Dictionary holding the flows that should be considered in constraint. |
||
70 | Keys are (source, target) objects of the Flow. If no dictionary is |
||
71 | given all flows containing the keyword attribute will be |
||
72 | used. |
||
73 | keyword : string |
||
74 | attribute to consider |
||
75 | upper_limit : numeric |
||
76 | Absolute upper limit of keyword attribute for the energy system. |
||
77 | lower_limit : numeric |
||
78 | Absolute lower limit of keyword attribute for the energy system. |
||
79 | |||
80 | Note |
||
81 | ---- |
||
82 | Flow objects require an attribute named like keyword! |
||
83 | |||
84 | |||
85 | **Constraint:** |
||
86 | |||
87 | .. math:: \sum_{i \in F_E} \sum_{t \in T} P_i(p, t) \cdot w_i(t) |
||
88 | \cdot \tau(t) \leq UB |
||
89 | |||
90 | .. math:: \sum_{i \in F_E} \sum_{t \in T} P_i(p, t) \cdot w_i(t) |
||
91 | \cdot \tau(t) \geq LB |
||
92 | |||
93 | |||
94 | With `F_I` being the set of flows considered for the integral limit and |
||
95 | `T` being the set of time steps. |
||
96 | |||
97 | The symbols used are defined as follows |
||
98 | (with Variables (V) and Parameters (P)): |
||
99 | |||
100 | ================= ==== ==================================================== |
||
101 | math. symbol type explanation |
||
102 | ================= ==== ==================================================== |
||
103 | :math:`P_n(p, t)` V power flow :math:`n` at time index :math:`p, t` |
||
104 | :math:`w_N(t)` P weight given to Flow named according to `keyword` |
||
105 | :math:`\tau(t)` P width of time step :math:`t` |
||
106 | :math:`UB` P global limit given by keyword `upper_limit` |
||
107 | :math:`LB` P global limit given by keyword `lower_limit` |
||
108 | ================= ==== ==================================================== |
||
109 | |||
110 | Examples |
||
111 | -------- |
||
112 | >>> import pandas as pd |
||
113 | >>> from oemof import solph |
||
114 | >>> date_time_index = pd.date_range('1/1/2012', periods=6, freq='h') |
||
115 | >>> energysystem = solph.EnergySystem( |
||
116 | ... timeindex=date_time_index, |
||
117 | ... infer_last_interval=False, |
||
118 | ... ) |
||
119 | >>> bel = solph.buses.Bus(label='electricityBus') |
||
120 | >>> flow1 = solph.flows.Flow( |
||
121 | ... nominal_value=100, |
||
122 | ... custom_attributes={"my_factor": 0.8}, |
||
123 | ... ) |
||
124 | >>> flow2 = solph.flows.Flow(nominal_value=50) |
||
125 | >>> src1 = solph.components.Source(label='source1', outputs={bel: flow1}) |
||
126 | >>> src2 = solph.components.Source(label='source2', outputs={bel: flow2}) |
||
127 | >>> energysystem.add(bel, src1, src2) |
||
128 | >>> model = solph.Model(energysystem) |
||
129 | >>> flow_with_keyword = {(src1, bel): flow1, } |
||
130 | >>> model = solph.constraints.generic_integral_limit( |
||
131 | ... model, "my_factor", flow_with_keyword, limit=777) |
||
132 | """ |
||
133 | flows = _check_and_set_flows(om, flows, keyword) |
||
134 | limit_name = "integral_limit_" + keyword |
||
135 | |||
136 | if limit is not None: |
||
137 | msg = ( |
||
138 | "The keyword argument 'limit' to generic_integral_limit has been" |
||
139 | "renamed to 'upper_limit'. The transitional wrapper will be" |
||
140 | "deleted in a future version." |
||
141 | ) |
||
142 | warnings.warn(msg, FutureWarning) |
||
143 | upper_limit = limit |
||
144 | |||
145 | if upper_limit is None and lower_limit is None: |
||
146 | raise ValueError( |
||
147 | "At least one of upper_limit and lower_limit needs to be defined." |
||
148 | ) |
||
149 | |||
150 | setattr( |
||
151 | om, |
||
152 | limit_name, |
||
153 | po.Expression( |
||
154 | expr=sum( |
||
155 | om.flow[inflow, outflow, t] |
||
156 | * om.timeincrement[t] |
||
157 | * sequence(getattr(flows[inflow, outflow], keyword))[t] |
||
158 | for (inflow, outflow) in flows |
||
159 | for t in om.TIMESTEPS |
||
160 | ) |
||
161 | ), |
||
162 | ) |
||
163 | |||
164 | if upper_limit is not None: |
||
165 | setattr( |
||
166 | om, |
||
167 | limit_name + "_upper_limit", |
||
168 | po.Constraint(expr=(getattr(om, limit_name) <= upper_limit)), |
||
169 | ) |
||
170 | if lower_limit is not None: |
||
171 | setattr( |
||
172 | om, |
||
173 | limit_name + "_lower_limit", |
||
174 | po.Constraint(expr=(getattr(om, limit_name) >= lower_limit)), |
||
175 | ) |
||
176 | |||
177 | return om |
||
178 | |||
298 |