-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathParticleSpecies.cpp
199 lines (182 loc) · 6.47 KB
/
ParticleSpecies.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* Copyright 2017-2021 Fabian Koller
*
* This file is part of openPMD-api.
*
* openPMD-api is free software: you can redistribute it and/or modify
* it under the terms of of either the GNU General Public License or
* the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* openPMD-api is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License and the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* and the GNU Lesser General Public License along with openPMD-api.
* If not, see <http://www.gnu.org/licenses/>.
*/
#include "openPMD/ParticleSpecies.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/DerefDynamicCast.hpp"
#include "openPMD/backend/Writable.hpp"
#include <algorithm>
#include <iostream>
namespace openPMD
{
ParticleSpecies::ParticleSpecies()
{
particlePatches.writable().ownKeyWithinParent = "particlePatches";
}
void ParticleSpecies::read()
{
/* obtain all non-scalar records */
Parameter<Operation::LIST_PATHS> pList;
IOHandler()->enqueue(IOTask(this, pList));
IOHandler()->flush(internal::defaultFlushParams);
internal::EraseStaleEntries<ParticleSpecies &> map{*this};
Parameter<Operation::OPEN_PATH> pOpen;
Parameter<Operation::LIST_ATTS> aList;
bool hasParticlePatches = false;
for (auto const &record_name : *pList.paths)
{
if (record_name == "particlePatches")
{
hasParticlePatches = true;
pOpen.path = "particlePatches";
IOHandler()->enqueue(IOTask(&particlePatches, pOpen));
try
{
particlePatches.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle patches and will skip them "
"due to read error:\n"
<< err.what() << std::endl;
hasParticlePatches = false;
}
}
else
{
Record &r = map[record_name];
pOpen.path = record_name;
aList.attributes->clear();
IOHandler()->enqueue(IOTask(&r, pOpen));
IOHandler()->enqueue(IOTask(&r, aList));
IOHandler()->flush(internal::defaultFlushParams);
auto att_begin = aList.attributes->begin();
auto att_end = aList.attributes->end();
auto value = std::find(att_begin, att_end, "value");
auto shape = std::find(att_begin, att_end, "shape");
if (value != att_end && shape != att_end)
{
RecordComponent &rc = r;
IOHandler()->enqueue(IOTask(&rc, pOpen));
IOHandler()->flush(internal::defaultFlushParams);
rc.get().m_isConstant = true;
}
try
{
r.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle record '" << record_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(record_name);
}
}
}
if (!hasParticlePatches)
{
auto &container = particlePatches.container();
container.erase("numParticles");
container.erase("numParticlesOffset");
particlePatches.setDirty(false);
}
/* obtain all scalar records */
Parameter<Operation::LIST_DATASETS> dList;
IOHandler()->enqueue(IOTask(this, dList));
IOHandler()->flush(internal::defaultFlushParams);
Parameter<Operation::OPEN_DATASET> dOpen;
for (auto const &record_name : *dList.datasets)
{
try
{
Record &r = map[record_name];
dOpen.name = record_name;
IOHandler()->enqueue(IOTask(&r, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
RecordComponent &rc = r;
IOHandler()->enqueue(IOTask(&rc, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
rc.setWritten(false, Attributable::EnqueueAsynchronously::No);
rc.resetDataset(Dataset(*dOpen.dtype, *dOpen.extent));
rc.setWritten(true, Attributable::EnqueueAsynchronously::No);
r.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle record '" << record_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(record_name);
//(*this)[record_name].erase(RecordComponent::SCALAR);
// this->erase(record_name);
}
}
readAttributes(ReadMode::FullyReread);
}
namespace
{
bool flushParticlePatches(ParticlePatches const &particlePatches)
{
return !particlePatches.empty();
}
} // namespace
void ParticleSpecies::flush(
std::string const &path, internal::FlushParams const &flushParams)
{
if (access::readOnly(IOHandler()->m_frontendAccess))
{
for (auto &record : *this)
record.second.flush(record.first, flushParams);
for (auto &patch : particlePatches)
patch.second.flush(patch.first, flushParams);
if (flushParams.flushLevel != FlushLevel::SkeletonOnly)
{
particlePatches.setDirty(false);
}
}
else
{
auto it = find("position");
if (it != end())
it->second.setUnitDimension({{UnitDimension::L, 1}});
it = find("positionOffset");
if (it != end())
it->second.setUnitDimension({{UnitDimension::L, 1}});
Container<Record>::flush(path, flushParams);
for (auto &record : *this)
record.second.flush(record.first, flushParams);
if (flushParticlePatches(particlePatches))
{
particlePatches.flush("particlePatches", flushParams);
for (auto &patch : particlePatches)
patch.second.flush(patch.first, flushParams);
}
else
{
particlePatches.setDirty(false);
}
}
if (flushParams.flushLevel != FlushLevel::SkeletonOnly)
{
setDirty(false);
}
}
} // namespace openPMD