Skip to content

Commit

Permalink
move per unitage to java (#718)
Browse files Browse the repository at this point in the history
Signed-off-by: Etienne LESOT <[email protected]>
  • Loading branch information
EtienneLt authored May 2, 2024
1 parent 3e8d9b7 commit bffd5f8
Show file tree
Hide file tree
Showing 41 changed files with 1,384 additions and 422 deletions.
4 changes: 2 additions & 2 deletions cpp/src/bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -741,7 +741,7 @@ PYBIND11_MODULE(_pypowsybl, m) {
py::arg("element_type"));

m.def("create_network_elements_series_array", &pypowsybl::createNetworkElementsSeriesArray, "Create a network elements series array for a given element type",
py::call_guard<py::gil_scoped_release>(), py::arg("network"), py::arg("element_type"), py::arg("filter_attributes_type"), py::arg("attributes"), py::arg("array"));
py::call_guard<py::gil_scoped_release>(), py::arg("network"), py::arg("element_type"), py::arg("filter_attributes_type"), py::arg("attributes"), py::arg("array"), py::arg("per_unit"), py::arg("nominal_apparent_power"));

m.def("create_network_elements_extension_series_array", &pypowsybl::createNetworkElementsExtensionSeriesArray, "Create a network elements extensions series array for a given extension name",
py::call_guard<py::gil_scoped_release>(), py::arg("network"), py::arg("extension_name"), py::arg("table_name"));
Expand All @@ -751,7 +751,7 @@ PYBIND11_MODULE(_pypowsybl, m) {
m.def("get_extensions_information", &pypowsybl::getExtensionsInformation, "get more information about all extensions");

m.def("update_network_elements_with_series", pypowsybl::updateNetworkElementsWithSeries, "Update network elements for a given element type with a series",
py::call_guard<py::gil_scoped_release>(), py::arg("network"), py::arg("dataframe"), py::arg("element_type"));
py::call_guard<py::gil_scoped_release>(), py::arg("network"), py::arg("dataframe"), py::arg("element_type"), py::arg("per_unit"), py::arg("nominal_apparent_power"));

m.def("create_dataframe", ::createDataframe, "create dataframe to update or create new elements", py::arg("columns_values"), py::arg("columns_names"), py::arg("columns_types"),
py::arg("is_index"));
Expand Down
8 changes: 4 additions & 4 deletions cpp/src/pypowsybl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,9 +986,9 @@ matrix* getReferenceMatrix(const JavaHandle& sensitivityAnalysisResultContext, c
(char*) matrixId.c_str(), (char*) contingencyId.c_str());
}

SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe) {
SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe, bool perUnit, double nominalApparentPower) {
ToCharPtrPtr attributesPtr(attributes);
return new SeriesArray(callJava<array*>(::createNetworkElementsSeriesArray, network, elementType, filterAttributesType, attributesPtr.get(), attributes.size(), dataframe));
return new SeriesArray(callJava<array*>(::createNetworkElementsSeriesArray, network, elementType, filterAttributesType, attributesPtr.get(), attributes.size(), dataframe, perUnit, nominalApparentPower));
}

