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

Upgrade farao dep 1.29.0 #184

Merged
merged 2 commits into from
Apr 16, 2024
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 @@ -30,10 +30,10 @@ public DichotomyResultHelper(FileImporter fileImporter) {
this.fileImporter = fileImporter;
}

public String getLimitingElement(DichotomyResult<DichotomyRaoResponse> dichotomyResult) {
public String getLimitingElement(DichotomyResult<DichotomyRaoResponse> dichotomyResult, Network network) {
DichotomyStepResult<DichotomyRaoResponse> highestValidStepResult = dichotomyResult.getHighestValidStep();
Crac crac = fileImporter.importCracFromJson(highestValidStepResult.getValidationData()
.getRaoResponse().getCracFileUrl());
.getRaoResponse().getCracFileUrl(), network);
RaoResult raoResult = fileImporter.importRaoResult(highestValidStepResult.getValidationData()
.getRaoResponse().getRaoResultFileUrl(), crac);
FlowCnec worstCnec = CnecUtil.getWorstCnec(crac, raoResult);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public MultipleDichotomyResult<DichotomyRaoResponse> runMultipleDichotomy(CseReq
String limitingCause = TtcResult.limitingCauseToString(initialDichotomyResult.getLimitingCause());

if (initialDichotomyResult.hasValidStep()) {
limitingElement = dichotomyResultHelper.getLimitingElement(multipleDichotomyResult.getBestDichotomyResult());
limitingElement = dichotomyResultHelper.getLimitingElement(multipleDichotomyResult.getBestDichotomyResult(), network);
ttcString = String.valueOf(round(dichotomyResultHelper.computeHighestSecureItalianImport(initialDichotomyResult)));
printablePrasIds = toString(getActivatedRangeActionInPreventive(crac, initialDichotomyResult));
printableForcedPrasIds = toString(getForcedPrasIds(initialDichotomyResult));
Expand Down Expand Up @@ -126,7 +126,7 @@ public MultipleDichotomyResult<DichotomyRaoResponse> runMultipleDichotomy(CseReq
flattenPrasIds(forcedPrasIds));

if (nextDichotomyResult.hasValidStep()) {
String newLimitingElement = dichotomyResultHelper.getLimitingElement(nextDichotomyResult);
String newLimitingElement = dichotomyResultHelper.getLimitingElement(nextDichotomyResult, network);
double previousLowestUnsecureItalianImport =
dichotomyResultHelper.computeLowestUnsecureItalianImport(multipleDichotomyResult.getBestDichotomyResult());
double newLowestUnsecureItalianImport = dichotomyResultHelper.computeLowestUnsecureItalianImport(nextDichotomyResult);
Expand Down Expand Up @@ -181,7 +181,7 @@ public MultipleDichotomyResult<DichotomyRaoResponse> runMultipleDichotomy(CseReq
String finalLimitingCause = TtcResult.limitingCauseToString(multipleDichotomyResult.getBestDichotomyResult().getLimitingCause());

if (multipleDichotomyResult.getBestDichotomyResult().hasValidStep()) {
finalLimitingElement = dichotomyResultHelper.getLimitingElement(multipleDichotomyResult.getBestDichotomyResult());
finalLimitingElement = dichotomyResultHelper.getLimitingElement(multipleDichotomyResult.getBestDichotomyResult(), network);
finalTtcString = String.valueOf(round(dichotomyResultHelper.computeHighestSecureItalianImport(multipleDichotomyResult.getBestDichotomyResult())));
finalPrintablePrasIds = toString(getActivatedRangeActionInPreventive(crac, multipleDichotomyResult.getBestDichotomyResult()));
finalPrintableForcedPrasIds = toString(getForcedPrasIds(multipleDichotomyResult.getBestDichotomyResult()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public DichotomyStepResult<DichotomyRaoResponse> validateNetwork(Network network
String networkPresignedUrl = fileExporter.saveNetworkInArtifact(network, baseDirPathForCurrentStep + scaledNetworkName, "", processTargetDateTime, processType, isImportEcProcess);

try {
Crac crac = fileImporter.importCracFromJson(cracUrl);
Crac crac = fileImporter.importCracFromJson(cracUrl, network);
List<String> appliedRemedialActionInPreviousStep = lastDichotomyStepResult != null && lastDichotomyStepResult.getRaoResult() != null ? lastDichotomyStepResult.getRaoResult().getActivatedNetworkActionsDuringState(crac.getPreventiveState())
.stream().map(NetworkAction::getId).toList() : Collections.emptyList();
RaoRequest raoRequest = buildRaoRequest(networkPresignedUrl, baseDirPathForCurrentStep, appliedRemedialActionInPreviousStep);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ public CseCrac importCseCrac(String cracUrl) {
return cseCracImporter.importNativeCrac(cracInputStream);
}

public Crac importCracFromJson(String cracUrl) {
public Crac importCracFromJson(String cracUrl, Network network) {
InputStream cracResultStream = openUrlStream(cracUrl);
return CracImporters.importCrac(FileUtil.getFilenameFromUrl(cracUrl), cracResultStream);
return CracImporters.importCrac(FileUtil.getFilenameFromUrl(cracUrl), cracResultStream, network);
}

public ZonalData<Scalable> importGlsk(String glskUrl, Network network) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import com.farao_community.farao.dichotomy.api.results.DichotomyResult;
import com.farao_community.farao.dichotomy.api.results.DichotomyStepResult;
import com.farao_community.farao.rao_runner.api.resource.RaoResponse;
import com.powsybl.iidm.network.Identifiable;
import com.powsybl.iidm.network.Line;
import com.powsybl.iidm.network.Network;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -35,14 +38,19 @@ void testLimitingElement() throws IOException {
DichotomyStepResult<DichotomyRaoResponse> highestValidStep = Mockito.mock(DichotomyStepResult.class);
DichotomyRaoResponse dichotomyRaoResponse = Mockito.mock(DichotomyRaoResponse.class);
RaoResponse raoResponse = Mockito.mock(RaoResponse.class);
Network network = Mockito.mock(Network.class);
Identifiable line = Mockito.mock(Line.class);

Mockito.when(line.getId()).thenReturn("anId");
Mockito.when(network.getIdentifiable(Mockito.anyString())).thenReturn(line);

Mockito.when(dichotomyResult.getHighestValidStep()).thenReturn(highestValidStep);
Mockito.when(highestValidStep.getValidationData()).thenReturn(dichotomyRaoResponse);
Mockito.when(dichotomyRaoResponse.getRaoResponse()).thenReturn(raoResponse);
Mockito.when(raoResponse.getRaoResultFileUrl()).thenReturn("file://" + Objects.requireNonNull(getClass().getResource("rao-result-v1.1.json")).getPath());
Mockito.when(raoResponse.getCracFileUrl()).thenReturn("file://" + Objects.requireNonNull(getClass().getResource("crac-for-rao-result-v1.1.json")).getPath());

String limitingElement = dichotomyResultHelper.getLimitingElement(dichotomyResult);
String limitingElement = dichotomyResultHelper.getLimitingElement(dichotomyResult, network);

assertEquals("cnec1prevId", limitingElement);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,10 @@ private void mockCrac() {
private DichotomyResult<DichotomyRaoResponse> mockDichotomyResult(String limitingElement, double lowestUnsecureItalianImport) throws IOException {
DichotomyStepResult<DichotomyRaoResponse> highestValidStep = Mockito.mock(DichotomyStepResult.class);
DichotomyResult<DichotomyRaoResponse> dichotomyResult = Mockito.mock(DichotomyResult.class);

Mockito.when(dichotomyResult.getHighestValidStep()).thenReturn(highestValidStep);
Mockito.when(dichotomyResult.hasValidStep()).thenReturn(true);
Mockito.when(dichotomyResultHelper.getLimitingElement(dichotomyResult)).thenReturn(limitingElement);
Mockito.when(dichotomyResultHelper.getLimitingElement(dichotomyResult, network)).thenReturn(limitingElement);
Mockito.when(dichotomyResultHelper.computeLowestUnsecureItalianImport(dichotomyResult)).thenReturn(lowestUnsecureItalianImport);
return dichotomyResult;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public Logger getLoggerTest() {

@Test
void checkCracAndForcedPrasAreConsistent() {
Crac crac = CracImporters.importCrac("crac-for-forced-pras.json", Objects.requireNonNull(getClass().getResourceAsStream("crac-for-forced-pras.json")));
Network network = Network.read("network-for-forced-pras.xiidm", getClass().getResourceAsStream("network-for-forced-pras.xiidm"));
Crac crac = CracImporters.importCrac("crac-for-forced-pras.json", Objects.requireNonNull(getClass().getResourceAsStream("crac-for-forced-pras.json")), network);

Set<String> manualForcedPrasIds = Set.of("Open line NL1-NL2", "Open line BE2-FR3", "PRA_PST_BE");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
*/
package com.farao_community.farao.cse.data.cnec;

import com.powsybl.contingency.ContingencyElement;
import com.powsybl.openrao.commons.Unit;
import com.farao_community.farao.cse.data.CseDataException;

import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.Identifiable;
import com.powsybl.openrao.data.cracapi.Instant;
import com.powsybl.openrao.data.cracapi.NetworkElement;
import com.powsybl.openrao.data.cracapi.Contingency;
import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.data.cracapi.cnec.FlowCnec;
import com.powsybl.openrao.data.cracapi.cnec.Side;
import com.powsybl.openrao.data.cracapi.rangeaction.InjectionRangeAction;
Expand All @@ -38,7 +39,6 @@
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -250,40 +250,56 @@ public FlowCnecResult getFlowCnecResultInAmpere(FlowCnec flowCnec, Instant optim
return new FlowCnecResult(flow, iMax);
}

public String getAreaFrom(ContingencyElement contingencyElement) {
String nodeFrom = getNodeFrom(contingencyElement);
String nodeTo = getNodeTo(contingencyElement);
String countryFrom = UcteCountryCode.fromUcteCode(nodeFrom.charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nodeTo.charAt(0)).toString();
return getCountryOfNode(contingencyElement.getId(), countryFrom, countryTo);
}

public String getAreaTo(ContingencyElement contingencyElement) {
String nodeFrom = getNodeFrom(contingencyElement);
String nodeTo = getNodeTo(contingencyElement);
String countryFrom = UcteCountryCode.fromUcteCode(nodeFrom.charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nodeTo.charAt(0)).toString();
return getCountryOfNode(contingencyElement.getId(), countryTo, countryFrom);
}

public String getAreaFrom(NetworkElement networkElement) {
String nodeFrom = getNodeFrom(networkElement);
String nodeTo = getNodeTo(networkElement);
String countryFrom = UcteCountryCode.fromUcteCode(nodeFrom.charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nodeTo.charAt(0)).toString();
return getCountryOfNode(networkElement, countryFrom, countryTo);
return getCountryOfNode(networkElement.getId(), countryFrom, countryTo);
}

public String getAreaTo(NetworkElement networkElement) {
String nodeFrom = getNodeFrom(networkElement);
String nodeTo = getNodeTo(networkElement);
String countryFrom = UcteCountryCode.fromUcteCode(nodeFrom.charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nodeTo.charAt(0)).toString();
return getCountryOfNode(networkElement, countryTo, countryFrom);
return getCountryOfNode(networkElement.getId(), countryTo, countryFrom);
}

private String getAreaFrom(NetworkElement networkElement, NativeBranch nativeBranch) {
String countryFrom = UcteCountryCode.fromUcteCode(nativeBranch.getFrom().charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nativeBranch.getTo().charAt(0)).toString();
return getCountryOfNode(networkElement, countryFrom, countryTo);
return getCountryOfNode(networkElement.getId(), countryFrom, countryTo);
}

private String getAreaTo(NetworkElement networkElement, NativeBranch nativeBranch) {
String countryFrom = UcteCountryCode.fromUcteCode(nativeBranch.getFrom().charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nativeBranch.getTo().charAt(0)).toString();
return getCountryOfNode(networkElement, countryTo, countryFrom);
return getCountryOfNode(networkElement.getId(), countryTo, countryFrom);
}

private String getCountryOfNode(NetworkElement networkElement, String nodeCountry, String destinationNodeCountry) {
private String getCountryOfNode(String elementId, String nodeCountry, String destinationNodeCountry) {
if (!nodeCountry.equals(UcteCountryCode.XX.toString())) {
return nodeCountry;
} else {
String area1 = getCountrySide1(networkElement);
String area2 = getCountrySide2(networkElement);
String area1 = getCountrySide1(elementId);
String area2 = getCountrySide2(elementId);
if (StringUtils.equals(area1, destinationNodeCountry)) {
return area2;
} else {
Expand All @@ -292,31 +308,31 @@ private String getCountryOfNode(NetworkElement networkElement, String nodeCountr
}
}

private String getCountrySide1(NetworkElement networkElement) {
Optional<Substation> substationOpt = network.getBranch(networkElement.getId()).getTerminal1().getVoltageLevel().getSubstation();
private String getCountrySide1(String elementId) {
Optional<Substation> substationOpt = network.getBranch(elementId).getTerminal1().getVoltageLevel().getSubstation();
if (substationOpt.isPresent()) {
Optional<Country> country = substationOpt.get().getCountry();
if (country.isPresent()) {
return country.get().toString();
} else {
throw new CseDataException("NetworkElement " + networkElement.getId() + " has no country on side 1");
throw new CseDataException("NetworkElement " + elementId + " has no country on side 1");
}
} else {
throw new CseDataException("NetworkElement " + networkElement.getId() + " has no country on side 1");
throw new CseDataException("NetworkElement " + elementId + " has no country on side 1");
}
}

private String getCountrySide2(NetworkElement networkElement) {
Optional<Substation> substationOpt = network.getBranch(networkElement.getId()).getTerminal2().getVoltageLevel().getSubstation();
private String getCountrySide2(String elementId) {
Optional<Substation> substationOpt = network.getBranch(elementId).getTerminal2().getVoltageLevel().getSubstation();
if (substationOpt.isPresent()) {
Optional<Country> country = substationOpt.get().getCountry();
if (country.isPresent()) {
return country.get().toString();
} else {
throw new CseDataException("NetworkElement " + networkElement.getId() + " has no country on side 2");
throw new CseDataException("NetworkElement " + elementId + " has no country on side 2");
}
} else {
throw new CseDataException("NetworkElement " + networkElement.getId() + " has no country on side 2");
throw new CseDataException("NetworkElement " + elementId + " has no country on side 2");
}
}

Expand All @@ -328,25 +344,33 @@ public String getNodeTo(NetworkElement networkElement) {
return networkElement.getId().substring(9, 17);
}

public String getOrderCode(NetworkElement networkElement) {
return networkElement.getId().substring(18, 19);
public String getNodeFrom(ContingencyElement contingencyElement) {
return contingencyElement.getId().substring(0, 8);
}

public String getNodeTo(ContingencyElement contingencyElement) {
return contingencyElement.getId().substring(9, 17);
}

public String getOrderCode(ContingencyElement contingencyElement) {
return contingencyElement.getId().substring(18, 19);
}

public static String getOutageName(FlowCnec flowCnec) {
Optional<Contingency> contingencyOpt = flowCnec.getState().getContingency();
if (contingencyOpt.isPresent()) {
return contingencyOpt.get().getName();
return contingencyOpt.get().getName().orElse(contingencyOpt.get().getId());
} else {
return PREVENTIVE_OUTAGE_NAME;
}
}

public static Set<NetworkElement> getOutageElements(FlowCnec flowCnec) {
public static List<ContingencyElement> getOutageElements(FlowCnec flowCnec) {
Optional<Contingency> contingencyOpt = flowCnec.getState().getContingency();
if (contingencyOpt.isPresent()) {
return contingencyOpt.get().getNetworkElements();
return contingencyOpt.get().getElements();
} else {
return Collections.emptySet();
return Collections.emptyList();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.farao_community.farao.cse.data.xsd.ttc_rao.PreventiveResult;
import com.farao_community.farao.cse.data.xsd.ttc_rao.Status;
import com.farao_community.farao.cse.data.xsd.ttc_rao.StringValue;
import com.powsybl.openrao.data.cracapi.Contingency;
import com.powsybl.contingency.Contingency;
import com.powsybl.openrao.data.craccreation.creator.api.ElementaryCreationContext;
import com.powsybl.openrao.data.craccreation.creator.api.stdcreationcontext.RemedialActionCreationContext;
import com.powsybl.openrao.data.craccreation.creator.cse.remedialaction.CseHvdcCreationContext;
Expand Down Expand Up @@ -96,11 +96,11 @@ private static List<OutageResult> getOutageResults(CracResultsHelper cracResults
outage.setName(cseOutageCreationContext.getNativeId());
outageResult.setOutage(outage);
Contingency contingency = cracResultsHelper.getCrac().getContingency(cseOutageCreationContext.getCreatedContingencyId());
contingency.getNetworkElements().forEach(contingencyNetworkElement -> {
contingency.getElements().forEach(contingencyElement -> {
Branch branch = new Branch();
branch.setCode(getStringValue(cracResultsHelper.getOrderCode(contingencyNetworkElement)));
branch.setFromNode(getStringValue(cracResultsHelper.getNodeFrom(contingencyNetworkElement)));
branch.setToNode(getStringValue(cracResultsHelper.getNodeTo(contingencyNetworkElement)));
branch.setCode(getStringValue(cracResultsHelper.getOrderCode(contingencyElement)));
branch.setFromNode(getStringValue(cracResultsHelper.getNodeFrom(contingencyElement)));
branch.setToNode(getStringValue(cracResultsHelper.getNodeTo(contingencyElement)));
outage.getBranch().add(branch);
});

Expand Down
Loading
Loading