| Conditions | 7 |
| Total Lines | 67 |
| Code Lines | 36 |
| 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 -*- |
||
| 112 | def _create(self, group=None): |
||
| 113 | """Creates the linear constraint for the class:`ElectricalLine` |
||
| 114 | block. |
||
| 115 | |||
| 116 | Parameters |
||
| 117 | ---------- |
||
| 118 | group : list |
||
| 119 | List of oemof.solph.ElectricalLine (eline) objects for which |
||
| 120 | the linear relation of inputs and outputs is created |
||
| 121 | e.g. group = [eline1, eline2, ...]. The components inside the |
||
| 122 | list need to hold a attribute `reactance` of type Sequence |
||
| 123 | containing the reactance of the line. |
||
| 124 | """ |
||
| 125 | if group is None: |
||
| 126 | return None |
||
| 127 | |||
| 128 | m = self.parent_block() |
||
| 129 | |||
| 130 | # create voltage angle variables |
||
| 131 | self.ELECTRICAL_BUSES = Set( |
||
| 132 | initialize=[n for n in m.es.nodes if isinstance(n, ElectricalBus)] |
||
| 133 | ) |
||
| 134 | |||
| 135 | def _voltage_angle_bounds(block, b, t): |
||
| 136 | return b.v_min, b.v_max |
||
| 137 | |||
| 138 | self.voltage_angle = Var( |
||
| 139 | self.ELECTRICAL_BUSES, m.TIMESTEPS, bounds=_voltage_angle_bounds |
||
| 140 | ) |
||
| 141 | |||
| 142 | if True not in [b.slack for b in self.ELECTRICAL_BUSES]: |
||
| 143 | # TODO: Make this robust to select the same slack bus for |
||
| 144 | # the same problems |
||
| 145 | bus = [b for b in self.ELECTRICAL_BUSES][0] |
||
| 146 | logging.info( |
||
| 147 | "No slack bus set,setting bus {0} as slack bus".format( |
||
| 148 | bus.label |
||
| 149 | ) |
||
| 150 | ) |
||
| 151 | bus.slack = True |
||
| 152 | |||
| 153 | def _voltage_angle_relation(block): |
||
| 154 | for t in m.TIMESTEPS: |
||
|
|
|||
| 155 | for n in group: |
||
| 156 | if n.input.slack is True: |
||
| 157 | self.voltage_angle[n.output, t].value = 0 |
||
| 158 | self.voltage_angle[n.output, t].fix() |
||
| 159 | try: |
||
| 160 | lhs = m.flow[n.input, n.output, t] |
||
| 161 | rhs = ( |
||
| 162 | 1 |
||
| 163 | / n.reactance[t] |
||
| 164 | * ( |
||
| 165 | self.voltage_angle[n.input, t] |
||
| 166 | - self.voltage_angle[n.output, t] |
||
| 167 | ) |
||
| 168 | ) |
||
| 169 | except ValueError: |
||
| 170 | raise ValueError( |
||
| 171 | "Error in constraint creation", |
||
| 172 | "of node {}".format(n.label), |
||
| 173 | ) |
||
| 174 | block.electrical_flow.add((n, t), (lhs == rhs)) |
||
| 175 | |||
| 176 | self.electrical_flow = Constraint(group, m.TIMESTEPS, noruleinit=True) |
||
| 177 | |||
| 178 | self.electrical_flow_build = BuildAction(rule=_voltage_angle_relation) |
||
| 179 |