SeriesArray* createNetworkElementsExtensionSeriesArray(const JavaHandle& network, const std::string& extensionName, const std::string& tableName) {
Expand Down Expand Up @@ -1091,8 +1091,8 @@ SeriesArray* getBusBreakerViewElements(const JavaHandle& network, std::string& v
return new SeriesArray(callJava<array*>(::getBusBreakerViewElements, network, (char*) voltageLevel.c_str()));
}

void updateNetworkElementsWithSeries(pypowsybl::JavaHandle network, dataframe* dataframe, element_type elementType) {
pypowsybl::callJava<>(::updateNetworkElementsWithSeries, network, elementType, dataframe);
void updateNetworkElementsWithSeries(pypowsybl::JavaHandle network, dataframe* dataframe, element_type elementType, bool perUnit, double nominalApparentPower) {
pypowsybl::callJava<>(::updateNetworkElementsWithSeries, network, elementType, dataframe, perUnit, nominalApparentPower);
}

std::vector<SeriesMetadata> convertDataframeMetadata(dataframe_metadata* dataframeMetadata) {
Expand Down
4 changes: 2 additions & 2 deletions cpp/src/pypowsybl.h
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ matrix* getSensitivityMatrix(const JavaHandle& sensitivityAnalysisResultContext,

matrix* getReferenceMatrix(const JavaHandle& sensitivityAnalysisResultContext, const std::string& matrixId, const std::string& contingencyId);

SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe);
SeriesArray* createNetworkElementsSeriesArray(const JavaHandle& network, element_type elementType, filter_attributes_type filterAttributesType, const std::vector<std::string>& attributes, dataframe* dataframe, bool perUnit, double nominalApparentPower);

void removeNetworkElements(const JavaHandle& network, const std::vector<std::string>& elementIds);

Expand All @@ -458,7 +458,7 @@ std::vector<std::string> getExtensionsNames();

SeriesArray* getExtensionsInformation();

void updateNetworkElementsWithSeries(pypowsybl::JavaHandle network, dataframe* dataframe, element_type elementType);
void updateNetworkElementsWithSeries(pypowsybl::JavaHandle network, dataframe* dataframe, element_type elementType, bool perUnit, double nominalApparentPower);

std::string getWorkingVariantId(const JavaHandle& network);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.dataframe;

import com.powsybl.commons.PowsyblException;
import com.powsybl.dataframe.network.DataframeContext;
import com.powsybl.dataframe.update.DoubleSeries;
import com.powsybl.dataframe.update.IntSeries;
import com.powsybl.dataframe.update.StringSeries;
Expand All @@ -30,7 +31,7 @@ public abstract class AbstractDataframeMapper<T, U> implements DataframeMapper<T

public AbstractDataframeMapper(List<SeriesMapper<U>> seriesMappers) {
this.seriesMappers = seriesMappers.stream()
.collect(toImmutableMap(mapper -> mapper.getMetadata().getName(), Function.identity()));
.collect(toImmutableMap(mapper -> mapper.getMetadata().getName(), Function.identity()));
}

@Override
Expand All @@ -48,15 +49,16 @@ public SeriesMetadata getSeriesMetadata(String seriesName) {
}

@Override
public void createDataframe(T object, DataframeHandler dataframeHandler, DataframeFilter dataframeFilter) {
public void createDataframe(T object, DataframeHandler dataframeHandler, DataframeFilter dataframeFilter, DataframeContext dataframeContext) {
Collection<SeriesMapper<U>> mappers = getSeriesMappers(dataframeFilter);
dataframeHandler.allocate(mappers.size());
List<U> items = getItems(object);
mappers.stream().forEach(mapper -> mapper.createSeries(items, dataframeHandler));
mappers.forEach(mapper -> mapper.createSeries(items, dataframeHandler, dataframeContext));
}

interface ColumnUpdater<U> {
void update(int index, U object);

void update(int index, U object, DataframeContext context);
}

private static final class IntColumnUpdater<U> implements ColumnUpdater<U> {
Expand All @@ -69,7 +71,7 @@ private IntColumnUpdater(IntSeries values, SeriesMapper<U> mapper) {
}

@Override
public void update(int index, U object) {
public void update(int index, U object, DataframeContext context) {
mapper.updateInt(object, values.get(index));
}
}
Expand All @@ -84,8 +86,8 @@ private DoubleColumnUpdater(DoubleSeries values, SeriesMapper<U> mapper) {
}

@Override
public void update(int index, U object) {
mapper.updateDouble(object, values.get(index));
public void update(int index, U object, DataframeContext context) {
mapper.updateDouble(object, values.get(index), context);
}
}

Expand All @@ -99,13 +101,13 @@ private StringColumnUpdater(StringSeries values, SeriesMapper<U> mapper) {
}

@Override
public void update(int index, U object) {
public void update(int index, U object, DataframeContext context) {
mapper.updateString(object, values.get(index));
}
}

@Override
public void updateSeries(T object, UpdatingDataframe updatingDataframe) {
public void updateSeries(T object, UpdatingDataframe updatingDataframe, DataframeContext context) {

//Setup links to minimize searches on column names
List<ColumnUpdater<U>> updaters = new ArrayList<>();
Expand All @@ -127,7 +129,7 @@ public void updateSeries(T object, UpdatingDataframe updatingDataframe) {
for (int i = 0; i < updatingDataframe.getRowCount(); i++) {
U item = getItem(object, updatingDataframe, i);
int itemIndex = i;
updaters.forEach(updater -> updater.update(itemIndex, item));
updaters.forEach(updater -> updater.update(itemIndex, item, context));
}
}

Expand All @@ -139,15 +141,15 @@ public boolean isSeriesMetaDataExists(String seriesName) {
public Collection<SeriesMapper<U>> getSeriesMappers(DataframeFilter dataframeFilter) {
Collection<SeriesMapper<U>> mappers = seriesMappers.values();
return mappers.stream()
.filter(mapper -> filterMapper(mapper, dataframeFilter))
.collect(Collectors.toList());
.filter(mapper -> filterMapper(mapper, dataframeFilter))
.collect(Collectors.toList());
}

protected boolean filterMapper(SeriesMapper<U> mapper, DataframeFilter dataframeFilter) {
return switch (dataframeFilter.getAttributeFilterType()) {
case DEFAULT_ATTRIBUTES -> mapper.getMetadata().isDefaultAttribute() || mapper.getMetadata().isIndex();
case INPUT_ATTRIBUTES ->
dataframeFilter.getInputAttributes().contains(mapper.getMetadata().getName()) || mapper.getMetadata().isIndex();
dataframeFilter.getInputAttributes().contains(mapper.getMetadata().getName()) || mapper.getMetadata().isIndex();
case ALL_ATTRIBUTES -> true;
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package com.powsybl.dataframe;

import com.powsybl.commons.PowsyblException;
import com.powsybl.dataframe.network.DataframeContext;
import com.powsybl.dataframe.update.UpdatingDataframe;

import java.util.ArrayList;
Expand Down Expand Up @@ -65,11 +66,20 @@ public B doubles(String name, ToDoubleFunction<U> value, DoubleSeriesMapper.Doub
return doubles(name, value, updater, true);
}

public B doubles(String name, ToDoubleFunction<U> value, DoubleSeriesMapper.DoubleUpdater<U> updater, boolean defaultAttribute) {
public B doubles(String name, ToDoubleBiFunction<U, DataframeContext> value, DoubleSeriesMapper.DoubleUpdater<U> updater, boolean defaultAttribute) {
series.add(new DoubleSeriesMapper<>(name, value, updater, defaultAttribute));
return (B) this;
}

public B doubles(String name, ToDoubleBiFunction<U, DataframeContext> value, DoubleSeriesMapper.DoubleUpdater<U> updater) {
series.add(new DoubleSeriesMapper<>(name, value, updater, true));
return (B) this;
}

public B doubles(String name, ToDoubleFunction<U> value, DoubleSeriesMapper.DoubleUpdater<U> updater, boolean defaultAttribute) {
return doubles(name, (u, pu) -> value.applyAsDouble(u), updater, defaultAttribute);
}

public B doubles(Map<String, ToDoubleFunction<U>> nameValuesMap) {
nameValuesMap.forEach(this::doubles);
return (B) this;
Expand All @@ -79,6 +89,14 @@ public B doubles(String name, ToDoubleFunction<U> value) {
return doubles(name, value, null, true);
}

public B doubles(String name, ToDoubleBiFunction<U, DataframeContext> value) {
return doubles(name, value, (DoubleSeriesMapper.DoubleUpdater<U>) null, true);
}

public B doubles(String name, ToDoubleBiFunction<U, DataframeContext> value, boolean defaultAttribute) {
return doubles(name, value, (DoubleSeriesMapper.DoubleUpdater<U>) null, defaultAttribute);
}

public B doubles(String name, ToDoubleFunction<U> value, boolean defaultAttribute) {
return doubles(name, value, null, defaultAttribute);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;

import java.util.List;
import java.util.function.Predicate;

Expand Down Expand Up @@ -39,7 +41,7 @@ public SeriesMetadata getMetadata() {
}

@Override
public void createSeries(List<T> items, DataframeHandler handler) {
public void createSeries(List<T> items, DataframeHandler handler, DataframeContext dataframeContext) {
DataframeHandler.BooleanSeriesWriter writer = handler.newBooleanSeries(metadata.getName(), items.size());
for (int i = 0; i < items.size(); i++) {
writer.set(i, value.test(items.get(i)));
Expand Down
5 changes: 3 additions & 2 deletions java/src/main/java/com/powsybl/dataframe/DataframeMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;
import com.powsybl.dataframe.update.UpdatingDataframe;

import java.util.List;
Expand All @@ -26,7 +27,7 @@ public interface DataframeMapper<T> {
* Provides dataframe data to the handler, which is responsible to
* format it as needed.
*/
void createDataframe(T object, DataframeHandler dataframeHandler, DataframeFilter dataframeFilter);
void createDataframe(T object, DataframeHandler dataframeHandler, DataframeFilter dataframeFilter, DataframeContext dataframeContext);

List<SeriesMetadata> getSeriesMetadata();

Expand All @@ -35,7 +36,7 @@ public interface DataframeMapper<T> {
/**
* Updates object data with the provided series.
*/
void updateSeries(T object, UpdatingDataframe updatingDataframe);
void updateSeries(T object, UpdatingDataframe updatingDataframe, DataframeContext context);

boolean isSeriesMetaDataExists(String seriesName);
}
20 changes: 11 additions & 9 deletions java/src/main/java/com/powsybl/dataframe/DoubleSeriesMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;

import java.util.List;
import java.util.function.ToDoubleFunction;
import java.util.function.ToDoubleBiFunction;

/**
* @author Sylvain Leclerc <sylvain.leclerc at rte-france.com>
Expand All @@ -16,18 +18,18 @@ public class DoubleSeriesMapper<T> implements SeriesMapper<T> {

private final SeriesMetadata metadata;
private final DoubleUpdater<T> updater;
private final ToDoubleFunction<T> value;
private final ToDoubleBiFunction<T, DataframeContext> value;

@FunctionalInterface
public interface DoubleUpdater<U> {
void update(U object, double value);
void update(U object, double value, DataframeContext context);
}

public DoubleSeriesMapper(String name, ToDoubleFunction<T> value) {
public DoubleSeriesMapper(String name, ToDoubleBiFunction<T, DataframeContext> value) {
this(name, value, null, true);
}

public DoubleSeriesMapper(String name, ToDoubleFunction<T> value, DoubleUpdater<T> updater, boolean defaultAttribute) {
public DoubleSeriesMapper(String name, ToDoubleBiFunction<T, DataframeContext> value, DoubleUpdater<T> updater, boolean defaultAttribute) {
this.metadata = new SeriesMetadata(false, name, updater != null, SeriesDataType.DOUBLE, defaultAttribute);
this.updater = updater;
this.value = value;
Expand All @@ -39,19 +41,19 @@ public SeriesMetadata getMetadata() {
}

@Override
public void createSeries(List<T> items, DataframeHandler factory) {
public void createSeries(List<T> items, DataframeHandler factory, DataframeContext dataframeContext) {

DataframeHandler.DoubleSeriesWriter writer = factory.newDoubleSeries(metadata.getName(), items.size());
for (int i = 0; i < items.size(); i++) {
writer.set(i, value.applyAsDouble(items.get(i)));
writer.set(i, value.applyAsDouble(items.get(i), dataframeContext));
}
}

@Override
public void updateDouble(T object, double value) {
public void updateDouble(T object, double value, DataframeContext context) {
if (updater == null) {
throw new UnsupportedOperationException("Series '" + getMetadata().getName() + "' is not modifiable.");
}
updater.update(object, value);
updater.update(object, value, context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;

import java.util.List;
import java.util.Objects;
import java.util.function.BiConsumer;
Expand Down Expand Up @@ -38,7 +40,7 @@ public SeriesMetadata getMetadata() {
}

@Override
public void createSeries(List<T> items, DataframeHandler factory) {
public void createSeries(List<T> items, DataframeHandler factory, DataframeContext dataframeContext) {
DataframeHandler.StringSeriesWriter writer = factory.newStringSeries(metadata.getName(), items.size());
for (int i = 0; i < items.size(); i++) {
writer.set(i, Objects.toString(value.apply(items.get(i)), ""));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;

import java.util.List;
import java.util.function.ToIntFunction;

Expand Down Expand Up @@ -51,7 +53,7 @@ public SeriesMetadata getMetadata() {
}

@Override
public void createSeries(List<T> items, DataframeHandler handler) {
public void createSeries(List<T> items, DataframeHandler handler, DataframeContext dataframeContext) {
boolean index = metadata.isIndex();
String name = metadata.getName();
DataframeHandler.IntSeriesWriter writer = index ? handler.newIntIndex(name, items.size()) : handler.newIntSeries(name, items.size());
Expand Down
18 changes: 18 additions & 0 deletions java/src/main/java/com/powsybl/dataframe/MappingUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
*/
package com.powsybl.dataframe;

import com.powsybl.dataframe.network.DataframeContext;
import com.powsybl.dataframe.network.PerUnitUtil;

import java.util.function.Function;
import java.util.function.ToDoubleBiFunction;
import java.util.function.ToDoubleFunction;
import java.util.function.ToIntFunction;

Expand Down Expand Up @@ -40,6 +44,20 @@ public static <T, U> ToDoubleFunction<T> ifExistsDouble(Function<T, U> objectGet
return ifExistsDouble(objectGetter, valueGetter, Double.NaN);
}

public static <T, U> ToDoubleBiFunction<T, DataframeContext> ifExistsDoublePerUnitPQ(Function<T, U> objectGetter, ToDoubleFunction<U> valueGetter) {
return (item, context) -> {
U object = objectGetter.apply(item);
return object != null ? PerUnitUtil.perUnitPQ(context, valueGetter.applyAsDouble(object)) : Double.NaN;
};
}

public static <T, U> ToDoubleBiFunction<T, DataframeContext> ifExistsDoublePerUnitAngle(Function<T, U> objectGetter, ToDoubleFunction<U> valueGetter) {
return (item, context) -> {
U object = objectGetter.apply(item);
return object != null ? PerUnitUtil.perUnitAngle(context, valueGetter.applyAsDouble(object)) : Double.NaN;
};
}

/**
* Maps T to another object U and returns the specified undefined value if U is {@code null}
*/
Expand Down
Loading

0 comments on commit bffd5f8

Please sign in to comment.