Skip to content

Commit

Permalink
remove duplication
Browse files Browse the repository at this point in the history
  • Loading branch information
WalAmeni committed Mar 11, 2024
1 parent 22586e4 commit 09d40a9
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,36 @@

import com.farao_community.farao.cse.export_runner.app.FileUtil;
import com.farao_community.farao.cse.export_runner.app.configurations.ProcessConfiguration;
import com.farao_community.farao.cse.network_processing.CracCreationParametersService;
import com.farao_community.farao.cse.network_processing.busbar_change.BusBarChangePostProcessor;
import com.farao_community.farao.cse.network_processing.busbar_change.BusBarChangePreProcessor;
import com.farao_community.farao.cse.network_processing.ucte_pst_change.PstInitializer;
import com.farao_community.farao.cse.runner.api.exception.CseInternalException;
import com.farao_community.farao.cse.runner.api.resource.CseExportRequest;
import com.farao_community.farao.cse.runner.api.resource.CseExportResponse;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.openrao.data.craccreation.creator.api.CracCreators;
import com.farao_community.farao.minio_adapter.starter.GridcapaFileGroup;
import com.farao_community.farao.rao_runner.api.resource.RaoResponse;
import com.powsybl.iidm.network.Network;
import com.powsybl.loadflow.LoadFlow;
import com.powsybl.loadflow.LoadFlowParameters;
import com.powsybl.loadflow.LoadFlowResult;
import com.powsybl.openrao.data.craccreation.creator.api.CracCreators;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.JsonCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.CseCrac;
import com.powsybl.openrao.data.craccreation.creator.cse.CseCracCreationContext;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import com.powsybl.openrao.data.raoresultapi.RaoResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.InputStream;
import java.util.Map;
import java.time.OffsetDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Map;
import java.util.Set;

