Skip to content

Commit

Permalink
Add an alias API + some bit of testing
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Feb 14, 2024
1 parent d04df0e commit be3dbbd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 8 deletions.
25 changes: 21 additions & 4 deletions include/openPMD/Mesh.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class Mesh : public BaseRecord<MeshRecordComponent>
* Note that this API follows the legacy (openPMD 1.*) definition
* of gridUnitSI.
* In order to specify the gridUnitSI per dimension,
* use `setGridUnitSIPerDimension()`.
* use the vector overload or `setGridUnitSIPerDimension()`.
*
* @param gridUnitSI unit-conversion factor to multiply each value in
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
Expand All @@ -196,6 +196,22 @@ class Mesh : public BaseRecord<MeshRecordComponent>
*/
Mesh &setGridUnitSI(double gridUnitSI);

/** Set the unit-conversion factor per dimension to multiply each value in
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
* simulation units to SI units.
*
* Note that this is a feature of openPMD 2.0.
* The legacy behavior (a scalar gridUnitSI) is implemented
* by `setGridUnitSI(double)`.
*
* @param gridUnitSI unit-conversion factor to multiply each value in
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
* simulation units to SI units.
*
* @return Reference to modified mesh.
*/
Mesh &setGridUnitSI(std::vector<double> gridUnitSI);

/**
* @return A vector of the gridUnitSI per grid dimension as defined
* by the axisLabels. If the gridUnitSI is defined as a scalar
Expand All @@ -204,21 +220,22 @@ class Mesh : public BaseRecord<MeshRecordComponent>
*/
std::vector<double> gridUnitSIPerDimension() const;

/** Set the unit-conversion factor per dimension to multiply each value in
/** Alias for `setGridUnitSI(std::vector<double>)`.
*
* Set the unit-conversion factor per dimension to multiply each value in
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
* simulation units to SI units.
*
* Note that this is a feature of openPMD 2.0.
* The legacy behavior (a scalar gridUnitSI) is implemented
* by `setGridUnitSI()`.
* by `setGridUnitSI(double)`.
*
* @param gridUnitSI unit-conversion factor to multiply each value in
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
* simulation units to SI units.
*
* @return Reference to modified mesh.
*/

Mesh &setGridUnitSIPerDimension(std::vector<double> gridUnitSI);

/** Set the powers of the 7 base measures characterizing the record's unit
Expand Down
5 changes: 5 additions & 0 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,11 @@ Mesh &Mesh::setGridUnitSI(double gusi)
return *this;
}

Mesh &Mesh::setGridUnitSI(std::vector<double> gusi)
{
return setGridUnitSIPerDimension(std::move(gusi));
}

namespace
{
uint64_t retrieveMeshDimensionality(Mesh const &m)
Expand Down
9 changes: 7 additions & 2 deletions src/binding/python/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ void init_Mesh(py::module &m)
"grid_global_offset",
&Mesh::gridGlobalOffset,
&Mesh::setGridGlobalOffset)
.def_property("grid_unit_SI", &Mesh::gridUnitSI, &Mesh::setGridUnitSI)
.def_property(
"grid_unit_SI",
&Mesh::gridUnitSI,
py::overload_cast<double>(&Mesh::setGridUnitSI))
.def_property(
"grid_unit_SI_per_dimension",
&Mesh::gridUnitSIPerDimension,
Expand All @@ -122,7 +125,9 @@ void init_Mesh(py::module &m)
.def("set_grid_spacing", &Mesh::setGridSpacing<double>)
.def("set_grid_spacing", &Mesh::setGridSpacing<long double>)
.def("set_grid_global_offset", &Mesh::setGridGlobalOffset)
.def("set_grid_unit_SI", &Mesh::setGridUnitSI);
.def(
"set_grid_unit_SI",
py::overload_cast<double>(&Mesh::setGridUnitSI));
add_pickle(
cl, [](openPMD::Series &series, std::vector<std::string> const &group) {
uint64_t const n_it = std::stoull(group.at(1));
Expand Down
19 changes: 17 additions & 2 deletions test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// expose private and protected members for invasive testing
#include "openPMD/Datatype.hpp"
#include "openPMD/IO/Access.hpp"
#include "openPMD/UnitDimension.hpp"
#if openPMD_USE_INVASIVE_TESTS
#define OPENPMD_private public:
#define OPENPMD_protected public:
Expand Down Expand Up @@ -926,12 +927,20 @@ inline void constant_scalar(std::string const &file_ending)

// store a number of predefined attributes in E
Mesh &E_mesh = s.iterations[1].meshes["E"];
// test that these can be defined successively
E_mesh.setGridUnitDimension({{{UnitDimension::L, 1}}, {}, {}});
E_mesh.setGridUnitDimension(
{{}, {{UnitDimension::L, 1}}, {{UnitDimension::L, 1}}});
// let's modify the last dimension for the test's purpose now
E_mesh.setGridUnitDimension({{}, {}, {{UnitDimension::M, 1}}});
// should now all be [1,0,0,0,0,0,0], except the last which should be
// [1,1,0,0,0,0,0]
E_mesh.setGeometry(geometry);
E_mesh.setGeometryParameters(geometryParameters);
E_mesh.setDataOrder(dataOrder);
E_mesh.setGridSpacing(gridSpacing);
E_mesh.setGridGlobalOffset(gridGlobalOffset);
E_mesh.setGridUnitSI(gridUnitSI);
E_mesh.setGridUnitSI(std::vector(3, gridUnitSI));
E_mesh.setAxisLabels(axisLabels);
E_mesh.setUnitDimension(unitDimensions);
E_mesh.setTimeOffset(timeOffset);
Expand Down Expand Up @@ -1095,12 +1104,18 @@ inline void constant_scalar(std::string const &file_ending)
Extent{3, 2, 1});

Mesh &E_mesh = s.iterations[1].meshes["E"];
REQUIRE(
E_mesh.gridUnitDimension() ==
std::vector{
std::array<double, 7>{1., 0., 0., 0., 0, .0, 0.},
std::array<double, 7>{1., 0., 0., 0., 0, .0, 0.},
std::array<double, 7>{1., 1., 0., 0., 0, .0, 0.}});
REQUIRE(E_mesh.geometry() == geometry);
REQUIRE(E_mesh.geometryParameters() == geometryParameters);
REQUIRE(E_mesh.dataOrder() == dataOrder);
REQUIRE(E_mesh.gridSpacing<double>() == gridSpacing);
REQUIRE(E_mesh.gridGlobalOffset() == gridGlobalOffset);
REQUIRE(E_mesh.gridUnitSI() == gridUnitSI);
REQUIRE(E_mesh.gridUnitSIPerDimension() == std::vector(3, gridUnitSI));
REQUIRE(E_mesh.axisLabels() == axisLabels);
// REQUIRE( E_mesh.unitDimension() == unitDimensions );
REQUIRE(E_mesh.timeOffset<double>() == timeOffset);
Expand Down

0 comments on commit be3dbbd

Please sign in to comment.