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

Dynaflow refactor #130

Merged
merged 17 commits into from
Nov 25, 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
package com.powsybl.dynaflow;

import com.google.common.collect.ImmutableMap;
import com.powsybl.commons.config.ModuleConfig;
import com.powsybl.commons.config.PlatformConfig;

import java.nio.file.Path;
Expand Down Expand Up @@ -36,10 +35,9 @@ public static DynaFlowConfig fromPropertyFile() {

public static DynaFlowConfig fromPlatformConfig(PlatformConfig platformConfig) {
Objects.requireNonNull(platformConfig);
ModuleConfig config = platformConfig.getModuleConfig("dynaflow");
Path homeDir = config.getPathProperty("homeDir");
boolean debug = config.getBooleanProperty("debug", DEFAULT_DEBUG);
return new DynaFlowConfig(homeDir, debug);
return platformConfig.getOptionalModuleConfig("dynaflow")
.map(config -> new DynaFlowConfig(config.getPathProperty("homeDir"), config.getBooleanProperty("debug", DEFAULT_DEBUG)))
.orElse(null);
}

public Map<String, String> createEnv() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,65 @@
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;

import com.powsybl.iidm.xml.IidmXmlVersion;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* @author Guillaume Pernin <guillaume.pernin at rte-france.com>
*/
public final class DynaFlowConstants {

public static final String DYNAFLOW_NAME = "DynaFlow";

public static final String CONFIG_FILENAME = "config.json";

public static final String IIDM_FILENAME = "network.xiidm";

public static final String VERSION = "0.1";
public static final DynaFlowVersion VERSION_MIN = DynaFlowVersion.V_1_3_0;

public static final DynaFlowVersion VERSION = DynaFlowVersion.V_1_3_1;

public static final String OUTPUT_IIDM_FILENAME = "outputIIDM.xml";

public static final String OUTPUT_RESULTS_FILENAME = "results.json";

public static final String IIDM_VERSION = IidmXmlVersion.V_1_4.toString(".");

public enum DynaFlowVersion {
V_1_3_0(List.of(1, 3, 0)),
V_1_3_1(List.of(1, 3, 1));

private final List<Integer> versionArray;
private static final String DEFAULT_DELIMITER = ".";

DynaFlowVersion(List<Integer> versionArray) {
this.versionArray = versionArray;
}

public String toString(String separator) {
return versionArray.stream().map(Object::toString).collect(Collectors.joining(separator));
}

@Override
public String toString() {
return this.toString(DEFAULT_DELIMITER);
}

public static Optional<DynaFlowVersion> of(String version) {
return of(version, DEFAULT_DELIMITER);
}

public static Optional<DynaFlowVersion> of(String version, String separator) {
return Stream.of(DynaFlowVersion.values())
.filter(v -> version.equals(v.toString(separator)))
.findFirst(); // there can only be 0 or exactly 1 match
}
}

public enum OutputTypes {
STEADYSTATE,
LOSTEQ,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.google.auto.service.AutoService;
import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.config.PlatformConfig;
import com.powsybl.commons.extensions.Extension;
import com.powsybl.commons.extensions.ExtensionJsonSerializer;
Expand All @@ -17,7 +18,6 @@
import com.powsybl.dynaflow.json.JsonDynaFlowParametersSerializer;
import com.powsybl.dynawo.commons.DynawoResultsNetworkUpdate;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.xml.IidmXmlVersion;
import com.powsybl.iidm.xml.NetworkXml;
import com.powsybl.iidm.xml.XMLExporter;
import com.powsybl.loadflow.LoadFlowParameters;
Expand Down Expand Up @@ -57,7 +57,7 @@ public DynaFlowProvider(Supplier<DynaFlowConfig> configSupplier) {

private static void writeIIDM(Path workingDir, Network network) {
Properties params = new Properties();
params.setProperty(XMLExporter.VERSION, IidmXmlVersion.V_1_2.toString("."));
params.setProperty(XMLExporter.VERSION, IIDM_VERSION);
network.write("XIIDM", params, workingDir.resolve(IIDM_FILENAME));
}

Expand Down Expand Up @@ -94,12 +94,12 @@ private static DynaFlowParameters getParametersExt(LoadFlowParameters parameters

@Override
public String getName() {
return "DynaFlow";
return DYNAFLOW_NAME;
}

@Override
public String getVersion() {
return "0.1";
return VERSION.toString();
}

private static CommandExecution createCommandExecution(DynaFlowConfig config) {
Expand All @@ -117,7 +117,9 @@ public CompletableFuture<LoadFlowResult> run(Network network, ComputationManager
DynaFlowConfig config = Objects.requireNonNull(configSupplier.get());
ExecutionEnvironment env = new ExecutionEnvironment(config.createEnv(), WORKING_DIR_PREFIX, config.isDebug());
Command versionCmd = getVersionCommand(config);
DynaFlowUtil.checkDynaFlowVersion(env, computationManager, versionCmd);
if (!DynaFlowUtil.checkDynaFlowVersion(env, computationManager, versionCmd)) {
throw new PowsyblException("DynaFlow version not supported. Must be " + VERSION_MIN + " <= version <= " + VERSION);
}
return computationManager.execute(env, new AbstractExecutionHandler<LoadFlowResult>() {

@Override
Expand Down Expand Up @@ -147,7 +149,6 @@ public LoadFlowResult after(Path workingDir, ExecutionReport report) throws IOEx
Path resultsPath = workingDir.resolve(OUTPUT_RESULTS_FILENAME);
if (!Files.exists(resultsPath)) {
Map<String, String> metrics = new HashMap<>();
String logs = null;
List<LoadFlowResult.ComponentResult> componentResults = new ArrayList<>(1);
componentResults.add(new LoadFlowResultImpl.ComponentResultImpl(0,
0,
Expand All @@ -156,7 +157,7 @@ public LoadFlowResult after(Path workingDir, ExecutionReport report) throws IOEx
"not-found",
0.,
Double.NaN));
return new LoadFlowResultImpl(status, metrics, logs, componentResults);
return new LoadFlowResultImpl(status, metrics, null, componentResults);
}
return LoadFlowResultDeserializer.read(resultsPath);
}
Expand Down
18 changes: 15 additions & 3 deletions dynaflow/src/main/java/com/powsybl/dynaflow/DynaFlowUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import com.powsybl.dynaflow.DynaFlowConstants.DynaFlowVersion;

/**
*
Expand All @@ -41,14 +42,25 @@ public Boolean after(Path workingDir, ExecutionReport report) throws IOException
}
try (Reader reader = new InputStreamReader(stdErr.get())) {
String stdErrContent = CharStreams.toString(reader);
return stdErrContent.equals(DynaFlowConstants.VERSION);
return DynaFlowUtil.versionIsInRange(stdErrContent, DynaFlowConstants.VERSION_MIN, DynaFlowConstants.VERSION);
}

}
}).join();
}

private DynaFlowUtil() {
public static boolean versionRespectsMin(String version, DynaFlowVersion minDynaFlowVersion) {
return DynaFlowVersion.of(version).map(v -> v.compareTo(minDynaFlowVersion) >= 0).orElse(false);
}

public static boolean versionRespectsMax(String version, DynaFlowVersion maxDynaFlowVersion) {
return DynaFlowVersion.of(version).map(v -> v.compareTo(maxDynaFlowVersion) <= 0).orElse(false);
}

public static boolean versionIsInRange(String version, DynaFlowVersion minDynaFlowVersion, DynaFlowVersion maxDynaFlowVersion) {
return versionRespectsMin(version, minDynaFlowVersion)
&& versionRespectsMax(version, maxDynaFlowVersion);
}

private DynaFlowUtil() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.nio.file.Path;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

/**
*
Expand Down Expand Up @@ -53,6 +54,14 @@ public void fromPlatformConfigTest() {
assertEquals(debug, config.isDebug());
}

@Test
public void fromPlatformConfigNull() {
InMemoryPlatformConfig platformConfig = new InMemoryPlatformConfig(fileSystem);

DynaFlowConfig config = DynaFlowConfig.fromPlatformConfig(platformConfig);
assertNull(config);
}

@Test
public void checkGetters() {
Path pathHomeDir = fileSystem.getPath(homeDir);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.dynaflow;

import com.powsybl.commons.AbstractConverterTest;
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.datasource.ResourceDataSource;
import com.powsybl.commons.datasource.ResourceSet;
import com.powsybl.computation.ComputationManager;
Expand Down Expand Up @@ -128,8 +129,8 @@ public void test() throws Exception {
LoadFlow.Runner dynaFlowSimulation = LoadFlow.find();
LoadFlowParameters params = LoadFlowParameters.load();

assertEquals("DynaFlow", dynaFlowSimulation.getName());
assertEquals("0.1", dynaFlowSimulation.getVersion());
assertEquals(DYNAFLOW_NAME, dynaFlowSimulation.getName());
assertEquals(VERSION, DynaFlowVersion.of(dynaFlowSimulation.getVersion()).get());

LocalCommandExecutor commandExecutor = new LocalCommandExecutorMock("/dynaflow_version.out",
"/SmallBusBranch_outputIIDM.xml", "/results.json");
Expand All @@ -145,8 +146,8 @@ public void testFail() throws Exception {
LoadFlow.Runner dynaFlowSimulation = LoadFlow.find();
LoadFlowParameters params = LoadFlowParameters.load();

assertEquals("DynaFlow", dynaFlowSimulation.getName());
assertEquals("0.1", dynaFlowSimulation.getVersion());
assertEquals(DYNAFLOW_NAME, dynaFlowSimulation.getName());
assertEquals(VERSION, DynaFlowVersion.of(dynaFlowSimulation.getVersion()).get());

LocalCommandExecutor commandExecutor = new EmptyLocalCommandExecutorMock("/dynaflow_version.out");
ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(fileSystem.getPath("/working-dir"), 1), commandExecutor, ForkJoinPool.commonPool());
Expand All @@ -155,14 +156,25 @@ public void testFail() throws Exception {
assertFalse(result.isOk());
}

@Test(expected = PowsyblException.class)
public void testCallingBadVersionDynaFlow() throws Exception {
Network network = createTestSmallBusBranch();
LoadFlow.Runner dynaFlowSimulation = LoadFlow.find();
LoadFlowParameters params = LoadFlowParameters.load();

LocalCommandExecutor commandExecutor = new EmptyLocalCommandExecutorMock("/dynaflow_bad_version.out");
ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(fileSystem.getPath("/working-dir"), 1), commandExecutor, ForkJoinPool.commonPool());
LoadFlowResult result = dynaFlowSimulation.run(network, computationManager, params);
}

@Test
public void testUpdate() throws Exception {
Network network = createTestSmallBusBranch();
LoadFlow.Runner dynaFlowSimulation = LoadFlow.find();
LoadFlowParameters params = LoadFlowParameters.load();

assertEquals("DynaFlow", dynaFlowSimulation.getName());
assertEquals("0.1", dynaFlowSimulation.getVersion());
assertEquals(DYNAFLOW_NAME, dynaFlowSimulation.getName());
assertEquals(VERSION, DynaFlowVersion.of(dynaFlowSimulation.getVersion()).get());

LocalCommandExecutor commandExecutor = new LocalCommandExecutorMock("/dynaflow_version.out",
"/SmallBusBranch_outputIIDM.xml", "/results.json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,69 @@ public void versionTestNotExistingFile() throws IOException {
ComputationManager computationManager = new LocalComputationManager(new LocalComputationConfig(fileSystem.getPath("/working-dir"), 1), commandExecutor, ForkJoinPool.commonPool());
DynaFlowUtil.checkDynaFlowVersion(env, computationManager, badVersionCmd);
}

@Test
public void testVersionRespectsMin() {
assertTrue(DynaFlowUtil.versionRespectsMin("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionAboveMin() {
assertTrue(DynaFlowUtil.versionRespectsMin("1.3.1", DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionDoesNotRespectMin() {
assertFalse(DynaFlowUtil.versionRespectsMin("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_1));
}

@Test
public void testVersionNotConvertibleMin() {
assertFalse(DynaFlowUtil.versionRespectsMin("1.1", DynaFlowConstants.DynaFlowVersion.V_1_3_1));
}

@Test
public void testVersionRespectsMax() {
assertTrue(DynaFlowUtil.versionRespectsMax("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionAboveMax() {
assertFalse(DynaFlowUtil.versionRespectsMax("1.3.1", DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionBelowMax() {
assertTrue(DynaFlowUtil.versionRespectsMax("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_1));
}

@Test
public void testVersionNotConvertibleMax() {
assertFalse(DynaFlowUtil.versionRespectsMin("1.1", DynaFlowConstants.DynaFlowVersion.V_1_3_1));
}

@Test
public void testVersionIsInRange() {
assertTrue(DynaFlowUtil.versionIsInRange("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_0, DynaFlowConstants.DynaFlowVersion.V_1_3_1));
}

@Test
public void testVersionIsInRange2() {
assertTrue(DynaFlowUtil.versionIsInRange("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_0, DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionIsNotInRange() {
assertFalse(DynaFlowUtil.versionIsInRange("1.3.0", DynaFlowConstants.DynaFlowVersion.V_1_3_1, DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionIsNotInRange2() {
assertFalse(DynaFlowUtil.versionIsInRange("1.3.1", DynaFlowConstants.DynaFlowVersion.V_1_3_0, DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}

@Test
public void testVersionNotKnownInRange() {
assertFalse(DynaFlowUtil.versionIsInRange("1.1", DynaFlowConstants.DynaFlowVersion.V_1_3_0, DynaFlowConstants.DynaFlowVersion.V_1_3_0));
}
}
2 changes: 1 addition & 1 deletion dynaflow/src/test/resources/dynaflow_version.out
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.1
1.3.1