def test_value_federate_subscription_and_publication_registration(vFed):
    pubid3 = h.helicsFederateRegisterTypePublication(vFed, "pub3", "double",
                                                     "V")

    subid1 = h.helicsFederateRegisterSubscription(vFed, "sub1", "")
    subid2 = h.helicsFederateRegisterSubscription(vFed, "sub2", "")
    subid3 = h.helicsFederateRegisterSubscription(vFed, "sub3", "V")

    h.helicsFederateEnterExecutingMode(vFed)

    publication_type = h.helicsPublicationGetType(pubid3)
    assert publication_type == 'double'

    sub_key = h.helicsSubscriptionGetKey(subid1)
    assert sub_key == 'sub1'
    sub_type = h.helicsInputGetType(subid1)
    assert sub_type == ''
    sub_key = h.helicsSubscriptionGetKey(subid2)
    assert sub_key == 'sub2'
    sub_key = h.helicsSubscriptionGetKey(subid3)
    assert sub_key == 'sub3'
    sub_type = h.helicsInputGetType(subid3)
    assert sub_type == ''
    sub_units = h.helicsInputGetUnits(subid3)
    assert sub_units == 'V'
    sub_type = h.helicsInputGetType(subid2)
    assert sub_type == ''
Exemple #2
0
def test_valuefederate_test_single_transfer():
    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    s = "n2"

    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_STRING, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishString(pubid, "string1")

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    s = h.helicsInputGetString(subid)

    assert s == "string1"

    t = h.helicsInputLastUpdateTime(subid)
    assert t == 0.01

    h.helicsPublicationPublishString(pubid, "string2")

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #3
0
def test_value_federate_runFederateTestComplex(vFed):
    rDefaultValue = 1.0
    iDefaultValue = 1.0
    rTestValue = 2.0
    iTestValue = 2.0
    pubid = h.helicsFederateRegisterGlobalTypePublication(
        vFed, "pub1", h.HELICS_DATA_TYPE_COMPLEX, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "double", "")
    h.helicsSubscriptionSetDefaultComplex(subid, rDefaultValue, iDefaultValue)

    h.helicsFederateEnterExecutionMode(vFed)

    # publish string1 at time=0.0;
    h.helicsPublicationPublishComplex(pubid, rTestValue, iTestValue)

    status, value1, value2 = h.helicsSubscriptionGetComplex(subid)
    assert value1 == rDefaultValue
    assert value2 == iDefaultValue

    status, grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    status, value1, value2 = h.helicsSubscriptionGetComplex(subid)
    assert value1 == rTestValue
    assert value2 == iTestValue
