<jupyter_start><jupyter_text>Micropolis Power Network Generation Buses<jupyter_code>micropolis_allbuses = gpd.read_file(micropolis_folder/'all_buses.shp', encoding="utf-8")
micropolis_buses = micropolis_allbuses[micropolis_allbuses.layer != 'Substation buses']
micropolis_buses['x'] = micropolis_buses.centroid.x
micropolis_buses['y'] = micropolis_buses.centroid.y

bus_vn_kv_dict = {'Feeder1 buses': 13.8,
                  'Overhead distribution buses': 13.8,
                  'Feeder2_buses': 13.8,
                  'Underground distribution buses': 13.8,
                  }

micropolis_buses["vn_kv"] = micropolis_buses["layer"].apply(lambda x: bus_vn_kv_dict.get(x))

del micropolis_buses['path']
del micropolis_buses['geometry']
#micropolis_buses.FID = micropolis_buses.FID.astype(int)
micropolis_buses.head()
pn = pp.create_empty_network(
    name="sample_network", f_hz=50.0, sn_mva=1, add_stdtypes=True
)
buses = dict()
for index, row in micropolis_buses.iterrows():
    x, y = row.x, row.y
    buses[f'bus{row.FID}'] = pp.create_bus(pn, vn_kv=row['vn_kv'], name=f'P_B{row.FID}', geodata=(x, y), type="b")
    
#Substation bus
buses[f'bus_Substation_ext'] = pp.create_bus(pn, vn_kv = 138, name=f'P_B1000', geodata=(4911,3250), type="b")
buses[f'bus_Substation'] = pp.create_bus(pn, vn_kv = 13.8, name=f'P_B1001', geodata=(4911,3180), type="b")
pn.bus.tail()<jupyter_output><empty_output><jupyter_text>External grid<jupyter_code>ext_grids = dict()

pn.ext_grid_geodata = pd.DataFrame(columns = ['x', 'y'])
ext_grids['ext_grid1'] = pp.create_ext_grid(pn, buses['bus_Substation_ext'], name="P_EG1", s_sc_max_mva=1000, rx_max=.1, x0x_max=1.0, r0x0_max=0.1)
pn.ext_grid_geodata = pn.ext_grid_geodata.append({'x': 4911, 'y': 3325}, ignore_index = True)<jupyter_output><empty_output><jupyter_text>Lines, Transformers, and Switches<jupyter_code>micropolis_links = gpd.read_file(micropolis_folder/'combined_lines.shp', encoding="utf-8")
micropolis_links.id = micropolis_links.id.values.astype(int)<jupyter_output><empty_output><jupyter_text>Lines<jupyter_code>micropolis_lines = micropolis_links[micropolis_links.layer.isin(['Feeder1_lines', 'Feeder2_lines', 'Overhead lines', 'Underground lines'])]
micropolis_lines.head()
def compute_distance(bus1, bus2):
    x1 = micropolis_buses[micropolis_buses.FID == int(bus1)].x.item()
    x2 = micropolis_buses[micropolis_buses.FID == int(bus2)].x.item()
    y1 = micropolis_buses[micropolis_buses.FID == int(bus1)].y.item()
    y2 = micropolis_buses[micropolis_buses.FID == int(bus2)].y.item()
    dist = math.hypot(x2 - x1, y2 - y1)/1000
    return dist
pp.create_std_type(pn, {'c_nf_per_km': 210, 
                        'r_ohm_per_km': 0.642,
                        'x_ohm_per_km': 0.083, 
                        'max_i_ka': 0.142,
                        'type': 'ol'}, 
                     name="micropolis_line")

micropolis_line = 'micropolis_line'
#micropolis_line  = '15-AL1/3-ST1A 0.4'
for i, row in micropolis_lines.iterrows():
    pp.create_line(pn, 
                    from_bus = buses['bus{}'.format(row['from'])], 
                    to_bus = buses['bus{}'.format(row['to'])], 
                    length_km = compute_distance(row['from'], row['to']), 
                    std_type=micropolis_line, 
                    name= 'P_L{}'.format(row['id']), 
                    #index = i,
                    )
