Skip to content

Commit

Permalink
feat: add query for extracting mapping between synchronous machines a…
Browse files Browse the repository at this point in the history
…nd generating units
  • Loading branch information
davidkleiven committed Nov 22, 2024
1 parent 9e213b3 commit 5aa7e79
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/cimsparql/adaptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ def set_generation_type(self) -> None:
def adapt(self, eq_uri: str) -> None:
self.add_zero_sv_power_flow()
self.add_zero_sv_injection()
self.add_generating_unit()
self.add_mrid()
self.add_dtypes()
self.set_generation_type()
Expand Down Expand Up @@ -150,6 +151,13 @@ def add_network_analysis_enable(self) -> None:
prepared_update_query = prepareUpdate(query.substitute(self.namespaces()))
self.graph.update(prepared_update_query)

def add_generating_unit(self) -> None:
with open(Path(__file__).parent / "sparql/test_configuration_modifications/add_gen_unit_mrid.sparql") as f:
query = Template(f.read())

prepared_update_query = prepareUpdate(query.substitute(self.namespaces()))
self.graph.update(prepared_update_query)


def is_uuid(x: str) -> bool:
return re.match("^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$", x) is not None
Expand Down
8 changes: 8 additions & 0 deletions src/cimsparql/data_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,3 +412,11 @@ class AssociatedSwitches(CoercingSchema):


AssociatedSwitchesDataFrame = DataFrame[AssociatedSwitches]


class GenUnitAndSyncMachineMridSchema(CoercingSchema):
gen_unit_mrid: str = pa.Field(unique=True)
sync_machine_mrid: str = pa.Field(unique=True)


GenUnitAndSyncMachineMridDataFrame = DataFrame[GenUnitAndSyncMachineMridSchema]
6 changes: 6 additions & 0 deletions src/cimsparql/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DisconnectedDataFrame,
ExchangeDataFrame,
FullModelDataFrame,
GenUnitAndSyncMachineMridDataFrame,
HVDCBidzonesDataFrame,
HVDCDataFrame,
LoadsDataFrame,
Expand Down Expand Up @@ -690,6 +691,11 @@ def associated_switches(self) -> AssociatedSwitchesDataFrame:
query = self.template_to_query(templates.ASSOCIATED_SWITCHES)
return AssociatedSwitchesDataFrame(self.get_table_and_convert(query))

@time_it
def gen_unit_and_sync_machine_mrid(self) -> GenUnitAndSyncMachineMridDataFrame:
query = self.template_to_query(templates.GEN_UNIT_MRID_AND_SYNC_MACHINE)
return GenUnitAndSyncMachineMridDataFrame(self.get_table_and_convert(query))


class SingleClientModel(Model):
def __init__(
Expand Down
6 changes: 6 additions & 0 deletions src/cimsparql/sparql/gen_unit_and_sync_machine_mapping.sparql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Name: GeneratingUnit mrid to physical equipment mrid
PREFIX cim: <${cim}>
select ?gen_unit_mrid ?sync_machine_mrid where {
?s cim:SynchronousMachine.GeneratingUnit/cim:IdentifiedObject.mRID ?gen_unit_mrid;
cim:IdentifiedObject.mRID ?sync_machine_mrid
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
prefix cim:<${cim}>
prefix SN:<${SN}>
insert {
graph <http://cimsparql/xml-adpator/EQ-modifications> {
_:b0 a SN:GeneratingUnit;
cim:IdentifiedObject.name "GeneratingUnit" .
?machine cim:SynchronousMachine.GeneratingUnit _:b0
}}
where {
?machine a cim:SynchronousMachine
filter not exists{?machine cim:SynchronousMachine.GeneratingUnit ?unit}
}
1 change: 1 addition & 0 deletions src/cimsparql/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def _read_template(filename: pathlib.Path) -> Template:
DISCONNECTED_QUERY = _read_template(sparql_folder / "disconnected.sparql")
EXCHANGE_QUERY = _read_template(sparql_folder / "exchange.sparql")
FULL_MODEL_QUERY = _read_template(sparql_folder / "full_model.sparql")
GEN_UNIT_MRID_AND_SYNC_MACHINE = _read_template(sparql_folder / "gen_unit_and_sync_machine_mapping.sparql")
HVDC = _read_template(sparql_folder / "hvdc.sparql")
HVDC_CONVERTER_BIDZONES = _read_template(sparql_folder / "converter_hvdc_bidzones.sparql")
LOADS_QUERY = _read_template(sparql_folder / "loads.sparql")
Expand Down
1 change: 1 addition & 0 deletions tests/test_graphdb.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ async def collect_data(model: Model) -> dict[str, pd.DataFrame]:
model.disconnected,
model.exchange,
model.full_model,
model.gen_unit_and_sync_machine_mrid,
model.hvdc_converter_bidzones,
model.loads,
model.market_dates,
Expand Down
9 changes: 9 additions & 0 deletions tests/test_micro_t1_nl.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,3 +352,12 @@ def test_associated_switches(test_model: t_common.ModelTest) -> None:
# Test values for power transformer BE-TR3. Should be connected to two switches
switches = set(df.query("name == 'BE-TR2_1'")["switch_names"].iloc[0].split(","))
assert switches == {"BE_Breaker_1", "BE_Breaker_2"}


@pytest.mark.parametrize("test_model", t_entsoe.micro_models())
def test_gen_unit_and_sync_machine_mrid(test_model: t_common.ModelTest) -> None:
t_common.check_model(test_model)
assert test_model.model

df = test_model.model.gen_unit_and_sync_machine_mrid()
assert len(df) == 4
6 changes: 6 additions & 0 deletions tests/test_xml_adaptor.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ def test_add_network_analysis_enable(xml_adaptor: XmlModelAdaptor) -> None:
xml_adaptor.add_network_analysis_enable()
query = "select * {?terminal cim:Terminal.ConductingEquipment/SN:Equipment.networkAnalysisEnable True}"
assert len(xml_adaptor.graph.query(query, initNs=xml_adaptor.namespaces())) > 0


def test_add_generating_unit(xml_adaptor: XmlModelAdaptor) -> None:
xml_adaptor.add_generating_unit()
query = "select * {?machine cim:SynchronousMachine.GeneratingUnit ?unit}"
assert len(xml_adaptor.graph.query(query, initNs=xml_adaptor.namespaces())) > 0

0 comments on commit 5aa7e79

Please sign in to comment.