Skip to content

Commit

Permalink
Merge pull request #12 from sogno-platform/feature/cgmes-pgm-converter
Browse files Browse the repository at this point in the history
convert lines + sources
  • Loading branch information
petersalemink95 authored Nov 9, 2023
2 parents abef780 + 2d1370d commit b3f9bab
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 25 deletions.
16 changes: 11 additions & 5 deletions examples/run_power_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

from src.pgm_service.power_grid.cgmes_pgm_converter import System

from power_grid_model import PowerGridModel

logging.basicConfig(filename='run_powerflow.log', level=logging.INFO, filemode='w')

def download_grid_data(name, url):
Expand All @@ -18,10 +20,10 @@ def download_grid_data(name, url):
url = 'https://raw.githubusercontent.com/dpsim-simulator/cim-grid-data/master/BasicGrids/NEPLAN/Slack_Load_Line_Sample/'
filename = 'Rootnet_FULL_NE_19J18h'

download_grid_data(filename+'_EQ.xml', url + filename + '_EQ.xml')
download_grid_data(filename+'_TP.xml', url + filename + '_TP.xml')
download_grid_data(filename+'_SV.xml', url + filename + '_SV.xml')

# download_grid_data(filename+'_EQ.xml', url + filename + '_EQ.xml')
# download_grid_data(filename+'_TP.xml', url + filename + '_TP.xml')
# download_grid_data(filename+'_SV.xml', url + filename + '_SV.xml')
#
files = glob.glob(filename+'_*.xml')

print('CGMES files downloaded:')
Expand All @@ -42,5 +44,9 @@ def download_grid_data(name, url):
system = System()
system.load_cim_data(res)
pgm_input = system.create_pgm_input()
print(pgm_input)

pgm = PowerGridModel(input_data=pgm_input)
results = pgm.calculate_power_flow()

print(pgm_input)
print(results["node"])
81 changes: 61 additions & 20 deletions src/pgm_service/power_grid/cgmes_pgm_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
from power_grid_model import initialize_array


FREQUENCY = 50.0
DEFAULT_SOURCE_SHORT_CIRCUIT_POWER = 1e20

class System():
def __init__(self):
self.nodes = []
self.voltages = []
self.lines = {}
self.sources = {}

def load_cim_data(self, res):
"""
Expand All @@ -29,6 +33,7 @@ def load_cim_data(self, res):
# elem.ConductingEquipment.__class__.__name__ == "EnergySource"]
# list_Terminals_EC = [elem for elem in list_Terminals if
# elem.ConductingEquipment.__class__.__name__ == "EnergyConsumer"]
list_Source = [elem for elem in res['topology'].values() if elem.__class__.__name__ == "ExternalNetworkInjection"]

# create nodes
for TPNode in list_TPNode:
Expand All @@ -39,32 +44,56 @@ def load_cim_data(self, res):
for ACLineSegment in list_ACLineSegment:
uuid_ACLineSegment = ACLineSegment.mRID
connected_nodes = self._get_nodes(list_Terminals, uuid_ACLineSegment)
node_ids = list[connected_nodes.keys()]
status = list[connected_nodes.values()]
# self.lines[ACLineSegment.mRID] = {"from_node": node_ids[0],
# "to_node": node_ids[1]}

line = initialize_array("input", "line", len(self.lines))
# line["id"] =
# line["from_node"] =
# line["to_node"] =
# line["from_status"] =
# line["to_status"] =
# line["r1"] = [single_line.r for single_line in self.lines.values()]
# line["x1"] = [single_line.x for single_line in self.lines.values()]
# line["c1"] = [single_line.bch / (2 * np.pi * FREQUENCY) for single_line in self.lines.values()]
# line["tan1"] = [single_line.gch / single_line.bch for single_line in self.lines.values()]
# line["i_n"] = [single_line.ratedCurrent for single_line in self.lines.values()]


node_ids = list(connected_nodes.keys())
status = list(connected_nodes.values())
self.lines[ACLineSegment.mRID] = {"from_node": self.nodes.index(node_ids[0]),
"to_node": self.nodes.index(node_ids[1]),
"from_status": status[0],
"to_status": status[0],
"r1": ACLineSegment.r,
"x1": ACLineSegment.x,
"c1": ACLineSegment.bch / (2 * np.pi * FREQUENCY),
"tan1": ACLineSegment.gch / ACLineSegment.bch}
# TODO: check if there is a multiplier for r1/x1, c1, tan1, i_n

for source in list_Source:
connected_node = self._get_node(list_Terminals, source.mRID)
node_id = next(iter(connected_node))
status = connected_node[node_id]
self.sources[source.mRID] = {"node": self.nodes.index(node_id),
"status": status,
"u_ref": 1.0,
"sk": DEFAULT_SOURCE_SHORT_CIRCUIT_POWER}

def create_pgm_input(self):
id_counter = 0
node = initialize_array("input", "node", len(self.nodes))
node["id"] = range(len(self.nodes))
node["u_rated"] = self.voltages
print("debug")
id_counter += len(self.nodes)

return {"node": node}
line = initialize_array("input", "line", len(self.lines))
line["id"] = range(id_counter, id_counter + len(self.lines))
line["from_node"] = [line_param["from_node"] for line_param in self.lines.values()]
line["to_node"] = [line_param["to_node"] for line_param in self.lines.values()]
line["from_status"] = [line_param["from_status"] for line_param in self.lines.values()]
line["to_status"] = [line_param["to_status"] for line_param in self.lines.values()]
line["r1"] = [line_param["r1"] for line_param in self.lines.values()]
line["x1"] = [line_param["x1"] for line_param in self.lines.values()]
line["c1"] = [line_param["c1"] for line_param in self.lines.values()]
line["tan1"] = [line_param["tan1"] for line_param in self.lines.values()]
id_counter += len(self.lines)

source = initialize_array("input", "source", len(self.sources))
source["id"] = range(id_counter, id_counter + len(self.sources))
source["node"] = [source_param["node"] for source_param in self.sources.values()]
source["status"] = [source_param["status"] for source_param in self.sources.values()]
source["u_ref"] = [source_param["u_ref"] for source_param in self.sources.values()]
source["sk"] = [source_param["sk"] for source_param in self.sources.values()]

return {"node": node,
"line": line,
"source": source}

def _get_nodes(self, list_Terminals, elem_uuid):
start_node_uuid = None
Expand All @@ -85,3 +114,15 @@ def _get_nodes(self, list_Terminals, elem_uuid):


return {start_node_uuid: start_node_connected, end_node_uuid: end_node_connected}

def _get_node(self, list_Terminals, elem_uuid):
node_uuid = None
node_connected = None

for terminal in list_Terminals:
if terminal.ConductingEquipment.mRID != elem_uuid:
continue
node_uuid = terminal.TopologicalNode.mRID
node_connected = terminal.connected

return {node_uuid: node_connected}

0 comments on commit b3f9bab

Please sign in to comment.