Skip to content

Commit

Permalink
Map Fsv to dataframes
Browse files Browse the repository at this point in the history
Signed-off-by: lisrte <laurent.issertial@rte-france.com>
  • Loading branch information
Lisrte committed Dec 12, 2024
1 parent 2544b7e commit e682730
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 16 deletions.
4 changes: 4 additions & 0 deletions cpp/powsybl-cpp/powsybl-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1367,6 +1367,10 @@ std::vector<std::string> getAllDynamicCurvesIds(JavaHandle resultHandle) {
return vector.get();
}

SeriesArray* getFinalStateValues(JavaHandle resultHandle) {
return new SeriesArray(PowsyblCaller::get()->callJava<array*>(::getFinalStateValues, resultHandle));
}

std::vector<std::string> getSupportedModels(DynamicMappingType mappingType) {
ToStringVector vector(PowsyblCaller::get()->callJava<array*>(::getSupportedModels, mappingType));
return vector.get();
Expand Down
1 change: 1 addition & 0 deletions cpp/powsybl-cpp/powsybl-cpp.h
Original file line number Diff line number Diff line change
Expand Up @@ -784,6 +784,7 @@ std::vector<std::string> getSupportedModels(DynamicMappingType mappingType);
std::string getDynamicSimulationResultsStatus(JavaHandle dynamicSimulationResultsHandle);
SeriesArray* getDynamicCurve(JavaHandle resultHandle, std::string curveName);
std::vector<std::string> getAllDynamicCurvesIds(JavaHandle resultHandle);
SeriesArray* getFinalStateValues(JavaHandle resultHandle);

//=======END OF dynamic modeling for dynawo package==========

Expand Down
1 change: 1 addition & 0 deletions cpp/pypowsybl-cpp/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ void dynamicSimulationBindings(py::module_& m) {
m.def("get_dynamic_simulation_results_status", &pypowsybl::getDynamicSimulationResultsStatus, py::arg("result_handle"));
m.def("get_dynamic_curve", &pypowsybl::getDynamicCurve, py::arg("report_handle"), py::arg("curve_name"));
m.def("get_all_dynamic_curves_ids", &pypowsybl::getAllDynamicCurvesIds, py::arg("report_handle"));
m.def("get_final_state_values", &pypowsybl::getFinalStateValues, py::arg("result_handle"));
}

void voltageInitializerBinding(py::module_& m) {
Expand Down
5 changes: 1 addition & 4 deletions integration_tests/test_dynawo.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,9 @@ def test_simulation():
sim = dyn.Simulation()
res = sim.run(network, model_mapping, event_mapping, variables_mapping, 0, 100, report_node)

print(report_node)
print(res.curves())
assert report_node
assert 'Ok' == res.status()
assert 'BBM_GEN6_generator_PGen' in res.curves()
assert 'BBM_GEN6_generator_QGen' in res.curves()
assert 'BBM_GEN6_generator_UStatorPu' in res.curves()

test_simulation()
assert res.final_state_values().loc['NETWORK_B3_Upu_value'].empty == False
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,29 @@
import com.powsybl.timeseries.DoublePoint;
import com.powsybl.timeseries.TimeSeries;

import java.util.Map;

/**
* @author Nicolas Pierre {@literal <nicolas.pierre@artelys.com>}
*/
public final class CurvesSeries {
public final class OutputVariablesSeries {

private CurvesSeries() {
private OutputVariablesSeries() {
}

public static DataframeMapper<TimeSeries<DoublePoint, ?>, Void> curvesDataFrameMapper(String colName) {
DataframeMapperBuilder<TimeSeries<DoublePoint, ?>, DoublePoint, Void> df = new DataframeMapperBuilder<>();
df.itemsStreamProvider(TimeSeries::stream)
return new DataframeMapperBuilder<TimeSeries<DoublePoint, ?>, DoublePoint, Void>()
.itemsStreamProvider(TimeSeries::stream)
.intsIndex("timestamp", pt -> (int) (pt.getTime() % Integer.MAX_VALUE))
.doubles(colName, DoublePoint::getValue);
return df.build();
.doubles(colName, DoublePoint::getValue)
.build();
}

public static DataframeMapper<Map<String, Double>, Void> fsvDataFrameMapper() {
return new DataframeMapperBuilder<Map<String, Double>, Map.Entry<String, Double>, Void>()
.itemsStreamProvider(m -> m.entrySet().stream())
.stringsIndex("variables", Map.Entry::getKey)
.doubles("values", Map.Entry::getValue)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import com.powsybl.commons.report.ReportNode;
import com.powsybl.dataframe.SeriesMetadata;
import com.powsybl.dataframe.dynamic.CurvesSeries;
import com.powsybl.dataframe.dynamic.OutputVariablesSeries;
import com.powsybl.python.network.Dataframes;
import com.powsybl.python.report.ReportCUtils;
import com.powsybl.timeseries.DoublePoint;
Expand Down Expand Up @@ -217,7 +217,7 @@ public static ArrayPointer<SeriesPointer> getDynamicCurve(IsolateThread thread,
DynamicSimulationResult result = ObjectHandles.getGlobal().get(resultHandle);
String curveName = CTypeUtil.toString(curveNamePtr);
TimeSeries<DoublePoint, ?> curve = result.getCurve(curveName);
return Dataframes.createCDataframe(CurvesSeries.curvesDataFrameMapper(curveName), curve);
return Dataframes.createCDataframe(OutputVariablesSeries.curvesDataFrameMapper(curveName), curve);
});
}

Expand All @@ -230,4 +230,11 @@ public static ArrayPointer<CCharPointerPointer> getAllDynamicCurvesIds(IsolateTh
return Util.createCharPtrArray(new ArrayList<>(result.getCurves().keySet()));
});
}

@CEntryPoint(name = "getFinalStateValues")
public static ArrayPointer<SeriesPointer> getFinalStateValues(IsolateThread thread, ObjectHandle resultHandle,
ExceptionHandlerPointer exceptionHandlerPtr) {
DynamicSimulationResult result = ObjectHandles.getGlobal().get(resultHandle);
return Dataframes.createCDataframe(OutputVariablesSeries.fsvDataFrameMapper(), result.getFinalStateValues());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@
*/
package com.powsybl.dataframe.dynamic.adders;

import com.powsybl.dataframe.impl.Series;
import com.powsybl.dynamicsimulation.OutputVariable;
import com.powsybl.iidm.network.test.EurostagTutorialExample1Factory;
import com.powsybl.python.dynamic.PythonOutputVariablesSupplier;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.Map;

import static com.powsybl.dataframe.dynamic.OutputVariablesSeries.fsvDataFrameMapper;
import static com.powsybl.dynamicsimulation.OutputVariable.OutputType.*;
import static com.powsybl.python.network.Dataframes.createSeries;
import static org.assertj.core.api.Assertions.assertThat;

/**
Expand All @@ -40,4 +44,16 @@ void testPythonSupplier() {
.hasFieldOrPropertyWithValue("outputType", FINAL_STATE)
);
}

@Test
void testFsvDataframesMapper() {
Map<String, Double> fsv = Map.of("LOAD_load_PPu", 22.1, "GEN_Upu_value", 45.8);
List<Series> series = createSeries(fsvDataFrameMapper(), fsv);
assertThat(series)
.extracting(Series::getName)
.containsExactly("variables", "values");
assertThat(series).satisfiesExactly(
col1 -> assertThat(col1.getStrings()).containsExactly("LOAD_load_PPu", "GEN_Upu_value"),
col2 -> assertThat(col2.getDoubles()).containsExactly(22.1, 45.8));
}
}
3 changes: 2 additions & 1 deletion pypowsybl/_pypowsybl.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -909,13 +909,14 @@ def run_dynamic_model(dynamic_model: JavaHandle, network: JavaHandle, dynamic_ma
def add_all_dynamic_mappings(dynamic_mapping_handle: JavaHandle, mapping_type: DynamicMappingType, dataframes: List[Optional[Dataframe]]) -> None: ...
def get_dynamic_mappings_meta_data(mapping_type: DynamicMappingType) -> List[List[SeriesMetadata]]: ...
def get_supported_models(mapping_type: DynamicMappingType) -> List[str]: ...
def add_output_variables(output_variables_handle: JavaHandle, dynamic_id: str, variables: str, is_dynamic: bool, output_variable_type: OutputVariableType) -> None: ...
def add_output_variables(output_variables_handle: JavaHandle, dynamic_id: str, variables: List[str], is_dynamic: bool, output_variable_type: OutputVariableType) -> None: ...
def add_all_event_mappings(event_mapping_handle: JavaHandle, mapping_type: EventMappingType, mapping_df: Dataframe) -> None: ...
def get_event_mappings_meta_data(mapping_type: EventMappingType) -> List[SeriesMetadata]: ...
def set_powsybl_config_location(absolute_path_to_config:str, config_file_name: str) -> None: ...
def get_dynamic_simulation_results_status(result_handle: JavaHandle) -> str: ...
def get_dynamic_curve(report_handle: JavaHandle, curve_name: str) -> SeriesArray: ...
def get_all_dynamic_curves_ids(report_handle: JavaHandle) -> List[str]: ...
def get_final_state_values(result_handle: JavaHandle) -> SeriesArray: ...
def remove_elements_modification(network: JavaHandle, connectable_ids: List[str], dataframe: Optional[Dataframe], remove_modification_type: RemoveModificationType, raise_exception: Optional[bool], report_node: Optional[JavaHandle]) -> None: ...
def get_network_modification_metadata(network_modification_type: NetworkModificationType) -> List[SeriesMetadata]: ...
def get_network_modification_metadata_with_element_type(network_modification_type: NetworkModificationType, element_type: ElementType) -> List[List[SeriesMetadata]]: ...
Expand Down
6 changes: 3 additions & 3 deletions pypowsybl/dynamic/impl/simulation_result.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def __init__(self, handle: _pp.JavaHandle) -> None:
self._handle = handle
self._status = _pp.get_dynamic_simulation_results_status(self._handle)
self._curves = self._get_all_curves()
self._fsv = _pp.get_dynamic_simulation_results_status(self._handle)
self._fsv = create_data_frame_from_series_array(_pp.get_final_state_values(self._handle))

def status(self) -> str:
"""
Expand All @@ -41,5 +41,5 @@ def _get_all_curves(self) -> pd.DataFrame:
return pd.concat(df_curves, axis=1).ffill() if df_curves else pd.DataFrame()

def final_state_values(self) -> pd.DataFrame:
"""Dataframe of the final state values results, columns are the fsv names and rows are timestep"""
return self._fsv
"""Dataframe of the final state values results, first column is the fsv names, second one the final state values"""
return self._fsv

0 comments on commit e682730

Please sign in to comment.