def test_send_simulation_status_integration(gridappsd_client: GridAPPSD):

    class Listener:
        def __init__(self):
            self.call_count = 0

        def reset(self):
            self.call_count = 0

        def on_message(self, headers, message):
            print("Message was: {}".format(message))
            self.call_count += 1

    listener = Listener()
    gappsd = gridappsd_client
    assert os.environ['GRIDAPPSD_SIMULATION_ID'] == '1234'
    assert gappsd.get_simulation_id() == "1234"

    log_topic = t.simulation_log_topic(gappsd.get_simulation_id())
    gappsd.subscribe(log_topic, listener)
    gappsd.send_simulation_status("RUNNING",
        "testing the sending and recieving of send_simulation_status().", 
        logging.DEBUG)
    sleep(1)
    assert listener.call_count == 1

    new_log_topic = t.simulation_log_topic("54232")
    gappsd.set_simulation_id(54232)
    gappsd.subscribe(new_log_topic, listener)
    gappsd.send_simulation_status(ProcessStatusEnum.COMPLETE.value, "Complete")
    sleep(1)
    assert listener.call_count == 2
Exemple #2
0
def callback2(headers, message):
    global end_program, SIMULATION_TIME, log_timestamps
    publish_to_topic = simulation_log_topic(simulation_id)
    if type(message) == str:
        message = json.loads(message)
    print(message)
    SIMULATION_TIME = message["timestamp"]
    print(SIMULATION_TIME)
    if 'message' not in message:
        if message['processStatus'] == 'COMPLETE' or \
           message['processStatus']=='CLOSED':
            print('Adding timestamps to logs...')
            f.close()
            #Use pandas to append timestamps
            csv_input = pd.read_csv('csvwritertest.csv')
            print(csv_input)
            print(log_timestamps)
            log_timestamps = pd.to_datetime(log_timestamps, unit='ms')
            csv_input['Timecode'] = log_timestamps
            movecolumn = csv_input.pop("Timecode")
            csv_input.insert(0, "Timecode", movecolumn)
            csv_input.to_csv('csvwritertest.csv', index=False)
            print("Ending Simulation...")
            end_program = True
            quit()
    else:
        print(message)
Exemple #3
0
    def __init__(self, gad: GridAPPSD, sim_id: str, sim_start_ts: int,
                 log_interval=60):
        """Initialize attributes, subscribe to the simulation log.

        :param gad: Initialized gridappsd.GridAPPSD object.
        :param sim_id: Simulation ID of the simulation to track.
        :param sim_start_ts: Simulation start timestamp in seconds since
            the epoch.
        :param log_interval: How many simulation seconds in between
            logging the current simulation time.
        """
        # Setup logging.
        self.log = logging.getLogger(self.__class__.__name__)

        # Simply set the simulation starting time as an attribute.
        self.sim_start_ts = sim_start_ts
        self.log_interval = log_interval

        # Use attributes for tracking the simulation time and current
        # time (as indicated by the most recent message).
        self.sim_time = None
        self.time_step = None
        self.msg_time = None
        self.last_log_time = None

        # Compile a regular expression for extracting the time from the
        # message.
        self.regexp = re.compile('(?:incrementing to )([0-9]+)')

        # Subscribe to the simulation log.
        gad.subscribe(topic=topics.simulation_log_topic(sim_id),
                      callback=self._on_message)

        self.log.info('SimulationClock configured and initialized.')
def test_topic_and_status_set_correctly(logger_mock):

    expected_topic = t.simulation_log_topic(GRIDAPPSD_SIMULATION_ID)

    gapps, log = logger_mock

    log.debug("A message")

    topic, message = gapps.send.call_args.args

    assert expected_topic == topic
    assert "RUNNING" == message['processStatus']
Exemple #5
0
def generate_all_measurements_and_simulation_log_13():
    """Generate the files ALL_MEAS_13, MEASUREMENTS_13, and HEADER_13.
    Note that data for ALL_MEAS_13 comes from the time series database,
    while MEASUREMENTS_13 and HEADER_13 come straight from the platform.
    """

    # Define a helper for writing our measurements and headers to file.
    def meas_header_to_file(header, message):
        _dict_to_json(data=header, fname=HEADER_13)
        _dict_to_json(data=message, fname=MEASUREMENTS_13)

    # Initialize list for placing the simulation logs.
    logs = []

    # Define helper function for collecting our log messages.
    def add_log_entry(header, message):
        logs.append({'header': header, 'message': message})

    # Get a platform manager, start the simulation.
    platform = gridappsd_platform.PlatformManager()
    # For now, we need a 2nd platform manager since it seems subscribing
    # to the simulation logs interferes with the Simulation object under
    # the hood.
    platform2 = gridappsd_platform.PlatformManager()

    sim_id = platform.run_simulation(feeder_id=FEEDER_MRID_13,
                                     start_time=MEAS_13_START,
                                     duration=MEAS_13_DURATION,
                                     realtime=False)

    # Subscribe to simulation logs.
    platform2.gad.subscribe(topic=topics.simulation_log_topic(sim_id),
                            callback=add_log_entry)

    # Subscribe to simulation output so we can write the header +
    # message to file. Note this is not particularly efficient as the
    # files will be overwritten a few times before the simulation ends.
    # Who cares? Not me :) Just gotta make rapid progress.
    platform.gad.subscribe(topic=topics.simulation_output_topic(sim_id),
                           callback=meas_header_to_file)

    # Wait for simulation completion.
    platform.wait_for_simulation()

    # Get the measurements.
    # noinspection PyProtectedMember
    data = platform._query_simulation_output(simulation_id=sim_id)

    # Write to file.
    _dict_to_json(fname=ALL_MEAS_13, data=data)

    _dict_to_json(data=logs, fname=SIMULATION_LOG)
def test_topic_and_status_set_correctly():

    sim_id = "543"
    application_id = "wicked_good_app_id"
    mock_gapps = init_gapps_mock(
        simulation_id=sim_id,
        application_id=application_id,
        process_status=ProcessStatusEnum.RUNNING.value)

    expected_topic = t.simulation_log_topic(sim_id)

    log = Logger(mock_gapps)

    log.debug("A message")

    # During the call to debug we expect the send function to be
    # called on the mock object.  Grab the arguments and then
    # make sure that they are what we expect.
    topic, message = mock_gapps.send.call_args.args

    assert message['source'] == application_id
    assert topic == expected_topic
    assert message['processStatus'] == "RUNNING"
