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

DynaWaltz: Line model #147

Merged
merged 16 commits into from
Feb 3, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Copyright (c) 2022, RTE (http://www.rte-france.com/)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
* SPDX-License-Identifier: MPL-2.0
*/
package com.powsybl.dynawaltz.dsl.models.lines

import com.google.auto.service.AutoService
import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.DynamicModel
import com.powsybl.dynamicsimulation.groovy.DynamicModelGroovyExtension
import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.dynawaltz.models.buses.StandardBus
import com.powsybl.dynawaltz.models.lines.StandardLine
import com.powsybl.iidm.network.Branch

import java.util.function.Consumer

/**
* An implementation of {@link DynamicModelGroovyExtension} that adds the <pre>Line</pre> keyword to the DSL
*
* @author Dimitri Baudrier <dimitri.baudrier at rte-france.com>
*/
@AutoService(DynamicModelGroovyExtension.class)
class LineGroovyExtension implements DynamicModelGroovyExtension {

static class LineSpec {
String dynamicModelId
String staticId
String parameterSetId
Branch.Side side

void dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId
}

void staticId(String staticId) {
this.staticId = staticId
}

void parameterSetId(String parameterSetId) {
this.parameterSetId = parameterSetId
}

void side(Branch.Side side) {
this.side = side
}
}

String getName() {
return DynaWaltzProvider.NAME
}

void load(Binding binding, Consumer<DynamicModel> consumer) {
binding.Line = { Closure<Void> closure ->
def cloned = closure.clone()
LineSpec lineSpec = new LineSpec()

cloned.delegate = lineSpec
cloned()

if (!lineSpec.staticId) {
throw new DslException("'staticId' field is not set");
}
if (!lineSpec.parameterSetId) {
throw new DslException("'parameterSetId' field is not set")
}

String dynamicModelId = lineSpec.dynamicModelId ? lineSpec.dynamicModelId : lineSpec.staticId
Branch.Side side = lineSpec.side ? lineSpec.side : Branch.Side.ONE
consumer.accept(new StandardLine(dynamicModelId, lineSpec.staticId, lineSpec.parameterSetId, side))
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.dynawaltz.dsl.models.buses.BusGroovyExtension;
import com.powsybl.dynawaltz.dsl.models.generators.GeneratorModelGroovyExtension;
import com.powsybl.dynawaltz.dsl.models.lines.LineGroovyExtension;
import com.powsybl.dynawaltz.dsl.models.loads.LoadAlphaBetaGroovyExtension;
import com.powsybl.dynawaltz.dsl.models.loads.LoadOneTransformerGroovyExtension;
import com.powsybl.dynawaltz.models.AbstractBlackBoxModel;
import com.powsybl.dynawaltz.models.automatons.CurrentLimitAutomaton;
import com.powsybl.dynawaltz.dsl.automatons.CurrentLimitAutomatonGroovyExtension;
import com.powsybl.dynawaltz.models.buses.StandardBus;
import com.powsybl.dynawaltz.models.generators.*;
import com.powsybl.dynawaltz.models.lines.StandardLine;
import com.powsybl.dynawaltz.models.loads.LoadAlphaBeta;
import com.powsybl.dynawaltz.models.loads.LoadOneTransformer;
import com.powsybl.iidm.network.*;
Expand Down Expand Up @@ -65,7 +67,7 @@ public void tearDown() throws IOException {
public void test() {

List<DynamicModelGroovyExtension> extensions = GroovyExtension.find(DynamicModelGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(6, extensions.size());
assertEquals(7, extensions.size());
extensions.forEach(this::validateExtension);

DynamicModelsSupplier supplier = new GroovyDynamicModelsSupplier(fileSystem.getPath("/dynamicModels.groovy"), extensions);
Expand All @@ -76,7 +78,8 @@ public void test() {
int numGenerators = network.getGeneratorCount();
int numLines = network.getLineCount();
long numBuses = network.getBusBreakerView().getBusStream().count();
int expectedDynamicModelsSize = numLoads + numGenerators + numLines + (int) numBuses;
long numAutomatons = dynamicModels.stream().filter(CurrentLimitAutomaton.class::isInstance).count();
int expectedDynamicModelsSize = numLoads + numGenerators + numLines + (int) numBuses + (int) numAutomatons;
assertEquals(expectedDynamicModelsSize, dynamicModels.size());
dynamicModels.forEach(this::validateModel);
}
Expand Down Expand Up @@ -160,7 +163,8 @@ private void validateExtension(DynamicModelGroovyExtension extension) {
boolean isLoadExtension = isLoadAlphaBetaExtension || isLoadOneTransformerExtension;
boolean isGeneratorExtension = extension instanceof GeneratorModelGroovyExtension;
boolean isBusExtension = extension instanceof BusGroovyExtension;
boolean isDynamicModelExtension = isLoadExtension || isGeneratorExtension || isBusExtension;
boolean isLineExtension = extension instanceof LineGroovyExtension;
boolean isDynamicModelExtension = isLoadExtension || isGeneratorExtension || isBusExtension || isLineExtension;

boolean isCurrentLimitAutomatonExtension = extension instanceof CurrentLimitAutomatonGroovyExtension;
boolean isAutomatonExtension = isCurrentLimitAutomatonExtension;
Expand Down Expand Up @@ -198,6 +202,11 @@ private void validateModel(DynamicModel dynamicModel) {
assertEquals("BBM_" + identifiable.getId(), blackBoxModel.getDynamicModelId());
assertEquals("SB", blackBoxModel.getParameterSetId());
assertTrue(identifiable instanceof Bus);
} else if (blackBoxModel instanceof StandardLine) {
Identifiable<?> identifiable = network.getIdentifiable(blackBoxModel.getStaticId().orElse(null));
assertEquals("BBM_" + identifiable.getId(), blackBoxModel.getDynamicModelId());
assertEquals("SL", blackBoxModel.getParameterSetId());
assertTrue(identifiable instanceof Line);
}
}

Expand Down
8 changes: 8 additions & 0 deletions dynawaltz-dsl/src/test/resources/dynamicModels.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/


import com.powsybl.dynawaltz.models.lines.StandardLine
import com.powsybl.iidm.network.Branch
import com.powsybl.iidm.network.Bus
import com.powsybl.iidm.network.Line
Expand Down Expand Up @@ -78,6 +79,13 @@ for (Line line : network.lines) {
parameterSetId "CLA"
side Branch.Side.TWO
}

Line {
staticId line.id
dynamicModelId "BBM_" + line.id
parameterSetId "SL"
side Branch.Side.TWO
}
}

for (Bus bus : network.busBreakerView.buses) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,9 @@ protected void writeBlackBoxModel(XMLStreamWriter writer, DynaWaltzContext conte
writer.writeStartElement(DYN_URI, "blackBoxModel");
writeDynamicAttributes(writer, context);
writer.writeAttribute("staticId", staticId);
MacroStaticReference.writeMacroStaticRef(writer, getLib());
if (!getVarsMapping().isEmpty()) {
MacroStaticReference.writeMacroStaticRef(writer, getLib());
}
writer.writeEndElement();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ public List<Model> getModelsConnectedTo(DynaWaltzContext context) {
if (line == null) {
throw new PowsyblException("Unknown line static id: " + lineStaticId);
}
String connectedStaticId = line.getTerminal(side).getBusBreakerView().getConnectableBus().getId();
BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModelMap().get(connectedStaticId);
BlackBoxModel connectedBbm = context.getStaticIdBlackBoxModelMap().get(line.getId());
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
if (connectedBbm == null) {
return List.of(context.getNetworkModel().getDefaultLineModel(lineStaticId, side));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package com.powsybl.dynawaltz.models.lines;

import com.powsybl.commons.PowsyblException;
import com.powsybl.dynawaltz.DynaWaltzContext;
import com.powsybl.dynawaltz.models.AbstractBlackBoxModel;
import com.powsybl.dynawaltz.models.Model;
import com.powsybl.dynawaltz.models.VarConnection;
import com.powsybl.dynawaltz.models.VarMapping;
import com.powsybl.dynawaltz.models.buses.BusModel;
import com.powsybl.iidm.network.Branch;
import com.powsybl.iidm.network.Bus;
import com.powsybl.iidm.network.Line;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class StandardLine extends AbstractBlackBoxModel implements LineModel {

private final String sidePostfix;

public StandardLine(String dynamicModelId, String statidId, String parameterSetId, Branch.Side side) {
super(dynamicModelId, statidId, parameterSetId);
this.sidePostfix = LineModel.getSuffix(side);
}

@Override
public String getLib() {
return "Line";
}

@Override
public List<VarMapping> getVarsMapping() {
return Collections.emptyList();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect that some static vars correspond to dynamic vars, why isn't it the case? @gautierbureau

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly I don't know.
I made this development according to this example : https://github.com/dynawo/dynawo/blob/master/nrt/data/SMIB/SMIB_BasicTestCases/SMIB_1_StepPm_IIDM/SMIB.dyd

I didn't find any dyd file that describes a line with macroconnectors. So for now, I just assumed there's no varsMapping.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, none so far

}

@Override
public List<VarConnection> getVarConnectionsWith(Model connected) {
if (connected instanceof BusModel) {
BusModel busModel = (BusModel) connected;
return Arrays.asList(
new VarConnection(getIVarName(), busModel.getNumCCVarName()),
new VarConnection(getStateVarName(), busModel.getTerminalVarName())
);
} else {
throw new PowsyblException("StandardLineModel can only connect to BusModel");
}
}

@Override
public List<Model> getModelsConnectedTo(DynaWaltzContext dynaWaltzContext) {
Line line = dynaWaltzContext.getNetwork().getLine(getStaticId().orElse(null));
if (line == null) {
throw new PowsyblException("Line static id unkwown: " + getStaticId());
}
List<Model> connectedBbm = new ArrayList<>();
for (Bus b : dynaWaltzContext.getNetwork().getBusBreakerView().getBuses()) {
if (b.getLineStream().anyMatch(l -> l.equals(line))) {
connectedBbm.add(dynaWaltzContext.getStaticIdBlackBoxModelMap().get(b.getId()));
}
}
return connectedBbm;
}

@Override
public String getName() {
return getLib();
}

@Override
public String getIVarName() {
return getDynamicModelId() + sidePostfix;
}

@Override
public String getStateVarName() {
return getDynamicModelId() + "_state";
}

@Override
public String getDesactivateCurrentLimitsVarName() {
return getDynamicModelId() + "_desactivate_currentLimits";
}

@Override
public String getStateValueVarName() {
return getDynamicModelId() + "_state_value";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import com.powsybl.dynawaltz.models.events.EventQuadripoleDisconnection;
import com.powsybl.dynawaltz.models.events.EventSetPointBoolean;
import com.powsybl.dynawaltz.models.generators.*;
import com.powsybl.dynawaltz.models.lines.StandardLine;
import com.powsybl.dynawaltz.models.loads.LoadAlphaBeta;
import com.powsybl.dynawaltz.models.loads.LoadOneTransformer;
import com.powsybl.iidm.network.*;
Expand Down Expand Up @@ -94,10 +95,15 @@ public void setup() {
}
});
network.getBusBreakerView().getBuses().forEach(b -> {
if (b.getId().equals("NGEN")) {
if (b.getId().equals("NHV2") || b.getId().equals("NHV1")) {
dynamicModels.add(new StandardBus("BBM_" + b.getId(), b.getId(), "SB"));
}
});
network.getLineStream().forEach(l -> {
if (l.getId().equals("NHV1_NHV2_1")) {
dynamicModels.add(new StandardLine("Line_" + l.getId(), l.getId(), "SL", Branch.Side.ONE));
}
});

// Events
eventModels = new ArrayList<>();
Expand Down
Loading