class Bus(AbstractNode): def __init__(self, name, linesIn, linesOut): """ Initialize a bus. :param name: Name of the bus. :param linesIn: List of ingoing lines :param linesOut: List of outgoing lines """ super(Bus, self).__init__(name, linesIn, linesOut) assert len(linesIn) > 0 assert len(linesOut) > 0 def consistencyCheckP1(self, state): """ This consistency rule checks whether Kirchhoff's current law holds at the bus. :param state: State object (observed or calculated) :return: True if consistency rule holds, False otherwise (violation) """ logCheckDescription("P1", indentation=3) passed = True try: # compare sum of ingoing current with sum of outgoing current sumOfIngoingCurrent = sum([ l.retrieveValue(state, self, "local", "current") for l in self.linesIn ]) sumOfOutgoingCurrent = sum([ l.retrieveValue(state, self, "local", "current") for l in self.linesOut ]) passed = isClose(sumOfIngoingCurrent, sumOfOutgoingCurrent) logDebugCheckValues( "Ingoing current: %f (==) Outgoing current: %f." % (sumOfIngoingCurrent, sumOfOutgoingCurrent), passed, indentation=3) except ValueNotStoredException, e: logDebugUnknownValues(e.message, indentation=3) logCheckPassed("P1", passed, indentation=3) return passed
class Consumer(AbstractNode): def __init__(self, name, linesIn, linesOut, consumedPowerKey=None): """ Initialize a consumer. :param name: Name of the consumer. :param linesIn: List of ingoing lines :param linesOut: List of outgoing lines :param generatedPowerKey: Key for retrieving power state """ super(Consumer, self).__init__(name, linesIn, linesOut) assert len(linesIn) == 1 assert len(linesOut) == 0 self.consumedPowerKey = consumedPowerKey if consumedPowerKey else "%s_P" % self.name.upper( ) def consistencyCheckP5b(self, state): """ This consistency rule checks whether P = I * V holds for the consumer. :param state: State object (observed or calculated) :return: True if consistency rule holds, False otherwise (violation) """ logCheckDescription("P5b", indentation=3) passed = True try: l = self.linesIn[0] localVoltage = l.retrieveValue(state, self, "local", "voltage") localCurrent = l.retrieveValue(state, self, "local", "current") calculatedPower = localVoltage * localCurrent consumedPower = (-1) * state.retrieveValue(self.consumedPowerKey) passed = isClose(calculatedPower, consumedPower) logDebugCheckValues("Consumer %s. V=%f,A=%f. V*A=%f (==) P=%f." % (self.name, localVoltage, localCurrent, calculatedPower, consumedPower), passed, indentation=3) except ValueNotStoredException, e: logDebugUnknownValues(e.message, indentation=3) logCheckPassed("P5b", passed, indentation=3) return passed
# l.name, localVoltage, localCurrent, str(currentPassed)), indentation=3) logDebugCheckValues( "Open circuit (switch/fuse/protectiveRelay) on line %s. Local: A=%f (==0.0). Remote values unknown." % (l.name, localCurrent), currentPassed, indentation=3) except ValueNotStoredException, e: pass else: logDebugCheckValues( "No open circuit (by switch/fuse/protectiveRelay) found at Bus %s." % (self.name), True, indentation=3) logCheckPassed("P3", passed, indentation=3) return passed def consistencyCheckP4(self, state): """ This consistency rule checks that the voltage and current at the start of a line is the same as at the end. :param state: State object (observed or calculated) :return: True if consistency rule holds, False otherwise (violation) """ logCheckDescription("P4", indentation=3) passed = True for l in self.getAllConnectedLines(): try: localVoltage = l.retrieveValue(state, self, "local", "voltage") localCurrent = l.retrieveValue(state, self, "local", "current") remoteVoltage = l.retrieveValue(state, self, "remote",
l.retrieveValue(state, self, "local", "voltage") for l in self.getAllConnectedLines() ] relevantMeasuredVoltages = [ v for v in allMeasuredVoltages if not isClose(v, 0.00) ] passed = isClose(min(relevantMeasuredVoltages), max(relevantMeasuredVoltages)) logDebugCheckValues( "Minimum V: %f (==) Maximum V: %f." % (min(relevantMeasuredVoltages), max(relevantMeasuredVoltages)), passed, indentation=3) except ValueNotStoredException, e: logDebugUnknownValues(e.message, indentation=3) logCheckPassed("P2", passed, indentation=3) return passed def executeConsistencyCheck(self, state): """ Execute consistency check over this compnent. :param state: State object which contains state information """ logAllChecksDescription("CONSISTENCY", "BUS %s" % self.name, indentation=2) checkStatus = dict() try: checkStatus["P1"] = self.consistencyCheckP1(state) checkStatus["P2"] = self.consistencyCheckP2(state) checkStatus["P3"] = self.consistencyCheckP3(state)
except ValueNotStoredException, e: logDebugUnknownValues(e.message, l.name, indentation=3) try: consumedPower = (-1) * state.retrieveValue(l.consumedPowerKey) localVoltage = l.linesIn[0].retrieveValue(state, l, "local", "voltage") localSwitch = l.linesIn[0].retrieveValue(state, l, "local", "switchState") currentPassed = localSwitch and not isZero(localVoltage) passed = False if not currentPassed else passed logDebugCheckValues("Consumer %s connected to power supply. Local Switch: %s (== True). Remote Switch unknown. V=%f (>0) P=%fW." % (l.name, localSwitch, localVoltage, consumedPower), currentPassed, indentation=3) except ValueNotStoredException, e: pass else: currentPassed = False passed = False if not currentPassed else passed logCheckPassed("R6", passed, indentation=2) return passed def safetyCheckR7(self, state): """ This safety rule checks whether the global generated power equals the global consumed power :param state: State object (observed or calculated) :return: True if safety rule holds, False otherwise (violation) """ logCheckDescription("R7", indentation=2) passed = True try: sumOfGeneratedPower = sum( [state.retrieveValue(g.generatedPowerKey) for g in getAllComponentsOfType(Generator)]) sumOfConsumedPower = (-1) * sum( [state.retrieveValue(c.consumedPowerKey) for c in getAllComponentsOfType(Consumer)])
expectedMeasuredTransformedOutVoltage), passed, indentation=3) except IndexError: passed = False logDebugCheckValues( "Transformer %s. Discrete tap function, tap position %d has no rate defined. " % (self.name, int(round(currentTapPosition))), passed, indentation=3) except ValueNotStoredException, e: passed = True logDebugUnknownValues(e.message, indentation=3) else: passed = False logCheckPassed("P6a", passed, indentation=3) return passed def consistencyCheckP6b(self, state): """ This consistency rule checks whether the transformation rate is consistent with the current measurement. :param state: State object (observed or calculated) :return: True if consistency rule holds, False otherwise (violation) """ logCheckDescription("P6b", indentation=3) passed = True if callable(self.rateFunction): try: measuredInCurrent = self.linesIn[0].retrieveValue( state, self, "local", "current") measuredOutCurrent = self.linesOut[0].retrieveValue(