/**
Expand All @@ -48,7 +48,7 @@ public class CseExportRunner {
private static final String NETWORK_PRE_PROCESSED_FILE_NAME = "network_pre_processed";
private static final DateTimeFormatter OUTPUTS_DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyyMMdd_HHmm");
private static final Logger LOGGER = LoggerFactory.getLogger(CseExportRunner.class);
private static final String CRAC_CREATION_PARAMETERS_JSON = "/crac/cseCracCreationParameters.json";
private static final String CRAC_CREATION_PARAMETERS_JSON = "cseCracCreationParameters.json";
private final FileImporter fileImporter;
private final FileExporter fileExporter;
private final PiSaService pisaService;
Expand Down Expand Up @@ -83,7 +83,9 @@ public CseExportResponse run(CseExportRequest cseExportRequest) {
// Create CRAC creation context
CseCrac nativeCseCrac = fileImporter.importCseCrac(cseExportRequest.getMergedCracUrl());
Set<BusBarChangeSwitches> busBarChangeSwitchesSet = BusBarChangePreProcessor.process(network, nativeCseCrac); // Pre-treatment on network
CracCreationParameters cracCreationParameters = getCracCreationParameters(busBarChangeSwitchesSet);
LOGGER.info("Importing Crac Creation Parameters file: {}", CRAC_CREATION_PARAMETERS_JSON);
InputStream cracCreationParametersInputStream = getClass().getResourceAsStream(CRAC_CREATION_PARAMETERS_JSON);
CracCreationParameters cracCreationParameters = CracCreationParametersService.getCracCreationParameters(cracCreationParametersInputStream, busBarChangeSwitchesSet);
CseCracCreationContext cseCracCreationContext = (CseCracCreationContext) CracCreators.createCrac(nativeCseCrac,
network, cseExportRequest.getTargetProcessDateTime(), cracCreationParameters);

Expand Down Expand Up @@ -114,20 +116,6 @@ public CseExportResponse run(CseExportRequest cseExportRequest) {
}
}

CracCreationParameters getCracCreationParameters(Set<BusBarChangeSwitches> busBarChangeSwitches) {
LOGGER.info("Importing Crac Creation Parameters file: {}", CRAC_CREATION_PARAMETERS_JSON);
CracCreationParameters cracCreationParameters = JsonCracCreationParameters.read(getClass().getResourceAsStream(CRAC_CREATION_PARAMETERS_JSON));
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
if (cseCracCreationParameters != null) {
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
} else {
cseCracCreationParameters = new CseCracCreationParameters();
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
cracCreationParameters.addExtension(CseCracCreationParameters.class, cseCracCreationParameters);
}
return cracCreationParameters;
}

private String saveInitialNetwork(CseExportRequest cseExportRequest, Network initialNetwork) {
return fileExporter.saveNetwork(initialNetwork, "XIIDM", GridcapaFileGroup.ARTIFACT,
cseExportRequest.getProcessType(), NETWORK_PRE_PROCESSED_FILE_NAME, cseExportRequest.getTargetProcessDateTime());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"crac-factory": "CracImplFactory",
"ra-usage-limits-per-instant": [
{
"instant": "curative",
"max-ra-per-tso": {
"IT": 6,
"FR": 5,
"CH": 1,
"AT": 3,
"SI": 3
}
}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,13 @@
package com.farao_community.farao.cse.export_runner.app.services;

import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.openrao.data.cracapi.RaUsageLimits;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.SwitchPairId;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.time.OffsetDateTime;
import java.util.Set;

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

/**
* @author Amira Kahya {@literal <amira.kahya at rte-france.com>}
Expand All @@ -40,20 +33,4 @@ void getFinalNetworkFilenameTest() {
assertEquals(expectedFilenameWithoutExtension, actualFilenameWithoutExtension);

}

@Test
void getCracCreationParametersTest() {
Set<BusBarChangeSwitches> busBarChangesSwitches = Set.of(new BusBarChangeSwitches("ra-id", Set.of(new SwitchPairId("switch-to-open", "switch-to-close"))));
CracCreationParameters cracCreationParameters = cseExportRunner.getCracCreationParameters(busBarChangesSwitches);
RaUsageLimits raUsageLimits = cracCreationParameters.getRaUsageLimitsPerInstant().get("curative");
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertEquals(6, raUsageLimits.getMaxRaPerTso().get("IT"));
assertEquals(5, raUsageLimits.getMaxRaPerTso().get("FR"));
assertEquals(1, raUsageLimits.getMaxRaPerTso().get("CH"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("AT"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("SI"));
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
assertEquals(1, cseCracCreationParameters.getBusBarChangeSwitches("ra-id").getSwitchPairs().size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.farao_community.farao.cse.import_runner.app.dichotomy.NetworkShifterUtil;
import com.farao_community.farao.cse.import_runner.app.util.FileUtil;
import com.farao_community.farao.cse.import_runner.app.util.Threadable;
import com.farao_community.farao.cse.network_processing.CracCreationParametersService;
import com.farao_community.farao.cse.network_processing.busbar_change.BusBarChangePostProcessor;
import com.farao_community.farao.cse.network_processing.busbar_change.BusBarChangePreProcessor;
import com.farao_community.farao.cse.network_processing.ucte_pst_change.PstInitializer;
Expand All @@ -29,16 +30,15 @@
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.craccreation.creator.api.CracCreators;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.JsonCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.CseCrac;
import com.powsybl.openrao.data.craccreation.creator.cse.CseCracCreationContext;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -149,26 +149,14 @@ CracImportData importCracAndModifyNetworkForBusBars(String cracUrl, OffsetDateTi
CseCrac nativeCseCrac = fileImporter.importCseCrac(cracUrl);
// Pre-treatment on network
Set<BusBarChangeSwitches> busBarChangeSwitchesSet = BusBarChangePreProcessor.process(network, nativeCseCrac);
CracCreationParameters cracCreationParameters = getCracCreationParameters(busBarChangeSwitchesSet);
LOGGER.info("Importing Crac Creation Parameters file: {}", CRAC_CREATION_PARAMETERS_JSON);
InputStream cracCreationParametersIs = getClass().getResourceAsStream(CRAC_CREATION_PARAMETERS_JSON);
CracCreationParameters cracCreationParameters = CracCreationParametersService.getCracCreationParameters(cracCreationParametersIs, busBarChangeSwitchesSet);
CseCracCreationContext cracCreationContext = (CseCracCreationContext) CracCreators.createCrac(
nativeCseCrac, network, targetProcessDateTime, cracCreationParameters);
return new CracImportData(cracCreationContext, busBarChangeSwitchesSet);
}

CracCreationParameters getCracCreationParameters(Set<BusBarChangeSwitches> busBarChangeSwitches) {
LOGGER.info("Importing Crac Creation Parameters file: {}", CRAC_CREATION_PARAMETERS_JSON);
CracCreationParameters cracCreationParameters = JsonCracCreationParameters.read(getClass().getResourceAsStream(CRAC_CREATION_PARAMETERS_JSON));
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
if (cseCracCreationParameters != null) {
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
} else {
cseCracCreationParameters = new CseCracCreationParameters();
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
cracCreationParameters.addExtension(CseCracCreationParameters.class, cseCracCreationParameters);
}
return cracCreationParameters;
}

static final class CracImportData {
final CseCracCreationContext cseCracCreationContext;
final Set<BusBarChangeSwitches> busBarChangeSwitchesSet;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"crac-factory": "CracImplFactory",
"ra-usage-limits-per-instant": [
{
"instant": "curative",
"max-ra-per-tso": {
"IT": 6,
"FR": 5,
"CH": 1,
"AT": 3,
"SI": 3
}
}]
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,10 @@
import com.farao_community.farao.cse.runner.api.resource.CseRequest;
import com.farao_community.farao.cse.runner.api.resource.CseResponse;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.powsybl.openrao.data.cracapi.Crac;
import com.farao_community.farao.dichotomy.api.results.DichotomyResult;
import com.farao_community.farao.minio_adapter.starter.GridcapaFileGroup;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.cracapi.RaUsageLimits;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.SwitchPairId;
import com.powsybl.openrao.data.cracapi.Crac;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -36,12 +31,21 @@
import java.time.OffsetDateTime;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyBoolean;
import static org.mockito.Mockito.anyDouble;
import static org.mockito.Mockito.anyMap;
import static org.mockito.Mockito.anyString;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

/**
* @author Joris Mancini {@literal <joris.mancini at rte-france.com>}
Expand Down Expand Up @@ -137,20 +141,4 @@ void testRun() {
fail();
}
}

@Test
void getCracCreationParametersTest() {
Set<BusBarChangeSwitches> busBarChangesSwitches = Set.of(new BusBarChangeSwitches("ra-id", Set.of(new SwitchPairId("switch-to-open", "switch-to-close"))));
CracCreationParameters cracCreationParameters = cseRunner.getCracCreationParameters(busBarChangesSwitches);
RaUsageLimits raUsageLimits = cracCreationParameters.getRaUsageLimitsPerInstant().get("curative");
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertEquals(6, raUsageLimits.getMaxRaPerTso().get("IT"));
assertEquals(5, raUsageLimits.getMaxRaPerTso().get("FR"));
assertEquals(1, raUsageLimits.getMaxRaPerTso().get("CH"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("AT"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("SI"));
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
assertEquals(1, cseCracCreationParameters.getBusBarChangeSwitches("ra-id").getSwitchPairs().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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/.
*/
package com.farao_community.farao.cse.network_processing;

