Skip to content
This repository has been archived by the owner on May 16, 2018. It is now read-only.

Commit

Permalink
Add check results to output files
Browse files Browse the repository at this point in the history
  • Loading branch information
massimo-ferraro committed Sep 27, 2017
1 parent 0dc65c8 commit 3a958b6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,11 @@ protected TableFormatter createTableFormatter(String id, Class<? extends TableFo
@Override
public abstract void write(String branchId, double p1, double p1Calc, double q1, double q1Calc, double p2, double p2Calc, double q2, double q2Calc,
double r, double x, double g1, double g2, double b1, double b2, double rho1, double rho2, double alpha1, double alpha2,
double u1, double u2, double theta1, double theta2, double z, double y, double ksi) throws IOException;
double u1, double u2, double theta1, double theta2, double z, double y, double ksi, boolean ok) throws IOException;

@Override
public abstract void write(String generatorId, float p, float q, float v, float targetP, float targetQ, float targetV,
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ) throws IOException;
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ, boolean ok) throws IOException;

@Override
public void close() throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,6 @@ public static boolean checkFlows(String id, double r, double x, double rho1, dou
double p2Calc = rho2 * rho1 * u2 * u1 * y * Math.sin(theta2 - theta1 - ksi + alpha2 - alpha1) + rho2 * rho2 * u2 * u2 * (y * Math.sin(ksi) + g2);
double q2Calc = -rho2 * rho1 * u2 * u1 * y * Math.cos(theta2 - theta1 - ksi + alpha2 - alpha1) + rho2 * rho2 * u2 * u2 * (y * Math.cos(ksi) - b2);

flowsWriter.write(id, p1, p1Calc, q1, q1Calc, p2, p2Calc, q2, q2Calc, r, x, g1, g2, b1, b2, rho1, rho2, alpha1, alpha2, u1, u2, theta1, theta2, z, y, ksi);

if ((Double.isNaN(p1Calc) && !config.areOkMissingValues()) || Math.abs(p1 - p1Calc) > config.getThreshold()) {
LOGGER.warn("Flows validation error: " + id + " P1 " + p1 + " " + p1Calc);
ok = false;
Expand All @@ -95,6 +93,8 @@ public static boolean checkFlows(String id, double r, double x, double rho1, dou
LOGGER.warn("Flows validation error: " + id + " Q2 " + q2 + " " + q2Calc);
ok = false;
}

flowsWriter.write(id, p1, p1Calc, q1, q1Calc, p2, p2Calc, q2, q2Calc, r, x, g1, g2, b1, b2, rho1, rho2, alpha1, alpha2, u1, u2, theta1, theta2, z, y, ksi, ok);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down Expand Up @@ -307,7 +307,7 @@ public static boolean checkGenerators(Generator gen, ValidationConfig config, Va
return checkGenerators(gen.getId(), p, q, v, targetP, targetQ, targetV, voltageRegulatorOn, minQ, maxQ, config, generatorsWriter);
}
try {
generatorsWriter.write(gen.getId(), p, q, Float.NaN, targetP, targetQ, targetV, gen.getTerminal().isConnected(), voltageRegulatorOn, minQ, maxQ);
generatorsWriter.write(gen.getId(), p, q, Float.NaN, targetP, targetQ, targetV, gen.getTerminal().isConnected(), voltageRegulatorOn, minQ, maxQ, true);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand All @@ -331,39 +331,40 @@ public static boolean checkGenerators(String id, float p, float q, float v, floa
boolean voltageRegulatorOn, float minQ, float maxQ, ValidationConfig config, ValidationWriter generatorsWriter) {
boolean ok = true;
try {
generatorsWriter.write(id, p, q, v, targetP, targetQ, targetV, true, voltageRegulatorOn, minQ, maxQ);
// a validation error should be detected if there is both a voltage and a target but no p or q
if (Float.isNaN(p) || Float.isNaN(q)) {
if ((!Float.isNaN(targetP) && targetP != 0)
|| (!Float.isNaN(targetQ) && targetQ != 0)) {
LOGGER.warn("Generators validation error: " + id + ": P=" + p + " targetP=" + targetP + " - Q=" + q + " targetP=" + targetQ);
return false;
ok = false;
} else {
return true;
ok = true;
}
} else {
// active power should be equal to set point
if ((Float.isNaN(targetP) && !config.areOkMissingValues()) || Math.abs(p + targetP) > config.getThreshold()) {
LOGGER.warn("Generators validation error: " + id + ": P=" + p + " targetP=" + targetP);
ok = false;
}
// if voltageRegulatorOn="false" then reactive power should be equal to set point
if (!voltageRegulatorOn && ((Float.isNaN(targetQ) && !config.areOkMissingValues()) || Math.abs(q + targetQ) > config.getThreshold())) {
LOGGER.warn("Generators validation error: " + id + ": voltage regulator off - Q=" + q + " targetQ=" + targetQ);
ok = false;
}
// if voltageRegulatorOn="true" then
// either q is equal to g.getReactiveLimits().getMinQ(p) and V is lower than g.getTargetV()
// or q is equal to g.getReactiveLimits().getMaxQ(p) and V is higher than g.getTargetV()
// or V at the connected bus is equal to g.getTargetV()
if (voltageRegulatorOn
&& (((Float.isNaN(minQ) || Float.isNaN(maxQ) || Float.isNaN(targetV)) && !config.areOkMissingValues())
|| ((Math.abs(q + minQ) > config.getThreshold() || (v - targetV) >= config.getThreshold())
&& (Math.abs(q + maxQ) > config.getThreshold() || (targetV - v) >= config.getThreshold())
&& Math.abs(v - targetV) > config.getThreshold()))) {
LOGGER.warn("Generators validation error: " + id + ": voltage regulator on - Q=" + q + " minQ=" + minQ + " maxQ=" + maxQ + " - V=" + v + " targetV=" + targetV);
ok = false;
}
}
// active power should be equal to set point
if ((Float.isNaN(targetP) && !config.areOkMissingValues()) || Math.abs(p + targetP) > config.getThreshold()) {
LOGGER.warn("Generators validation error: " + id + ": P=" + p + " targetP=" + targetP);
ok = false;
}
// if voltageRegulatorOn="false" then reactive power should be equal to set point
if (!voltageRegulatorOn && ((Float.isNaN(targetQ) && !config.areOkMissingValues()) || Math.abs(q + targetQ) > config.getThreshold())) {
LOGGER.warn("Generators validation error: " + id + ": voltage regulator off - Q=" + q + " targetQ=" + targetQ);
ok = false;
}
// if voltageRegulatorOn="true" then
// either q is equal to g.getReactiveLimits().getMinQ(p) and V is lower than g.getTargetV()
// or q is equal to g.getReactiveLimits().getMaxQ(p) and V is higher than g.getTargetV()
// or V at the connected bus is equal to g.getTargetV()
if (voltageRegulatorOn
&& (((Float.isNaN(minQ) || Float.isNaN(maxQ) || Float.isNaN(targetV)) && !config.areOkMissingValues())
|| ((Math.abs(q + minQ) > config.getThreshold() || (v - targetV) >= config.getThreshold())
&& (Math.abs(q + maxQ) > config.getThreshold() || (targetV - v) >= config.getThreshold())
&& Math.abs(v - targetV) > config.getThreshold()))) {
LOGGER.warn("Generators validation error: " + id + ": voltage regulator on - Q=" + q + " minQ=" + minQ + " maxQ=" + maxQ + " - V=" + v + " targetV=" + targetV);
ok = false;
}
generatorsWriter.write(id, p, q, v, targetP, targetQ, targetV, true, voltageRegulatorOn, minQ, maxQ, ok);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ protected Column[] getColumns() {
@Override
public void write(String branchId, double p1, double p1Calc, double q1, double q1Calc, double p2, double p2Calc, double q2, double q2Calc,
double r, double x, double g1, double g2, double b1, double b2, double rho1, double rho2, double alpha1, double alpha2,
double u1, double u2, double theta1, double theta2, double z, double y, double ksi) throws IOException {
double u1, double u2, double theta1, double theta2, double z, double y, double ksi, boolean ok) throws IOException {
Objects.requireNonNull(branchId);
formatter.writeCell(branchId).writeCell("network_p1").writeCell(p1)
.writeCell(branchId).writeCell("expected_p1").writeCell(p1Calc)
Expand All @@ -76,13 +76,14 @@ public void write(String branchId, double p1, double p1Calc, double q1, double q
.writeCell(branchId).writeCell("theta2").writeCell(theta2)
.writeCell(branchId).writeCell("z").writeCell(z)
.writeCell(branchId).writeCell("y").writeCell(y)
.writeCell(branchId).writeCell("ksi").writeCell(ksi);
.writeCell(branchId).writeCell("ksi").writeCell(ksi)
.writeCell(branchId).writeCell("validation").writeCell(ok ? "success" : "fail");
}
}

@Override
public void write(String generatorId, float p, float q, float v, float targetP, float targetQ, float targetV,
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ) throws IOException {
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ, boolean ok) throws IOException {
Objects.requireNonNull(generatorId);
formatter.writeCell(generatorId).writeCell("p").writeCell(-p)
.writeCell(generatorId).writeCell("q").writeCell(-q)
Expand All @@ -94,7 +95,8 @@ public void write(String generatorId, float p, float q, float v, float targetP,
formatter.writeCell(generatorId).writeCell("connected").writeCell(connected)
.writeCell(generatorId).writeCell("voltageRegulatorOn").writeCell(voltageRegulatorOn)
.writeCell(generatorId).writeCell("minQ").writeCell(minQ)
.writeCell(generatorId).writeCell("maxQ").writeCell(maxQ);
.writeCell(generatorId).writeCell("maxQ").writeCell(maxQ)
.writeCell(generatorId).writeCell("validation").writeCell(ok ? "success" : "fail");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ protected Column[] getColumns() {
new Column("theta2"),
new Column("z"),
new Column("y"),
new Column("ksi")
new Column("ksi"),
new Column("validation")
};
}
return new Column[] {
Expand All @@ -96,7 +97,8 @@ protected Column[] getColumns() {
new Column("connected"),
new Column("voltageRegulatorOn"),
new Column("minQ"),
new Column("maxQ")
new Column("maxQ"),
new Column("validation")
};
}
return new Column[] {
Expand All @@ -116,7 +118,7 @@ protected Column[] getColumns() {
@Override
public void write(String branchId, double p1, double p1Calc, double q1, double q1Calc, double p2, double p2Calc, double q2, double q2Calc,
double r, double x, double g1, double g2, double b1, double b2, double rho1, double rho2, double alpha1, double alpha2,
double u1, double u2, double theta1, double theta2, double z, double y, double ksi) throws IOException {
double u1, double u2, double theta1, double theta2, double z, double y, double ksi, boolean ok) throws IOException {
Objects.requireNonNull(branchId);
formatter.writeCell(branchId)
.writeCell(p1)
Expand Down Expand Up @@ -144,13 +146,14 @@ public void write(String branchId, double p1, double p1Calc, double q1, double q
.writeCell(theta2)
.writeCell(z)
.writeCell(y)
.writeCell(ksi);
.writeCell(ksi)
.writeCell(ok ? "success" : "fail");
}
}

@Override
public void write(String generatorId, float p, float q, float v, float targetP, float targetQ, float targetV,
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ) throws IOException {
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ, boolean ok) throws IOException {
Objects.requireNonNull(generatorId);
formatter.writeCell(generatorId)
.writeCell(-p)
Expand All @@ -163,7 +166,8 @@ public void write(String generatorId, float p, float q, float v, float targetP,
formatter.writeCell(connected)
.writeCell(voltageRegulatorOn)
.writeCell(minQ)
.writeCell(maxQ);
.writeCell(maxQ)
.writeCell(ok ? "success" : "fail");
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ public interface ValidationWriter extends AutoCloseable {

void write(String branchId, double p1, double p1Calc, double q1, double q1Calc, double p2, double p2Calc, double q2, double q2Calc,
double r, double x, double g1, double g2, double b1, double b2, double rho1, double rho2, double alpha1, double alpha2,
double u1, double u2, double theta1, double theta2, double z, double y, double ksi) throws IOException;
double u1, double u2, double theta1, double theta2, double z, double y, double ksi, boolean ok) throws IOException;

void write(String generatorId, float p, float q, float v, float targetP, float targetQ, float targetV,
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ) throws IOException;
boolean connected, boolean voltageRegulatorOn, float minQ, float maxQ, boolean ok) throws IOException;

@Override
void close() throws IOException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@ public void testFlowsVerbose() throws Exception {
String.join(";", branchId, "theta2", String.format(Locale.getDefault(), "%g", theta2)),
String.join(";", branchId, "z", String.format(Locale.getDefault(), "%g", z)),
String.join(";", branchId, "y", String.format(Locale.getDefault(), "%g", y)),
String.join(";", branchId, "ksi", String.format(Locale.getDefault(), "%g", ksi)));
String.join(";", branchId, "ksi", String.format(Locale.getDefault(), "%g", ksi)),
String.join(";", branchId, "validation", "success"));
testFlows(flowsContent, true);
}

Expand All @@ -117,7 +118,7 @@ private void testFlows(String flowsContent, boolean verbose) throws IOException
TableFormatterConfig config = new TableFormatterConfig(Locale.getDefault(), ';', "inv", true, true);
try (ValidationWriter flowsWriter = new ValidationFormatterCsvMultilineWriter("test", CsvTableFormatterFactory.class, config, writer, verbose, ValidationType.FLOWS)) {
flowsWriter.write(branchId, p1, p1Calc, q1, q1Calc, p2, p2Calc, q2, q2Calc, r, x, g1, g2, b1, b2, rho1, rho2,
alpha1, alpha2, u1, u2, theta1, theta2, z, y, ksi);
alpha1, alpha2, u1, u2, theta1, theta2, z, y, ksi, true);
assertEquals(flowsContent, writer.toString().trim());
}
}
Expand Down Expand Up @@ -150,15 +151,16 @@ public void testGeneratorsVerbose() throws Exception {
String.join(";", generatorId, "connected", Boolean.toString(connected)),
String.join(";", generatorId, "voltageRegulatorOn", Boolean.toString(voltageRegulatorOn)),
String.join(";", generatorId, "minQ", String.format(Locale.getDefault(), "%g", minQ)),
String.join(";", generatorId, "maxQ", String.format(Locale.getDefault(), "%g", maxQ)));
String.join(";", generatorId, "maxQ", String.format(Locale.getDefault(), "%g", maxQ)),
String.join(";", generatorId, "validation", "success"));
testGenerators(generatorsContent, true);
}

private void testGenerators(String generatorsContent, boolean verbose) throws IOException {
Writer writer = new StringWriter();
TableFormatterConfig config = new TableFormatterConfig(Locale.getDefault(), ';', "inv", true, true);
try (ValidationWriter generatorsWriter = new ValidationFormatterCsvMultilineWriter("test", CsvTableFormatterFactory.class, config, writer, verbose, ValidationType.GENERATORS)) {
generatorsWriter.write(generatorId, p, q, v, targetP, targetQ, targetV, connected, voltageRegulatorOn, minQ, maxQ);
generatorsWriter.write(generatorId, p, q, v, targetP, targetQ, targetV, connected, voltageRegulatorOn, minQ, maxQ, true);
assertEquals(generatorsContent, writer.toString().trim());
}
}
Expand Down

0 comments on commit 3a958b6

Please sign in to comment.