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

Skip models with duplicate dynamic id for dynamic and event model lists #293

Merged
merged 5 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
33 changes: 15 additions & 18 deletions dynawaltz/src/main/java/com/powsybl/dynawaltz/DynaWaltzContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
List<Curve> curves, DynamicSimulationParameters parameters, DynaWaltzParameters dynaWaltzParameters) {
this.network = Objects.requireNonNull(network);
this.workingVariantId = Objects.requireNonNull(workingVariantId);
this.dynamicModels = checkDuplicateStaticId(Objects.requireNonNull(dynamicModels));
this.eventModels = checkEventModelIdUniqueness(Objects.requireNonNull(eventModels));
this.dynamicModels = checkDuplicateDynamicId(checkDuplicateStaticId(Objects.requireNonNull(dynamicModels.stream()))).toList();
Lisrte marked this conversation as resolved.
Show resolved Hide resolved
this.eventModels = checkDuplicateDynamicId(Objects.requireNonNull(eventModels.stream())).toList();
this.staticIdBlackBoxModelMap = getInputBlackBoxDynamicModelStream()
.filter(EquipmentBlackBoxModel.class::isInstance)
.map(EquipmentBlackBoxModel.class::cast)
Expand Down Expand Up @@ -163,31 +163,28 @@ public <T extends Model> T getPureDynamicModel(String dynamicId, Class<T> connec
}
}

private static List<BlackBoxModel> checkDuplicateStaticId(List<BlackBoxModel> dynamicModels) {
private static Stream<BlackBoxModel> checkDuplicateStaticId(Stream<BlackBoxModel> bbmStream) {
Set<String> staticIds = new HashSet<>();
return dynamicModels.stream()
return bbmStream
.filter(bbm -> {
if (bbm instanceof EquipmentBlackBoxModel eBbm && !staticIds.add(eBbm.getStaticId())) {
LOGGER.warn("Duplicate static id found: {} -> dynamic model {} {} will be skipped", eBbm.getStaticId(), eBbm.getLib(), eBbm.getDynamicModelId());
return false;
}
return true;
})
.collect(Collectors.toList());
});
}

