Example #1
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)
def test_message_filter_registration(broker):

    fFed = AddFederate(broker, "zmq", 1, 1, "filter")
    mFed = AddFederate(broker, "zmq", 1, 1, "message")

    h.helicsFederateRegisterGlobalEndpoint(mFed, "port1", "")
    h.helicsFederateRegisterGlobalEndpoint(mFed, "port2", None)

    f1 = h.helicsFederateRegisterSourceFilter(fFed, h.helics_custom_filter,
                                              "filter1", "port1")
    f2 = h.helicsFederateRegisterDestinationFilter(fFed,
                                                   h.helics_custom_filter,
                                                   "filter2", "port2")
    ep1 = h.helicsFederateRegisterEndpoint(fFed, "fout", "")
    f3 = h.helicsFederateRegisterSourceFilter(fFed, h.helics_custom_filter, "",
                                              "filter0/fout")

    h.helicsFederateFinalize(mFed)
    h.helicsFederateFinalize(fFed)

    FreeFederate(fFed)
    FreeFederate(mFed)
    time.sleep(1.0)
def test_message_filter_function(broker):

    fFed = AddFederate(broker, "zmq", 1, 1, "filter")
    mFed = AddFederate(broker, "zmq", 1, 1, "message")

    p1 = h.helicsFederateRegisterGlobalEndpoint(mFed, "port1", "")
    p2 = h.helicsFederateRegisterGlobalEndpoint(mFed, "port2", "")

    f1 = h.helicsFederateRegisterSourceFilter(fFed, h.helics_delay_filter,
                                              "port1", "filter1")
    status = h.helicsFilterSet(f1, "delay", 2.5)
    assert status == 0
    status = h.helicsFederateEnterExecutionModeAsync(fFed)
    assert status == 0
    status = h.helicsFederateEnterExecutionMode(mFed)
    assert status == 0
    status = h.helicsFederateEnterExecutionModeComplete(fFed)
    assert status == 0
    status, state = h.helicsFederateGetState(fFed)
    assert status == 0
    assert state == 2
    data = "hello world"
    h.helicsEndpointSendMessageRaw(p1, "port2", data)
    grantedtime = h.helicsFederateRequestTimeAsync(mFed, 1.0)
    assert grantedtime == 0
    status, grantedtime = h.helicsFederateRequestTime(fFed, 1.0)
    assert status == 0
    assert grantedtime == 1.0
    status, grantedtime = h.helicsFederateRequestTimeComplete(mFed)
    assert status == 0
    assert grantedtime == 1.0

    h.helicsFederateFinalize(mFed)
    h.helicsFederateFinalize(fFed)
    #f2 = h.helicsFederateRegisterDestinationFilter (fFed, h.helics_custom_filter, "filter2", "port2")
    #ep1 = h.helicsFederateRegisterEndpoint (fFed, "fout", "")
    #f3 = h.helicsFederateRegisterSourceFilter (fFed, h.helics_custom_filter, "", "filter0/fout")

    FreeFederate(fFed)
    FreeFederate(mFed)
    time.sleep(1.0)
