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

Newton raphson new stopping criteria #699

Merged
merged 38 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
af94038
WIP try adding new convergence criteria
lejeana Jan 9, 2023
8779d26
revert change from last commit
lejeana Jan 10, 2023
f38db60
feature(OPLF/ac): changed class NewtonRaphsonStoppingCriteria to requ…
lejeana Jan 10, 2023
418dd86
feat(OPLF/ac): added a new class that inherits from NewtonRaphsonStop…
lejeana Jan 10, 2023
ab658e8
feat(OPLF/ac): added possible usage in OpenLoadFlowParameters of the …
lejeana Jan 10, 2023
74ff9f6
fix(OPLF/parameters): added suffix DEFAULT_VALUE to constants that re…
lejeana Jan 16, 2023
1a735ea
refactor(OPLF): changed variable's name in enum NewtonRaphsonStopping…
lejeana Jan 16, 2023
88117d7
feat(OPLF/parameters): changed a return null to a throw exception for…
lejeana Jan 16, 2023
7599960
fix(OPLF): added Licence Identifier and fix author in file CustomNewt…
lejeana Jan 16, 2023
358d2e4
refactor(OPLF): drop legacy code
lejeana Jan 16, 2023
c9cc199
feat(OPLF/parameters): added get/set for new attributes
lejeana Jan 16, 2023
9b19840
fix(OPLF/parameters): updated toString() method
lejeana Jan 16, 2023
f2b57a6
fix(OPLF/NewtonCriteria): comparison is now with absolute value and c…
lejeana Jan 16, 2023
0854dd1
[WIP]feat(OPLF/NewtonCriteria): update switch to deal with all equati…
lejeana Jan 16, 2023
4e4f945
feat(OPLF/parameters): added in class OpenLoadFlowParameters an attri…
lejeana Jan 17, 2023
c431c95
fix(OPLF): added missing constants (PARAM_NAME) for new parameters pr…
lejeana Jan 17, 2023
68ad866
fix(OPLF/NewtonCriteria): update tests to pass the new modifications
lejeana Jan 17, 2023
d30c3d7
Rename stopping criteria and try unlock the ci.
obrix Jan 18, 2023
086ce75
fix(OPLF/NewtonCriteria): corrected default value of the maximum volt…
lejeana Jan 24, 2023
33e236a
refactoring(OPLF/NewtonCriteria): renamed variables as discussed in t…
lejeana Jan 24, 2023
9aba8ef
test(OPLF/NewtonCriteria): added new class test for Newton-Raphson st…
lejeana Jan 31, 2023
96d63ae
Update number of parameters after rebase.
obrix Jan 31, 2023
0fc5bda
feat(OPLF/NewtonCriteria): added access to mismatch value for angle, …
lejeana Jan 31, 2023
dbb75bf
test(OPLF/NewtonCriteria): added test for angle mismatch in NewtonRap…
lejeana Feb 1, 2023
8ba5f20
Merge branch 'main' into newtonraphsonnewstoppingcriteria
geofjamg Feb 7, 2023
135c9c0
Fix copyright and added missing @author
lejeana Feb 9, 2023
a0d9363
feat(OPLF/NewtonCriteria): added value requirements in new set() func…
lejeana Feb 9, 2023
ace5a61
refactor(OPLF/NewtonCriteria): refactored a function name from "gener…
lejeana Feb 9, 2023
bd837b7
test(OPLF/NewtonCriteria): added test for ratio and susceptance misma…
lejeana Feb 9, 2023
8a58a90
fix(OPLF/NewtonCriteria): added missing enumeration of possible value…
lejeana Feb 9, 2023
e908e1b
test(OPLF/NewtonCriteria): added tests for throw of PowsyblException
lejeana Feb 9, 2023
6424463
Merge branch 'main' into newtonraphsonnewstoppingcriteria
geofjamg Feb 9, 2023
8c2fe44
Fix
geofjamg Feb 9, 2023
6f5950d
Fix MacOS build
geofjamg Feb 9, 2023
185c8b7
Merge branch 'main' into newtonraphsonnewstoppingcriteria
geofjamg Feb 10, 2023
99297c0
feat(OPLF/NewtonCriteria): added default case in switch
lejeana Feb 10, 2023
ed50bf0
fix(OPLF/NewtonCriteria): mismatch value must be strictly positive, c…
lejeana Feb 10, 2023
a58ab85
Merge branch 'main' into newtonraphsonnewstoppingcriteria
geofjamg Feb 10, 2023
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
183 changes: 178 additions & 5 deletions src/main/java/com/powsybl/openloadflow/OpenLoadFlowParameters.java

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
package com.powsybl.openloadflow.ac.nr;

