Skip to content

Commit 9965f6c

Browse files
committed
Fallback for undefined gridUnitSI
1 parent 1c22770 commit 9965f6c

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

src/Mesh.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -209,14 +209,10 @@ namespace
209209
{
210210
uint64_t retrieveMeshDimensionality(Mesh const &m)
211211
{
212-
try
212+
if (m.containsAttribute("axisLabels"))
213213
{
214214
return m.axisLabels().size();
215215
}
216-
catch (no_such_attribute_error const &)
217-
{
218-
// no-op, continue with fallback below
219-
}
220216

221217
// maybe we have record components and can ask them
222218
if (auto it = m.begin(); it != m.end())
@@ -233,7 +229,27 @@ namespace
233229

234230
std::vector<double> Mesh::gridUnitSIPerDimension() const
235231
{
236-
return getAttribute("gridUnitSI").get<std::vector<double>>();
232+
if (containsAttribute("gridUnitSI"))
233+
{
234+
if (auto series_opt = retrieveSeries_optional(); series_opt.has_value())
235+
{
236+
if (auto version = series_opt->openPMD(); version < "2.")
237+
{
238+
// If the openPMD version is lower than 2.0, the gridUnitSI is a
239+
// scalar interpreted for all axes. Copy it d times.
240+
return std::vector<double>(
241+
retrieveMeshDimensionality(*this),
242+
getAttribute("gridUnitSI").get<double>());
243+
}
244+
}
245+
return getAttribute("gridUnitSI").get<std::vector<double>>();
246+
}
247+
else
248+
{
249+
// gridUnitSI is an optional attribute
250+
// if it is missing, the mesh is interpreted as unscaled
251+
return std::vector<double>(retrieveMeshDimensionality(*this), 1.);
252+
}
237253
}
238254

239255
Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> gridUnitSI)
@@ -328,7 +344,7 @@ std::vector<std::array<double, 7>> Mesh::gridUnitDimension() const
328344
}
329345
else
330346
{
331-
// gridUnitSI is an optional attribute
347+
// gridUnitDimension is an optional attribute
332348
// if it is missing, the mesh is interpreted as spatial
333349
std::array<double, 7> spatialMesh;
334350
fromMapOfUnitDimension(spatialMesh.begin(), {{UnitDimension::L, 1}});

test/SerialIOTest.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -947,10 +947,15 @@ inline void constant_scalar(std::string const &file_ending)
947947
E_mesh.setDataOrder(dataOrder);
948948
E_mesh.setGridSpacing(gridSpacing);
949949
E_mesh.setGridGlobalOffset(gridGlobalOffset);
950-
E_mesh.setGridUnitSI(std::vector(3, gridUnitSI));
951950
E_mesh.setAxisLabels(axisLabels);
952951
E_mesh.setUnitDimension(unitDimensions);
953952
E_mesh.setTimeOffset(timeOffset);
953+
REQUIRE(
954+
E_mesh.gridUnitSIPerDimension() == std::vector<double>{1., 1., 1.});
955+
E_mesh.setGridUnitSI(std::vector(3, gridUnitSI));
956+
REQUIRE(
957+
E_mesh.gridUnitSIPerDimension() ==
958+
std::vector<double>{gridUnitSI, gridUnitSI, gridUnitSI});
954959

955960
// constant scalar
956961
auto pos =

0 commit comments

Comments
 (0)