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
Beispiel #2
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)
Beispiel #3
0
 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
Beispiel #4
0
    def updateHelicsSubscriptions(self):

        for element_name, sub_info in self._subscriptions.items():
            if 'Subscription' in sub_info:
                value = None
                if sub_info['Data type'].lower() == 'double':
                    value = helics.helicsInputGetDouble(
                        sub_info['Subscription'])
                elif sub_info['Data type'].lower() == 'vector':
                    value = helics.helicsInputGetVector(
                        sub_info['Subscription'])
                elif sub_info['Data type'].lower() == 'string':
                    value = helics.helicsInputGetString(
                        sub_info['Subscription'])
                elif sub_info['Data type'].lower() == 'boolean':
                    value = helics.helicsInputGetBoolean(
                        sub_info['Subscription'])
                elif sub_info['Data type'].lower() == 'integer':
                    value = helics.helicsInputGetInteger(
                        sub_info['Subscription'])

                if value and value != 0:
                    value = value * sub_info['Multiplier']

                    dssElement = self._objects_by_element[element_name]
                    dssElement.SetParameter(sub_info['Property'], value)

                    self._logger.info(
                        'Value for "{}.{}" changed to "{}"'.format(
                            element_name, sub_info['Property'],
                            value * sub_info['Multiplier']))

                    if self._settings.helics.iterative_mode:
                        if self.c_seconds != self.c_seconds_old:
                            self._subscription_dState[element_name] = [
                                self.init_state
                            ] * self.n_states
                        else:
                            self._subscription_dState[element_name].insert(
                                0,
                                self._subscription_dState[element_name].pop())
                        self._subscription_dState[element_name][0] = value
                else:
                    self._logger.warning(
                        '{} will not be used to update element for "{}.{}" '.
                        format(value, element_name, sub_info['Property']))
        self.c_seconds_old = self.c_seconds
Beispiel #5
0
    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
Beispiel #6
0
def test_iteration_async_test():

    broker = createBroker(1)
    vFed1, fedinfo1 = createValueFederate(1, "fed0")
    vFed2, fedinfo2 = createValueFederate(1, "fed1")

    # register the publications
    pub1 = h.helicsFederateRegisterGlobalPublication(vFed1, "pub1",
                                                     h.HELICS_DATA_TYPE_INT)

    sub1 = h.helicsFederateRegisterSubscription(vFed2, "pub1")
    pub2 = h.helicsFederateRegisterGlobalPublication(vFed2, "pub2",
                                                     h.HELICS_DATA_TYPE_INT)

    sub2 = h.helicsFederateRegisterSubscription(vFed1, "pub2")
    h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0)
    h.helicsFederateSetTimeProperty(vFed2, h.HELICS_PROPERTY_TIME_PERIOD, 1.0)
    # vFed1->setLoggingLevel(5)
    # vFed2->setLoggingLevel(5)

    h.helicsFederateEnterInitializingModeAsync(vFed1)
    h.helicsFederateEnterInitializingMode(vFed2)
    h.helicsFederateEnterInitializingModeComplete(vFed1)

    c1 = 0
    c2 = 0

    h.helicsPublicationPublishInteger(pub1, c1)
    h.helicsPublicationPublishInteger(pub2, c2)

    h.helicsFederateEnterExecutingModeAsync(vFed1)
    h.helicsFederateEnterExecutingMode(vFed2)
    h.helicsFederateEnterExecutingModeComplete(vFed1)

    while c1 <= 10:
        h.helicsInputGetInteger(sub1), c1
        h.helicsInputGetInteger(sub2), c2
        c1 += 1
        c2 += 1

        if c1 <= 10:
            h.helicsPublicationPublishInteger(pub1, c1)
            h.helicsPublicationPublishInteger(pub2, c2)

        h.helicsFederateRequestTimeIterativeAsync(
            vFed1, 1.0, h.HELICS_ITERATION_REQUEST_ITERATE_IF_NEEDED)

        grantedTime, state = h.helicsFederateRequestTimeIterative(
            vFed2, 1.0, h.HELICS_ITERATION_REQUEST_ITERATE_IF_NEEDED)
        if c1 <= 10:
            # assert state == h.HELICS_ITERATION_RESULT_ITERATING
            assert grantedTime == 0.0
        else:
            # assert state == h.HELICS_ITERATION_RESULT_NEXT_STEP
            assert grantedTime == 1.0

        grantedTime, state = h.helicsFederateRequestTimeIterativeComplete(
            vFed1)
        if c1 <= 10:
            # assert state == h.HELICS_ITERATION_RESULT_ITERATING
            assert grantedTime == 0.0
            # assert state == h.HELICS_ITERATION_RESULT_NEXT_STEP
            # assert grantedTime == 1.0

    destroyFederate(vFed1, fedinfo1)
    destroyFederate(vFed2, fedinfo2)
    destroyBroker(broker)