Пример #1
0
def init_manager(config_file_name, worker_name_list):
    print("manager in")
    fed = h.helicsCreateMessageFederateFromConfig(config_file_name)
    print("manager passed config")
    my_epid = h.helicsFederateGetEndpoint(fed, "data")
    log_id = h.helicsFederateGetEndpoint(fed, "logger")
    log_dest = h.helicsEndpointGetDefaultDestination(log_id)

    currenttime = -1
    print("Manager about to Started")
    h.helicsFederateEnterExecutingMode(fed)
    print("Manager Started")
    for t in range(1, 11):
        h.helicsEndpointSendMessageRaw(log_id, log_dest,
                                       "Timestep {}".format(t))

        message = "Hello World " + str(t)
        while currenttime < t:
            currenttime = h.helicsFederateRequestTime(fed, t)

        log_msg = ""

        ################### START TIMING ###############
        start_time = time.time_ns() / (10**9)
        ##############################################

        for worker_name in worker_name_list:
            h.helicsEndpointSendMessageRaw(my_epid, worker_name + "/data",
                                           message)

        while not (h.helicsEndpointPendingMessages(my_epid)
                   == len(worker_name_list)):
            currenttime = h.helicsFederateRequestTime(fed, 0)

        for i in range(len(worker_name_list)):
            if h.helicsEndpointHasMessage(my_epid):
                new_message = h.helicsEndpointGetMessage(my_epid)
                log_msg = log_msg + "From: {}  Msg: {}  Time: {}\n".format(
                    new_message.source, new_message.data, new_message.time)

        ################### END TIMING ##################
        end_time = time.time_ns() / (10**9)
        ###############################################

        time_taken = "Time taken for iteration " + str(t) + ": " + str(
            end_time - start_time) + "\n"

        h.helicsEndpointSendMessageRaw(log_id, log_dest, log_msg)
        h.helicsEndpointSendMessageRaw(log_id, log_dest, time_taken)

    fed_name = h.helicsFederateGetName(fed)
    h.helicsFederateFinalize(fed)
    #	print("{}: Federate finalized".format(fed_name))
    h.helicsFederateFree(fed)
    h.helicsCloseLibrary()
Пример #2
0
def test_messagefederate_send_receive_2fed_multisend():

    broker = createBroker(2)
    mFed1, fedinfo1 = createMessageFederate(1, "A Federate")
    mFed2, fedinfo2 = createMessageFederate(1, "B Federate")

    epid1 = h.helicsFederateRegisterEndpoint(mFed1, "ep1", "")
    epid2 = h.helicsFederateRegisterGlobalEndpoint(mFed2, "ep2", "random")

    h.helicsEndpointSetOption(epid1, h.HELICS_HANDLE_OPTION_IGNORE_INTERRUPTS,
                              True)

    h.helicsFederateSetTimeProperty(mFed1, h.HELICS_PROPERTY_TIME_DELTA, 1.0)
    h.helicsFederateSetTimeProperty(mFed2, h.HELICS_PROPERTY_TIME_DELTA, 1.0)

    h.helicsFederateEnterExecutingModeAsync(mFed1)
    h.helicsFederateEnterExecutingMode(mFed2)
    h.helicsFederateEnterExecutingModeComplete(mFed1)

    assert h.HELICS_STATE_EXECUTION == h.helicsFederateGetState(mFed1)
    assert h.HELICS_STATE_EXECUTION == h.helicsFederateGetState(mFed2)

    h.helicsEndpointSetDefaultDestination(epid1, "ep2")

    h.helicsEndpointSendMessageRaw(epid1, "", "a".encode())
    h.helicsEndpointSendMessageRaw(epid1, "", "a".encode())
    h.helicsEndpointSendMessageRaw(epid1, "", "a".encode())

    h.helicsFederateRequestTimeAsync(mFed1, 1.0)
    granted_time = h.helicsFederateRequestTime(mFed2, 1.0)
    complete_time = h.helicsFederateRequestTimeComplete(mFed1)

    assert granted_time == 1.0
    assert complete_time == 1.0

    res = h.helicsEndpointPendingMessages(epid2)
    assert res == 3

    res = h.helicsFederatePendingMessages(mFed2)
    assert res == 3

    assert h.helicsEndpointGetDefaultDestination(epid1) == "ep2"

    # FIXME: Someday this will be implemented.
    # @test_broken h.helicsEndpointGetOption(epid1, h.HELICS_HANDLE_OPTION_IGNORE_INTERRUPTS) is True

    destroyFederate(mFed1, fedinfo1)
    destroyFederate(mFed2, fedinfo2)
    destroyBroker(broker)