Exemple #7
0
def _startTest(username,
               password,
               gossServer='localhost',
               stompPort='61613',
               simulationID=1234,
               rulePort=5000,
               topic="input"):
    req_template = {
        "power_system_config": {
            "SubGeographicalRegion_name":
            "_1CD7D2EE-3C91-3248-5662-A43EFEFAC224",
            "GeographicalRegion_name": "_24809814-4EC6-29D2-B509-7F8BFB646437",
            "Line_name": "_C1C3E687-6FFD-C753-582B-632A27E28507"
        },
        "simulation_config": {
            "power_flow_solver_method": "NR",
            "duration": 120,
            "simulation_name": "ieee123",
            "simulator": "GridLAB-D",
            "start_time": 1248156000,
            "run_realtime": True,
            "timestep_frequency": "1000",
            "timestep_increment": "1000",
            "model_creation_config": {
                "load_scaling_factor": 1.0,
                "triplex": "y",
                "encoding": "u",
                "system_frequency": 60,
                "voltage_multiplier": 1.0,
                "power_unit_conversion": 1.0,
                "unique_names": "y",
                "schedule_name": "ieeezipload",
                "z_fraction": "0",
                "i_fraction": "0",
                "p_fraction": "1",
                "randomize_zipload_fractions": False,
                "use_houses": False
            },
            "simulation_broker_port": 52798,
            "simulation_broker_location": "127.0.0.1"
        },
        "application_config": {
            "applications": [{
                "name": "der_dispatch_app",
                "config_string": "{}"
            }]
        },
        "simulation_request_type": "NEW",
        "test_config": {
            "events": []
        }
    }

    # _B221C5F6-E08A-BC62-91DE-ADC1E2F8FF96
    sw3_event = {
        "message": {
            "forward_differences": [{
                "object": "_B221C5F6-E08A-BC62-91DE-ADC1E2F8FF96",
                "attribute": "Switch.open",
                "value": 1
            }],
            "reverse_differences": [{
                "object": "_B221C5F6-E08A-BC62-91DE-ADC1E2F8FF96",
                "attribute": "Switch.open",
                "value": 0
            }]
        },
        "event_type": "ScheduledCommandEvent",
        "occuredDateTime": 1374258660 + (4 * 60),
        "stopDateTime": 1374258660 + (8 * 60)
    }
    pv_84_90_event = {
        "allOutputOutage":
        False,
        "allInputOutage":
        False,
        "inputOutageList": [
            {
                "objectMRID": "_233D4DC1-66EA-DF3C-D859-D10438ECCBDF",
                "attribute": "PowerElectronicsConnection.p"
            },
            {
                "objectMRID": "_233D4DC1-66EA-DF3C-D859-D10438ECCBDF",
                "attribute": "PowerElectronicsConnection.q"
            },
            {
                "objectMRID": "_60E702BC-A8E7-6AB8-F5EB-D038283E4D3E",
                "attribute": "PowerElectronicsConnection.p"
            },
            {
                "objectMRID": "_60E702BC-A8E7-6AB8-F5EB-D038283E4D3E",
                "attribute": "PowerElectronicsConnection.q"
            },
        ],
        "outputOutageList": [
            '_a5107987-1609-47b2-8f5b-f91f99658390',
            '_2c4e0cb2-4bf0-4a2f-be94-83ee9b87d1e5'
        ],
        "event_type":
        "CommOutage",
        "occuredDateTime":
        1374510600 + (5 * 60),
        "stopDateTime":
        1374510600 + (10 * 60)
    }

    pv_84_90_event = {
        "allOutputOutage":
        False,
        "allInputOutage":
        False,
        "inputOutageList": [
            {
                "objectMRID": "_EAE0584D-6B67-2F23-FC02-E3F2C8C6A48D",
                "attribute": "PowerElectronicsConnection.p"
            },
            {
                "objectMRID": "_EAE0584D-6B67-2F23-FC02-E3F2C8C6A48D",
                "attribute": "PowerElectronicsConnection.q"
            },
            {
                "objectMRID": "_73E7B579-37DB-B7F2-EBC6-D083E8BBA1F3",
                "attribute": "PowerElectronicsConnection.p"
            },
            {
                "objectMRID": "_73E7B579-37DB-B7F2-EBC6-D083E8BBA1F3",
                "attribute": "PowerElectronicsConnection.q"
            },
        ],
        "outputOutageList": [
            '_b8442bbd-4d3e-4b2e-884e-96639bb207bc',
            '_b005322e-7dba-48d3-b6ce-f6fe57c4dd61'
        ],
        "event_type":
        "CommOutage",
        "occuredDateTime":
        1374510600 + (5 * 60),
        "stopDateTime":
        1374510600 + (10 * 60)
    }

    # {"applications": [{"name": "der_dispatch_app", "config_string": ""}]}
    req_template['simulation_config']['model_creation_config'][
        'load_scaling_factor'] = 1
    req_template['simulation_config']['run_realtime'] = False
    req_template['simulation_config']['duration'] = 60 * 15

    req_template['simulation_config']['start_time'] = 1538510000
    req_template['simulation_config'][
        'start_time'] = 1374498000  # 7/22/2013 What I was doing
    req_template['simulation_config'][
        'start_time'] = 1374510600  # GMT: Monday, July 22, 2013 4:30:00 PM
    req_template['simulation_config']['start_time'] = 1374483600  # 3:00 am
    # req_template['simulation_config']['start_time'] = 1374519600  # (GMT): Monday, July 22, 2013 7:00:00 PM
    req_template['simulation_config'][
        'start_time'] = 1374530400  # (GMT): Monday, July 22, 2013 10:00:00 PM Cause
    req_template['simulation_config'][
        'start_time'] = 1374240600  # July 19 13:30 AM GMT / 7:30 AM MST
    # req_template['simulation_config']['start_time'] = 1374249600  #  July 19, 2013 4:00:00 PM
    req_template['simulation_config'][
        'start_time'] = 1374258600  # July 19, 2013 6:30:00 PM  - 2013-07-19 18:30:00 / 12:30 PM MST  # MAX PV!!!
    req_template['simulation_config'][
        'start_time'] = 1248156000  # Tuesday, July 21, 2009 6:00:00 AM
    req_template['simulation_config'][
        'start_time'] = 1248192000  # Tuesday, July 21, 2009 4:00:00 PM / 10:00:00 AM
    req_template['simulation_config']['start_time'] = timegm(
        strptime('2009-07-21 15:03:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))

    req_template['simulation_config']['start_time'] = timegm(
        strptime('2009-07-21 00:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 08:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 12:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    req_template['simulation_config']['start_time'] = timegm(
        strptime('2019-07-22 12:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    req_template['simulation_config']['start_time'] = timegm(
        strptime('2019-07-23 14:50:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))

    pv_84_90_event["occuredDateTime"] = req_template['simulation_config'][
        'start_time'] + (2 * 60)
    pv_84_90_event["stopDateTime"] = req_template['simulation_config'][
        'start_time'] + (4 * 60)
    req_template["test_config"]["events"].append(pv_84_90_event)

    # sw3_event
    sw3_event["occuredDateTime"] = req_template['simulation_config'][
        'start_time'] + (7 * 60)
    sw3_event["stopDateTime"] = req_template['simulation_config'][
        'start_time'] + (9 * 60)
    # req_template["test_config"]["events"].append(sw3_event)

    app_config = {
        'OPF': 0,
        'run_freq': 60,
        'run_on_host': True,
        'run_realtime': req_template['simulation_config']['run_realtime'],
        'historical_run': 875592221
    }  #30
    app_config['historical_run'] = 0  # Turn Off
    req_template["application_config"]["applications"] = [{
        "name":
        "grid_forecasting_app",
        "config_string":
        json.dumps(app_config)
    }]

    # GMT: Tuesday, October 2, 2018 4:50:00 PM
    # Your time zone: Tuesday, October 2, 2018 10:50:00 AM GMT-06:00 DST
    req_template['power_system_config'][
        'Line_name'] = '_C1C3E687-6FFD-C753-582B-632A27E28507'
    req_template['power_system_config'][
        'Line_name'] = '_E407CBB6-8C8D-9BC9-589C-AB83FBF0826D'  # Mine 123pv
    # req_template['power_system_config']['Line_name'] = '_EBDB5A4A-543C-9025-243E-8CAD24307380'  # 123 with reg

    req_template['power_system_config'][
        'Line_name'] = '_DA00D94F-4683-FD19-15D9-8FF002220115'  # mine with house
    # req_template['power_system_config']['Line_name'] = '_88B3A3D8-51CD-7466-5E29-B692F21723CB' # Mine with feet conv
    # req_template['power_system_config']['Line_name'] = '_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62'  # 13
    # req_template['power_system_config']['Line_name'] = '_C1C3E687-6FFD-C753-582B-632A27E28507'  # 123
    # req_template['power_system_config']['Line_name'] = '_AAE94E4A-2465-6F5E-37B1-3E72183A4E44'  # New 8500
    # req_template['power_system_config']['Line_name'] = '_AAE94E4A-Jeff'

    # req_template['power_system_config']['Line_name'] = '_4F76A5F9-271D-9EB8-5E31-AA362D86F2C3'
    req_template["application_config"]["applications"][0][
        'name'] = 'grid_forecasting_app'

    simCfg13pv = json.dumps(req_template)
    print(simCfg13pv)
    # exit(0)
    goss = GOSS()
    goss.connect()

    simulation_id = goss.get_response(goss_sim, simCfg13pv, timeout=200)
    simulation_id = int(simulation_id['simulationId'])
    print(simulation_id)
    print('sent simulation request')
    time.sleep(1)

    if app_config['run_on_host']:
        listening_to_topic = simulation_output_topic(simulation_id)
        log_topic = simulation_log_topic(simulation_id)
        model_mrid = req_template['power_system_config']['Line_name']
        start_time = req_template['simulation_config']['start_time']
        app_configs = req_template["application_config"]["applications"]
        app_config = [
            json.loads(app['config_string']) for app in app_configs
            if app['name'] == 'grid_forecasting_app'
        ][0]

        gapps = GridAPPSD(simulation_id)
        load_scale = req_template['simulation_config'][
            'model_creation_config']['load_scaling_factor']
        app = Grid_Forecast(simulation_id, gapps, model_mrid, start_time,
                            app_config, load_scale)
        app.setup()
        gapps.subscribe(listening_to_topic, app)
        # gapps.subscribe(log_topic, app)

        while True:
            time.sleep(0.1)
Exemple #8
0
#Start the simulation....

gapps_sim = GridAPPSD()
simulation = Simulation(gapps_sim, run_config_13)
simulation.start_simulation()

simulation_id = simulation.simulation_id
print(simulation_id)

#Test the callback function
sim_output_topic = simulation_output_topic(simulation_id)
f = open('csvwritertest.csv', 'w')

gapps.subscribe(sim_output_topic, callback)
sim_log_topic = simulation_log_topic(simulation_id)
gapps.subscribe(sim_log_topic, callback2)


def _main():
    global end_program
    print('test')
    while not end_program:
        time.sleep(0.1)
    if end_program:
        f.close()
        print('bye')
        quit()


if __name__ == "__main__":
Exemple #9
0
    # print("Alka3")
    #print(gapps.query_data( query,database_type=POWERGRID_MODEL, timeout=30 ))
    # # with open("power2.json", 'r') as g:
    # #     print(gapps.get_response(request_topic, json.load(g), timeout=30))
    # print("Alka4")
    # print(gapps.query_object_dictionary('_0f6f3735-b297-46aa-8861-547d3cd0dee9', object_type='http://iec.ch/TC57/CIM100#ACLineSegment', object_id=None))

    # with open("./weather_data.json", 'r') as g:
    #     print(gapps.get_response(t.TIMESERIES, json.load(g), timeout=30))
    #
    # with open("./query.json", 'r') as f:
    #     query = json.load(f)
    #     query["queryFilter"]["simulation_id"] = opts.simulation_id
    #     print(query["queryFilter"]["simulation_id"])
    #     print("Alka")
    #     print(gapps.get_response(t.TIMESERIES, query, timeout=30))

    # For testing Alarms topic uncomment the below lines
    # alarms_topic = t.service_output_topic('gridappsd-alarms', simulation_id)
    # print(alarms_topic)
    # print(simulation_id)
    # print("Alka")
    # print(gapps.subscribe(alarms_topic, Subscribe.on_message))
    # print(gapps.query_model_names(model_id=None))

    #For testing the  Logging API uncomment the below line
    print(gapps.subscribe(simulation_log_topic(simulation_id), Subscribe.on_message))

    while True:
        time.sleep(30)
def start(log_file, feeder_mrid, model_api_topic, simulation_id):
    global logfile
    logfile = log_file

    print(
        "\nAC_LINE_AMPACITY starting!!!----------------------------------------------------"
    )
    print(
        "\nAC_LINE_AMPACITY starting!!!----------------------------------------------------",
        file=logfile)

    SPARQLManager = getattr(importlib.import_module('shared.sparql'),
                            'SPARQLManager')

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    # AC Line segement rating check
    global df_acline_measA
    df_acline_measA = sparql_mgr.acline_measurements(logfile)
    # Combine measurement mrids for 'A' and rating together
    df_acline_rating = sparql_mgr.acline_rating_query()
    if df_acline_measA is not None:
        print('AC_LINE_AMPACITY ACLineSegment measurements obtained',
              flush=True)
        print('AC_LINE_AMPACITY ACLineSegment measurements obtained',
              file=logfile)
        df_acline_measA = df_acline_measA.assign(
            flow=np.zeros(df_acline_measA.shape[0]))
        for r in df_acline_rating.itertuples(index=False):
            index = df_acline_measA.index[df_acline_measA['eqname'] ==
                                          r.eqname].tolist()
            rating = r.val
            for k in index:
                df_acline_measA.loc[df_acline_measA.index == k,
                                    'rating'] = rating
        print('AC_LINE_AMPACITY ACLineSegment rating obtained', flush=True)
        print('AC_LINE_AMPACITY ACLineSegment rating obtained', file=logfile)
        print('AC_LINE_AMPACITY df_acline_measA: ' + str(df_acline_measA),
              flush=True)
        print('AC_LINE_AMPACITY df_acline_measA: ' + str(df_acline_measA),
              file=logfile)
    else:
        return

    sim_output_topic = simulation_output_topic(simulation_id)
    sim_log_topic = simulation_log_topic(simulation_id)
    print('AC_LINE_AMPACITY simulation output topic from function: ' +
          sim_output_topic,
          flush=True)
    print('AC_LINE_AMPACITY simulation output topic from function: ' +
          sim_output_topic,
          file=logfile)
    print('AC_LINE_AMPACITY simulation log topic from function: ' +
          sim_log_topic,
          flush=True)
    print('AC_LINE_AMPACITY simulation log topic from function: ' +
          sim_log_topic,
          file=logfile)

    gapps.subscribe(topic=sim_output_topic, callback=on_message)
    gapps.subscribe(topic=sim_log_topic, callback=on_message)
    print(
        'AC_LINE_AMPACITY subscribed to both output and log topics, waiting for messages',
        flush=True)
    print(
        'AC_LINE_AMPACITY subscribed to both output and log topics, waiting for messages',
        file=logfile)

    global exit_flag
    exit_flag = False

    while not exit_flag:
        time.sleep(0.1)
def test_simulation_output(sim_config_file, sim_result_file):
    sim_config_file = os.path.join(
        os.path.dirname(__file__),
        f"simulation_config_files/{sim_config_file}")
    sim_result_file = os.path.join(
        os.path.dirname(__file__),
        f"simulation_baseline_files/{sim_result_file}")
    assert os.path.exists(
        sim_config_file
    ), f"File {sim_config_file} must exist to run simulation test"
    # assert os.path.exists(sim_result_file), f"File {sim_result_file} must exist to run simulation test"

    with startup_containers():
        # Allow proven to come up
        sleep(30)
        starttime = int(time())

        with gappsd() as gapps:
            os.makedirs("/tmp/output", exist_ok=True)
            with open("/tmp/output/simulation.output", 'w') as outfile:
                LOGGER.info('Configuring simulation')
                sim_complete = False
                rcvd_measurement = False
                rcvd_first_measurement = 0
                are_we_paused = False

                with open(sim_config_file) as fp:
                    LOGGER.info('Reading config')
                    run_config = json.load(fp)
                    run_config["simulation_config"]["start_time"] = str(
                        starttime)

                sim = Simulation(gapps, run_config)

                def onmeasurement(sim, timestep, measurements):
                    LOGGER.info('Measurement received at %s', timestep)
                    nonlocal rcvd_measurement
                    nonlocal rcvd_first_measurement
                    nonlocal are_we_paused

                    if not are_we_paused and not rcvd_first_measurement:
                        LOGGER.debug("Pausing sim now")
                        sim.pause()
                        are_we_paused = True
                        LOGGER.debug(f"ARWEPAUSED {are_we_paused}")
                        # Setting up so if we get another measurement wheil we
                        # are paused we know it
                        rcvd_measurement = False

                    if not rcvd_measurement:
                        print(f"A measurement happened at {timestep}")
                        # outfile.write(f"{timestep}|{json.dumps(measurements)}\n")
                        data = {"data": measurements}
                        outfile.write(json.dumps(data))
                        rcvd_measurement = True

                    else:
                        rcvd_measurement = True
                    rcvd_first_measurement = True

                # sleep here until rcvd_measuremnt = True again

                def ontimestep(sim, timestep):
                    print("Timestamp: {}".format(timestep))

                def onfinishsimulation(sim):
                    nonlocal sim_complete
                    sim_complete = True
                    LOGGER.info('Simulation Complete')

                LOGGER.info(f"Start time is {starttime}")
                LOGGER.info('Loading config')

                # tm: typo in add_onmesurement
                LOGGER.info('sim.add_onmesurement_callback')

                LOGGER.info('Starting sim')
                sim.start_simulation()
                print(sim.simulation_id)
                sim.add_onmesurement_callback(onmeasurement)
                sim.add_ontimestep_callback(ontimestep)
                gapps.subscribe(t.simulation_log_topic(sim.simulation_id),
                                on_message)
                sim.add_oncomplete_callback(onfinishsimulation)
                LOGGER.info('sim.add_oncomplete_callback')
                secs = 0
                while secs < 30:
                    LOGGER.info(f"Sleep {secs}")
                    # we have to wait until the first measurement is called before
                    # the are_we_paused could  have a chance of being set
                    secs += 1
                    sleep(1)

                paused_seconds = 0
                while are_we_paused:
                    LOGGER.info(f"PAUSED {paused_seconds}")
                    paused_seconds += 1

                    # s
                    if paused_seconds > 30:
                        LOGGER.info('Resuming simulation')
                        sim.resume()
                        LOGGER.info('Resumed simulation')
                        are_we_paused = False
                        break
                    sleep(1)

                assert not are_we_paused, "We should have came out of the paused_seconds > 30"

                while not sim_complete:
                    LOGGER.info('Sleeping')
                    sleep(5)
Exemple #12
0
    def onstart(self, sender, **kwargs):
        """
        Subscribe to VOLTTRON topics on VOLTTRON message bus.
        Register config parameters with Gridappsd.
        Start Gridappsd simulation.
        """
        # subscribe to the VOLTTRON topics if given.
        if self.volttron_subscriptions is not None:
            for sub in self.volttron_subscriptions:
                _log.info('Subscribing to {}'.format(sub))
                self.vip.pubsub.subscribe(
                    peer='pubsub',
                    prefix=sub,
                    callback=self.on_receive_publisher_message)

        # Exit if GridAPPSD isn't installed in the current environment.
        if not self.Gridappsd_sim.is_sim_installed():
            _log.error(
                "GridAPPSD module is unavailable please add it to the python environment."
            )
            self.core.stop()
            return

        try:
            # Register events with GridAPPSD
            # There are 4 event callbacks that GridAPPSD provides to monitor the status
            # - onstart, ontimestep, onmesurement, oncomplete
            # This example shows how to register with GridAPPSD simulation to get
            # event notifications
            event_callbacks = {
                'MEASUREMENT': self.onmeasurement,
                "TIMESTEP": self.ontimestep,
                "FINISH": self.onfinishsimulation
            }
            self.Gridappsd_sim.register_event_callbacks(event_callbacks)

            # Register the config file with GridAPPS-D
            self.Gridappsd_sim.register_inputs(self.config, self.do_work)
            # Start the simulation
            self.Gridappsd_sim.start_simulation()

            # Waiting for simulation to start
            i = 1
            while not self.Gridappsd_sim.sim_id and i < 20:
                _log.debug(f"waiting for simulation to start {i}")
                gevent.sleep(1)
                i += 1

            # Subscribe to GridAPPSD log messages
            if self.Gridappsd_sim.sim_id:
                self.Gridappsd_sim.gridappsd.subscribe(
                    t.simulation_log_topic(self.Gridappsd_sim.sim_id),
                    self.on_message)
                self.sim_running = True
            else:
                self.sim_running = False
                _log.debug("Simulation did not start")
        except stomp.exception.NotConnectedException as ex:
            _log.error("Unable to connect to GridAPPSD: {}".format(ex))
            _log.error("Exiting!!")
            self.core.stop()
        except ValueError as ex:
            _log.error(
                "Unable to register inputs with GridAPPSD: {}".format(ex))
            self.core.stop()
            return
Exemple #13
0
def _startTest(username,password,gossServer='localhost',stompPort='61613', simulationID=1234, rulePort=5000, topic="input"):

    req_template = {"power_system_config": {"SubGeographicalRegion_name": "_1CD7D2EE-3C91-3248-5662-A43EFEFAC224",
                                            "GeographicalRegion_name": "_24809814-4EC6-29D2-B509-7F8BFB646437",
                                            "Line_name": "_C1C3E687-6FFD-C753-582B-632A27E28507"},
                    "simulation_config": {"power_flow_solver_method": "NR",
                                          "duration": 120,
                                          "simulation_name": "ieee123",
                                          "simulator": "GridLAB-D",
                                          "start_time": 1248156000,
                                          "run_realtime": True,
                                          "timestep_frequency": "1000",
                                          "timestep_increment": "1000",
                                          "model_creation_config": {"load_scaling_factor": 1.0, "triplex": "y",
                                                                    "encoding": "u", "system_frequency": 60,
                                                                    "voltage_multiplier": 1.0,
                                                                    "power_unit_conversion": 1.0, "unique_names": "y",
                                                                    "schedule_name": "ieeezipload", "z_fraction": "0",
                                                                    "i_fraction": "1", "p_fraction": "0",
                                                                    "randomize_zipload_fractions": False,
                                                                    "use_houses": False},
                                          "simulation_broker_port": 52798, "simulation_broker_location": "127.0.0.1"},
                    "application_config": {"applications": [{"name": "der_dispatch_app", "config_string": "{}"}]},
                    "simulation_request_type": "NEW", "test_config": {"events": []}}
 #138236b0
    xxx =  {"power_system_config":
                {"GeographicalRegion_name":"_73C512BD-7249-4F50-50DA-D93849B89C43",
                 "SubGeographicalRegion_name":"_A1170111-942A-6ABD-D325-C64886DC4D7D",
                 "Line_name":"_AAE94E4A-2465-6F5E-37B1-3E72183A4E44"},
                "application_config":{"applications":[]},
            "simulation_config":{
                "start_time":"1572040521",
                "duration":"120",
                "simulator":"GridLAB-D",
                "timestep_frequency":"1000",
                "timestep_increment":"1000",
                "run_realtime":True,
                "simulation_name":"test9500new",
                "power_flow_solver_method":"NR",
                "model_creation_config":{"load_scaling_factor":"1",
                                         "schedule_name":"ieeezipload",
                                         "z_fraction":"0",
                                         "i_fraction":"1",
                                         "p_fraction":"0",
                                         "randomize_zipload_fractions":False,
                                         "use_houses":False}
            },
            "test_config":{"events":[],"appId":""},"service_configs":[]}

    sw5_event = {
            "message": {
                "forward_differences": [
                    {
                        "object": "_60208A8D-E4EA-DA37-C758-428756C84F0D",
                        "attribute": "Switch.open",
                        "value": 1
                    }
                ],
                "reverse_differences": [
                    {
                        "object": "_60208A8D-E4EA-DA37-C758-428756C84F0D",
                        "attribute": "Switch.open",
                        "value": 0
                    }
                ]
            },
            "event_type": "ScheduledCommandEvent",
            "occuredDateTime": 1374510750,
            "stopDateTime": 1374510960
        }

    sw3_event = {
            "message": {
                "forward_differences": [
                    {
                        "object": "_4AA2369A-BF4B-F677-1229-CF5FB9A3A07E",
                        "attribute": "Switch.open",
                        "value": 1
                    }
                ],
                "reverse_differences": [
                    {
                        "object": "_4AA2369A-BF4B-F677-1229-CF5FB9A3A07E",
                        "attribute": "Switch.open",
                        "value": 0
                    }
                ]
            },
            "event_type": "ScheduledCommandEvent",
            "occuredDateTime": 1374258660 + (4*60),
            "stopDateTime": 1374258660 + (8*60)
        }
    event_l114 =  {"PhaseConnectedFaultKind": "lineToLineToGround",
                "FaultImpedance": {
                    "xGround": 0.36,
                    "rGround": 0.36,
                    "xLineToLine": 0.36,
                    "rLineToLine": 0.36
                },
                "ObjectMRID": ["_81CF3E64-ABA9-EF74-EE81-B86439ED61D5"], #  _ACA88F2A-96E3-B942-B09B-274CDD213CA6 PV no switches
                "phases": "ABC",
                "event_type": "Fault",
                "occuredDateTime": 1374258600 + (4*60),
                "stopDateTime":  1374258600 + (8*60)
    }
    event_1_v2019_10_0 = {
        "message": {
            "forward_differences": [
                {
                    "object": "_1B6A5DFD-9ADA-404A-83DF-C9AC89D9323C", # l9191_48332_sw
                    "attribute": "Switch.open",
                    "value": 1
                }
            ],
            "reverse_differences": [
                {
                    "object": "_1B6A5DFD-9ADA-404A-83DF-C9AC89D9323C",
                    "attribute": "Switch.open",
                    "value": 0
                }
            ]
        },
        "event_type": "ScheduledCommandEvent",
        "occuredDateTime": 1248174120,  # 2009-07-21 11:02:00 AM
        "stopDateTime": 1248174240      # 2009-07-21 11:04:00 AM
    }
    event_1 = {'message': {'forward_differences': [{'object': '_302E3119-B3ED-46A1-87D5-EBC8496357DF', 'attribute': 'Switch.open', 'value': 1}],
                           'reverse_differences': [{'object': '_302E3119-B3ED-46A1-87D5-EBC8496357DF', 'attribute': 'Switch.open', 'value': 0}]},
               'event_type': 'ScheduledCommandEvent',
               'occuredDateTime': 1248177660,
               'stopDateTime': 1248177780}

    restore_event_1 = {
      "message": {
        "forward_differences": [
          {
            "object": "_D287FFEF-4813-44C4-8F30-CBF836D58DF7",
            "attribute": "Switch.open",
            "value": 1
          },
          {
            "object": "_E976600E-B276-4B9A-A65F-DEEF65A7F080",
            "attribute": "Switch.open",
            "value": 1
          },
          {
            "object": "_57574CED-ACDC-4B14-AD36-D8BE9B6DD42C",
            "attribute": "Switch.open",
            "value": 0
          },
          {
            "object": "_37619299-61AC-40B4-BB3E-6B7C2A5D5719",
            "attribute": "Switch.open",
            "value": 0
          }
        ],
        "reverse_differences": [
          {
            "object": "_D287FFEF-4813-44C4-8F30-CBF836D58DF7",
            "attribute": "Switch.open",
            "value": 0
          },
          {
            "object": "_E976600E-B276-4B9A-A65F-DEEF65A7F080",
            "attribute": "Switch.open",
            "value": 0
          },
          {
            "object": "_57574CED-ACDC-4B14-AD36-D8BE9B6DD42C",
            "attribute": "Switch.open",
            "value": 1
          },
          {
            "object": "_37619299-61AC-40B4-BB3E-6B7C2A5D5719",
            "attribute": "Switch.open",
            "value": 1
          }
        ]
      },
      "event_type": "ScheduledCommandEvent",
      "occuredDateTime": 1248177690.0,
      "stopDateTime": 1248177810.0
    }

    event_3_v2019_10_0 = {"message": {
        "forward_differences": [
          {
            "object": "_2455DC96-1030-44F6-81E9-000A3702E157",
            "attribute": "Switch.open",
            "value": 1
          },
          {
            "object": "_A7AAF230-5237-4ABC-9F0B-845DD245CC1E",
            "attribute": "Switch.open",
            "value": 1
          }
        ],
        "reverse_differences": [
          {
            "object": "_2455DC96-1030-44F6-81E9-000A3702E157",
            "attribute": "Switch.open",
            "value": 0
          },
          {
            "object": "_A7AAF230-5237-4ABC-9F0B-845DD245CC1E",
            "attribute": "Switch.open",
            "value": 0
          }
        ]
      },
      "event_type": "ScheduledCommandEvent",
      "occuredDateTime": 1563796860,
      "stopDateTime": 1563796980
    }



    # 2009-07-21 05:00:00 AM
    # ##### event_1
    # Line LINE.LN5593236-6
    # node m1047515

    # "dg_84": "_233D4DC1-66EA-DF3C-D859-D10438ECCBDF", "dg_90": "_60E702BC-A8E7-6AB8-F5EB-D038283E4D3E"
    # Meas "_facde6ab-95e2-471b-b151-1b7125d863f0","_888e15c8-380d-4dcf-9876-ccf8949d45b1"

    # sx2991914c.1
    # new Line.2002200004991174_sw phases=3 bus1=d6290228-6_int.1.2.3 bus2=q16642.1.2.3 switch=y // CIM LoadBreakSwitch
    # ~ normamps=400.00 emergamps=600.00
    #   close Line.2002200004991174_sw 1
    # {'command': 'update', 'input': {'simulation_id': 966953393, 'message': {'timestamp': 1571850450, 'difference_mrid': 'caf85954-d594-42ec-b3d1-644a32941a4a', 'reverse_differences': [{'object': '_CB845255-3CD8-4E25-9B48-3CB74EE59F63', 'attribute': 'Switch.open', 'value': 1}], 'forward_differences': [{'object': '_CB845255-3CD8-4E25-9B48-3CB74EE59F63', 'attribute': 'Switch.open', 'value': 0}]}}}

    pv_84_90_event = {
        "allOutputOutage": False,
        "allInputOutage": False,
        "inputOutageList": [{"objectMRID": "_233D4DC1-66EA-DF3C-D859-D10438ECCBDF", "attribute": "PowerElectronicsConnection.p"},
                            {"objectMRID": "_233D4DC1-66EA-DF3C-D859-D10438ECCBDF", "attribute": "PowerElectronicsConnection.q"},
                            {"objectMRID": "_60E702BC-A8E7-6AB8-F5EB-D038283E4D3E", "attribute": "PowerElectronicsConnection.p"},
                            {"objectMRID": "_60E702BC-A8E7-6AB8-F5EB-D038283E4D3E", "attribute": "PowerElectronicsConnection.q"},
                            ],
        "outputOutageList": ['_a5107987-1609-47b2-8f5b-f91f99658390', '_2c4e0cb2-4bf0-4a2f-be94-83ee9b87d1e5'],
        "event_type": "CommOutage",
        "occuredDateTime": 1374510600 + (5*60),
        "stopDateTime": 1374510600 + (10*60)
    }

    # _EAE0584D-6B67-2F23-FC02-E3F2C8C6A48D"
    # _73E7B579-37DB-B7F2-EBC6-D083E8BBA1F3
    # 104.3 dg_84 _b8442bbd-4d3e-4b2e-884e-96639bb207bc
    # 113.1 dg_90 _b005322e-7dba-48d3-b6ce-f6fe57c4dd61

    pv_84_90_event = {
        "allOutputOutage": False,
        "allInputOutage": False,
        "inputOutageList": [{"objectMRID": "_EAE0584D-6B67-2F23-FC02-E3F2C8C6A48D", "attribute": "PowerElectronicsConnection.p"},
                            {"objectMRID": "_EAE0584D-6B67-2F23-FC02-E3F2C8C6A48D", "attribute": "PowerElectronicsConnection.q"},
                            {"objectMRID": "_73E7B579-37DB-B7F2-EBC6-D083E8BBA1F3", "attribute": "PowerElectronicsConnection.p"},
                            {"objectMRID": "_73E7B579-37DB-B7F2-EBC6-D083E8BBA1F3", "attribute": "PowerElectronicsConnection.q"},
                            ],
        "outputOutageList": ['_b8442bbd-4d3e-4b2e-884e-96639bb207bc', '_b005322e-7dba-48d3-b6ce-f6fe57c4dd61'],
        "event_type": "CommOutage",
        "occuredDateTime": 1374510600 + (5*60),
        "stopDateTime": 1374510600 + (10*60)
    }


    # {"applications": [{"name": "der_dispatch_app", "config_string": ""}]}
    req_template['simulation_config']['model_creation_config']['load_scaling_factor'] = 1
    req_template['simulation_config']['run_realtime'] = False
    req_template['simulation_config']['duration'] = 60 * 60 * 1
    req_template['simulation_config']['duration'] = 60 * 20

    req_template['simulation_config']['start_time'] = 1538510000
    req_template['simulation_config']['start_time'] = 1374498000  # GMT: Monday, July 22, 2013 1:00:00 PM What I was doing
    req_template['simulation_config']['start_time'] = 1374510600  # GMT: Monday, July 22, 2013 4:30:00 PM MST 10:30:00 AM
    # req_template['simulation_config']['start_time'] = 1374517800   # GMT: Monday, July 22, 2013 6:30:00 PM
    # req_template['simulation_config']['start_time'] = 1374510720  # GMT: Monday, July 22, 2013 4:30:00 PM PLUS 2 minutes!!
    # July 22, 2013 4:32:00 GMT
    # July 22, 2013 10:32:00 2013-07-22 10:32:00
    # req_template['simulation_config']['start_time'] = 1374514200  # GMT: Monday, July 22, 2013 5:30:00 PM

    # req_template['simulation_config']['start_time'] = 1374519600  # (GMT): Monday, July 22, 2013 7:00:00 PM
    # req_template['simulation_config']['start_time'] = 1374530400  # (GMT): Monday, July 22, 2013 10:00:00 PM Cause
    # req_template['simulation_config']['start_time'] = 1374454800  # (GMT): Monday, July 22, 2013 1:00:00 AM
    # req_template['simulation_config']['start_time'] = 1374411600  # 7/21/2013 7AM
    req_template['simulation_config']['start_time'] = 1374256800  # (GMT): Friday, July 19, 2013 6:00:00 PM
    req_template['simulation_config']['start_time'] = 1374217200  # July 19 07:00 AM GMT / 1:00 AM MST
    req_template['simulation_config']['start_time'] = 1374228000  # July 19 10:00 AM GMT / 4:00 AM MST
    req_template['simulation_config']['start_time'] = 1374233400  # July 19 11:30 AM GMT / 5:30 AM MST
    req_template['simulation_config']['start_time'] = 1374240600  # July 19 13:30 AM GMT / 7:30 AM MST
    req_template['simulation_config']['start_time'] = 1374213600  # July 19 06:00 AM GMT / 00:00 AM MST
    req_template['simulation_config']['start_time'] = 1374235200  # July 19 12:00 PM GMT / 06:00 AM MST
    req_template['simulation_config']['start_time'] = 1248156000  # Tuesday, July 21, 2009 6:00:00 AM
    req_template['simulation_config']['start_time'] = 1248192000  # Tuesday, July 21, 2009 4:00:00 PM / 10:00:00 AM
    req_template['simulation_config']['start_time'] = 1248199200  # Tuesday, July 21, 2009 6:00:00 PM / 12:00:00 PM
    req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 13:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 12:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    req_template['simulation_config']['start_time'] = timegm(strptime('2019-07-22 11:01:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))

    req_template['simulation_config']['start_time'] = timegm(strptime('2019-07-23 14:50:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    req_template['simulation_config']['start_time'] = timegm(strptime('2013-07-22 12:01:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    #2013-07-14 08:00:00

    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 10:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 09:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 18:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 20:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 21:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-21 18:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))
    # req_template['simulation_config']['start_time'] = 1374249600  #  July 19, 2013 4:00:00 PM
    # req_template['simulation_config']['start_time'] = 1374258600  # July 19, 2013 6:30:00 PM  - 2013-07-19 18:30:00 / 12:30 PM MST  # MAX PV!!!
    # req_template['simulation_config']['start_time'] = timegm(strptime('2009-07-19 12:30:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))  # July 19, 2013 6:30:00 PM  - 2013-07-19 18:30:00 / 12:30 PM MST  # MAX PV!!!
    # dg_42 _2B5D7749-6C18-D77E-B848-3F4C31ADC3E6 p=146621.68181873375 q=-179.94738632961975
    # 2013-07-19 18:32:00
    # 2013-07-19 18:35:00

    # pv_84_90_event["occuredDateTime"] = req_template['simulation_config']['start_time'] + (5*60)
    # pv_84_90_event["stopDateTime"]    = req_template['simulation_config']['start_time'] + (10*60)
    # req_template["test_config"]["events"].append(pv_84_90_event)

    # req_template["test_config"]["events"].append(sw3_event)
    # req_template["test_config"]["events"].append(event_l114)

    # event_1_v2019_10_0["occuredDateTime"] = req_template['simulation_config']['start_time'] + (1*60)
    # event_1_v2019_10_0["stopDateTime"]    = req_template['simulation_config']['start_time'] + (3*60)
    # req_template["test_config"]["events"].append(event_1_v2019_10_0)

    # event_1["occuredDateTime"] = req_template['simulation_config']['start_time'] + (1*60)
    # event_1["stopDateTime"]    = req_template['simulation_config']['start_time'] + (3*60)
    # req_template["test_config"]["events"].append(event_1)

    #event_3_v2019_10_0
    event_3_v2019_10_0["occuredDateTime"] = req_template['simulation_config']['start_time'] + (1*60)
    event_3_v2019_10_0["stopDateTime"]    = req_template['simulation_config']['start_time'] + (3*60)
    # req_template["test_config"]["events"].append(event_3_v2019_10_0)

    # event_3["occuredDateTime"] = req_template['simulation_config']['start_time'] + (1*60)
    # event_3["stopDateTime"]    = req_template['simulation_config']['start_time'] + (3*60)
    # req_template["test_config"]["events"].append(event_3)

    app_config = {'OPF': 1, 'run_freq': 15, 'run_on_host': True}
    app_config['run_realtime'] = req_template['simulation_config']['run_realtime']
    app_config['stepsize_xp'] = 0.2
    app_config['stepsize_xq'] = 2
    # app_config['coeff_p'] = 0.1
    # app_config['coeff_q'] = 0.00005
    app_config['coeff_p'] = 0.005
    app_config['coeff_q'] = 0.0005
    app_config['Vupper'] = 1.025
    app_config['Vlower'] = 0.95
    app_config['stepsize_mu'] = 50000
    app_config['optimizer_num_iterations'] = 10
    print(json.dumps(app_config,indent=2))
    # exit(0)

    #TODO stepsize_mu = 50000 lower this! 500 or 50
    req_template["application_config"]["applications"] = [{"name": "der_dispatch_app", "config_string": json.dumps(app_config)}]

    # GMT: Tuesday, October 2, 2018 4:50:00 PM
    # Your time zone: Tuesday, October 2, 2018 10:50:00 AM GMT-06:00 DST
    req_template['power_system_config']['Line_name'] = '_E407CBB6-8C8D-9BC9-589C-AB83FBF0826D'  # Mine 123pv'
    # req_template['power_system_config']['Line_name'] = '_EBDB5A4A-543C-9025-243E-8CAD24307380'  # 123 with reg
    # req_template['power_system_config']['Line_name'] = '_88B3A3D8-51CD-7466-5E29-B692F21723CB' # Mine with feet conv
    req_template['power_system_config']['Line_name'] = '_DA00D94F-4683-FD19-15D9-8FF002220115'  # mine with house

    # req_template['power_system_config']['Line_name'] = '_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62'  # 13
    req_template['power_system_config']['Line_name'] = '_AAE94E4A-2465-6F5E-37B1-3E72183A4E44'  # New 8500 9500
    # req_template['power_system_config']['Line_name'] = '_C1C3E687-6FFD-C753-582B-632A27E28507'

    # req_template['power_system_config']['Line_name'] = '_4F76A5F9-271D-9EB8-5E31-AA362D86F2C3'
    # req_template["application_config"]["applications"][0]['name'] = 'sample_app'
    req_template["application_config"]["applications"][0]['name'] = 'der_dispatch_app'

    req_template['power_system_config']['Line_name'] = '_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62'
    # req_template['power_system_config']['Line_name'] = '_E407CBB6-8C8D-9BC9-589C-AB83FBF0826D'
    req_template['power_system_config']['Line_name'] = '_AAE94E4A-2465-6F5E-37B1-3E72183A4E44'

    ## TOD test with 13 8/24/2020

    simCfg13pv = json.dumps(req_template)
    print(simCfg13pv)

    goss = GOSS()
    goss.connect()

    simulation_id = goss.get_response(goss_sim, simCfg13pv, timeout=220) # 180 Maybe?
    simulation_id = int(simulation_id['simulationId'])
    print(simulation_id)
    print('sent simulation request')
    time.sleep(1)

    if app_config['run_on_host']:
        from main_app_new import DER_Dispatch
        listening_to_topic = simulation_output_topic(simulation_id)
        print(listening_to_topic)
        log_topic = simulation_log_topic(simulation_id)
        model_mrid = req_template['power_system_config']['Line_name']
        start_time = req_template['simulation_config']['start_time']
        app_configs = req_template["application_config"]["applications"]
        app_config = [json.loads(app['config_string']) for app in app_configs if app['name'] == 'der_dispatch_app'][0]

        # gapps = GridAPPSD(simulation_id)
        from gridappsd import utils
        gapps = GridAPPSD(simulation_id, address=utils.get_gridappsd_address(),
                          username=utils.get_gridappsd_user(), password=utils.get_gridappsd_pass())
        load_scale = req_template['simulation_config']['model_creation_config']['load_scaling_factor']
        der_0 = DER_Dispatch(simulation_id, gapps, model_mrid, './FeederInfo', start_time, app_config, load_scale)
        der_0.setup()
        gapps.subscribe(listening_to_topic, der_0)
        gapps.subscribe(log_topic, der_0)

        while der_0.running():
            time.sleep(0.1)
Exemple #14
0
def test_simulation_output(gridappsd_client, sim_config_file, sim_result_file):
    global resume_msg
    global pause_msg
    sim_config_file = os.path.join(os.path.dirname(__file__), f"simulation_config_files/{sim_config_file}")
    sim_expected_results_file = os.path.join(os.path.dirname(__file__), f"simulation_baseline_files/{sim_result_file}")
    sim_actual_result_file = f"/tmp/output/{sim_result_file}"
    os.makedirs(Path(sim_actual_result_file).parent, exist_ok=True)
    assert os.path.exists(sim_config_file), f"File {sim_config_file} must exist to run simulation test"
    # assert os.path.exists(sim_result_file), f"File {sim_result_file} must exist to run simulation test"

    gapps = gridappsd_client

    sleep(30)
    os.makedirs("/tmp/output", exist_ok=True)
    with open(sim_actual_result_file, 'w') as outfile:
        LOGGER.info('Configuring simulation')
        sim_complete = False
        rcvd_measurement = False
        rcvd_first_measurement = 0
        are_we_paused = False
        received_measurment_count = 0

        LOGGER.info('Loading config')
        with open(sim_config_file) as fp:
            LOGGER.info('Reading config')
            run_config = json.load(fp)
        # run_config["simulation_config"]["start_time"] = str(starttime)

        sim = Simulation(gapps, run_config)

        def onmeasurement(sim, timestep, measurements):
            LOGGER.info('Measurement received at %s', timestep)
            nonlocal rcvd_measurement
            nonlocal received_measurment_count
            received_measurment_count += 1
            try:

                if not rcvd_measurement:
                    print(f"A measurement happened at {timestep}")
                    # outfile.write(f"{timestep}|{json.dumps(measurements)}\n")
                    data = {"data": measurements}
                    outfile.write(json.dumps(data))
                    rcvd_measurement = True

                else:
                    rcvd_measurement = True
                # rcvd_first_measurement = True
            except Exception as ex:
                LOGGER.error(f"An exception handled in callback {ex}")

        # sleep here until rcvd_measuremnt = True again

        # def ontimestep(sim, timestep):
        #    # print("Timestamp: {}".format(timestep))

        def onfinishsimulation(sim):
            nonlocal sim_complete
            sim_complete = True
            LOGGER.info('Simulation Complete')

        LOGGER.info('sim.add_onmesurement_callback')

        LOGGER.info('Starting sim')
        sim.start_simulation()
        print(sim.simulation_id)
        sim.add_onmesurement_callback(onmeasurement)
        # sim.add_ontimestep_callback(ontimestep)
        gapps.subscribe(t.simulation_log_topic(sim.simulation_id), on_message)
        sim.add_oncomplete_callback(onfinishsimulation)
        LOGGER.info('sim.add_oncomplete_callback')
        secs = 0
        num_measurements_before_pause = 3
        while received_measurment_count < num_measurements_before_pause:
            LOGGER.info(f"Waiting for at least {num_measurements_before_pause} measurements"
                        f"but have {received_measurment_count} time take {secs}s ")
            secs += 1
            sleep(1)

        LOGGER.debug("Pausing sim now")
        sim.pause()
        are_we_paused = True

        paused_seconds = 0
        while are_we_paused:
            LOGGER.info(f"PAUSED {paused_seconds}")
            paused_seconds += 1

            if paused_seconds > 30:
                LOGGER.info('Resuming simulation')
                sim.resume()
                LOGGER.info('Resumed simulation')
                are_we_paused = False
                break
            sleep(1)

        assert not are_we_paused, "We should have came out of the paused_seconds > 30"
        assert "paused" in pause_msg, 'Pause command not called'
        sleep(10)
        assert "resumed" in resume_msg, 'Resume command not called'

        while not sim_complete:
            LOGGER.info('Sleeping')
            sleep(5)