Skip to content

Commit 7534cb8

Browse files
committed
Default value for gridUnitSI set upon flush
1 parent 0314df5 commit 7534cb8

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
@@ -406,6 +405,18 @@ void Mesh::flush_impl(
406405
comp.second.flush(comp.first, flushParams);
407406
}
408407
}
408+
if (!containsAttribute("gridUnitSI"))
409+
{
410+
if (auto series = retrieveSeries(); series.openPMD() < "2.")
411+
{
412+
setGridUnitSI(1);
413+
}
414+
else
415+
{
416+
setGridUnitSIPerDimension(
417+
std::vector<double>(retrieveMeshDimensionality(*this), 1));
418+
}
419+
}
409420
flushAttributes(flushParams);
410421
}
411422
}

test/CoreTest.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -539,11 +539,10 @@ TEST_CASE("mesh_constructor_test", "[core]")
539539
REQUIRE(m.gridSpacing<double>() == gs);
540540
std::vector<double> ggo{0};
541541
REQUIRE(m.gridGlobalOffset() == ggo);
542-
REQUIRE(m.gridUnitSI() == static_cast<double>(1));
543542
REQUIRE(
544543
m.numAttributes() ==
545-
8); /* axisLabels, dataOrder, geometry, gridGlobalOffset, gridSpacing,
546-
gridUnitSI, timeOffset, unitDimension */
544+
7); /* axisLabels, dataOrder, geometry, gridGlobalOffset, gridSpacing,
545+
timeOffset, unitDimension */
547546
}
548547

549548
TEST_CASE("mesh_modification_test", "[core]")
@@ -557,22 +556,22 @@ TEST_CASE("mesh_modification_test", "[core]")
557556

558557
m.setGeometry(Mesh::Geometry::spherical);
559558
REQUIRE(m.geometry() == Mesh::Geometry::spherical);
560-
REQUIRE(m.numAttributes() == 8);
559+
REQUIRE(m.numAttributes() == 7);
561560
m.setDataOrder(Mesh::DataOrder::F);
562561
REQUIRE(m.dataOrder() == Mesh::DataOrder::F);
563-
REQUIRE(m.numAttributes() == 8);
562+
REQUIRE(m.numAttributes() == 7);
564563
std::vector<std::string> al{"z_", "y_", "x_"};
565564
m.setAxisLabels({"z_", "y_", "x_"});
566565
REQUIRE(m.axisLabels() == al);
567-
REQUIRE(m.numAttributes() == 8);
566+
REQUIRE(m.numAttributes() == 7);
568567
std::vector<double> gs{1e-5, 2e-5, 3e-5};
569568
m.setGridSpacing(gs);
570569
REQUIRE(m.gridSpacing<double>() == gs);
571-
REQUIRE(m.numAttributes() == 8);
570+
REQUIRE(m.numAttributes() == 7);
572571
std::vector<double> ggo{1e-10, 2e-10, 3e-10};
573572
m.setGridGlobalOffset({1e-10, 2e-10, 3e-10});
574573
REQUIRE(m.gridGlobalOffset() == ggo);
575-
REQUIRE(m.numAttributes() == 8);
574+
REQUIRE(m.numAttributes() == 7);
576575
m.setGridUnitSI(42.0);
577576
REQUIRE(m.gridUnitSI() == static_cast<double>(42));
578577
REQUIRE(m.numAttributes() == 8);

0 commit comments

Comments
 (0)