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

remove xnodes config #174

Merged
merged 7 commits into from
Dec 20, 2023
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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,8 @@ public CseExportResponse run(CseExportRequest cseExportRequest) {
// Save again on MinIO to proper process location and naming
String networkWithPraUrl = saveNetworkWithPra(cseExportRequest, networkWithPra);
RaoResult raoResult = fileImporter.importRaoResult(raoResponse.getRaoResultFileUrl(), cseCracCreationContext.getCrac());
String ttcResultUrl = ttcRaoService.saveTtcRao(cseExportRequest, cseCracCreationContext, raoResult, preprocessedPsts);
Network networkForTtc = fileImporter.importNetwork(raoResponse.getNetworkWithPraFileUrl());
String ttcResultUrl = ttcRaoService.saveTtcRao(cseExportRequest, cseCracCreationContext, raoResult, networkForTtc, preprocessedPsts);
return new CseExportResponse(cseExportRequest.getId(), ttcResultUrl, networkWithPraUrl, logsFileUrl);
} catch (CseInternalException e) {
// Temporary return of an empty string for ttc logs file and cgm file
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

import com.farao_community.farao.cse.data.cnec.CracResultsHelper;
import com.farao_community.farao.cse.data.ttc_rao.TtcRao;
import com.farao_community.farao.cse.data.xnode.XNodeReader;
import com.farao_community.farao.cse.data.xsd.ttc_rao.CseRaoResult;
import com.farao_community.farao.cse.export_runner.app.FileUtil;
import com.farao_community.farao.cse.export_runner.app.configurations.XNodesConfiguration;
import com.farao_community.farao.cse.runner.api.resource.CseExportRequest;
import com.farao_community.farao.data.crac_creation.creator.cse.CseCracCreationContext;
import com.farao_community.farao.data.rao_result_api.RaoResult;
import com.powsybl.iidm.network.Network;
import org.springframework.stereotype.Service;

import java.util.Map;
Expand All @@ -26,18 +25,16 @@
public class TtcRaoService {

private final FileExporter fileExporter;
private final XNodesConfiguration xNodesConfiguration;

public TtcRaoService(FileExporter fileExporter, XNodesConfiguration xNodesConfiguration) {
public TtcRaoService(FileExporter fileExporter) {
this.fileExporter = fileExporter;
this.xNodesConfiguration = xNodesConfiguration;
}

public String saveTtcRao(CseExportRequest request, CseCracCreationContext cracCreationContext, RaoResult raoResult, Map<String, Integer> preprocessedPsts) {
public String saveTtcRao(CseExportRequest request, CseCracCreationContext cracCreationContext, RaoResult raoResult, Network network, Map<String, Integer> preprocessedPsts) {
CracResultsHelper cracResultsHelper = new CracResultsHelper(
cracCreationContext,
raoResult,
XNodeReader.getXNodes(xNodesConfiguration.getxNodesFilePath()));
network);
CseRaoResult cseRaoResult = TtcRao.generate(request.getTargetProcessDateTime(), cracResultsHelper, preprocessedPsts);
return fileExporter.saveTtcRao(cseRaoResult, request.getProcessType(), request.getTargetProcessDateTime(), FileUtil.getFilenameFromUrl(request.getCgmUrl()));
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
import com.farao_community.farao.cse.computation.BorderExchanges;
import com.farao_community.farao.cse.data.cnec.CracResultsHelper;
import com.farao_community.farao.cse.data.ttc_res.TtcResult;
import com.farao_community.farao.cse.data.xnode.XNodeReader;
import com.farao_community.farao.cse.data.xsd.ttc_res.Timestamp;
import com.farao_community.farao.cse.import_runner.app.dichotomy.DichotomyRaoResponse;
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.runner.api.resource.CseRequest;
import com.farao_community.farao.cse.import_runner.app.CseData;
import com.farao_community.farao.cse.import_runner.app.configurations.XNodesConfiguration;
import com.farao_community.farao.cse.runner.api.resource.ProcessType;
import com.farao_community.farao.data.crac_creation.creator.cse.CseCracCreationContext;
import com.farao_community.farao.data.rao_result_api.RaoResult;
Expand All @@ -36,12 +34,10 @@ public class TtcResultService {

private final FileExporter fileExporter;
private final FileImporter fileImporter;
private final XNodesConfiguration xNodesConfiguration;

public TtcResultService(FileExporter fileExporter, FileImporter fileImporter, XNodesConfiguration xNodesConfiguration) {
public TtcResultService(FileExporter fileExporter, FileImporter fileImporter) {
this.fileExporter = fileExporter;
this.fileImporter = fileImporter;
this.xNodesConfiguration = xNodesConfiguration;
}

public String saveFailedTtcResult(CseRequest cseRequest, String firstShiftNetworkName, TtcResult.FailedProcessData.FailedProcessReason failedProcessReason) {
Expand Down Expand Up @@ -76,7 +72,7 @@ public String saveTtcResult(CseRequest cseRequest, CseData cseData, CseCracCreat

RaoResult raoResult = fileImporter.importRaoResult(highestSecureStepRaoResponse.getRaoResponse().getRaoResultFileUrl(), cseCracCreationContext.getCrac());
CracResultsHelper cracResultsHelper = new CracResultsHelper(
cseCracCreationContext, raoResult, XNodeReader.getXNodes(xNodesConfiguration.getxNodesFilePath()));
cseCracCreationContext, raoResult, networkWithPra);
Timestamp timestamp = TtcResult.generate(ttcFiles, processData, cracResultsHelper, preprocessedPsts, preprocessedPisaLinks);
return fileExporter.saveTtcResult(timestamp, cseRequest.getTargetProcessDateTime(), cseRequest.getProcessType(), cseRequest.isImportEcProcess());
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import com.farao_community.farao.commons.Unit;
import com.farao_community.farao.cse.data.CseDataException;
import com.farao_community.farao.cse.data.xnode.XNode;
import com.farao_community.farao.data.crac_api.*;
import com.farao_community.farao.data.crac_api.cnec.FlowCnec;
import com.farao_community.farao.data.crac_api.cnec.Side;
Expand All @@ -21,7 +20,11 @@
import com.farao_community.farao.data.crac_creation.creator.cse.critical_branch.CseCriticalBranchCreationContext;
import com.farao_community.farao.data.crac_creation.creator.cse.outage.CseOutageCreationContext;
import com.farao_community.farao.data.rao_result_api.RaoResult;
import com.powsybl.iidm.network.Country;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.Substation;
import com.powsybl.ucte.network.UcteCountryCode;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;
Expand All @@ -35,13 +38,13 @@ public class CracResultsHelper {
private final CseCracCreationContext cseCracCreationContext;
private final Crac crac;
private final RaoResult raoResult;
private final List<XNode> xNodeList;
private final Network network;

public CracResultsHelper(CseCracCreationContext cseCracCreationContext, RaoResult result, List<XNode> xNodeList) {
public CracResultsHelper(CseCracCreationContext cseCracCreationContext, RaoResult result, Network network) {
this.cseCracCreationContext = cseCracCreationContext;
this.crac = cseCracCreationContext.getCrac();
this.raoResult = result;
this.xNodeList = xNodeList;
this.network = network;
}

public Crac getCrac() {
Expand Down Expand Up @@ -131,7 +134,7 @@ public List<CnecPreventive> getPreventiveCnecs() {
// CNEC name is in the FlowCnec name
FlowCnec flowCnecPrev = crac.getFlowCnec(branchCnecCreationContext.getCreatedCnecsIds().get(Instant.PREVENTIVE));
if (flowCnecPrev != null) {
CnecCommon cnecCommon = makeCnecCommon(flowCnecPrev.getName(), branchCnecCreationContext.getNativeBranch(),
CnecCommon cnecCommon = makeCnecCommon(flowCnecPrev, branchCnecCreationContext.getNativeBranch(),
((CseCriticalBranchCreationContext) branchCnecCreationContext).isSelected());
CnecPreventive cnecPrev = new CnecPreventive();
cnecPrev.setCnecCommon(cnecCommon);
Expand All @@ -154,10 +157,9 @@ public Map<String, MergedCnec> getMergedCnecs(String contingencyId) {
branchCnecCreationContexts.forEach(branchCnecCreationContext -> {
MergedCnec mergedCnec = new MergedCnec();
mergedCnecs.put(branchCnecCreationContext.getNativeId(), mergedCnec);
String cnecName = "undefined";
FlowCnec flowCnec = null;
for (Map.Entry<Instant, String> entry : branchCnecCreationContext.getCreatedCnecsIds().entrySet()) {
FlowCnec flowCnec = crac.getFlowCnec(entry.getValue());
cnecName = flowCnec.getName();
flowCnec = crac.getFlowCnec(entry.getValue());
FlowCnecResult flowCnecResult;
switch (entry.getKey()) {
case OUTAGE:
Expand All @@ -182,19 +184,20 @@ public Map<String, MergedCnec> getMergedCnecs(String contingencyId) {
throw new CseDataException("Couldn't find Cnec type in cnec Id : " + flowCnec.getId());
}
}
CnecCommon cnecCommon = makeCnecCommon(cnecName, branchCnecCreationContext.getNativeBranch(),
CnecCommon cnecCommon = makeCnecCommon(flowCnec, branchCnecCreationContext.getNativeBranch(),
vbochetRTE marked this conversation as resolved.
Show resolved Hide resolved
((CseCriticalBranchCreationContext) branchCnecCreationContext).isSelected());
mergedCnec.setCnecCommon(cnecCommon);
});
return mergedCnecs;
}

private CnecCommon makeCnecCommon(String nativeId, NativeBranch nativeBranch, boolean selected) {
private CnecCommon makeCnecCommon(FlowCnec cnec, NativeBranch nativeBranch, boolean selected) {
NetworkElement networkElement = cnec.getNetworkElement();
CnecCommon cnecCommon = new CnecCommon();
cnecCommon.setName(nativeId);
cnecCommon.setName(cnec.getName());
cnecCommon.setCode(makeCode(nativeBranch));
cnecCommon.setAreaFrom(getAreaFrom(nativeBranch));
cnecCommon.setAreaTo(getAreaTo(nativeBranch));
cnecCommon.setAreaFrom(getAreaFrom(networkElement, nativeBranch));
cnecCommon.setAreaTo(getAreaTo(networkElement, nativeBranch));
cnecCommon.setNodeFrom(nativeBranch.getFrom());
cnecCommon.setNodeTo(nativeBranch.getTo());
cnecCommon.setOrderCode(nativeBranch.getSuffix());
Expand Down Expand Up @@ -233,51 +236,76 @@ public FlowCnecResult getFlowCnecResultInAmpere(FlowCnec flowCnec, Instant optim
return new FlowCnecResult(flow, iMax);
}

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

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(nodeFrom, countryFrom, countryTo);
}

public String getAreaTo(NativeBranch nativeBranch) {
String countryFrom = UcteCountryCode.fromUcteCode(nativeBranch.getFrom().charAt(0)).toString();
String countryTo = UcteCountryCode.fromUcteCode(nativeBranch.getTo().charAt(0)).toString();
return getCountryOfNode(nativeBranch.getTo(), countryTo, countryFrom);
return getCountryOfNode(networkElement, 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(nodeTo, countryTo, countryFrom);
return getCountryOfNode(networkElement, 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);
}

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);
}

private String getCountryOfNode(String nodeFrom, String countryFrom, String countryTo) {
if (!countryFrom.equals(UcteCountryCode.XX.toString())) {
return countryFrom;
private String getCountryOfNode(NetworkElement networkElement, String nodeCountry, String destinationNodeCountry) {
if (!nodeCountry.equals(UcteCountryCode.XX.toString())) {
return nodeCountry;
} else {
Optional<XNode> xNodeOpt = xNodeList.stream().filter(xNode -> xNode.getName().equals(nodeFrom)).findFirst();
if (xNodeOpt.isPresent()) {
String area1 = xNodeOpt.get().getArea1();
String area2 = xNodeOpt.get().getArea2();
if (area1.equals(countryTo)) {
return area2;
} else {
return area1;
}
String area1 = getCountrySide1(networkElement);
String area2 = getCountrySide2(networkElement);
if (StringUtils.equals(area1, destinationNodeCountry)) {
return area2;
} else {
throw new CseDataException("XNode " + nodeFrom + " Not found in XNodes configuration file");
return area1;
}
}
}

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

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

Optional<Country> getCountryOptionalFromSubstation(Optional<Substation> substation) {
if (substation.isPresent()) {
Optional<Country> countryOpt = substation.get().getCountry();
if (countryOpt.isPresent()) {
return countryOpt;
}
}
return Optional.empty();
}

public String getNodeFrom(NetworkElement networkElement) {
Expand Down Expand Up @@ -306,7 +334,7 @@ public static Set<NetworkElement> getOutageElements(FlowCnec flowCnec) {
if (contingencyOpt.isPresent()) {
return contingencyOpt.get().getNetworkElements();
} else {
return Collections.EMPTY_SET;
return Collections.emptySet();
}
}

Expand Down
Loading