def test_filter_types_tests_core_fitler_registration(): core1 = h.helicsCreateCore("inproc", "core1", "--autobroker") core2 = h.helicsCoreClone(core1) core1IdentifierString = h.helicsCoreGetIdentifier(core1) assert core1IdentifierString == "core1" sourceFilter1 = h.helicsCoreRegisterFilter(core1, h.HELICS_FILTER_TYPE_DELAY, "core1SourceFilter") h.helicsFilterAddSourceTarget(sourceFilter1, "ep1") destinationFilter1 = h.helicsCoreRegisterFilter(core1, h.HELICS_FILTER_TYPE_DELAY, "core1DestinationFilter") h.helicsFilterAddDestinationTarget(destinationFilter1, "ep2") cloningFilter1 = h.helicsCoreRegisterCloningFilter(core1, "ep3") h.helicsFilterRemoveDeliveryEndpoint(cloningFilter1, "ep3") core1IsConnected = h.helicsCoreIsConnected(core1) assert core1IsConnected != 0 h.helicsCoreSetReadyToInit(core1) h.helicsCoreDisconnect(core1) h.helicsCoreDisconnect(core2) h.helicsCoreFree(core1) h.helicsCoreFree(core2) h.helicsCloseLibrary()
def test_query_broker_tests(): broker = createBroker(2) vFed1, fedinfo1 = createValueFederate(1, "fed0") vFed2, fedinfo2 = createValueFederate(1, "fed1") core = h.helicsFederateGetCoreObject(vFed1) q1 = h.helicsCreateQuery("root", "federates") res = h.helicsQueryCoreExecute(q1, core) name1 = h.helicsFederateGetName(vFed1) name2 = h.helicsFederateGetName(vFed2) try: assert f"[{name1};{name2}]" == res except AssertionError: assert [name1, name2] == res res = h.helicsQueryExecute(q1, vFed1) try: assert f"[{name1};{name2}]" == res except AssertionError: assert [name1, name2] == res h.helicsFederateEnterInitializingModeAsync(vFed1) h.helicsFederateEnterInitializingMode(vFed2) h.helicsFederateEnterInitializingModeComplete(vFed1) h.helicsQueryFree(q1) h.helicsCoreFree(core) h.helicsFederateFinalizeAsync(vFed1) h.helicsFederateFinalize(vFed2) h.helicsFederateFinalizeComplete(vFed1) destroyFederate(vFed1, fedinfo1) destroyFederate(vFed2, fedinfo2) destroyBroker(broker)
def test_core_api(): core1 = h.helicsCreateCore("inproc", "core1", "--autobroker") assert h.helicsCoreIsValid(core1) is True core2 = h.helicsCoreClone(core1) assert "core1" in h.helicsCoreGetIdentifier(core1) assert h.helicsCoreIsConnected(core1) == 0 sourceFilter1 = h.helicsCoreRegisterFilter(core1, h.HELICS_FILTER_TYPE_DELAY, "core1SourceFilter") h.helicsFilterAddSourceTarget(sourceFilter1, "ep1") destinationFilter1 = h.helicsCoreRegisterFilter(core1, h.HELICS_FILTER_TYPE_DELAY, "core1DestinationFilter") h.helicsFilterAddDestinationTarget(destinationFilter1, "ep2") cloningFilter1 = h.helicsCoreRegisterCloningFilter(core1, "ep3") h.helicsFilterRemoveDeliveryEndpoint(cloningFilter1, "ep3") h.helicsCoreSetReadyToInit(core1) h.helicsCoreDisconnect(core1) h.helicsCoreDisconnect(core2) h.helicsCoreFree(core1) h.helicsCoreFree(core2) h.helicsCloseLibrary()
def BMecho_singleCore(federates): """This function performs the echo test. Args: federates (int) - The number of federates to create for the single core echo test. Returns: (null) """ logging.info("starting the single core test") t = Timer(1, timer) t.cancel() feds = [f for f in range(0, federates)] wcore = h.helicsCreateCore( "inproc", None, "--autobroker --federates={}".format((federates))) hub = EchoHub_c() hub_vFed = hub.create_value_federate(h.helicsCoreGetIdentifier(wcore)) hub.initialize(hub_vFed, federates) leafs = [EchoLeaf_c() for f in range(0, federates)] i = 0 leaf_vFeds = [] logging.info("preparing the federates") for f in feds: leaf_vFed = leafs[f].create_value_federate( h.helicsCoreGetIdentifier(wcore), i) leafs[f].initialize(leaf_vFed, i) leaf_vFeds.append(leaf_vFed) i += 1 threads = [] i = 0 logging.info("creating the threads") for l, f in zip(leaf_vFeds, feds): x = Thread(target=leafs[f].run, name=leafs[f], args=(len(feds) + 1, l)) threads.append(x) x.start() i += 1 time.sleep(0.1) hub.make_ready(hub_vFed) logging.info("executing the echo hub") t.start() hub.run(len(feds) + 1, hub_vFed) t.cancel() logging.info("joining the threads") for thrd in threads: thrd.join() h.helicsCoreFree(wcore) h.helicsCleanupLibrary() logging.info("finished the single core test")
def test_query_federate_tests(): broker = createBroker(2) vFed1, fedinfo1 = createValueFederate(1, "fed0") vFed2, fedinfo2 = createValueFederate(1, "fed1") h.helicsFederateRegisterGlobalTypePublication(vFed1, "pub1", "double", "") h.helicsFederateRegisterTypePublication(vFed1, "pub2", "double", "") h.helicsFederateRegisterTypePublication(vFed2, "pub3", "double", "") h.helicsFederateEnterInitializingModeAsync(vFed1) h.helicsFederateEnterInitializingMode(vFed2) h.helicsFederateEnterInitializingModeComplete(vFed1) core = h.helicsFederateGetCoreObject(vFed1) q1 = h.helicsCreateQuery("Testfed0", "publications") res = h.helicsQueryCoreExecute(q1, core) try: assert res == "[pub1;Testfed0/pub2]" except: assert res == ["pub1", "Testfed0/pub2"] # res = h.helicsQueryExecute(q1, vFed2) # assert res == "[pub1;Testfed0/pub2]" h.helicsQueryFree(q1) # q1 = h.helicsCreateQuery("Testfed1", "isinit") # res = h.helicsQueryExecute(q1, vFed1) # assert res, "True" # h.helicsQueryFree(q1) # q1 = h.helicsCreateQuery("Testfed1", "publications") # res = h.helicsQueryExecute(q1, vFed1) # assert res == "[Testfed1/pub3]" # h.helicsQueryFree(q1) h.helicsCoreFree(core) h.helicsFederateFinalizeAsync(vFed1) h.helicsFederateFinalize(vFed2) h.helicsFederateFinalizeComplete(vFed1) destroyFederate(vFed1, fedinfo1) destroyFederate(vFed2, fedinfo2) destroyBroker(broker)
def test_filter_test_types_clone_test_dest_connections(): broker = createBroker(3) sFed, fedinfo1 = createMessageFederate(1, "source", 1.0) dFed, fedinfo2 = createMessageFederate(1, "dest", 1.0) dcFed, fedinfo3 = createMessageFederate(1, "dest_clone", 2.0) p1 = h.helicsFederateRegisterGlobalEndpoint(sFed, "src", "") _ = h.helicsFederateRegisterGlobalEndpoint(dFed, "dest", "") _ = h.helicsFederateRegisterGlobalEndpoint(dcFed, "cm", "") f1 = h.helicsFederateRegisterGlobalCloningFilter(dcFed, "filt1") h.helicsFilterAddDeliveryEndpoint(f1, "cm") cr = h.helicsFederateGetCoreObject(sFed) h.helicsCoreAddDestinationFilterToEndpoint(cr, "filt1", "dest") h.helicsCoreAddDestinationFilterToEndpoint(cr, "", "dest") h.helicsCoreFree(cr) h.helicsFederateEnterExecutingModeAsync(sFed) h.helicsFederateEnterExecutingModeAsync(dcFed) h.helicsFederateEnterExecutingMode(dFed) h.helicsFederateEnterExecutingModeComplete(sFed) h.helicsFederateEnterExecutingModeComplete(dcFed) q = h.helicsCreateQuery("", "filtered_endpoints") filteredEndpoints = h.helicsQueryExecute(q, dFed) assert "cloningdestFilter" in str(filteredEndpoints) h.helicsQueryFree(q) state = h.helicsFederateGetState(sFed) assert state == h.HELICS_STATE_EXECUTION data = "".join(["a" for _ in range(0, 500)]).encode() h.helicsEndpointSendMessageRaw(p1, "dest", data) h.helicsFederateFinalize(sFed) # TODO: implement threading # auto dFedExec = [&]() { # h.helicsFederateRequestTime(dFed, 1.0) # m2 = h.helicsEndpointGetMessage(p2) # h.helicsFederateFinalize(dFed, "") # } # h.helics_message m3 # auto dcFedExec = [&]() { # h.helicsFederateRequestTime(dcFed, 2.0) # auto res = h.helicsFederateHasMessage(dcFed) # if (res == h.helics_False) { # std::this_thread::sleep_for(std::chrono::milliseconds(50)) # h.helicsFederateRequestTime(dcFed, 4.0) # } # m3 = h.helicsEndpointGetMessage(p3) # h.helicsFederateFinalize(dcFed) # } # auto threaddFed = std::thread(dFedExec) # auto threaddcFed = std::thread(dcFedExec) # threaddFed.join() # (m2.source, "src") # (m2.original_source, "src") # (m2.dest, "dest") # (m2.length, static_cast<int64_t>(data.size())) # threaddcFed.join() # (m3.source, "src") # (m3.original_source, "src") # (m3.dest, "cm") # (m3.original_dest, "dest") # (m3.length, static_cast<int64_t>(data.size())) # (state = h.helicsFederateGetState(sFed)) # (state == h.helics_state_finalize) destroyFederate(sFed, fedinfo1) destroyFederate(dFed, fedinfo2) destroyFederate(dcFed, fedinfo3) destroyBroker(broker)
def BMecho_multiCore(cTypeString, federates): """This function performs the multicore test for a specific core type. Args: cTypeString (str) - Specific core type, e.g. inproc federates (int) - The number of federates to create for the echo multicore test. Returns: (null) """ logging.info("starting the multicore test for {}".format(cTypeString)) t = Timer(1, timer) t.cancel() if h.helicsIsCoreTypeAvailable(cTypeString) == h.helics_false: t.start() feds = [f for f in range(0, federates)] initString = "--log_level=no_print --federates={}".format(federates) broker = h.helicsCreateBroker(cTypeString, "brokerf", initString) wcore = h.helicsCreateCore(cTypeString, "", "--federates=1 --log_level=no_print") hub = EchoHub_c() hub_vFed = hub.create_value_federate(h.helicsCoreGetIdentifier(wcore)) hub.initialize(hub_vFed, federates) leafs = [EchoLeaf_c() for f in range(0, federates)] cores = [] i = 0 leaf_vFeds = [] logging.info("preparing the federates") for f in feds: core = h.helicsCreateCore(cTypeString, None, "-f 1 --log_level=no_print") h.helicsCoreConnect(core) leaf_vFed = leafs[f].create_value_federate( h.helicsCoreGetIdentifier(core), i) leafs[f].initialize(leaf_vFed, i) leaf_vFeds.append(leaf_vFed) cores.append(core) i += 1 threads = [] i = 0 logging.info("creating the threads") for l, f in zip(leaf_vFeds, feds): x = Thread(target=leafs[f].run, name=leafs[f], args=(len(feds) + 1, l)) threads.append(x) x.start() i += 1 time.sleep(0.1) hub.make_ready(hub_vFed) logging.info("executing the echo hub") t.start() hub.run(len(feds) + 1, hub_vFed) t.cancel() logging.info("joining the threads") for thrd in threads: thrd.join() h.helicsBrokerDisconnect(broker) h.helicsBrokerFree(broker) logging.info("clearing the cores") for cr in cores: h.helicsCoreFree(cr) cores.clear() h.helicsCoreFree(wcore) h.helicsCleanupLibrary() logging.info("finished the multicore test for {}".format(cTypeString))