Skip to content

Commit

Permalink
Merge branch 'lazy-loading-extension-attributes' of https://github.co…
Browse files Browse the repository at this point in the history
…m/powsybl/powsybl-network-store into generic-topleveldocument
  • Loading branch information
antoinebhs committed Sep 17, 2024
2 parents b272714 + 1548f23 commit 21866dd
Show file tree
Hide file tree
Showing 32 changed files with 523 additions and 565 deletions.
20 changes: 1 addition & 19 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
uses: actions/checkout@v1

- name: Build with Maven
run: mvn --batch-mode -Pjacoco install
run: mvn --batch-mode -Pjacoco verify

- name: Run SonarCloud analysis on server
run: >
Expand All @@ -36,24 +36,6 @@ jobs:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}

- name: Build Docker image - Main
if: github.ref == 'refs/heads/main'
run: >
mvn --batch-mode deploy -DskipTests -Dmaven.install.skip -Dmaven.deploy.skip -Dpowsybl.docker.deploy
-Djib.httpTimeout=60000
-Djib.to.image=docker.io/powsybl/network-store-server
-Djib.to.auth.username=powsyblci
-Djib.to.auth.password=${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Docker image - Tag
if: startsWith(github.ref, 'refs/tags/')
run: >
mvn --batch-mode deploy -DskipTests -Dmaven.install.skip -Dmaven.deploy.skip -Dpowsybl.docker.deploy
-Djib.httpTimeout=60000
-Djib.to.image=docker.io/powsybl/network-store-server:${GITHUB_REF_NAME#v}
-Djib.to.auth.username=powsyblci
-Djib.to.auth.password=${{ secrets.DOCKERHUB_TOKEN }}
- name: Broadcast update event
if: github.ref == 'refs/heads/main'
uses: gridsuite/broadcast-event@main
Expand Down
2 changes: 1 addition & 1 deletion network-store-client-distribution/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client-parent</artifactId>
<version>1.16.0-SNAPSHOT</version>
<version>1.18.0-SNAPSHOT</version>
</parent>

<artifactId>powsybl-network-store-client-distribution</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion network-store-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<parent>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-network-store-client-parent</artifactId>
<version>1.16.0-SNAPSHOT</version>
<version>1.18.0-SNAPSHOT</version>
</parent>

<artifactId>powsybl-network-store-client</artifactId>
Expand Down
2 changes: 1 addition & 1 deletion network-store-iidm-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<parent>
<artifactId>powsybl-network-store-client-parent</artifactId>
<groupId>com.powsybl</groupId>
<version>1.16.0-SNAPSHOT</version>
<version>1.18.0-SNAPSHOT</version>
</parent>

<artifactId>powsybl-network-store-iidm-impl</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.iidm.network.Injection;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.iidm.network.ThreeSides;
import com.powsybl.iidm.network.ValidationUtil;
import com.powsybl.iidm.network.extensions.ConnectablePosition;
import com.powsybl.network.store.model.AbstractIdentifiableAttributes;
import com.powsybl.network.store.model.InjectionAttributes;
Expand Down Expand Up @@ -57,6 +58,19 @@ public <E extends Extension<I>> E createConnectablePositionExtension() {
return extension;
}

protected void setRegTerminal(Terminal regulatingTerminal) {
ValidationUtil.checkRegulatingTerminal(this, regulatingTerminal, getNetwork());
if (regulatingTerminal instanceof TerminalImpl<?>) {
regulatingPoint.setRegulatingTerminal((TerminalImpl<?>) regulatingTerminal);
} else {
regulatingPoint.setRegulatingTerminalAsLocalTerminal();
}
}

public Terminal getRegulatingTerminal() {
return regulatingPoint.getRegulatingTerminal();
}

@Override
public <E extends Extension<I>> E getExtension(Class<? super E> type) {
E extension;
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,32 @@
*/
package com.powsybl.network.store.iidm.impl;

import com.powsybl.iidm.network.*;
import com.powsybl.network.store.model.AttributeFilter;
import com.powsybl.network.store.model.CalculatedBusAttributes;
import com.powsybl.iidm.network.Battery;
import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.Component;
import com.powsybl.iidm.network.Connectable;
import com.powsybl.iidm.network.DanglingLine;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.HvdcConverterStation;
import com.powsybl.iidm.network.IdentifiableType;
import com.powsybl.iidm.network.LccConverterStation;
import com.powsybl.iidm.network.Line;
import com.powsybl.iidm.network.Load;
import com.powsybl.iidm.network.ShuntCompensator;
import com.powsybl.iidm.network.StaticVarCompensator;
import com.powsybl.iidm.network.Terminal;
import com.powsybl.iidm.network.ThreeWindingsTransformer;
import com.powsybl.iidm.network.TwoWindingsTransformer;
import com.powsybl.iidm.network.ValidationException;
import com.powsybl.iidm.network.VoltageLevel;
import com.powsybl.iidm.network.VscConverterStation;
import com.powsybl.network.store.model.ConfiguredBusAttributes;
import com.powsybl.network.store.model.Resource;
import org.apache.commons.collections4.CollectionUtils;

import java.util.List;
import java.util.Optional;
import java.util.function.*;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -55,146 +69,39 @@ public VoltageLevel getVoltageLevel() {

@Override
public double getV() {
return getBusAttribute(this::getConfiguredBusVoltage, this::getCalculatedBusVoltage);
return getResource().getAttributes().getV();
}

@Override
public double getAngle() {
return getBusAttribute(this::getConfiguredBusAngle, this::getCalculatedBusAngle);
}

private double getBusAttribute(DoubleSupplier configuredBusGetter, Supplier<Optional<Double>> calculatedBusGetter) {
// Attempt to get the attribute from configuredBus
Double configuredValue = configuredBusGetter.getAsDouble();
if (!Double.isNaN(configuredValue)) {
return configuredValue;
}

// Attempt to get the attribute from calculatedBusesForBusView
return calculatedBusGetter.get().orElse(Double.NaN);
}

private double getConfiguredBusVoltage() {
return getConfiguredBusAttribute(ConfiguredBusAttributes::getV);
}

private double getConfiguredBusAngle() {
return getConfiguredBusAttribute(ConfiguredBusAttributes::getAngle);
}

private double getConfiguredBusAttribute(ToDoubleFunction<ConfiguredBusAttributes> attributeGetter) {
ConfiguredBusAttributes attributes = getResource().getAttributes();
if (attributes != null) {
double value = attributeGetter.applyAsDouble(attributes);
if (!Double.isNaN(value)) {
return value;
}
}
return Double.NaN;
}

private Optional<Double> getCalculatedBusVoltage() {
return getCalculatedBusAttribute(CalculatedBusAttributes::getV);
}

private Optional<Double> getCalculatedBusAngle() {
return getCalculatedBusAttribute(CalculatedBusAttributes::getAngle);
}

private Optional<Double> getCalculatedBusAttribute(Function<CalculatedBusAttributes, Double> attributeGetter) {
ConfiguredBusAttributes attributes = getResource().getAttributes();
if (attributes == null) {
return Optional.empty();
}

String voltageLevelId = attributes.getVoltageLevelId();
Optional<VoltageLevelImpl> voltageLevelOpt = index.getVoltageLevel(voltageLevelId);

return voltageLevelOpt
.filter(voltageLevel -> voltageLevel.getResource().getVariantNum() == getResource().getVariantNum())
.flatMap(voltageLevel -> voltageLevel.getResource().getAttributes().getCalculatedBusesForBusView() != null
? voltageLevel.getResource().getAttributes().getCalculatedBusesForBusView().stream().findFirst()
: Optional.empty()
)
.map(attributeGetter);
return getResource().getAttributes().getAngle();
}

@Override
public Bus setV(double v) {
validateNonNegative(v, "voltage");

ConfiguredBusAttributes configuredBus = getResource().getAttributes();
double oldValue = configuredBus.getV();

if (v < 0) {
throw new ValidationException(this, "voltage cannot be < 0");
}
double oldValue = getResource().getAttributes().getV();
if (v != oldValue) {
updateAttribute(v, oldValue, "v", configuredBus::setV);
updateCalculatedBusAttributesIfNeeded(v, configuredBus.getVoltageLevelId(), this::updateCalculatedBusAttributesV);
updateResource(res -> res.getAttributes().setV(v));
String variantId = index.getNetwork().getVariantManager().getWorkingVariantId();
index.notifyUpdate(this, "v", variantId, oldValue, v);
}
return this;
}

@Override
public Bus setAngle(double angle) {

ConfiguredBusAttributes configuredBus = getResource().getAttributes();
double oldValue = configuredBus.getAngle();

double oldValue = getResource().getAttributes().getAngle();
if (angle != oldValue) {
updateAttribute(angle, oldValue, "angle", configuredBus::setAngle);
updateCalculatedBusAttributesIfNeeded(angle, configuredBus.getVoltageLevelId(), this::updateCalculatedBusAttributesAngle);
updateResource(res -> res.getAttributes().setAngle(angle));
String variantId = index.getNetwork().getVariantManager().getWorkingVariantId();
index.notifyUpdate(this, "angle", variantId, oldValue, angle);
}
return this;
}

private void validateNonNegative(double value, String attributeName) {
if (value < 0) {
throw new ValidationException(this, attributeName + " cannot be < 0");
}
}

private void updateAttribute(double newValue, double oldValue, String attributeName, DoubleConsumer setter) {
updateResource(res -> setter.accept(newValue));
String variantId = index.getNetwork().getVariantManager().getWorkingVariantId();
index.notifyUpdate(this, attributeName, variantId, oldValue, newValue);
}

private void updateCalculatedBusAttributesIfNeeded(double newValue, String voltageLevelId,
BiConsumer<Double, List<CalculatedBusAttributes>> updater) {
Optional<VoltageLevelImpl> voltageLevelOpt = index.getVoltageLevel(voltageLevelId);

voltageLevelOpt.ifPresent(voltageLevel -> {
if (isSameVariant(voltageLevel)) {
List<CalculatedBusAttributes> calculatedBusAttributesList = voltageLevel.getResource()
.getAttributes()
.getCalculatedBusesForBusView();

if (CollectionUtils.isNotEmpty(calculatedBusAttributesList)) {
List<Integer> calculatedBusesNum = getAllTerminals().stream().map(t -> ((CalculatedBus) t.getBusView().getBus()).getCalculatedBusNum()).distinct().toList();
List<CalculatedBusAttributes> busesToUpdateList = IntStream.range(0, calculatedBusAttributesList.size())
.filter(calculatedBusesNum::contains)
.mapToObj(calculatedBusAttributesList::get)
.toList();
if (CollectionUtils.isNotEmpty(busesToUpdateList)) {
updater.accept(newValue, busesToUpdateList);
index.updateVoltageLevelResource(voltageLevel.getResource(), AttributeFilter.SV);
}
}
}
});
}

private boolean isSameVariant(VoltageLevelImpl voltageLevel) {
return voltageLevel.getResource().getVariantNum() == getResource().getVariantNum();
}

private void updateCalculatedBusAttributesV(double v, List<CalculatedBusAttributes> calculatedBusAttributesList) {
calculatedBusAttributesList.forEach(calculatedBusAttributes -> calculatedBusAttributes.setV(v));
}

private void updateCalculatedBusAttributesAngle(double angle, List<CalculatedBusAttributes> calculatedBusAttributesList) {
calculatedBusAttributesList.forEach(calculatedBusAttributes -> calculatedBusAttributes.setAngle(angle));
}

@Override
public double getFictitiousP0() {
return getResource().getAttributes().getFictitiousP0();
Expand Down
Loading

0 comments on commit 21866dd

Please sign in to comment.