Пример #3
0
def init_manager(config_file_name, worker_name_list):
    fed = h.helicsCreateMessageFederateFromConfig(config_file_name)

    my_epid = h.helicsFederateGetEndpoint(fed, "data")
    log_id = h.helicsFederateGetEndpoint(fed, "logger")
    log_dest = h.helicsEndpointGetDefaultDestination(log_id)

    currenttime = -1
    h.helicsFederateEnterExecutingMode(fed)

    for t in range(1, 11):
        h.helicsEndpointSendMessageRaw(log_id, log_dest,
                                       "Timestep {}".format(t))

        message = "Hello World " + str(t)
        while currenttime < t:
            currenttime = h.helicsFederateRequestTime(fed, t)

        for worker_name in worker_name_list:
            h.helicsEndpointSendMessageRaw(my_epid, worker_name + "/data",
                                           message)

        while not (h.helicsEndpointPendingMessages(my_epid)
                   == len(worker_name_list)):
            currenttime = h.helicsFederateRequestTime(fed, 0)
        log_msg = ""
        for i in range(len(worker_name_list)):
            if h.helicsEndpointHasMessage(my_epid):
                new_message = h.helicsEndpointGetMessage(my_epid)
                log_msg = log_msg + "From: {}  Msg: {}  Time: {}\n".format(
                    new_message.source, new_message.data, new_message.time)

        h.helicsEndpointSendMessageRaw(log_id, log_dest, log_msg)

    fed_name = h.helicsFederateGetName(fed)
    h.helicsFederateFinalize(fed)

    #	print("{}: Federate finalized".format(fed_name))

    h.helicsFederateFree(fed)
    h.helicsCloseLibrary()
Пример #4
0
                    logger.info(f'\tEV full; removing charging voltage')
            else:
                logger.debug(f'\tNo messages at endpoint {endpoint_name} '
                             f'recieved at '
                             f'time {grantedtime}')

            # Publish updated charging voltage
            h.helicsPublicationPublishDouble(pubid[j], charging_voltage[j])
            logger.debug(
                f'\tPublishing charging voltage of {charging_voltage[j]} '
                f' at time {grantedtime}')

            # Send message to Controller with SOC every 15 minutes
            if grantedtime % 900 == 0:
                destination_name = str(
                    h.helicsEndpointGetDefaultDestination(endid[j]))
                h.helicsEndpointSendMessageRaw(
                    endid[j], "", f'{currentsoc[j]:4f}'.encode())  #
                logger.debug(f'Sent message from endpoint {endpoint_name}'
                             f' at time {grantedtime}'
                             f' with payload SOC {currentsoc[j]:4f}')

        # Calculate the total power required by all chargers. This is the
        #   primary metric of interest, to understand the power profile
        #   and capacity requirements required for this charging garage.
        total_power = 0
        for j in range(0, end_count):
            total_power += charge_rate[(EVlist[j] - 1)]

        # Data collection vectors
        time_sim.append(grantedtime)
Пример #5
0
    currentsoc = np.linspace(0.1, 0.5, num=end_count)

    # Data collection lists
    time_sim = []
    power = []

    # Blocking call for a time request at simulation time 0
    initial_time = 60
    logger.debug(f'Requesting initial time {initial_time}')
    t = h.helicsFederateRequestTime(fed, initial_time)
    logger.debug(f'Granted time {t}')

    # Once granted an initial time, send the initial SOCs to the EV
    #   Controller
    for j in range(0, end_count):
        destination_name = str(h.helicsEndpointGetDefaultDestination(endid[j]))
        h.helicsEndpointSendMessageRaw(endid[j], "",
                                       str(currentsoc[j]).encode())  #

    # As long as granted time is in the time range to be simulated...
    while t < total_interval:

        # Time request for the next physical interval to be simulated
        requested_time = (t + update_interval)
        logger.debug(f'Requesting time {requested_time}')
        grantedtime = h.helicsFederateRequestTime(fed, requested_time)
        logger.debug(f'Granted time {grantedtime}')

        t = grantedtime

        for j in range(0, end_count):
