Skip to content

Commit

Permalink
Add curve supplier
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <[email protected]>
  • Loading branch information
Lisrte committed Jun 12, 2024
1 parent 9231e71 commit 80ed0af
Show file tree
Hide file tree
Showing 19 changed files with 285 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,12 @@ package com.powsybl.dynawaltz.dsl

import com.google.auto.service.AutoService
import com.powsybl.commons.report.ReportNode
import com.powsybl.dsl.DslException
import com.powsybl.dynamicsimulation.Curve
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension

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

import java.util.function.Consumer

/**
* An implementation of {@link CurveGroovyExtension} that adds the <pre>curve</pre> keyword to the DSL
*
Expand All @@ -26,60 +22,11 @@ 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).
* <pre>DynaWaltz</pre> expects {@code dynamicModelId} = “NETWORK” for these variables.
*/
static class CurvesSpec {
String dynamicModelId
String staticId
String[] variables

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

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

void variables(String[] variables) {
this.variables = variables
}

void variable(String variable) {
this.variables = [variable]
}
}

@Override
String getName() {
DynaWaltzProvider.NAME
}

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

if (curveSpec.staticId && curveSpec.dynamicModelId) {
throw new DslException("Both staticId and dynamicModelId are defined")
}
if (!curveSpec.variables) {
throw new DslException("'variables' field is not set")
}
if (curveSpec.variables.length == 0) {
throw new DslException("'variables' field is empty")
}

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

@Override
void load(Binding binding, Consumer<Curve> consumer, ReportNode reportNode) {
Closure<Void> closure = { Closure<Void> closure ->
Expand All @@ -92,5 +39,4 @@ class DynaWaltzCurveGroovyExtension implements CurveGroovyExtension {
binding.curve = closure
binding.curves = closure
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension;
import com.powsybl.dynamicsimulation.groovy.GroovyCurvesSupplier;
import com.powsybl.dynamicsimulation.groovy.GroovyExtension;
import com.powsybl.dynawaltz.DynawoCurve;
import com.powsybl.dynawaltz.curves.DynawoCurve;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.iidm.network.Generator;
import com.powsybl.iidm.network.Load;
Expand All @@ -22,17 +22,13 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.io.IOException;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;

import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -50,9 +46,6 @@ void setup() throws IOException {
network = EurostagTutorialExample1Factory.create();

Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves.groovy")), fileSystem.getPath("/curves.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_dynamicModelId_staticId.groovy")), fileSystem.getPath("/curves_dynamicModelId_staticId.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_variable.groovy")), fileSystem.getPath("/curves_variable.groovy"));
Files.copy(Objects.requireNonNull(getClass().getResourceAsStream("/curves_variables.groovy")), fileSystem.getPath("/curves_variables.groovy"));
}

@AfterEach
Expand All @@ -69,23 +62,6 @@ void test() {
curves.forEach(this::validateCurve);
}

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

private static Stream<Arguments> provideFileError() {
return Stream.of(
Arguments.of("/curves_dynamicModelId_staticId.groovy", "Both staticId and dynamicModelId are defined"),
Arguments.of("/curves_variable.groovy", "'variables' field is not set"),
Arguments.of("/curves_variables.groovy", "'variables' field is not set")
);
}

private List<CurveGroovyExtension> validateGroovyExtension() {
List<CurveGroovyExtension> extensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
assertEquals(1, extensions.size());
Expand Down

This file was deleted.

14 changes: 0 additions & 14 deletions dynawaltz-dsl/src/test/resources/curves_variable.groovy

This file was deleted.

11 changes: 0 additions & 11 deletions dynawaltz-dsl/src/test/resources/curves_variables.groovy

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.powsybl.commons.report.ReportNode;
import com.powsybl.dynamicsimulation.Curve;
import com.powsybl.dynamicsimulation.DynamicSimulationParameters;
import com.powsybl.dynawaltz.curves.DynawoCurve;
import com.powsybl.dynawaltz.models.AbstractPureDynamicBlackBoxModel;
import com.powsybl.dynawaltz.models.BlackBoxModel;
import com.powsybl.dynawaltz.models.EquipmentBlackBoxModel;
Expand Down Expand Up @@ -97,7 +98,6 @@ public DynaWaltzContext(Network network, String workingVariantId, List<BlackBoxM
.map(ContextDependentEvent.class::cast)
.forEach(e -> e.setEquipmentHasDynamicModel(this));

//TODO cast before context ?
this.curves = Objects.requireNonNull(curves).stream()
.map(DynawoCurve.class::cast)
.toList();
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* 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;
package com.powsybl.dynawaltz.curves;

import com.powsybl.dynamicsimulation.Curve;

Expand All @@ -13,13 +13,12 @@
/**
* @author Mathieu Bague {@literal <[email protected]>}
*/
//TODO switch to record
public class DynawoCurve implements Curve {

private final String dynamicModelId;
private final String variable;

public DynawoCurve(String dynamicModelId, String variable) {
DynawoCurve(String dynamicModelId, String variable) {
this.dynamicModelId = Objects.requireNonNull(dynamicModelId);
this.variable = Objects.requireNonNull(variable);
}
Expand Down
Loading

0 comments on commit 80ed0af

Please sign in to comment.