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_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 test_federate_tests_federateGeneratedGlobalError(): brk = h.helicsCreateBroker("inproc", "gbrokerc", "--root") cr = h.helicsCreateCore("inproc", "gcore", "--broker=gbrokerc") argv = ["", "--corename=gcore"] fi = h.helicsCreateFederateInfo() h.helicsFederateInfoLoadFromArgs(fi, argv) fed1 = h.helicsCreateValueFederate("fed0", fi) h.helicsFederateInfoFree(fi) h.helicsFederateEnterExecutingMode(fed1) h.helicsFederateRequestTime(fed1, 2.0) h.helicsFederateGlobalError(fed1, 9827, "user generated global error") with pt.raises(h.HelicsException): h.helicsFederateRequestTime(fed1, 3.0) h.helicsFederateDestroy(fed1) h.helicsCoreDisconnect(cr) h.helicsBrokerDisconnect(brk) h.helicsCloseLibrary()
def test_system_tests_federate_logging(): lfile = "log.txt" rm(lfile, force=True) core = h.helicsCreateCore("inproc", "clogf", "--autobroker --log_level=trace") fi = h.helicsCreateFederateInfo() h.helicsFederateInfoSetBrokerKey(fi, "key") h.helicsFederateInfoSetCoreName(fi, "clogf") fed = h.helicsCreateValueFederate("f1", fi) h.helicsFederateSetLogFile(fed, lfile) h.helicsFederateLogLevelMessage(fed, 7, "hello") h.helicsFederateLogErrorMessage(fed, "hello") h.helicsFederateLogDebugMessage(fed, "hello") h.helicsFederateLogWarningMessage(fed, "hello") h.helicsFederateClearMessages(fed) h.helicsCoreSetLogFile(core, lfile) h.helicsCoreDisconnect(core) h.helicsFederateFinalize(fed) h.helicsFederateInfoFree(fi) h.helicsCloseLibrary() assert isfile(lfile) rm(lfile, force=True)
def test_system_tests_core_logging(): lfile = "log.txt" rm(lfile, force=True) core = h.helicsCreateCore("inproc", "clog", "--autobroker --log_level=trace") h.helicsCoreSetLogFile(core, lfile) h.helicsCoreDisconnect(core) h.helicsCloseLibrary() assert isfile(lfile) rm(lfile, force=True)
def test_system_test_federate_global_value(): brk = h.helicsCreateBroker("inproc", "gbrokerc", "--root") cr = h.helicsCreateCore("inproc", "gcore", "--broker=gbrokerc") argv = ["", "--corename=gcore"] fi = h.helicsCreateFederateInfo() h.helicsFederateInfoLoadFromArgs(fi, argv) fed = h.helicsCreateValueFederate("fed0", fi) fi2 = h.helicsFederateInfoClone(fi) h.helicsFederateInfoFree(fi2) h.helicsFederateInfoFree(fi) globalVal = "this is a string constant that functions as a global" globalVal2 = "this is a second string constant that functions as a global" h.helicsFederateSetGlobal(fed, "testglobal", globalVal) q = h.helicsCreateQuery("global", "testglobal") res = h.helicsQueryExecute(q, fed) assert res == globalVal h.helicsFederateSetGlobal(fed, "testglobal2", globalVal2) h.helicsQueryFree(q) q = h.helicsCreateQuery("global", "testglobal2") h.helicsQueryExecuteAsync(q, fed) while h.helicsQueryIsCompleted(q) is False: time.sleep(0.20) res = h.helicsQueryExecuteComplete(q) assert res == globalVal2 q2 = h.helicsCreateQuery("", "isinit") h.helicsQueryExecuteAsync(q2, fed) while h.helicsQueryIsCompleted(q2) is False: time.sleep(0.20) res = h.helicsQueryExecuteComplete(q2) assert str(res).lower() == "false" h.helicsFederateFinalize(fed) h.helicsCoreDisconnect(cr) h.helicsBrokerDisconnect(brk) h.helicsQueryFree(q) h.helicsQueryFree(q2) assert h.helicsBrokerIsConnected(brk) is False h.helicsBrokerDisconnect(brk) h.helicsCoreDisconnect(cr) assert h.helicsBrokerIsConnected(brk) is False h.helicsCloseLibrary()
def test_python_api3(): core1 = h.helicsCreateCore("inproc", "core1", "--autobroker") assert """HelicsCore(identifier = "core1", address = "core1")""" in repr( core1) core2 = core1.clone() assert core1.identifier == "core1" source_filter1 = core1.register_filter(h.HELICS_FILTER_TYPE_DELAY, "core1SourceFilter") source_filter1.add_source_target("ep1") assert ( """<{ 'CONNECTION_REQUIRED' = 0, 'CONNECTION_OPTIONAL' = 0, 'SINGLE_CONNECTION_ONLY' = 0, 'MULTIPLE_CONNECTIONS_ALLOWED' = 0, 'BUFFER_DATA' = 0, 'STRICT_TYPE_CHECKING' = 0, 'IGNORE_UNIT_MISMATCH' = 0, 'ONLY_TRANSMIT_ON_CHANGE' = 0, 'ONLY_UPDATE_ON_CHANGE' = 0, 'IGNORE_INTERRUPTS' = 0, 'MULTI_INPUT_HANDLING_METHOD' = 0, 'INPUT_PRIORITY_LOCATION' = 0, 'CLEAR_PRIORITY_LIST' = 0, 'CONNECTIONS' = 0 }>""" in repr(source_filter1.option)) source_filter1.option["CONNECTION_REQUIRED"] = 1 assert source_filter1.option["CONNECTION_REQUIRED"] == 1 source_filter1.add_source_target("hello") source_filter1.add_destination_target("world") source_filter1.remove_destination_target("world") source_filter1.info = "hello world" assert source_filter1.info == "hello world" source_filter1.set("hello", 1) destination_filter1 = core1.register_filter(h.HELICS_FILTER_TYPE_DELAY, "core1DestinationFilter") destination_filter1.add_destination_target("ep2") cloning_filter1 = core1.register_cloning_filter("ep3") cloning_filter1.remove_delivery_endpoint("ep3") cloning_filter1.add_delivery_endpoint("ep3") assert core1.is_valid() assert core1.is_connected() core1.set_ready_to_init() core1.disconnect() core2.disconnect() del core1 del 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_system_test_core_global_value1(): brk = h.helicsCreateBroker("zmq", "gbrokerc", "--root") cr = h.helicsCreateCore("zmq", "gcore", "--broker=gbrokerc") globalVal = "this is a string constant that functions as a global" _ = "this is a second string constant that functions as a global" h.helicsCoreSetGlobal(cr, "testglobal", globalVal) # q = h.helicsCreateQuery("global", "testglobal") # TODO: This hangs on core execute # res = h.helicsQueryCoreExecute(q, cr) # assert res == globalVal # h.helicsQueryFree(q) # @test_broken False h.helicsCoreDisconnect(cr) h.helicsBrokerDisconnect(brk) assert h.helicsBrokerIsConnected(brk) is False h.helicsCloseLibrary()
def test_system_test_core_global_value2(): brk = h.helicsCreateBroker("zmq", "gbrokerc", "--root") cr = h.helicsCreateCore("zmq", "gcore", "--broker=gbrokerc") connected = h.helicsCoreConnect(cr) assert connected == True assert h.helicsCoreIsConnected(cr) == True globalVal = "this is a string constant that functions as a global" globalVal2 = "this is a second string constant that functions as a global" h.helicsCoreSetGlobal(cr, "testglobal", globalVal) q = h.helicsCreateQuery("global", "testglobal") res = h.helicsQueryCoreExecute(q, cr) assert res == globalVal h.helicsCoreSetGlobal(cr, "testglobal2", globalVal2) h.helicsQueryFree(q) q = h.helicsCreateQuery("global", "testglobal2") res = h.helicsQueryCoreExecute(q, cr) assert res == globalVal2 h.helicsBrokerDisconnect(brk) h.helicsCoreDisconnect(cr) h.helicsQueryFree(q) assert h.helicsBrokerIsConnected(brk) == False
def test_misc_functions_api(): print(h.helicsGetBuildFlags()) assert len(h.helicsGetBuildFlags()) > 0 assert len(h.helicsGetCompilerVersion()) > 0 with pytest.raises(h.HelicsException): h.helicsCreateCore("something random", "here", "not an init string")
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))