Skip to content

Commit

Permalink
Subnetwork support and adaptation to release 2023.3.0.
Browse files Browse the repository at this point in the history
Signed-off-by: Bertrand Rix <bertrand.rix@artelys.com>
  • Loading branch information
obrix committed Oct 25, 2023
1 parent d711d33 commit 00087b7
Show file tree
Hide file tree
Showing 56 changed files with 1,967 additions and 119 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ public class BufferedNetworkStoreClient extends AbstractForwardingNetworkStoreCl
delegate::updateTieLines,
delegate::removeTieLines));

private final NetworkCollectionIndex<CollectionBuffer<SubnetworkAttributes>> subnetworkResourcesToFlush
= new NetworkCollectionIndex<>(() -> new CollectionBuffer<>(delegate::createSubnetworks,
delegate::updateSubnetworks,
delegate::removeSubnetworks));

private final List<NetworkCollectionIndex<? extends CollectionBuffer<? extends IdentifiableAttributes>>> allBuffers = List.of(
networkResourcesToFlush,
substationResourcesToFlush,
Expand All @@ -147,7 +152,8 @@ public class BufferedNetworkStoreClient extends AbstractForwardingNetworkStoreCl
threeWindingsTransformerResourcesToFlush,
lineResourcesToFlush,
busResourcesToFlush,
tieLineResourcesToFlush);
tieLineResourcesToFlush,
subnetworkResourcesToFlush);

private final ExecutorService executorService;

Expand Down Expand Up @@ -532,6 +538,25 @@ public void removeTieLines(UUID networkUuid, int variantNum, List<String> tieLin
tieLineResourcesToFlush.getCollection(networkUuid, variantNum).remove(tieLinesId);
}

@Override
public void createSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subNetworkAttributes) {
for (Resource<SubnetworkAttributes> subnetworkResource : subNetworkAttributes) {
subnetworkResourcesToFlush.getCollection(networkUuid, subnetworkResource.getVariantNum()).create(subnetworkResource);
}
}

@Override
public void updateSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subnetworkResources, AttributeFilter attributeFilter) {
for (Resource<SubnetworkAttributes> subnetworkResource : subnetworkResources) {
subnetworkResourcesToFlush.getCollection(networkUuid, subnetworkResource.getVariantNum()).update(subnetworkResource, attributeFilter);
}
}

@Override
public void removeSubnetworks(UUID networkUuid, int variantNum, List<String> subnetworkIds) {
subnetworkResourcesToFlush.getCollection(networkUuid, variantNum).remove(subnetworkIds);
}

@Override
public void flush(UUID networkUuid) {
Stopwatch stopwatch = Stopwatch.createStarted();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -864,4 +864,38 @@ public void removeConfiguredBuses(UUID networkUuid, int variantNum, List<String>
ensureCached(ResourceType.CONFIGURED_BUS, networkUuid, variantNum);
delegate.removeConfiguredBuses(networkUuid, variantNum, configuredBusesId);
}

@Override
public void createSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subNetworkAttributes) {
for (Resource<SubnetworkAttributes> subNetworkResource : subNetworkAttributes) {
ensureCached(ResourceType.SUBNETWORK, networkUuid, subNetworkResource.getVariantNum());
}
delegate.createSubnetworks(networkUuid, subNetworkAttributes);
}

@Override
public List<Resource<SubnetworkAttributes>> getSubnetworks(UUID networkUuid, int variantNum) {
ensureCached(ResourceType.SUBNETWORK, networkUuid, variantNum);
return delegate.getSubnetworks(networkUuid, variantNum);
}

@Override
public Optional<Resource<SubnetworkAttributes>> getSubnetwork(UUID networkUuid, int variantNum, String subnetworkId) {
ensureCached(ResourceType.SUBNETWORK, networkUuid, variantNum);
return delegate.getSubnetwork(networkUuid, variantNum, subnetworkId);
}

@Override
public void removeSubnetworks(UUID networkUuid, int variantNum, List<String> subnetworkIds) {
ensureCached(ResourceType.SUBNETWORK, networkUuid, variantNum);
delegate.removeTieLines(networkUuid, variantNum, subnetworkIds);
}

@Override
public void updateSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subnetworkResources, AttributeFilter attributeFilter) {
for (Resource<SubnetworkAttributes> subnetworkResource : subnetworkResources) {
ensureCached(ResourceType.SUBNETWORK, networkUuid, subnetworkResource.getVariantNum());
}
delegate.updateSubnetworks(networkUuid, subnetworkResources, attributeFilter);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -773,6 +773,31 @@ public void updateTieLines(UUID networkUuid, List<Resource<TieLineAttributes>> t
updateAll("tie line", "/networks/{networkUuid}/tie-lines", tieLineResources, attributeFilter, networkUuid);
}

@Override
public void createSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subNetworkAttributes) {

}