Exemple #4
0
def test_valuefederate_test_string():
    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    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)

    h.helicsPublicationPublishString(pubid, testValue)

    value = h.helicsInputGetString(subid)
    assert value == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetString(subid)
    assert value == testValue

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #5
0
def test_valuefederate_test_integer():

    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    defaultValue = 1
    testValue = 2
    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_INT, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")
    h.helicsInputSetDefaultInteger(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishInteger(pubid, testValue)

    value = h.helicsInputGetInteger(subid)
    assert value == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetInteger(subid)
    assert value == testValue

    h.helicsPublicationPublishInteger(pubid, testValue + 1)

    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    value = h.helicsInputGetInteger(subid)
    assert value == testValue + 1

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #6
0
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_value_federate_runFederateTestDouble(vFed):
    defaultValue = 1.0
    testValue = 2.0
    pubid = h.helicsFederateRegisterGlobalPublication(
        vFed, "pub1", h.helics_data_type_double, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")
    h.helicsInputSetDefaultDouble(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    # publish string1 at time=0.0;
    h.helicsPublicationPublishDouble(pubid, testValue)

    value = h.helicsInputGetDouble(subid)
    assert value == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetDouble(subid)
    assert value == testValue

    # publish string1 at time=0.0;
    h.helicsPublicationPublishDouble(pubid, testValue + 1)

    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    value = h.helicsInputGetDouble(subid)
    assert value == testValue + 1
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
Exemple #9
0
def test_iteration_execution_iteration_test():

    broker = createBroker(1)
    vFed1, fedinfo = createValueFederate(1, "fed0")
    # register the publications

    pubid = h.helicsFederateRegisterGlobalPublication(
        vFed1, "pub1", h.HELICS_DATA_TYPE_DOUBLE, "")

    subid = h.helicsFederateRegisterSubscription(vFed1, "pub1", "")
    h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_DELTA, 1.0)

    h.helicsFederateEnterInitializingMode(vFed1)
    h.helicsPublicationPublishDouble(pubid, 27.0)

    comp = h.helicsFederateEnterExecutingModeIterative(
        vFed1, h.HELICS_ITERATION_REQUEST_ITERATE_IF_NEEDED)
    assert comp == h.HELICS_ITERATION_RESULT_ITERATING
    val = h.helicsInputGetDouble(subid)
    assert val == 27.0

    comp = h.helicsFederateEnterExecutingModeIterative(
        vFed1, h.HELICS_ITERATION_REQUEST_ITERATE_IF_NEEDED)

    assert comp == h.HELICS_ITERATION_RESULT_NEXT_STEP

    val2 = h.helicsInputGetDouble(subid)

    assert val2 == val

    h.helicsFederateFinalize(vFed1)
    destroyFederate(vFed1, fedinfo)
    destroyBroker(broker)
Exemple #10
0
def test_valuefederate_test_info_filed():

    broker = createBroker()
    vFed, fedinfo = createValueFederate(1, "fed0")

    h.helicsFederateSetFlagOption(vFed, h.HELICS_HANDLE_OPTION_CONNECTION_OPTIONAL, True)
    # register the publications/subscriptions

    subid1 = h.helicsFederateRegisterSubscription(vFed, "sub1", "")
    pubid1 = h.helicsFederateRegisterTypePublication(vFed, "pub1", "string", "")
    pubid2 = h.helicsFederateRegisterGlobalTypePublication(vFed, "pub2", "string", "")

    # Set info fields
    h.helicsInputSetInfo(subid1, "sub1_test")
    h.helicsPublicationSetInfo(pubid1, "pub1_test")
    h.helicsPublicationSetInfo(pubid2, "pub2_test")
    h.helicsFederateEnterExecutingMode(vFed)

    assert h.helicsInputGetInfo(subid1) == "sub1_test"
    assert h.helicsPublicationGetInfo(pubid1) == "pub1_test"
    assert h.helicsPublicationGetInfo(pubid2) == "pub2_test"

    cr = h.helicsFederateGetCoreObject(vFed)
    h.helicsFederateFinalize(vFed)

    wait = h.helicsCoreWaitForDisconnect(cr, 70)
    if wait is False:
        wait = h.helicsCoreWaitForDisconnect(cr, 500)
    assert wait is True

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #11
0
def test_valuefederate_test_vectord():
    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    defaultValue = [0.0, 1.0, 2.0]
    testValue = [3.0, 4.0, 5.0]
    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_VECTOR, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")
    h.helicsInputSetDefaultVector(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishVector(pubid, testValue)

    assert h.helicsInputGetVector(subid) == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetVector(subid)

    assert value == testValue

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #12
0
def test_value_federate_runFederateTestDouble(vFed):
    defaultValue = 1.0
    testValue = 2.0
    pubid = h.helicsFederateRegisterGlobalTypePublication(
        vFed, "pub1", h.HELICS_DATA_TYPE_DOUBLE, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "double", "")
    h.helicsSubscriptionSetDefaultDouble(subid, defaultValue)

    h.helicsFederateEnterExecutionMode(vFed)

    # publish string1 at time=0.0;
    h.helicsPublicationPublishDouble(pubid, testValue)

    status, value = h.helicsSubscriptionGetDouble(subid)
    assert value == defaultValue

    status, grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    status, value = h.helicsSubscriptionGetDouble(subid)
    assert value == testValue

    # publish string1 at time=0.0;
    h.helicsPublicationPublishDouble(pubid, testValue + 1)

    status, grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    status, value = h.helicsSubscriptionGetDouble(subid)
    assert value == testValue + 1
def test_value_federate_runFederateTestInteger(vFed):
    defaultValue = 1
    testValue = 2
    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1",
                                                      h.helics_data_type_int,
                                                      "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")
    h.helicsInputSetDefaultInteger(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishInteger(pubid, testValue)

    value = h.helicsInputGetInteger(subid)
    assert value == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetInteger(subid)
    assert value == testValue

    h.helicsPublicationPublishInteger(pubid, testValue + 1)

    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    value = h.helicsInputGetInteger(subid)
    assert value == testValue + 1
Exemple #14
0
def test_value_federate_runFederateTestInteger(vFed):
    defaultValue = 1
    testValue = 2
    pubid = h.helicsFederateRegisterGlobalTypePublication(
        vFed, "pub1", h.HELICS_DATA_TYPE_INT, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "int", "")
    h.helicsSubscriptionSetDefaultInteger(subid, defaultValue)

    h.helicsFederateEnterExecutionMode(vFed)

    h.helicsPublicationPublishInteger(pubid, testValue)

    status, value = h.helicsSubscriptionGetInteger(subid)
    assert value == defaultValue

    status, grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    status, value = h.helicsSubscriptionGetInteger(subid)
    assert value == testValue

    h.helicsPublicationPublishInteger(pubid, testValue + 1)

    status, grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    status, value = h.helicsSubscriptionGetInteger(subid)
    assert value == testValue + 1
Exemple #15
0
def main(delay=None):

    broker = create_broker()
    fed = create_value_federate(broker)

    pubid = h.helicsFederateRegisterGlobalTypePublication(
        fed, "federate1-to-federate2", h.HELICS_DATA_TYPE_STRING, "")
    subid = h.helicsFederateRegisterSubscription(fed, "federate2-to-federate1",
                                                 "double", "")
    epid = h.helicsFederateRegisterGlobalEndpoint(fed, "endpoint1", "")

    if delay is not None:
        fid = h.helicsFederateRegisterSourceFilter(fed, h.helics_delay_filter,
                                                   "endpoint2", "filter-name")

    h.helicsSubscriptionSetDefaultDouble(subid, 0)

    print("Entering execution mode")
    h.helicsFederateEnterExecutionMode(fed)

    if delay is not None:
        h.helicsFilterSet(fid, "delay", 2.0)

    grantedtime = -1
    while True:
        try:
            stop_at_time, value_to_send = get_input(grantedtime)
        except KeyboardInterrupt:
            print("")
            break
        while grantedtime < stop_at_time:
            print(">>>>>>>> Requesting time = {}".format(stop_at_time))
            status, grantedtime = h.helicsFederateRequestTime(
                fed, stop_at_time)
            assert status == 0
            if grantedtime != stop_at_time:
                status, value = h.helicsSubscriptionGetString(subid)
                assert status == 0
                print("Interrupt value '{}' from Federate 2".format(value))
            print("<<<<<<<< Granted Time = {}".format(grantedtime))
        assert grantedtime == stop_at_time, "stop_at_time = {}, grantedtime = {}".format(
            stop_at_time, grantedtime)
        if value_to_send is not None and value_to_send != '':
            print("Sending '{}' to Federate 2".format(value_to_send))
            status = h.helicsPublicationPublishString(pubid,
                                                      str(value_to_send))
            assert status == 0
            status = h.helicsEndpointSendMessageRaw(epid, "endpoint2",
                                                    str(value_to_send))
            assert status == 0
        status, value = h.helicsSubscriptionGetString(subid)
        assert status == 0
        print("Received value '{}' from Federate 2".format(value))
        while h.helicsEndpointHasMessage(epid):
            value = h.helicsEndpointGetMessage(epid)
            print("Received message '{}' at time {} from Federate 2".format(
                value.data, value.time))
        print("----------------------------------")

    destroy_value_federate(fed, broker)
Exemple #16
0
    def _registerFederateSubscriptions(self, subs):
        """
        :param subs:
        :return:
        """

        if subs is not None:
            SubscriptionList = subs
        else:
            self._sub_file_reader = pySubscriptionReader(
                os.path.join(
                    self._system_paths["ExportLists"],
                    "Subscriptions.toml",
                ), )
            SubscriptionList = self._sub_file_reader.SubscriptionList
        self._subscriptions = {}
        self._subscription_dState = {}
        for element, subscription in SubscriptionList.items():
            assert element in self._objects_by_element, '"{}" listed in the subscription file not '.format(element) +\
                                                     "available in PyDSS's master object dictionary."

            sub = helics.helicsFederateRegisterSubscription(
                self._PyDSSfederate, subscription["Subscription ID"],
                subscription["Unit"])
            #helics.helicsInputSetMinimumChange(sub, self._settings.helics.error_tolerance)
            self._logger.info(
                'Subscription registered: "{}" with units "{}"'.format(
                    subscription["Subscription ID"], subscription["Unit"]))
            subscription['Subscription'] = sub
            self._subscriptions[element] = subscription
            self._subscription_dState[element] = [self.init_state
                                                  ] * self.n_states
        return
Exemple #17
0
    def initialize(self, vFed, cnt):
        """This function prepares the data for running the test.

        Args:
            vFed (helics federate object) - The value federate.

            cnt (int) - An initial number.  In this case, it is
            the number of federates.

        Returns:
            (null)
        """
        logging.info("echo hub - preparing the data for the run")
        self.vFed = vFed
        self.cnt_ = cnt
        i = 0
        while i < self.cnt_:
            leafname = "leafrx_{}".format(i)
            self.pubs.append(
                h.helicsFederateRegisterGlobalPublication(
                    self.vFed, leafname, h.helics_data_type_string, ""))
            leafname2 = "leafsend_{}".format(i)
            self.subs.append(
                h.helicsFederateRegisterSubscription(self.vFed, leafname2, ""))
            i += 1
        self.initialized = True
        logging.info("echo hub - the data is prepared for the run")
Exemple #18
0
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)
Exemple #19
0
def test_value_federate_subscription_registration(vFed):
    subid1 = h.helicsFederateRegisterSubscription(vFed, "sub1", "double", "V")
    subid2 = h.helicsFederateRegisterTypeSubscription(vFed, "sub2",
                                                      h.HELICS_DATA_TYPE_INT,
                                                      "")
    subid3 = h.helicsFederateRegisterOptionalSubscription(
        vFed, "sub3", "double", "V")
    h.helicsFederateEnterExecutionMode(vFed)
def main(delay=None):

    broker = create_broker()
    fed = create_value_federate(broker)

    pubid = h.helicsFederateRegisterGlobalTypePublication(
        fed, "federate1-to-federate2", "string", "")
    subid = h.helicsFederateRegisterSubscription(fed, "federate2-to-federate1",
                                                 "")
    epid = h.helicsFederateRegisterGlobalEndpoint(fed, "endpoint1", "")

    if delay is not None:
        fid = h.helicsFederateRegisterGlobalFilter(fed,
                                                   h.helics_filter_type_delay,
                                                   "filter-name")
        h.helicsFilterAddSourceTarget(fid, "endpoint1")

    h.helicsInputSetDefaultNamedPoint(subid, "", 0)

    print("Entering execution mode")
    h.helicsFederateEnterExecutingMode(fed)

    if delay is not None:
        h.helicsFilterSet(fid, "delay", delay)
    grantedtime = -1
    while True:
        try:
            stop_at_time, value_to_send = get_input(grantedtime)
            print(stop_at_time)
        except KeyboardInterrupt:
            print("")
            break
        while grantedtime < stop_at_time:
            print(">>>>>>>> Requesting time = {}".format(stop_at_time))
            grantedtime = h.helicsFederateRequestTime(fed, stop_at_time)
            grantedtime = int(grantedtime)
            if grantedtime != stop_at_time:
                value = h.helicsSubscriptionGetKey(subid)
                print("Interrupt value '{}' from Federate 2".format(value))
            print("<<<<<<<< Granted Time = {}".format(grantedtime))
        assert (grantedtime == stop_at_time
                ), "stop_at_time = {}, grantedtime = {}".format(
                    stop_at_time, grantedtime)
        if value_to_send is not None and value_to_send != "":
            print("Sending '{}' to Federate 2".format(value_to_send))
            h.helicsPublicationPublishString(pubid, str(value_to_send))
            h.helicsEndpointSendMessageRaw(epid, "endpoint2",
                                           str(value_to_send))
        value = h.helicsSubscriptionGetKey(subid)
        print("Received value '{}' from Federate 2".format(value))
        while h.helicsEndpointHasMessage(epid):
            value = h.helicsEndpointGetMessage(epid)
            print("Received message '{}' at time {} from Federate 2".format(
                value.data, value.time))
        print("----------------------------------")

    destroy_value_federate(fed, broker)
def test_value_federate_runFederateTestNamedPoint(vFed):
    defaultValue = (
        "start of a longer string in place of the shorter one and now this should be very long"
    )
    defVal = 5.3
    # testValue1 = "inside of the functional relationship of helics"
    testValue1 = "short string"
    testVal1 = 45.7823
    testValue2 = "I am a string"
    testVal2 = 0.0

    pubid = h.helicsFederateRegisterGlobalPublication(
        vFed, "pub1", h.helics_data_type_named_point, ""
    )
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsInputSetDefaultNamedPoint(subid, defaultValue, defVal)

    h.helicsFederateEnterExecutingMode(vFed)

    # publish string1 at time=0.0;
    h.helicsPublicationPublishNamedPoint(pubid, testValue1, testVal1)

    # double val;
    val = h.helicsInputGetNamedPoint(subid)
    # assert value == defaultValue
    assert val == [defaultValue, defVal]

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)

    assert grantedtime == 0.01

    # get the value
    val2 = h.helicsInputGetNamedPoint(subid)
    # make sure the string is what we expect
    # assert value2 == testValue1
    assert val2 == [testValue1, testVal1]

    # publish a second string
    h.helicsPublicationPublishNamedPoint(pubid, testValue2, testVal2)

    # make sure the value is still what we expect
    val3 = h.helicsInputGetNamedPoint(subid)
    # make sure the string is what we expect
    # assert value3 == testValue1
    assert val3 == [testValue1, testVal1]

    # advance time
    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    assert grantedtime == 0.02

    # make sure the value was updated
    val4 = h.helicsInputGetNamedPoint(subid)
    # make sure the string is what we expect
    # assert value4 == testValue2
    assert val4 == [testValue2, testVal2]
def test_value_federate_single_transfer(vFed):

    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.helics_data_type_string, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishString(pubid, "string1")

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    s = h.helicsInputGetString(subid)
    assert s == "string1"
Exemple #23
0
def test_valuefederate_test_bool():
    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    defaultValue = True
    testValue1 = True
    testValue2 = False

    # register the publications
    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_BOOLEAN, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsInputSetDefaultBoolean(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    # publish string1 at time=0.0
    h.helicsPublicationPublishBoolean(pubid, testValue1)
    val = h.helicsInputGetBoolean(subid)

    assert val == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    # get the value
    val = h.helicsInputGetBoolean(subid)

    # make sure the string is what we expect
    assert val == testValue1

    # publish a second string
    h.helicsPublicationPublishBoolean(pubid, testValue2)

    # make sure the value is still what we expect
    val = h.helicsInputGetBoolean(subid)
    assert val == testValue1
    # advance time
    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    # make sure the value was updated
    assert grantedtime == 0.02

    val = h.helicsInputGetBoolean(subid)
    assert val == testValue2

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #24
0
def test_valuefederate_named_point():
    broker = createBroker()
    vFed, fedinfo = createValueFederate()

    defaultValue = "start of a longer string in place of the shorter one and now this should be very long"
    defVal = 5.3
    # testValue1 = "inside of the functional relationship of helics"
    testValue1 = "short string"
    testVal1 = 45.7823
    testValue2 = "I am a string"
    testVal2 = 0.0

    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.HELICS_DATA_TYPE_NAMED_POINT, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsInputSetDefaultNamedPoint(subid, defaultValue, defVal)

    h.helicsFederateEnterExecutingMode(vFed)

    # publish string1 at time=0.0
    h.helicsPublicationPublishNamedPoint(pubid, testValue1, testVal1)

    assert h.helicsInputGetNamedPoint(subid) == (defaultValue, defVal)

    assert h.helicsFederateRequestTime(vFed, 1.0) == 0.01

    # # get the value
    assert h.helicsInputGetNamedPoint(subid) == (testValue1, testVal1)

    # publish a second string
    h.helicsPublicationPublishNamedPoint(pubid, testValue2, testVal2)

    # # make sure the value is still what we expect
    assert h.helicsInputGetNamedPoint(subid) == (testValue1, testVal1)
    # # make sure the string is what we expect
    # # assert value3 == testValue1
    # assert val3 == [testValue1, testVal1]

    # # advance time
    assert h.helicsFederateRequestTime(vFed, 2.0) == 0.02

    # # make sure the value was updated
    assert h.helicsInputGetNamedPoint(subid) == (testValue2, testVal2)

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)
Exemple #25
0
    def setup(self, ID):
        """ID needs to be a list that contains all the T&D interface nodes."""
        try:
            # read solar mapping
            self.solarData=np.genfromtxt(dirName+'/data/solar_diffusion_map.csv',\
            delimiter=',')

            self.ID = ID

            # set up comm with helics
            self.fi = fi = h.helicsFederateInfoCreate()  # create info obj
            status = h.helicsFederateInfoSetFederateName(fi, "pyPflow")
            status = h.helicsFederateInfoSetCoreTypeFromString(fi, "zmq")
            status = h.helicsFederateInfoSetCoreInitString(fi, "--federates=1")
            status = h.helicsFederateInfoSetTimeDelta(
                fi, 300)  # 5 min dispatch interval
            status = h.helicsFederateInfoSetLoggingLevel(fi, 1)
            self.vf = vf = h.helicsCreateValueFederate(fi)
            self.pub = pub = h.helicsFederateRegisterGlobalPublication(
                vf, "adaptive_volt_var", "string", "")

            self.sub = sub = {}
            for entry in ID:  # subscribe to all IDs
                sub['pyGld_' +
                    str(entry)] = h.helicsFederateRegisterSubscription(
                        vf, 'pyGld_' + str(entry), "string", "")

            status = h.helicsFederateEnterExecutionMode(vf)

            # Read schema
            f = open(dirName + '/data/schema_case9.json')
            self.data = eval(f.read())
            f.close()

            # add V in schema for all subscriptions
            for entry in ID:
                self.data['mpc']['get']['V'].append([entry, 0, 0])

            # run initial PFLOW (base case condition as read from .m file)
            self.socket.send_string(json.dumps(
                self.data))  # send instructions to pflow
            self.msgFromPflow = eval(
                self.socket.recv())  # receive data from pflow

        except:
            PrintException()
def test_value_federate_runFederateTestBool(vFed):
    defaultValue = True
    testValue1 = True
    testValue2 = False

    # register the publications
    pubid = h.helicsFederateRegisterGlobalPublication(
        vFed, "pub1", h.helics_data_type_boolean, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")

    h.helicsInputSetDefaultBoolean(
        subid, h.helics_true if defaultValue else h.helics_false)

    h.helicsFederateEnterExecutingMode(vFed)

    # publish string1 at time=0.0;
    h.helicsPublicationPublishBoolean(
        pubid, h.helics_true if testValue1 else h.helics_false)
    val = h.helicsInputGetBoolean(subid)

    assert val == h.helics_true if defaultValue else h.helics_false

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    # get the value
    val = h.helicsInputGetBoolean(subid)

    # make sure the string is what we expect
    assert val == h.helics_true if testValue1 else h.helics_false

    # publish a second string
    h.helicsPublicationPublishBoolean(
        pubid, h.helics_true if testValue2 else h.helics_false)

    # make sure the value is still what we expect
    val = h.helicsInputGetBoolean(subid)
    assert val == h.helics_true if testValue1 else h.helics_false
    # advance time
    grantedtime = h.helicsFederateRequestTime(vFed, 2.0)
    # make sure the value was updated
    assert grantedtime == 0.02

    val = h.helicsInputGetBoolean(subid)
    assert val == h.helics_false if testValue2 else h.helics_true
Exemple #27
0
def test_value_federate_single_transfer(vFed):

    pubid = h.helicsFederateRegisterGlobalTypePublication(
        vFed, "pub1", h.HELICS_DATA_TYPE_STRING, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "string", "")

    h.helicsFederateEnterExecutionMode(vFed)

    h.helicsPublicationPublishString(pubid, "string1")

    status, grantedtime = h.helicsFederateRequestTime(vFed, 1.0)

    assert grantedtime == 0.01

    status, s = h.helicsSubscriptionGetString(subid)

    assert status == 0
    assert s == "string1"
def test_value_federate_runFederateTestVectorD(vFed):
    defaultValue = [0, 1, 2]
    testValue = [3, 4, 5]
    pubid = h.helicsFederateRegisterGlobalPublication(vFed, "pub1", h.helics_data_type_vector, "")
    subid = h.helicsFederateRegisterSubscription(vFed, "pub1", "")
    h.helicsInputSetDefaultVector(subid, defaultValue)

    h.helicsFederateEnterExecutingMode(vFed)

    h.helicsPublicationPublishVector(pubid, testValue)

    value = h.helicsInputGetVector(subid)
    assert value == [0, 1, 2]

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetVector(subid)
    assert value == [3, 4, 5]
def test_value_federate_runFederateTestString(vFed):
    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)

    h.helicsPublicationPublishString(pubid, testValue)

    value = h.helicsInputGetString(subid)
    assert value == defaultValue

    grantedtime = h.helicsFederateRequestTime(vFed, 1.0)
    assert grantedtime == 0.01

    value = h.helicsInputGetString(subid)
    assert value == testValue
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)
Exemple #31
0
# Set the message interval (timedelta) for federate. Note that
# 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
print("PI RECEIVER: Setting Federate Info Time Delta")
h.helicsFederateInfoSetTimeProperty(fedinfo, h.helics_property_time_delta, deltat)

# Create value federate
print("PI RECEIVER: Creating Value Federate")
vfed = h.helicsCreateValueFederate("TestB Federate", fedinfo)
print("PI RECEIVER: Value federate created")

# Subscribe to PI SENDER's publication
sub = h.helicsFederateRegisterSubscription(vfed, "testA", "")
print("PI RECEIVER: Subscription registered")

h.helicsFederateEnterExecutingMode(vfed)
print("PI RECEIVER: Entering execution mode")

value = 0.0
prevtime = 0

currenttime = -1

while currenttime <= 100:

    currenttime = h.helicsFederateRequestTime(vfed, 100)

    value = h.helicsInputGetString(sub)