Skip to content

Commit 5fc0073

Browse files
committed
Fix bugs detected by this verifier
Both of the form "data not found in places where data was expected"
1 parent 3414e59 commit 5fc0073

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

src/Series.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -742,8 +742,13 @@ void Series::flushFileBased(
742742
bool flushIOHandler)
743743
{
744744
auto &series = get();
745-
if (end == begin)
746-
throw std::runtime_error(
745+
if (end == begin &&
746+
/*
747+
* At parsing time, this might happen since iterations might contain
748+
* errors and be deleted.
749+
*/
750+
IOHandler()->m_seriesStatus != internal::SeriesStatus::Parsing)
751+
throw error::WrongAPIUsage(
747752
"fileBased output can not be written with no iterations.");
748753

749754
switch (IOHandler()->m_frontendAccess)
@@ -859,6 +864,26 @@ void Series::flushGorVBased(
859864
bool flushIOHandler)
860865
{
861866
auto &series = get();
867+
if (iterationEncoding() == IterationEncoding::variableBased &&
868+
/*
869+
* At parsing time, this might happen since iterations might contain
870+
* errors and be deleted.
871+
*/
872+
IOHandler()->m_seriesStatus != internal::SeriesStatus::Parsing &&
873+
iterations.empty())
874+
/*
875+
* Note: Unlike flushFileBased, it's ok if `begin == end` since this
876+
* method may be called without an explicit iteration.
877+
* But since in variable-based encoding the base path is the same as the
878+
* path to the (currently active) iteration, there must be at least one
879+
* iteration present since the openPMD standard requires mandatory
880+
* attributes.
881+
* In group-based encoding, any number of iterations might be included
882+
* in the base path, in variable-based encoding there must be exactly
883+
* one iteration currently active.
884+
*/
885+
throw error::WrongAPIUsage(
886+
"variableBased output can not be written with no iterations.");
862887
if (access::readOnly(IOHandler()->m_frontendAccess))
863888
{
864889
for (auto it = begin; it != end; ++it)

test/CoreTest.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,8 @@ TEST_CASE("myPath", "[core]")
190190
REQUIRE(
191191
pathOf(scalarMesh) ==
192192
vec_t{"iterations", "1234", "meshes", "e_chargeDensity"});
193-
auto scalarMeshComponent = scalarMesh[RecordComponent::SCALAR];
193+
auto scalarMeshComponent = scalarMesh[RecordComponent::SCALAR].resetDataset(
194+
{Datatype::FLOAT, {10}});
194195
REQUIRE(
195196
pathOf(scalarMeshComponent) ==
196197
vec_t{
@@ -203,7 +204,8 @@ TEST_CASE("myPath", "[core]")
203204

204205
auto vectorMesh = iteration.meshes["E"];
205206
REQUIRE(pathOf(vectorMesh) == vec_t{"iterations", "1234", "meshes", "E"});
206-
auto vectorMeshComponent = vectorMesh["x"];
207+
auto vectorMeshComponent =
208+
vectorMesh["x"].resetDataset({Datatype::FLOAT, {10}});
207209
REQUIRE(
208210
pathOf(vectorMeshComponent) ==
209211
vec_t{"iterations", "1234", "meshes", "E", "x"});
@@ -220,7 +222,8 @@ TEST_CASE("myPath", "[core]")
220222
pathOf(speciesPosition) ==
221223
vec_t{"iterations", "1234", "particles", "e", "position"});
222224

223-
auto speciesPositionX = speciesPosition["x"];
225+
auto speciesPositionX =
226+
speciesPosition["x"].resetDataset({Datatype::FLOAT, {10}});
224227
REQUIRE(
225228
pathOf(speciesPositionX) ==
226229
vec_t{"iterations", "1234", "particles", "e", "position", "x"});
@@ -231,7 +234,9 @@ TEST_CASE("myPath", "[core]")
231234
pathOf(speciesWeighting) ==
232235
vec_t{"iterations", "1234", "particles", "e", "weighting"});
233236

234-
auto speciesWeightingX = speciesWeighting[RecordComponent::SCALAR];
237+
auto speciesWeightingX =
238+
speciesWeighting[RecordComponent::SCALAR].resetDataset(
239+
{Datatype::FLOAT, {10}});
235240
REQUIRE(
236241
pathOf(speciesWeightingX) ==
237242
vec_t{
@@ -258,7 +263,7 @@ TEST_CASE("myPath", "[core]")
258263
"particlePatches",
259264
"extent"});
260265

261-
auto patchExtentX = patchExtent["x"];
266+
auto patchExtentX = patchExtent["x"].resetDataset({Datatype::INT, {10}});
262267
REQUIRE(
263268
pathOf(patchExtentX) ==
264269
vec_t{
@@ -282,7 +287,8 @@ TEST_CASE("myPath", "[core]")
282287
"numParticles"});
283288

284289
auto patchNumParticlesComponent =
285-
patchNumParticles[RecordComponent::SCALAR];
290+
patchNumParticles[RecordComponent::SCALAR].resetDataset(
291+
{Datatype::INT, {10}});
286292
REQUIRE(
287293
pathOf(patchNumParticlesComponent) ==
288294
vec_t{
@@ -293,6 +299,10 @@ TEST_CASE("myPath", "[core]")
293299
"particlePatches",
294300
"numParticles",
295301
RecordComponent::SCALAR});
302+
303+
speciesE.particlePatches["offset"]["x"].resetDataset({Datatype::INT, {10}});
304+
speciesE.particlePatches["numParticlesOffset"][RecordComponent::SCALAR]
305+
.resetDataset({Datatype::INT, {10}});
296306
#endif
297307
}
298308

@@ -1058,6 +1068,7 @@ TEST_CASE("backend_via_json", "[core]")
10581068
{
10591069
Series series(
10601070
"../samples/optionsViaJson", Access::CREATE, encodingVariableBased);
1071+
series.iterations[0]; // v-based encoding requires at least 1 iteration
10611072
REQUIRE(series.backend() == "JSON");
10621073
REQUIRE(series.iterationEncoding() == IterationEncoding::variableBased);
10631074
}
@@ -1071,6 +1082,7 @@ TEST_CASE("backend_via_json", "[core]")
10711082
"../samples/optionsViaJson.bp",
10721083
Access::CREATE,
10731084
encodingVariableBased);
1085+
series.iterations[0]; // v-based encoding requires at least 1 iteration
10741086
REQUIRE(series.backend() == "JSON");
10751087
REQUIRE(series.iterationEncoding() == IterationEncoding::variableBased);
10761088
}

0 commit comments

Comments
 (0)