Exemple #1
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 #2
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
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]
Exemple #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
h.helicsFederateEnterExecutingMode(vfed)

basevolt = 4.16

for t in range(1, 1440):
    time_requested = t * 60
    #while time_requested < r_seconds:
    currenttime = h.helicsFederateRequestTime(vfed, time_requested)
    iteration_state = h.helics_iteration_result_iterating
    for i in range(20):
        currenttime, iteration_state = h.helicsFederateRequestTimeIterative(
            vfed, time_requested, h.helics_iteration_request_iterate_if_needed)
        print(iteration_state)
        h.helicsPublicationPublishDouble(pubA, basevolt + random.random() / 30)
        h.helicsPublicationPublishDouble(pubB, basevolt + random.random() / 30)
        h.helicsPublicationPublishDouble(pubC, basevolt + random.random() / 30)
        value = h.helicsInputGetVector(sub1)
        print("PyDSS.Circuit.heco19021.TotalPower: {} kW @ time: {}".format(
            value, currenttime))
        #i+=1

h.helicsFederateFinalize(vfed)
print("PI SENDER: Federate finalized")

while h.helicsBrokerIsConnected(broker):
    time.sleep(1)

h.helicsFederateFree(vfed)
h.helicsCloseLibrary()

print("PI SENDER: Broker disconnected")
Exemple #6
0
def test_valuefederate_default_value_tests():
    broker = createBroker()
    vFed1, fedinfo = createValueFederate(1, "fed0")

    inp_raw1 = h.helicsFederateRegisterInput(vFed1, "key1", h.HELICS_DATA_TYPE_RAW, "raw")
    inp_raw2 = h.helicsFederateRegisterInput(vFed1, "key2", h.HELICS_DATA_TYPE_RAW, "raw")

    inp_bool = h.helicsFederateRegisterInput(vFed1, "key3", h.HELICS_DATA_TYPE_BOOLEAN, "")

    inp_time = h.helicsFederateRegisterInput(vFed1, "key4", h.HELICS_DATA_TYPE_TIME, "")

    inp_char = h.helicsFederateRegisterInput(vFed1, "key5", h.HELICS_DATA_TYPE_STRING, "")

    inp_vect = h.helicsFederateRegisterInput(vFed1, "key6", h.HELICS_DATA_TYPE_VECTOR, "V")

    inp_double = h.helicsFederateRegisterInput(vFed1, "key7", h.HELICS_DATA_TYPE_DOUBLE, "kW")

    inp_double2 = h.helicsFederateRegisterInput(vFed1, "key8", h.HELICS_DATA_TYPE_DOUBLE, "")

    inp_np = h.helicsFederateRegisterInput(vFed1, "key9", h.HELICS_DATA_TYPE_NAMED_POINT, "")

    h.helicsInputSetMinimumChange(inp_double, 1100.0)
    h.helicsInputSetDefaultDouble(inp_double, 10000.0)

    h.helicsInputSetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True)

    pub = h.helicsFederateRegisterPublication(vFed1, "", h.HELICS_DATA_TYPE_INT, "MW")
    h.helicsPublicationSetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED, True)
    h.helicsPublicationAddTarget(pub, "Testfed0/key7")
    h.helicsPublicationAddTarget(pub, "Testfed0/key8")

    h.helicsInputSetDefaultRaw(inp_raw1, "")
    data = "this is a string"
    h.helicsInputSetDefaultRaw(inp_raw2, data)

    h.helicsInputSetDefaultBoolean(inp_bool, True)

    h.helicsInputSetDefaultTime(inp_time, 12.3)
    h.helicsInputSetDefaultChar(inp_char, "q")
    h.helicsInputSetDefaultVector(inp_vect, [])
    h.helicsInputSetDefaultNamedPoint(inp_np, data, 15.7)

    h.helicsFederateEnterExecutingMode(vFed1)
    assert h.helicsInputGetInjectionUnits(inp_double) == "MW"
    assert h.helicsInputGetInjectionUnits(inp_double2) == "MW"
    assert h.helicsInputGetType(inp_double) == "double"
    assert h.helicsInputGetPublicationType(inp_double) == "int64"

    c2 = h.helicsInputGetChar(inp_char)
    assert c2 == "q"
    h.helicsInputGetVector(inp_vect)

    optset = h.helicsInputGetOption(inp_double2, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED)
    assert optset == 1

    optset = h.helicsPublicationGetOption(pub, h.HELICS_HANDLE_OPTION_CONNECTION_REQUIRED)
    assert optset == 1
    h.helicsPublicationPublishInteger(pub, 12)

    h.helicsFederateRequestNextStep(vFed1)
    assert h.helicsInputGetDouble(inp_double) == 12000.0
    assert h.helicsInputGetDouble(inp_double2) == 12.0

    h.helicsPublicationPublishInteger(pub, 13)

    h.helicsFederateRequestNextStep(vFed1)
    assert h.helicsInputIsUpdated(inp_double) is False
    assert h.helicsInputIsUpdated(inp_double2) is True

    assert h.helicsInputGetDouble(inp_double) == 12000.0
    assert h.helicsInputGetDouble(inp_double2) == 13.0

    h.helicsPublicationPublishInteger(pub, 15)

    h.helicsFederateRequestNextStep(vFed1)

    assert h.helicsInputIsUpdated(inp_double) is True
    assert h.helicsInputIsUpdated(inp_double2) is True

    h.helicsInputClearUpdate(inp_double)
    h.helicsInputClearUpdate(inp_double2)

    assert h.helicsInputIsUpdated(inp_double) is False
    assert h.helicsInputIsUpdated(inp_double2) is False

    _, rval = h.helicsInputGetNamedPoint(inp_np)
    assert rval == 15.7

    out, rval = h.helicsInputGetNamedPoint(inp_np)
    assert out == "this is a string"
    assert rval == 15.7

    h.helicsFederateFinalize(vFed1)

    destroyFederate(vFed1, fedinfo)
    destroyBroker(broker)