Skip to content

Commit

Permalink
Better function argument types
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Nov 4, 2024
1 parent 44decb6 commit 054ae0c
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 27 deletions.
30 changes: 27 additions & 3 deletions include/openPMD/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ class Mesh : public BaseRecord<MeshRecordComponent>
*
* @return Reference to modified mesh.
*/
Mesh &setGridUnitSI(std::vector<double> gridUnitSI);
Mesh &setGridUnitSI(std::vector<double> const &gridUnitSI);

/**
* @return A vector of the gridUnitSI per grid axis in the order of
Expand All @@ -235,7 +235,7 @@ class Mesh : public BaseRecord<MeshRecordComponent>
*
* @return Reference to modified mesh.
*/
Mesh &setGridUnitSIPerDimension(std::vector<double> gridUnitSI);
Mesh &setGridUnitSIPerDimension(std::vector<double> const &gridUnitSI);

/** Set the powers of the 7 base measures characterizing the record's unit
* in SI.
Expand All @@ -246,11 +246,20 @@ class Mesh : public BaseRecord<MeshRecordComponent>
*/
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
Expand All @@ -261,6 +270,21 @@ class Mesh : public BaseRecord<MeshRecordComponent>
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<UnitDimension, double>` 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.
Expand Down
1 change: 1 addition & 0 deletions include/openPMD/Record.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ class Record : public BaseRecord<RecordComponent>
~Record() override = default;

Record &setUnitDimension(unit_representations::AsMap const &);
Record &setUnitDimension(unit_representations::AsArray const &);

template <typename T>
T timeOffset() const;
Expand Down
4 changes: 2 additions & 2 deletions include/openPMD/UnitDimension.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ namespace unit_representations
using AsArrays = std::vector<AsArray>;

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
{
Expand Down
22 changes: 17 additions & 5 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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<double> gusi)
Mesh &Mesh::setGridUnitSI(std::vector<double> const &gusi)
{
return setGridUnitSIPerDimension(std::move(gusi));
return setGridUnitSIPerDimension(gusi);
}

namespace
Expand Down Expand Up @@ -247,7 +247,7 @@ std::vector<double> Mesh::gridUnitSIPerDimension() const
}
}

Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> gridUnitSI)
Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> const &gridUnitSI)
{
if (auto standard = IOHandler()->m_standard;
standard < OpenpmdStandard::v_2_0_0)
Expand All @@ -260,7 +260,7 @@ Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> 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;
}

Expand All @@ -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]() {
Expand All @@ -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"))
Expand Down
5 changes: 5 additions & 0 deletions src/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
13 changes: 7 additions & 6 deletions src/UnitDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<UnitDimension>(i)] = array[i];
}
Expand All @@ -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;
}

Expand Down
36 changes: 29 additions & 7 deletions src/binding/python/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -124,11 +143,11 @@ void init_Mesh(py::module &m)
return return_t(self.gridUnitSIPerDimension());
}
},
[](Mesh &self, std::variant<double, std::vector<double>> arg) {
[](Mesh &self,
std::variant<double, std::vector<double>> const &arg) -> Mesh & {
return std::visit(
[&](auto &&arg_resolved) {
return self.setGridUnitSI(
static_cast<decltype(arg_resolved)>(arg_resolved));
[&](auto const &arg_resolved) -> Mesh & {
return self.setGridUnitSI(arg_resolved);
},
arg);
},
Expand Down Expand Up @@ -159,7 +178,10 @@ Ref.: https://github.com/openPMD/openPMD-standard/pull/193)"[1])
&Mesh::setTimeOffset<double>)

// TODO remove in future versions (deprecated)
.def("set_unit_dimension", &Mesh::setUnitDimension)
.def(
"set_unit_dimension",
py::overload_cast<unit_representations::AsMap const &>(
&Mesh::setUnitDimension))
.def(
"set_geometry",
py::overload_cast<Mesh::Geometry>(&Mesh::setGeometry))
Expand Down
17 changes: 15 additions & 2 deletions src/binding/python/Record.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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(
Expand All @@ -67,7 +77,10 @@ void init_Record(py::module &m)
&Record::setTimeOffset<long double>)

// TODO remove in future versions (deprecated)
.def("set_unit_dimension", &Record::setUnitDimension)
.def(
"set_unit_dimension",
py::overload_cast<unit_representations::AsMap const &>(
&Record::setUnitDimension))
.def("set_time_offset", &Record::setTimeOffset<float>)
.def("set_time_offset", &Record::setTimeOffset<double>)
.def("set_time_offset", &Record::setTimeOffset<long double>);
Expand Down
10 changes: 8 additions & 2 deletions src/binding/python/UnitDimension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,13 @@ void init_UnitDimension(py::module &m)
return static_cast<UnitDimension>(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);
}

0 comments on commit 054ae0c

Please sign in to comment.