Skip to content

Commit

Permalink
Fallback for undefined gridUnitSI
Browse files Browse the repository at this point in the history
  • Loading branch information
franzpoeschel committed Jul 3, 2024
1 parent 2ff3cb5 commit f034d20
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
19 changes: 12 additions & 7 deletions src/Mesh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,14 +209,10 @@ namespace
{
uint64_t retrieveMeshDimensionality(Mesh const &m)
{
try
if (m.containsAttribute("axisLabels"))
{
return m.axisLabels().size();
}
catch (no_such_attribute_error const &)
{
// no-op, continue with fallback below
}

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

std::vector<double> Mesh::gridUnitSIPerDimension() const
{
return getAttribute("gridUnitSI").get<std::vector<double>>();
if (containsAttribute("gridUnitSI"))
{
return getAttribute("gridUnitSI").get<std::vector<double>>();
}
else
{
// gridUnitSI is an optional attribute
// if it is missing, the mesh is interpreted as unscaled
return std::vector<double>(retrieveMeshDimensionality(*this), 1.);
}
}

Mesh &Mesh::setGridUnitSIPerDimension(std::vector<double> gridUnitSI)
Expand Down Expand Up @@ -328,7 +333,7 @@ std::vector<std::array<double, 7>> Mesh::gridUnitDimension() const
}
else
{
// gridUnitSI is an optional attribute
// gridUnitDimension is an optional attribute
// if it is missing, the mesh is interpreted as spatial
std::array<double, 7> spatialMesh;
fromMapOfUnitDimension(spatialMesh.begin(), {{UnitDimension::L, 1}});
Expand Down
7 changes: 6 additions & 1 deletion test/SerialIOTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -947,10 +947,15 @@ inline void constant_scalar(std::string const &file_ending)
E_mesh.setDataOrder(dataOrder);
E_mesh.setGridSpacing(gridSpacing);
E_mesh.setGridGlobalOffset(gridGlobalOffset);
E_mesh.setGridUnitSI(std::vector(3, gridUnitSI));
E_mesh.setAxisLabels(axisLabels);
E_mesh.setUnitDimension(unitDimensions);
E_mesh.setTimeOffset(timeOffset);
REQUIRE(
E_mesh.gridUnitSIPerDimension() == std::vector<double>{1., 1., 1.});
E_mesh.setGridUnitSI(std::vector(3, gridUnitSI));
REQUIRE(
E_mesh.gridUnitSIPerDimension() ==
std::vector<double>{gridUnitSI, gridUnitSI, gridUnitSI});

// constant scalar
auto pos =
Expand Down

0 comments on commit f034d20

Please sign in to comment.