Skip to content

Commit a00d87e

Browse files
committed
Fix bug with metadata set too late
1 parent 07d1096 commit a00d87e

4 files changed

Lines changed: 44 additions & 37 deletions

File tree

include/openPMD/Iteration.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,11 @@ class Iteration
291291
particles.visitHierarchy(v);
292292
}
293293

294+
// TODO: make this available on every class that inherits from
295+
// scientificdefaults for this, somehow make visitHierarchy a non-template,
296+
// so we can use it as a virtual function of attributable
297+
void populateDefaultMetadata();
298+
294299
Container<Mesh> meshes{};
295300
Container<ParticleSpecies> particles{}; // particleSpecies?
296301

@@ -370,8 +375,6 @@ class Iteration
370375
void readMeshes(std::string const &meshesPath);
371376
void readParticles(std::string const &particlesPath);
372377

373-
void setDefaultAttributes();
374-
375378
/**
376379
* Status after beginning an IO step. Currently includes:
377380
* * The advance status (OK, OVER, RANDOMACCESS)

src/Iteration.cpp

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Iteration &Iteration::close(bool _flush)
127127

128128
if (access::write(IOHandler()->m_frontendAccess))
129129
{
130-
setDefaultAttributes();
130+
populateDefaultMetadata();
131131
}
132132

133133
if (_flush)
@@ -161,40 +161,6 @@ Iteration &Iteration::close(bool _flush)
161161
return *this;
162162
}
163163

164-
void Iteration::setDefaultAttributes()
165-
{
166-
auto standard = IOHandler()->m_standard;
167-
visitHierarchy([standard](auto &component) {
168-
using ComponentType = std::remove_reference_t<decltype(component)>;
169-
if constexpr (auxiliary::IsTemplateBaseOf_v<BaseRecord, ComponentType>)
170-
{
171-
if (component.empty() && !component.datasetDefined())
172-
{
173-
std::cerr
174-
<< "Cannot flush Record without any contained components: '"
175-
<< component.myPath().openPMDPath() << "'. Will ignore.";
176-
if (component.written())
177-
{
178-
std::cerr
179-
<< "\n(Note: The Record seems to have been written "
180-
"previously?)";
181-
}
182-
std::cerr << std::endl;
183-
return;
184-
}
185-
}
186-
187-
if constexpr (
188-
!std::is_same_v<ComponentType, Container<Mesh>> &&
189-
!std::is_same_v<ComponentType, Container<Record>> &&
190-
!std::is_same_v<ComponentType, Container<PatchRecord>> &&
191-
!std::is_same_v<ComponentType, Container<ParticleSpecies>>)
192-
{
193-
component.writeDefaults(standard);
194-
}
195-
});
196-
}
197-
198164
Iteration &Iteration::open()
199165
{
200166
Series s = retrieveSeries();
@@ -278,6 +244,40 @@ bool Iteration::closedByWriter() const
278244
}
279245
}
280246

247+
void Iteration::populateDefaultMetadata()
248+
{
249+
auto standard = IOHandler()->m_standard;
250+
visitHierarchy([standard](auto &component) {
251+
using ComponentType = std::remove_reference_t<decltype(component)>;
252+
if constexpr (auxiliary::IsTemplateBaseOf_v<BaseRecord, ComponentType>)
253+
{
254+
if (component.empty() && !component.datasetDefined())
255+
{
256+
std::cerr
257+
<< "Cannot flush Record without any contained components: '"
258+
<< component.myPath().openPMDPath() << "'. Will ignore.";
259+
if (component.written())
260+
{
261+
std::cerr
262+
<< "\n(Note: The Record seems to have been written "
263+
"previously?)";
264+
}
265+
std::cerr << std::endl;
266+
return;
267+
}
268+
}
269+
270+
if constexpr (
271+
!std::is_same_v<ComponentType, Container<Mesh>> &&
272+
!std::is_same_v<ComponentType, Container<Record>> &&
273+
!std::is_same_v<ComponentType, Container<PatchRecord>> &&
274+
!std::is_same_v<ComponentType, Container<ParticleSpecies>>)
275+
{
276+
component.writeDefaults(standard);
277+
}
278+
});
279+
}
280+
281281
void Iteration::flushFileBased(
282282
std::string const &filename,
283283
IterationIndex_t i,

src/binding/python/Iteration.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ void init_Iteration(py::module &m)
9393
.def("set_time", &Iteration::setTime<double>)
9494
.def("set_dt", &Iteration::setDt<double>)
9595
.def("set_time_unit_SI", &Iteration::setTimeUnitSI)
96+
.def("populate_default_metadata", &Iteration::populateDefaultMetadata)
9697

9798
.def_property_readonly(
9899
"meshes",

test/python/unittest/API/APITest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2376,6 +2376,7 @@ def get_component_only():
23762376
mesh.axis_labels = ["x", "y"]
23772377
component.reset_dataset(io.Dataset(np.dtype("float"), [10, 10]))
23782378

2379+
iteration.populate_default_metadata()
23792380
del iteration
23802381
del mesh
23812382
del series
@@ -2423,6 +2424,7 @@ def get_component_only():
24232424
position.reset_dataset(
24242425
io.Dataset(np.dtype("float"), [num_particles]))
24252426

2427+
iteration.populate_default_metadata()
24262428
del iteration
24272429
del particles
24282430
del series
@@ -2474,6 +2476,7 @@ def get_component_only():
24742476
num_particles_comp.store(0, np.uint64(10))
24752477
num_particles_comp.store(1, np.uint64(20))
24762478

2479+
iteration.populate_default_metadata()
24772480
del iteration
24782481
del particles
24792482
del series

0 commit comments

Comments
 (0)