From 054ae0cb64336ad1409905fccb2aae0fc672111c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Franz=20P=C3=B6schel?= Date: Mon, 4 Nov 2024 16:53:22 +0100 Subject: [PATCH] Better function argument types --- include/openPMD/Mesh.hpp | 30 ++++++++++++++++++++--- include/openPMD/Record.hpp | 1 + include/openPMD/UnitDimension.hpp | 4 ++-- src/Mesh.cpp | 22 +++++++++++++---- src/Record.cpp | 5 ++++ src/UnitDimension.cpp | 13 +++++----- src/binding/python/Mesh.cpp | 36 ++++++++++++++++++++++------ src/binding/python/Record.cpp | 17 +++++++++++-- src/binding/python/UnitDimension.cpp | 10 ++++++-- 9 files changed, 111 insertions(+), 27 deletions(-) diff --git a/include/openPMD/Mesh.hpp b/include/openPMD/Mesh.hpp index e6d70a5e7e..77ef8b2886 100644 --- a/include/openPMD/Mesh.hpp +++ b/include/openPMD/Mesh.hpp @@ -211,7 +211,7 @@ class Mesh : public BaseRecord * * @return Reference to modified mesh. */ - Mesh &setGridUnitSI(std::vector gridUnitSI); + Mesh &setGridUnitSI(std::vector const &gridUnitSI); /** * @return A vector of the gridUnitSI per grid axis in the order of @@ -235,7 +235,7 @@ class Mesh : public BaseRecord * * @return Reference to modified mesh. */ - Mesh &setGridUnitSIPerDimension(std::vector gridUnitSI); + Mesh &setGridUnitSIPerDimension(std::vector const &gridUnitSI); /** Set the powers of the 7 base measures characterizing the record's unit * in SI. @@ -246,11 +246,20 @@ class Mesh : public BaseRecord */ Mesh &setUnitDimension(unit_representations::AsMap const &unitDimension); + /** Set the powers of the 7 base measures characterizing the record's unit + * in SI. + * + * @param unitDimension array containing seven doubles, each + * representing the power of the particular base in order. + * @return Reference to modified mesh. + */ + Mesh &setUnitDimension(unit_representations::AsArray const &unitDimension); + /** * @brief Set the unitDimension for each axis of the current grid. * * @param gridUnitDimension A vector of the unitDimensions for each - * axis of the grid in the order of the axisLabels. + * axis of the grid in the order of the axisLabels, in dict representation. * Behavior note: This is an updating method, meaning that an SI unit that * has been defined before and is in the next call not explicitly set @@ -261,6 +270,21 @@ class Mesh : public BaseRecord Mesh & setGridUnitDimension(unit_representations::AsMaps const &gridUnitDimension); + /** + * @brief Set the unitDimension for each axis of the current grid. + * + * @param gridUnitDimension A vector of the unitDimensions for each + * axis of the grid in the order of the axisLabels, in array representation. + + * Behavior note: This is an updating method, meaning that an SI unit that + * has been defined before and is in the next call not explicitly set + * in the `std::map` will keep its previous value. + * + * @return Reference to modified mesh. + */ + Mesh &setGridUnitDimension( + unit_representations::AsArrays const &gridUnitDimension); + /** * @brief Return the physical dimensions of the mesh axes. diff --git a/include/openPMD/Record.hpp b/include/openPMD/Record.hpp index 957536bf2c..791c4c15f8 100644 --- a/include/openPMD/Record.hpp +++ b/include/openPMD/Record.hpp @@ -41,6 +41,7 @@ class Record : public BaseRecord ~Record() override = default; Record &setUnitDimension(unit_representations::AsMap const &); + Record &setUnitDimension(unit_representations::AsArray const &); template T timeOffset() const; diff --git a/include/openPMD/UnitDimension.hpp b/include/openPMD/UnitDimension.hpp index e1773fc7a6..480901c0ca 100644 --- a/include/openPMD/UnitDimension.hpp +++ b/include/openPMD/UnitDimension.hpp @@ -54,10 +54,10 @@ namespace unit_representations using AsArrays = std::vector; auto asArray(AsMap const &) -> AsArray; - auto asMap(AsArray const &) -> AsMap; + auto asMap(AsArray const &, bool skip_zeros = true) -> AsMap; auto asArrays(AsMaps const &) -> AsArrays; - auto asMaps(AsArrays const &) -> AsMaps; + auto asMaps(AsArrays const &, bool skip_zeros = true) -> AsMaps; namespace auxiliary { diff --git a/src/Mesh.cpp b/src/Mesh.cpp index 5f7c5c34ef..c5cdefe483 100644 --- a/src/Mesh.cpp +++ b/src/Mesh.cpp @@ -184,7 +184,6 @@ double Mesh::gridUnitSI() const Mesh &Mesh::setGridUnitSI(double gusi) { - setAttribute("gridUnitSI", gusi); if (auto standard = IOHandler()->m_standard; standard >= OpenpmdStandard::v_2_0_0) { @@ -195,12 +194,13 @@ Mesh &Mesh::setGridUnitSI(double gusi) "specify the gridUnitSI per axis (ref.: " "https://github.com/openPMD/openPMD-standard/pull/193).\n"; } + setAttribute("gridUnitSI", gusi); return *this; } -Mesh &Mesh::setGridUnitSI(std::vector gusi) +Mesh &Mesh::setGridUnitSI(std::vector const &gusi) { - return setGridUnitSIPerDimension(std::move(gusi)); + return setGridUnitSIPerDimension(gusi); } namespace @@ -247,7 +247,7 @@ std::vector Mesh::gridUnitSIPerDimension() const } } -Mesh &Mesh::setGridUnitSIPerDimension(std::vector gridUnitSI) +Mesh &Mesh::setGridUnitSIPerDimension(std::vector const &gridUnitSI) { if (auto standard = IOHandler()->m_standard; standard < OpenpmdStandard::v_2_0_0) @@ -260,7 +260,7 @@ Mesh &Mesh::setGridUnitSIPerDimension(std::vector gridUnitSI) "openPMD 2.0. Either upgrade the file to openPMD >= 2.0 " "or specify a scalar that applies to all axes."); } - setAttribute("gridUnitSI", std::move(gridUnitSI)); + setAttribute("gridUnitSI", gridUnitSI); return *this; } @@ -276,6 +276,12 @@ Mesh &Mesh::setUnitDimension(unit_representations::AsMap const &udim) return *this; } +Mesh &Mesh::setUnitDimension(unit_representations::AsArray const &udim) +{ + return setUnitDimension( + unit_representations::asMap(udim, /* skip_zeros = */ false)); +} + Mesh &Mesh::setGridUnitDimension(unit_representations::AsMaps const &udims) { auto rawGridUnitDimension = [this]() { @@ -300,6 +306,12 @@ Mesh &Mesh::setGridUnitDimension(unit_representations::AsMaps const &udims) return *this; } +Mesh &Mesh::setGridUnitDimension(unit_representations::AsArrays const &udims) +{ + return setGridUnitDimension( + unit_representations::asMaps(udims, /* skip_zeros = */ false)); +} + unit_representations::AsArrays Mesh::gridUnitDimension() const { if (containsAttribute("gridUnitDimension")) diff --git a/src/Record.cpp b/src/Record.cpp index ad3771bdfe..7d41fce5c2 100644 --- a/src/Record.cpp +++ b/src/Record.cpp @@ -43,6 +43,11 @@ Record &Record::setUnitDimension(unit_representations::AsMap const &udim) } return *this; } +Record &Record::setUnitDimension(unit_representations::AsArray const &udim) +{ + return setUnitDimension( + unit_representations::asMap(udim, /* skip_zeros = */ false)); +} void Record::flush_impl( std::string const &name, internal::FlushParams const &flushParams) diff --git a/src/UnitDimension.cpp b/src/UnitDimension.cpp index 103fdea6c5..3c8c7755a3 100644 --- a/src/UnitDimension.cpp +++ b/src/UnitDimension.cpp @@ -10,12 +10,12 @@ auto asArray(AsMap const &udim) -> AsArray auxiliary::fromMapOfUnitDimension(res.data(), udim); return res; } -auto asMap(AsArray const &array) -> AsMap +auto asMap(AsArray const &array, bool skip_zeros) -> AsMap { AsMap udim; for (size_t i = 0; i < array.size(); ++i) { - if (array[i] != 0) + if (!skip_zeros || array[i] != 0) { udim[static_cast(i)] = array[i]; } @@ -33,14 +33,15 @@ auto asArrays(AsMaps const &vec) -> AsArrays }); return res; } -auto asMaps(AsArrays const &vec) -> AsMaps +auto asMaps(AsArrays const &vec, bool skip_zeros) -> AsMaps { AsMaps res; res.reserve(vec.size()); std::transform( - vec.begin(), vec.end(), std::back_inserter(res), [](auto const &array) { - return asMap(array); - }); + vec.begin(), + vec.end(), + std::back_inserter(res), + [&](auto const &array) { return asMap(array, skip_zeros); }); return res; } diff --git a/src/binding/python/Mesh.cpp b/src/binding/python/Mesh.cpp index 0346a68dfd..bdc6f182f6 100644 --- a/src/binding/python/Mesh.cpp +++ b/src/binding/python/Mesh.cpp @@ -21,6 +21,7 @@ #include "openPMD/Mesh.hpp" #include "openPMD/Error.hpp" #include "openPMD/IO/AbstractIOHandler.hpp" +#include "openPMD/UnitDimension.hpp" #include "openPMD/backend/Attributable.hpp" #include "openPMD/backend/BaseRecord.hpp" #include "openPMD/backend/MeshRecordComponent.hpp" @@ -61,13 +62,31 @@ void init_Mesh(py::module &m) .def_property( "unit_dimension", &Mesh::unitDimension, - &Mesh::setUnitDimension, + [](Mesh &self, + std::variant< + unit_representations::AsMap, + unit_representations::AsArray> const &arg) -> Mesh & { + return std::visit( + [&](auto const &arg_resolved) -> Mesh & { + return self.setUnitDimension(arg_resolved); + }, + arg); + }, python::doc_unit_dimension) .def_property( "grid_unit_dimension", &Mesh::gridUnitDimension, - &Mesh::setGridUnitDimension, + [](Mesh &self, + std::variant< + unit_representations::AsMaps, + unit_representations::AsArrays> const &arg) -> Mesh & { + return std::visit( + [&](auto const &arg_resolved) -> Mesh & { + return self.setGridUnitDimension(arg_resolved); + }, + arg); + }, python::doc_mesh_unit_dimension) .def_property( @@ -124,11 +143,11 @@ void init_Mesh(py::module &m) return return_t(self.gridUnitSIPerDimension()); } }, - [](Mesh &self, std::variant> arg) { + [](Mesh &self, + std::variant> const &arg) -> Mesh & { return std::visit( - [&](auto &&arg_resolved) { - return self.setGridUnitSI( - static_cast(arg_resolved)); + [&](auto const &arg_resolved) -> Mesh & { + return self.setGridUnitSI(arg_resolved); }, arg); }, @@ -159,7 +178,10 @@ Ref.: https://github.com/openPMD/openPMD-standard/pull/193)"[1]) &Mesh::setTimeOffset) // TODO remove in future versions (deprecated) - .def("set_unit_dimension", &Mesh::setUnitDimension) + .def( + "set_unit_dimension", + py::overload_cast( + &Mesh::setUnitDimension)) .def( "set_geometry", py::overload_cast(&Mesh::setGeometry)) diff --git a/src/binding/python/Record.cpp b/src/binding/python/Record.cpp index b4f732a83d..0d8a0e94bb 100644 --- a/src/binding/python/Record.cpp +++ b/src/binding/python/Record.cpp @@ -20,6 +20,7 @@ */ #include "openPMD/Record.hpp" #include "openPMD/RecordComponent.hpp" +#include "openPMD/UnitDimension.hpp" #include "openPMD/backend/Attributable.hpp" #include "openPMD/backend/BaseRecord.hpp" @@ -50,7 +51,16 @@ void init_Record(py::module &m) .def_property( "unit_dimension", &Record::unitDimension, - &Record::setUnitDimension, + [](Record &self, + std::variant< + unit_representations::AsMap, + unit_representations::AsArray> const &arg) -> Record & { + return std::visit( + [&](auto const &arg_resolved) -> Record & { + return self.setUnitDimension(arg_resolved); + }, + arg); + }, python::doc_unit_dimension) .def_property( @@ -67,7 +77,10 @@ void init_Record(py::module &m) &Record::setTimeOffset) // TODO remove in future versions (deprecated) - .def("set_unit_dimension", &Record::setUnitDimension) + .def( + "set_unit_dimension", + py::overload_cast( + &Record::setUnitDimension)) .def("set_time_offset", &Record::setTimeOffset) .def("set_time_offset", &Record::setTimeOffset) .def("set_time_offset", &Record::setTimeOffset); diff --git a/src/binding/python/UnitDimension.cpp b/src/binding/python/UnitDimension.cpp index eb32e87de1..abd119c5b3 100644 --- a/src/binding/python/UnitDimension.cpp +++ b/src/binding/python/UnitDimension.cpp @@ -43,7 +43,13 @@ void init_UnitDimension(py::module &m) return static_cast(idx); }) .def("as_array", &unit_representations::asArray) - .def("as_map", &unit_representations::asMap) + .def( + "as_map", + &unit_representations::asMap, + py::arg("skip_zeros") = true) .def("as_arrays", &unit_representations::asArrays) - .def("as_maps", &unit_representations::asMaps); + .def( + "as_maps", + &unit_representations::asMaps, + py::arg("skip_zeros") = true); }