|
| 1 | +#include <openPMD/openPMD.hpp> |
| 2 | + |
| 3 | +std::string backendEnding() |
| 4 | +{ |
| 5 | + auto extensions = openPMD::getFileExtensions(); |
| 6 | + if (auto it = std::find(extensions.begin(), extensions.end(), "toml"); |
| 7 | + it != extensions.end()) |
| 8 | + { |
| 9 | + return *it; |
| 10 | + } |
| 11 | + else |
| 12 | + { |
| 13 | + // Fallback for buggy old NVidia compiler |
| 14 | + return "json"; |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +void write() |
| 19 | +{ |
| 20 | + std::string config = R"( |
| 21 | +{ |
| 22 | + "iteration_encoding": "variable_based", |
| 23 | + "json": { |
| 24 | + "dataset": {"mode": "template"}, |
| 25 | + "attribute": {"mode": "short"} |
| 26 | + }, |
| 27 | + "toml": { |
| 28 | + "dataset": {"mode": "template"}, |
| 29 | + "attribute": {"mode": "short"} |
| 30 | + } |
| 31 | +} |
| 32 | +)"; |
| 33 | + |
| 34 | + openPMD::Series writeTemplate( |
| 35 | + "../samples/tomlTemplate." + backendEnding(), |
| 36 | + openPMD::Access::CREATE, |
| 37 | + config); |
| 38 | + auto iteration = writeTemplate.writeIterations()[0]; |
| 39 | + |
| 40 | + openPMD::Dataset ds{openPMD::Datatype::FLOAT, {5, 5}}; |
| 41 | + |
| 42 | + auto temperature = |
| 43 | + iteration.meshes["temperature"][openPMD::RecordComponent::SCALAR]; |
| 44 | + temperature.resetDataset(ds); |
| 45 | + |
| 46 | + auto E = iteration.meshes["E"]; |
| 47 | + E["x"].resetDataset(ds); |
| 48 | + E["y"].resetDataset(ds); |
| 49 | + /* |
| 50 | + * Don't specify datatype and extent for this one to indicate that this |
| 51 | + * information is not yet known. |
| 52 | + */ |
| 53 | + E["z"].resetDataset({}); |
| 54 | + |
| 55 | + ds.extent = {10}; |
| 56 | + |
| 57 | + auto electrons = iteration.particles["e"]; |
| 58 | + electrons["position"]["x"].resetDataset(ds); |
| 59 | + electrons["position"]["y"].resetDataset(ds); |
| 60 | + electrons["position"]["z"].resetDataset(ds); |
| 61 | + |
| 62 | + electrons["positionOffset"]["x"].resetDataset(ds); |
| 63 | + electrons["positionOffset"]["y"].resetDataset(ds); |
| 64 | + electrons["positionOffset"]["z"].resetDataset(ds); |
| 65 | + electrons["positionOffset"]["x"].makeConstant(3.14); |
| 66 | + electrons["positionOffset"]["y"].makeConstant(3.14); |
| 67 | + electrons["positionOffset"]["z"].makeConstant(3.14); |
| 68 | + |
| 69 | + ds.dtype = openPMD::determineDatatype<uint64_t>(); |
| 70 | + electrons.particlePatches["numParticles"][openPMD::RecordComponent::SCALAR] |
| 71 | + .resetDataset(ds); |
| 72 | + electrons |
| 73 | + .particlePatches["numParticlesOffset"][openPMD::RecordComponent::SCALAR] |
| 74 | + .resetDataset(ds); |
| 75 | + electrons.particlePatches["offset"]["x"].resetDataset(ds); |
| 76 | + electrons.particlePatches["offset"]["y"].resetDataset(ds); |
| 77 | + electrons.particlePatches["offset"]["z"].resetDataset(ds); |
| 78 | + electrons.particlePatches["extent"]["x"].resetDataset(ds); |
| 79 | + electrons.particlePatches["extent"]["y"].resetDataset(ds); |
| 80 | + electrons.particlePatches["extent"]["z"].resetDataset(ds); |
| 81 | +} |
| 82 | + |
| 83 | +void read() |
| 84 | +{ |
| 85 | + /* |
| 86 | + * The config is entirely optional, these things are also detected |
| 87 | + * automatically when reading |
| 88 | + */ |
| 89 | + |
| 90 | + // std::string config = R"( |
| 91 | + // { |
| 92 | + // "iteration_encoding": "variable_based", |
| 93 | + // "toml": { |
| 94 | + // "dataset": {"mode": "template"}, |
| 95 | + // "attribute": {"mode": "short"} |
| 96 | + // } |
| 97 | + // } |
| 98 | + // )"; |
| 99 | + |
| 100 | + openPMD::Series read( |
| 101 | + "../samples/tomlTemplate." + backendEnding(), |
| 102 | + openPMD::Access::READ_LINEAR); |
| 103 | + read.parseBase(); |
| 104 | + openPMD::helper::listSeries(read); |
| 105 | +} |
| 106 | + |
| 107 | +int main() |
| 108 | +{ |
| 109 | + write(); |
| 110 | + read(); |
| 111 | +} |
0 commit comments