Skip to content

Commit

Permalink
[GridDyna] Using gridsuite filter library for mapping (#95)
Browse files Browse the repository at this point in the history
  • Loading branch information
thangqp authored Jul 12, 2024
1 parent a75742d commit b5e40d8
Show file tree
Hide file tree
Showing 41 changed files with 751 additions and 507 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,17 @@
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-dynawaltz</artifactId>
<version>2.5.0-alpha-1</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-dynawaltz-dsl</artifactId>
<version>2.5.0-alpha-1</version>
</dependency>
<dependency>
<groupId>org.gridsuite</groupId>
<artifactId>gridsuite-filter</artifactId>
<version>1.0.9</version>
</dependency>
<dependency>
<groupId>com.powsybl</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
*/
package org.gridsuite.ds.server;

import lombok.Getter;

/**
* @author Abdelsalem Hedhili <abdelsalem.hedhili at rte-france.com>
*/
@Getter
public class DynamicSimulationException extends RuntimeException {

public enum Type {
Expand All @@ -17,9 +20,11 @@ public enum Type {
MAPPING_NOT_PROVIDED,
RESULT_UUID_NOT_FOUND,
DYNAMIC_MAPPING_NOT_FOUND,
CREATE_MAPPING_SCRIPT_ERROR,
EXPORT_PARAMETERS_ERROR,
GET_DYNAMIC_MAPPING_ERROR,
CREATE_TIME_SERIES_ERROR,
DELETE_TIME_SERIES_ERROR
DELETE_TIME_SERIES_ERROR,
MAPPING_NOT_LAST_RULE_WITH_EMPTY_FILTER_ERROR,
}

private final Type type;
Expand All @@ -28,8 +33,4 @@ public DynamicSimulationException(Type type, String message) {
super(message);
this.type = type;
}

public Type getType() {
return type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,13 @@ protected ResponseEntity<Object> handleDynamicSimulationException(DynamicSimulat
PROVIDER_NOT_FOUND
-> ResponseEntity.status(HttpStatus.NOT_FOUND).body(exception.getMessage());
case URI_SYNTAX,
CREATE_MAPPING_SCRIPT_ERROR,
GET_DYNAMIC_MAPPING_ERROR,
EXPORT_PARAMETERS_ERROR,
CREATE_TIME_SERIES_ERROR,
DELETE_TIME_SERIES_ERROR
-> ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(exception.getMessage());
case MAPPING_NOT_PROVIDED
case MAPPING_NOT_PROVIDED,
MAPPING_NOT_LAST_RULE_WITH_EMPTY_FILTER_ERROR
-> ResponseEntity.status(HttpStatus.BAD_REQUEST).body(exception.getMessage());
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* 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/.
*/
package org.gridsuite.ds.server.dto.dynamicmapping;

import io.swagger.v3.oas.annotations.media.Schema;
import org.gridsuite.ds.server.dto.dynamicmapping.automata.Automaton;

import java.util.List;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Schema(description = "Mapping")
public record InputMapping(
@Schema(description = "Name")
String name,
@Schema(description = "Rules")
List<Rule> rules,
@Schema(description = "Automata")
List<Automaton> automata
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* 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/.
*/
package org.gridsuite.ds.server.dto.dynamicmapping;

import io.swagger.v3.oas.annotations.media.Schema;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Schema(description = "Parameter sets in *.par format")
public record ParameterFile(
@Schema(description = "Name of the parent mapping")
String mappingName,

@Schema(description = "Parameter file content in *.par format")
String fileContent
) {

}
36 changes: 36 additions & 0 deletions src/main/java/org/gridsuite/ds/server/dto/dynamicmapping/Rule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* 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/.
*/
package org.gridsuite.ds.server.dto.dynamicmapping;

import io.swagger.v3.oas.annotations.media.Schema;
import org.gridsuite.ds.server.utils.SetGroupType;
import org.gridsuite.filter.expertfilter.ExpertFilter;
import org.gridsuite.filter.utils.EquipmentType;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Schema(description = "Rule")
public record Rule(
@Schema(description = "Equipment type")
EquipmentType equipmentType,

@Schema(description = "Mapped Model Instance ID")
String mappedModel,

@Schema(description = "Mapped Parameter Set Group ID")
String setGroup,

@Schema(description = "Mapped Parameter Set Group Type")
SetGroupType groupType,

@Schema(description = "Filter")
ExpertFilter filter
) {

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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/.
*/
package org.gridsuite.ds.server.dto.dynamicmapping.automata;

import io.swagger.v3.oas.annotations.media.Schema;

import java.util.List;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Schema(description = "Automaton")
public record Automaton(
@Schema(description = "Mapped Model Instance ID")
String model,
@Schema(description = "Mapped Parameters Set Group ID")
String setGroup,
@Schema(description = "Properties of automaton model")
List<BasicProperty> properties
) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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/.
*/

package org.gridsuite.ds.server.dto.dynamicmapping.automata;

import io.swagger.v3.oas.annotations.media.Schema;
import org.gridsuite.ds.server.utils.PropertyType;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
@Schema(description = "Basic property")
public record BasicProperty(
@Schema(description = "Property name")
String name,
@Schema(description = "Property value in string representation")
String value,
@Schema(description = "Property type")
PropertyType type
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.computation.ComputationManager;
import com.powsybl.dynamicsimulation.*;
import com.powsybl.dynamicsimulation.groovy.*;
import com.powsybl.dynamicsimulation.groovy.CurveGroovyExtension;
import com.powsybl.dynamicsimulation.groovy.GroovyCurvesSupplier;
import com.powsybl.dynamicsimulation.groovy.GroovyExtension;
import com.powsybl.dynawaltz.DynaWaltzProvider;
import com.powsybl.dynawaltz.suppliers.dynamicmodels.DynamicModelConfig;
import com.powsybl.dynawaltz.suppliers.dynamicmodels.DynawoModelsSupplier;
import com.powsybl.dynawaltz.suppliers.events.DynawoEventModelsSupplier;
import com.powsybl.dynawaltz.suppliers.events.EventModelConfig;
import com.powsybl.iidm.network.Network;
import com.powsybl.iidm.network.VariantManagerConstants;
import com.powsybl.network.store.client.NetworkStoreService;
import com.powsybl.timeseries.IrregularTimeSeriesIndex;
import com.powsybl.timeseries.TimeSeries;
import org.apache.commons.collections4.CollectionUtils;
import com.powsybl.ws.commons.computation.service.*;
import org.apache.commons.collections4.CollectionUtils;
import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos;
import org.gridsuite.ds.server.dto.DynamicSimulationStatus;
import org.gridsuite.ds.server.dto.dynamicmapping.Script;
import org.gridsuite.ds.server.dto.dynamicmapping.InputMapping;
import org.gridsuite.ds.server.dto.dynamicmapping.ParameterFile;
import org.gridsuite.ds.server.service.client.dynamicmapping.DynamicMappingClient;
import org.gridsuite.ds.server.service.contexts.DynamicSimulationResultContext;
import org.gridsuite.ds.server.service.contexts.DynamicSimulationRunContext;
Expand Down Expand Up @@ -127,21 +134,24 @@ public void preRun(DynamicSimulationRunContext runContext) {
super.preRun(runContext);
DynamicSimulationParametersInfos parametersInfos = runContext.getParameters();

// get script and parameters file from dynamic mapping server
Script scriptObj = dynamicMappingClient.createFromMapping(runContext.getMapping());
// get parameters file from dynamic mapping server
ParameterFile parameterFile = dynamicMappingClient.exportParameters(runContext.getMapping());

// get all dynamic simulation parameters
String parametersFile = scriptObj.getParametersFile();
String parameterFileContent = parameterFile.fileContent();
DynamicSimulationParameters parameters = parametersService.getDynamicSimulationParameters(
parametersFile.getBytes(StandardCharsets.UTF_8), runContext.getProvider(), parametersInfos);
parameterFileContent.getBytes(StandardCharsets.UTF_8), runContext.getProvider(), parametersInfos);

// get mapping then generate dynamic model configs
InputMapping inputMapping = dynamicMappingClient.getMapping(runContext.getMapping());
List<DynamicModelConfig> dynamicModel = parametersService.getDynamicModel(inputMapping, runContext.getNetwork());
List<EventModelConfig > eventModel = parametersService.getEventModel(parametersInfos.getEvents());

// set start and stop times
parameters.setStartTime(parametersInfos.getStartTime().intValue()); // TODO remove intValue() when correct startTime to double in powsybl
parameters.setStopTime(parametersInfos.getStopTime().intValue()); // TODO remove intValue() when correct stopTime to double in powsybl

// groovy scripts
String dynamicModel = scriptObj.getScript();
String eventModel = parametersService.getEventModel(parametersInfos.getEvents());
String curveModel = parametersService.getCurveModel(parametersInfos.getCurves());

// enrich runContext
Expand All @@ -154,15 +164,9 @@ public void preRun(DynamicSimulationRunContext runContext) {
@Override
public CompletableFuture<DynamicSimulationResult> getCompletableFuture(DynamicSimulationRunContext runContext, String provider, UUID resultUuid) {

List<DynamicModelGroovyExtension> dynamicModelExtensions = GroovyExtension.find(DynamicModelGroovyExtension.class, DynaWaltzProvider.NAME);
DynamicModelsSupplier dynamicModelsSupplier = new GroovyDynamicModelsSupplier(
new ByteArrayInputStream(runContext.getDynamicModelContent().getBytes()), dynamicModelExtensions
);
DynamicModelsSupplier dynamicModelsSupplier = new DynawoModelsSupplier(runContext.getDynamicModelContent());

List<EventModelGroovyExtension> eventModelExtensions = GroovyExtension.find(EventModelGroovyExtension.class, DynaWaltzProvider.NAME);
EventModelsSupplier eventModelsSupplier = new GroovyEventModelsSupplier(
new ByteArrayInputStream(runContext.getEventModelContent().getBytes()), eventModelExtensions
);
EventModelsSupplier eventModelsSupplier = new DynawoEventModelsSupplier(runContext.getEventModelContent());

List<CurveGroovyExtension> curveExtensions = GroovyExtension.find(CurveGroovyExtension.class, DynaWaltzProvider.NAME);
CurvesSupplier curvesSupplier = new GroovyCurvesSupplier(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public interface RestClient {
String DELIMITER = "/";
String URL_DELIMITER = "/";

RestTemplate getRestTemplate();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,21 @@
*/
package org.gridsuite.ds.server.service.client.dynamicmapping;

import org.gridsuite.ds.server.dto.dynamicmapping.Script;
import org.gridsuite.ds.server.dto.dynamicmapping.InputMapping;
import org.gridsuite.ds.server.dto.dynamicmapping.ParameterFile;

import static org.gridsuite.ds.server.service.client.RestClient.DELIMITER;
import static org.gridsuite.ds.server.service.client.RestClient.URL_DELIMITER;

/**
* @author Thang PHAM <quyet-thang.pham at rte-france.com>
*/
public interface DynamicMappingClient {
String API_VERSION = "";
String DYNAMIC_MAPPING_SCRIPT_BASE_END_POINT = "scripts";
String DYNAMIC_MAPPING_SCRIPT_CREATE_END_POINT = DYNAMIC_MAPPING_SCRIPT_BASE_END_POINT + DELIMITER + "from";
String DYNAMIC_MAPPING_PARAMETERS_BASE_ENDPOINT = "parameters";
String DYNAMIC_MAPPING_PARAMETERS_EXPORT_ENDPOINT = DYNAMIC_MAPPING_PARAMETERS_BASE_ENDPOINT + URL_DELIMITER + "export";
String DYNAMIC_MAPPING_MAPPINGS_BASE_ENDPOINT = "mappings";

Script createFromMapping(String mappingName);
ParameterFile exportParameters(String mappingName);

InputMapping getMapping(String mappingName);
}
Loading

0 comments on commit b5e40d8

Please sign in to comment.