pn.line.head()<jupyter_output><empty_output><jupyter_text>Switches<jupyter_code>micropolis_switches = micropolis_links[micropolis_links.layer == 'Switches']
micropolis_switches.head()
switches = dict()
for i, row in micropolis_switches.iterrows():
    #print(row)
    #create switch buses
    switch_bus = micropolis_buses[micropolis_buses['FID'] == int(row['from'])]
    #print(switch_bus)
    buses['busS{}'.format(row['from'])] = pp.create_bus(pn, 
                                                vn_kv=switch_bus['vn_kv'], 
                                                name='P_B{}S'.format(row['id']), 
                                                geodata=(switch_bus.x.item(), switch_bus.y.item()), 
                                                type="b")
    
    #create switches
    switches['switch{}'.format(row.id)] = pp.create_switch(
        pn, 
        buses['bus{}'.format(row['from'])], 
        buses['busS{}'.format(row['from'])], 
        et="b", 
        type="CB", 
        closed=True, 
        name="P_S{}".format(row['id'])
    )

    #switch_bus to bus line
    pp.create_line(pn, 
                    from_bus = buses['busS{}'.format(row['from'])], 
                    to_bus = buses['bus{}'.format(row['to'])], 
                    length_km = compute_distance(row['from'], row['to']), 
                    std_type=micropolis_line, 
                    name= 'P_L{}S'.format(row['id']),
                    )
#Substation breakers
switches['switch_Feeder1'] = pp.create_switch(
        pn, 
        buses['bus_Substation'], 
        buses['bus0'], 
        et="b", 
        type="CB", 
        closed=True,
        name="P_S2001"
)
switches['switch_Feeder2'] = pp.create_switch(
        pn, 
        buses['bus_Substation'], 
        buses['bus60'], 
        et="b", 
        type="CB", 
        closed=True, 
        name="P_S2002"
)
pn.switch.head()
micropolis_offswitches = micropolis_links[micropolis_links.layer == 'Off Switch']
micropolis_offswitches.head()
for i, row in micropolis_offswitches.iterrows():

    #create switch buses
    switch_bus = micropolis_buses[micropolis_buses['FID'] == int(row['from'])]
    buses['busS{}'.format(row['from'])] = pp.create_bus(pn, 
                                                vn_kv=switch_bus['vn_kv'], 
                                                name='P_B{}S'.format(row['id']), 
                                                geodata=(switch_bus.x.item(), switch_bus.y.item()), 
                                                type="b")
    
    #create switches
    switches['switch{}'.format(row.id)] = pp.create_switch(
        pn, 
        buses['bus{}'.format(row['from'])], 
        buses['busS{}'.format(row['from'])], 
        et="b", 
        type="CB", 
        closed=False, 
        name="P_S{}".format(row['id'])
    )

    #switch_bus to bus line
    pp.create_line(pn, 
                    from_bus = buses['busS{}'.format(row['from'])], 
                    to_bus = buses['bus{}'.format(row['to'])], 
                    length_km = compute_distance(row['from'], row['to']), 
                    std_type=micropolis_line, 
                    name= 'P_L{}S'.format(row['id']),
                    )<jupyter_output><empty_output><jupyter_text>Loads<jupyter_code>micropolis_buildings = gpd.read_file(micropolis_folder/'buildings_v1.shp', encoding="utf-8")