private static List<BlackBoxModel> checkEventModelIdUniqueness(List<BlackBoxModel> eventModels) {
private static Stream<BlackBoxModel> checkDuplicateDynamicId(Stream<BlackBoxModel> bbmStream) {
Set<String> dynamicIds = new HashSet<>();
Set<String> duplicates = new HashSet<>();
for (BlackBoxModel bbm : eventModels) {
if (!dynamicIds.add(bbm.getDynamicModelId())) {
duplicates.add(bbm.getDynamicModelId());
}
}
if (!duplicates.isEmpty()) {
throw new PowsyblException("Duplicate dynamicId: " + duplicates);
}
return eventModels;
return bbmStream
.filter(bbm -> {
if (!dynamicIds.add(bbm.getDynamicModelId())) {
LOGGER.warn("Duplicate dynamic id found: {} -> model {} will be skipped", bbm.getDynamicModelId(), bbm.getName());
return false;
}
return true;
});
}

public void addMacroConnect(String macroConnectorId, List<MacroConnectAttribute> attributesFrom, List<MacroConnectAttribute> attributesTo) {
Expand Down Expand Up @@ -259,7 +256,7 @@ public Stream<BlackBoxModel> getBlackBoxEventModelStream() {
}

public List<BlackBoxModel> getBlackBoxEventModels() {
return Collections.unmodifiableList(eventModels);
return eventModels;
}

public List<Curve> getCurves() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@
import com.powsybl.dynawaltz.models.InjectionModel;
import com.powsybl.dynawaltz.models.generators.GeneratorFictitious;
import com.powsybl.dynawaltz.models.lines.LineModel;
import com.powsybl.dynawaltz.models.loads.BaseLoad;
import com.powsybl.dynawaltz.models.transformers.TapChangerModel;
import com.powsybl.iidm.network.Identifiable;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

Expand Down Expand Up @@ -61,14 +63,25 @@ void writeDynamicModelWithLoadsAndOnlyOneFictitiousGenerator() throws SAXExcepti
@Test
void duplicateStaticId() {
dynamicModels.clear();
network.getGeneratorStream().forEach(gen -> {
if (gen.getId().equals("GEN5") || gen.getId().equals("GEN6")) {
dynamicModels.add(new GeneratorFictitious("BBM_" + gen.getId(), network.getGenerator("GEN5"), "GF"));
}
});
BaseLoad load1 = new BaseLoad("BBM_LOAD", network.getLoad("LOAD"), "lab", "LoadAlphaBeta");
BaseLoad load2 = new BaseLoad("BBM_LOAD2", network.getLoad("LOAD"), "lab", "LoadAlphaBeta");
dynamicModels.add(load1);
dynamicModels.add(load2);
String workingVariantId = network.getVariantManager().getWorkingVariantId();
DynaWaltzContext context = new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load());
Assertions.assertThat(context.getBlackBoxDynamicModels()).containsExactly(load1);
}

@Test
void duplicateDynamicId() {
dynamicModels.clear();
BaseLoad load1 = new BaseLoad("BBM_LOAD", network.getLoad("LOAD"), "lab", "LoadAlphaBeta");
BaseLoad load2 = new BaseLoad("BBM_LOAD", network.getLoad("LOAD2"), "lab", "LoadAlphaBeta");
dynamicModels.add(load1);
dynamicModels.add(load2);
String workingVariantId = network.getVariantManager().getWorkingVariantId();
DynaWaltzContext context = new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load());
assertEquals(1, context.getBlackBoxDynamicModels().size());
Assertions.assertThat(context.getBlackBoxDynamicModels()).containsExactly(load1);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,19 @@
*/
package com.powsybl.dynawaltz.xml;

import com.powsybl.commons.PowsyblException;
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
import com.powsybl.dynawaltz.DynaWaltzContext;
import com.powsybl.dynawaltz.DynaWaltzParameters;
import com.powsybl.dynawaltz.models.events.EventInjectionDisconnection;
import com.powsybl.dynawaltz.models.events.EventQuadripoleDisconnection;
import com.powsybl.dynawaltz.models.generators.SynchronousGenerator;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.xml.sax.SAXException;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;

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

/**
* @author Marcos de Miguel <demiguelm at aia.es>
*/
Expand All @@ -43,12 +40,14 @@ void writeDynamicModel() throws SAXException, IOException, XMLStreamException {
@Test
void duplicateEventId() {
eventModels.clear();
eventModels.add(new EventQuadripoleDisconnection(network.getLine("NHV1_NHV2_1"), 5));
EventQuadripoleDisconnection event1 = new EventQuadripoleDisconnection(network.getLine("NHV1_NHV2_1"), 5);
EventInjectionDisconnection event2 = new EventInjectionDisconnection(network.getGenerator("GEN2"), 1, true);
eventModels.add(event1);
eventModels.add(new EventQuadripoleDisconnection(network.getLine("NHV1_NHV2_1"), 5, true, false));
eventModels.add(new EventInjectionDisconnection(network.getGenerator("GEN2"), 1, true));
eventModels.add(event2);
eventModels.add(new EventInjectionDisconnection(network.getGenerator("GEN2"), 1, false));
String workingVariantId = network.getVariantManager().getWorkingVariantId();
Exception e = assertThrows(PowsyblException.class, () -> new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, null, null));
assertEquals("Duplicate dynamicId: [Disconnect_NHV1_NHV2_1, Disconnect_GEN2]", e.getMessage());
DynaWaltzContext context = new DynaWaltzContext(network, workingVariantId, dynamicModels, eventModels, curves, DynamicSimulationParameters.load(), DynaWaltzParameters.load());
Assertions.assertThat(context.getBlackBoxEventModels()).containsExactly(event1, event2);
}
}
Loading