Ejemplo n.º 1
0
line_mrid = run_config['power_system_config']['Line_name']

# indx = 0
# node = None
# for node in run_config['service_configs']:
#     if node['id'] == "gridappsd-sensor-simulator":
#         break
# if node is None:
#     raise AttributeError("Sensor service configuration is invalid.")
# node['user_options']['sensors-config'] = get_sensor_config(line_mrid)
import time, datetime

start_time = time.mktime(datetime.datetime.today().timetuple())

try:
    gapps = GridAPPSD(goss_log_level=logging.INFO)
except ConnectFailedException:
    print("Failed to connect, possible system is not running or login invalid!")
    sys.exit()

if not gapps.connected:
    print("Not connected yet, is the server running?")
    sys.exit()
sim = Simulation(gapps, run_config)

sim_complete = False

# fd = open("measurements.txt", 'w')

sim_output = []
measurement_output = []
Ejemplo n.º 2
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)
Ejemplo n.º 3
0
def test_can_start_gridapps():
    Containers.reset_all_containers()
    with run_dependency_containers() as cont:
        with run_gridappsd_container() as cont2:
            g = GridAPPSD()
            assert g.connected
Ejemplo n.º 4
0
                #     m_names = y.get("modelNames", [])
                if len(dict1) != len(dict2):
                    return False



                return True
if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('simulation_id', help="Simulation id")
    opts = parser.parse_args()
    simulation_id = opts.simulation_id
    sim_result_file = '/home/singha42/repos/gridappsd-testing/simulation_baseline_files/9500-simulation.json'


    gapps = GridAPPSD(opts.simulation_id, address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(), password=utils.get_gridappsd_pass())

    # For subscribing to the simulation output topic uncomment the below line
    #gapps.subscribe(simulation_output_topic(opts.simulation_id), Subscribe.on_message)


    #For testing PowergridAPI uncomment the following lines
    # object = '_0f6f3735-b297-46aa-8861-547d3cd0dee9'
    # with open("/tmp/output/power.json", 'w') as f:
    #     f.write(gapps.query_model_names(model_id=None))
    # r2 = gapps.query_object(object, model_id=None)
    # r3 = gapps.query_object_types(model_id=None)
    # # file.write(f'\n{json.dumps(r1, indent=4, sort_keys=True)},')
    # # file.write(f'\n{json.dumps(r2, indent=4, sort_keys=True)},')
    # #file.write(f'\n{json.dumps(r3, indent=4, sort_keys=True)}')
    # with open("/tmp/output/power.json", 'w') as f:
import os
import sys
import json
import pandas as pd
from collections import defaultdict
from tabulate import tabulate
import query_model_adms as query_model
sys.path.append('/usr/src/gridappsd-python')
from gridappsd import GridAPPSD

__GRIDAPPSD_URI__ = os.environ.get("GRIDAPPSD_URI", "localhost:61613")
print(__GRIDAPPSD_URI__)
if __GRIDAPPSD_URI__ == 'localhost:61613':
    gridappsd_obj = GridAPPSD(1234)
else:
    from gridappsd import utils
    # gridappsd_obj = GridAPPSD(simulation_id=1234, address=__GRIDAPPSD_URI__)
    print(utils.get_gridappsd_address())
    gridappsd_obj = GridAPPSD(1069573052,
                              address=utils.get_gridappsd_address(),
                              username=utils.get_gridappsd_user(),
                              password=utils.get_gridappsd_pass())
goss_sim = "goss.gridappsd.process.request.simulation"
timeseries = 'goss.gridappsd.process.request.data.timeseries'


def get_meas_dict(historical_results, convert2rect=True):
    meas_dict = defaultdict(dict)
    pos_dict = defaultdict(dict)
    diff_dict = defaultdict(dict)
    if 'data' in historical_results:
Ejemplo n.º 6
0
    def run_gridappsd_container(stop_after=True, rebuild_if_present=False) -> Container:
        """ A contextmanager that uses """

        parent_container = get_docker_in_docker()

        client = docker.from_env()
        # if there is a parent_container then we need to make sure that it is connected
        # to the same network as our systems.  If not then we need to modify the network
        # to include the parent container
        if parent_container:
            env = DEFAULT_GRIDAPPSD_DOCKER_CONFIG['gridappsd'].get('environment')
            if env is None:
                env = {}
            env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_ADDRESS.name] = 'gridappsd'
            env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_USER.name] = 'test_app_user'
            env[GRIDAPPSD_ENV_ENUM.GRIDAPPSD_PASSWORD.name] = '4Test'
            DEFAULT_GRIDAPPSD_DOCKER_CONFIG['gridappsd']['environment'] = env

            _log.debug(f"Running inside a container environment: {parent_container.name}")
            network = client.networks.get(NETWORK)
            has_it = False
            for x in network.containers:
                if x.name == parent_container.name:
                    has_it = True
                    _log.debug(f"parent_container {parent_container.name} is connected to the network.")
                    break
            if not has_it:
                _log.debug(f"Connecting new container to the network: {parent_container.name}")
                network.connect(parent_container)
        else:
            _log.debug("Not running in a container")

        containers = Containers(DEFAULT_GRIDAPPSD_DOCKER_CONFIG)

        gridappsd_container = None
        try:
            gridappsd_container = client.containers.get("gridappsd")
            _log.info(f"{gridappsd_container.name} container found")
            if rebuild_if_present:
                gridappsd_container.kill()
        except docker.errors.NotFound:
            _log.debug("gridappsd container not found!")

        try:
            if gridappsd_container is None:
                containers.start()

                # the gridappsd container itself will take a bit to start up.
                time.sleep(30)

                tries = 30
                while True:
                    tries -= 1
                    if tries <=0:
                        raise RuntimeError("Couldn't connect to gridappsd server in a timely manner!")
                    try:
                        g = GridAPPSD()
                        if g.connected:
                            _log.info("Connected to gridappsd!")
                            g.disconnect()
                            break

                    except stomp.exception.ConnectFailedException or stomp.exception.NotConnectedException:
                        _log.error("Retesting connection")

            yield gridappsd_container
        finally:
            if stop_after:
                containers.stop()
Ejemplo n.º 7
0
 def __init__(self, setting1=1, setting2="some/random/topic", **kwargs):
     super(GridAPPSDVolttron, self).__init__(**kwargs)
     self._gapps = GridAPPSD(override_threading=gevent.spawn)
     self._publish_event = None
Ejemplo n.º 8
0
def run(log,
        config,
        sim_id,
        model_id,
        gridappsd_address,
        sim_in_topic,
        sim_out_topic=None):

    # Initialize GridAPPSD object from within the platform.
    gridappsd_object = GridAPPSD(simulation_id=sim_id,
                                 address=gridappsd_address,
                                 username=utils.get_gridappsd_user(),
                                 password=utils.get_gridappsd_pass())

    # if sim_out_topic is not None:
    #     dump_output = DumpOutput()
    #     gridappsd_object.subscribe(topic=sim_out_topic, callback=dump_output)

    # Get all relevant model data from blazegraph/CIM.
    model_data = get_all_model_data(gridappsd_object, model_id, log=log)

    # Hard-code timezone for weather data.
    # TODO: when to "un-hard code?"
    tz = dateutil.tz.gettz('America/Denver')

    # Hard-code starting date in 2013, since that's what we have weather
    # data for.
    # TODO: "un-hard code" when possible
    # TODO: When UTC conversion bug is fixed with weather data, change
    # hour from 7 to 0 below.
    start_dt = datetime.datetime(year=2013,
                                 month=1,
                                 day=1,
                                 hour=7,
                                 minute=0,
                                 second=0,
                                 microsecond=0,
                                 tzinfo=tz)
    # NOTE: while this probably is slower than adding to the Unix
    # timestamp, this gives us handy dates for logging. This probably
    # isn't the way to go long term.
    end_dt = start_dt + dateutil.relativedelta.relativedelta(days=14)

    # Convert to Unix timestamps (which are in UTC)
    start_ts = datetime.datetime.timestamp(start_dt)
    end_ts = datetime.datetime.timestamp(end_dt)

    # The platform uses microseconds since the epoch, rather than
    # seconds, so be sure to convert. Also, it's taking strings, which
    # is annoying.
    start_time = '{:.0f}'.format(start_ts * 1e6)
    end_time = '{:.0f}'.format(end_ts * 1e6)

    # Pull weather data for the specified interval from the time series
    # database, and average it over 15 minute intervals.
    interval = 15
    interval_unit = 'min'
    weather = get_weather(gridappsd_object,
                          start_time,
                          end_time,
                          interval=interval,
                          interval_unit=interval_unit)

    # Get strings for dates (logging only)
    # TODO: hard-coding date formatting... yay!
    # TODO: should probably only do this if the log level is INFO.
    start_str = start_dt.strftime('%Y-%m-%d %H:%M:%S%z')
    end_str = end_dt.strftime('%Y-%m-%d %H:%M:%S%z')

    # Log it.
    log_str = \
        ('Weather data for {} through {} '.format(start_str, end_str)
         + 'pulled and averaged over {} {} '.format(interval, interval_unit)
         + 'intervals.')
    log.info(log_str)

    # Loop over the load measurements and pull historic data.

    # Grab a single MRID for experimentation
    meter_name = 'sx2673305b'
    data = get_data_for_meter(gridappsd_object, sim_id,
                              model_data['load_measurements'][meter_name])
    log.info('Data for meter {} pulled and parsed.'.format(meter_name))
    print('Measurements for meter {}:'.format(meter_name))
    print(data)

    # Get the GridLAB-D model
    # TODO: add to the Python API.
    payload = {
        'configurationType': 'GridLAB-D Base GLM',
        'parameters': {
            'model_id': model_id
        }
    }
    gld_model = gridappsd_object.get_response(topic=topics.CONFIG,
                                              message=payload,
                                              timeout=30)
    log.info('GridLAB-D model for GA use received.')

    # Remove the json remnants from the message via regular expressions.

    gld_model['message'] = re.sub('^\s*\{\s*"data"\s*:\s*', '',
                                  gld_model['message'])
    gld_model['message'] = re.sub('\s*,\s*"responseComplete".+$', '',
                                  gld_model['message'])
    log.warn('Bad json for GridLAB-D model fixed via regular expressions.')

    # Get a modGLM model to modify the base model.
    model_obj = modGLM.modGLM(strModel=gld_model['message'],
                              pathModelOut='test.glm',
                              pathModelIn='pyvvo.glm')
    model_obj.writeModel()
    log.info('modGLM object instantiated.')

    # Set up the model to run.
    st = '2016-01-01 00:00:00'
    et = '2016-01-01 00:15:00'
    tz = 'UTC0'

    swing_meter_name = model_obj.setupModel(
        starttime=st,
        stoptime=et,
        timezone=tz,
        database=config['GLD-DB'],
        powerflowFlag=True,
        vSource=model_data['swing_voltage'],
        triplexGroup=CONST.LOADS['triplex']['group'],
        triplexList=model_data['load_nominal_voltage']['triplex']['meters'])
    log.info('GridLAB-D model prepped for GA use.')

    # Write the base model
    # model_obj.writeModel()
    # log.info('Base GridLAB-D model configured and written to file.')

    # Build dictionary of recorder definitions which individuals in the
    # population will add to their model. We'll use the append record mode.
    # This can be risky! If you're not careful about clearing the database out
    # between subsequent test runs, you can write duplicate rows.
    recorders = buildRecorderDicts(
        energyInterval=config['INTERVALS']['OPTIMIZATION'],
        powerInterval=config['INTERVALS']['SAMPLE'],
        voltageInterval=config['INTERVALS']['SAMPLE'],
        energyPowerMeter=swing_meter_name,
        triplexGroup=CONST.LOADS['triplex']['group'],
        recordMode='a',
        query_buffer_limit=config['GLD-DB-OTHER']['QUERY_BUFFER_LIMIT'])
    log.info('Recorder dictionaries created.')

    # Convert costs from fraction of nominal voltage to actual voltage
    # Get pointer to costs dict.
    costs = config['COSTS']
    costs['undervoltage']['limit'] = \
        (costs['undervoltage']['limit']
         * model_data['load_nominal_voltage']['triplex']['v'])
    costs['overvoltage']['limit'] = \
        (costs['overvoltage']['limit']
         * model_data['load_nominal_voltage']['triplex']['v'])
    log.info('Voltage fractions converted to actual voltage for costs.')

    # Initialize a clock object for datetimes.
    clockObj = helper.clock(startStr=st,
                            finalStr=et,
                            interval=config['INTERVALS']['OPTIMIZATION'],
                            tzStr=tz)
    log.info('Clock object initialized')

    # Connect to the MySQL database for GridLAB-D simulations
    db_obj = db.db(**config['GLD-DB'],
                   pool_size=config['GLD-DB-OTHER']['NUM-CONNECTIONS'])
    log.info('Connected to MySQL for GA GridLAB-D output.')

    # Clear out the database while testing.
    # TODO: take this out?
    db_obj.dropAllTables()
    log.warning('All tables dropped in {}'.format(
        config['GLD-DB']['database']))

    # Initialize a population.
    # TODO - let's get the 'inPath' outta here. It's really just being used for
    # model naming, and we may as well be more explicit about that.
    sdt, edt = clockObj.getStartStop()
    pop_obj = population.population(strModel=model_obj.strModel,
                                    numInd=config['GA']['INDIVIDUALS'],
                                    numGen=config['GA']['GENERATIONS'],
                                    numModelThreads=config['GA']['THREADS'],
                                    recorders=recorders,
                                    dbObj=db_obj,
                                    starttime=sdt,
                                    stoptime=edt,
                                    timezone=tz,
                                    inPath=model_obj.pathModelIn,
                                    outDir='/pyvvo/pyvvo/models',
                                    reg=model_data['voltage_regulator'],
                                    cap=model_data['capacitor'],
                                    costs=costs,
                                    probabilities=config['PROBABILITIES'],
                                    gldInstall=config['GLD-INSTALLATION'],
                                    randomSeed=config['RANDOM-SEED'],
                                    log=log,
                                    baseControlFlag=0)

    log.info('Population object initialized.')

    log.info('Starting genetic algorithm...')
    best_ind = pop_obj.ga()
    log.info('Shutting down genetic algorithm threads...')
    pop_obj.stopThreads()

    log.info('Baseline costs:\n{}'.format(
        json.dumps(pop_obj.baselineData['costs'], indent=4)))
    log.info('Best individual:\n{}'.format(best_ind))

    # Send commands.
    if sim_in_topic is not None:
        command_capacitors(log=log,
                           sim_id=sim_id,
                           cap_dict=best_ind.cap,
                           gridappsd_object=gridappsd_object,
                           sim_in_topic=sim_in_topic)

        log.warning('Sleeping 5 seconds before commanding regulators.')
        time.sleep(5)

        command_regulators(log=log,
                           sim_id=sim_id,
                           reg_dict=best_ind.reg,
                           gridappsd_object=gridappsd_object,
                           sim_in_topic=sim_in_topic)
Ejemplo n.º 9
0
from time import sleep
from gridappsd import GridAPPSD

g = GridAPPSD()


def cb(header, msg):
    print(f"header: {header} message: {msg}")


g.subscribe("/topic/data", cb)

houses = g.get_houses()
hs = houses.get_houses_for_feeder('_503D6E20-F499-4CC7-8051-971E23D0BF79')
print(hs)

while True:
    sleep(0.1)
