<jupyter_start><jupyter_text>Lesson 3.0. Putting it all together: Hackathon!! Objective: Find capacitor settings that minimize total substation real power (measuring line `l115`)Rules of Engagement: You will complete the following steps below in order in the code blocks provided. Each step should be commented with proper documentation of your code and from which lesson you pulled (such as "pulled code from L2.6, S4.4 - Obtain measurement mRIDs").Cheat Sheet: GridAPPS-D Sample App - opens and closes capacitors every 5 messages: https://github.com/GRIDAPPSD/gridappsd-sample-app/blob/master/sample_app/runsample.py  1. Import Libraries<jupyter_code>import argparse
import json
import logging
import sys
import time
import pytz
import stomp

import numpy as np
from gridappsd import GridAPPSD, utils, DifferenceBuilder
from gridappsd import topics as t
from gridappsd.topics import simulation_input_topic, simulation_output_topic<jupyter_output><empty_output><jupyter_text>2. Connect to GridAPPS-D Simulation<jupyter_code>viz_simulation_id = "1768890986"

gapps = GridAPPSD(viz_simulation_id, "('localhost', 61613)", username='system', password='<PASSWORD>')<jupyter_output><empty_output><jupyter_text>3. Obtain Equipment mRIDs<jupyter_code># model ID from run123demo.py
model_mrid = "_C1C3E687-6FFD-C753-582B-632A27E28507"
topic = "goss.gridappsd.process.request.data.powergridmodel"

message = {
    "requestType": "QUERY_OBJECT_IDS",
    "resultFormat": "JSON",
    "modelId": model_mrid,
    "objectType": "LinearShuntCompensator"
} 

gapps.get_response(topic, message)
response_obj = gapps.get_response(topic, message)
ShuntComp_id = response_obj['data']['objectIds']
print(ShuntComp_id)
topic = "goss.gridappsd.process.request.data.powergridmodel"
message = {
    "requestType": "QUERY_OBJECT_DICT",
    "resultFormat": "JSON",
    "modelId": model_mrid,
    "objectType": "ACLineSegment"
} 

 

gapps.get_response(topic, message)
response_obj = gapps.get_response(topic, message)
Line_info = response_obj['data']
Line_l115_mrid = [k['IdentifiedObject.mRID'] for k in Line_info if k['IdentifiedObject.name'] == 'l115']<jupyter_output><empty_output><jupyter_text>4. Obtain Measurement mRIDs<jupyter_code>message = {
    "modelId": model_mrid,
    "requestType": "QUERY_OBJECT_MEASUREMENTS",
    "resultFormat": "JSON",
    "objectId": Line_l115_mrid[0]
}

response_obj = gapps.get_response(t.REQUEST_POWERGRID_DATA,message)

measurements_obj = response_obj["data"]

VA_obj = [k for k in measurements_obj if k['type'] == 'VA']

VA_obj_a = [k for k in measurements_obj if k['type'] == 'VA' and k['phases'] == 'A']
VA_obj_b = [k for k in measurements_obj if k['type'] == 'VA'and k['phases'] == 'B']
VA_obj_c = [k for k in measurements_obj if k['type'] == 'VA' and k['phases'] == 'C']

