Skip to content

Commit a244159

Browse files
committed
Default value for gridUnitSI set upon flush
1 parent 7bdac76 commit a244159

File tree

3 files changed

+29
-20
lines changed

3 files changed

+29
-20
lines changed

include/openPMD/Mesh.hpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,8 @@ class Mesh : public BaseRecord<MeshRecordComponent>
184184
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
185185
* simulation units to SI units.
186186
*
187-
* Note that this API follows the legacy (openPMD 1.*) definition
188-
* of gridUnitSI.
189-
* In order to specify the gridUnitSI per dimension,
187+
* Valid for openPMD version 1.*.
188+
* In order to specify the gridUnitSI per dimension (openPMD 2.*),
190189
* use the vector overload or `setGridUnitSIPerDimension()`.
191190
*
192191
* @param gridUnitSI unit-conversion factor to multiply each value in
@@ -196,12 +195,14 @@ class Mesh : public BaseRecord<MeshRecordComponent>
196195
*/
197196
Mesh &setGridUnitSI(double gridUnitSI);
198197

199-
/** Set the unit-conversion factor per dimension to multiply each value in
198+
/** Alias for `setGridUnitSI(std::vector<double>)`.
199+
*
200+
* Set the unit-conversion factor per dimension to multiply each value in
200201
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
201202
* simulation units to SI units.
202203
*
203-
* Note that this is a feature of openPMD 2.0.
204-
* The legacy behavior (a scalar gridUnitSI) is implemented
204+
* Valid for openPMD 2.*.
205+
* The legacy behavior (openPMD 1.*, a scalar gridUnitSI) is implemented
205206
* by `setGridUnitSI(double)`.
206207
*
207208
* @param gridUnitSI unit-conversion factor to multiply each value in
@@ -220,14 +221,12 @@ class Mesh : public BaseRecord<MeshRecordComponent>
220221
*/
221222
std::vector<double> gridUnitSIPerDimension() const;
222223

223-
/** Alias for `setGridUnitSI(std::vector<double>)`.
224-
*
225-
* Set the unit-conversion factor per dimension to multiply each value in
224+
/* Set the unit-conversion factor per dimension to multiply each value in
226225
* Mesh::gridSpacing and Mesh::gridGlobalOffset, in order to convert from
227226
* simulation units to SI units.
228227
*
229-
* Note that this is a feature of openPMD 2.0.
230-
* The legacy behavior (a scalar gridUnitSI) is implemented
228+
* Valid for openPMD 2.*.
229+
* The legacy behavior (openPMD 1.*, a scalar gridUnitSI) is implemented
231230
* by `setGridUnitSI(double)`.
232231
*
233232
* @param gridUnitSI unit-conversion factor to multiply each value in

src/Mesh.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ Mesh::Mesh()
4242
setAxisLabels({"x"}); // empty strings are not allowed in HDF5
4343
setGridSpacing(std::vector<double>{1});
4444
setGridGlobalOffset({0});
45-
setGridUnitSI(1);
4645
}
4746

4847
Mesh::Geometry Mesh::geometry() const
@@ -405,6 +404,18 @@ void Mesh::flush_impl(
405404
comp.second.flush(comp.first, flushParams);
406405
}
407406
}
407+
if (!containsAttribute("gridUnitSI"))
408+
{
409+
if (auto series = retrieveSeries(); series.openPMD() < "2.")
410+
{
411+
setGridUnitSI(1);
412+
}
413+
else
414+
{
415+
setGridUnitSIPerDimension(
416+
std::vector<double>(retrieveMeshDimensionality(*this), 1));
417+
}
418+
}
408419
flushAttributes(flushParams);
409420
}
410421
}

test/CoreTest.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -527,11 +527,10 @@ TEST_CASE("mesh_constructor_test", "[core]")
527527
REQUIRE(m.gridSpacing<double>() == gs);
528528
std::vector<double> ggo{0};
529529
REQUIRE(m.gridGlobalOffset() == ggo);
530-
REQUIRE(m.gridUnitSI() == static_cast<double>(1));
531530
REQUIRE(
532531
m.numAttributes() ==
533-
8); /* axisLabels, dataOrder, geometry, gridGlobalOffset, gridSpacing,
534-
gridUnitSI, timeOffset, unitDimension */
532+
7); /* axisLabels, dataOrder, geometry, gridGlobalOffset, gridSpacing,
533+
timeOffset, unitDimension */
535534

536535
o.flush();
537536
REQUIRE(m["x"].unitSI() == 1);
@@ -556,22 +555,22 @@ TEST_CASE("mesh_modification_test", "[core]")
556555

557556
m.setGeometry(Mesh::Geometry::spherical);
558557
REQUIRE(m.geometry() == Mesh::Geometry::spherical);
559-
REQUIRE(m.numAttributes() == 8);
558+
REQUIRE(m.numAttributes() == 7);
560559
m.setDataOrder(Mesh::DataOrder::F);
561560
REQUIRE(m.dataOrder() == Mesh::DataOrder::F);
562-
REQUIRE(m.numAttributes() == 8);
561+
REQUIRE(m.numAttributes() == 7);
563562
std::vector<std::string> al{"z_", "y_", "x_"};
564563
m.setAxisLabels({"z_", "y_", "x_"});
565564
REQUIRE(m.axisLabels() == al);
566-
REQUIRE(m.numAttributes() == 8);
565+
REQUIRE(m.numAttributes() == 7);
567566
std::vector<double> gs{1e-5, 2e-5, 3e-5};
568567
m.setGridSpacing(gs);
569568
REQUIRE(m.gridSpacing<double>() == gs);
570-
REQUIRE(m.numAttributes() == 8);
569+
REQUIRE(m.numAttributes() == 7);
571570
std::vector<double> ggo{1e-10, 2e-10, 3e-10};
572571
m.setGridGlobalOffset({1e-10, 2e-10, 3e-10});
573572
REQUIRE(m.gridGlobalOffset() == ggo);
574-
REQUIRE(m.numAttributes() == 8);
573+
REQUIRE(m.numAttributes() == 7);
575574
m.setGridUnitSI(42.0);
576575
REQUIRE(m.gridUnitSI() == static_cast<double>(42));
577576
REQUIRE(m.numAttributes() == 8);

0 commit comments

Comments
 (0)