Exemplo n.º 1
0
def test_bad_input_init_error():
    broker = createBroker(1)
    vFed1, fedinfo = createValueFederate(1, "fed0")

    # register the publications

    # the types here don't match which causes an error when initializing the federation
    h.helicsFederateRegisterGlobalTypePublication(vFed1, "pub1", "custom1", "")

    subid = h.helicsFederateRegisterTypeInput(vFed1, "inp1", "custom2", "")
    k1 = h.helicsInputGetKey(subid)

    # check some other calls
    inp2 = h.helicsFederateGetInput(vFed1, "inp1")
    k2 = h.helicsInputGetKey(inp2)
    assert k1 == k2

    inp3 = h.helicsFederateGetInputByIndex(vFed1, 0)
    k3 = h.helicsInputGetKey(inp3)
    assert k1 == k3

    h.helicsInputAddTarget(subid, "pub1")

    h.helicsFederateSetTimeProperty(vFed1, h.HELICS_PROPERTY_TIME_PERIOD, 1.0)

    # unknown publication
    with pt.raises(h.HelicsException):
        # @test_throws h.HELICSErrorInvalidArgument
        pub3 = h.helicsFederateGetPublication(vFed1, "unknown")

    # error in this call from the mismatch
    with pt.raises(h.HelicsException):
        # @test_throws h.HELICSErrorConnectionFailure
        h.helicsFederateEnterInitializingMode(vFed1)

    with pt.raises(h.HelicsException):
        # @test_throws h.HELICSErrorInvalidFunctionCall
        h.helicsFederateRequestTimeAdvance(vFed1, 0.1)

    # unknown input
    with pt.raises(h.HelicsException):
        # @test_throws h.HELICSErrorInvalidArgument
        inp4 = h.helicsFederateGetInput(vFed1, "unknown")

    # invalid input index
    # TODO: does this test segfault some times?
    with pt.raises(h.HelicsException):
        # @test_throws h.HELICSErrorInvalidArgument
        inp5 = h.helicsFederateGetInputByIndex(vFed1, 4)

    h.helicsFederateFinalize(vFed1)

    destroyFederate(vFed1, fedinfo)
    destroyBroker(broker)
Exemplo n.º 2
0
# -*- coding: utf-8 -*-
import os
import helics as h
from math import pi

fed = h.helicsCreateCombinationFederateFromConfig(
    os.path.join(os.path.dirname(__file__), "sender.json"))

topicA = h.helicsFederateGetPublication(fed, "topicA")

h.helicsFederateEnterExecutingMode(fed)

currenttime = 0

for t in range(5, 10 + 1):
    while currenttime < t:
        currenttime = h.helicsFederateRequestTime(fed, t)
    print(f"Sending value = {pi} at time = {currenttime}")
    h.helicsPublicationPublishDouble(topicA, pi)

h.helicsFederateFinalize(fed)
h.helicsFederateFree(fed)
h.helicsCloseLibrary()
Exemplo n.º 3
0
# -*- coding: utf-8 -*-
import helics as h
import time
import struct
import math

initstring = "-f 2 --name=mainbroker"
broker = h.helicsCreateBroker("zmq", "", initstring)

fed = h.helicsCreateCombinationFederateFromConfig("sender.json")
pub = h.helicsFederateGetPublication(fed, "data")

h.helicsFederateEnterExecutingMode(fed)

for request_time in range(1, 10):
    h.helicsFederateRequestTime(fed, request_time)
    h.helicsPublicationPublishDouble(pub, math.pi)

h.helicsFederateFinalize(fed)
h.helicsFederateFree(fed)
h.helicsCloseLibrary()
Exemplo n.º 4
0
def test_valuefederate_subscription_and_publication_registration():

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

    pubid = h.helicsFederateRegisterPublication(vFed, "pub1", h.HELICS_DATA_TYPE_STRING, "")
    pubid2 = h.helicsFederateRegisterGlobalPublication(vFed, "pub2", h.HELICS_DATA_TYPE_INT, "volts")
    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 == ""

    subid_b = h.helicsFederateGetSubscription(vFed, "sub1")
    tmp = h.helicsSubscriptionGetKey(subid_b)
    assert tmp == "sub1"
    # check the getSubscriptionByIndex function
    subid_c = h.helicsFederateGetInputByIndex(vFed, 2)
    tmp = h.helicsInputGetUnits(subid_c)
    assert tmp == "V"
    # check publications

    sv = h.helicsPublicationGetKey(pubid)
    sv2 = h.helicsPublicationGetKey(pubid2)
    assert sv == "Testfed0/pub1"
    assert sv2 == "pub2"
    pub3name = h.helicsPublicationGetKey(pubid3)
    assert pub3name == "Testfed0/pub3"

    type = h.helicsPublicationGetType(pubid3)
    assert type == "double"
    units = h.helicsPublicationGetUnits(pubid3)
    assert units == "V"

    # check the getSubscription function

    pubid_b = h.helicsFederateGetPublication(vFed, "Testfed0/pub1")
    tmp = h.helicsPublicationGetType(pubid_b)
    assert tmp == "string"
    # check the getSubscriptionByIndex function
    pubid_c = h.helicsFederateGetPublicationByIndex(vFed, 1)
    tmp = h.helicsPublicationGetUnits(pubid_c)
    assert tmp == "volts"

    # this one should be invalid
    # @test_throws h.HELICSErrorInvalidArgument pubid_d = h.helicsFederateGetPublicationByIndex(vFed, 5)

    h.helicsFederateFinalize(vFed)

    state = h.helicsFederateGetState(vFed)
    assert state == h.HELICS_STATE_FINALIZE

    destroyFederate(vFed, fedinfo)
    destroyBroker(broker)