Beispiel #1
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)
Beispiel #2
0
def generate_energy_consumer_measurements_9500():
    """Generate energy_consumer_measurements_9500.json"""
    platform = gridappsd_platform.PlatformManager()
    starttime = datetime(2013, 1, 14, 0, 0)
    sim_id = platform.run_simulation(feeder_id=FEEDER_MRID_9500,
                                     start_time=starttime,
                                     duration=20,
                                     realtime=False)

    # Get load measurement data. Save time by reading the csv.
    load_meas = pd.read_csv(LOAD_MEAS_9500)
    # Extract the first entry.
    mrid = load_meas.iloc[0]['id']

    # Wait for simulation completion.
    platform.wait_for_simulation()

    # TODO: Remove this time.sleep when
    #  https://github.com/GRIDAPPSD/gridappsd-forum/issues/24#issue-487936782
    #  has been addressed.
    time.sleep(10)

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

    # Write to file.
    _dict_to_json(fname=E_CONS_MEAS_9500, data=data)
Beispiel #3
0
def generate_weather_simple():
    """Generate simple single-entry weather data."""
    p = gridappsd_platform.PlatformManager()
    # Start with the json file.
    # noinspection PyProtectedMember
    j = p._query_weather(start_time=WEATHER_SIMPLE_START,
                         end_time=WEATHER_SIMPLE_END)
    _dict_to_json(j, WEATHER_SIMPLE_JSON)
Beispiel #4
0
def generate_weather_two_week():
    """Generate two weeks worth of data in 15 minute intervals."""
    p = gridappsd_platform.PlatformManager()
    d = p.get_weather(start_time=WEATHER_TWO_WEEK_START,
                      end_time=WEATHER_TWO_WEEK_END)
    to_file(d.resample('15Min', closed='right', label='right').mean(),
            WEATHER_TWO_WEEK,
            index=True)
Beispiel #5
0
def generate_model_info():
    """Generate 'query_model_info.json.
    """
    platform = gridappsd_platform.PlatformManager()

    info = platform.gad.query_model_info()

    _dict_to_json(data=info, fname=MODEL_INFO)
 def test_send_command_no_sim_id(self):
     """Ensure things properly blow up if there's no simulation ID to
     be found when trying to send a command.
     """
     # Get a fresh manager.
     p = gridappsd_platform.PlatformManager()
     with self.assertRaisesRegex(ValueError, 'In order to send a command,'):
         p.send_command(object_ids=['a'], attributes=['b'],
                        forward_values=[1], reverse_values=[2])
Beispiel #7
0
def generate_weather_for_sensor_data_9500():
    """Get weather data that lines up with the sensor data.
    """
    p = gridappsd_platform.PlatformManager()
    # Start with the json file.
    # noinspection PyProtectedMember
    j = p._query_weather(start_time=SENSOR_MEASUREMENT_TIME_START,
                         end_time=SENSOR_MEASUREMENT_TIME_END)
    _dict_to_json(j, WEATHER_FOR_SENSOR_DATA_9500_JSON)
    d = p.get_weather(start_time=SENSOR_MEASUREMENT_TIME_START,
                      end_time=SENSOR_MEASUREMENT_TIME_END)
    to_file(d, WEATHER_FOR_SENSOR_DATA_9500)
    def test_run_simulation_actually_run(self):
        """Test run_simulation, and legitimately run a simulation. Note
        this is more of an integration test, and also tests
        _update_sim_complete and wait_for_simulation.
        """
        # Get a fresh manager.
        p = gridappsd_platform.PlatformManager()

        # Use the smallest model we've got.
        feeder_id = _df.FEEDER_MRID_13

        # sim and sim_complete should be None.
        self.assertIsNone(p.sim_complete)
        self.assertIsNone(p.sim)

        # Run the simulation.
        p.run_simulation(feeder_id=feeder_id,
                         start_time=datetime(2013, 1, 1, 0, 0, 0),
                         duration=5, realtime=False, random_zip=False,
                         houses=False)

        # While the simulation is still running, sim_complete should be
        # False. NOTE: This builds in the assumption that this Python
        # code will take less time than the platform does to generate
        # and run the model. Seems reasonable.
        self.assertFalse(p.sim_complete)

        # sim should now be a gridappsd.simulation.Simulation object.
        self.assertIsInstance(p.sim, simulation.Simulation)

        # Wait for the simulation to complete.
        with utils.time_limit(10):
            p.wait_for_simulation()

        # Now, sim_complete should be None.
        self.assertIsNone(p.sim_complete)
