Skip to content

Commit

Permalink
Rename DynaWaltzCurve to DynawoCurve
Browse files Browse the repository at this point in the history
Add DynawoCurveBuilder

Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Jun 10, 2024
1 parent c803d65 commit 9231e71
Show file tree
Hide file tree
Showing 13 changed files with 324 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.Curve
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension

import com.powsybl.dynawaltz.DynaWaltzCurve
import com.powsybl.dynawaltz.DynawoCurve
import com.powsybl.dynawaltz.DynaWaltzProvider
import com.powsybl.dynawaltz.DynawoCurvesBuilder

import java.util.function.Consumer

Expand All @@ -25,6 +26,7 @@ import java.util.function.Consumer
@AutoService(CurveGroovyExtension.class)
class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {

//TODO use doc in builder
/**
* A curve for <pre>DynaWaltz</pre> can be defined in DSL using {@code staticId} and {@code variable} or {@code dynamicModelId} and {@code variable}.
* Definition with {@code staticId} and {@code variable} are used when no explicit dynamic component exists (buses).
Expand Down Expand Up @@ -57,7 +59,7 @@ class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {
DynaWaltzProvider.NAME
}

DynaWaltzCurve dynawoCurve(CurvesSpec curveSpec, Consumer<Curve> consumer) {
static void dynawoCurve(CurvesSpec curveSpec, Consumer<Curve> consumer) {

if (curveSpec.staticId && curveSpec.dynamicModelId) {
throw new DslException("Both staticId and dynamicModelId are defined")
Expand All @@ -71,34 +73,24 @@ class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {

for (String variable : curveSpec.variables) {
if (curveSpec.staticId) {
consumer.accept(new DynaWaltzCurve("NETWORK", curveSpec.staticId + "_" + variable))
consumer.accept(new DynawoCurve("NETWORK", curveSpec.staticId + "_" + variable))
} else {
consumer.accept(new DynaWaltzCurve(curveSpec.dynamicModelId, variable))
consumer.accept(new DynawoCurve(curveSpec.dynamicModelId, variable))
}
}
}

@Override
void load(Binding binding, Consumer<Curve> consumer, ReportNode reportNode) {
binding.curve = { Closure<Void> closure ->
Closure<Void> closure = { Closure<Void> closure ->
def cloned = closure.clone()
CurvesSpec curveSpec = new CurvesSpec()

cloned.delegate = curveSpec
cloned()

dynawoCurve(curveSpec, consumer)
}

binding.curves = { Closure<Void> closure ->
def cloned = closure.clone()
CurvesSpec curvesSpec = new CurvesSpec()

cloned.delegate = curvesSpec
DynawoCurvesBuilder curvesBuilder = new DynawoCurvesBuilder(reportNode)
cloned.delegate = curvesBuilder
cloned()

dynawoCurve(curvesSpec, consumer)
curvesBuilder.add(consumer)
}
binding.curve = closure
binding.curves = closure
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,12 @@

import com.google.common.jimfs.Configuration;
import com.google.common.jimfs.Jimfs;
import com.powsybl.dsl.DslException;
import com.powsybl.dynamicsimulation.Curve;
import com.powsybl.dynamicsimulation.CurvesSupplier;
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension;
import com.powsybl.dynamicsimulation.groovy.GroovyCurvesSupplier;
import com.powsybl.dynamicsimulation.groovy.GroovyExtension;
import com.powsybl.dynawaltz.DynaWaltzCurve;
import com.powsybl.dynawaltz.DynawoCurve;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Load;
Expand All @@ -40,7 +39,7 @@
/**
* @author Marcos de Miguel {@literal <demiguelm at aia.es>}
*/
class DynaWaltzGroovyCurvesSupplierTest {
class DynaWaltzGroovyCurvesSupplierTest extends AbstractModelSupplierTest {

private FileSystem fileSystem;
private Network network;
Expand Down Expand Up @@ -72,11 +71,11 @@ void test() {

@ParameterizedTest(name = "{1}")
@MethodSource("provideFileError")
void testScriptError(String fileName, String error) {
void testScriptError(String fileName, String report) throws IOException {
List<CurveGroovyExtension> extensions = validateGroovyExtension();
CurvesSupplier supplier = new GroovyCurvesSupplier(fileSystem.getPath(fileName), extensions);
DslException exception = assertThrows(DslException.class, () -> supplier.get(network));
assertEquals(error, exception.getMessage());
assertTrue(supplier.get(network, reportNode).isEmpty());
checkReportNode(report);
}

private static Stream<Arguments> provideFileError() {
Expand All @@ -95,8 +94,8 @@ private List<CurveGroovyExtension> validateGroovyExtension() {
}

private void validateCurve(Curve curve) {
assertEquals(DynaWaltzCurve.class, curve.getClass());
DynaWaltzCurve curveImpl = (DynaWaltzCurve) curve;
assertEquals(DynawoCurve.class, curve.getClass());
DynawoCurve curveImpl = (DynawoCurve) curve;
if (curveImpl.getModelId().equals("NETWORK")) {
assertTrue(Arrays.asList("NGEN_Upu_value", "NHV1_Upu_value", "NHV2_Upu_value", "NLOAD_Upu_value").contains(curveImpl.getVariable()));
} else if (network.getIdentifiable(curveImpl.getModelId()) instanceof Generator) {
Expand Down
9 changes: 3 additions & 6 deletions dynawaltz-dsl/src/test/resources/curves_variables.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import com.powsybl.iidm.network.Generator

for (Generator gen : network.generators) {
curves {
dynamicModelId gen.id
}
curves {
dynamicModelId "GEN"
}

Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public class DynaWaltzContext {
private final List<BlackBoxModel> dynamicModels;
private final List<BlackBoxModel> eventModels;
private final Map<String, EquipmentBlackBoxModel> staticIdBlackBoxModelMap;
private final List<Curve> curves;
private final List<DynawoCurve> curves;
private final Map<String, MacroStaticReference> macroStaticReferences = new LinkedHashMap<>();
private final List<MacroConnect> macroConnectList = new ArrayList<>();
private final Map<String, MacroConnector> macroConnectorsMap = new LinkedHashMap<>();
Expand Down Expand Up @@ -97,7 +97,10 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
.map(ContextDependentEvent.class::cast)
.forEach(e -> e.setEquipmentHasDynamicModel(this));

this.curves = Objects.requireNonNull(curves);
//TODO cast before context ?
this.curves = Objects.requireNonNull(curves).stream()
.map(DynawoCurve.class::cast)
.toList();
this.frequencySynchronizer = setupFrequencySynchronizer(dynamicModels.stream().anyMatch(AbstractBus.class::isInstance) ? SetPoint::new : OmegaRef::new);
this.macroConnectionsAdder = new MacroConnectionsAdder(this::getDynamicModel,
this::getPureDynamicModel,
Expand Down Expand Up @@ -256,8 +259,8 @@ public List<BlackBoxModel> getBlackBoxEventModels() {
return eventModels;
}

public List<Curve> getCurves() {
return Collections.unmodifiableList(curves);
public List<DynawoCurve> getCurves() {
return curves;
}

public boolean withCurves() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
* 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/.
*/

package com.powsybl.dynawaltz;

import com.powsybl.dynamicsimulation.Curve;
Expand All @@ -14,12 +13,13 @@
/**
* @author Mathieu Bague {@literal <[email protected]>}
*/
public class DynaWaltzCurve implements Curve {
//TODO switch to record
public class DynawoCurve implements Curve {

private final String dynamicModelId;
private final String variable;

public DynaWaltzCurve(String dynamicModelId, String variable) {
public DynawoCurve(String dynamicModelId, String variable) {
this.dynamicModelId = Objects.requireNonNull(dynamicModelId);
this.variable = Objects.requireNonNull(variable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* Copyright (c) 2024, 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;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynawaltz.builders.BuilderReports;

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynawoCurveBuilder {

private static final String DEFAULT_DYNAMIC_MODEL_ID = "NETWORK";

private final ReportNode reportNode;
private boolean isInstantiable = true;
private String dynamicModelId;
private String staticId;
private String variable;
private String[] variables;

public DynawoCurveBuilder(ReportNode reportNode) {
this.reportNode = reportNode;
}

public DynawoCurveBuilder dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId;
return this;
}

public DynawoCurveBuilder staticId(String staticId) {
this.staticId = staticId;
return this;
}

public DynawoCurveBuilder variables(String[] variables) {
this.variables = variables;
return this;
}

public DynawoCurveBuilder variable(String variable) {
this.variable = variable;
return this;
}

private String getId() {
return dynamicModelId != null ? dynamicModelId : staticId;
}

private void checkData() {
if (staticId != null && dynamicModelId != null) {
BuilderReports.reportFieldConflict(reportNode, "dynamicModelId", "staticId");
}
if (variable == null) {
BuilderReports.reportFieldNotSet(reportNode, "variable");
isInstantiable = false;
}
}

private boolean isInstantiable() {
checkData();
if (!isInstantiable) {
BuilderReports.reportCurveInstantiationFailure(reportNode, getId());
}
return isInstantiable;
}

//TODO handle list
public DynawoCurve build() {
if (isInstantiable()) {
return dynamicModelId != null ? new DynawoCurve(dynamicModelId, variable)
: new DynawoCurve(DEFAULT_DYNAMIC_MODEL_ID, staticId + "_" + variable);
}
return null;
}
}
109 changes: 109 additions & 0 deletions dynawaltz/src/main/java/com/powsybl/dynawaltz/DynawoCurvesBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/**
* Copyright (c) 2024, 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;

import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.Curve;
import com.powsybl.dynawaltz.builders.BuilderReports;

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

/**
* @author Laurent Issertial {@literal <laurent.issertial at rte-france.com>}
*/
public class DynawoCurvesBuilder {

private static final String DEFAULT_DYNAMIC_MODEL_ID = "NETWORK";

private final ReportNode reportNode;
private boolean isInstantiable = true;
private String dynamicModelId;
private String staticId;
private String[] variables;

public DynawoCurvesBuilder(ReportNode reportNode) {
this.reportNode = reportNode;
}

public DynawoCurvesBuilder dynamicModelId(String dynamicModelId) {
this.dynamicModelId = dynamicModelId;
return this;
}

public DynawoCurvesBuilder staticId(String staticId) {
this.staticId = staticId;
return this;
}

public DynawoCurvesBuilder variables(String... variables) {
this.variables = variables;
return this;
}

//TODO switch to deprecated ?
public DynawoCurvesBuilder variable(String variable) {
this.variables = new String[]{variable};
return this;
}

private String getId() {
return dynamicModelId != null ? dynamicModelId : staticId;
}

private void checkData() {
if (staticId != null && dynamicModelId != null) {
BuilderReports.reportFieldConflict(reportNode, "dynamicModelId", "staticId");
}
if (variables == null) {
BuilderReports.reportFieldNotSet(reportNode, "variables");
isInstantiable = false;
} else if (variables.length == 0) {
BuilderReports.reportEmptyList(reportNode, "variables");
isInstantiable = false;
}
}

private boolean isInstantiable() {
checkData();
if (!isInstantiable) {
BuilderReports.reportCurveInstantiationFailure(reportNode, getId());
}
return isInstantiable;
}

public void add(Consumer<Curve> curveConsumer) {
if (isInstantiable()) {
boolean hasDynamicModelId = dynamicModelId != null;
String id = hasDynamicModelId ? dynamicModelId : DEFAULT_DYNAMIC_MODEL_ID;
for (String variable : variables) {
curveConsumer.accept(new DynawoCurve(id, hasDynamicModelId ? variable : staticId + "_" + variable));
}
}
}

public List<Curve> buildAlt() {
List<Curve> curves = new ArrayList<>();
add(curves::add);
return curves;
}

public List<DynawoCurve> build() {
if (isInstantiable()) {
boolean hasDynamicModelId = dynamicModelId != null;
String id = hasDynamicModelId ? dynamicModelId : DEFAULT_DYNAMIC_MODEL_ID;
return Arrays.stream(variables)
.map(v -> new DynawoCurve(id, hasDynamicModelId ? v : staticId + "_" + v))
.toList();
}
return Collections.emptyList();
}
}
Loading

0 comments on commit 9231e71

Please sign in to comment.