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

Add max iteration and NR epsilon to parameters #393

Merged
merged 1 commit into from
Dec 4, 2021
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
Expand Up @@ -10,6 +10,8 @@
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.extensions.AbstractExtension;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.openloadflow.ac.nr.DefaultNewtonRaphsonStoppingCriteria;
import com.powsybl.openloadflow.ac.nr.NewtonRaphsonParameters;
import com.powsybl.openloadflow.network.LfNetworkParameters;
import com.powsybl.openloadflow.network.SlackBusSelectionMode;

Expand Down Expand Up @@ -68,6 +70,10 @@ public enum LowImpedanceBranchMode {

private boolean reactivePowerRemoteControl = REACTIVE_POWER_REMOTE_CONTROL_DEFAULT_VALUE;

private int maxIteration = NewtonRaphsonParameters.DEFAULT_MAX_ITERATION;

private double newtonRaphsonConvEpsPerEq = DefaultNewtonRaphsonStoppingCriteria.DEFAULT_CONV_EPS_PER_EQ;

@Override
public String getName() {
return "open-load-flow-parameters";
Expand Down Expand Up @@ -180,6 +186,24 @@ public OpenLoadFlowParameters setReactivePowerRemoteControl(boolean reactivePowe
return this;
}

public int getMaxIteration() {
return maxIteration;
}

public OpenLoadFlowParameters setMaxIteration(int maxIteration) {
this.maxIteration = NewtonRaphsonParameters.checkMaxIteration(maxIteration);
return this;
}

public double getNewtonRaphsonConvEpsPerEq() {
return newtonRaphsonConvEpsPerEq;
}

public OpenLoadFlowParameters setNewtonRaphsonConvEpsPerEq(double newtonRaphsonConvEpsPerEq) {
this.newtonRaphsonConvEpsPerEq = newtonRaphsonConvEpsPerEq;
return this;
}

public static OpenLoadFlowParameters load() {
return new OpenLoadFlowConfigLoader().load(PlatformConfig.defaultConfig());
}
Expand All @@ -198,6 +222,8 @@ public String toString() {
", slackBusPMaxMismatch=" + slackBusPMaxMismatch +
", voltagePerReactivePowerControl=" + voltagePerReactivePowerControl +
", reactivePowerRemoteControl=" + reactivePowerRemoteControl +
", maxIteration=" + maxIteration +
", newtonRaphsonConvEpsPerEq=" + newtonRaphsonConvEpsPerEq +
')';
}

Expand Down Expand Up @@ -226,6 +252,10 @@ public static class OpenLoadFlowConfigLoader implements LoadFlowParameters.Confi

public static final String VOLTAGE_PER_REACTIVE_POWER_CONTROL_NAME = "voltagePerReactivePowerControl";

public static final String MAX_ITERATION_NAME = "maxIteration";

public static final String NEWTON_RAPHSON_CONV_EPS_PER_EQ_NAME = "newtonRaphsonConvEpsPerEq";

@Override
public OpenLoadFlowParameters load(PlatformConfig platformConfig) {
OpenLoadFlowParameters parameters = new OpenLoadFlowParameters();
Expand All @@ -243,8 +273,10 @@ public OpenLoadFlowParameters load(PlatformConfig platformConfig) {
.setPlausibleActivePowerLimit(config.getDoubleProperty(PLAUSIBLE_ACTIVE_POWER_LIMIT_PARAM_NAME, LfNetworkParameters.PLAUSIBLE_ACTIVE_POWER_LIMIT_DEFAULT_VALUE))
.setAddRatioToLinesWithDifferentNominalVoltageAtBothEnds(config.getBooleanProperty(ADD_RATIO_TO_LINES_WITH_DIFFERENT_NOMINAL_VOLTAGE_AT_BOTH_ENDS_NAME, ADD_RATIO_TO_LINES_WITH_DIFFERENT_NOMINAL_VOLTAGE_AT_BOTH_ENDS_DEFAULT_VALUE))
.setSlackBusPMaxMismatch(config.getDoubleProperty(SLACK_BUS_P_MAX_MISMATCH_NAME, SLACK_BUS_P_MAX_MISMATCH_DEFAULT_VALUE))
.setVoltagePerReactivePowerControl(VOLTAGE_PER_REACTIVE_POWER_CONTROL_DEFAULT_VALUE)
.setVoltagePerReactivePowerControl(config.getBooleanProperty(VOLTAGE_PER_REACTIVE_POWER_CONTROL_NAME, VOLTAGE_PER_REACTIVE_POWER_CONTROL_DEFAULT_VALUE))
.setReactivePowerRemoteControl(config.getBooleanProperty(REACTIVE_POWER_REMOTE_CONTROL_PARAM_NAME, REACTIVE_POWER_REMOTE_CONTROL_DEFAULT_VALUE))
.setMaxIteration(config.getIntProperty(MAX_ITERATION_NAME, NewtonRaphsonParameters.DEFAULT_MAX_ITERATION))
.setNewtonRaphsonConvEpsPerEq(config.getDoubleProperty(NEWTON_RAPHSON_CONV_EPS_PER_EQ_NAME, DefaultNewtonRaphsonStoppingCriteria.DEFAULT_CONV_EPS_PER_EQ))
);
return parameters;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.powsybl.math.matrix.SparseMatrixFactory;
import com.powsybl.openloadflow.ac.DefaultOuterLoopConfig;
import com.powsybl.openloadflow.ac.equations.AcEquationSystemCreationParameters;
import com.powsybl.openloadflow.ac.nr.DefaultNewtonRaphsonStoppingCriteria;
import com.powsybl.openloadflow.ac.nr.NewtonRaphsonParameters;
import com.powsybl.openloadflow.ac.nr.NewtonRaphsonStatus;
import com.powsybl.openloadflow.ac.outerloop.*;
Expand Down Expand Up @@ -187,7 +188,9 @@ public static AcLoadFlowParameters createAcParameters(Network network, MatrixFac
VoltageInitializer voltageInitializer = getVoltageInitializer(parameters, networkParameters, matrixFactory, reporter);

var newtonRaphsonParameters = new NewtonRaphsonParameters()
.setVoltageInitializer(voltageInitializer);
.setVoltageInitializer(voltageInitializer)
.setStoppingCriteria(new DefaultNewtonRaphsonStoppingCriteria(parametersExt.getNewtonRaphsonConvEpsPerEq()))
.setMaxIteration(parametersExt.getMaxIteration());

return new AcLoadFlowParameters(networkParameters,
equationSystemCreationParameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,23 @@ public class DefaultNewtonRaphsonStoppingCriteria implements NewtonRaphsonStoppi
/**
* Convergence epsilon per equation: 10^-4 in p.u => 10^-2 in Kv, Mw or MVar
*/
public static final double CONV_EPS_PER_EQ = Math.pow(10, -4);
public static final double DEFAULT_CONV_EPS_PER_EQ = Math.pow(10, -4);

private final double convEpsPerEq;

public DefaultNewtonRaphsonStoppingCriteria() {
this(DEFAULT_CONV_EPS_PER_EQ);
}

public DefaultNewtonRaphsonStoppingCriteria(double convEpsPerEq) {
this.convEpsPerEq = convEpsPerEq;
}

@Override
public TestResult test(double[] fx) {
// calculate norm L2 of equations mismatch vector
double norm = Vectors.norm2(fx);
boolean stop = norm < FastMath.sqrt(CONV_EPS_PER_EQ * CONV_EPS_PER_EQ * fx.length);
boolean stop = norm < FastMath.sqrt(convEpsPerEq * convEpsPerEq * fx.length);
return new TestResult(stop, norm);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class NewtonRaphsonParameters {

private static final int DEFAULT_MAX_ITERATION = 30;
public static final int DEFAULT_MAX_ITERATION = 30;

private int maxIteration = DEFAULT_MAX_ITERATION;

Expand All @@ -26,13 +26,17 @@ public int getMaxIteration() {
return maxIteration;
}

private final NewtonRaphsonStoppingCriteria stoppingCriteria = new DefaultNewtonRaphsonStoppingCriteria();
private NewtonRaphsonStoppingCriteria stoppingCriteria = new DefaultNewtonRaphsonStoppingCriteria();

public NewtonRaphsonParameters setMaxIteration(int maxIteration) {
public static int checkMaxIteration(int maxIteration) {
if (maxIteration < 1) {
throw new IllegalArgumentException("Invalid max iteration value: " + maxIteration);
}
this.maxIteration = maxIteration;
return maxIteration;
}

public NewtonRaphsonParameters setMaxIteration(int maxIteration) {
this.maxIteration = checkMaxIteration(maxIteration);
return this;
}

Expand All @@ -49,6 +53,11 @@ public NewtonRaphsonStoppingCriteria getStoppingCriteria() {
return stoppingCriteria;
}

public NewtonRaphsonParameters setStoppingCriteria(NewtonRaphsonStoppingCriteria stoppingCriteria) {
this.stoppingCriteria = Objects.requireNonNull(stoppingCriteria);
return this;
}

@Override
public String toString() {
return "NewtonRaphsonParameters(" +
Expand Down
4 changes: 3 additions & 1 deletion src/test/resources/debug-parameters.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
"plausibleActivePowerLimit" : 5000.0,
"addRatioToLinesWithDifferentNominalVoltageAtBothEnds" : false,
"slackBusPMaxMismatch" : 1.0,
"voltagePerReactivePowerControl" : false
"voltagePerReactivePowerControl" : false,
"maxIteration" : 30,
"newtonRaphsonConvEpsPerEq" : 1.0E-4
}
}
},
Expand Down