Skip to content

Fix PV→PQ bus type conversion when bus rows are not in ID order#49

Open
yemine0x01 wants to merge 3 commits into
gridfm:mainfrom
yemine0x01:fix-deactivate-gens
Open

Fix PV→PQ bus type conversion when bus rows are not in ID order#49
yemine0x01 wants to merge 3 commits into
gridfm:mainfrom
yemine0x01:fix-deactivate-gens

Conversation

@yemine0x01

Copy link
Copy Markdown
Contributor
  • Root cause: gen_count is produced by np.bincount over gens_on[:, GEN_BUS] (0-based bus ID), so it is indexed by bus ID. The mask (self.buses[:, BUS_TYPE] == PV) & (gen_count == 0) was comparing element-wise against gen_count in row order. When pypowsybl exports buses in non-sorted order (MATPOWER, PSS/E, XIIDM formats), bus row ≠ bus ID, so the wrong buses were promoted from PV to PQ.
  • Fix: Index gen_count with bus_ids = self.buses[:, BUS_I].astype(int) so the comparison aligns with the bus row being evaluated.

@yemine0x01 yemine0x01 force-pushed the fix-deactivate-gens branch 2 times, most recently from 2ac86c6 to b8ec1b4 Compare May 5, 2026 02:48
…n_count with bus IDs

Signed-off-by: eminyouskn <youssouf.emine@artelys.com>
@yemine0x01 yemine0x01 force-pushed the fix-deactivate-gens branch from b8ec1b4 to 5cc90e9 Compare May 5, 2026 02:49
@frmir frmir requested review from albanpuech, frmir and romeokienzler and removed request for albanpuech and romeokienzler May 19, 2026 18:56

@albanpuech albanpuech left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good.
However, here are many parts of the code in datakit and in graphkit that expect the buses to be sorted. Just to give one example: https://github.com/gridfm/gridfm-graphkit/blob/a5e6449f5fd5f77211ba22483ef8e7f3f14c6120/gridfm_graphkit/datasets/powergrid_hetero_dataset.py#L162

@yemine0x01

Copy link
Copy Markdown
Contributor Author

Thanks for the review, @albanpuech. Just to clarify, do we strictly need the buses to be ordered by ID? For context, these changes don't remove the existing bus ordering; they simply ensure the logic still functions correctly even if the buses happen to be unordered.

@albanpuech

Copy link
Copy Markdown
Collaborator

Thanks for the review, @albanpuech. Just to clarify, do we strictly need the buses to be ordered by ID? For context, these changes don't remove the existing bus ordering; they simply ensure the logic still functions correctly even if the buses happen to be unordered.

There are many places in the code that assume that, yes.
e.g.

Pg_bus = np.bincount(gen_bus, weights=pg_gen, minlength=n_buses)

or https://github.com/gridfm/gridfm-graphkit/blob/a5e6449f5fd5f77211ba22483ef8e7f3f14c6120/gridfm_graphkit/datasets/powergrid_hetero_dataset.py#L162

We would need to check everything and list where code needs to be changed

@frmir frmir self-assigned this May 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants