Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Detect invalid element overrides #203

Merged
merged 5 commits into from
Mar 18, 2024
Merged

Detect invalid element overrides #203

merged 5 commits into from
Mar 18, 2024

Conversation

alihamdan
Copy link
Member

Fixes #186

@alihamdan alihamdan added the enhancement New feature or request label Mar 18, 2024
@alihamdan alihamdan self-assigned this Mar 18, 2024
@Saelyos
Copy link
Collaborator

Saelyos commented Mar 18, 2024

Hello, I'm not sure of the problem you are trying to solve.

If we look at the example linked in #186, and if we run the following code:

import roseau.load_flow as lf
bus = lf.Bus("bus1", phases="an")
load1 = lf.PowerLoad("load1", bus, powers=[1000])
load2 = lf.PowerLoad("load1", bus, powers=[1000])  # <- oops, forgot to change the ID
en = lf.ElectricalNetwork.from_element(bus)

We get the right error message:

roseau.load_flow.exceptions.RoseauLoadFlowException: Duplicate ID for an load in this network: 'load1'. [bad_load_id]

However the problem could arise if we connect the load later on (I had to add a few elements):

import roseau.load_flow as lf
bus = lf.Bus("bus1", phases="an")
vs = lf.VoltageSource("source", bus, voltages=[10])
pref = lf.PotentialRef("pref", bus)
load1 = lf.PowerLoad("load1", bus, powers=[1000])
en = lf.ElectricalNetwork.from_element(bus)
load2 = lf.PowerLoad("load1", bus, powers=[1000])  # <- oops, forgot to change the ID

In that case no error message is currently raised, but it should be easy to add in the ElectricalNetwork._connect_element method something like:

elif isinstance(element, AbstractLoad):
    if element.id in self.loads:
        raise RoseauLoadFlowException("Duplicate ID...", code=error_code)
    self.loads[element.id] = element

This seems simpler than modifying every elements _connected_elements attributes. What do you think ?

@alihamdan
Copy link
Member Author

However the problem could arise if we connect the load later on

Yes, this is the main problem we are trying to solve.

This seems simpler than modifying every elements _connected_elements attributes. What do you think ?

You are right, and that's why the PR is still a draft 😉.
I started by changing the _connected_elements to a dictionary and adding the check there following the suggeston of Benoît but then realized that this does not detect all cases and we have to modify the network's _connect_element method instead anyway.

@alihamdan alihamdan marked this pull request as ready for review March 18, 2024 11:51
@alihamdan alihamdan requested review from Saelyos and benoit9126 March 18, 2024 11:51
roseau/load_flow/network.py Outdated Show resolved Hide resolved
@alihamdan alihamdan merged commit 7d02ca7 into develop Mar 18, 2024
4 checks passed
@alihamdan alihamdan deleted the invalid-override branch March 18, 2024 12:49
@benoit9126 benoit9126 mentioned this pull request Mar 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

ENH: Detect invalid element overrides
3 participants