print(micropolis_buildings.shape)
micropolis_buildings.head(2)
micropolis_hub_dist = gpd.read_file(micropolis_folder/'Hub distance.shp', encoding="utf-8")
micropolis_hub_dist1 = micropolis_hub_dist.loc[:, ['ID', 'HubName', 'HubDist']]
micropolis_buildings= pd.merge(micropolis_buildings, micropolis_hub_dist1, left_on = 'ID', right_on = 'ID', how = 'inner')
#del micropolis_buildings['geometry']
print(micropolis_buildings.shape)
micropolis_buildings.head(2)
micropolis_power_details = pd.read_csv(micropolis_folder/'building_power_details.csv')
micropolis_power_details.head()
micropolis_power_details.voltage.unique()
micropolis_buildings= pd.merge(micropolis_buildings, micropolis_power_details, left_on = 'FACILITY_T', right_on = 'building', how = 'inner')
del micropolis_buildings['building']
print(micropolis_buildings.shape)
micropolis_buildings.head()
switch_list = list(micropolis_buses.Switches.unique())
#phase_list = [1,2,3]
phase_loads = [0,0,0]
phase_dict = dict()

for switch_comb in switch_list:
    #print(switch_comb)
    #phase_dict[switch] = secrets.choice(phase_list)
    buses_using_switch_comb = list(micropolis_buses[micropolis_buses['Switches'] == switch_comb]['FID'].unique())
    buses_using_switch_comb = [str(bus) for bus in buses_using_switch_comb]
    #print(buses_using_switch_comb)
    buildings_using_switch_comb_1p = micropolis_buildings.loc[(micropolis_buildings['HubName'].isin(buses_using_switch_comb)) & (micropolis_buildings['phases'] == 1), :]
    #print(buildings_using_switch_comb_1p)
    tot_load_on_switch_comb = buildings_using_switch_comb_1p['Pabc'].sum()
    #print(tot_load_on_switch_comb)

    phase_to_load = phase_loads.index(min(phase_loads))
    phase_loads[phase_to_load] += tot_load_on_switch_comb
    phase_dict[switch_comb] = phase_to_load

print(phase_loads)
print(phase_dict)
micropolis_buildings.Pabc.sum()
pn.loads_geodata = pd.DataFrame(columns = ['x', 'y'])
sym_loads = dict()

for i, row in micropolis_buildings.iterrows():
    coord = row['Centroid']
    x, y = coord.split(',')
    x, y = float(x), float(y)

    load_switch = micropolis_buses[micropolis_buses.FID == int(row['HubName'])].Switches.item()
    phase = phase_dict[load_switch]

    buses[f'busL{row.ID}'] = pp.create_bus(pn, 
                                          vn_kv=row['voltage']/1000, 
                                          name=f'P_B{row.ID}L', 
                                          geodata=(x, y), 
                                          type="b")

    pp.create_transformer_from_parameters(pn, 
                                        hv_bus=buses[f'bus{row.HubName}'], 
                                        lv_bus=buses[f'busL{row.ID}'], 
                                        sn_mva= 1.2*row['Pabc'], 
                                        vn_hv_kv= 13.8, 
                                        vn_lv_kv= row['voltage']/1000, 
                                        vk_percent= 10.1, 
                                        vkr_percent= 0.266, 
                                        pfe_kw= 0, 
                                        i0_percent= 0.05, 
                                        shift_degree= 150, 
                                        tap_side= 'hv',
                                        tap_neutral= 0,
                                        tap_min= -2, 
                                        tap_max= 2,
                                        tap_step_degree= 0,
                                        tap_step_percent= 2.5,
                                        tap_phase_shifter= False,
                                        index=pp.get_free_id(pn.trafo)+1,
                                        name = "P_TF{}".format(row.ID)
                                      )
    
    pp.create_load(pn, 
                    buses[f'busL{row.ID}'], 
                    p_mw=row['Pabc']/1000, 
                    q_mvar= 0.2 * row['Pabc']/1000, 
                    name='P_LO{}'.format(row['ID']))
    pn.loads_geodata = pn.loads_geodata.append({'x': x, 'y': y}, ignore_index = True)
        
pp.create_transformer_from_parameters(pn, 
                                      hv_bus=buses['bus_Substation_ext'], 
                                      lv_bus=buses['bus_Substation'], 
                                      sn_mva= 25*1.2, 
                                      vn_hv_kv= 138, 
                                      vn_lv_kv= 13.8, 
                                      vk_percent= 12, 
                                      vkr_percent= 0.41, 
                                      pfe_kw= 14, 
                                      i0_percent= 0, 
                                      shift_degree= 150, 
                                      tap_side= 'hv',
                                      tap_neutral= 0,
                                      tap_min= -9, 
                                      tap_max= 9,
                                      tap_step_degree= 0,
                                      tap_step_percent= 1.5,
                                      tap_phase_shifter= False,
                                      index=pp.get_free_id(pn.trafo)+1,
                                      name = "P_TF0"
                                    )
# Pump
pumps = dict()
pp.create_motor(pn, buses['busL705'], pn_mech_mw=0.23, cos_phi=0.8, name="P_MP1")
pp.create_motor(pn, buses['busL705'], pn_mech_mw=0.23, cos_phi=0.8, name="P_MP2")
pp.create_motor(pn, buses['busL705'], pn_mech_mw=0.23, cos_phi=0.8, name="P_MP3")

pn.pumps_geodata = pd.DataFrame(columns = ['x', 'y'])
pn.pumps_geodata = pn.pumps_geodata.append({'x': 3440, 'y': 6350}, ignore_index = True)
pn.pumps_geodata = pn.pumps_geodata.append({'x': 3465, 'y': 5870}, ignore_index = True)
pn.pumps_geodata = pn.pumps_geodata.append({'x': 3380, 'y': 5895}, ignore_index = True)
pp.diagnostic(pn)
pp.to_json(pn, "../../infrarisk/data/networks/micropolis/power/power.json")
pn_sim = pp.runpp(pn)
stop
import pandapower as pp
import pandapower.plotting as pandaplot
import matplotlib.pyplot as plt
pn = pp.from_json("../../infrarisk/data/networks/micropolis/power/power.json", convert=True)
options = {
        "bus_size": 0.25,
        "plot_loads": True,
        "library": "networkx",
        "bus_color": "indigo",
        "switch_color": 'green',
        "trafo_color": 'royalblue',
        "load_size": 0.5, 
        "show_plot": True,
        "scale_size": True,
        "trafo_size": 0.25
    }
plt.figure(1, figsize=(50, 35))
pandaplot.simple_plot(pn, **options)
import copy

pn2= copy.deepcopy(pn)
# pn2.switch.at[switches['switch302'], "closed"] = False
# pn2.switch.at[switches['switch305'], "closed"] = False
# pn2.switch.at[switches['switch65'], "closed"] = False
#pn.bus[pn.bus.name== 'P_B247']['in_service'] = False
#pn.bus.at[buses['bus247'], "in_service"] = True
#pp.set_element_status(pn, 'P_B247', True)
#pn2.line.at[38, "in_service"] = False
pn2.bus.at[1087, "in_service"] = False
#print(pn2.line.at[38, "in_service"])
print(pn2.bus.at[1087, "in_service"])
#pn2.line.name == 'P_L57'
pp.runpp(pn2)
pn2.load['total_consumption_ratio'] = pn2.res_load.p_mw/pn2.load.p_mw
#pn.asymmetric_load.head()
pn2.load.total_consumption_ratio.unique()
pn2.load['total_consumption_ratio'] = pn2.res_load.p_mw/pn2.load['p_mw']
pn2.load.head()
print(pn2.res_load.p_mw.unique())
print('Total single phase demand: ', round(pn2.res_load.sum(axis = 1).sum(),2), 'mw', '; Total three phase demand: ', round(pn2.res_asymmetric_load_3ph.sum(axis = 1).sum(),2), 'mw')
micropolis_buildings['power_consump_ratio'] = 'NA'
for index, row in micropolis_buildings.iterrows():
    micropolis_buildings.loc[index, 'power_consump_ratio'] = pn2.load[pn.load['name'] == 'P_LO{}'.format(row['ID'])]['total_consumption_ratio'].item()