import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.JsonCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.InputStream;
import java.util.Set;

/**
* @author Ameni Walha {@literal <ameni.walha at rte-france.com>}
*/
public final class CracCreationParametersService {
private static final Logger LOGGER = LoggerFactory.getLogger(CracCreationParametersService.class);

private CracCreationParametersService() {
// utility class
}

public static CracCreationParameters getCracCreationParameters(InputStream cracCreationParamsInputStream, Set<BusBarChangeSwitches> busBarChangeSwitches) {
CracCreationParameters cracCreationParameters = JsonCracCreationParameters.read(cracCreationParamsInputStream);
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
if (cseCracCreationParameters != null) {
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
} else {
cseCracCreationParameters = new CseCracCreationParameters();
cseCracCreationParameters.setBusBarChangeSwitchesSet(busBarChangeSwitches);
cracCreationParameters.addExtension(CseCracCreationParameters.class, cseCracCreationParameters);
}
return cracCreationParameters;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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/.
*/
package com.farao_community.farao.cse.network_processing;

import com.powsybl.openrao.data.cracapi.RaUsageLimits;
import com.powsybl.openrao.data.craccreation.creator.api.parameters.CracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.BusBarChangeSwitches;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.CseCracCreationParameters;
import com.powsybl.openrao.data.craccreation.creator.cse.parameters.SwitchPairId;
import org.junit.jupiter.api.Test;

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

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

/**
* @author Ameni Walha {@literal <ameni.walha at rte-france.com>}
*/
class CracCreationParametersServiceTest {
@Test
void getCracCreationParametersTest() {
InputStream cracCreationParametersJsonPath = Objects.requireNonNull(getClass().getResourceAsStream("cseCracCreationParameters.json"));
Set<BusBarChangeSwitches> busBarChangesSwitches = Set.of(new BusBarChangeSwitches("ra-id", Set.of(new SwitchPairId("switch-to-open", "switch-to-close"))));
CracCreationParameters cracCreationParameters = CracCreationParametersService.getCracCreationParameters(cracCreationParametersJsonPath, busBarChangesSwitches);
RaUsageLimits raUsageLimits = cracCreationParameters.getRaUsageLimitsPerInstant().get("curative");
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertTrue(raUsageLimits.getMaxTopoPerTso().isEmpty());
assertEquals(6, raUsageLimits.getMaxRaPerTso().get("IT"));
assertEquals(5, raUsageLimits.getMaxRaPerTso().get("FR"));
assertEquals(1, raUsageLimits.getMaxRaPerTso().get("CH"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("AT"));
assertEquals(3, raUsageLimits.getMaxRaPerTso().get("SI"));
CseCracCreationParameters cseCracCreationParameters = cracCreationParameters.getExtension(CseCracCreationParameters.class);
assertEquals(1, cseCracCreationParameters.getBusBarChangeSwitches("ra-id").getSwitchPairs().size());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"crac-factory": "CracImplFactory",
"ra-usage-limits-per-instant": [
{
"instant": "curative",
"max-ra-per-tso": {
"IT": 6,
"FR": 5,
"CH": 1,
"AT": 3,
"SI": 3
}
}]
}

0 comments on commit 09d40a9

Please sign in to comment.