forked from openPMD/openPMD-api
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initialize Series attributes and datasets from template
- Loading branch information
1 parent
8ca52da
commit cc89024
Showing
6 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#pragma once | ||
|
||
#include "openPMD/Series.hpp" | ||
|
||
namespace openPMD::auxiliary | ||
{ | ||
// @todo replace uint64_t with proper type after merging #1285 | ||
Series &initializeFromTemplate( | ||
Series &initializeMe, Series const &fromTemplate, uint64_t iteration); | ||
} // namespace openPMD::auxiliary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
#include "openPMD/auxiliary/TemplateFile.hpp" | ||
#include "openPMD/DatatypeHelpers.hpp" | ||
|
||
#include <iostream> | ||
|
||
namespace openPMD::auxiliary | ||
{ | ||
namespace | ||
{ | ||
// Some forward declarations | ||
template <typename T> | ||
void initializeFromTemplate( | ||
Container<T> &initializeMe, Container<T> const &fromTemplate); | ||
|
||
struct SetAttribute | ||
{ | ||
template <typename T> | ||
static void | ||
call(Attributable &object, std::string const &name, Attribute attr) | ||
{ | ||
object.setAttribute(name, attr.get<T>()); | ||
} | ||
|
||
template <unsigned n> | ||
static void call(Attributable &, std::string const &name, Attribute) | ||
{ | ||
std::cerr << "Unknown datatype for template attribute '" << name | ||
<< "'. Will skip it." << std::endl; | ||
} | ||
}; | ||
|
||
void copyAttributes( | ||
Attributable &target, | ||
Attributable const &source, | ||
std::vector<std::string> ignore = {}) | ||
{ | ||
auto shouldBeIgnored = [&ignore](std::string const &attrName) { | ||
// `ignore` is empty by default and normally has only a handful of | ||
// entries otherwise. | ||
// So just use linear search. | ||
for (auto const &ignored : ignore) | ||
{ | ||
if (attrName == ignored) | ||
{ | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
|
||
for (auto const &attrName : source.attributes()) | ||
{ | ||
if (shouldBeIgnored(attrName)) | ||
{ | ||
continue; | ||
} | ||
auto attr = source.getAttribute(attrName); | ||
auto dtype = attr.dtype; | ||
switchType<SetAttribute>(dtype, target, attrName, std::move(attr)); | ||
} | ||
} | ||
|
||
void initializeFromTemplate( | ||
BaseRecordComponent &initializeMe, | ||
BaseRecordComponent const &fromTemplate) | ||
{ | ||
copyAttributes(initializeMe, fromTemplate); | ||
} | ||
|
||
void initializeFromTemplate( | ||
RecordComponent &initializeMe, RecordComponent const &fromTemplate) | ||
{ | ||
if (fromTemplate.getDatatype() != Datatype::UNDEFINED) | ||
{ | ||
initializeMe.resetDataset( | ||
Dataset{fromTemplate.getDatatype(), fromTemplate.getExtent()}); | ||
} | ||
initializeFromTemplate( | ||
static_cast<BaseRecordComponent &>(initializeMe), | ||
static_cast<BaseRecordComponent const &>(fromTemplate)); | ||
} | ||
|
||
void initializeFromTemplate( | ||
PatchRecordComponent &initializeMe, | ||
PatchRecordComponent const &fromTemplate) | ||
{ | ||
if (fromTemplate.getDatatype() != Datatype::UNDEFINED) | ||
{ | ||
initializeMe.resetDataset( | ||
Dataset{fromTemplate.getDatatype(), fromTemplate.getExtent()}); | ||
} | ||
initializeFromTemplate( | ||
static_cast<BaseRecordComponent &>(initializeMe), | ||
static_cast<BaseRecordComponent const &>(fromTemplate)); | ||
} | ||
|
||
void initializeFromTemplate( | ||
ParticleSpecies &initializeMe, ParticleSpecies const &fromTemplate) | ||
{ | ||
if (!fromTemplate.particlePatches.empty()) | ||
{ | ||
initializeFromTemplate( | ||
static_cast<Container<PatchRecord> &>( | ||
initializeMe.particlePatches), | ||
static_cast<Container<PatchRecord> const &>( | ||
fromTemplate.particlePatches)); | ||
} | ||
initializeFromTemplate( | ||
static_cast<Container<Record> &>(initializeMe), | ||
static_cast<Container<Record> const &>(fromTemplate)); | ||
} | ||
|
||
template <typename T> | ||
void initializeFromTemplate( | ||
Container<T> &initializeMe, Container<T> const &fromTemplate) | ||
{ | ||
copyAttributes(initializeMe, fromTemplate); | ||
for (auto const &pair : fromTemplate) | ||
{ | ||
initializeFromTemplate(initializeMe[pair.first], pair.second); | ||
} | ||
} | ||
|
||
void initializeFromTemplate( | ||
Iteration &initializeMe, Iteration const &fromTemplate) | ||
{ | ||
copyAttributes(initializeMe, fromTemplate, {"snapshot"}); | ||
if (fromTemplate.hasMeshes()) | ||
{ | ||
initializeFromTemplate(initializeMe.meshes, fromTemplate.meshes); | ||
} | ||
if (fromTemplate.hasParticles()) | ||
{ | ||
initializeFromTemplate( | ||
initializeMe.particles, fromTemplate.particles); | ||
} | ||
} | ||
} // namespace | ||
|
||
Series &initializeFromTemplate( | ||
Series &initializeMe, Series const &fromTemplate, uint64_t iteration) | ||
{ | ||
if (!initializeMe.containsAttribute("from_template")) | ||
{ | ||
copyAttributes( | ||
initializeMe, | ||
fromTemplate, | ||
{"basePath", "iterationEncoding", "iterationFormat", "openPMD"}); | ||
initializeMe.setAttribute("from_template", fromTemplate.name()); | ||
} | ||
|
||
uint64_t sourceIteration = iteration; | ||
if (!fromTemplate.iterations.contains(sourceIteration)) | ||
{ | ||
if (fromTemplate.iterations.empty()) | ||
{ | ||
std::cerr << "Template file has no iterations, will only fill in " | ||
"global attributes." | ||
<< std::endl; | ||
return initializeMe; | ||
} | ||
else | ||
{ | ||
sourceIteration = fromTemplate.iterations.begin()->first; | ||
} | ||
} | ||
|
||
initializeFromTemplate( | ||
initializeMe.iterations[iteration], | ||
fromTemplate.iterations.at(sourceIteration)); | ||
return initializeMe; | ||
} | ||
} // namespace openPMD::auxiliary |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters