| Conditions | 7 |
| Total Lines | 97 |
| Code Lines | 58 |
| 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 -*- |
||
| 140 | def _create(self, group=None): |
||
| 141 | """Creates the relation for the class:`Link`. |
||
| 142 | |||
| 143 | Parameters |
||
| 144 | ---------- |
||
| 145 | group : list |
||
| 146 | List of oemof.solph.components.experimental.Link objects for which |
||
| 147 | the relation of inputs and outputs is createdBuildAction |
||
| 148 | e.g. group = [link1, link2, link3, ...]. The components inside |
||
| 149 | the list need to hold an attribute `conversion_factors` of type |
||
| 150 | dict containing the conversion factors for all inputs to outputs. |
||
| 151 | """ |
||
| 152 | if group is None: |
||
| 153 | return None |
||
| 154 | |||
| 155 | m = self.parent_block() |
||
| 156 | |||
| 157 | all_conversions = {} |
||
| 158 | for n in group: |
||
| 159 | all_conversions[n] = { |
||
| 160 | k: v for k, v in n.conversion_factors.items() |
||
| 161 | } |
||
| 162 | |||
| 163 | self.LINKS = Set(initialize=[g for g in group]) |
||
| 164 | |||
| 165 | directed_conversions = { |
||
| 166 | n: n.conversion_factors for n in group if n.limit_direction |
||
| 167 | } |
||
| 168 | self.LINK_1ST_INFLOWS = Set( |
||
| 169 | initialize=[ |
||
| 170 | (list(c)[0][0], n) for n, c in directed_conversions.items() |
||
| 171 | ] |
||
| 172 | ) |
||
| 173 | self.LINK_2ND_INFLOWS = Set( |
||
| 174 | initialize=[ |
||
| 175 | (list(c)[1][0], n) for n, c in directed_conversions.items() |
||
| 176 | ] |
||
| 177 | ) |
||
| 178 | |||
| 179 | # 0: Flows 1 connected; 1: Flows 2 connected |
||
| 180 | self.direction = Var(self.LINKS, m.TIMESTEPS, within=Binary) |
||
| 181 | |||
| 182 | def _input_output_relation(block): |
||
| 183 | for t in m.TIMESTEPS: |
||
|
|
|||
| 184 | for n, conversion in all_conversions.items(): |
||
| 185 | for cidx, c in conversion.items(): |
||
| 186 | try: |
||
| 187 | expr = ( |
||
| 188 | m.flow[n, cidx[1], t] |
||
| 189 | == c[t] * m.flow[cidx[0], n, t] |
||
| 190 | ) |
||
| 191 | except ValueError: |
||
| 192 | raise ValueError( |
||
| 193 | "Error in constraint creation", |
||
| 194 | "from: {0}, to: {1}, via: {2}".format( |
||
| 195 | cidx[0], cidx[1], n |
||
| 196 | ), |
||
| 197 | ) |
||
| 198 | block.relation.add((n, cidx[0], cidx[1], t), (expr)) |
||
| 199 | |||
| 200 | self.relation = Constraint( |
||
| 201 | [ |
||
| 202 | (n, cidx[0], cidx[1], t) |
||
| 203 | for t in m.TIMESTEPS |
||
| 204 | for n, conversion in all_conversions.items() |
||
| 205 | for cidx, c in conversion.items() |
||
| 206 | ], |
||
| 207 | noruleinit=True, |
||
| 208 | ) |
||
| 209 | self.relation_build = BuildAction(rule=_input_output_relation) |
||
| 210 | |||
| 211 | def _flow1_rule(block, i, link, t): |
||
| 212 | """Rule definition for Eq. (2).""" |
||
| 213 | expr = 1 >= ( |
||
| 214 | self.direction[link, t] |
||
| 215 | + m.flow[i, link, t] |
||
| 216 | * m.flows[i, link].max[t] |
||
| 217 | * m.flows[i, link].nominal_value |
||
| 218 | ) |
||
| 219 | return expr |
||
| 220 | |||
| 221 | self.flow1 = Constraint( |
||
| 222 | self.LINK_1ST_INFLOWS, m.TIMESTEPS, rule=_flow1_rule |
||
| 223 | ) |
||
| 224 | |||
| 225 | def _flow2_rule(block, i, link, t): |
||
| 226 | """Rule definition for Eq. (3).""" |
||
| 227 | expr = 0 <= ( |
||
| 228 | self.direction[link, t] |
||
| 229 | - m.flow[i, link, t] |
||
| 230 | * m.flows[i, link].max[t] |
||
| 231 | * m.flows[i, link].nominal_value |
||
| 232 | ) |
||
| 233 | return expr |
||
| 234 | |||
| 235 | self.flow2 = Constraint( |
||
| 236 | self.LINK_2ND_INFLOWS, m.TIMESTEPS, rule=_flow2_rule |
||
| 237 | ) |
||
| 238 |