import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.Vectors;
import net.jafama.FastMath;

Expand All @@ -30,7 +33,7 @@ public DefaultNewtonRaphsonStoppingCriteria(double convEpsPerEq) {
}

@Override
public TestResult test(double[] fx) {
public TestResult test(double[] fx, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
// calculate norm L2 of equations mismatch vector
double norm = Vectors.norm2(fx);
boolean stop = norm < FastMath.sqrt(convEpsPerEq * convEpsPerEq * fx.length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,12 @@ public void apply(double[] dx, EquationSystem<AcVariableType, AcEquationType> eq
}

@Override
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(StateVector stateVector,
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(EquationSystem<AcVariableType, AcEquationType> equationSystem,
EquationVector<AcVariableType, AcEquationType> equationVector,
TargetVector<AcVariableType, AcEquationType> targetVector,
NewtonRaphsonStoppingCriteria stoppingCriteria,
NewtonRaphsonStoppingCriteria.TestResult testResult) {
StateVector stateVector = equationSystem.getStateVector();
if (lastTestResult != null) {
double stepSize = 1;
NewtonRaphsonStoppingCriteria.TestResult currentTestResult = testResult;
Expand All @@ -78,7 +79,7 @@ public NewtonRaphsonStoppingCriteria.TestResult applyAfter(StateVector stateVect
equationVector.minus(targetVector);

// and recompute new norm
currentTestResult = stoppingCriteria.test(equationVector.getArray());
currentTestResult = stoppingCriteria.test(equationVector.getArray(), equationSystem);

iteration++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.EquationVector;
import com.powsybl.openloadflow.equations.StateVector;
import com.powsybl.openloadflow.equations.TargetVector;
import com.powsybl.openloadflow.equations.Vectors;
import org.slf4j.Logger;
Expand Down Expand Up @@ -80,7 +79,7 @@ public void apply(double[] dx, EquationSystem<AcVariableType, AcEquationType> eq
}

@Override
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(StateVector stateVector,
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(EquationSystem<AcVariableType, AcEquationType> equationSystem,
EquationVector<AcVariableType, AcEquationType> equationVector,
TargetVector<AcVariableType, AcEquationType> targetVector,
NewtonRaphsonStoppingCriteria stoppingCriteria,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ private NewtonRaphsonStatus runIteration(StateVectorScaling svScaling) {
}

// test stopping criteria and log norm(fx)
NewtonRaphsonStoppingCriteria.TestResult testResult = parameters.getStoppingCriteria().test(equationVector.getArray());
NewtonRaphsonStoppingCriteria.TestResult testResult = parameters.getStoppingCriteria().test(equationVector.getArray(), equationSystem);

testResult = svScaling.applyAfter(equationSystem.getStateVector(), equationVector, targetVector,
testResult = svScaling.applyAfter(equationSystem, equationVector, targetVector,
parameters.getStoppingCriteria(), testResult);

LOGGER.debug("|f(x)|={}", testResult.getNorm());
Expand Down Expand Up @@ -214,7 +214,7 @@ public NewtonRaphsonResult run(VoltageInitializer voltageInitializer) {

Vectors.minus(equationVector.getArray(), targetVector.getArray());

NewtonRaphsonStoppingCriteria.TestResult initialTestResult = parameters.getStoppingCriteria().test(equationVector.getArray());
NewtonRaphsonStoppingCriteria.TestResult initialTestResult = parameters.getStoppingCriteria().test(equationVector.getArray(), equationSystem);
LOGGER.debug("|f(x0)|={}", initialTestResult.getNorm());

StateVectorScaling svScaling = StateVectorScaling.fromMode(parameters.getStateVectorScalingMode(), initialTestResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
*/
package com.powsybl.openloadflow.ac.nr;

import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;

/**
* @author Geoffroy Jamgotchian <geoffroy.jamgotchian at rte-france.com>
*/
Expand All @@ -31,5 +35,5 @@ public double getNorm() {
}
}

TestResult test(double[] fx);
TestResult test(double[] fx, EquationSystem<AcVariableType, AcEquationType> equationSystem);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright (c) 2023, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/)
* 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.openloadflow.ac.nr;

/**
* @author Alexandre Le Jean <alexandre.le-jean at artelys.com>
*/
public enum NewtonRaphsonStoppingCriteriaType {
lejeana marked this conversation as resolved.
Show resolved Hide resolved
UNIFORM_CRITERIA,
PER_EQUATION_TYPE_CRITERIA
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.EquationVector;
import com.powsybl.openloadflow.equations.StateVector;
import com.powsybl.openloadflow.equations.TargetVector;

/**
Expand All @@ -30,7 +29,7 @@ public void apply(double[] dx, EquationSystem<AcVariableType, AcEquationType> eq
}

@Override
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(StateVector stateVector,
public NewtonRaphsonStoppingCriteria.TestResult applyAfter(EquationSystem<AcVariableType, AcEquationType> equationSystem,
EquationVector<AcVariableType, AcEquationType> equationVector,
TargetVector<AcVariableType, AcEquationType> targetVector,
NewtonRaphsonStoppingCriteria stoppingCriteria,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/**
* Copyright (c) 2023, Coreso SA (https://www.coreso.eu/) and TSCNET Services GmbH (https://www.tscnet.eu/)
* 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.openloadflow.ac.nr;

import com.powsybl.commons.PowsyblException;
import com.powsybl.openloadflow.ac.equations.AcEquationType;
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.Vectors;
import com.powsybl.openloadflow.util.PerUnit;

/**
* @author Alexandre Le Jean <alexandre.le-jean at artelys.com>
*/
public class PerEquationTypeStoppingCriteria implements NewtonRaphsonStoppingCriteria {

private final double maxDefaultAngleMismatch;

private final double maxDefaultRatioMismatch;

private final double maxDefaultSusceptanceMismatch;

private final double maxActivePowerMismatch;

private final double maxReactivePowerMismatch;

private final double maxVoltageMismatch;

public PerEquationTypeStoppingCriteria(double maxActivePowerMismatch,
double maxReactivePowerMismatch, double maxVoltageMismatch,
double maxDefaultAngleMismatch, double maxDefaultRatioMismatch,
double maxDefaultSusceptanceMismatch) {
this.maxActivePowerMismatch = maxActivePowerMismatch;
this.maxReactivePowerMismatch = maxReactivePowerMismatch;
this.maxVoltageMismatch = maxVoltageMismatch;
this.maxDefaultAngleMismatch = maxDefaultAngleMismatch;
this.maxDefaultRatioMismatch = maxDefaultRatioMismatch;
this.maxDefaultSusceptanceMismatch = maxDefaultSusceptanceMismatch;
}

private double computeNorm(double[] fx) {
return Vectors.norm2(fx);
}

private boolean computeStop(double[] fx, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
for (var eq : equationSystem.getIndex().getSortedEquationsToSolve()) {
var type = eq.getType();
var idx = eq.getColumn();
switch (type) {
case BRANCH_TARGET_P:
case BUS_TARGET_P:
case DUMMY_TARGET_P:
if (Math.abs(fx[idx]) * PerUnit.SB >= maxActivePowerMismatch) {
return false;
}
break;
case BRANCH_TARGET_Q:
case BUS_TARGET_Q:
case DISTR_Q:
case DUMMY_TARGET_Q:
if (Math.abs(fx[idx]) * PerUnit.SB >= maxReactivePowerMismatch) {
return false;
}
break;
case BUS_TARGET_V:
case BUS_TARGET_V_WITH_SLOPE:
case ZERO_V:
if (Math.abs(fx[idx]) >= maxVoltageMismatch) {
return false;
}
break;
case BRANCH_TARGET_RHO1:
case DISTR_RHO:
if (Math.abs(fx[idx]) >= maxDefaultRatioMismatch) {
return false;
}
break;
case DISTR_SHUNT_B:
case SHUNT_TARGET_B:
if (Math.abs(fx[idx]) >= maxDefaultSusceptanceMismatch) {
return false;
}
break;
case BUS_TARGET_PHI:
case ZERO_PHI:
case BRANCH_TARGET_ALPHA1:
if (Math.abs(fx[idx]) >= maxDefaultAngleMismatch) {
return false;
}
break;
default:
throw new PowsyblException("Unknown equation term");
}
}
return true;
}

@Override
public TestResult test(double[] fx, EquationSystem<AcVariableType, AcEquationType> equationSystem) {
return new TestResult(computeStop(fx, equationSystem), computeNorm(fx));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.openloadflow.ac.equations.AcVariableType;
import com.powsybl.openloadflow.equations.EquationSystem;
import com.powsybl.openloadflow.equations.EquationVector;
import com.powsybl.openloadflow.equations.StateVector;
import com.powsybl.openloadflow.equations.TargetVector;

import java.util.Objects;
Expand Down Expand Up @@ -48,7 +47,7 @@ static StateVectorScaling fromMode(StateVectorScalingMode mode, NewtonRaphsonSto
/**
* Apply scaling to state vector after equation mismatches and norm have been calculated.
*/
NewtonRaphsonStoppingCriteria.TestResult applyAfter(StateVector stateVector,
NewtonRaphsonStoppingCriteria.TestResult applyAfter(EquationSystem<AcVariableType, AcEquationType> equationSystem,
EquationVector<AcVariableType, AcEquationType> equationVector,
TargetVector<AcVariableType, AcEquationType> targetVector,
NewtonRaphsonStoppingCriteria stoppingCriteria,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.InMemoryPlatformConfig;
import com.powsybl.commons.config.MapModuleConfig;
import com.powsybl.commons.config.PlatformConfig;
Expand Down Expand Up @@ -115,6 +116,22 @@ void testInvalidOpenLoadflowConfig() {
assertEquals("No enum constant com.powsybl.openloadflow.network.SlackBusSelectionMode.Invalid", exception.getMessage());
}

@Test
void testInvalidOpenLoadflowConfigNewtonRaphson() {
MapModuleConfig olfModuleConfig = platformConfig.createModuleConfig("open-loadflow-default-parameters");
olfModuleConfig.setStringProperty("newtonRaphsonStoppingCriteriaType", "Invalid");

IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> LoadFlowParameters.load(platformConfig));
assertEquals("No enum constant com.powsybl.openloadflow.ac.nr.NewtonRaphsonStoppingCriteriaType.Invalid", exception.getMessage());

OpenLoadFlowParameters openLoadFlowParameters = OpenLoadFlowParameters.create(new LoadFlowParameters());
assertThrows(PowsyblException.class, () -> openLoadFlowParameters.setMaxAngleMismatch(-1));
assertThrows(PowsyblException.class, () -> openLoadFlowParameters.setMaxVoltageMismatch(-1));
assertThrows(PowsyblException.class, () -> openLoadFlowParameters.setMaxRatioMismatch(-1));
assertThrows(PowsyblException.class, () -> openLoadFlowParameters.setMaxActivePowerMismatch(-1));
assertThrows(PowsyblException.class, () -> openLoadFlowParameters.setMaxReactivePowerMismatch(-1));
}

@Test
void testFirstSlackBusSelector() throws IOException {
Path cfgDir = Files.createDirectory(fileSystem.getPath("config"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ void testGetExtendedVoltageInitializer() {
@Test
void specificParametersTest() {
OpenLoadFlowProvider provider = new OpenLoadFlowProvider();
assertEquals(30, provider.getSpecificParameters().size());
assertEquals(37, provider.getSpecificParameters().size());
LoadFlowParameters parameters = new LoadFlowParameters();

provider.loadSpecificParameters(Collections.emptyMap())
Expand Down
Loading