def FreeFederate(fed, fedinfo): h.helicsFederateFinalize(fed) state = h.helicsFederateGetState(fed) assert state == 3 # TODO: should this be 3? h.helicsFederateInfoFree(fedinfo) h.helicsFederateFree(fed)
def init_worker(config_file_name, manager_name, individual_simulation_time): print("worker in") fed = h.helicsCreateMessageFederateFromConfig(config_file_name) print("worker passed config") my_epid = h.helicsFederateGetEndpoint(fed, "data") fed_name = h.helicsFederateGetName(fed) dest = manager_name + "/data" pid = os.getpid() print(fed_name, " about to Started") h.helicsFederateEnterExecutingMode(fed) print(fed_name, " Started") current_time = -1 while current_time < maxtime: current_time = h.helicsFederateRequestTime(fed, maxtime) if h.helicsEndpointHasMessage(my_epid): message = h.helicsEndpointGetMessage(my_epid) data = str(fed_name) + "(" + str(pid) + "): " + str(message.data) # time.sleep(random.randrange(4)) time.sleep(individual_simulation_time) h.helicsEndpointSendMessageRaw(my_epid, str(dest), data) h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def test_combination_federate(): broker = createBroker() cfed = h.helicsCreateCombinationFederateFromConfig( os.path.join(CURRENT_DIRECTORY, "combinationfederate.json")) assert h.helicsFederateIsValid(cfed) assert h.helicsFederateGetEndpointCount(cfed) == 2 assert h.helicsFederateGetFilterCount(cfed) == 0 assert h.helicsFederateGetInputCount(cfed) == 2 ept = h.helicsFederateGetEndpointByIndex(cfed, 0) assert h.helicsEndpointGetName(ept) == "ept1" ipt = h.helicsFederateGetInputByIndex(cfed, 1) assert h.helicsInputGetExtractionUnits(ipt) == "" assert h.helicsSubscriptionGetKey(ipt) == "comboFed/pub2" h.helicsEndpointClearMessages(ept) h.helicsFederateDestroy(cfed) h.helicsFederateFree(cfed) h.helicsBrokerDestroy(broker) h.helicsCloseLibrary()
def init_worker(config_file_name, manager_name): fed = h.helicsCreateMessageFederateFromConfig(config_file_name) my_epid = h.helicsFederateGetEndpoint(fed, "data") fed_name = h.helicsFederateGetName(fed) dest = manager_name + "/data" pid = os.getpid() h.helicsFederateEnterExecutingMode(fed) current_time = h.helicsFederateRequestTime(fed, maxtime) simulation_time_data = h.helicsEndpointGetMessage(my_epid) sleep_time = float(simulation_time_data.data) current_time = -1 while current_time < maxtime: current_time = h.helicsFederateRequestTime(fed, maxtime) if h.helicsEndpointHasMessage(my_epid): message = h.helicsEndpointGetMessage(my_epid) data = str(fed_name) + "(" + str(pid) + "): " + str(message.data) # time.sleep(random.randrange(4)) time.sleep(float(sleep_time)) h.helicsEndpointSendMessageRaw(my_epid, str(dest), data) h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def FreeFederate(fed): status = h.helicsFederateFinalize(fed) status, state = h.helicsFederateGetState(fed) assert state == 3 h.helicsFederateFree(fed)
def close(self, ID=5): try: h.helicsFederateFree(self.vf) h.helicsCloseLibrary() self.pyGldWorker.close(ID=ID) except: PrintException()
def destroy_federate(fed): """ fed: federate to destroy """ h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def init_logger(config_file_name): fed = h.helicsCreateMessageFederateFromConfig(config_file_name) my_epid = h.helicsFederateGetEndpoint(fed, "logger") fed_name = h.helicsFederateGetName(fed) print(fed_name) pid = os.getpid() print("Logger Pid {}".format(pid)) currenttime = -1 output = "" log.basicConfig(filename='./log/logger.log', filemode='w', format='%(message)s', level=log.INFO) h.helicsFederateEnterExecutingMode(fed) while (currenttime < maxtime): currenttime = h.helicsFederateRequestTime(fed, maxtime) if h.helicsEndpointHasMessage(my_epid): count = h.helicsEndpointPendingMessages(my_epid) for i in range(count): message = h.helicsEndpointGetMessage(my_epid) output = output + str(message.data) + "\n" log.info("Current Time : {}".format(currenttime)) log.info(output) h.helicsFederateFinalize(fed) print(fed_name, " : Federate finalized") h.helicsFederateFree(fed) h.helicsCloseLibrary()
def test_value_federate_runFederateTimeoutTest(helicsBroker): fedinitstring = "--broker=mainbroker --federates=1" deltat = 0.01 # Create Federate Info object that describes the federate properties # fedinfo = h.helicsCreateFederateInfo() # Set Federate name # h.helicsFederateInfoSetCoreName(fedinfo, "TestA Core") # Set core type from string # h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq") # Federate init string # h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring) # Set the message interval (timedelta) for federate. Note th# # HELICS minimum message time interval is 1 ns and by default # it uses a time delta of 1 second. What is provided to the # setTimedelta routine is a multiplier for the default timedelta. # Set one second message interval # h.helicsFederateInfoSetTimeProperty(fedinfo, h.helics_property_time_delta, deltat) h.helicsFederateInfoSetIntegerProperty(fedinfo, h.helics_property_int_log_level, 1) vFed = h.helicsCreateValueFederate("TestA Federate", fedinfo) defaultValue = "String1" testValue = "String2" pubid = h.helicsFederateRegisterGlobalPublication( vFed, "pub1", h.helics_data_type_string, "") subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "") h.helicsInputSetDefaultString(subid, defaultValue) h.helicsFederateEnterExecutingMode(vFed) counter = 60 while counter > 0: counter -= 1 time.sleep(1) # Broker should be connected at this point assert h.helicsBrokerIsConnected( helicsBroker) == 1, "Broker should still be connected" h.helicsFederateFinalize(vFed) state = h.helicsFederateGetState(vFed) assert state == 3 while (h.helicsBrokerIsConnected(helicsBroker)): time.sleep(1) h.helicsFederateInfoFree(fedinfo) h.helicsFederateFree(vFed)
def destroy_federate(fed): status = h.helicsFederateFinalize(fed) #while h.helicsBrokerIsConnected(broker): # print('broker is still connected') # time.sleep(1) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def destroy_value_federate(fed): h.helicsFederateFinalize(fed) state = h.helicsFederateGetState(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def destroy_federate(fed, broker=None): status = h.helicsFederateFinalize(fed) state = h.helicsFederateGetState(fed) assert state == 3 while (h.helicsBrokerIsConnected(broker)): time.sleep(1) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def destroy_value_federate(fed): status = h.helicsFederateFinalize(fed) status, state = h.helicsFederateGetState(fed) assert state == 3 h.helicsFederateFree(fed) h.helicsCloseLibrary()
def destroyFederate(fed, fedinfo, broker=None): h.helicsFederateFinalize(fed) _ = h.helicsFederateGetState(fed) if broker is not None: while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateInfoFree(fedinfo) h.helicsFederateFree(fed) if broker is not None: destroyBroker(broker)
def init_manager(config_file_name, worker_name_list): print("manager in") fed = h.helicsCreateMessageFederateFromConfig(config_file_name) print("manager passed config") my_epid = h.helicsFederateGetEndpoint(fed, "data") log_id = h.helicsFederateGetEndpoint(fed, "logger") log_dest = h.helicsEndpointGetDefaultDestination(log_id) currenttime = -1 print("Manager about to Started") h.helicsFederateEnterExecutingMode(fed) print("Manager Started") for t in range(1, 11): h.helicsEndpointSendMessageRaw(log_id, log_dest, "Timestep {}".format(t)) message = "Hello World " + str(t) while currenttime < t: currenttime = h.helicsFederateRequestTime(fed, t) log_msg = "" ################### START TIMING ############### start_time = time.time_ns() / (10**9) ############################################## for worker_name in worker_name_list: h.helicsEndpointSendMessageRaw(my_epid, worker_name + "/data", message) while not (h.helicsEndpointPendingMessages(my_epid) == len(worker_name_list)): currenttime = h.helicsFederateRequestTime(fed, 0) for i in range(len(worker_name_list)): if h.helicsEndpointHasMessage(my_epid): new_message = h.helicsEndpointGetMessage(my_epid) log_msg = log_msg + "From: {} Msg: {} Time: {}\n".format( new_message.source, new_message.data, new_message.time) ################### END TIMING ################## end_time = time.time_ns() / (10**9) ############################################### time_taken = "Time taken for iteration " + str(t) + ": " + str( end_time - start_time) + "\n" h.helicsEndpointSendMessageRaw(log_id, log_dest, log_msg) h.helicsEndpointSendMessageRaw(log_id, log_dest, time_taken) fed_name = h.helicsFederateGetName(fed) h.helicsFederateFinalize(fed) # print("{}: Federate finalized".format(fed_name)) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def vFed(): initstring = "-f 1 --name=mainbroker" fedinitstring = "--broker=mainbroker --federates=1" deltat = 0.01 #TODO: should add an assert here about helicsGetVersion h.helicsGetVersion() # Create broker # broker = h.helicsCreateBroker("zmq", "", initstring) isconnected = h.helicsBrokerIsConnected(broker) if isconnected == 1: pass # Create Federate Info object that describes the federate properties # fedinfo = h.helicsCreateFederateInfo() # Set Federate name # h.helicsFederateInfoSetCoreName(fedinfo, "TestA Core") # Set core type from string # h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq") # Federate init string # h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring) # Set the message interval (timedelta) for federate. Note th# # HELICS minimum message time interval is 1 ns and by default # it uses a time delta of 1 second. What is provided to the # setTimedelta routine is a multiplier for the default timedelta. # Set one second message interval # h.helicsFederateInfoSetTimeProperty(fedinfo, h.helics_property_time_delta, deltat) h.helicsFederateInfoSetIntegerProperty(fedinfo, h.helics_property_int_log_level, 1) vFed = h.helicsCreateValueFederate("TestA Federate", fedinfo) yield vFed h.helicsFederateFinalize(vFed) state = h.helicsFederateGetState(vFed) assert state == 3 while (h.helicsBrokerIsConnected(broker)): time.sleep(1) h.helicsFederateInfoFree(fedinfo) h.helicsFederateFree(vFed) h.helicsCloseLibrary()
def stop_simulation(self, *args, **kwargs): """ Disconnect the federate from helics core and close the library :return: """ try: h.helicsFederateFinalize(self.fed) h.helicsFederateFree(self.fed) h.helicsCloseLibrary() except h._helics.HelicsException as e: _log.exception("Error stopping HELICS federate {}".format(e))
def destroy_federate(fed): h.helicsFederateFinalize(fed) # status, state = h.helicsFederateGetState(fed) # assert state == 3 while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def destroy_value_federate(fed, broker): h.helicsFederateFinalize(fed) state = h.helicsFederateGetState(fed) while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def mFed(): import helics as h initstring = "1 --name=mainbroker" fedinitstring = "--broker=mainbroker --federates=1" deltat = 0.01 helicsversion = h.helicsGetVersion() # Create broker # broker = h.helicsCreateBroker("zmq", "", initstring) isconnected = h.helicsBrokerIsConnected(broker) if isconnected == 1: pass # Create Federate Info object that describes the federate properties # fedinfo = h.helicsFederateInfoCreate() # Set Federate name # status = h.helicsFederateInfoSetFederateName(fedinfo, "TestA Federate") # Set core type from string # status = h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq") # Federate init string # status = h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring) # Set the message interval (timedelta) for federate. Note th# # HELICS minimum message time interval is 1 ns and by default # it uses a time delta of 1 second. What is provided to the # setTimedelta routine is a multiplier for the default timedelta. # Set one second message interval # status = h.helicsFederateInfoSetTimeDelta(fedinfo, deltat) status = h.helicsFederateInfoSetLoggingLevel(fedinfo, 1) mFed = h.helicsCreateMessageFederate(fedinfo) yield mFed status = h.helicsFederateFinalize(mFed) status, state = h.helicsFederateGetState(mFed) assert state == 3 while (h.helicsBrokerIsConnected(broker)): time.sleep(1) h.helicsFederateFree(mFed) h.helicsCloseLibrary()
def mFed(): initstring = "-f 1 --name=mainbroker" fedinitstring = "--broker=mainbroker --federates=1" deltat = 0.01 h.helicsGetVersion() # Create broker # broker = h.helicsCreateBroker("zmq", "", initstring) isconnected = h.helicsBrokerIsConnected(broker) if isconnected == 1: pass # Create Federate Info object that describes the federate properties # fedinfo = h.helicsCreateFederateInfo() # Set Federate name # h.helicsFederateInfoSetCoreName(fedinfo, "CoreA Federate") # Set core type from string # h.helicsFederateInfoSetCoreTypeFromString(fedinfo, "zmq") # Federate init string # h.helicsFederateInfoSetCoreInitString(fedinfo, fedinitstring) # Set the message interval (timedelta) for federate. Note th# # HELICS minimum message time interval is 1 ns and by default # it uses a time delta of 1 second. What is provided to the # setTimedelta routine is a multiplier for the default timedelta. # Set one second message interval # h.helicsFederateInfoSetTimeProperty(fedinfo, h.HELICS_PROPERTY_TIME_DELTA, deltat) h.helicsFederateInfoSetIntegerProperty(fedinfo, h.HELICS_PROPERTY_INT_LOG_LEVEL, 1) mFed = h.helicsCreateMessageFederate("TestA Federate", fedinfo) yield mFed h.helicsFederateFinalize(mFed) state = h.helicsFederateGetState(mFed) assert state == 3 while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateInfoFree(fedinfo) h.helicsFederateFree(mFed) h.helicsCloseLibrary()
def test_valuefederate_test_file_load(): filename = os.path.join(CURRENT_DIRECTORY, "valuefederate.json") vFed = h.helicsCreateValueFederateFromConfig(filename) name = h.helicsFederateGetName(vFed) assert name == "valueFed" assert h.helicsFederateGetInputCount(vFed) == 3 assert h.helicsFederateGetPublicationCount(vFed) == 2 h.helicsFederateFinalize(vFed) h.helicsFederateFree(vFed) h.helicsCloseLibrary()
def destroy_federate(fed): ''' As part of ending a HELICS co-simulation it is good housekeeping to formally destroy a federate. Doing so informs the rest of the federation that it is no longer a part of the co-simulation and they should proceed without it (if applicable). Generally this is done when the co-simulation is complete and all federates end execution at more or less the same wall-clock time. :param fed: Federate to be destroyed :return: (none) ''' status = h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary() logger.info('Federate finalized')
def test_bad_inputs_frees(): broker = createBroker(1) vFed1, fedinfo = createValueFederate(1, "fed0") fi = h.helicsCreateFederateInfo() h.helicsFederateInfoSetBroker(fi, "broker test") h.helicsFederateEnterInitializingMode(vFed1) h.helicsFederateFinalize(vFed1) h.helicsFederateInfoSetBrokerPort(fi, 8929) h.helicsFederateInfoSetLocalPort(fi, "8229") h.helicsFederateInfoFree(fi) h.helicsFederateFree(vFed1) # @test_throws h.HELICSErrorInvalidObject with pt.raises(h.HelicsException): destroyFederate(vFed1, fedinfo) destroyBroker(broker)
def init_manager(config_file_name, worker_name_list): fed = h.helicsCreateMessageFederateFromConfig(config_file_name) my_epid = h.helicsFederateGetEndpoint(fed, "data") log_id = h.helicsFederateGetEndpoint(fed, "logger") log_dest = h.helicsEndpointGetDefaultDestination(log_id) currenttime = -1 h.helicsFederateEnterExecutingMode(fed) for t in range(1, 11): h.helicsEndpointSendMessageRaw(log_id, log_dest, "Timestep {}".format(t)) message = "Hello World " + str(t) while currenttime < t: currenttime = h.helicsFederateRequestTime(fed, t) for worker_name in worker_name_list: h.helicsEndpointSendMessageRaw(my_epid, worker_name + "/data", message) while not (h.helicsEndpointPendingMessages(my_epid) == len(worker_name_list)): currenttime = h.helicsFederateRequestTime(fed, 0) log_msg = "" for i in range(len(worker_name_list)): if h.helicsEndpointHasMessage(my_epid): new_message = h.helicsEndpointGetMessage(my_epid) log_msg = log_msg + "From: {} Msg: {} Time: {}\n".format( new_message.source, new_message.data, new_message.time) h.helicsEndpointSendMessageRaw(log_id, log_dest, log_msg) fed_name = h.helicsFederateGetName(fed) h.helicsFederateFinalize(fed) # print("{}: Federate finalized".format(fed_name)) h.helicsFederateFree(fed) h.helicsCloseLibrary()
def test_logging_api(): fi = h.helicsCreateFederateInfo() broker = h.helicsCreateBroker("zmq", "broker", "--federates 1 --loglevel 1") h.helicsFederateInfoSetCoreInitString(fi, "--federates 1") h.helicsFederateInfoSetIntegerProperty(fi, h.HELICS_PROPERTY_INT_LOG_LEVEL, 5) fed = h.helicsCreateValueFederate("test1", fi) userdata = UserData(5) handle = h.ffi.new_handle(userdata) h.helicsFederateSetLoggingCallback(fed, logger, handle) h.helicsFederateEnterExecutingMode(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateLogInfoMessage(fed, "test MEXAGE") h.helicsFederateRequestNextStep(fed) h.helicsFederateFinalize(fed) assert userdata.x == 9 h.helicsFederateFree(fed) h.helicsFederateInfoFree(fi) h.helicsBrokerDisconnect(broker) h.helicsBrokerFree(broker) h.helicsCleanupLibrary() h.helicsCloseLibrary()
def init_logger(config_file_name, log_out_file_name, total_simulation_time, individual_simulation_time): print("logger in") fed = h.helicsCreateMessageFederateFromConfig(config_file_name) print("logger passed config") my_epid = h.helicsFederateGetEndpoint(fed, "logger") fed_name = h.helicsFederateGetName(fed) # print(fed_name) pid = os.getpid() # print("Logger Pid {}".format(pid)) currenttime = -1 output = "" log.basicConfig(filename="./log/{}".format(log_out_file_name), filemode='w', format='%(message)s', level=log.INFO) print(fed_name, " about to Started") h.helicsFederateEnterExecutingMode(fed) print(fed_name, "Started") while (currenttime < maxtime): currenttime = h.helicsFederateRequestTime(fed, maxtime) if h.helicsEndpointHasMessage(my_epid): count = h.helicsEndpointPendingMessages(my_epid) for i in range(count): message = h.helicsEndpointGetMessage(my_epid) # log.log(str(message.messageID)) output = output + str(message.data) + "message id: " + "\n" log.info(output) h.helicsFederateFinalize(fed) print(fed_name, " : Federate finalized") h.helicsFederateFree(fed) h.helicsCloseLibrary()
assert h.helicsBrokerIsConnected(broker) is True fedinfo = h.helicsCreateFederateInfo() h.helicsFederateInfoSetCoreType(fedinfo, h.helics_core_type_zmq) h.helicsFederateInfoSetCoreInitString(fedinfo, "--loglevel=7") h.helicsFederateInfoSetTimeProperty(fedinfo, h.helics_property_time_delta, 1.0) fed = h.helicsCreateCombinationFederate("sender", fedinfo,) topicA = h.helicsFederateRegisterGlobalPublication( fed, "topicA", h.helics_data_type_double, "" ) h.helicsFederateEnterExecutingMode(fed) currenttime = 0 for t in range(5, 10 + 1): while currenttime < t: currenttime = h.helicsFederateRequestTime(fed, t) print(f"Sending value = {pi} at time = {currenttime}") h.helicsPublicationPublishDouble(topicA, pi) h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) while h.helicsBrokerIsConnected(broker) is True: time.sleep(1) h.helicsBrokerDisconnect(broker) h.helicsCloseLibrary()
def destroy_federate(fed): h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary()
h.helicsFederateEnterExecutingMode(vfed) print("PI SENDER: Entering execution mode") # This federate will be publishing deltat*pi for numsteps steps # this_time = 0.0 value = pi for t in range(5, 10): val = value currenttime = h.helicsFederateRequestTime(vfed, t) h.helicsPublicationPublishDouble(pub, val) print( "PI SENDER: Sending value pi = {} at time {} to PI RECEIVER".format( val, currenttime ) ) time.sleep(1) h.helicsFederateFinalize(vfed) print("PI SENDER: Federate finalized") while h.helicsBrokerIsConnected(broker): time.sleep(1) h.helicsFederateFree(vfed) h.helicsCloseLibrary() print("PI SENDER: Broker disconnected")
def destroy_federate(fed): status = h.helicsFederateFinalize(fed) h.helicsFederateFree(fed) h.helicsCloseLibrary() print("EVController: Federate finalized")