def test_value_federate_runFederateTestComplex(vFed): rDefaultValue = 1.0 iDefaultValue = 1.0 rTestValue = 2.0 iTestValue = 2.0 pubid = h.helicsFederateRegisterGlobalPublication( vFed, "pub1", h.helics_data_type_complex, "") subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "") h.helicsInputSetDefaultComplex(subid, rDefaultValue, iDefaultValue) h.helicsFederateEnterExecutingMode(vFed) # publish string1 at time=0.0; h.helicsPublicationPublishComplex(pubid, rTestValue, iTestValue) value1, value2 = h.helicsInputGetComplex(subid) assert value1 == rDefaultValue assert value2 == iDefaultValue grantedtime = h.helicsFederateRequestTime(vFed, 1.0) assert grantedtime == 0.01 value1, value2 = h.helicsInputGetComplex(subid) assert value1 == rTestValue assert value2 == iTestValue
def test_valuefederate_test_complex(): broker = createBroker() vFed, fedinfo = createValueFederate() rDefaultValue = 1.0 iDefaultValue = 1.0 rTestValue = 2.0 iTestValue = 2.0 pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_COMPLEX, "") subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "") h.helicsInputSetDefaultComplex(subid, complex(rDefaultValue, iDefaultValue)) h.helicsFederateEnterExecutingMode(vFed) # publish string1 at time=0.0 h.helicsPublicationPublishComplex(pubid, complex(rTestValue, iTestValue)) assert (rDefaultValue, iDefaultValue) == h.helicsInputGetComplex(subid) grantedtime = h.helicsFederateRequestTime(vFed, 1.0) assert grantedtime == 0.01 assert (rTestValue, iTestValue) == h.helicsInputGetComplex(subid) destroyFederate(vFed, fedinfo) destroyBroker(broker)
def test_bad_input_tests_raw_tests(): broker = createBroker(1) vFed1, fedinfo = createValueFederate(1, "test") pubid = h.helicsFederateRegisterPublication(vFed1, "pub1", h.HELICS_DATA_TYPE_RAW, "") subid = h.helicsFederateRegisterGlobalInput(vFed1, "inp1", h.HELICS_DATA_TYPE_RAW, "") h.helicsPublicationAddTarget(pubid, "inp1") h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0) h.helicsFederateEnterExecutingMode(vFed1) h.helicsPublicationPublishRaw(pubid, b"hello world") h.helicsFederateRequestNextStep(vFed1) s = h.helicsInputGetRawValue(subid) assert s == b"hello world" h.helicsPublicationPublishDouble(pubid, 27) h.helicsFederateRequestNextStep(vFed1) s = h.helicsInputGetComplex(subid) assert complex(*s) != 27 + 0j h.helicsFederateFinalize(vFed1) t, res = h.helicsFederateRequestTimeIterative( vFed1, 1.0, h.HELICS_ITERATION_REQUEST_NO_ITERATION) assert res == h.HELICS_ITERATION_RESULT_HALTED destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def _get_input_based_on_type(self, in_put): """ Get input based on type :param in_put: :return: """ val = None sub_id = in_put['sub_id'] try: if in_put['type'] == 'integer': val = h.helicsInputGetInteger(sub_id) elif in_put['type'] == 'double': val = h.helicsInputGetDouble(sub_id) elif in_put['type'] == 'string': val = h.helicsInputGetString(sub_id) elif in_put['type'] == 'complex': real, imag = h.helicsInputGetComplex(sub_id) val = [real, imag] elif in_put['type'] == 'vector': val = h.helicsInputGetVector(sub_id) elif in_put['type'] == 'boolean': val = h.helicsInputGetBoolean(sub_id) else: _log.error("Unknown datatype: {}".format(in_put['type'])) except h._helics.HelicsException as e: _log.exception("Error getting input from HELICS {}".format(e)) return val
def main(): # broker = create_broker() # Broker already created from 1st terminal fed = create_federate() # Register publication pubid = h.helicsFederateRegisterGlobalPublication( fed, "TransmissionSim/B2Voltage", h.helics_data_type_complex, "") # Register subscription subid = h.helicsFederateRegisterSubscription( fed, "DistributionSim_B2_G_1/totalLoad", "") # Register endpoint epid = h.helicsFederateRegisterEndpoint(fed, "ep1", None) # h.helicsSubscriptionSetDefaultComplex(subid, 0, 0) # Enter execution mode h.helicsFederateEnterExecutingMode(fed) hours = 1 seconds = int(60 * 60 * hours) grantedtime = -1 random.seed(0) for t in range(0, seconds, 60 * 5): c = complex(132790.562, 0) * (1 + (random.random() - 0.5) / 2) logger.info("Voltage value = {} kV".format(abs(c) / 1000)) status = h.helicsPublicationPublishComplex(pubid, c.real, c.imag) # status = h.helicsEndpointSendEventRaw(epid, "fixed_price", 10, t) while grantedtime < t: grantedtime = h.helicsFederateRequestTime(fed, t) time.sleep(1) rValue, iValue = h.helicsInputGetComplex(subid) logger.info("Python Federate grantedtime = {}".format(grantedtime)) logger.info("Load value = {} MVA".format( complex(rValue, iValue) / 1000)) t = 60 * 60 * 24 while grantedtime < t: grantedtime = h.helicsFederateRequestTime(fed, t) logger.info("Destroying federate") destroy_federate(fed)
def update_subscriptions(self): for subName, subList in self.Subscriptions.items(): for subInfo in subList: sub = subInfo["subscriptionObj"] if subInfo["dType"].lower() == "complex": value = h.helicsInputGetComplex(sub) elif subInfo["dType"].lower() == 'real': value = h.helicsInputGetDouble(sub) elif subInfo["dType"].lower() == 'text': value = h.helicsInputGetString(sub) elif subInfo["dType"].lower() == "number": value = h.helicsInputGetInteger(sub) else: value = h.helicsInputGetDouble(sub) if value: value = value * subInfo["mult"] X1 = subInfo["elementObj"].GetValue(subInfo["property"]) if value < 10000.0 and value > -100000.0: subInfo["elementObj"].SetValue(value, subInfo["property"]) #X2 = subInfo["elementObj"].GetValue(subInfo["property"]) self.__Logger.debug( f"{subInfo['class']}.{subInfo['name']}.{subInfo['property']} updated to {value}" ) if self.settings['helics']['coiter_mode']: if self.c_seconds != self.c_seconds_old: subInfo["dStates"] = [self.init_state ] * self.n_states else: subInfo["dStates"].insert(0, subInfo["dStates"].pop()) subInfo["dStates"][0] = value #self.cympy.study.Save() return
logger.info("Voltage value = {} kV".format(abs(voltage_gld) / 1000)) for i in range(0, pubkeys_count): pub = pubid["m{}".format(i)] status = h.helicsPublicationPublishComplex(pub, voltage_gld.real, voltage_gld.imag) # status = h.helicsEndpointSendEventRaw(epid, "fixed_price", 10, t) while grantedtime < t: grantedtime = h.helicsFederateRequestTime(fed, t) time.sleep(0.1) ############################# Subscribing to Feeder Load from to GridLAB-D ############################################## for i in range(0, subkeys_count): sub = subid["m{}".format(i)] rload, iload = h.helicsInputGetComplex(sub) logger.info("Python Federate grantedtime = {}".format(grantedtime)) logger.info("Load value = {} kW".format(complex(rload, iload) / 1000)) #print(voltage_plot,real_demand) actual_demand = peak_demand * bus_profiles[x, :] ppc['bus'][:, 2] = actual_demand ppc['bus'][:, 3] = actual_demand * math.tan(math.acos(.85)) ppc['bus'][cosim_bus, 2] = rload * load_amplification_factor / 1000000 ppc['bus'][cosim_bus, 3] = iload * load_amplification_factor / 1000000 ppopt = ppoption(PF_ALG=1) print('PF TIme is {} and ACOPF time is {}'.format( time_pf[x], time_opf[k])) ############################ Running OPF For optimal power flow intervals ##############################