micropolis_buildings.head(1)
micropolis_buildings.power_consump_ratio.unique()
#plotting
fig, ax = plt.subplots(figsize= (20,20)) 
micropolis_links.plot(ax = ax)
micropolis_hub_dist.plot(ax = ax)
micropolis_buildings.plot(ax = ax,
                          column='power_consump_ratio',
                          cmap='viridis_r',
                          categorical = False,
                          legend=True,
                          label = 'Consump'
)

ax.set(title='Building power consumption ratio')
ax.set_axis_off()
pn2.res_load.p_mw.hist()
pump_index = pn2.motor[pn2.motor.name == 'P_MP1'].index.item()
pn2.res_motor.iloc[pump_index].p_mw
pn2.res_motor.p_mw<jupyter_output><empty_output><jupyter_text>Micropolis Traffic Network Generation<jupyter_code>nodes = gpd.read_file(micropolis_folder/'transpo_junctions.shp', encoding="utf-8")
nodes.id = 'T_J' + nodes.id.astype(str)
nodes['X'] = round(nodes.centroid.x, 0)
nodes['X'] = nodes['X'].astype(int)
nodes['Y'] = round(nodes.centroid.y,0)
nodes['Y'] = nodes['Y'].astype(int)
nodes.head()
#del nodes['geometry']
#Trip generation
generate_dict = {
    'Out-City': {'Out-City': [0, 100, 200],
                 'Residentia': [0, 0, 10, 20], 
                 'CBD': [0, 100, 200], 
                 'Outskirts': [0, 10, 15], 
                 'Industrial': [0, 100, 200], 
                 'Indus/CBD': [0, 100, 200]}, 
    'Residentia': {'Out-City': [0, 5, 10], 
                   'Residentia': [0, 0, 5], 
                   'CBD': [0, 10, 20], 
                   'Outskirts': [0, 5, 10], 
                   'Industrial': [0, 20, 50], 
                   'Indus/CBD': [0, 20, 30]}, 
    'CBD': {'Out-City': [0, 50, 100, 150], 
            'Residentia': [0, 5, 10], 
            'CBD': [0, 25, 50, 100], 
            'Outskirts': [0, 10, 20], 
            'Industrial': [0, 50 ,100], 
            'Indus/CBD': [0, 25, 50 , 100]}, 
    'Outskirts': {'Out-City': [0, 5, 10], 
                'Residentia': [0, 0, 5], 
                'CBD': [0, 10, 20],
                'Outskirts': [0, 0 , 10], 
                'Industrial': [0, 10, 20], 
                'Indus/CBD': [0, 10, 20]}, 
    'Industrial': {'Out-City': [0, 100, 150],
                   'Residentia': [0, 0, 5, 10], 
                   'CBD': [0, 50, 100], 
                   'Outskirts': [0, 0, 5, 10], 
                   'Industrial': [0, 100, 150], 
                   'Indus/CBD': [0, 50, 100]},
    'Indus/CBD': {'Out-City': [0, 50, 100], 
                  'Residentia': [0, 0, 5], 
                  'CBD': [0, 25, 50], 
                  'Outskirts': [0, 0, 5], 
                  'Industrial': [0, 50, 100], 
                  'Indus/CBD': [0, 25, 50]}
}
#new attractor columns
attractor_nodes = nodes[nodes['Attractor'] == 1].id.unique()
for col in attractor_nodes:
    nodes[col] = 'NA'
random.seed(30)
for origin_index, row in nodes.iterrows():
    origin_category = row['Category']
    for attractor_index, attractor in enumerate(attractor_nodes):
        attractor_category = nodes[nodes['id'] == attractor]['Category'].item()
        if row['id'] != attractor:
            nodes.loc[origin_index, attractor] = random.choice(generate_dict[origin_category][attractor_category])
        else:
            nodes.loc[origin_index, attractor] = 0
    print(origin_index, end = ", ")
