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

Share code for network modifications #524

Merged
merged 30 commits into from
Sep 26, 2024

Conversation

Mathieu-Deharbe
Copy link
Contributor

No description provided.

Mathieu-Deharbe and others added 17 commits August 28, 2024 10:44
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
…ions' into share-code-for-network-modifications
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
Signed-off-by: Mathieu DEHARBE <[email protected]>
@ghazwarhili ghazwarhili self-requested a review September 12, 2024 09:54
@@ -36,14 +43,28 @@ public static Double getReferenceValue(Battery battery, String batteryField) {

public static void setNewValue(Battery battery, String batteryField, Double newValue) {
BatteryField field = BatteryField.valueOf(batteryField);
final AttributeModification<Double> modif = new AttributeModification<>(newValue, OperationType.SET);
Copy link
Contributor

Choose a reason for hiding this comment

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

doubleAttributeModification ? or attributeModification
use variable name more descriptive

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is attributeModif enough ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok I renamed it to the whole attributeModification.

case ACTIVE_POWER_SET_POINT -> {
ModificationUtils.getInstance().checkActivePowerZeroOrBetweenMinAndMaxActivePower(
modif, null, null, battery.getMinP(),
battery.getMaxP(), battery.getTargetP(), MODIFY_GENERATOR_ERROR, "Battery '" + battery.getId() + "' : "
Copy link
Contributor

Choose a reason for hiding this comment

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

MODIFY_BATTERY_ERROR

ActivePowerControlAdder<Battery> activePowerControlAdder = battery.newExtension(ActivePowerControlAdder.class);
ModificationUtils.getInstance().modifyActivePowerControlAttributes(
activePowerControl, activePowerControlAdder, null,
new AttributeModification<>(newValue.floatValue(), OperationType.SET), null, null);
Copy link
Contributor

Choose a reason for hiding this comment

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

check for null before converting it to a float
you can use optional to handle null safely like
Optional.ofNullable(newValue) .map(Double::floatValue) ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added @NotNull to the Double newValue parameter.

import org.gridsuite.modification.server.dto.OperationType;
import org.gridsuite.modification.server.modifications.ModificationUtils;

import static org.gridsuite.modification.server.NetworkModificationException.Type.MODIFY_GENERATOR_ERROR;
Copy link
Contributor

Choose a reason for hiding this comment

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

Battery exception type !

GeneratorField field = GeneratorField.valueOf(generatorField);
final AttributeModification<Double> attrModif = new AttributeModification<>(newValue, OperationType.SET);
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

And same as well.

case PHASE_LOW_TAP_POSITION -> phaseTapChanger.setLowTapPosition(newValue.intValue());
case PHASE_TAP_POSITION -> phaseTapChanger.setTapPosition(newValue.intValue());
case PHASE_TARGET_DEADBAND -> phaseTapChanger.setTargetDeadband(newValue);
case R -> modifyBranchFields(transformer, attrModif, null, null);
Copy link
Contributor

Choose a reason for hiding this comment

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

modifyAttributeR ? or modifyR ?
same remarq for X attribute

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not sure why but those field got put together and called branch specific fields in the function modifyCharacteristics. They also have a BranchModificationInfos object joining them. So I kept them together.

But ok let's separate them. We'll see.

ReportNode subReporterSetpoints) {
List<ReportNode> reports = new ArrayList<>();
if (activePowerControl != null) {
modifyExistingActivePowerControl(activePowerControl, participateInfo, droopInfo, reports);
} else {
createNewActivePowerControl(activePowerControlAdder, participateInfo, droopInfo, reports);
}
if (subReportNode == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

why ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the end of the function is all about reports. But this is not clear, I reversed it. Let me know if this is ok.

AttributeModification<Double> ipMax,
ReportNode subReportNode,
VoltageLevel voltageLevel) {
if (ipMin == null && ipMax == null) {
Copy link
Contributor

Choose a reason for hiding this comment

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

we have to report this case ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so : in the case of a regular network modification most value are null (which means not modified). So this case is simply ignored. In the case of modification by formula, null fields are detected much sooner.

.buildModificationReport(oldIpMaxToReport, newIpMaxToReport, "High short circuit current limit"));
} else if (oldIpMax != null) {
identifiableShortCircuitAdder.withIpMax(oldIpMax);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

to optimize and refacto this code I suggest something like

     private void updateShortCircuitLimits(ValueWrapper ipMin, ValueWrapper ipMax, Double oldIpMin, Double oldIpMax, IdentifiableShortCircuitAdder identifiableShortCircuitAdder, List<ModificationReport> reports) {
        updateShortCircuitLimit(ipMin, oldIpMin, "Low short circuit current limit", identifiableShortCircuitAdder::withIpMin, reports);
        updateShortCircuitLimit(ipMax, oldIpMax, "High short circuit current limit", identifiableShortCircuitAdder::withIpMax, reports);
    }

    private void updateShortCircuitLimit(ValueWrapper newValueWrapper, Double oldValue, String limitDescription,
                                         Consumer<Double> setterMethod, List<ModificationReport> reports) {
        Double newValue = (newValueWrapper != null) ? newValueWrapper.getValue() : null;

        if (newValue != null) {
            setterMethod.accept(newValue);
            Double oldValueToReport = convertToKiloAmps(oldValue);
            Double newValueToReport = convertToKiloAmps(newValue);

            reports.add(ModificationUtils.getInstance().buildModificationReport(oldValueToReport, newValueToReport, limitDescription));
        } else if (oldValue != null) {
            setterMethod.accept(oldValue);
        }
    }

    private Double convertToKiloAmps(Double value) {
        return (value != null) ? value * 0.001 : null;
    }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks nice. I tried to integrate it but it broke the unit tests, I don't know why. So I just kept convertToKiloAmps.

double oldMagnetizingConductanceToReport = transformer.getG() * Math.pow(10, 6);
double newMagnetizingConductanceToReport = modifG.getValue() * Math.pow(10, 6);
insertReportNode(reportNode, ModificationUtils.getInstance().buildModificationReportWithIndentation(oldMagnetizingConductanceToReport,
newMagnetizingConductanceToReport, "Magnetizing conductance", 1));
Copy link
Contributor

Choose a reason for hiding this comment

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

Use Constants for Keys

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Like this ?

Copy link
Contributor

@EtienneLt EtienneLt left a comment

Choose a reason for hiding this comment

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

little comments

twoWindingsTransformer.setRatedU1(twoWindingsTransformerModificationInfos.getRatedU1().getValue());
}

public static void modifyBranchFields(TwoWindingsTransformer twt, AttributeModification<Double> modifR, AttributeModification<Double> modifX, ReportNode reportNode) {
Copy link
Contributor

Choose a reason for hiding this comment

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

change name and maybe separate in two methods

Copy link
Contributor

Choose a reason for hiding this comment

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

one for R and one for X

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha you agree with Ghazwa. Done.

Mathieu-Deharbe and others added 4 commits September 17, 2024 18:16
Signed-off-by: Mathieu DEHARBE <[email protected]>
…ion-server into share-code-for-network-modifications

Signed-off-by: Mathieu DEHARBE <[email protected]>

# Conflicts:
#	src/main/java/org/gridsuite/modification/server/modifications/GeneratorModification.java
#	src/main/java/org/gridsuite/modification/server/modifications/ModificationUtils.java
#	src/main/java/org/gridsuite/modification/server/modifications/TwoWindingsTransformerModification.java
Signed-off-by: Mathieu DEHARBE <[email protected]>
…ions' into share-code-for-network-modifications
Signed-off-by: Mathieu DEHARBE <[email protected]>
Copy link
Contributor Author

@Mathieu-Deharbe Mathieu-Deharbe left a comment

Choose a reason for hiding this comment

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

Ok I renamed it to the whole attributeModification.

Many merge conflicts are solved in the last commits.

@@ -36,14 +43,28 @@ public static Double getReferenceValue(Battery battery, String batteryField) {

public static void setNewValue(Battery battery, String batteryField, Double newValue) {
BatteryField field = BatteryField.valueOf(batteryField);
final AttributeModification<Double> modif = new AttributeModification<>(newValue, OperationType.SET);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is attributeModif enough ?

ActivePowerControlAdder<Battery> activePowerControlAdder = battery.newExtension(ActivePowerControlAdder.class);
ModificationUtils.getInstance().modifyActivePowerControlAttributes(
activePowerControl, activePowerControlAdder, null,
new AttributeModification<>(newValue.floatValue(), OperationType.SET), null, null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added @NotNull to the Double newValue parameter.

GeneratorField field = GeneratorField.valueOf(generatorField);
final AttributeModification<Double> attrModif = new AttributeModification<>(newValue, OperationType.SET);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

And same as well.

@@ -28,8 +33,8 @@ public static Double getReferenceValue(Load load, String loadField) {
public static void setNewValue(Load load, String loadField, Double newValue) {
LoadField field = LoadField.valueOf(loadField);
switch (field) {
case ACTIVE_POWER -> load.setP0(newValue);
case REACTIVE_POWER -> load.setQ0(newValue);
case ACTIVE_POWER -> modifyP0(load, new AttributeModification<>(newValue, OperationType.SET), null);
Copy link
Contributor Author

@Mathieu-Deharbe Mathieu-Deharbe Sep 17, 2024

Choose a reason for hiding this comment

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

But P0 is an attribute. Therefore attribute is implicit ?

case ACTIVE_POWER -> load.setP0(newValue);
case REACTIVE_POWER -> load.setQ0(newValue);
case ACTIVE_POWER -> modifyP0(load, new AttributeModification<>(newValue, OperationType.SET), null);
case REACTIVE_POWER -> modifyQ0(load, new AttributeModification<>(newValue, OperationType.SET), null);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Same remark.

twoWindingsTransformer.setRatedU1(twoWindingsTransformerModificationInfos.getRatedU1().getValue());
}

public static void modifyBranchFields(TwoWindingsTransformer twt, AttributeModification<Double> modifR, AttributeModification<Double> modifX, ReportNode reportNode) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Haha you agree with Ghazwa. Done.

AttributeModification<Double> ipMax,
ReportNode subReportNode,
VoltageLevel voltageLevel) {
if (ipMin == null && ipMax == null) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think so : in the case of a regular network modification most value are null (which means not modified). So this case is simply ignored. In the case of modification by formula, null fields are detected much sooner.

.buildModificationReport(oldIpMaxToReport, newIpMaxToReport, "High short circuit current limit"));
} else if (oldIpMax != null) {
identifiableShortCircuitAdder.withIpMax(oldIpMax);
}
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks nice. I tried to integrate it but it broke the unit tests, I don't know why. So I just kept convertToKiloAmps.

Copy link
Contributor

@EtienneLt EtienneLt left a comment

Choose a reason for hiding this comment

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

A few more comments and it is good for me

@@ -61,86 +66,42 @@ public static Double getReferenceValue(Generator generator, String generatorFiel
};
}

public static void setNewValue(Generator generator, String generatorField, Double newValue) {
public static void setNewValue(Generator generator, String generatorField, @NotNull Double newValue) {
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be better to set a primitive type (double), it forces to be not null

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Why not. Done.

}
}
report(formulaSubReporter, formulaReports);
Copy link
Contributor

Choose a reason for hiding this comment

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

are you sure ? the second

report(formulaSubReporter, formulaReports);

is in another if loop

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, pretty sure.
Previously if none of your equipment had been modified those reports were not displayed even if it was because of a problem in the network or the formula.
I think it is much better to have the details. This way the user can display the TRACE in the logs if he wants to, and see what really happened.

ReportNode reportReactivePower = null;
if (modifTargetQ != null) {
if (modifTargetQ.getOp() == OperationType.SET) {
reportReactivePower = ModificationUtils.getInstance().applyElementaryModificationsAndReturnReport(generator::setTargetQ, generator::getTargetQ, modifTargetQ, "Reactive power");
Copy link
Contributor

Choose a reason for hiding this comment

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

should it be specified that it is target reactive power or setpoint ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was simply refactoring that part but I think that you are right. Ok renamed.

Signed-off-by: Mathieu DEHARBE <[email protected]>
Copy link

@Mathieu-Deharbe Mathieu-Deharbe merged commit bc45748 into main Sep 26, 2024
3 checks passed
@Mathieu-Deharbe Mathieu-Deharbe deleted the share-code-for-network-modifications branch September 26, 2024 08:07
EtienneLt pushed a commit that referenced this pull request Oct 8, 2024
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.

4 participants