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

CNE export no longer uses network #1157

Merged
merged 7 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 6 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,12 +7,15 @@

package com.powsybl.openrao.data.cracio.cim.craccreator;

import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Network;
import com.powsybl.openrao.data.cracapi.Crac;
import com.powsybl.openrao.data.cracapi.CracCreationContext;
import com.powsybl.openrao.data.cracapi.CracCreationReport;
import com.powsybl.openrao.data.cracio.commons.api.ElementaryCreationContext;

import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

Expand All @@ -29,15 +32,20 @@ public class CimCracCreationContext implements CracCreationContext {
private Set<RemedialActionSeriesCreationContext> remedialActionSeriesCreationContexts;
private final CracCreationReport creationReport;
private final OffsetDateTime timeStamp;
private final OffsetDateTime networkCaseDate;
private final String networkName;
private Map<String, Branch<?>> networkBranches;

CimCracCreationContext(Crac crac, OffsetDateTime timeStamp, String networkName) {
CimCracCreationContext(Crac crac, OffsetDateTime timeStamp, Network network) {
this.crac = crac;
creationReport = new CracCreationReport();
this.timeStamp = timeStamp;
this.angleCnecCreationContexts = new HashSet<>();
this.voltageCnecCreationContexts = new HashSet<>();
this.networkName = networkName;
this.networkName = network.getNameOrId();
this.networkCaseDate = network.getCaseDate().toInstant().atOffset(ZoneOffset.UTC);
networkBranches = new HashMap<>();
network.getBranches().forEach(branch -> networkBranches.put(branch.getId(), branch));
}

protected CimCracCreationContext(CimCracCreationContext toCopy) {
Expand All @@ -51,6 +59,8 @@ protected CimCracCreationContext(CimCracCreationContext toCopy) {
this.creationReport = toCopy.creationReport;
this.timeStamp = toCopy.timeStamp;
this.networkName = toCopy.networkName;
this.networkCaseDate = toCopy.networkCaseDate;
this.networkBranches = new HashMap<>(toCopy.networkBranches);
}

@Override
Expand Down Expand Up @@ -213,6 +223,14 @@ public ElementaryCreationContext getContingencyCreationContextByName(String cont
return contingencyCreationContexts.stream().filter(contingencyCreationContext -> contingencyCreationContext.getNativeObjectName().equals(contingencyName)).findAny().orElse(null);
}

public OffsetDateTime getNetworkCaseDate() {
return networkCaseDate;
}

public Map<String, Branch<?>> getNetworkBranches() {
return networkBranches;
}

void setCreationFailure() {
this.isCreationSuccessful = false;
this.crac = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ CimCracCreationContext createCrac(CRACMarketDocument cimCrac, Network network, O
RaUsageLimitsAdder.addRaUsageLimits(crac, parameters);
this.network = network;
this.cimTimeSeries = new ArrayList<>(cimCrac.getTimeSeries());
this.creationContext = new CimCracCreationContext(crac, offsetDateTime, network.getNameOrId());
this.creationContext = new CimCracCreationContext(crac, offsetDateTime, network);

// Get warning messages from parameters parsing
CimCracCreationParameters cimCracCreationParameters = parameters.getExtension(CimCracCreationParameters.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.io.InputStream;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.*;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -347,6 +348,8 @@ void cracCreationWithParameters() throws IOException {
setUp("/cracs/CIM_21_1_1.xml", baseNetwork, OffsetDateTime.parse("2021-04-01T22:00Z"), cracCreationParameters);
assertTrue(cracCreationContext.isCreationSuccessful());
assertEquals(2, cracCreationContext.getCrac().getRaUsageLimits(preventiveInstant).getMaxRa());
assertEquals(OffsetDateTime.of(2021, 2, 9, 19, 30, 0, 0, ZoneOffset.UTC), cracCreationContext.getNetworkCaseDate());
assertEquals(14, cracCreationContext.getNetworkBranches().size());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import com.powsybl.openrao.raoapi.parameters.RaoParameters;
import com.powsybl.openrao.raoapi.parameters.extensions.LoopFlowParametersExtension;
import com.powsybl.openrao.raoapi.parameters.extensions.MnecParametersExtension;
import com.powsybl.iidm.network.Network;

import static com.powsybl.openrao.data.cneexportercommons.CneConstants.PATL_MEASUREMENT_TYPE;
import static com.powsybl.openrao.data.cneexportercommons.CneConstants.TATL_MEASUREMENT_TYPE;
Expand All @@ -25,16 +24,14 @@
public class CneHelper {

private Crac crac;
private Network network;
private boolean relativePositiveMargins;
private boolean withLoopflows;
private RaoResult raoResult;
private CneExporterParameters exporterParameters;
private double mnecAcceptableMarginDiminution;

public CneHelper(Crac crac, Network network, RaoResult raoResult, RaoParameters raoParameters, CneExporterParameters exporterParameters) {
public CneHelper(Crac crac, RaoResult raoResult, RaoParameters raoParameters, CneExporterParameters exporterParameters) {
this.crac = crac;
this.network = network;
this.raoResult = raoResult;
this.exporterParameters = exporterParameters;

Expand All @@ -55,10 +52,6 @@ public double getMnecAcceptableMarginDiminution() {
return mnecAcceptableMarginDiminution;
}

public Network getNetwork() {
return network;
}

public Crac getCrac() {
return crac;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import com.powsybl.openrao.data.cracio.commons.api.stdcreationcontext.UcteCracCreationContext;
import com.powsybl.openrao.data.raoresultapi.RaoResult;
import com.powsybl.openrao.raoapi.parameters.RaoParameters;
import com.powsybl.iidm.network.Network;

import javax.xml.datatype.DatatypeConfigurationException;
import java.time.OffsetDateTime;
Expand All @@ -43,9 +42,9 @@ public class CoreCne {
private final CneHelper cneHelper;
private final UcteCracCreationContext cracCreationContext;

public CoreCne(Crac crac, Network network, UcteCracCreationContext cracCreationContext, RaoResult raoResult, RaoParameters raoParameters, CneExporterParameters exporterParameters) {
public CoreCne(Crac crac, UcteCracCreationContext cracCreationContext, RaoResult raoResult, RaoParameters raoParameters, CneExporterParameters exporterParameters) {
marketDocument = new CriticalNetworkElementMarketDocument();
cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
this.cracCreationContext = cracCreationContext;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.powsybl.openrao.data.cracio.commons.api.stdcreationcontext.UcteCracCreationContext;
import com.powsybl.openrao.data.raoresultapi.RaoResult;
import com.powsybl.openrao.raoapi.parameters.RaoParameters;
import com.powsybl.iidm.network.Network;
import org.xml.sax.SAXException;

import javax.xml.XMLConstants;
Expand Down Expand Up @@ -45,11 +44,11 @@
*/
public class CoreCneExporter {

public void exportCne(Crac crac, Network network,
public void exportCne(Crac crac,
UcteCracCreationContext cracCreationContext,
RaoResult raoResult, RaoParameters raoParameters,
CneExporterParameters exporterParameters, OutputStream outputStream) {
CoreCne cne = new CoreCne(crac, network, cracCreationContext, raoResult, raoParameters, exporterParameters);
CoreCne cne = new CoreCne(crac, cracCreationContext, raoResult, raoParameters, exporterParameters);
cne.generate();
CriticalNetworkElementMarketDocument marketDocument = cne.getMarketDocument();
StringWriter stringWriter = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ void testExportTwoPreventiveCnecs() {
mockCnecResult(cnec2, 800, -200, -999999999, .2);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

List<ConstraintSeries> cnecsConstraintSeries = cneCnecsCreator.generate();
Expand Down Expand Up @@ -224,7 +224,7 @@ void testExportPreventivePureMnec() {
mockCnecResult(cnec1, 80, 20, 200, .1);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

List<ConstraintSeries> cnecsConstraintSeries = cneCnecsCreator.generate();
Expand Down Expand Up @@ -262,7 +262,7 @@ void testExportPreventiveCnecAndMnec() {
mockCnecResult(cnec1, 80, 20, 200, .1);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

List<ConstraintSeries> cnecsConstraintSeries = cneCnecsCreator.generate();
Expand Down Expand Up @@ -340,7 +340,7 @@ void testCurativeCnecs() {

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
when(raoResult.getActivatedNetworkActionsDuringState(crac.getState(cnecCur.getState().getContingency().orElseThrow(), curativeInstant))).thenReturn(Set.of(Mockito.mock(NetworkAction.class)));
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

List<ConstraintSeries> cnecsConstraintSeries = cneCnecsCreator.generate();
Expand Down Expand Up @@ -393,7 +393,7 @@ void testWithLoopFlow() {

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
raoParameters.addExtension(LoopFlowParametersExtension.class, new LoopFlowParametersExtension());
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneCnecsCreator cneCnecsCreator = new CoreCneCnecsCreator(cneHelper, new MockCracCreationContext(crac));

List<ConstraintSeries> cnecsConstraintSeries = cneCnecsCreator.generate();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ void testPstInitialSetpoint() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

List<ConstraintSeries> constraintSeriesList = cneRemedialActionsCreator.generate();
Expand Down Expand Up @@ -153,7 +153,7 @@ void testPstInitialSetpointUnused() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(false);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

List<ConstraintSeries> constraintSeriesList = cneRemedialActionsCreator.generate();
Expand All @@ -175,7 +175,7 @@ void testIgnorePstWithNoUsageRule() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

List<ConstraintSeries> constraintSeriesList = cneRemedialActionsCreator.generate();
Expand All @@ -201,7 +201,7 @@ void testPstUsedInPreventive() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);

Expand Down Expand Up @@ -254,7 +254,7 @@ void testPstUsedInCurative() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);

Expand Down Expand Up @@ -302,7 +302,7 @@ void testIgnoreNetworkActionWithNoUsageRule() {
Mockito.when(raoResult.getActivatedNetworkActionsDuringState(any())).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), new ArrayList<>());

List<ConstraintSeries> constraintSeriesList = cneRemedialActionsCreator.generate();
Expand All @@ -322,7 +322,7 @@ void testNetworkActionUsedInPreventive() {
Mockito.when(raoResult.getActivatedNetworkActionsDuringState(crac.getPreventiveState())).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);

Expand Down Expand Up @@ -366,7 +366,7 @@ void testNetworkActionUsedInCurative() {
Mockito.when(raoResult.getActivatedNetworkActionsDuringState(crac.getState("contingency-id", curativeInstant))).thenReturn(Set.of(networkAction));

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);

CoreCneRemedialActionsCreator cneRemedialActionsCreator = new CoreCneRemedialActionsCreator(cneHelper, new MockCracCreationContext(crac), cnecsConstraintSeries);

Expand Down Expand Up @@ -412,7 +412,7 @@ void testPstInitialSetpointInverted() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
raContext.setInverted(true);
Expand Down Expand Up @@ -456,7 +456,7 @@ void testPstUsedInPreventiveInverted() {
Mockito.when(raoResult.isActivatedDuringState(crac.getStates().iterator().next(), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
raContext.setInverted(true);
Expand Down Expand Up @@ -512,7 +512,7 @@ void testPstUsedInCurativeInverted() {
Mockito.when(raoResult.isActivatedDuringState(crac.getState("contingency-id", curativeInstant), pstRangeAction)).thenReturn(true);

raoParameters.getObjectiveFunctionParameters().setType(ObjectiveFunctionParameters.ObjectiveFunctionType.MAX_MIN_RELATIVE_MARGIN_IN_MEGAWATT);
CneHelper cneHelper = new CneHelper(crac, network, raoResult, raoParameters, exporterParameters);
CneHelper cneHelper = new CneHelper(crac, raoResult, raoParameters, exporterParameters);
UcteCracCreationContext cracCreationContext = new MockCracCreationContext(crac);
MockCracCreationContext.MockRemedialActionCreationContext raContext = (MockCracCreationContext.MockRemedialActionCreationContext) cracCreationContext.getRemedialActionCreationContexts().get(0);
raContext.setInverted(true);
Expand Down
Loading
Loading