예제 #1
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
예제 #2
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)
예제 #3
0
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
예제 #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