Example #4
0
def main(delay=None, verbose=False):
    if verbose is not False:
        logger.setLevel(logging.DEBUG)

    mapping = create_mapping()

    logger.info("Creating CombinationFederate for FESTIV")
    fed = create_value_federate()

    pubid1 = h.helicsFederateRegisterGlobalTypePublication(
        fed, "AGCGenDispatch/Alta", h.HELICS_DATA_TYPE_COMPLEX, "")
    pubid2 = h.helicsFederateRegisterGlobalTypePublication(
        fed, "AGCGenDispatch/Brighton", h.HELICS_DATA_TYPE_COMPLEX, "")
    pubid3 = h.helicsFederateRegisterGlobalTypePublication(
        fed, "AGCGenDispatch/ParkCity", h.HELICS_DATA_TYPE_COMPLEX, "")
    pubid4 = h.helicsFederateRegisterGlobalTypePublication(
        fed, "AGCGenDispatch/Solitude", h.HELICS_DATA_TYPE_COMPLEX, "")
    pubid5 = h.helicsFederateRegisterGlobalTypePublication(
        fed, "AGCGenDispatch/Sundance", h.HELICS_DATA_TYPE_COMPLEX, "")

    logger.info("Registering endpoint")
    epid = h.helicsFederateRegisterGlobalEndpoint(fed, "festiv-fixed-price",
                                                  "")
    if delay is not None:
        fedfilter = h.helicsFederateRegisterSourceFilter(
            fed, 1, "festiv-fixed-price", "delay_filter")
        status = h.helicsFilterSet(fedfilter, "delay", int(delay))

    h.helicsFederateEnterExecutionMode(fed)

    time_granted = -1
    last_second = -1
    ticker = 0

    for day in [
            '2020-08-03',
            '2020-08-04',
            '2020-08-05',
            '2020-08-06',
            '2020-08-07',
            '2020-08-08',
            '2020-08-09',
    ]:

        logger.info("Running DAM for day={day}".format(day=day))
        df = get_load(day)
        dam_s = df.loc[day, 'LOAD'].resample('1H').mean()

        dam_m = build_DAM_model(day, dam_s)
        dam_m.solve('cbc', verbose=False)

        rtm_s = df.loc[day, 'LOAD'].resample('5T').mean()

        for interval in range(0, int(24 * 60 / 5)):
            hour = int(interval * 5 / 60)
            logger.info(
                "Running RTM for day={day} for minute={m} (hour={hour})".
                format(day=day, m=interval * 5, hour=hour))
            commitment = dam_m.results.unit_commitment.loc[hour:hour, :]
            rtm_m = build_RTM_model(day, rtm_s.iloc[interval], commitment)
            rtm_m.solve('cbc', verbose=False)
            logger.debug("LMP = {lmp} \t Power Generated = {pg}".format(
                lmp=rtm_m.results.lmp, pg=rtm_m.results.power_generated))

            for minute in range(0, 5):

                for second in range(0, 60):
                    ticker = ticker + 1

                    if int(second % 6) == 0:
                        stop_at_time = ticker
                        while time_granted < stop_at_time:
                            status, time_granted = h.helicsFederateRequestTime(
                                fed, stop_at_time)

                        b2, b3, b4 = rtm_m.results.lmp[['B2', 'B3',
                                                        'B4']].values[0]
                        for name in mapping["B2"]:
                            status = h.helicsEndpointSendMessageRaw(
                                epid, "{}/fixed_price".format(name), str(b2))
                        for name in mapping["B3"]:
                            status = h.helicsEndpointSendMessageRaw(
                                epid, "{}/fixed_price".format(name), str(b3))
                        for name in mapping["B4"]:
                            status = h.helicsEndpointSendMessageRaw(
                                epid, "{}/fixed_price".format(name), str(b4))

                        pg = rtm_m.results.power_generated.loc[0].to_dict()

                        status = h.helicsPublicationPublishDouble(
                            pubid1, pg["ALTA"])
                        status = h.helicsPublicationPublishDouble(
                            pubid2, pg["BRIGHTON"])
                        status = h.helicsPublicationPublishDouble(
                            pubid3, pg["PARKCITY"])
                        status = h.helicsPublicationPublishDouble(
                            pubid4, pg["SOLITUDE"])
                        status = h.helicsPublicationPublishDouble(
                            pubid5, pg["SUNDANCE"])

                        logger.info("Publishing lmp B2={}".format(b2))
                        logger.info("Publishing lmp B3={}".format(b2))
                        logger.info("Publishing lmp B4={}".format(b2))

                        logger.info("Publishing pg = {}".format(pg))
                        logger.info("Current time = {minutes} ".format(
                            minutes=ticker / 60))
status = h.helicsFederateInfoSetPeriod(fedinfo, timePeriod)

status = h.helicsFederateInfoSetLoggingLevel(fedinfo, 1)

# Create value federate #
fed = h.helicsCreateCombinationFederate(fedinfo)
print("PI SENDER: Value federate created")

# Register the publication #
pub = h.helicsFederateRegisterGlobalPublication(fed, "testA", "double", "")
print("PI SENDER: Publication registered")

epid = h.helicsFederateRegisterGlobalEndpoint(fed, "endpoint1", "")

fid = h.helicsFederateRegisterSourceFilter(
    fed, h.helics_delay_filter, "endpoint1", "filter-name"
)

h.helicsFilterSet(fid, "delay", 2.0)

# Enter execution mode #
status = h.helicsFederateEnterExecutionMode(fed)
print("PI SENDER: Entering execution mode")

# This federate will be publishing deltat*pi for numsteps steps #
this_time = 0.0
value = pi

for t in range(5, 10):
    val = value