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

Factorize PhaseTapChangerAdderImpl and RatioTapChangerAdderImpl #3033

Merged
merged 3 commits into from
May 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.iidm.network.impl;

import com.powsybl.commons.report.TypedValue;
import com.powsybl.iidm.network.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* @author Florian Dupuy {@literal <florian.dupuy at rte-france.com>}
*/
abstract class AbstractTapChangerAdderImpl<
A extends AbstractTapChangerAdderImpl<A, H, T, S>,
H extends TapChangerParent,
T extends TapChanger<T, ?, ?, ?>,
S extends TapChangerStepImpl<S>> {

private static final Logger LOGGER = LoggerFactory.getLogger(AbstractTapChangerAdderImpl.class);

protected final H parent;
private int lowTapPosition = 0;
private Integer tapPosition;
protected final List<S> steps;
private double regulationValue = Double.NaN;
private boolean regulating = false;
private double targetDeadband = Double.NaN;
private TerminalExt regulationTerminal;

protected AbstractTapChangerAdderImpl(H parent) {
this.parent = parent;
this.steps = new ArrayList<>();
}

public A setLowTapPosition(int lowTapPosition) {
this.lowTapPosition = lowTapPosition;
return self();
}

public A setTapPosition(int tapPosition) {
this.tapPosition = tapPosition;
return self();
}

public A setRegulationValue(double regulationValue) {
this.regulationValue = regulationValue;
return self();
}

public A setRegulating(boolean regulating) {
this.regulating = regulating;
return self();
}

public A setTargetDeadband(double targetDeadband) {
this.targetDeadband = targetDeadband;
return self();
}

public A setRegulationTerminal(Terminal regulationTerminal) {
this.regulationTerminal = (TerminalExt) regulationTerminal;
return self();
}

NetworkImpl getNetwork() {
return parent.getNetwork();
}

public T add() {
NetworkImpl network = getNetwork();
if (tapPosition == null) {
ValidationUtil.throwExceptionOrLogError(parent, "tap position is not set", network.getMinValidationLevel());
network.setValidationLevelIfGreaterThan(ValidationLevel.EQUIPMENT);
}
if (steps.isEmpty()) {
throw new ValidationException(parent, getValidableType() + " should have at least one step");
}
if (tapPosition != null) {
int highTapPosition = lowTapPosition + steps.size() - 1;
if (tapPosition < lowTapPosition || tapPosition > highTapPosition) {
ValidationUtil.throwExceptionOrLogError(parent, "incorrect tap position "
+ tapPosition + " [" + lowTapPosition + ", "
+ highTapPosition + "]", network.getMinValidationLevel());
network.setValidationLevelIfGreaterThan(ValidationLevel.EQUIPMENT);
}
}

network.setValidationLevelIfGreaterThan(checkTapChangerRegulation(parent, regulationValue, regulating, regulationTerminal));
network.setValidationLevelIfGreaterThan(ValidationUtil.checkTargetDeadband(parent, getValidableType(), regulating,
targetDeadband, network.getMinValidationLevel()));

T tapChanger = createTapChanger(parent, lowTapPosition, steps, regulationTerminal, tapPosition, regulating, regulationValue, targetDeadband);

Set<TapChanger<?, ?, ?, ?>> otherTapChangers = new HashSet<>(parent.getAllTapChangers());
otherTapChangers.remove(tapChanger);
network.setValidationLevelIfGreaterThan(ValidationUtil.checkOnlyOneTapChangerRegulatingEnabled(parent, otherTapChangers, regulating,
network.getMinValidationLevel().compareTo(ValidationLevel.STEADY_STATE_HYPOTHESIS) >= 0));

if (parent.hasPhaseTapChanger() && parent.hasRatioTapChanger()) {
LOGGER.warn("{} has both Ratio and Phase Tap Changer", parent);
network.getReportNodeContext().getReportNode().newReportNode()
.withMessageTemplate("validationWarning", "${parent} has both Ratio and Phase Tap Changer.")
.withUntypedValue("parent", parent.getMessageHeader())
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}

return tapChanger;
}

protected abstract T createTapChanger(H parent, int lowTapPosition, List<S> steps, TerminalExt regulationTerminal, Integer tapPosition, boolean regulating, double regulationValue, double targetDeadband);

protected abstract A self();

protected abstract ValidationLevel checkTapChangerRegulation(H parent, double regulationValue, boolean regulating, TerminalExt regulationTerminal);

protected abstract String getValidableType();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,42 +7,21 @@
*/
package com.powsybl.iidm.network.impl;

import com.powsybl.commons.report.TypedValue;
import com.powsybl.iidm.network.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.powsybl.iidm.network.PhaseTapChanger;
import com.powsybl.iidm.network.PhaseTapChangerAdder;
import com.powsybl.iidm.network.ValidationLevel;
import com.powsybl.iidm.network.ValidationUtil;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
*
* @author Geoffroy Jamgotchian {@literal <geoffroy.jamgotchian at rte-france.com>}
*/
class PhaseTapChangerAdderImpl implements PhaseTapChangerAdder {

private static final Logger LOGGER = LoggerFactory.getLogger(PhaseTapChangerAdderImpl.class);

private final PhaseTapChangerParent parent;

private int lowTapPosition = 0;

private Integer tapPosition;

private final List<PhaseTapChangerStepImpl> steps = new ArrayList<>();
class PhaseTapChangerAdderImpl extends AbstractTapChangerAdderImpl<PhaseTapChangerAdderImpl, PhaseTapChangerParent, PhaseTapChanger, PhaseTapChangerStepImpl> implements PhaseTapChangerAdder {

private PhaseTapChanger.RegulationMode regulationMode = PhaseTapChanger.RegulationMode.FIXED_TAP;

private double regulationValue = Double.NaN;

private boolean regulating = false;

private double targetDeadband = Double.NaN;

private TerminalExt regulationTerminal;

class StepAdderImpl implements PhaseTapChangerAdder.StepAdder {

private double alpha = Double.NaN;
Expand Down Expand Up @@ -104,23 +83,7 @@ public PhaseTapChangerAdder endStep() {
}

PhaseTapChangerAdderImpl(PhaseTapChangerParent parent) {
this.parent = parent;
}

NetworkImpl getNetwork() {
return parent.getNetwork();
}

@Override
public PhaseTapChangerAdder setLowTapPosition(int lowTapPosition) {
this.lowTapPosition = lowTapPosition;
return this;
}

@Override
public PhaseTapChangerAdder setTapPosition(int tapPosition) {
this.tapPosition = tapPosition;
return this;
super(parent);
}

@Override
Expand All @@ -130,76 +93,30 @@ public PhaseTapChangerAdder setRegulationMode(PhaseTapChanger.RegulationMode reg
}

@Override
public PhaseTapChangerAdder setRegulationValue(double regulationValue) {
this.regulationValue = regulationValue;
return this;
public PhaseTapChangerAdder.StepAdder beginStep() {
return new StepAdderImpl();
}

@Override
public PhaseTapChangerAdder setRegulating(boolean regulating) {
this.regulating = regulating;
return this;
protected PhaseTapChanger createTapChanger(PhaseTapChangerParent parent, int lowTapPosition, List<PhaseTapChangerStepImpl> steps, TerminalExt regulationTerminal, Integer tapPosition, boolean regulating, double regulationValue, double targetDeadband) {
PhaseTapChangerImpl tapChanger = new PhaseTapChangerImpl(parent, lowTapPosition, steps, regulationTerminal, tapPosition, regulating, regulationMode, regulationValue, targetDeadband);
parent.setPhaseTapChanger(tapChanger);
return tapChanger;
}

@Override
public PhaseTapChangerAdder setTargetDeadband(double targetDeadband) {
this.targetDeadband = targetDeadband;
protected PhaseTapChangerAdderImpl self() {
return this;
}

@Override
public PhaseTapChangerAdder setRegulationTerminal(Terminal regulationTerminal) {
this.regulationTerminal = (TerminalExt) regulationTerminal;
return this;
protected ValidationLevel checkTapChangerRegulation(PhaseTapChangerParent parent, double regulationValue, boolean regulating, TerminalExt regulationTerminal) {
return ValidationUtil.checkPhaseTapChangerRegulation(parent, regulationMode, regulationValue, regulating,
regulationTerminal, getNetwork(), getNetwork().getMinValidationLevel().compareTo(ValidationLevel.STEADY_STATE_HYPOTHESIS) >= 0);
}

@Override
public PhaseTapChangerAdder.StepAdder beginStep() {
return new StepAdderImpl();
protected String getValidableType() {
return "phase tap changer";
}

@Override
public PhaseTapChanger add() {
NetworkImpl network = getNetwork();
if (tapPosition == null) {
ValidationUtil.throwExceptionOrLogError(parent, "tap position is not set", network.getMinValidationLevel());
network.setValidationLevelIfGreaterThan(ValidationLevel.EQUIPMENT);
}
if (steps.isEmpty()) {
throw new ValidationException(parent, "a phase tap changer shall have at least one step");
}
if (tapPosition != null) {
int highTapPosition = lowTapPosition + steps.size() - 1;
if (tapPosition < lowTapPosition || tapPosition > highTapPosition) {
ValidationUtil.throwExceptionOrLogError(parent, "incorrect tap position "
+ tapPosition + " [" + lowTapPosition + ", "
+ highTapPosition + "]", network.getMinValidationLevel());
network.setValidationLevelIfGreaterThan(ValidationLevel.EQUIPMENT);
}
}
network.setValidationLevelIfGreaterThan(ValidationUtil.checkPhaseTapChangerRegulation(parent, regulationMode, regulationValue, regulating,
regulationTerminal, network, network.getMinValidationLevel().compareTo(ValidationLevel.STEADY_STATE_HYPOTHESIS) >= 0));
network.setValidationLevelIfGreaterThan(ValidationUtil.checkTargetDeadband(parent, "phase tap changer", regulating,
targetDeadband, network.getMinValidationLevel()));
PhaseTapChangerImpl tapChanger
= new PhaseTapChangerImpl(parent, lowTapPosition, steps, regulationTerminal, tapPosition, regulating, regulationMode, regulationValue, targetDeadband);

Set<TapChanger<?, ?, ?, ?>> tapChangers = new HashSet<>(parent.getAllTapChangers());
tapChangers.remove(parent.getPhaseTapChanger());
network.setValidationLevelIfGreaterThan(ValidationUtil.checkOnlyOneTapChangerRegulatingEnabled(parent, tapChangers,
regulating, network.getMinValidationLevel().compareTo(ValidationLevel.STEADY_STATE_HYPOTHESIS) >= 0));

if (parent.hasRatioTapChanger()) {
LOGGER.warn("{} has both Ratio and Phase Tap Changer", parent);
network.getReportNodeContext().getReportNode().newReportNode()
.withMessageTemplate("validationWarning", "${parent} has both Ratio and Phase Tap Changer.")
.withUntypedValue("parent", parent.getMessageHeader())
.withSeverity(TypedValue.WARN_SEVERITY)
.add();
}

parent.setPhaseTapChanger(tapChanger);
return tapChanger;
}

}
Loading