VA_obj_mrid_a = [k['measid'] for k in VA_obj_a]
VA_obj_mrid_b = [k['measid'] for k in VA_obj_b]
VA_obj_mrid_c = [k['measid'] for k in VA_obj_c]<jupyter_output><empty_output><jupyter_text>5. Create Simulation Subscription Method or FunctionNote: Instead chose to query Timeseries Database<jupyter_code>def my_timeseries_query(VA_obj_mrid_a, VA_obj_mrid_b, VA_obj_mrid_c):
    topic = "goss.gridappsd.process.request.data.timeseries" # Specify Timeseries API GridAPPS-D topic
    
    # Get time that capacitor diff message was sent
    diff_message_time = int(time.time())-60
    
    # Wait 10 seconds for cap command to execute, new measurement to be created, 
    # then measurement saved to Timeseries Influx DB. This takes a bit of time, so wait to query
    #time.sleep(20)
    

    
    # Query for a particular set of measurments, starting at time cap was commanded
    message = {
        "queryMeasurement":"simulation",
        "queryFilter":{"simulation_id": viz_simulation_id,
                       "startTime": diff_message_time, 
                       "measurement_mrid":VA_obj_mrid_a},
        "responseFormat":"JSON"
    } 

    
    VA_measurement_a = gapps.get_response(topic, message)  
    VA_measurement_obj_a = VA_measurement_a['data']
    


    # Query for a particular set of measurments
    message = {
        "queryMeasurement":"simulation",
        "queryFilter":{"simulation_id": viz_simulation_id,
                       "startTime": diff_message_time, 
                       "measurement_mrid":VA_obj_mrid_b},
        "responseFormat":"JSON"
    } 

    VA_measurement_b = gapps.get_response(topic, message)  
    VA_measurement_obj_b = VA_measurement_b['data']

   
    # Query for a particular set of measurments
    message = {
        "queryMeasurement":"simulation",
        "queryFilter":{"simulation_id": viz_simulation_id,
                       "startTime": diff_message_time, 
                       "measurement_mrid":VA_obj_mrid_c},
        "responseFormat":"JSON"
    } 

    VA_measurement_c = gapps.get_response(topic, message)  
    VA_measurement_obj_c = VA_measurement_c['data']

    VA_meas_mag_a = []
    VA_meas_ang_a = []
    for index in VA_measurement_obj_a:
        magnitude = index['magnitude']
        angle = index['angle']
        VA_meas_mag_a.append(magnitude)
        VA_meas_ang_a.append(angle)

    VA_meas_mag_b = []
    VA_meas_ang_b = []
    for index in VA_measurement_obj_b:
        magnitude = index['magnitude']
        angle = index['angle']
        VA_meas_mag_b.append(magnitude)
        VA_meas_ang_b.append(angle)

    VA_meas_mag_c = []
    VA_meas_ang_c = []
    for index in VA_measurement_obj_c:
        magnitude = index['magnitude']
        angle = index['angle']
        VA_meas_mag_c.append(magnitude)
        VA_meas_ang_c.append(angle)
        
    P_a = VA_meas_mag_a[-1]*np.cos(VA_meas_ang_a[-1]*np.pi/180)
    P_b = VA_meas_mag_b[-1]*np.cos(VA_meas_ang_b[-1]*np.pi/180)
    P_c = VA_meas_mag_c[-1]*np.cos(VA_meas_ang_c[-1]*np.pi/180)
    P = P_a + P_b + P_c

    return P<jupyter_output><empty_output><jupyter_text>6. Run Optimization, Get Results [Link to list of control attributes for difference message](Lesson%202.6.%20Simulation%20API%20-%20Publishing%20and%20Subscribing.ipynb3.2.-Obtain-Equipment-Control-mRIDs)<jupyter_code># Get control attribute from Lesson 2.6, Section 3.2

input_topic = simulation_input_topic(viz_simulation_id)

my_diff_build = DifferenceBuilder(viz_simulation_id)

P_list=[]
P = my_timeseries_query(VA_obj_mrid_a, VA_obj_mrid_b, VA_obj_mrid_c)
P_list.append(P)

# Open all Caps
for k in ShuntComp_id:
    my_diff_build.add_difference(k, "ShuntCompensator.sections", 0, 1) # status change for capacitor given by cap_mrid
    message = my_diff_build.get_message()
    gapps.send(input_topic,message)

time.sleep(60)

P = my_timeseries_query(VA_obj_mrid_a, VA_obj_mrid_b, VA_obj_mrid_c)
P_list.append(P)
    
for k in ShuntComp_id:
    my_diff_build.add_difference(k, "ShuntCompensator.sections", 1, 0) # status change for capacitor given by cap_mrid
    message = my_diff_build.get_message()
    gapps.send(input_topic,message)
    
time.sleep(60)

P = my_timeseries_query(VA_obj_mrid_a, VA_obj_mrid_b, VA_obj_mrid_c)
P_list.append(P)

P_max = max(P_list)
P_min = min(P_list)
print("Worst value is", P_max)
print(".....")
print("Best value is", P_min)<jupyter_output>Worst value is 3745407.546634
.....
Best value is 3694657.0859089997
{'fraction_non_alphanumeric': 0.1075565361279647, 'fraction_numerical': 0.02027027027027027, 'mean_word_length': 4.274909090909091, 'pattern_counts': {'":': 30, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '546015', 'license': 'BSD-2-Clause', 'n_tokens_mistral': 2715, 'n_tokens_neox': 2567, 'n_words': 534, 'path': 'Completed Hackathon Code.ipynb', 'repo_name': 'GRIDAPPSD/gridappsd-hackathon', 'size': '7249'}