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 option to write inactive equations and terms #423

Merged
merged 2 commits into from
Jan 17, 2022
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
10 changes: 8 additions & 2 deletions src/main/java/com/powsybl/openloadflow/equations/Equation.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,20 @@ public int compareTo(Equation<V, E> o) {
return c;
}

public void write(Writer writer) throws IOException {
public void write(Writer writer, boolean writeInactiveTerms) throws IOException {
writer.append(type.getSymbol())
.append(Integer.toString(elementNum))
.append(" = ");
List<EquationTerm<V, E>> activeTerms = terms.stream().filter(EquationTerm::isActive).collect(Collectors.toList());
List<EquationTerm<V, E>> activeTerms = writeInactiveTerms ? terms : terms.stream().filter(EquationTerm::isActive).collect(Collectors.toList());
for (Iterator<EquationTerm<V, E>> it = activeTerms.iterator(); it.hasNext();) {
EquationTerm<V, E> term = it.next();
if (!term.isActive()) {
writer.write("[ ");
}
term.write(writer);
if (!term.isActive()) {
writer.write(" ]");
}
if (it.hasNext()) {
writer.append(" + ");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.*;
Expand Down Expand Up @@ -344,11 +345,17 @@ void notifyEquationTermChange(EquationTerm<V, E> term, EquationTermEventType eve
listeners.forEach(listener -> listener.onEquationTermChange(term, eventType));
}

public void write(Writer writer) {
public void write(Writer writer, boolean writeInactiveEquations) {
try {
for (Equation<V, E> equation : getSortedEquationsToSolve().navigableKeySet()) {
if (equation.isActive()) {
equation.write(writer);
for (Equation<V, E> equation : equations.values().stream().sorted().collect(Collectors.toList())) {
if (writeInactiveEquations || equation.isActive()) {
if (!equation.isActive()) {
writer.write("[ ");
}
equation.write(writer, writeInactiveEquations);
if (!equation.isActive()) {
writer.write(" ]");
}
writer.write(System.lineSeparator());
}
}
Expand All @@ -358,6 +365,20 @@ public void write(Writer writer) {
}
}

public String writeToString(boolean writeInactiveEquations) {
try (StringWriter writer = new StringWriter()) {
write(writer, writeInactiveEquations);
writer.flush();
return writer.toString();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

public String writeToString() {
return writeToString(false);
}

public List<Pair<Equation<V, E>, Double>> findLargestMismatches(double[] mismatch, int count) {
return getSortedEquationsToSolve().keySet().stream()
.map(equation -> Pair.of(equation, mismatch[equation.getColumn()]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
import org.apache.commons.lang3.tuple.Pair;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -123,45 +121,65 @@ public void onEquationTermChange(EquationTerm<AcVariableType, AcEquationType> te
}

@Test
void writeAcSystemTest() throws IOException {
void writeAcSystemTest() {
List<LfNetwork> lfNetworks = Networks.load(EurostagTutorialExample1Factory.create(), new FirstSlackBusSelector());
LfNetwork network = lfNetworks.get(0);

EquationSystem<AcVariableType, AcEquationType> equationSystem = AcEquationSystem.create(network);
try (StringWriter writer = new StringWriter()) {
equationSystem.write(writer);
writer.flush();
String ref = String.join(System.lineSeparator(),
"bus_target_v0 = v0",
"bus_target_φ0 = φ0",
"bus_target_p1 = ac_p_closed_2(v0, v1, φ0, φ1) + ac_p_closed_1(v1, v2, φ1, φ2) + ac_p_closed_1(v1, v2, φ1, φ2)",
"bus_target_q1 = ac_q_closed_2(v0, v1, φ0, φ1) + ac_q_closed_1(v1, v2, φ1, φ2) + ac_q_closed_1(v1, v2, φ1, φ2)",
"bus_target_p2 = ac_p_closed_2(v1, v2, φ1, φ2) + ac_p_closed_2(v1, v2, φ1, φ2) + ac_p_closed_1(v2, v3, φ2, φ3)",
"bus_target_q2 = ac_q_closed_2(v1, v2, φ1, φ2) + ac_q_closed_2(v1, v2, φ1, φ2) + ac_q_closed_1(v2, v3, φ2, φ3)",
"bus_target_p3 = ac_p_closed_2(v2, v3, φ2, φ3)",
"bus_target_q3 = ac_q_closed_2(v2, v3, φ2, φ3)")
+ System.lineSeparator();
assertEquals(ref, writer.toString());
String ref = String.join(System.lineSeparator(),
"bus_target_v0 = v0",
"bus_target_φ0 = φ0",
"bus_target_p1 = ac_p_closed_2(v0, v1, φ0, φ1) + ac_p_closed_1(v1, v2, φ1, φ2) + ac_p_closed_1(v1, v2, φ1, φ2)",
"bus_target_q1 = ac_q_closed_2(v0, v1, φ0, φ1) + ac_q_closed_1(v1, v2, φ1, φ2) + ac_q_closed_1(v1, v2, φ1, φ2)",
"bus_target_p2 = ac_p_closed_2(v1, v2, φ1, φ2) + ac_p_closed_2(v1, v2, φ1, φ2) + ac_p_closed_1(v2, v3, φ2, φ3)",
"bus_target_q2 = ac_q_closed_2(v1, v2, φ1, φ2) + ac_q_closed_2(v1, v2, φ1, φ2) + ac_q_closed_1(v2, v3, φ2, φ3)",
"bus_target_p3 = ac_p_closed_2(v2, v3, φ2, φ3)",
"bus_target_q3 = ac_q_closed_2(v2, v3, φ2, φ3)")
+ System.lineSeparator();
assertEquals(ref, equationSystem.writeToString());
}

@Test
void writeAllEquationsAcSystemTest() {
List<LfNetwork> lfNetworks = Networks.load(EurostagTutorialExample1Factory.create(), new FirstSlackBusSelector());
LfNetwork network = lfNetworks.get(0);

EquationSystem<AcVariableType, AcEquationType> equationSystem = AcEquationSystem.create(network);
// just to test inactive term writing
for (var equationTerm : equationSystem.getEquationTerms(ElementType.BRANCH, network.getBranchById("NHV1_NHV2_1").getNum())) {
equationTerm.setActive(false);
}
String ref = String.join(System.lineSeparator(),
"[ bus_target_p0 = ac_p_closed_1(v0, v1, φ0, φ1) ]",
"[ bus_target_q0 = ac_q_closed_1(v0, v1, φ0, φ1) ]",
"bus_target_v0 = v0",
"bus_target_φ0 = φ0",
"bus_target_p1 = ac_p_closed_2(v0, v1, φ0, φ1) + [ ac_p_closed_1(v1, v2, φ1, φ2) ] + ac_p_closed_1(v1, v2, φ1, φ2)",
"bus_target_q1 = ac_q_closed_2(v0, v1, φ0, φ1) + [ ac_q_closed_1(v1, v2, φ1, φ2) ] + ac_q_closed_1(v1, v2, φ1, φ2)",
"[ bus_target_v1 = v1 ]",
"bus_target_p2 = [ ac_p_closed_2(v1, v2, φ1, φ2) ] + ac_p_closed_2(v1, v2, φ1, φ2) + ac_p_closed_1(v2, v3, φ2, φ3)",
"bus_target_q2 = [ ac_q_closed_2(v1, v2, φ1, φ2) ] + ac_q_closed_2(v1, v2, φ1, φ2) + ac_q_closed_1(v2, v3, φ2, φ3)",
"[ bus_target_v2 = v2 ]",
"bus_target_p3 = ac_p_closed_2(v2, v3, φ2, φ3)",
"bus_target_q3 = ac_q_closed_2(v2, v3, φ2, φ3)",
"[ bus_target_v3 = v3 ]")
+ System.lineSeparator();
assertEquals(ref, equationSystem.writeToString(true));
}

@Test
void writeDcSystemTest() throws IOException {
void writeDcSystemTest() {
List<LfNetwork> lfNetworks = Networks.load(EurostagTutorialExample1Factory.create(), new FirstSlackBusSelector());
LfNetwork network = lfNetworks.get(0);

EquationSystem<DcVariableType, DcEquationType> equationSystem = DcEquationSystem.create(network, new DcEquationSystemCreationParameters(true, false, false, true));
try (StringWriter writer = new StringWriter()) {
equationSystem.write(writer);
writer.flush();
String ref = String.join(System.lineSeparator(),
"bus_target_φ0 = φ0",
"bus_target_p1 = dc_p_2(φ0, φ1) + dc_p_1(φ1, φ2) + dc_p_1(φ1, φ2)",
"bus_target_p2 = dc_p_2(φ1, φ2) + dc_p_2(φ1, φ2) + dc_p_1(φ2, φ3)",
"bus_target_p3 = dc_p_2(φ2, φ3)")
+ System.lineSeparator();
assertEquals(ref, writer.toString());
}
String ref = String.join(System.lineSeparator(),
"bus_target_φ0 = φ0",
"bus_target_p1 = dc_p_2(φ0, φ1) + dc_p_1(φ1, φ2) + dc_p_1(φ1, φ2)",
"bus_target_p2 = dc_p_2(φ1, φ2) + dc_p_2(φ1, φ2) + dc_p_1(φ2, φ3)",
"bus_target_p3 = dc_p_2(φ2, φ3)")
+ System.lineSeparator();
assertEquals(ref, equationSystem.writeToString());
}

@Test
Expand Down