nodes.head()
total_flows = 0
for index, row in nodes.iterrows():
    print('Origin  {}'.format(row['id']))

    for i, attractor in enumerate(attractor_nodes):
        print('    {0} :    {1};'.format(attractor, nodes.loc[index, attractor]), end = '')
        total_flows += nodes.loc[index, attractor]
        
        if (i+1) % 5 == 0:
            print('')
    print('\n')
print(total_flows)
#Node	X	Y	;
node_final = nodes[['id', 'X', 'Y']]
node_final.columns = ['Node', 'X', 'Y']
node_final.to_csv('../../infrarisk/data/networks/micropolis/transportation/nodes_final.csv', sep = '\t', index = False, line_terminator='\t;\n')
node_final.head()<jupyter_output><empty_output><jupyter_text>Links<jupyter_code>links = gpd.read_file(micropolis_folder/'transpo_links.shp', encoding="utf-8")
links['from'] = 'T_J' + links['from'].astype(str)
links.to = 'T_J' + links.to.astype(str)
links.Distance = links.Distance/1000
del links['geometry']
links.head()
links_reverse = links.copy(deep=True)
links_reverse['id'] = range(len(links.id) + 1, 2*len(links.id) + 1)
links_reverse['from'] = links['to']
links_reverse['to'] = links['from']

links = links.append(links_reverse)
links.id = 'T_L' + links['id'].astype(str)
links.head()
link_dict = {
    'Arterial': {'Capacity': 900*3, 'Free Flow Speed': 50}, 
    'Subarteria': {'Capacity': 800*3, 'Free Flow Speed': 30},
    'Collectors': {'Capacity': 1500, 'Free Flow Speed': 25}
}
delay_dict = {
    'Out-City': 0, 
    'Residentia': 0.5, 
    'CBD': 2, 
    'Outskirts': 0.5, 
    'Industrial': 2,
    'Indus/CBD': 2
}

links['Capacity'] = links["Category"].apply(lambda x: link_dict.get(x)['Capacity'])
links['Free Flow Speed'] = links["Category"].apply(lambda x: link_dict.get(x)['Free Flow Speed'])
links['Free Flow Time1'] = 60*links['Distance']/ links['Free Flow Speed']

links['start_junc_type'] = 'NA'
for index, row in links.iterrows():
    links.loc[index, 'start_junc_type'] = nodes[nodes['id'] == row['from']].Category.values[0]

 
links['Delay'] = links["start_junc_type"].apply(lambda x: delay_dict.get(x))  
links['Free Flow Time'] = round(links['Free Flow Time1'] + links['Delay'],2)

links['B'] = 0.15
links['Power'] = 4
links['Speed limit'] = 0
links['Toll'] = 0
links['Type'] = 1

def find_number(text):
    num = re.findall(r'[0-9]+',text)
    return " ".join(num)


links_final = links[['from', 'to', 'Capacity', 'Distance', 'Free Flow Time', 	'B', 'Power', 'Speed limit', 'Toll', 'Type', 'id']]
links_final.columns = ['Init node',	'Term node', 'Capacity', 'Length', 'Free Flow Time',	'B', 'Power', 'Speed limit', 'Toll', 'Type', 'id']

links_final['sort_index'] = links_final['Init node'].apply(lambda x: find_number(x)).astype(int)
links_final = links_final.sort_values('sort_index')
del links_final['sort_index']

links_final.to_csv('../../infrarisk/data/networks/micropolis/transportation/links_final.csv', sep = '\t', index = False, line_terminator='\t;\n')
math.cos(31.8, )<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15789221097086914, 'fraction_numerical': 0.02937083073379085, 'mean_word_length': 2.411591355599214, 'pattern_counts': {'":': 10, '<': 33, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '276581', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 7789, 'n_tokens_neox': 7014, 'n_words': 1128, 'path': 'notebooks/tests/micropolis_shapefiles_balanced.ipynb', 'repo_name': 'srijithbalakrishnan/dreaminsg-integrated-model', 'size': '20837'}