@Override
public List<Resource<SubnetworkAttributes>> getSubnetworks(UUID networkUuid, int variantNum) {
return null;
}

@Override
public Optional<Resource<SubnetworkAttributes>> getSubnetwork(UUID networkUuid, int variantNum, String subnetworkId) {
return Optional.empty();
}

@Override
public void removeSubnetworks(UUID networkUuid, int variantNum, List<String> subnetworkIds) {

}

@Override
public void updateSubnetworks(UUID networkUuid, List<Resource<SubnetworkAttributes>> subnetworkResources, AttributeFilter attributeFilter) {

}

@Override
public Optional<Resource<IdentifiableAttributes>> getIdentifiable(UUID networkUuid, int variantNum, String id) {
return get("identifiable", "/networks/{networkUuid}/{variantNum}/identifiables/{id}", networkUuid, variantNum, id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ abstract class AbstractBranchAdder<T extends AbstractBranchAdder<T>> extends Abs

private String voltageLevelId2;

public AbstractBranchAdder(NetworkObjectIndex index) {
super(index);
public AbstractBranchAdder(NetworkObjectIndex index, String parentNetwork) {
super(index, parentNetwork);
}

public Integer getNode1() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ public abstract class AbstractHvdcConverterStationAdder<T extends AbstractHvdcCo

private float lossFactor = Float.NaN;

AbstractHvdcConverterStationAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index) {
super(voltageLevelResource, index);
AbstractHvdcConverterStationAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index, String parentNetwork) {
super(voltageLevelResource, index, parentNetwork);
}

public float getLossFactor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ abstract class AbstractIdentifiableAdder<T extends AbstractIdentifiableAdder<T>>

private boolean fictitious = false;

AbstractIdentifiableAdder(NetworkObjectIndex index) {
private String parentNetwork;

AbstractIdentifiableAdder(NetworkObjectIndex index, String parentNetwork) {
this.index = index;
this.parentNetwork = parentNetwork;
}

protected NetworkImpl getNetwork() {
Expand Down Expand Up @@ -74,6 +77,15 @@ public T setFictitious(boolean fictitious) {
return (T) this;
}

protected String getParentNetwork() {
return parentNetwork;
}

public T setParentNetwork(String parentNetwork) {
this.parentNetwork = parentNetwork;
return (T) this;
}

protected String checkAndGetUniqueId() {
if (id == null) {
throw new PowsyblException(getTypeDescription() + " id is not set");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,16 @@ public NetworkImpl getNetwork() {
return index.getNetwork();
}

@Override
public Network getParentNetwork() {
String parentId = getResource().getParentNetwork();
if (parentId == null || index.getNetwork().getId().equals(parentId)) {
return index.getNetwork();
} else {
return index.getSubnetwork(parentId).orElse(null);
}
}

protected void invalidateCalculatedBuses(List<? extends Terminal> terminals) {
terminals.stream().map(Terminal::getVoltageLevel).filter(Objects::nonNull).map(VoltageLevel::getId)
.forEach(id -> index.getVoltageLevel(id).ifPresent(VoltageLevelImpl::invalidateCalculatedBuses));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ abstract class AbstractInjectionAdder<T extends AbstractInjectionAdder<T>> exten

private String connectableBus;

public AbstractInjectionAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index) {
super(index);
public AbstractInjectionAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index, String parentNetwork) {
super(index, parentNetwork);
this.voltageLevelResource = voltageLevelResource;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
package com.powsybl.network.store.iidm.impl;

import com.google.common.collect.ImmutableList;
import com.powsybl.iidm.network.*;
import com.powsybl.network.store.model.IdentifiableAttributes;
import com.powsybl.network.store.model.Resource;
import org.joda.time.DateTime;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.Comparator.comparingInt;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toCollection;

public abstract class AbstractNetwork<D extends IdentifiableAttributes> extends AbstractIdentifiableImpl<Network, D> implements Network, Validable {

private final BusBreakerView busBreakerView = new BusBreakerViewImpl();

private final BusView busView = new BusViewImpl();

protected AbstractNetwork(NetworkObjectIndex index, Resource<D> resource) {
super(index, resource);
}

@Override
public DateTime getCaseDate() {
return index.getNetwork().getCaseDate();
}

@Override
public Network setCaseDate(DateTime dateTime) {
return null;
}

@Override
public int getForecastDistance() {
return 0;
}

@Override
public Network setForecastDistance(int i) {
return null;
}

@Override
public String getSourceFormat() {
return null;
}

@Override
public Iterable<Substation> getSubstations() {
return getSubstationStream().toList();
}

@Override
public int getSubstationCount() {
return (int) getSubstationStream().count();
}

@Override
public Iterable<Substation> getSubstations(Country country, String tsoId, String... geographicalTags) {
return getSubstations(Optional.ofNullable(country).map(Country::getName).orElse(null), tsoId, geographicalTags);
}

@Override
public Iterable<Substation> getSubstations(String country, String tsoId, String... geographicalTags) {
return getSubstationStream().filter(substation -> {
if (country != null && !country.equals(substation.getCountry().map(Country::getName).orElse(""))) {
return false;
}
if (tsoId != null && !tsoId.equals(substation.getTso())) {
return false;
}
for (String tag : geographicalTags) {
if (!substation.getGeographicalTags().contains(tag)) {
return false;
}
}
return true;
}).collect(Collectors.toList());
}

class BusBreakerViewImpl implements BusBreakerView {

@Override
public Iterable<Bus> getBuses() {
return getBusStream().collect(Collectors.toList());
}

@Override
public Stream<Bus> getBusStream() {
return getVoltageLevelStream().flatMap(vl -> vl.getBusBreakerView().getBusStream());
}

@Override
public int getBusCount() {
return getVoltageLevelStream().mapToInt(vl -> vl.getBusBreakerView().getBusCount()).sum();
}

@Override
public Iterable<Switch> getSwitches() {
return getSwitchStream().collect(Collectors.toList());
}

@Override
public Stream<Switch> getSwitchStream() {
return getVoltageLevelStream().flatMap(vl -> vl.getBusBreakerView().getSwitchStream());
}

@Override
public int getSwitchCount() {
return (int) getSwitchStream().count();
}

@Override
public Bus getBus(String id) {
Optional<Bus> busInBusBreakerTopo = index.getConfiguredBus(id).map(Function.identity()); // start search in BB topo
return busInBusBreakerTopo.or(() -> getVoltageLevelStream().map(vl -> vl.getBusBreakerView().getBus(id)) // fallback to search in NB topo
.filter(Objects::nonNull)
.findFirst())
.orElse(null);
}
}

class BusViewImpl implements Network.BusView {

@Override
public Iterable<Bus> getBuses() {
return getBusStream().collect(Collectors.toList());
}

@Override
public Stream<Bus> getBusStream() {
return getVoltageLevelStream().flatMap(vl -> vl.getBusView().getBusStream());
}

@Override
public Bus getBus(String id) {
return getBusStream().filter(b -> b.getId().equals(id)).findFirst().orElse(null);
}

@Override
public Collection<Component> getConnectedComponents() {
return getBusStream().map(Bus::getConnectedComponent)
.collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(Component::getNum))), ArrayList::new));
}

@Override
public Collection<Component> getSynchronousComponents() {
return getBusStream().map(Bus::getSynchronousComponent)
.collect(collectingAndThen(toCollection(() -> new TreeSet<>(comparingInt(Component::getNum))), ArrayList::new));
}
}

@Override
public BusBreakerView getBusBreakerView() {
return busBreakerView;
}

@Override
public BusView getBusView() {
return busView;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ abstract class AbstractSwitchAdder<T extends AbstractSwitchAdder<T>> extends Abs

private boolean open = false;

AbstractSwitchAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index) {
super(index);
AbstractSwitchAdder(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index, String parentNetwork) {
super(index, parentNetwork);
this.voltageLevelResource = voltageLevelResource;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@ public class BatteryAdderImpl extends AbstractInjectionAdder<BatteryAdderImpl> i
private double maxP = Double.NaN;

BatteryAdderImpl(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index) {
super(voltageLevelResource, index);
super(voltageLevelResource, index, index.getNetwork().getId());
}

BatteryAdderImpl(Resource<VoltageLevelAttributes> voltageLevelResource, NetworkObjectIndex index, String parentNetwork) {
super(voltageLevelResource, index, parentNetwork);
}

@Override
Expand Down Expand Up @@ -72,6 +76,7 @@ public Battery add() {
Resource<BatteryAttributes> resource = Resource.batteryBuilder()
.id(id)
.variantNum(index.getWorkingVariantNum())
.parentNetwork(getParentNetwork())
.attributes(BatteryAttributes.builder()
.voltageLevelId(getVoltageLevelResource().getId())
.name(getName())
Expand Down
Loading

0 comments on commit 00087b7

Please sign in to comment.