Beispiel #9
0
# Dictionary of model MRIDs to file names. Note the 9500 node isn't
# truly from IEEE (it's a modified version of the 8500), but better to
# keep consistent + simple naming conventions.
MODELS = {
    '_4F76A5F9-271D-9EB8-5E31-AA362D86F2C3': 'ieee_8500',
    '_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62': 'ieee_13',
    '_C1C3E687-6FFD-C753-582B-632A27E28507': 'ieee_123',
    '_AAE94E4A-2465-6F5E-37B1-3E72183A4E44': 'ieee_9500'
}

# For convenience, create constants which other testing modules can
# import.
IEEE_8500 = os.path.join(MODEL_DIR, 'ieee_8500.glm')
IEEE_13 = os.path.join(MODEL_DIR, 'ieee_13.glm')
IEEE_123 = os.path.join(MODEL_DIR, 'ieee_123.glm')
# Modified 123 provided by Yuan so I (Brandon) can update glm.py for
# him.
IEEE_123_mod = os.path.join(MODEL_DIR, 'ieee_123_mod.glm')
IEEE_9500 = os.path.join(MODEL_DIR, 'ieee_9500.glm')

if __name__ == '__main__':
    platform = gridappsd_platform.PlatformManager()
    for mrid, model in MODELS.items():
        # Pull the model from the platform.
        model_str = platform.get_glm(model_id=mrid)

        with open(os.path.join(MODEL_DIR, model + '.glm'), 'w') as f:
            f.write(model_str)

    # That's it.
Beispiel #10
0
def generate_sensor_service_measurements_9500():
    """Use the sensor service to create average measurements for an
    energy consumer, then extract the measurements from the timeseries
    database and save them to file.
    """
    time_diff = SENSOR_MEASUREMENT_TIME_END - SENSOR_MEASUREMENT_TIME_START

    meas_data = _get_9500_meas_data_for_one_node()

    # Create a configuration for running the sensor service so that
    # we track the MRIDs of the given measurements.
    sensor_config = {}

    for mrid in meas_data['id']:
        sensor_config[mrid] = {
            # Well, this isn't true for all the measurements, but oh
            # well. We care less about the values and more about
            # actually getting the values.
            "nominal-value": 120,
            "perunit-confidence-band": 0.01,
            # TODO: Is this in seconds or time steps?
            "aggregation-interval": 30,
            # No drop out for now.
            "perunit-drop-rate": 0
        }

    full_config = [{
        'id': "gridappsd-sensor-simulator",
        'user_options': {
            "sensors-config": sensor_config,
            "random-seed": 42,
            "default-aggregation-interval": 30,
            "passthrough-if-not-specified": False,
            "default-perunit-confidence-band": 0.01,
            "default-perunit-drop-rate": 0
        }
    }]

    platform = gridappsd_platform.PlatformManager()
    # TODO: Use houses when
    #   https://github.com/GRIDAPPSD/gridappsd-forum/issues/26#issue-487939149
    #   is resolved.
    sim_id = platform.run_simulation(feeder_id=FEEDER_MRID_9500,
                                     start_time=SENSOR_MEASUREMENT_TIME_START,
                                     duration=int(time_diff.total_seconds()),
                                     realtime=False,
                                     applications=[],
                                     random_zip=False,
                                     houses=False,
                                     services=full_config,
                                     events=None)

    # Wait for simulation completion.
    platform.wait_for_simulation()

    # TODO: Remove this time.sleep when
    #  https://github.com/GRIDAPPSD/gridappsd-forum/issues/24#issue-487936782
    #  has been addressed.
    time.sleep(30)

    # Get output for all our MRIDs.
    for idx, meas_mrid in enumerate(meas_data['id'].values):
        # noinspection PyProtectedMember
        out = platform._query_simulation_output(
            simulation_id=sim_id,
            measurement_mrid=meas_mrid,
            query_measurement='gridappsd-sensor-simulator')

        # Save the output to file.
        _dict_to_json(data=out, fname=SENSOR_MEAS_LIST[idx])

    # Now, generate a file which has all measurements in one place.
    # NOTE: We'll ensure index_by_time is False.
    out = platform.get_simulation_output(
        simulation_id=sim_id,
        query_measurement='gridappsd-sensor-simulator',
        measurement_mrid=meas_data['id'].tolist(),
        index_by_time=False)

    # Save to file.
    to_file(df=out, csv_file=PARSED_SENSOR_9500_ALL)
