Skip to content

Commit

Permalink
Load default loadflow parameters from json file in classpath (#3040)
Browse files Browse the repository at this point in the history
* Add default loadflow parameters loader from json in resources
* Add abstract class
* Fix dummy extension class

Signed-off-by: Hugo KULESZA <hugo.kulesza@rte-france.com>
  • Loading branch information
HugoKulesza authored May 31, 2024
1 parent cae1343 commit 15a6deb
Show file tree
Hide file tree
Showing 6 changed files with 142 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* 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.loadflow;

import java.io.InputStream;
import java.util.Objects;

/**
* @author Hugo Kulesza {@literal <hugo.kulesza at rte-france.com>}
*/
public abstract class AbstractLoadFlowDefaultParametersLoader implements LoadFlowDefaultParametersLoader {

private final String name;
private final String jsonParametersFile;

AbstractLoadFlowDefaultParametersLoader(String name, String jsonParametersFile) {
this.name = Objects.requireNonNull(name);
this.jsonParametersFile = Objects.requireNonNull(jsonParametersFile);
}

@Override
public String getSourceName() {
return name;
}

public InputStream loadDefaultParametersFromFile() {
return getClass().getResourceAsStream(jsonParametersFile);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* 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.loadflow;

import java.io.InputStream;

/**
* @author Hugo Kulesza {@literal <hugo.kulesza at rte-france.com>}
*/
public interface LoadFlowDefaultParametersLoader {

String getSourceName();

InputStream loadDefaultParametersFromFile();
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
import com.powsybl.commons.util.ServiceLoaderCache;
import com.powsybl.iidm.network.Country;
import com.powsybl.loadflow.json.JsonLoadFlowParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.ByteArrayInputStream;
import java.io.IOException;
Expand Down Expand Up @@ -69,6 +71,8 @@ public enum ConnectedComponentMode {
ALL,
}

public static final Logger LOGGER = LoggerFactory.getLogger(LoadFlowParameters.class);

// VERSION = 1.0 specificCompatibility
// VERSION = 1.1 t2wtSplitShuntAdmittance
// VERSION = 1.2 twtSplitShuntAdmittance,
Expand Down Expand Up @@ -182,6 +186,25 @@ protected static void load(LoadFlowParameters parameters, PlatformConfig platfor
private double dcPowerFactor = DEFAULT_DC_POWER_FACTOR;

public LoadFlowParameters() {
this(ServiceLoader.load(LoadFlowDefaultParametersLoader.class)
.stream()
.map(ServiceLoader.Provider::get)
.toList());
}

public LoadFlowParameters(List<LoadFlowDefaultParametersLoader> defaultParametersLoaders) {
int numberOfLoadersFound = Objects.requireNonNull(defaultParametersLoaders).size();
if (numberOfLoadersFound > 1) {
List<String> names = defaultParametersLoaders.stream()
.map(LoadFlowDefaultParametersLoader::getSourceName)
.toList();
LOGGER.warn("Multiple default loadflow parameters classes have been found in the class path : {}. No default parameters file loaded",
names);
} else if (numberOfLoadersFound == 1) {
LoadFlowDefaultParametersLoader loader = defaultParametersLoaders.get(0);
JsonLoadFlowParameters.update(this, loader.loadDefaultParametersFromFile());
LOGGER.debug("Default loadflow configuration has been updated using the reference file from parameters loader '{}'", loader.getSourceName());
}
}

protected LoadFlowParameters(LoadFlowParameters other) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* 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.loadflow;

/**
* @author Hugo Kulesza {@literal <hugo.kulesza at rte-france.com>}
*/
public class LoadFlowDefaultParametersLoaderMock extends AbstractLoadFlowDefaultParametersLoader {

private static final String RESOURCE_FILE = "/LoadFlowParametersUpdate.json";

LoadFlowDefaultParametersLoaderMock(String name) {
super(name, RESOURCE_FILE);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* Copyright (c) 2024, RTE (http://www.rte-france.com)
* 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.loadflow;

import com.powsybl.commons.extensions.Extension;
import com.powsybl.loadflow.json.JsonLoadFlowParametersTest;
import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

/**
* @author Hugo Kulesza {@literal <hugo.kulesza at rte-france.com>}
*/
class LoadFlowDefaultParametersLoaderTest {

@Test
void testLoadParametersFromClassPath() {
LoadFlowDefaultParametersLoaderMock loader = new LoadFlowDefaultParametersLoaderMock("test");

LoadFlowParameters parameters = new LoadFlowParameters(List.of(loader));

List<Extension<LoadFlowParameters>> extensions = parameters.getExtensions().stream().toList();
assertEquals(1, extensions.size());
JsonLoadFlowParametersTest.DummyExtension dummyExtension = (JsonLoadFlowParametersTest.DummyExtension) extensions.get(0);
assertEquals(5, dummyExtension.getParameterDouble());
}

@Test
void testConflictBetweenDefaultParametersLoader() {
LoadFlowDefaultParametersLoaderMock loader1 = new LoadFlowDefaultParametersLoaderMock("test1");
LoadFlowDefaultParametersLoaderMock loader2 = new LoadFlowDefaultParametersLoaderMock("test2");

LoadFlowParameters parameters = new LoadFlowParameters(List.of(loader1, loader2));
List<Extension<LoadFlowParameters>> extensions = parameters.getExtensions().stream().toList();
assertEquals(0, extensions.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ private static ObjectMapper createMapper() {

@Override
public DummyExtension deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException {
return new DummyExtension();
return createMapper().readValue(jsonParser, DummyExtension.class);
}

@Override
Expand Down

0 comments on commit 15a6deb

Please sign in to comment.