Skip to content

Commit

Permalink
Implementação para alocação de EnergyMeters (#31)
Browse files Browse the repository at this point in the history
Implementando alterações nos arquivos Line, Utils e Core para
possibilitar a alocação de EnergyMeters nas chaves de MT.
  • Loading branch information
PauloRadatz authored May 28, 2024
2 parents e8b2f38 + 23fb292 commit c08739f
Show file tree
Hide file tree
Showing 77 changed files with 316 additions and 235 deletions.
2 changes: 1 addition & 1 deletion bdgd_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@

if __name__ == '__main__':

bdgd.run(r"C:\Users\ADERALDO\Desktop\TCC BDGB\CRELUZ-D_598_2022-12-31_V11_20230831-0921.gdb", feeder="1_3PAS_1")
bdgd.run(r"C:\Raphael_Projetos\BDGD_tools_RTS_fork\bdgd-tools-RTS\bdgd_tools\sample\raw\aneel\CRELUZ-D_598_2022-12-31_V11_20230831-0921.gdb", feeder="1_3PAS_1")
36 changes: 29 additions & 7 deletions bdgd_tools/core/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
from bdgd_tools import Sample, Case, Circuit, LineCode, Line, LoadShape, Transformer, RegControl, Load
from bdgd_tools.core.Utils import load_json, merge_entities_tables, inner_entities_tables, create_output_file, create_output_feeder_coords, create_dfs_coords
from bdgd_tools.gui.GUI import GUI
import bdgd_tools.model.BusCoords as Coords

import bdgd_tools.model.BusCoords as buscoords


class Table:
Expand Down Expand Up @@ -111,6 +112,10 @@ def create_geodataframes(self, filename, runs=1):
conversion_times = []
gdf_converted = None

#* DEBUG
if table_name == 'SSDMT':
pass

for _ in range(runs):
start_time = time.time()
gdf_ = gpd.read_file(filename, layer=table.name,
Expand Down Expand Up @@ -146,22 +151,22 @@ def get_caller_directory(caller_frame: inspect) -> pathlib.Path:
caller_file = inspect.getfile(caller_frame)
return pathlib.Path(caller_file).resolve().parent

#* a função apagada era igual a de cima


def run_gui(folder_bdgd: str) -> None:
def run_gui(folder_bdgd: str, feeder) -> None:
caller_frame = inspect.currentframe().f_back
caller_path = get_caller_directory(caller_frame)
json_file = os.path.join(caller_path, "bdgd2dss.json")

print(f"Base escolhida {folder_bdgd}")
data = load_json(json_file=json_file)

gui = GUI(folder_bdgd, data)
#! verificar variável data
gui = GUI(folder_bdgd, data, feeder)
gui.load_window()


def run(folder: Optional[str] = None, feeder: Optional[str] = None, all_feeders: Optional[bool] = None, limit_ramal_30m: Optional[bool] = False) -> None:
print("Iniciando...")

if feeder is None:
all_feeders = True
Expand All @@ -174,6 +179,11 @@ def run(folder: Optional[str] = None, feeder: Optional[str] = None, all_feeders

geodataframes = json_data.create_geodataframes(folder_bdgd)

#df_coords = buscoords.coords()

# #! Modificado
# run_gui(folder_bdgd, feeder)

for alimentador in geodataframes["CTMT"]['gdf']['COD_ID'].tolist():

if alimentador == feeder or all_feeders == True:
Expand All @@ -196,10 +206,17 @@ def run(folder: Optional[str] = None, feeder: Optional[str] = None, all_feeders

if not case.dfs[entity]['gdf'].query("CTMT == @alimentador").empty:
if limit_ramal_30m == True:
case.lines_SSDMT, aux = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity, ramal_30m = limit_ramal_30m)
#case.lines_SSDMT, aux = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity, ramal_30m = limit_ramal_30m)

case.lines_SSDMT, aux, aux_em = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity, ramal_30m = limit_ramal_30m)
else:
case.lines_SSDMT, aux = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity)
#case.lines_SSDMT, aux = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity)

case.lines_SSDMT, aux, aux_em = Line.create_line_from_json(json_data.data, case.dfs[entity]['gdf'].query("CTMT==@alimentador"), entity)
list_files_name.append(aux)

if aux_em != "":
list_files_name.append(aux_em)
else:
print(f'No {entity} elements found\n')

Expand All @@ -226,3 +243,8 @@ def run(folder: Optional[str] = None, feeder: Optional[str] = None, all_feeders

case.output_master(list_files_name)
case.create_outputs_masters(list_files_name)

#folder_output = r'C:\\bdgd-tools-main\\output\\'
#df_coords.to_csv(f'{folder_output}coords.csv', index=False)
#print ('teste')

20 changes: 12 additions & 8 deletions bdgd_tools/core/Utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ def load_json(json_file: str = "bdgd2dss.json"):


def merge_entities_tables(dataframe1: gpd.geodataframe.GeoDataFrame,dataframe2: gpd.geodataframe.GeoDataFrame):
"""Merge two GeoDataFrames of entities based on their indices and handle duplicated columns.
"""
Merge two GeoDataFrames of entities based on their indices and handle duplicated columns.
It's necessary when the element needs more of one table of the BDGD.
Expand All @@ -60,8 +61,8 @@ def merge_entities_tables(dataframe1: gpd.geodataframe.GeoDataFrame,dataframe2:
return merged_dfs

def inner_entities_tables(entity1_df, enetity2_df, left_column: str = "", right_column: str = ""):

"""Merge two entities's DataFrames using an inner join and process the resulting DataFrame.
"""
Merge two entities's DataFrames using an inner join and process the resulting DataFrame.
This function takes two DataFrames, 'entity1_df' and 'entity2_df', and merges them
using an inner join on the 'UN_RE' column of 'entity1_df' and the 'COD_ID' column
Expand Down Expand Up @@ -121,7 +122,6 @@ def create_output_file(object_list=[], file_name="", object_lists="", file_names
"""


if not os.path.exists("output"):
os.mkdir("output")

Expand All @@ -134,13 +134,13 @@ def create_output_file(object_list=[], file_name="", object_lists="", file_names

for object_list, file_name in zip(object_lists, file_names):


path = os.path.join(output_directory, f'{file_name}_{feeder}.dss')

try:
with open(path, "w") as file:
for string in object_list:
file.write(string.full_string() + "\n")

# print(f'O arquivo {file_name}_{feeder} foi gerado\n')
except Exception as e:
print(f"An error occurred: {str(e)}")
Expand All @@ -154,16 +154,21 @@ def create_output_file(object_list=[], file_name="", object_lists="", file_names
try:
with open(path, "w") as file:
for string in object_list:
file.write(string.full_string() + "\n")
if type(string) == str:
file.write(string + "\n")
else:
file.write(string.full_string() + "\n")

print(f'O arquivo {file_name}_{feeder} foi gerado\n')
except Exception as e:
print(f"An error occurred: {str(e)}")

return f'{file_name}_{feeder}.dss'

def create_master_file(file_name="", feeder="", master_content=""):
"""
Create an output file and write data from a list of objects.
"""Create an output file and write data from a list of objects.
Creates an output file in the 'output' directory and writes OpenDSS commands from the list,
separated by newline characters. If any error occurs, it will be displayed.
Expand All @@ -187,7 +192,6 @@ def create_master_file(file_name="", feeder="", master_content=""):
except Exception as e:
print(f"An error occurred: {str(e)}")


def create_output_feeder_coords(df: pd.DataFrame, feeder="", filename="buscoords"):

"""Crie um arquivo de saída csv e grave dados de um DataFrame.
Expand Down
24 changes: 21 additions & 3 deletions bdgd_tools/model/Line.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,16 @@ def pattern_switch(self):
f'r1={self.r1} r0={self.r0} x1={self.x1} x0={self.x0} c1={self.c1} c0={self.c0} ' \
f'switch = {self.switch} length={self.length:.5f}'

def pattern_energymeter(self):

return f'New Energymeter.EM_{self.prefix_name}_{self.line} element=line.{self.prefix_name}_{self.line} terminal=1'


def full_string(self) -> str:

# if em:
# return self.pattern_energymeter()

if self.prefix_name == "CMT" or self.prefix_name == "CBT":
return self.pattern_switch()
else:
Expand Down Expand Up @@ -338,9 +345,9 @@ def _create_line_from_row(line_config, row):
line_._process_calculated(line_, value, row)

elif key == "direct_mapping":
line_._process_direct_mapping(line_, value,row)
line_._process_direct_mapping(line_, value, row)
elif key == "indirect_mapping":
line_._process_indirect_mapping(line_, value,row)
line_._process_indirect_mapping(line_, value, row)
elif key == "static":
line_._process_static(line_, value)
return line_
Expand All @@ -349,13 +356,24 @@ def _create_line_from_row(line_config, row):
def create_line_from_json(json_data: Any, dataframe: gpd.geodataframe.GeoDataFrame, entity: str, ramal_30m = False):

lines = []
energymeters = []
line_config = json_data['elements']['Line'][entity]
progress_bar = tqdm(dataframe.iterrows(), total=len(dataframe), desc="Line", unit=" lines", ncols=100)
for _, row in progress_bar:
line_ = Line._create_line_from_row(line_config, row)

if line_.prefix_name == "CMT":
energymeters.append(line_.pattern_energymeter())

lines.append(line_)
progress_bar.set_description(f"Processing Line {entity} {_ + 1}")

file_name = create_output_file(lines, line_config["arquivo"], feeder=line_.feeder)

return lines, file_name
if energymeters != []:
EM_file_name = create_output_file(energymeters, "EnergyMeters", feeder=line_.feeder)

else:
EM_file_name = ""

return lines, file_name, EM_file_name
12 changes: 6 additions & 6 deletions output/1_3PAS_1/Cargas_BT_DO01_1_3PAS_1.dss
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ New "Load.BT_2805360_6711_M2" bus1="UC2805360.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805255_6732_M1" bus1="UC2805255.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.1469572 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU6_DO"
New "Load.BT_2805255_6732_M2" bus1="UC2805255.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.1469572 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU6_DO"

New "Load.BT_2805256_6733_M1" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 0.4065929 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M2" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 0.4065929 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M1" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 0.4065929 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M2" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 0.4065929 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"

New "Load.BT_2805357_6743_M1" bus1="UC2805357.1.2.3.4" phases=3 conn=Delta model=2 kv=0.38 kw = 1.1812195 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805357_6743_M2" bus1="UC2805357.1.2.3.4" phases=3 conn=Delta model=3 kv=0.38 kw = 1.1812195 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
Expand Down Expand Up @@ -586,8 +586,8 @@ New "Load.BT_2805238_6847_M2" bus1="UC2805238.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805239_6848_M1" bus1="UC2805239.2.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.0211139 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU1_DO"
New "Load.BT_2805239_6848_M2" bus1="UC2805239.2.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.0211139 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU1_DO"

New "Load.BT_2805240_6849_M1" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 0.9627979 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M2" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 0.9627979 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M1" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 0.9627979 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M2" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 0.9627979 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"

New "Load.BT_2805241_6850_M1" bus1="UC2805241.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.4695762 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU5_DO"
New "Load.BT_2805241_6850_M2" bus1="UC2805241.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.4695762 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU5_DO"
Expand Down Expand Up @@ -676,8 +676,8 @@ New "Load.BT_2805288_7022_M2" bus1="UC2805288.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805220_7028_M1" bus1="UC2805220.2.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.2424085 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU4_DO"
New "Load.BT_2805220_7028_M2" bus1="UC2805220.2.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.2424085 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU4_DO"

New "Load.BT_2805221_7029_M1" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 0.3133631 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M2" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 0.3133631 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M1" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 0.3133631 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M2" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 0.3133631 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"

New "Load.BT_2805222_7030_M1" bus1="UC2805222.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.0000000 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="CO5_DO"
New "Load.BT_2805222_7030_M2" bus1="UC2805222.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.0000000 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="CO5_DO"
Expand Down
12 changes: 6 additions & 6 deletions output/1_3PAS_1/Cargas_BT_DO02_1_3PAS_1.dss
Original file line number Diff line number Diff line change
Expand Up @@ -508,8 +508,8 @@ New "Load.BT_2805360_6711_M2" bus1="UC2805360.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805255_6732_M1" bus1="UC2805255.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.1693372 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU6_DO"
New "Load.BT_2805255_6732_M2" bus1="UC2805255.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.1693372 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU6_DO"

New "Load.BT_2805256_6733_M1" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 0.4532854 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M2" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 0.4532854 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M1" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 0.4532854 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805256_6733_M2" bus1="UC2805256.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 0.4532854 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"

New "Load.BT_2805357_6743_M1" bus1="UC2805357.1.2.3.4" phases=3 conn=Delta model=2 kv=0.38 kw = 0.8691971 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805357_6743_M2" bus1="UC2805357.1.2.3.4" phases=3 conn=Delta model=3 kv=0.38 kw = 0.8691971 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
Expand Down Expand Up @@ -586,8 +586,8 @@ New "Load.BT_2805238_6847_M2" bus1="UC2805238.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805239_6848_M1" bus1="UC2805239.2.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.0226285 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU1_DO"
New "Load.BT_2805239_6848_M2" bus1="UC2805239.2.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.0226285 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU1_DO"

New "Load.BT_2805240_6849_M1" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 1.2025739 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M2" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 1.2025739 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M1" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 1.2025739 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"
New "Load.BT_2805240_6849_M2" bus1="UC2805240.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 1.2025739 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU7_DO"

New "Load.BT_2805241_6850_M1" bus1="UC2805241.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.5940387 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU5_DO"
New "Load.BT_2805241_6850_M2" bus1="UC2805241.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.5940387 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU5_DO"
Expand Down Expand Up @@ -676,8 +676,8 @@ New "Load.BT_2805288_7022_M2" bus1="UC2805288.1.4" phases=1 conn=Delta model=3 k
New "Load.BT_2805220_7028_M1" bus1="UC2805220.2.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.2565092 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU4_DO"
New "Load.BT_2805220_7028_M2" bus1="UC2805220.2.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.2565092 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU4_DO"

New "Load.BT_2805221_7029_M1" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=2 kv=0.44 kw = 0.4007901 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M2" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=3 kv=0.44 kw = 0.4007901 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M1" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=2 kv=0.38 kw = 0.4007901 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"
New "Load.BT_2805221_7029_M2" bus1="UC2805221.1.2.4" phases=1 conn=Delta model=3 kv=0.38 kw = 0.4007901 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="RU2_DO"

New "Load.BT_2805222_7030_M1" bus1="UC2805222.1.4" phases=1 conn=Delta model=2 kv=0.22 kw = 0.0000000 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="CO5_DO"
New "Load.BT_2805222_7030_M2" bus1="UC2805222.1.4" phases=1 conn=Delta model=3 kv=0.22 kw = 0.0000000 pf=0.92 status=variable vmaxpu=1.5 vminpu=0.92 daily="CO5_DO"
Expand Down
Loading

0 comments on commit c08739f

Please sign in to comment.