-
Notifications
You must be signed in to change notification settings - Fork 51
/
Iteration.cpp
920 lines (842 loc) · 26.3 KB
/
Iteration.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* 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/Iteration.hpp"
#include "openPMD/Dataset.hpp"
#include "openPMD/Datatype.hpp"
#include "openPMD/Series.hpp"
#include "openPMD/auxiliary/DerefDynamicCast.hpp"
#include "openPMD/auxiliary/Filesystem.hpp"
#include "openPMD/auxiliary/StringManip.hpp"
#include "openPMD/backend/Writable.hpp"
#include <exception>
#include <iostream>
#include <tuple>
namespace openPMD
{
using internal::CloseStatus;
using internal::DeferredParseAccess;
Iteration::Iteration() : Attributable{nullptr}
{
Attributable::setData(m_iterationData);
setTime(static_cast<double>(0));
setDt(static_cast<double>(1));
setTimeUnitSI(1);
meshes.writable().ownKeyWithinParent = {"meshes"};
particles.writable().ownKeyWithinParent = {"particles"};
}
template <typename T>
Iteration &Iteration::setTime(T newTime)
{
static_assert(
std::is_floating_point<T>::value,
"Type of attribute must be floating point");
setAttribute("time", newTime);
return *this;
}
template <typename T>
Iteration &Iteration::setDt(T newDt)
{
static_assert(
std::is_floating_point<T>::value,
"Type of attribute must be floating point");
setAttribute("dt", newDt);
return *this;
}
double Iteration::timeUnitSI() const
{
return getAttribute("timeUnitSI").get<double>();
}
Iteration &Iteration::setTimeUnitSI(double newTimeUnitSI)
{
setAttribute("timeUnitSI", newTimeUnitSI);
return *this;
}
using iterator_t = Container<Iteration, Iteration::IterationIndex_t>::iterator;
Iteration &Iteration::close(bool _flush)
{
auto &it = get();
StepStatus flag = getStepStatus();
// update close status
switch (it.m_closed)
{
case CloseStatus::Open:
case CloseStatus::ClosedInFrontend:
it.m_closed = CloseStatus::ClosedInFrontend;
break;
case CloseStatus::ClosedTemporarily:
// should we bother to reopen?
if (dirtyRecursive())
{
// let's reopen
it.m_closed = CloseStatus::ClosedInFrontend;
}
else
{
// don't reopen
it.m_closed = CloseStatus::ClosedInBackend;
}
break;
case CloseStatus::ParseAccessDeferred:
case CloseStatus::ClosedInBackend:
// just keep it like it is
// (this means that closing an iteration that has not been parsed
// yet keeps it re-openable)
break;
}
if (_flush)
{
if (flag == StepStatus::DuringStep)
{
endStep();
setStepStatus(StepStatus::NoStep);
}
else
{
// flush things manually
Series s = retrieveSeries();
// figure out my iteration number
auto begin = s.indexOf(*this);
auto end = begin;
++end;
s.flush_impl(begin, end, {FlushLevel::UserFlush});
}
}
else
{
if (flag == StepStatus::DuringStep)
{
throw std::runtime_error(
"Using deferred Iteration::close "
"unimplemented in auto-stepping mode.");
}
}
return *this;
}
Iteration &Iteration::open()
{
auto &it = get();
if (it.m_closed == CloseStatus::ParseAccessDeferred)
{
it.m_closed = CloseStatus::Open;
runDeferredParseAccess();
}
Series s = retrieveSeries();
// figure out my iteration number
auto begin = s.indexOf(*this);
s.openIteration(begin->first, *this);
IOHandler()->flush(internal::defaultFlushParams);
return *this;
}
bool Iteration::closed() const
{
switch (get().m_closed)
{
case CloseStatus::ParseAccessDeferred:
case CloseStatus::Open:
/*
* Temporarily closing a file is something that the openPMD API
* does for optimization purposes.
* Logically to the user, it is still open.
*/
case CloseStatus::ClosedTemporarily:
return false;
case CloseStatus::ClosedInFrontend:
case CloseStatus::ClosedInBackend:
return true;
}
throw std::runtime_error("Unreachable!");
}
bool Iteration::closedByWriter() const
{
using bool_type = unsigned char;
if (containsAttribute("closed"))
{
return getAttribute("closed").get<bool_type>() == 0u ? false : true;
}
else
{
return false;
}
}
void Iteration::flushFileBased(
std::string const &filename,
IterationIndex_t i,
internal::FlushParams const &flushParams)
{
/* Find the root point [Series] of this file,
* meshesPath and particlesPath are stored there */
Series s = retrieveSeries();
if (!written())
{
/* create file */
Parameter<Operation::CREATE_FILE> fCreate;
fCreate.name = filename;
IOHandler()->enqueue(IOTask(&s.writable(), fCreate));
/* create basePath */
Parameter<Operation::CREATE_PATH> pCreate;
pCreate.path = auxiliary::replace_first(s.basePath(), "%T/", "");
IOHandler()->enqueue(IOTask(&s.iterations, pCreate));
/* create iteration path */
pCreate.path = std::to_string(i);
IOHandler()->enqueue(IOTask(this, pCreate));
}
else
{
// operations for create mode
if ((IOHandler()->m_frontendAccess == Access::CREATE) &&
((IOHandler()->backendName() == "MPI_ADIOS1") ||
(IOHandler()->backendName() == "ADIOS1")))
{
Parameter<Operation::OPEN_FILE> fOpen;
fOpen.name = filename;
fOpen.encoding = IterationEncoding::fileBased;
IOHandler()->enqueue(IOTask(&s.writable(), fOpen));
flush(flushParams);
return;
}
// operations for read/read-write mode
/* open file */
s.openIteration(i, *this);
}
switch (flushParams.flushLevel)
{
case FlushLevel::CreateOrOpenFiles:
break;
case FlushLevel::SkeletonOnly:
case FlushLevel::InternalFlush:
case FlushLevel::UserFlush:
flush(flushParams);
break;
}
}
void Iteration::flushGroupBased(
IterationIndex_t i, internal::FlushParams const &flushParams)
{
if (!written())
{
/* create iteration path */
Parameter<Operation::CREATE_PATH> pCreate;
pCreate.path = std::to_string(i);
IOHandler()->enqueue(IOTask(this, pCreate));
}
switch (flushParams.flushLevel)
{
case FlushLevel::CreateOrOpenFiles:
break;
case FlushLevel::SkeletonOnly:
case FlushLevel::InternalFlush:
case FlushLevel::UserFlush:
flush(flushParams);
break;
}
}
void Iteration::flushVariableBased(
IterationIndex_t i, internal::FlushParams const &flushParams)
{
if (!written())
{
/* create iteration path */
Parameter<Operation::OPEN_PATH> pOpen;
pOpen.path = "";
IOHandler()->enqueue(IOTask(this, pOpen));
}
switch (flushParams.flushLevel)
{
case FlushLevel::CreateOrOpenFiles:
return;
case FlushLevel::SkeletonOnly:
case FlushLevel::InternalFlush:
case FlushLevel::UserFlush:
flush(flushParams);
break;
}
if (!written())
{
/* create iteration path */
Parameter<Operation::OPEN_PATH> pOpen;
pOpen.path = "";
IOHandler()->enqueue(IOTask(this, pOpen));
/*
* In v-based encoding, the snapshot attribute must always be written,
* so don't set the `changesOverSteps` flag of the IOTask here.
* Reason: Even in backends that don't support changing attributes,
* variable-based iteration encoding can be used to write one single
* iteration. Then, this attribute determines which iteration it is.
*/
this->setAttribute("snapshot", i);
}
}
void Iteration::flush(internal::FlushParams const &flushParams)
{
if (access::readOnly(IOHandler()->m_frontendAccess))
{
for (auto &m : meshes)
m.second.flush(m.first, flushParams);
for (auto &species : particles)
species.second.flush(species.first, flushParams);
}
else
{
/* Find the root point [Series] of this file,
* meshesPath and particlesPath are stored there */
Series s = retrieveSeries();
if (!meshes.empty() || s.containsAttribute("meshesPath"))
{
if (!s.containsAttribute("meshesPath"))
{
s.setMeshesPath("meshes/");
s.flushMeshesPath();
}
meshes.flush(s.meshesPath(), flushParams);
for (auto &m : meshes)
m.second.flush(m.first, flushParams);
}
else
{
meshes.dirty() = false;
}
if (!particles.empty() || s.containsAttribute("particlesPath"))
{
if (!s.containsAttribute("particlesPath"))
{
s.setParticlesPath("particles/");
s.flushParticlesPath();
}
particles.flush(s.particlesPath(), flushParams);
for (auto &species : particles)
species.second.flush(species.first, flushParams);
}
else
{
particles.dirty() = false;
}
flushAttributes(flushParams);
}
}
void Iteration::deferParseAccess(DeferredParseAccess dr)
{
get().m_deferredParseAccess =
std::make_optional<DeferredParseAccess>(std::move(dr));
}
void Iteration::reread(std::string const &path)
{
if (get().m_deferredParseAccess.has_value())
{
throw std::runtime_error(
"[Iteration] Internal control flow error: Trying to reread an "
"iteration that has not yet been read for its first time.");
}
read_impl(path);
}
void Iteration::readFileBased(
std::string filePath, std::string const &groupPath, bool doBeginStep)
{
if (doBeginStep)
{
/*
* beginStep() must take care to open files
*/
beginStep(/* reread = */ false);
}
auto series = retrieveSeries();
series.readOneIterationFileBased(filePath);
get().m_overrideFilebasedFilename = filePath;
read_impl(groupPath);
}
void Iteration::readGorVBased(std::string const &groupPath, bool doBeginStep)
{
if (doBeginStep)
{
/*
* beginStep() must take care to open files
*/
beginStep(/* reread = */ false);
}
read_impl(groupPath);
}
void Iteration::read_impl(std::string const &groupPath)
{
Parameter<Operation::OPEN_PATH> pOpen;
pOpen.path = groupPath;
IOHandler()->enqueue(IOTask(this, pOpen));
using DT = Datatype;
Parameter<Operation::READ_ATT> aRead;
aRead.name = "dt";
IOHandler()->enqueue(IOTask(this, aRead));
IOHandler()->flush(internal::defaultFlushParams);
if (*aRead.dtype == DT::FLOAT)
setDt(Attribute(*aRead.resource).get<float>());
else if (*aRead.dtype == DT::DOUBLE)
setDt(Attribute(*aRead.resource).get<double>());
else if (*aRead.dtype == DT::LONG_DOUBLE)
setDt(Attribute(*aRead.resource).get<long double>());
// conversion cast if a backend reports an integer type
else if (auto val = Attribute(*aRead.resource).getOptional<double>();
val.has_value())
setDt(val.value());
else
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'dt' (expected double, found " +
datatypeToString(Attribute(*aRead.resource).dtype) + ")");
aRead.name = "time";
IOHandler()->enqueue(IOTask(this, aRead));
IOHandler()->flush(internal::defaultFlushParams);
if (*aRead.dtype == DT::FLOAT)
setTime(Attribute(*aRead.resource).get<float>());
else if (*aRead.dtype == DT::DOUBLE)
setTime(Attribute(*aRead.resource).get<double>());
else if (*aRead.dtype == DT::LONG_DOUBLE)
setTime(Attribute(*aRead.resource).get<long double>());
// conversion cast if a backend reports an integer type
else if (auto val = Attribute(*aRead.resource).getOptional<double>();
val.has_value())
setTime(val.value());
else
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'time' (expected double, "
"found " +
datatypeToString(Attribute(*aRead.resource).dtype) + ")");
aRead.name = "timeUnitSI";
IOHandler()->enqueue(IOTask(this, aRead));
IOHandler()->flush(internal::defaultFlushParams);
if (auto val = Attribute(*aRead.resource).getOptional<double>();
val.has_value())
setTimeUnitSI(val.value());
else
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::UnexpectedContent,
{},
"Unexpected Attribute datatype for 'timeUnitSI' (expected double, "
"found " +
datatypeToString(Attribute(*aRead.resource).dtype) + ")");
/* Find the root point [Series] of this file,
* meshesPath and particlesPath are stored there */
Series s = retrieveSeries();
Parameter<Operation::LIST_PATHS> pList;
std::string version = s.openPMD();
bool hasMeshes = false;
bool hasParticles = false;
if (version == "1.0.0" || version == "1.0.1")
{
IOHandler()->enqueue(IOTask(this, pList));
IOHandler()->flush(internal::defaultFlushParams);
hasMeshes = std::count(
pList.paths->begin(),
pList.paths->end(),
auxiliary::replace_last(s.meshesPath(), "/", "")) == 1;
hasParticles =
std::count(
pList.paths->begin(),
pList.paths->end(),
auxiliary::replace_last(s.particlesPath(), "/", "")) == 1;
pList.paths->clear();
}
else
{
hasMeshes = s.containsAttribute("meshesPath");
hasParticles = s.containsAttribute("particlesPath");
}
if (hasMeshes)
{
try
{
readMeshes(s.meshesPath());
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read meshes in iteration " << groupPath
<< " and will skip them due to read error:\n"
<< err.what() << std::endl;
meshes = {};
meshes.dirty() = false;
}
}
else
{
meshes.dirty() = false;
}
if (hasParticles)
{
try
{
readParticles(s.particlesPath());
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particles in iteration " << groupPath
<< " and will skip them due to read error:\n"
<< err.what() << std::endl;
particles = {};
particles.dirty() = false;
}
}
else
{
particles.dirty() = false;
}
readAttributes(ReadMode::FullyReread);
#ifdef openPMD_USE_INVASIVE_TESTS
if (containsAttribute("__openPMD_internal_fail"))
{
throw error::ReadError(
error::AffectedObject::Attribute,
error::Reason::Other,
{},
"Deliberately failing this iteration for testing purposes");
}
#endif
}
void Iteration::readMeshes(std::string const &meshesPath)
{
Parameter<Operation::OPEN_PATH> pOpen;
Parameter<Operation::LIST_PATHS> pList;
pOpen.path = meshesPath;
IOHandler()->enqueue(IOTask(&meshes, pOpen));
meshes.readAttributes(ReadMode::FullyReread);
internal::EraseStaleEntries<decltype(meshes)> map{meshes};
/* obtain all non-scalar meshes */
IOHandler()->enqueue(IOTask(&meshes, pList));
IOHandler()->flush(internal::defaultFlushParams);
Parameter<Operation::LIST_ATTS> aList;
for (auto const &mesh_name : *pList.paths)
{
Mesh &m = map[mesh_name];
pOpen.path = mesh_name;
aList.attributes->clear();
IOHandler()->enqueue(IOTask(&m, pOpen));
IOHandler()->enqueue(IOTask(&m, 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)
{
MeshRecordComponent &mrc = m[MeshRecordComponent::SCALAR];
mrc.parent() = m.parent();
IOHandler()->enqueue(IOTask(&mrc, pOpen));
IOHandler()->flush(internal::defaultFlushParams);
mrc.get().m_isConstant = true;
}
m.read();
try
{
m.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read mesh with name '" << mesh_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(mesh_name);
}
}
/* obtain all scalar meshes */
Parameter<Operation::LIST_DATASETS> dList;
IOHandler()->enqueue(IOTask(&meshes, dList));
IOHandler()->flush(internal::defaultFlushParams);
Parameter<Operation::OPEN_DATASET> dOpen;
for (auto const &mesh_name : *dList.datasets)
{
Mesh &m = map[mesh_name];
dOpen.name = mesh_name;
IOHandler()->enqueue(IOTask(&m, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
MeshRecordComponent &mrc = m[MeshRecordComponent::SCALAR];
mrc.parent() = m.parent();
IOHandler()->enqueue(IOTask(&mrc, dOpen));
IOHandler()->flush(internal::defaultFlushParams);
mrc.written() = false;
mrc.resetDataset(Dataset(*dOpen.dtype, *dOpen.extent));
mrc.written() = true;
try
{
m.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read mesh with name '" << mesh_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(mesh_name);
}
}
}
void Iteration::readParticles(std::string const &particlesPath)
{
Parameter<Operation::OPEN_PATH> pOpen;
Parameter<Operation::LIST_PATHS> pList;
pOpen.path = particlesPath;
IOHandler()->enqueue(IOTask(&particles, pOpen));
particles.readAttributes(ReadMode::FullyReread);
/* obtain all particle species */
pList.paths->clear();
IOHandler()->enqueue(IOTask(&particles, pList));
IOHandler()->flush(internal::defaultFlushParams);
internal::EraseStaleEntries<decltype(particles)> map{particles};
for (auto const &species_name : *pList.paths)
{
ParticleSpecies &p = map[species_name];
pOpen.path = species_name;
IOHandler()->enqueue(IOTask(&p, pOpen));
IOHandler()->flush(internal::defaultFlushParams);
try
{
p.read();
}
catch (error::ReadError const &err)
{
std::cerr << "Cannot read particle species with name '"
<< species_name
<< "' and will skip it due to read error:\n"
<< err.what() << std::endl;
map.forget(species_name);
}
}
}
auto Iteration::beginStep(bool reread) -> BeginStepStatus
{
BeginStepStatus res;
auto series = retrieveSeries();
return beginStep({*this}, series, reread);
}
auto Iteration::beginStep(
std::optional<Iteration> thisObject,
Series &series,
bool reread,
std::set<IterationIndex_t> const &ignoreIterations) -> BeginStepStatus
{
BeginStepStatus res;
using IE = IterationEncoding;
// Initialize file with this to quiet warnings
// The following switch is comprehensive
internal::AttributableData *file = nullptr;
switch (series.iterationEncoding())
{
case IE::fileBased:
if (thisObject.has_value())
{
file = &static_cast<Attributable &>(*thisObject).get();
}
else
{
throw error::Internal(
"Advancing a step in file-based iteration encoding is "
"iteration-specific.");
}
break;
case IE::groupBased:
case IE::variableBased:
file = &series.get();
break;
}
AdvanceStatus status;
if (thisObject.has_value())
{
status = series.advance(
AdvanceMode::BEGINSTEP,
*file,
series.indexOf(*thisObject),
*thisObject);
}
else
{
status = series.advance(AdvanceMode::BEGINSTEP);
}
switch (status)
{
case AdvanceStatus::OVER:
res.stepStatus = status;
return res;
case AdvanceStatus::OK:
case AdvanceStatus::RANDOMACCESS:
break;
}
// re-read -> new datasets might be available
auto IOHandl = series.IOHandler();
if (reread && status != AdvanceStatus::RANDOMACCESS &&
(series.iterationEncoding() == IE::groupBased ||
series.iterationEncoding() == IE::variableBased) &&
access::read(series.IOHandler()->m_frontendAccess))
{
bool previous = series.iterations.written();
series.iterations.written() = false;
auto oldStatus = IOHandl->m_seriesStatus;
IOHandl->m_seriesStatus = internal::SeriesStatus::Parsing;
try
{
res.iterationsInOpenedStep = series.readGorVBased(
/* do_always_throw_errors = */ true,
/* init = */ false,
ignoreIterations);
}
catch (...)
{
IOHandl->m_seriesStatus = oldStatus;
throw;
}
IOHandl->m_seriesStatus = oldStatus;
series.iterations.written() = previous;
}
res.stepStatus = status;
return res;
}
void Iteration::endStep()
{
using IE = IterationEncoding;
auto series = retrieveSeries();
// Initialize file with this to quiet warnings
// The following switch is comprehensive
internal::AttributableData *file = nullptr;
switch (series.iterationEncoding())
{
case IE::fileBased:
file = &Attributable::get();
break;
case IE::groupBased:
case IE::variableBased:
file = &series.get();
break;
}
// @todo filebased check
series.advance(AdvanceMode::ENDSTEP, *file, series.indexOf(*this), *this);
series.get().m_currentlyActiveIterations.clear();
}
StepStatus Iteration::getStepStatus()
{
Series s = retrieveSeries();
switch (s.iterationEncoding())
{
using IE = IterationEncoding;
case IE::fileBased:
return get().m_stepStatus;
case IE::groupBased:
case IE::variableBased:
return s.get().m_stepStatus;
default:
throw std::runtime_error("[Iteration] unreachable");
}
}
void Iteration::setStepStatus(StepStatus status)
{
Series s = retrieveSeries();
switch (s.iterationEncoding())
{
using IE = IterationEncoding;
case IE::fileBased:
get().m_stepStatus = status;
break;
case IE::groupBased:
case IE::variableBased:
s.get().m_stepStatus = status;
break;
default:
throw std::runtime_error("[Iteration] unreachable");
}
}
bool Iteration::dirtyRecursive() const
{
if (dirty())
{
return true;
}
if (particles.dirty() || meshes.dirty())
{
return true;
}
for (auto const &pair : particles)
{
if (pair.second.dirtyRecursive())
{
return true;
}
}
for (auto const &pair : meshes)
{
if (pair.second.dirtyRecursive())
{
return true;
}
}
return false;
}
void Iteration::linkHierarchy(Writable &w)
{
Attributable::linkHierarchy(w);
meshes.linkHierarchy(this->writable());
particles.linkHierarchy(this->writable());
}
void Iteration::runDeferredParseAccess()
{
if (access::read(IOHandler()->m_frontendAccess))
{
auto &it = get();
if (!it.m_deferredParseAccess.has_value())
{
return;
}
auto const &deferred = it.m_deferredParseAccess.value();
auto oldStatus = IOHandler()->m_seriesStatus;
IOHandler()->m_seriesStatus = internal::SeriesStatus::Parsing;
try
{
if (deferred.fileBased)
{
readFileBased(
deferred.filename, deferred.path, deferred.beginStep);
}
else
{
readGorVBased(deferred.path, deferred.beginStep);
}
}
catch (...)
{
// reset this thing
it.m_deferredParseAccess = std::optional<DeferredParseAccess>();
IOHandler()->m_seriesStatus = oldStatus;
throw;
}
// reset this thing
it.m_deferredParseAccess = std::optional<DeferredParseAccess>();
IOHandler()->m_seriesStatus = oldStatus;
}
}
template float Iteration::time<float>() const;
template double Iteration::time<double>() const;
template long double Iteration::time<long double>() const;
template float Iteration::dt<float>() const;
template double Iteration::dt<double>() const;
template long double Iteration::dt<long double>() const;
template Iteration &Iteration::setTime<float>(float time);
template Iteration &Iteration::setTime<double>(double time);
template Iteration &Iteration::setTime<long double>(long double time);
template Iteration &Iteration::setDt<float>(float dt);
template Iteration &Iteration::setDt<double>(double dt);
template Iteration &Iteration::setDt<long double>(long double dt);
} // namespace openPMD