Beispiel #11
0
def generate_cap_reg_switch_inverter_machine_meas_message_9500():
    """Generate cap_meas_message_9500.json, reg_meas_message_9500.json,
    switch_meas_message_9500.json, and inverter_meas_message_9500.json.
    """

    # Load up the capacitor data.
    caps = pd.read_csv(CAP_MEAS_9500)
    cap_mrids = caps['state_meas_mrid'].tolist()
    # Load up regulator data.
    regs = pd.read_csv(REG_MEAS_9500)
    reg_mrids = regs['pos_meas_mrid'].tolist()
    # Load up switch data.
    switches = pd.read_csv(SWITCH_MEAS_9500)
    switch_mrids = switches['state_meas_mrid'].tolist()
    # Load up inverter data.
    inverters = pd.read_csv(INVERTER_MEAS_9500)
    inverter_mrids = inverters['meas_mrid'].tolist()
    # Load up synchronous machine data.
    synch_mach = pd.read_csv(SYNCH_MACH_MEAS_9500)
    machine_mrids = synch_mach['meas_mrid'].tolist()

    # Initialize fn_mrid_list for a SimOutRouter.
    fn_mrid_list = [{
        'function': _dict_to_json,
        'mrids': cap_mrids,
        'kwargs': {
            'fname': CAP_MEAS_MSG_9500
        }
    }, {
        'function': _dict_to_json,
        'mrids': reg_mrids,
        'kwargs': {
            'fname': REG_MEAS_MSG_9500
        }
    }, {
        'function': _dict_to_json,
        'mrids': switch_mrids,
        'kwargs': {
            'fname': SWITCH_MEAS_MSG_9500
        }
    }, {
        'function': _dict_to_json,
        'mrids': inverter_mrids,
        'kwargs': {
            'fname': INVERTER_MEAS_MSG_9500
        }
    }, {
        'function': _dict_to_json,
        'mrids': machine_mrids,
        'kwargs': {
            'fname': SYNCH_MACH_MEAS_MSG_9500
        }
    }]

    platform = gridappsd_platform.PlatformManager()
    starttime = datetime(2013, 1, 14, 16, 0)
    sim_id = platform.run_simulation(feeder_id=FEEDER_MRID_9500,
                                     start_time=starttime,
                                     duration=5,
                                     realtime=False)

    # Create a SimOutRouter to save the measurements.
    # noinspection PyUnusedLocal
    router = gridappsd_platform.SimOutRouter(platform_manager=platform,
                                             sim_id=sim_id,
                                             fn_mrid_list=fn_mrid_list)

    # Wait for simulation completion.
    platform.wait_for_simulation()
 def setUpClass(cls):
     """Get a PlatformManager."""
     cls.platform = gridappsd_platform.PlatformManager()