Ejemplo n.º 10
0
def start(log_file, feeder_mrid, model_api_topic, simulation_id):
    global logfile
    logfile = log_file

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

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

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic,
                               simulation_id)

    print('Querying Ybus...', flush=True)
    ysparse, nodelist = sparql_mgr.ybus_export()
    print('Processing Ybus...', flush=True)

    idx = 1
    nodes = {}
    for obj in nodelist:
        nodes[idx] = obj.strip('\"')
        idx += 1
    #print(nodes)

    Ybus = {}
    Yexp = {}
    for obj in ysparse:
        items = obj.split(',')
        if items[0] == 'Row':  # skip header line
            continue
        if nodes[int(items[0])] not in Ybus:
            Ybus[nodes[int(items[0])]] = {}
        Ybus[nodes[int(items[0])]][nodes[int(items[1])]] = complex(
            float(items[2]), float(items[3]))
        if nodes[int(items[1])] not in Yexp:
            Yexp[nodes[int(items[1])]] = {}
        Yexp[nodes[int(items[1])]][nodes[int(items[0])]] = complex(
            float(items[2]), float(items[3]))
    #print(Ybus)

    print('Ybus Processed', flush=True)
    print('Querying Vnom...', flush=True)

    vnom = sparql_mgr.vnom_export()

    print('Processing Vnom...', flush=True)

    CNV = {}
    for obj in vnom:
        items = obj.split(',')
        if items[0] == 'Bus':  # skip header line
            continue

        bus = items[0].strip('"')
        #basekV = float(items[1])
        #print('bus: ' + bus + ', basekV: ' + str(basekV))

        # old logic to hardwire rho for basekV<0.25 instead of using
        # query results directly
        #if basekV < 0.25:
        #    rho = 120.0
        #else:
        #    rho = 1000.0*basekV/math.sqrt(3.0)

        node1 = items[2].strip()
        rho1 = float(items[3])
        angle = float(items[4])
        # the following bit of cleverness rounds to the nearest 30
        angle1 = float(30 * round(int(angle) / 30))
        theta1 = angle1 * math.pi / 180.0
        #print('**** rho1 for: ' + bus+'.'+node1 + ' is: ' + str(rho1) + ', theta1: ' + str(theta1))
        CNV[bus + '.' + node1] = complex(rho1 * math.cos(theta1),
                                         rho1 * math.sin(theta1))

        node2 = items[6].strip()
        if node2 != '0':
            rho2 = float(items[7])
            angle = float(items[8])
            angle2 = float(30 * round(int(angle) / 30))
            theta2 = angle2 * math.pi / 180.0
            #print('**** rho2 for: ' + bus+'.'+node2 + ' is: ' + str(rho2) + ', theta2: ' + str(theta1))
            CNV[bus + '.' + node2] = complex(rho2 * math.cos(theta2),
                                             rho2 * math.sin(theta2))

            node3 = items[10].strip()
            if node3 != '0':
                rho3 = float(items[11])
                angle = float(items[12])
                angle3 = float(30 * round(int(angle) / 30))
                theta3 = angle3 * math.pi / 180.0
                #print('**** rho3 for: ' + bus+'.'+node3 + ' is: ' + str(rho3) + ', theta3: ' + str(theta1))
                CNV[bus + '.' + node3] = complex(rho3 * math.cos(theta3),
                                                 rho3 * math.sin(theta3))

    print('Vnom Processed', flush=True)

    global greenCountReal, yellowCountReal, redCountReal
    greenCountReal = yellowCountReal = redCountReal = 0
    global greenCountImag, yellowCountImag, redCountImag
    greenCountImag = yellowCountImag = redCountImag = 0

    countReal, countImag = validate_ShuntElement_elements(
        sparql_mgr, Ybus, Yexp, CNV)

    # list of lists for the tabular report
    report = []

    if countReal > 0:
        VI = float(countReal - redCountReal) / float(countReal)
        report.append([
            "Real", countReal, "{:.4f}".format(VI), greenCountReal,
            yellowCountReal, redCountReal
        ])
    else:
        report.append(["Real", countReal])

    if countImag > 0:
        VI = float(countImag - redCountImag) / float(countImag)
        report.append([
            "Imaginary", countImag, "{:.4f}".format(VI), greenCountImag,
            yellowCountImag, redCountImag
        ])
    else:
        report.append(["Imaginary", countImag])

    print('\n', flush=True)
    print(tabulate(report,
                   headers=[
                       "Shunt Component", "# Nodes", "VI",
                       diffColor(0, True),
                       diffColor(1, True),
                       diffColor(2, True)
                   ],
                   tablefmt="fancy_grid"),
          flush=True)
    print('\n', file=logfile)
    print(tabulate(report,
                   headers=[
                       "Shunt Component", "# Nodes", "VI",
                       diffColor(0, False),
                       diffColor(1, False),
                       diffColor(2, False)
                   ],
                   tablefmt="fancy_grid"),
          file=logfile)

    print('\nSHUNT_ELEMENT_VALIDATOR DONE!!!', flush=True)
    print('\nSHUNT_ELEMENT_VALIDATOR DONE!!!', file=logfile)

    return
