Skip to content

Commit 373175c

Browse files
committed
Fix bugs detected by this verifier
Both of the form "data not found in places where data was expected"
1 parent 2e0c0bf commit 373175c

2 files changed

Lines changed: 45 additions & 8 deletions

File tree

src/Series.cpp

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -754,8 +754,13 @@ void Series::flushFileBased(
754754
bool flushIOHandler)
755755
{
756756
auto &series = get();
757-
if (end == begin)
758-
throw std::runtime_error(
757+
if (end == begin &&
758+
/*
759+
* At parsing time, this might happen since iterations might contain
760+
* errors and be deleted.
761+
*/
762+
IOHandler()->m_seriesStatus != internal::SeriesStatus::Parsing)
763+
throw error::WrongAPIUsage(
759764
"fileBased output can not be written with no iterations.");
760765

761766
switch (IOHandler()->m_frontendAccess)
@@ -871,6 +876,26 @@ void Series::flushGorVBased(
871876
bool flushIOHandler)
872877
{
873878
auto &series = get();
879+
if (iterationEncoding() == IterationEncoding::variableBased &&
880+
/*
881+
* At parsing time, this might happen since iterations might contain
882+
* errors and be deleted.
883+
*/
884+
IOHandler()->m_seriesStatus != internal::SeriesStatus::Parsing &&
885+
iterations.empty())
886+
/*
887+
* Note: Unlike flushFileBased, it's ok if `begin == end` since this
888+
* method may be called without an explicit iteration.
889+
* But since in variable-based encoding the base path is the same as the
890+
* path to the (currently active) iteration, there must be at least one
891+
* iteration present since the openPMD standard requires mandatory
892+
* attributes.
893+
* In group-based encoding, any number of iterations might be included
894+
* in the base path, in variable-based encoding there must be exactly
895+
* one iteration currently active.
896+
*/
897+
throw error::WrongAPIUsage(
898+
"variableBased output can not be written with no iterations.");
874899
if (access::readOnly(IOHandler()->m_frontendAccess))
875900
{
876901
for (auto it = begin; it != end; ++it)

test/CoreTest.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,17 @@ TEST_CASE("myPath", "[core]")
201201
REQUIRE(
202202
pathOf(scalarMesh) ==
203203
vec_t{"iterations", "1234", "meshes", "e_chargeDensity"});
204-
auto scalarMeshComponent = scalarMesh[RecordComponent::SCALAR];
204+
auto scalarMeshComponent = scalarMesh[RecordComponent::SCALAR].resetDataset(
205+
{Datatype::FLOAT, {10}});
205206
REQUIRE(
206207
pathOf(scalarMeshComponent) ==
207208
vec_t{"iterations", "1234", "meshes", "e_chargeDensity"});
208209
writeSomething(scalarMeshComponent);
209210

210211
auto vectorMesh = iteration.meshes["E"];
211212
REQUIRE(pathOf(vectorMesh) == vec_t{"iterations", "1234", "meshes", "E"});
212-
auto vectorMeshComponent = vectorMesh["x"];
213+
auto vectorMeshComponent =
214+
vectorMesh["x"].resetDataset({Datatype::FLOAT, {10}});
213215
REQUIRE(
214216
pathOf(vectorMeshComponent) ==
215217
vec_t{"iterations", "1234", "meshes", "E", "x"});
@@ -226,7 +228,8 @@ TEST_CASE("myPath", "[core]")
226228
pathOf(speciesPosition) ==
227229
vec_t{"iterations", "1234", "particles", "e", "position"});
228230

229-
auto speciesPositionX = speciesPosition["x"];
231+
auto speciesPositionX =
232+
speciesPosition["x"].resetDataset({Datatype::FLOAT, {10}});
230233
REQUIRE(
231234
pathOf(speciesPositionX) ==
232235
vec_t{"iterations", "1234", "particles", "e", "position", "x"});
@@ -237,7 +240,9 @@ TEST_CASE("myPath", "[core]")
237240
pathOf(speciesWeighting) ==
238241
vec_t{"iterations", "1234", "particles", "e", "weighting"});
239242

240-
auto speciesWeightingX = speciesWeighting[RecordComponent::SCALAR];
243+
auto speciesWeightingX =
244+
speciesWeighting[RecordComponent::SCALAR].resetDataset(
245+
{Datatype::FLOAT, {10}});
241246
REQUIRE(
242247
pathOf(speciesWeightingX) ==
243248
vec_t{"iterations", "1234", "particles", "e", "weighting"});
@@ -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{
@@ -292,6 +298,10 @@ TEST_CASE("myPath", "[core]")
292298
"e",
293299
"particlePatches",
294300
"numParticles"});
301+
302+
speciesE.particlePatches["offset"]["x"].resetDataset({Datatype::INT, {10}});
303+
speciesE.particlePatches["numParticlesOffset"][RecordComponent::SCALAR]
304+
.resetDataset({Datatype::INT, {10}});
295305
#endif
296306
}
297307

@@ -1095,6 +1105,7 @@ TEST_CASE("backend_via_json", "[core]")
10951105
{
10961106
Series series(
10971107
"../samples/optionsViaJson", Access::CREATE, encodingVariableBased);
1108+
series.iterations[0]; // v-based encoding requires at least 1 iteration
10981109
REQUIRE(series.backend() == "JSON");
10991110
REQUIRE(series.iterationEncoding() == IterationEncoding::variableBased);
11001111
}
@@ -1108,6 +1119,7 @@ TEST_CASE("backend_via_json", "[core]")
11081119
"../samples/optionsViaJson.bp",
11091120
Access::CREATE,
11101121
encodingVariableBased);
1122+
series.iterations[0]; // v-based encoding requires at least 1 iteration
11111123
REQUIRE(series.backend() == "JSON");
11121124
REQUIRE(series.iterationEncoding() == IterationEncoding::variableBased);
11131125
}

0 commit comments

Comments
 (0)