Пример #6
0
        logger.info("EV Controller grantedtime = {}".format(grantedtime))

        logger.info("Total Feeder Load is {} + {} j".format(Real_feeder_load, Imag_feeder_load))

        if Real_feeder_load > feeder_limit_upper:
            logger.info("Total Feeder Load is over the Feeder Upper Limit")
            logger.info("Warning ----> Feeder OverLimit --->  Turn off EV")

            if k < endpoint_count:
                end = endid["m{}".format(k)]
                logger.info("endid: {}".format(endid))
                end_name = str(h.helicsEndpointGetName(end))
                logger.info("Sending endpoint name: {}".format(end_name))
                destination_name = end_name.replace(federate_name, distribution_fed_name)
                logger.info(
                    "Endpoint destination: {}".format(h.helicsEndpointGetDefaultDestination(end))
                )
                status = h.helicsEndpointSendMessageRaw(end, "", str("0 + 0 j"))  #
                logger.info("Endpoint sending status: {}".format(status))
                logger.info("Turning off {}".format(end_name))
                k = k + 1
            else:
                logger.info("All EVs are Turned off")

        if Real_feeder_load < feeder_limit_lower:
            logger.info("Total Feeder Load is under the Feeder Lower Limit")
            logger.info("Feeder Can Support EVs ------>  Turn on EV")
            if k > 0:
                k = k - 1
                end = endid["m{}".format(k)]
                end_name = h.helicsEndpointGetName(end)
Пример #7
0
    numLvl1, numLvl2, numLvl3, EVlist = get_new_EV(end_count)
    ##
    ## Step through each time period starting from t = 0
    time_sim = []
    currentsoc = np.random.rand(end_count)
    #t = 0

    # this will be the first message that is "waiting" for the
    # EV Controller

    t = h.helicsFederateRequestTime(fed, 0)
    for j in range(0, len(enddest_EVsoc)):
        end_name = str(h.helicsEndpointGetName(end_EVsoc[j]))
        destination_name = str(
            h.helicsEndpointGetDefaultDestination(end_EVsoc[j]))
        h.helicsEndpointSendMessageRaw(end_EVsoc[j], "", str(currentsoc[j]))  #
        #print('destination: ',destination_name)
        #print('message sent: ',str(currentsoc[j]))
        #print('at time: ',t/3600)
        #print(t/3600,currentsoc[j])

    grantedtime = h.helicsFederateRequestTime(fed, (t + update_interval))
    t = grantedtime

    #logger.info('time: ',t)
    time_since_last_msg = np.zeros(end_count)
    print('time power')
    next_time = []
    while t < total_interval:
Пример #8
0
        logger.info('Total Feeder Load is {} + {} j'.format(
            Real_feeder_load, Imag_feeder_load))

        if (Real_feeder_load > feeder_limit_upper):
            logger.info('Total Feeder Load is over the Feeder Upper Limit')
            logger.info('Warning ----> Feeder OverLimit --->  Turn off EV')

            if (k < endpoint_count):
                end = endid["m{}".format(k)]
                logger.info('endid: {}'.format(endid))
                end_name = str(h.helicsEndpointGetName(end))
                logger.info('Sending endpoint name: {}'.format(end_name))
                destination_name = (end_name.replace(federate_name,
                                                     distribution_fed_name))
                logger.info('Endpoint destination: {}'.format(
                    h.helicsEndpointGetDefaultDestination(end)))
                status = h.helicsEndpointSendMessageRaw(
                    end, '', str('0 + 0 j'))  #
                logger.info('Endpoint sending status: {}'.format(status))
                logger.info('Turning off {}'.format(end_name))
                k = k + 1
            else:
                logger.info('All EVs are Turned off')

        if (Real_feeder_load < feeder_limit_lower):
            logger.info('Total Feeder Load is under the Feeder Lower Limit')
            logger.info('Feeder Can Support EVs ------>  Turn on EV')
            if (k > 0):
                k = k - 1
                end = endid["m{}".format(k)]
                end_name = h.helicsEndpointGetName(end)