def start(log_file, feeder_mrid, model_api_topic, simulation_id):
    global logfile
    logfile = log_file

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

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    #ysparse,nodelist = sparql_mgr.ybus_export()

    #idx = 1
    #nodes = {}
    #for obj in nodelist:
    #    nodes[idx] = obj.strip('\"')
    #    idx += 1
    ##print(nodes)

    #Ybus = {}
    #for obj in ysparse:
    #    items = obj.split(',')
    #    if items[0] == 'Row':
    #        continue
    #    if nodes[int(items[0])] not in Ybus:
    #        Ybus[nodes[int(items[0])]] = {}
    #    Ybus[nodes[int(items[0])]][nodes[int(items[1])]] = complex(float(items[2]), float(items[3]))
    ##print(Ybus)

    Ysys = {}
    Unsupported = {}

    mod_import = importlib.import_module('line_model_validator.line_model_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys, Unsupported)
    #print('line_model_validator Ysys...')
    #print(Ysys)
    #line_count = 0
    #for bus1 in Ysys:
    #    line_count += len(Ysys[bus1])
    #print('\nLine_model # entries: ' + str(line_count) + '\n', flush=True)
    #print('\nLine_model # entries: ' + str(line_count) + '\n', file=logfile)

    mod_import = importlib.import_module('power_transformer_validator.power_transformer_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys, Unsupported)
    #print('power_transformer_validator Ysys...')
    #print(Ysys)
    #count = 0
    #for bus1 in Ysys:
    #    count += len(Ysys[bus1])
    #xfmr_count = count - line_count
    #print('Power_transformer # entries: ' + str(xfmr_count) + '\n', flush=True)
    #print('Power_transformer # entries: ' + str(xfmr_count) + '\n', file=logfile)

    mod_import = importlib.import_module('switching_equipment_validator.switching_equipment_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys, Unsupported)
    #print('switching_equipment_validator (final) Ysys...')
    #print(Ysys)
    #count = 0
    #for bus1 in Ysys:
    #    count += len(Ysys[bus1])
    #switch_count = count - line_count - xfmr_count
    #print('Switching_equipment # entries: ' + str(switch_count) + '\n', flush=True)
    #print('Switching_equipment # entries: ' + str(switch_count) + '\n', file=logfile)

    #print('\n*** Full Ysys:\n')
    #for bus1 in Ysys:
    #    for bus2 in Ysys[bus1]:
    #        print(bus1 + ',' + bus2 + ',' + str(Ysys[bus1][bus2].real) + ',' + str(Ysys[bus1][bus2].imag))

    ysysCount = 0
    for bus1 in Ysys:
        ysysCount += len(Ysys[bus1])
    #print('Total computed # entries: ' + str(ysysCount) + '\n', flush=True)
    #print('Total computed # entries: ' + str(ysysCount) + '\n', file=logfile)

    # build the Numpy matrix from the full Ysys before we start deleting
    # entries to check Ysys vs. Ybus
    # first, create a node index dictionary
    Node2idx = {}
    N = 0
    for bus1 in list(Ysys):
        if bus1 not in Node2idx:
            Node2idx[bus1] = N
            N += 1
        for bus2 in list(Ysys[bus1]):
            if bus2 not in Node2idx:
                Node2idx[bus2] = N
                N += 1
    print('Node2idx size: ' + str(N))
    print('Node2idx dictionary:')
    print(Node2idx)

    sourcebus, sourcevang = sparql_mgr.sourcebus_query()
    sourcebus = sourcebus.upper()
    #print('\nquery results sourcebus: ' + sourcebus)
    #print('query results sourcevang: ' + str(sourcevang))

    bindings = sparql_mgr.nomv_query()
    #print('\nnomv query results:')
    #print(bindings)

    sqrt3 = math.sqrt(3.0)
    Vmag = {}

    for obj in bindings:
        busname = obj['busname']['value'].upper()
        nomv = float(obj['nomv']['value'])
        Vmag[busname] = nomv/sqrt3

    Vang = {}
    Vang['1'] = math.radians(0.0)
    Vang['2'] = math.radians(-120.0)
    Vang['3'] = math.radians(120.0)

    # calculate CandidateVnom
    CandidateVnom = {}
    CandidateVnomPolar = {}
    for node in Node2idx:
        bus = node[:node.find('.')]
        phase = node[node.find('.')+1:]

        # source bus is a special case for the angle
        if node.startswith(sourcebus+'.'):
            CandidateVnom[node] = pol2cart(Vmag[bus], sourcevang+Vang[phase])
            CandidateVnomPolar[node] = (Vmag[bus], math.degrees(sourcevang+Vang[phase]))
        else:
            if bus in Vmag:
                CandidateVnom[node] = pol2cart(Vmag[bus], Vang[phase])
                CandidateVnomPolar[node] = (Vmag[bus], math.degrees(Vang[phase]))
            else:
                print('*** WARNING:  no nomv value for bus: ' + bus + ' for node: ' + node)

    #print('\nCandidateVnom dictionary:')
    #print(CandidateVnom)

    src_idxs = []
    if sourcebus+'.1' in Node2idx:
        src_idxs.append(Node2idx[sourcebus+'.1'])
    if sourcebus+'.2' in Node2idx:
        src_idxs.append(Node2idx[sourcebus+'.2'])
    if sourcebus+'.3' in Node2idx:
        src_idxs.append(Node2idx[sourcebus+'.3'])
    print('\nsrc_idxs: ' + str(src_idxs))

    YsysMatrix = np.zeros((N,N), dtype=complex)
    # next, remap into a numpy array
    for bus1 in list(Ysys):
        for bus2 in list(Ysys[bus1]):
            YsysMatrix[Node2idx[bus2],Node2idx[bus1]] = YsysMatrix[Node2idx[bus1],Node2idx[bus2]] = Ysys[bus1][bus2]
    # dump YsysMatrix for MATLAB comparison
    #print('\nYsysMatrix for MATLAB:')
    #for row in range(N):
    #    for col in range(N):
    #        print(str(row+1) + ',' + str(col+1) + ',' + str(YsysMatrix[row,col].real) + ',' + str(YsysMatrix[row,col].imag))

    np.set_printoptions(threshold=sys.maxsize)
    #print('\nYsys numpy array:')
    #print(YsysMatrix)

    # create the CandidateVnom numpy vector for computations below
    CandidateVnomVec = np.zeros((N), dtype=complex)
    for node in Node2idx:
        if node in CandidateVnom:
            print('CandidateVnomVec node: ' + node + ', index: ' + str(Node2idx[node]) + ', cartesian value: ' + str(CandidateVnom[node]) + ', polar value: ' + str(CandidateVnomPolar[node]))
            CandidateVnomVec[Node2idx[node]] = CandidateVnom[node]
        else:
            print('*** WARNING: no CandidateVnom value for populating node: ' + node + ', index: ' + str(Node2idx[node]))
    #print('\nCandidateVnom:')
    #print(CandidateVnomVec)
    # dump CandidateVnomVec to CSV file for MATLAB comparison
    #print('\nCandidateVnom for MATLAB:')
    #for row in range(N):
    #    print(str(CandidateVnomVec[row].real) + ',' + str(CandidateVnomVec[row].imag))

    # time to get the source injection terms
    # first, get the dictionary of regulator ids
    bindings = sparql_mgr.regid_query()
    Rids = []
    for obj in bindings:
        Rids.append(obj['rid']['value'])
    print('\nRegulator IDs: ' + str(Rids))

    # second, subscribe to simulation output so we can start setting tap
    # positions to 0
    simSetRap = SimSetWrapper(gapps, simulation_id, Rids)
    conn_id = gapps.subscribe(simulation_output_topic(simulation_id), simSetRap)

    while simSetRap.keepLooping():
        #print('Sleeping....', flush=True)
        time.sleep(0.1)

    gapps.unsubscribe(conn_id)

    bindings = sparql_mgr.query_energyconsumer_lf()
    #print(bindings)

    phaseIdx = {'A': '.1', 'B': '.2', 'C': '.3', 's1': '.1', 's2': '.2'}
    DeltaList = []
    #print("\nDelta connected load EnergyConsumer query:")
    for obj in bindings:
        #name = obj['name']['value'].upper()
        bus = obj['bus']['value'].upper()
        conn = obj['conn']['value']
        phases = obj['phases']['value']
        #print('bus: ' + bus + ', conn: ' + conn + ', phases: ' + phases)
        if conn == 'D':
           if phases == '':
               DeltaList.append(bus+'.1')
               DeltaList.append(bus+'.2')
               DeltaList.append(bus+'.3')
           else:
               DeltaList.append(bus+phaseIdx[phases])

    PNVmag = np.zeros((N), dtype=float)

    # third, verify all tap positions are 0
    config_api_topic = 'goss.gridappsd.process.request.config'
    message = {
        'configurationType': 'CIM Dictionary',
        'parameters': {'model_id': feeder_mrid}
        }
    cim_dict = gapps.get_response(config_api_topic, message, timeout=10)
    #print('\nCIM Dictionary:')
    #print(cim_dict)
    # get list of regulator mRIDs
    RegMRIDs = []
    CondMRIDs = []
    PNVmRIDs = []
    PNVdict = {}
    condTypes = set(['EnergyConsumer', 'LinearShuntCompensator', 'PowerElectronicsConnection', 'SynchronousMachine'])
    phaseIdx2 = {'A': '.2', 'B': '.3', 'C': '.1'}

    for feeder in cim_dict['data']['feeders']:
        for measurement in feeder['measurements']:
            if measurement['name'].startswith('RatioTapChanger') and measurement['measurementType']=='Pos':
                RegMRIDs.append(measurement['mRID'])

            elif measurement['measurementType']=='VA' and (measurement['ConductingEquipment_type'] in condTypes):
                node = measurement['ConnectivityNode'].upper() + phaseIdx[measurement['phases']]
                if node in DeltaList:
                    node2 = measurement['ConnectivityNode'].upper() + phaseIdx2[measurement['phases']]
                    #print('Appending CondMRID tuple: (' + measurement['mRID'] + ', ' + measurement['ConductingEquipment_type'] + ', ' + str(Node2idx[node]) + ', ' + str(Node2idx[node2]) + ') for node: ' + node, flush=True)
                    CondMRIDs.append((measurement['mRID'], measurement['ConductingEquipment_type'], Node2idx[node], Node2idx[node2]))
                else:
                    #print('Appending CondMRID tuple: (' + measurement['mRID'] + ', ' + measurement['ConductingEquipment_type'] + ', ' + str(Node2idx[node]) + ', None) for node: ' + node, flush=True)
                    CondMRIDs.append((measurement['mRID'], measurement['ConductingEquipment_type'], Node2idx[node], None))

            elif measurement['measurementType'] == 'PNV':
                # save PNV measurements in Andy's mixing bowl for later
                node = measurement['ConnectivityNode'].upper() + phaseIdx[measurement['phases']]
                #print('Appending PNVmRID tuple: (' + measurement['mRID'] + ', ' + measurement['ConductingEquipment_type'] + ', ' + str(Node2idx[node]) + ') for node: ' + node, flush=True)
                PNVmRIDs.append((measurement['mRID'], Node2idx[node]))
                PNVdict[Node2idx[node]] = measurement['mRID']

    print('Found RatioTapChanger mRIDs: ' + str(RegMRIDs), flush=True)
    print('Found ConductingEquipment mRIDs: ' + str(CondMRIDs), flush=True)
    print('Found PNV dictionary: ' + str(PNVdict), flush=True)
    print('PNV dictionary size: ' + str(len(PNVdict)), flush=True)

    # fourth, verify tap ratios are all 0 and then set Sinj values for the
    # conducting equipment mRIDs by listening to simulation output

    # start with Sinj as zero vector and we will come back to this later
    Sinj = np.zeros((N), dtype=complex)
    Sinj[src_idxs] = complex(0.0,1.0)
    print('\nInitial Sinj:')
    print(Sinj)

    # subscribe to simulation output so we can start checking tap positions
    # and then setting Sinj
    simCheckRap = SimCheckWrapper(Sinj, PNVmag, RegMRIDs, CondMRIDs, PNVmRIDs, PNVdict)
    conn_id = gapps.subscribe(simulation_output_topic(simulation_id), simCheckRap)

    while simCheckRap.keepLooping():
        #print('Sleeping....', flush=True)
        time.sleep(0.1)

    gapps.unsubscribe(conn_id)

    print('\nFinal Sinj:')
    #print(Sinj)
    for key,value in Node2idx.items():
        print(key + ': ' + str(Sinj[value]))

    vsrc = np.zeros((3), dtype=complex)
    vsrc = CandidateVnomVec[src_idxs]
    #print('\nvsrc:')
    #print(vsrc)

    Iinj_nom = np.conj(Sinj/CandidateVnomVec)
    #print('\nIinj_nom:')
    #print(Iinj_nom)

    Yinj_nom = -Iinj_nom/CandidateVnomVec
    #print('\nYinj_nom:')
    #print(Yinj_nom)

    Yaug = YsysMatrix + np.diag(Yinj_nom)
    #print('\nYaug:')
    #print(Yaug)

    Zaug = np.linalg.inv(Yaug)
    #print('\nZaug:')
    #print(Zaug)

    tolerance = 0.01
    Nfpi = 10
    Nfpi = 15
    Isrc_vec = np.zeros((N), dtype=complex)
    Vfpi = np.zeros((N,Nfpi), dtype=complex)

    # start with the CandidateVnom for Vfpi
    Vfpi[:,0] = CandidateVnomVec
    #print('\nVfpi:')
    #print(Vfpi)

    k = 1
    maxdiff = 1.0

    while k<Nfpi and maxdiff>tolerance:
        Iload_tot = np.conj(Sinj / Vfpi[:,k-1])
        Iload_z = -Yinj_nom * Vfpi[:,k-1]
        Iload_comp = Iload_tot - Iload_z
        #print('\nIload_comp numpy matrix:')
        #print(Iload_comp)

        term1 = np.linalg.inv(Zaug[np.ix_(src_idxs,src_idxs)])
        term2 = vsrc - np.matmul(Zaug[np.ix_(src_idxs,list(range(N)))], Iload_comp)
        Isrc_vec[src_idxs] = np.matmul(term1, term2)
        #print("\nIsrc_vec:")
        #print(Isrc_vec)

        Icomp = Isrc_vec + Iload_comp
        Vfpi[:,k] = np.matmul(Zaug, Icomp)
        #print("\nVfpi:")
        #print(Vfpi)
        #print(Vfpi[:,k])

        maxlist = abs(abs(Vfpi[:,k]) - abs(Vfpi[:,k-1]))
        print("\nmaxlist:")
        for i in range(41):
          print(str(i) + ": " + str(maxlist[i]))

        maxdiff = max(abs(abs(Vfpi[:,k]) - abs(Vfpi[:,k-1])))
        print("\nk: " + str(k) + ", maxdiff: " + str(maxdiff))
        k += 1

    if k == Nfpi:
        print("\nDid not converge with k: " + str(k))
        return

    # set the final Vpfi index
    k -= 1
    print("\nconverged k: " + str(k))
    print("\nVfpi:")
    for key, value in Node2idx.items():
        rho, phi = cart2pol(Vfpi[value,k])
        print(key + ': rho: ' + str(rho) + ', phi: ' + str(math.degrees(phi)))
        print('index: ' + str(value) + ', sim mag: ' + str(PNVmag[value]))

    print("\nVfpi rho to sim magnitude CSV:")
    for key, value in Node2idx.items():
        mag = PNVmag[value]
        if mag != 0.0:
            rho, phi = cart2pol(Vfpi[value,k])
            print(str(value) + ',' + key + ',' + str(rho) + ',' + str(mag))

    return
Ejemplo n.º 12
0
from device import Device
from equipment import Equipments, SynchronousMachine, Solar, Battery
from DERGroups import DERGroups, EndDeviceGroup, EndDevice, DERFunction
from exceptions import SamemRIDException, SameGroupNameException
from message import ReplyType, HeaderType, ResultType, ErrorType, LevelType, UUIDWithAttribute, VerbType, IDKindType, \
    Name
from ExecuteDERGroupsCommands import insertEndDeviceGroup, deleteDERGroupByMrid, deleteDERGroupByName, modifyDERGroup
from DERGroupsMessage import DERGroupsPayloadType, DERGroupsResponseMessageType, DERGroupsRequestMessageType
from datetime import datetime
from DERGroupQueries import DERGroupQueries
from DERGroupQueriesMessage import DERGroupQueriesResponseMessageType, DERGroupQueriesRequestType, \
    DERGroupQueriesPayloadType
from DERGroupStatusQueriesMessage import DERGroupStatusQueriesResponseMessageType, DERGroupStatusQueriesRequestType

conn = GridAPPSD()
# conn.subscribe()

# Devices = []


def get_DERM_devices():
    # payload = conn._build_query_payload('QUERY', queryString=Queries.querySynchronousMachine)
    # request_topic = '.'.join((t.REQUEST_DATA, "powergridmodel"))
    # results = conn.get_response(request_topic, json.dumps(payload), timeout=30)
    # pprint.pprint(results)
    #
    # payload = conn._build_query_payload('QUERY', queryString=Queries.querySolar )
    # request_topic = '.'.join((t.REQUEST_DATA, "powergridmodel"))
    # results = conn.get_response(request_topic, json.dumps(payload), timeout=30)
    # pprint.pprint(results)
Ejemplo n.º 13
0
def start(log_file, feeder_mrid, model_api_topic):
    global logfile
    logfile = log_file

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

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    ysparse, nodelist = sparql_mgr.ybus_export()

    idx = 1
    nodes = {}
    for obj in nodelist:
        nodes[idx] = obj.strip('\"')
        idx += 1
    #print(nodes)

    Ybus = {}
    for obj in ysparse:
        items = obj.split(',')
        if items[0] == 'Row':
            continue
        if nodes[int(items[0])] not in Ybus:
            Ybus[nodes[int(items[0])]] = {}
        Ybus[nodes[int(items[0])]][nodes[int(items[1])]] = complex(
            float(items[2]), float(items[3]))
    #print(Ybus)

    Ysys = {}
    Unsupported = {}

    mod_import = importlib.import_module(
        'line_model_validator.line_model_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys,
               Unsupported)
    #print('line_model_validator Ysys...')
    #print(Ysys)
    #line_count = 0
    #for bus1 in Ysys:
    #    line_count += len(Ysys[bus1])
    #print('\nLine_model # entries: ' + str(line_count) + '\n', flush=True)
    #print('\nLine_model # entries: ' + str(line_count) + '\n', file=logfile)

    mod_import = importlib.import_module(
        'power_transformer_validator.power_transformer_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys,
               Unsupported)
    #print('power_transformer_validator Ysys...')
    #print(Ysys)
    #count = 0
    #for bus1 in Ysys:
    #    count += len(Ysys[bus1])
    #xfmr_count = count - line_count
    #print('Power_transformer # entries: ' + str(xfmr_count) + '\n', flush=True)
    #print('Power_transformer # entries: ' + str(xfmr_count) + '\n', file=logfile)

    mod_import = importlib.import_module(
        'switching_equipment_validator.switching_equipment_validator')
    start_func = getattr(mod_import, 'start')
    start_func(log_file, feeder_mrid, model_api_topic, False, Ysys,
               Unsupported)
    #print('switching_equipment_validator (final) Ysys...')
    #print(Ysys)
    #count = 0
    #for bus1 in Ysys:
    #    count += len(Ysys[bus1])
    #switch_count = count - line_count - xfmr_count
    #print('Switching_equipment # entries: ' + str(switch_count) + '\n', flush=True)
    #print('Switching_equipment # entries: ' + str(switch_count) + '\n', file=logfile)

    #print('\n*** Full Ysys:\n')
    #for bus1 in Ysys:
    #    for bus2 in Ysys[bus1]:
    #        print(bus1 + ',' + bus2 + ',' + str(Ysys[bus1][bus2].real) + ',' + str(Ysys[bus1][bus2].imag))

    ysysCount = 0
    for bus1 in Ysys:
        ysysCount += len(Ysys[bus1])
    #print('Total computed # entries: ' + str(ysysCount) + '\n', flush=True)
    #print('Total computed # entries: ' + str(ysysCount) + '\n', file=logfile)

    #print('\n*** Full Ybus:\n')
    #for bus1 in Ybus:
    #    for bus2 in Ybus[bus1]:
    #        print(bus1 + ',' + bus2 + ',' + str(Ybus[bus1][bus2].real) + ',' + str(Ybus[bus1][bus2].imag))

    ybusCount = 0
    for bus1 in Ybus:
        ybusCount += len(Ybus[bus1])
    #print('Total Ybus # entries: ' + str(ybusCount) + '\n', flush=True)
    #print('Total Ybus # entries: ' + str(ybusCount) + '\n', file=logfile)

    for bus1 in list(Ybus):
        for bus2 in list(Ybus[bus1]):
            delYFlag = False
            if (bus1 in Ysys) and (bus2 in Ysys[bus1]):
                del Ysys[bus1][bus2]
                if len(Ysys[bus1]) == 0:
                    del Ysys[bus1]

                del Ybus[bus1][bus2]
                if len(Ybus[bus1]) == 0:
                    del Ybus[bus1]
                delYFlag = True

            if (bus2 in Ysys) and (bus1 in Ysys[bus2]):
                del Ysys[bus2][bus1]
                if len(Ysys[bus2]) == 0:
                    del Ysys[bus2]

                if not delYFlag:
                    del Ybus[bus1][bus2]
                    if len(Ybus[bus1]) == 0:
                        del Ybus[bus1]

    redCount = 0
    for bus1 in Ysys:
        redCount += len(Ysys[bus1])
    print('\n*** Missing Ybus entries: ' + str(redCount) + '\n', flush=True)
    print('\n*** Missing Ybus entries: ' + str(redCount) + '\n', file=logfile)

    greenCount = ysysCount - redCount
    yellowCount = 0
    VI = float(greenCount + yellowCount) / float(ysysCount)
    report = []
    report.append([
        f"Expected entries\N{SUPERSCRIPT ONE}", ysysCount, "{:.4f}".format(VI),
        greenCount, yellowCount, redCount
    ])

    for bus1 in Ysys:
        for bus2 in Ysys[bus1]:
            print(bus1 + ',' + bus2 + ',' + str(Ysys[bus1][bus2].real) + ',' +
                  str(Ysys[bus1][bus2].imag) + ',' + redCircle(True),
                  flush=True)
            print(bus1 + ',' + bus2 + ',' + str(Ysys[bus1][bus2].real) + ',' +
                  str(Ysys[bus1][bus2].imag) + ',' + redCircle(False),
                  file=logfile)

    unexpectedCount = 0
    for bus1 in Ybus:
        unexpectedCount += len(Ybus[bus1])
    print('\n*** Unexpected Ybus entries: ' + str(unexpectedCount) + '\n',
          flush=True)
    print('\n*** Unexpected Ybus entries: ' + str(unexpectedCount) + '\n',
          file=logfile)

    yellowCount = 0
    redCount = 0
    for bus1 in Ybus:
        for bus2 in Ybus[bus1]:
            if abs(Ybus[bus1][bus2] - complex(0.0, 0.0)) > 1.0e-9:
                short_bus1 = bus1.split('.')[0]
                short_bus2 = bus2.split('.')[0]
                if short_bus1 in Unsupported and short_bus2 in Unsupported[
                        short_bus1][0]:
                    print(bus1 + ',' + bus2 + ',' +
                          str(Ybus[bus1][bus2].real) + ',' +
                          str(Ybus[bus1][bus2].imag) + ',' +
                          yellowCircle(True) + ' ,***UNSUPPORTED: ' +
                          Unsupported[short_bus1][1],
                          flush=True)
                    print(bus1 + ',' + bus2 + ',' +
                          str(Ybus[bus1][bus2].real) + ',' +
                          str(Ybus[bus1][bus2].imag) + ',' +
                          yellowCircle(False) + ' ,***UNSUPPORTED: ' +
                          Unsupported[short_bus1][1],
                          file=logfile)
                    yellowCount += 1
                else:
                    print(bus1 + ',' + bus2 + ',' +
                          str(Ybus[bus1][bus2].real) + ',' +
                          str(Ybus[bus1][bus2].imag) + ',' + redCircle(True),
                          flush=True)
                    print(bus1 + ',' + bus2 + ',' +
                          str(Ybus[bus1][bus2].real) + ',' +
                          str(Ybus[bus1][bus2].imag) + ',' + redCircle(False),
                          file=logfile)
                    redCount += 1
            else:
                print(bus1 + ',' + bus2 + ',' + str(Ybus[bus1][bus2].real) +
                      ',' + str(Ybus[bus1][bus2].imag) + ',' +
                      yellowCircle(True) + ' ,***NEAR_ZERO',
                      flush=True)
                print(bus1 + ',' + bus2 + ',' + str(Ybus[bus1][bus2].real) +
                      ',' + str(Ybus[bus1][bus2].imag) + ',' +
                      yellowCircle(False) + ' ,***NEAR_ZERO',
                      file=logfile)
                yellowCount += 1

    greenCount = ybusCount - unexpectedCount
    VI = float(ybusCount - redCount) / float(ybusCount)
    report.append([
        f"Existing entries\N{SUPERSCRIPT TWO}", ybusCount, "{:.4f}".format(VI),
        greenCount, yellowCount, redCount
    ])

    print('\n', flush=True)
    print(tabulate(report,
                   headers=[
                       "Ybus check", "Entries checked", "VI",
                       greenCircle(True),
                       yellowCircle(True),
                       redCircle(True)
                   ],
                   tablefmt="fancy_grid"),
          flush=True)
    print('', flush=True)

    print('\n', file=logfile)
    print(tabulate(report,
                   headers=[
                       "Ybus check", "Entries checked", "VI",
                       greenCircle(False),
                       yellowCircle(False),
                       redCircle(False)
                   ],
                   tablefmt="fancy_grid"),
          file=logfile)
    print('', file=logfile)

    print(
        f"\N{SUPERSCRIPT ONE}Checks whether each expected entry is found in Ybus where green=found; yellow=not found, but explainable; red=not found",
        flush=True)
    print(
        f"\N{SUPERSCRIPT TWO}Checks whether each existing entry in Ybus is expected where green=expected; yellow=unexpected, but explainable; red=unexpected\n",
        flush=True)
    print(
        f"\N{SUPERSCRIPT ONE}Checks whether each expected entry is found in Ybus where green=found; yellow=not found, but explainable; red=not found",
        file=logfile)
    print(
        f"\N{SUPERSCRIPT TWO}Checks whether each existing entry in Ybus is expected where green=expected; yellow=unexpected, but explainable; red=unexpected\n",
        file=logfile)
Ejemplo n.º 14
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.0,
                "i_fraction": 1.0,
                "p_fraction": 0.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"
    }

    req_template['simulation_config']['model_creation_config'][
        'load_scaling_factor'] = 1
    req_template['simulation_config']['run_realtime'] = True
    # req_template['simulation_config']['duration'] = 60 * 60 * 4
    req_template['simulation_config']['duration'] = 60 * 15
    req_template['simulation_config']['start_time'] = 1374510600
    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'] = '_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62'  # 13
    # req_template['power_system_config']['Line_name'] = '_AAE94E4A-2465-6F5E-37B1-3E72183A4E44'  # New 8500
    req_template["application_config"]["applications"][0][
        'name'] = 'solar_forecasting_app'
    req_template['simulation_config']['start_time'] = timegm(
        strptime('2013-07-22 08:00:00 GMT', '%Y-%m-%d %H:%M:%S %Z'))

    app_config = {'run_freq': 60, 'run_on_host': False}
    app_config['run_realtime'] = req_template['simulation_config'][
        'run_realtime']
    print(app_config)

    req_template["application_config"]["applications"] = [{
        "name":
        "solar_forecasting_app",
        "config_string":
        json.dumps(app_config)
    }]

    print(req_template)
    # req_template['power_system_config']['Line_name'] = '_67AB291F-DCCD-31B7-B499-338206B9828F' # J1

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

    simulation_id = goss.get_response(goss_sim, simCfg13pv, timeout=10)
    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)
        model_mrid = req_template['power_system_config']['Line_name']
        gapps = GridAPPSD(simulation_id)
        solar_forecast = Solar_Forecast(simulation_id, gapps, model_mrid,
                                        1538484951, app_config)
        gapps.subscribe(listening_to_topic, solar_forecast)

        while True:
            time.sleep(0.1)
Ejemplo n.º 15
0
def _main():
    _log.debug("Starting application")
    print("Application starting-------------------------------------------------------")
    global message_period
    parser = argparse.ArgumentParser()
    parser.add_argument("simulation_id",
                        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request",
                        help="Simulation Request")
    parser.add_argument("--message_period",
                        help="How often the sample app will send open/close capacitor message.",
                        default=DEFAULT_MESSAGE_PERIOD)
    opts = parser.parse_args()
    listening_to_topic = simulation_output_topic(opts.simulation_id)
    message_period = int(opts.message_period)
    sim_request = json.loads(opts.request.replace("\'",""))
    model_mrid = sim_request['power_system_config']['Line_name']
    print("\n \n The model running is IEEE 9500-node with MRID:", model_mrid)
    
    _log.debug("Model mrid is: {}".format(model_mrid))
    gapps = GridAPPSD(opts.simulation_id, address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(), password=utils.get_gridappsd_pass())

    # Get measurement MRIDS for Loadbreakswitches in the feeder
    print('Get Measurement MRIDS for Loadbreakswitches.....')
    topic = "goss.gridappsd.process.request.data.powergridmodel"
    message = {
        "modelId": model_mrid,
        "requestType": "QUERY_OBJECT_MEASUREMENTS",
        "resultFormat": "JSON",
        "objectType": "LoadBreakSwitch"}     
    obj_msr_loadsw = gapps.get_response(topic, message, timeout=180)
    with open('measid_LoadbreakSwitch.json', 'w') as json_file:
        json.dump(obj_msr_loadsw, json_file)    

    # Get measurement MRIDS for kW consumptions at each node
    print('Get Measurement MRIDS for EnergyConsumers.....')
    message = {
        "modelId": model_mrid,
        "requestType": "QUERY_OBJECT_MEASUREMENTS",
        "resultFormat": "JSON",
        "objectType": "EnergyConsumer"}     
    obj_msr_demand = gapps.get_response(topic, message, timeout=180)

    # Get Eq. MRIDs of Loadbreakswitches
    print('Get Switches Information.....')    
    switches = get_switches_mrids(gapps, model_mrid)

    # Load demand and lineparameters
    with open('Demand9500.json', 'r') as read_file:
        demand = json.load(read_file)
    with open('LineData.json', 'r') as read_file:
        line = json.load(read_file)

    print("Initialize.....")
    toggler = SwitchingActions(opts.simulation_id, gapps, switches, \
    obj_msr_loadsw, obj_msr_demand, demand, line)
    print("Now subscribing....")
    gapps.subscribe(listening_to_topic, toggler)
    while True:
        time.sleep(0.1)
Ejemplo n.º 16
0
        "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":[{
            "message":{
                "forward_differences":[{
                    "object":"_792127B0-9B3E-43EC-9D23-FD46F5A2F20D",
                    "attribute":"Switch.open","value":1}],
                "reverse_differences":[{"object":"_792127B0-9B3E-43EC-9D23-FD46F5A2F20D",
                                        "attribute":"Switch.open",
                                        "value":0
                                       }]
            },
            "event_type":"ScheduledCommandEvent",
            "occuredDateTime": 1373814600,
            "stopDateTime": 1373817600}],
        
        "appId":""
    },
    "service_configs":[]
}
"""
gapps = GridAPPSD()

simulation = Simulation(gapps, run_config_9500)
simulation.start_simulation()
Ejemplo n.º 17
0
def _main():
    parser = argparse.ArgumentParser()
    parser.add_argument("simulation_id",
                        help="Simulation id to use for responses on the message bus.")
    parser.add_argument("request",
                        help="Simulation Request")
    # These are now set through the docker container interface via env variables or defaulted to
    # proper values.
    #
    # parser.add_argument("-u", "--user", default="system",
    #                     help="The username to authenticate with the message bus.")
    # parser.add_argument("-p", "--password", default="manager",
    #                     help="The password to authenticate with the message bus.")
    # parser.add_argument("-a", "--address", default="127.0.0.1",
    #                     help="tcp address of the mesage bus.")
    # parser.add_argument("--port", default=61613, type=int,
    #                     help="the stomp port on the message bus.")
    #
    opts = parser.parse_args()
    sim_output_topic = simulation_output_topic(opts.simulation_id)
    sim_input_topic = simulation_input_topic(opts.simulation_id)
    sim_request = json.loads(opts.request.replace("\'",""))
    model_mrid = sim_request["power_system_config"]["Line_name"]
    gapps = GridAPPSD(opts.simulation_id, address=utils.get_gridappsd_address(),
                      username=utils.get_gridappsd_user(), password=utils.get_gridappsd_pass())
    capacitors_dict = {}
    switches_dict = {}
    capacitors_meas_dict = {}
    switches_meas_dict = {}

    request = {
        "modelId": model_mrid,
        "requestType": "QUERY_OBJECT_DICT",
        "resultFormat": "JSON",
        "objectType": "LinearShuntCompensator"
        }

    response = gapps.get_response("goss.gridappsd.process.request.data.powergridmodel",request)
    for capacitor in response["data"]:
        capacitors_dict[capacitor["id"]] = capacitor

    request = {
        "modelId": model_mrid,
        "requestType": "QUERY_OBJECT_DICT",
        "resultFormat": "JSON",
        "objectType": "LoadBreakSwitch"
        }

    response = gapps.get_response("goss.gridappsd.process.request.data.powergridmodel",request)
    for switch in response["data"]:
        switches_dict[switch["id"]] = switch

    #print(capacitors_dict)
    #print(switches_dict)
    
    request = {"modelId": model_mrid,
               "requestType": "QUERY_OBJECT_MEASUREMENTS",
               "resultFormat": "JSON",
               "objectType": "LinearShuntCompensator"
               }
    
    response = gapps.get_response("goss.gridappsd.process.request.data.powergridmodel",request)
    for measurement in response["data"]:
        capacitors_meas_dict[measurement["measid"]] = measurement
        
    request = {"modelId": model_mrid,
               "requestType": "QUERY_OBJECT_MEASUREMENTS",
               "resultFormat": "JSON",
               "objectType": "LoadBreakSwitch"
               }
    
    response = gapps.get_response("goss.gridappsd.process.request.data.powergridmodel",request)
    for measurement in response["data"]:
        switches_meas_dict[measurement["measid"]] = measurement

    #print(capacitors_meas_dict)
    #print(switches_meas_dict)

    #capacitors_dict = get_capacitor_measurements(gapps, model_mrid)
    #switches_dict = get_switch_measurements(gapps, model_mrid)
    subscriber = SimulationSubscriber(opts.simulation_id, gapps, capacitors_dict, switches_dict, capacitors_meas_dict, switches_meas_dict)
    gapps.subscribe(sim_input_topic, subscriber)
    gapps.subscribe(sim_output_topic, subscriber)
    while True:
        time.sleep(0.1)
Ejemplo n.º 18
0
def main():
    # Switch for app being managed by platform vs running outside of it.
    IN_PLATFORM = False

    # Read configuration file.
    config = read_config('config.json')

    # Setup log.
    log = setup_log(config['LOG'])
    log.info('Log configured.')

    if IN_PLATFORM:
        # In docker-compose, use the service name.
        MYSQL_HOST = 'mysql-pyvvo'

        # When running inside the platform, use standard MySQL port.
        MYSQL_PORT = 3306

        # Get the gridappsd_address.
        gridappsd_address = utils.get_gridappsd_address()

        # Initialize argument parser.
        parser = argparse.ArgumentParser()

        # Get simulation ID.
        parser.add_argument("sim_id",
                            help="Simulation ID to send/receive "
                            "data/commands")

        # Get the simulation request so we can extract the model ID.
        parser.add_argument("sim_request",
                            help="Request sent to start "
                            "simulation.")

        # Extract arguments.
        args = parser.parse_args()
        log.debug('Arguments parsed.')

        # Get the topic for listening to simulation output.
        sim_out_topic = topics.fncs_output_topic(args.sim_id)

        # Get the topic for sending commands to simulation.
        sim_in_topic = topics.fncs_input_topic(args.sim_id)

        # Get the simulation request into a dictionary.
        sim_request = json.loads(args.sim_request.replace("\'", ""))

        # Extract the model ID. It's silly that they've labeled the model
        # "Line_name"
        model_id = sim_request["power_system_config"]["Line_name"]
        log.debug('Model MRID: {}'.format(model_id))

        # Extract the sim_id.
        sim_id = args.sim_id

    else:
        # For development, use this machine's internal IP.
        '''
        # The code below only works when run OUTSIDE of a container.
        import socket
        s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        s.connect(("8.8.8.8", 80))
        INTERNAL_IP = s.getsockname()[0]
        s.close()
        '''
        INTERNAL_IP = '192.168.0.26'

        MYSQL_HOST = INTERNAL_IP

        # MYSQL_PORT is different when running outside the platform.
        MYSQL_PORT = 3307

        # Define where we can connect to the platform externally.
        gridappsd_address = (INTERNAL_IP, 61613)

        # For now, not listening to a simulation topic.
        # NOTE: the sim_id below is for an offline simulation, so I
        # can grab timeseries information.
        sim_id = "293975150"
        sim_out_topic = None
        sim_in_topic = None

        # Initialize GridAPPSD object so we can pull the model ID.
        gridappsd_object = GridAPPSD(address=gridappsd_address)

        # Get the id for the 8500 node feeder.
        model_name = 'ieee8500'
        model_id = get_model_id(gridappsd_object=gridappsd_object,
                                model_name=model_name,
                                log=log)
        log.info('Retrieved model ID for {}.'.format(model_name))

    # Override the MySQL host and port.
    config['GLD-DB']['host'] = MYSQL_HOST
    config['GLD-DB']['port'] = MYSQL_PORT

    run(log=log,
        config=config,
        sim_id=sim_id,
        model_id=model_id,
        sim_in_topic=sim_in_topic,
        gridappsd_address=gridappsd_address,
        sim_out_topic=None)

    # Initialize GridAPPSD object.
    gridappsd_object = GridAPPSD(simulation_id=sim_id,
                                 address=gridappsd_address,
                                 username=utils.get_gridappsd_user(),
                                 password=utils.get_gridappsd_pass())

    # Grab measurement information
    load_meas = \
        query_and_parse(
            gridappsd_object,
            query_string=sparql_queries.LOAD_MEASUREMENTS_QUERY.format(fdrid=model_id),
            parse_function=sparql_queries.parse_load_measurements_query,
            log_string='Load Measurement', log=log
        )

    log.info('Terminating pyvvo...')
    sys.exit(0)
Ejemplo n.º 19
0
    class Listener(object):
        def on_message(self, headers, message):
            print("headers: {}\nmessage: {}".format(headers, message))

    def on_message(headers, message):
        print("Received: headers: {}\nmessage: {}".format(headers, message))

    # Handle python 3 not having input_raw function.
    try:
        get_input = raw_input
    except NameError:
        get_input = input

    print("Creating GridAPPSD object")
    gapps = GridAPPSD(stomp_address="127.0.0.1",
                      stomp_port=61613,
                      username='******',
                      password='******')

    print("Subscribing to /topic/foo")
    gapps.subscribe('/topic/foo', on_message)
    result = get_input(
        "Press enter to send json.dumps(dict(bim='bash')) to topic /topic/foo")
    print("Sending data")
    gapps.send('/topic/foo', json.dumps(dict(bim='bash')))
    sleep(1)

    get_input("Press enter to receive platform status")
    resp = gapps.get_platform_status()
    pprint(resp)

    get_input("Press enter to query model info")
Ejemplo n.º 20
0
if __name__ == '__main__':
    import os
    import shutil

    sensors = dict()
    opts = get_opts()

    if opts.simulation_id == '-9999':
        raise SystemExit

    user_options = opts.request['service_configs'][0]['user_options']
    service_id = "gridappsd-sensor-simulator"

    gapp = GridAPPSD(username=opts.username,
                     password=opts.password,
                     address=opts.address)

    read_topic = simulation_output_topic(opts.simulation_id)
    write_topic = service_output_topic(service_id, opts.simulation_id)

    log_file = "/tmp/gridappsd_tmp/{}/sensors.log".format(opts.simulation_id)
    if not os.path.exists(os.path.dirname(log_file)):
        os.makedirs(os.path.dirname(log_file))

    with open(log_file, 'w') as fp:
        logging.basicConfig(stream=fp, level=logging.INFO)
        logging.getLogger().info(
            f"read topic: {read_topic}\nwrite topic: {write_topic}")
        logging.getLogger().info(f"user options: {user_options}")
        run_sensors = Sensors(gapp,
if __name__ == '__main__':

    sensors = dict()
    opts = get_opts()

    if opts.simulation_id == '-9999':
        print('entering test mode with', opts)
        run_test('Input.csv', 'Output.csv', opts)
        raise SystemExit

    read_topic = simulation_output_topic(opts.simulation_id)
    write_topic = service_output_topic("sensors", opts.simulation_id)

    gapp = GridAPPSD(username=opts.username,
                     password=opts.password,
                     stomp_address=opts.stomp_address,
                     stomp_port=opts.stomp_port)

    sensor = Sensor(
        gapp,
        seed=opts.random_seed,
        nominal=opts.
        nominal,  # TODO (Craig, Tom, Andy F): these 4 parameters will need to be different for each sensor instance
        perunit_confidence95=opts.perunit_confidence,
        perunit_dropping=opts.perunit_dropping,
        interval=opts.interval,
        output_topic=write_topic)

    gapp.subscribe(read_topic, sensor.on_simulation_message)

    while True:
Ejemplo n.º 22
0
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)
Ejemplo n.º 23
0
def main():
    loglevel = logging.INFO
    logging.basicConfig(
        stream=sys.stdout,
        level=loglevel,
        format="%(asctime)s - %(name)s;%(levelname)s|%(message)s",
        datefmt="%Y-%m-%d %H:%M:%S")
    logging.getLogger('stomp.py').setLevel(logging.ERROR)
    _log = logging.getLogger(__name__)

    problems = utils.validate_gridappsd_uri()

    if problems:
        for p in problems:
            _log.error(p)
        sys.exit(1)

    if not os.path.isfile("/appconfig"):
        _log.error(
            "Invalid /appconfig reference...map the /appconfig to your container"
        )
        sys.exit(1)

    config = json.loads(open("/appconfig").read())

    if "id" not in config:
        _log.error("Invalid appconfig, must have a unique id set.")
        sys.exit(1)

    os.environ['GRIDAPPSD_APPLICATION_ID'] = config['id']

    appreg = None
    gap = None
    while True:

        try:
            if gap is None:
                gap = GridAPPSD()

        except ConnectionRefusedError:  # Python 3 specific error code
            _log.debug("Retry in 10 seconds")
            gap = appreg = None
            time.sleep(10)
        except (stomp.exception.ConnectFailedException, OSError):
            _log.debug("Connect failed Retry in 10 seconds")
            gap = appreg = None
            time.sleep(10)
        except KeyboardInterrupt:
            gap = appreg = None
            _log.info("Exiting app")
            break
        else:
            if appreg is None:

                def end_app():
                    sys.exit(0)

                # app_config_minimal = {
                #     'id': 'an-app-id',
                #     'execution_path': '/home/osboxes/git/gridappsd-python/testapp.sh'
                # }
                try:
                    appreg = ApplicationController(config, gridappsd=gap)
                    appreg.register_app(end_app)
                    _log.info('Application {} registered.'.format(
                        config['id']))
                except:
                    _log.exception(
                        "An unhandled exception occured retrying app")
                    appreg = None
                    gap = None
            else:
                if not appreg.heartbeat_valid:
                    appreg = None
                    gap = None

            time.sleep(2)
Ejemplo n.º 24
0
gridapspd_address = utils.get_gridappsd_address()
gridapspd_user = utils.get_gridappsd_user()
gridappsd_pass = utils.get_gridappsd_pass()

if not os.path.isfile("/appconfig"):
    _log.error("Invalid /appconfig reference...map the /appconfig to your container")
    sys.exit(1)

config = json.loads(open("/appconfig").read())
appreg = None
gap = None
while True:

    try:
        if gap is None:
            gap = GridAPPSD(username=gridapspd_user, password=gridappsd_pass,
                            address=utils.get_gridappsd_address())

    except ConnectionRefusedError:  # Python 3 specific error code
        _log.debug("Retry in 10 seconds")
        gap = appreg = None
        time.sleep(10)
    except (stomp.exception.ConnectFailedException, OSError):
        _log.debug("Connect failed Retry in 10 seconds")
        gap = appreg = None
        time.sleep(10)
    else:
        if appreg is None:
            def end_app():
                sys.exit(0)

            # app_config_minimal = {
Ejemplo n.º 25
0
logging.getLogger('stomp.py').setLevel(logging.WARNING)
_log = logging.getLogger("goss.__main__")


if __name__ == '__main__':

    parser = ArgumentParser()

    group = parser.add_mutually_exclusive_group(required=True)
    group.add_argument("-s", "--run-simulation", type=argparse.FileType('r'),
                       help="Start running a simulation from a passed simulation file.")

    opts = parser.parse_args()

    if opts.run_simulation:
        # Example of how to pause simulation
        def next_timestep(simulation, timestep):
            simulation.pause()
            sleep(1)
            simulation.resume()

        gappsd = GridAPPSD()
        run_args = yaml.safe_load(opts.run_simulation)

        # if wanting to use the above next_timestep function use this
        # instead of the one below.
        # simulation = gappsd.run_simulation(run_args, next_timestep)
        simulation = gappsd.run_simulation(run_args)
        simulation.simulation_main_loop()
Ejemplo n.º 26
0
            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)


#Connect to GridAPPS
gapps = GridAPPSD("('localhost', 61613)",
                  username='******',
                  password='******')

#Define topic and message for id query
topic = t.REQUEST_POWERGRID_DATA
message = {"requestType": "QUERY_MODEL_NAMES", "resultFormat": "JSON"}

#Query the model names. We know the mrid already, but this gives us our simulation id as well.
x = gapps.get_response(topic, message)
simulation_id = x["id"]
model_mrid = "_49AD8E07-3BF9-A4E2-CB8F-C3722F837B62"  #for 13 node feeder

# #Playing around with queries. This gets us object IDs from the 13 node model.
# topic = "goss.gridappsd.process.request.data.powergridmodel"
# message = {
#     "requestType": "QUERY_OBJECT_IDS",
def start(log_file,
          feeder_mrid,
          model_api_topic,
          cmpFlag=True,
          Ysys=None,
          Unsupported=None):
    global logfile
    logfile = log_file

    if cmpFlag:
        print(
            "\nSWITCHING_EQUIPMENT_VALIDATOR starting!!!-----------------------------------------"
        )
        print(
            "\nSWITCHING_EQUIPMENT_VALIDATOR starting!!!-----------------------------------------",
            file=logfile)

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

    gapps = GridAPPSD()

    sparql_mgr = SPARQLManager(gapps, feeder_mrid, model_api_topic)

    if cmpFlag:
        ysparse, nodelist = sparql_mgr.ybus_export()

        idx = 1
        nodes = {}
        for obj in nodelist:
            nodes[idx] = obj.strip('\"')
            idx += 1
        #print(nodes)

        Ybus = {}
        for obj in ysparse:
            items = obj.split(',')
            if items[0] == 'Row':
                continue
            if nodes[int(items[0])] not in Ybus:
                Ybus[nodes[int(items[0])]] = {}
            Ybus[nodes[int(items[0])]][nodes[int(items[1])]] = complex(
                float(items[2]), float(items[3]))
        #print(Ybus)

        # list of lists for the tabular report
        report = []
    else:
        Ybus = None

    SwitchingEquipment_switches = validate_SwitchingEquipment_switches(
        sparql_mgr, Ybus, cmpFlag, Ysys)
    if cmpFlag:
        if SwitchingEquipment_switches > 0:
            count = greenCount + yellowCount + redCount
            VI = float(count - redCount) / float(count)
            report.append([
                SwitchingEquipment_switches, "{:.4f}".format(VI), greenCount,
                yellowCount, redCount
            ])
        else:
            report.append([SwitchingEquipment_switches])

        print('\n', flush=True)
        print(tabulate(report,
                       headers=[
                           "# Switches", "VI",
                           diffColor(0, True),
                           diffColor(1, True),
                           diffColor(2, True)
                       ],
                       tablefmt="fancy_grid"),
              flush=True)
        print('\n', file=logfile)
        print(tabulate(report,
                       headers=[
                           "# Switches", "VI",
                           diffColor(0, False),
                           diffColor(1, False),
                           diffColor(2, False)
                       ],
                       tablefmt="fancy_grid"),
              file=logfile)

        print('\nSWITCHING_EQUIPMENT_VALIDATOR DONE!!!', flush=True)
        print('\nSWITCHING_EQUIPMENT_VALIDATOR DONE!!!', file=logfile)