Skip to content

[Bug] ensureTopoVectIsUpToDate does not clear the topoChanges at the end of the execution in Grid2Op Backend #1187

Description

@RalfCortes

Describe the current behavior

L381 in ...grid2op/Backend.java it seems that the function ensureTopoVectIsUpToDate does not "empty" the topoChanges at the end.

I'm not too sure about the implication of this missing part, because if the order is preserved it shouldn't matter too much.

Yet I think we should still fix this by flushing the changes.

I plan to open a PR whenever I have time.

private void ensureTopoVectIsUpToDate() {
        if (!topoChanges.isEmpty()) {
            // apply changes on IIDM
            for (var topoChange : topoChanges) {
                if (topoChange.newBusId != null) {
                    topoChange.terminal.getBusBreakerView().setConnectableBus(topoChange.newBusId);
                }
                if (topoChange.connected) {
                    topoChange.terminal.connect();
                } else {
                    topoChange.terminal.disconnect();
                }
            }
            // some buses might have moved in or out of main CC, so we need to re-update all bus global nums
            for (int i = 0; i < loads.size(); i++) {
                Load load = loads.get(i);
                Bus bus = getBus(load.getTerminal());
                loadBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
            }
            for (int i = 0; i < generators.size(); i++) {
                Generator generator = generators.get(i);
                Bus bus = getBus(generator.getTerminal());
                generatorBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
            }
            for (int i = 0; i < shunts.size(); i++) {
                ShuntCompensator shunt = shunts.get(i);
                Bus bus = getBus(shunt.getTerminal());
                shuntBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
                shuntBusLocalNum.getPtr().write(i, globalToLocalBusNum(shuntBusGlobalNum[i]));
            }
            for (int i = 0; i < branches.size(); i++) {
                Branch<?> branch = branches.get(i);
                Bus bus1 = getBus(branch.getTerminal1());
                Bus bus2 = getBus(branch.getTerminal2());
                branchBusGlobalNum1[i] = bus1 == null ? -1 : busIdToGlobalNum.get(bus1.getId());
                branchBusGlobalNum2[i] = bus2 == null ? -1 : busIdToGlobalNum.get(bus2.getId());
            }
            // then we can update topo vect
            updateTopoVect();

        }
    }

Describe the expected behavior

Example of updated code :

private void ensureTopoVectIsUpToDate() {
        if (!topoChanges.isEmpty()) {
            // apply changes on IIDM
            for (var topoChange : topoChanges) {
                if (topoChange.newBusId != null) {
                    topoChange.terminal.getBusBreakerView().setConnectableBus(topoChange.newBusId);
                }
                if (topoChange.connected) {
                    topoChange.terminal.connect();
                } else {
                    topoChange.terminal.disconnect();
                }
            }
            /////////////////////////////////////// New code here ////////////////////////////////////////
            topoChanges.clear(); // or something similar
           //////////////////////////////////////////////////////////////////////////////////////////////////

            // some buses might have moved in or out of main CC, so we need to re-update all bus global nums
            for (int i = 0; i < loads.size(); i++) {
                Load load = loads.get(i);
                Bus bus = getBus(load.getTerminal());
                loadBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
            }
            for (int i = 0; i < generators.size(); i++) {
                Generator generator = generators.get(i);
                Bus bus = getBus(generator.getTerminal());
                generatorBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
            }
            for (int i = 0; i < shunts.size(); i++) {
                ShuntCompensator shunt = shunts.get(i);
                Bus bus = getBus(shunt.getTerminal());
                shuntBusGlobalNum[i] = bus == null ? -1 : busIdToGlobalNum.get(bus.getId());
                shuntBusLocalNum.getPtr().write(i, globalToLocalBusNum(shuntBusGlobalNum[i]));
            }
            for (int i = 0; i < branches.size(); i++) {
                Branch<?> branch = branches.get(i);
                Bus bus1 = getBus(branch.getTerminal1());
                Bus bus2 = getBus(branch.getTerminal2());
                branchBusGlobalNum1[i] = bus1 == null ? -1 : busIdToGlobalNum.get(bus1.getId());
                branchBusGlobalNum2[i] = bus2 == null ? -1 : busIdToGlobalNum.get(bus2.getId());
            }
            // then we can update topo vect
            updateTopoVect();
            
             



        }
    }

Describe the steps

No response

Environment

PP 1.14

Relevant Log Output

No response

Extra Information

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions