Skip to content

Commit

Permalink
Merge branch 'main' into feature/downloader
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstoeller authored Mar 7, 2023
2 parents f5341ae + d8dd031 commit 644915e
Show file tree
Hide file tree
Showing 4 changed files with 184 additions and 38 deletions.
68 changes: 61 additions & 7 deletions src/power_grid_model_io/converters/pandapower_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ def _parse_data(self, data: PandaPowerData, data_type: str, extra_info: Optional

# Construct extra_info
if extra_info is not None:
self._fill_extra_info(extra_info=extra_info)
self._fill_pgm_extra_info(extra_info=extra_info)
self._fill_pp_extra_info(extra_info=extra_info)

return self.pgm_input_data

Expand Down Expand Up @@ -135,22 +136,56 @@ def _create_input_data(self):
self._create_pgm_input_generators()
self._create_pgm_input_dclines()

def _fill_extra_info(self, extra_info: ExtraInfo):
def _fill_pgm_extra_info(self, extra_info: ExtraInfo):
"""
Fills in extra information of power-grid-model input after conversion from pandapower to the extra_info dict
Args:
extra_info: The extra info dict
"""
for (pp_table, name), indices in self.idx_lookup.items():
for pgm_id, pp_idx in zip(indices.index, indices):
if name:
extra_info[pgm_id] = {"id_reference": {"table": pp_table, "name": name, "index": pp_idx}}
else:
extra_info[pgm_id] = {"id_reference": {"table": pp_table, "index": pp_idx}}

for component_data in self.pgm_input_data.values():
for attr_name in component_data.dtype.names:
if not NODE_REF_RE.fullmatch(attr_name):
continue
for pgm_id, node_id in component_data[["id", attr_name]]:
if pgm_id in extra_info:
extra_info[pgm_id][attr_name] = node_id
else:
extra_info[pgm_id] = {attr_name: node_id}
if pgm_id not in extra_info:
extra_info[pgm_id] = {}
if "pgm_input" not in extra_info[pgm_id]:
extra_info[pgm_id]["pgm_input"] = {}
extra_info[pgm_id]["pgm_input"][attr_name] = node_id

def _fill_pp_extra_info(self, extra_info: ExtraInfo):
"""
Fills extra information from pandapower input dataframes not available in power-grid-model input
to the extra_info dict.
Currently, it is possible to only store the derating factor (df) of trafo.
Args:
extra_info: The extra info dict
"""
pp_input = {"trafo": {"df"}}
for pp_table, pp_attr in pp_input.items():
if pp_table in self.pp_input_data:
pp_attr = pp_attr & set(self.pp_input_data[pp_table].columns)
if not pp_attr:
continue
pgm_ids = self._get_pgm_ids(pp_table=pp_table)
pp_extra_data = self.pp_input_data[pp_table][pp_attr]
pp_extra_data.index = pgm_ids
for pgm_id, pp_element in pp_extra_data.iterrows():
if pgm_id not in extra_info:
extra_info[pgm_id] = {}
if "pp_input" not in extra_info[pgm_id]:
extra_info[pgm_id]["pp_input"] = {}
for attr in pp_attr:
extra_info[pgm_id]["pp_input"][attr] = pp_element[attr]

def _extra_info_to_idx_lookup(self, extra_info: ExtraInfo):
"""
Expand Down Expand Up @@ -202,10 +237,29 @@ def _extra_info_to_pgm_input_data(self, extra_info: ExtraInfo):
dtype={"names": ["id"] + node_cols, "formats": [dtype] * num_cols},
)
for i, pgm_id in enumerate(data["id"]):
extra = extra_info[pgm_id]
extra = extra_info[pgm_id].get("pgm_input", {})
ref[i] = (pgm_id,) + tuple(extra[col] for col in node_cols)
self.pgm_input_data[component] = ref

def _extra_info_to_pp_input_data(self, extra_info: ExtraInfo):
"""
Converts extra component info into node_lookup
Currently, it is possible to only retrieve the derating factor (df) of trafo.
Args:
extra_info: a dictionary where the node reference ids are stored
"""
assert not self.pp_input_data
assert self.pgm_output_data

if "transformer" not in self.pgm_output_data:
return

pgm_ids = self.pgm_output_data["transformer"]["id"]
pp_ids = self._get_pp_ids(pp_table="trafo", pgm_idx=pgm_ids)
derating_factor = (extra_info.get(pgm_id, {}).get("pp_input", {}).get("df", np.nan) for pgm_id in pgm_ids)
self.pp_input_data = {"trafo": pd.DataFrame(derating_factor, columns=["df"], index=pp_ids)}

def _create_output_data(self):
"""
Performs the conversion from power-grid-model to PandaPower by calling individual conversion functions.
Expand Down
30 changes: 15 additions & 15 deletions tests/data/pandapower/pgm_output_data.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,47 +10,47 @@
],
"line":
[
{"id": 6, "energized": 1, "loading": 0.5930290459172566, "p_from": -1763554.1135053905, "q_from": -1245081.6865976173, "i_from": 64.0471369590637, "s_from": 2158784.7316401307, "p_to": 1765096.6771171533, "q_to": 1169229.7700732222, "i_to": 62.767063741949805, "s_to": 2117230.39246925, "id_reference": {"table": "line", "index": 101}, "from_node": 2, "to_node": 1}
{"id": 6, "energized": 1, "loading": 0.5930290459172566, "p_from": -1763554.1135053905, "q_from": -1245081.6865976173, "i_from": 64.0471369590637, "s_from": 2158784.7316401307, "p_to": 1765096.6771171533, "q_to": 1169229.7700732222, "i_to": 62.767063741949805, "s_to": 2117230.39246925, "id_reference": {"table": "line", "index": 101}, "pgm_input": {"from_node": 2, "to_node": 1}}
],
"source":
[
{"id": 7, "energized": 1, "p": 1798666.0004384827, "q": 3476627.872944676, "i": 20.545012571488463, "s": 3914350.6163946516, "pf": 0.4595055928063901, "id_reference": {"table": "ext_grid", "index": 1}, "node": 0}
{"id": 7, "energized": 1, "p": 1798666.0004384827, "q": 3476627.872944676, "i": 20.545012571488463, "s": 3914350.6163946516, "pf": 0.4595055928063901, "id_reference": {"table": "ext_grid", "index": 1}, "pgm_input": {"node": 0}}
],
"sym_load":
[
{"id": 8, "energized": 1, "p": 575000.0, "q": 55200.0, "i": 17.13761141048308, "s": 577643.5232909652, "pf": 0.9954236078405164, "id_reference": {"table": "load", "name": "const_power", "index": 101}, "node": 2},
{"id": 9, "energized": 1, "p": 1207113.6835430216, "q": 115882.91362013007, "i": 35.977469977108086, "s": 1212663.3063904808, "pf": 0.9954236078405161, "id_reference": {"table": "load", "name": "const_impedance", "index": 101}, "node": 2},
{"id": 10, "energized": 1, "p": 632458.9617296124, "q": 60716.06032604279, "i": 18.850149424695108, "s": 635366.6486790245, "pf": 0.9954236078405163, "id_reference": {"table": "load", "name": "const_current", "index": 101}, "node": 2},
{"id": 18, "energized": 1, "p": 99999.99999999999, "q": 99999.99999999999, "i": 1.4000314325372134, "s": 141421.3562373095, "pf": 0.7071067811865475, "id_reference": {"table": "ward", "name": "ward_const_power_load", "index": 34}, "node": 4},
{"id": 19, "energized": 1, "p": 94477.99488734086, "q": 94477.99488734086, "i": 1.3227216252536735, "s": 133612.0617154934, "pf": 0.7071067811865475, "id_reference": {"table": "ward", "name": "ward_const_impedance_load", "index": 34}, "node": 4},
{"id": 20, "energized": 1, "p": 71111.11111111111, "q": 34440.68301069173, "i": 0.7821998773897324, "s": 79012.34567901235, "pf": 0.9, "id_reference": {"table": "motor", "name": "motor_load", "index": 12}, "node": 4}
{"id": 8, "energized": 1, "p": 575000.0, "q": 55200.0, "i": 17.13761141048308, "s": 577643.5232909652, "pf": 0.9954236078405164, "id_reference": {"table": "load", "name": "const_power", "index": 101}, "pgm_input": {"node": 2}},
{"id": 9, "energized": 1, "p": 1207113.6835430216, "q": 115882.91362013007, "i": 35.977469977108086, "s": 1212663.3063904808, "pf": 0.9954236078405161, "id_reference": {"table": "load", "name": "const_impedance", "index": 101}, "pgm_input": {"node": 2}},
{"id": 10, "energized": 1, "p": 632458.9617296124, "q": 60716.06032604279, "i": 18.850149424695108, "s": 635366.6486790245, "pf": 0.9954236078405163, "id_reference": {"table": "load", "name": "const_current", "index": 101}, "pgm_input": {"node": 2}},
{"id": 18, "energized": 1, "p": 99999.99999999999, "q": 99999.99999999999, "i": 1.4000314325372134, "s": 141421.3562373095, "pf": 0.7071067811865475, "id_reference": {"table": "ward", "name": "ward_const_power_load", "index": 34}, "pgm_input": {"node": 4}},
{"id": 19, "energized": 1, "p": 94477.99488734086, "q": 94477.99488734086, "i": 1.3227216252536735, "s": 133612.0617154934, "pf": 0.7071067811865475, "id_reference": {"table": "ward", "name": "ward_const_impedance_load", "index": 34}, "pgm_input": {"node": 4}},
{"id": 20, "energized": 1, "p": 71111.11111111111, "q": 34440.68301069173, "i": 0.7821998773897324, "s": 79012.34567901235, "pf": 0.9, "id_reference": {"table": "motor", "name": "motor_load", "index": 12}, "pgm_input": {"node": 4}}
],
"shunt":
[
{"id": 11, "energized": 1, "p": 282008.0001416396, "q": 1551044.0007790178, "i": 31.188122375690188, "s": 1576472.6462886913, "pf": 0.17888543819998318, "id_reference": {"table": "shunt", "index": 1201}, "node": 3}
{"id": 11, "energized": 1, "p": 282008.0001416396, "q": 1551044.0007790178, "i": 31.188122375690188, "s": 1576472.6462886913, "pf": 0.17888543819998318, "id_reference": {"table": "shunt", "index": 1201}, "pgm_input": {"node": 3}}
],
"transformer":
[
{"id": 12, "energized": 1, "loading": 0.04892939318768632, "p_from": 1798665.9834270997, "q_from": 3476628.8259518775, "i_from": 20.545016973103277, "s_from": 3914351.4550149054, "p_to": -1765096.6771171836, "q_to": -1169229.7700735242, "i_to": 62.76706374195551, "s_to": 2117230.3924694424, "id_reference": {"table": "trafo", "index": 101}, "from_node": 0, "to_node": 1}
{"id": 12, "energized": 1, "loading": 0.04892939318768632, "p_from": 1798665.9834270997, "q_from": 3476628.8259518775, "i_from": 20.545016973103277, "s_from": 3914351.4550149054, "p_to": -1765096.6771171836, "q_to": -1169229.7700735242, "i_to": 62.76706374195551, "s_to": 2117230.3924694424, "id_reference": {"table": "trafo", "index": 101}, "pgm_input": {"from_node": 0, "to_node": 1}, "pp_input": {"df": 1.0}}
],
"sym_gen":
[
{"id": 13, "energized": 1, "p": 1210000.0, "q": 810000.0, "i": 14.414885714493, "s": 1456090.6565183362, "pf": 0.8309922150680065, "id_reference": {"table": "sgen", "index": 31}, "node": 4}
{"id": 13, "energized": 1, "p": 1210000.0, "q": 810000.0, "i": 14.414885714493, "s": 1456090.6565183362, "pf": 0.8309922150680065, "id_reference": {"table": "sgen", "index": 31}, "pgm_input": {"node": 4}}
],
"three_winding_transformer":
[
{"id": 14, "energized": 1, "loading": 0.03152945292576947, "p_1": -651018.5317680555, "q_1": 1013282.7126517809, "i_1": 35.73216058618558, "s_1": 1204394.8623538653, "p_2": 944410.894001629, "q_2": 581081.3221020879, "i_2": 10.977386752839678, "s_2": 1108858.6202058704, "p_3": -282008.00014178595, "q_3": -1551044.0007787698, "i_3": 31.18812237568588, "s_3": 1576472.6462884734, "id_reference": {"table": "trafo3w", "index": 102}, "node_1": 2, "node_2": 4, "node_3": 3}
{"id": 14, "energized": 1, "loading": 0.03152945292576947, "p_1": -651018.5317680555, "q_1": 1013282.7126517809, "i_1": 35.73216058618558, "s_1": 1204394.8623538653, "p_2": 944410.894001629, "q_2": 581081.3221020879, "i_2": 10.977386752839678, "s_2": 1108858.6202058704, "p_3": -282008.00014178595, "q_3": -1551044.0007787698, "i_3": 31.18812237568588, "s_3": 1576472.6462884734, "id_reference": {"table": "trafo3w", "index": 102}, "pgm_input": {"node_1": 2, "node_2": 4, "node_3": 3}}
],
"link":
[
{"id": 15, "energized": 1, "loading": 0.0, "p_from": 0.0, "q_from": -0.0, "i_from": 0.0, "s_from": 0.0, "p_to": 0.0, "q_to": -0.0, "i_to": 0.0, "s_to": 0.0, "id_reference": {"table": "switch", "name": "b2b_switches", "index": 3021}, "from_node": 0, "to_node": 5}
{"id": 15, "energized": 1, "loading": 0.0, "p_from": 0.0, "q_from": -0.0, "i_from": 0.0, "s_from": 0.0, "p_to": 0.0, "q_to": -0.0, "i_to": 0.0, "s_to": 0.0, "id_reference": {"table": "switch", "name": "b2b_switches", "index": 3021}, "pgm_input": {"from_node": 0, "to_node": 5}}
],
"asym_load":
[
{"id": 16, "energized": 1, "p": 3300000.0000000005, "q": 30000.000000000004, "i": 32.670416687651056, "s": 3300136.36081905, "pf": 0.9999586802470745, "id_reference": {"table": "asymmetric_load", "index": 33}, "node": 4}
{"id": 16, "energized": 1, "p": 3300000.0000000005, "q": 30000.000000000004, "i": 32.670416687651056, "s": 3300136.36081905, "pf": 0.9999586802470745, "id_reference": {"table": "asymmetric_load", "index": 33}, "pgm_input": {"node": 4}}
],
"asym_gen":
[
{"id": 17, "energized": 1, "p": 3300000.0000000005, "q": 30000.000000000004, "i": 32.670416687651056, "s": 3300136.36081905, "pf": 0.9999586802470745, "id_reference": {"table": "asymmetric_sgen", "index": 32}, "node": 4}
{"id": 17, "energized": 1, "p": 3300000.0000000005, "q": 30000.000000000004, "i": 32.670416687651056, "s": 3300136.36081905, "pf": 0.9999586802470745, "id_reference": {"table": "asymmetric_sgen", "index": 32}, "pgm_input": {"node": 4}}
]
}
Loading

0 comments on commit 644915e

Please sign in to comment.