forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkXdmf3LightDataHandler.cxx
518 lines (482 loc) · 15.1 KB
/
vtkXdmf3LightDataHandler.cxx
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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkXdmf3LightDataHandler.cxx
Language: C++
Copyright (c) 1993-2002 Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
#include "vtkXdmf3LightDataHandler.h"
#include "vtkXdmf3SILBuilder.h"
#include "vtkXdmf3ArraySelection.h"
#include "vtksys/SystemTools.hxx"
#include "vtk_xdmf3.h"
#include VTKXDMF3_HEADER(XdmfAttribute.hpp)
#include VTKXDMF3_HEADER(XdmfAttributeCenter.hpp)
#include VTKXDMF3_HEADER(XdmfCurvilinearGrid.hpp)
#include VTKXDMF3_HEADER(XdmfDomain.hpp)
#include VTKXDMF3_HEADER(XdmfGraph.hpp)
#include VTKXDMF3_HEADER(XdmfGridCollection.hpp)
#include VTKXDMF3_HEADER(XdmfGridCollectionType.hpp)
#include VTKXDMF3_HEADER(XdmfGridController.hpp)
#include VTKXDMF3_HEADER(XdmfReader.hpp)
#include VTKXDMF3_HEADER(XdmfRectilinearGrid.hpp)
#include VTKXDMF3_HEADER(XdmfRegularGrid.hpp)
#include VTKXDMF3_HEADER(XdmfSet.hpp)
#include VTKXDMF3_HEADER(XdmfTime.hpp)
#include VTKXDMF3_HEADER(XdmfUnstructuredGrid.hpp)
#include VTKXDMF3_HEADER(core/XdmfVisitor.hpp)
#include <iostream>
#include <cassert>
//------------------------------------------------------------------------------
shared_ptr<vtkXdmf3LightDataHandler> vtkXdmf3LightDataHandler::New(
vtkXdmf3SILBuilder *sb,
vtkXdmf3ArraySelection* f,
vtkXdmf3ArraySelection* ce,
vtkXdmf3ArraySelection* pn,
vtkXdmf3ArraySelection* gc,
vtkXdmf3ArraySelection* sc,
unsigned int processor,
unsigned int nprocessors)
{
shared_ptr<vtkXdmf3LightDataHandler> p(new vtkXdmf3LightDataHandler());
p->SILBuilder = sb;
p->FieldArrays = f;
p->CellArrays = ce;
p->PointArrays = pn;
p->GridsCache = gc;
p->SetsCache = sc;
p->MaxDepth = 0;
p->Rank = processor;
p->NumProcs = nprocessors;
return p;
}
//------------------------------------------------------------------------------
vtkXdmf3LightDataHandler::~vtkXdmf3LightDataHandler() {}
//------------------------------------------------------------------------------
vtkXdmf3LightDataHandler::vtkXdmf3LightDataHandler()
{
SILBuilder = nullptr;
FieldArrays = nullptr;
CellArrays = nullptr;
PointArrays = nullptr;
GridsCache = nullptr;
SetsCache = nullptr;
MaxDepth = 0;
Rank = 0;
NumProcs = 0;
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::InspectXDMF
(shared_ptr<XdmfItem> item, vtkIdType parentVertex, unsigned int depth)
{
assert(this->SILBuilder);
assert(this->FieldArrays);
assert(this->PointArrays);
assert(this->CellArrays);
assert(this->GridsCache);
assert(this->SetsCache);
if (this->TooDeep(depth))
{
return;
}
this->InspectArrays(item);
this->InspectTime(item);
shared_ptr<XdmfDomain> coll = shared_dynamic_cast<XdmfDomain>(item);
if (!coll)
{
if (this->SILBuilder->IsMaxedOut())
{
return;
}
shared_ptr<XdmfGrid> grid = shared_dynamic_cast<XdmfGrid>(item);
if (grid)
{
shared_ptr<XdmfGridController> gcont = grid->getGridController();
if (gcont)
{
grid->read();
}
//atomic dataset
vtkIdType parent = parentVertex;
if (parentVertex == -1)
{
parent = this->SILBuilder->GetHierarchyRoot();
}
unsigned int nSets = grid->getNumberSets();
std::string name = grid->getName();
if (name.length() != 0 && (nSets > 0 || parentVertex != -1))
{
std::string uName = this->UniqueName(name, true);
grid->setName(uName);
this->AddNamedBlock(parent, name, uName);
for (unsigned int s = 0; s < nSets; s++)
{
shared_ptr<XdmfSet> set = grid->getSet(s);
std::string sName = set->getName();
std::string usName = this->UniqueName(sName, false);
set->setName(usName);
this->AddNamedSet(usName);
}
}
return;
}
shared_ptr<XdmfGraph> graph = shared_dynamic_cast<XdmfGraph>(item);
if (graph)
{
std::string name = graph->getName();
if (name.length() != 0 && parentVertex != -1)
{
std::string uName = this->UniqueName(name, true);
graph->setName(uName);
this->AddNamedBlock(parentVertex, name, uName);
}
return;
}
std::cerr << "Found unknown Xdmf data type" << std::endl;
return;
}
else
{
//four cases: domain, temporal, spatial or hierarchical
shared_ptr<XdmfGridCollection> asGC =
shared_dynamic_cast<XdmfGridCollection>(item);
bool isDomain = asGC?false:true;
if (asGC)
{
shared_ptr<XdmfGridController> gcont = asGC->getGridController();
if (gcont)
{
asGC->read();
}
}
bool isTemporal = false;
if (asGC && asGC->getType() == XdmfGridCollectionType::Temporal())
{
isTemporal = true;
}
vtkIdType silVertex = parentVertex;
if (!isTemporal && !isDomain)
{
std::string name = asGC->getName();
if (name.length() != 0 && !this->SILBuilder->IsMaxedOut())
{
silVertex = this->SILBuilder->AddVertex(name.c_str());
vtkIdType parent = parentVertex;
if (parentVertex == -1)
{
//topmost entry, we are the root
parent = this->SILBuilder->GetHierarchyRoot();
}
this->SILBuilder->AddChildEdge(parent, silVertex);
}
}
unsigned int nGridCollections = coll->getNumberGridCollections();
for (unsigned int i = 0; i < nGridCollections; i++)
{
if (isDomain && !this->ShouldRead(i, nGridCollections))
{
continue;
}
shared_ptr<XdmfGrid> child = coll->getGridCollection(i);
this->InspectXDMF(child, silVertex, depth+1);
}
for (unsigned int i = 0; i < coll->getNumberUnstructuredGrids(); i++)
{
shared_ptr<XdmfGrid> child = coll->getUnstructuredGrid(i);
this->InspectXDMF(child, silVertex, depth+1);
}
for (unsigned int i = 0; i < coll->getNumberRectilinearGrids(); i++)
{
shared_ptr<XdmfGrid> child = coll->getRectilinearGrid(i);
this->InspectXDMF(child, silVertex, depth+1);
}
for (unsigned int i = 0; i < coll->getNumberCurvilinearGrids(); i++)
{
shared_ptr<XdmfGrid> child = coll->getCurvilinearGrid(i);
this->InspectXDMF(child, silVertex, depth+1);
}
for (unsigned int i = 0; i < coll->getNumberRegularGrids(); i++)
{
shared_ptr<XdmfGrid> child = coll->getRegularGrid(i);
this->InspectXDMF(child, silVertex, depth+1);
}
for (unsigned int i = 0; i < coll->getNumberGraphs(); i++)
{
shared_ptr<XdmfGraph> child = coll->getGraph(i);
this->InspectXDMF(child, silVertex, depth+1);
}
}
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::ClearGridsIfNeeded(shared_ptr<XdmfItem> domain)
{
if (this->SILBuilder->IsMaxedOut())
{
//too numerous to be of use to user for manual selection, so clear out
this->GridsCache->clear();
this->SetsCache->clear();
this->SILBuilder->Initialize();
this->MaxDepth = 4;
this->InspectXDMF(domain, -1);
}
}
//------------------------------------------------------------------------------
std::set<double> vtkXdmf3LightDataHandler::getTimes()
{
return times;
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::InspectArrays(shared_ptr<XdmfItem> item)
{
shared_ptr<XdmfGrid> grid = shared_dynamic_cast<XdmfGrid>(item);
if (grid)
{
shared_ptr<XdmfGridController> gcont = grid->getGridController();
if (gcont)
{
grid->read();
}
for (unsigned int cc=0; cc < grid->getNumberAttributes(); cc++)
{
shared_ptr<XdmfAttribute> xmfAttribute = grid->getAttribute(cc);
std::string attrName = xmfAttribute->getName();
if (attrName.length() == 0)
{
std::cerr << "Skipping unnamed array." << std::endl;
continue;
}
shared_ptr<const XdmfAttributeCenter> attrCenter =
xmfAttribute->getCenter();
if (attrCenter == XdmfAttributeCenter::Grid()
||
attrCenter == XdmfAttributeCenter::Other())
{
if (!this->FieldArrays->HasArray(attrName.c_str()))
{
this->FieldArrays->AddArray(attrName.c_str());
}
}
else if (attrCenter == XdmfAttributeCenter::Cell())
{
if (!this->CellArrays->HasArray(attrName.c_str()))
{
this->CellArrays->AddArray(attrName.c_str());
}
}
else if (attrCenter == XdmfAttributeCenter::Node())
{
if (!this->PointArrays->HasArray(attrName.c_str()))
{
this->PointArrays->AddArray(attrName.c_str());
}
}
else
{
std::cerr << "Skipping " << attrName << " unrecognized association"
<< std::endl;
continue;
}
}
}
else
{
shared_ptr<XdmfGraph> graph = shared_dynamic_cast<XdmfGraph>(item);
if (graph)
{
for (unsigned int cc=0; cc < graph->getNumberAttributes(); cc++)
{
shared_ptr<XdmfAttribute> xmfAttribute = graph->getAttribute(cc);
std::string attrName = xmfAttribute->getName();
if (attrName.length() == 0)
{
std::cerr << "Skipping unnamed array." << std::endl;
continue;
}
shared_ptr<const XdmfAttributeCenter> attrCenter =
xmfAttribute->getCenter();
if (attrCenter == XdmfAttributeCenter::Grid())
{
if (!this->FieldArrays->HasArray(attrName.c_str()))
{
this->FieldArrays->AddArray(attrName.c_str());
}
}
else if (attrCenter == XdmfAttributeCenter::Edge())
{
if (!this->CellArrays->HasArray(attrName.c_str()))
{
this->CellArrays->AddArray(attrName.c_str());
}
}
else if (attrCenter == XdmfAttributeCenter::Node())
{
if (!this->PointArrays->HasArray(attrName.c_str()))
{
this->PointArrays->AddArray(attrName.c_str());
}
}
else
{
std::cerr << "Skipping " << attrName << " unrecognized association"
<< std::endl;
continue;
}
}
}
}
}
//------------------------------------------------------------------------------
bool vtkXdmf3LightDataHandler::TooDeep(unsigned int depth)
{
if (this->MaxDepth != 0 && depth >= this->MaxDepth)
{
return true;
}
return false;
}
//------------------------------------------------------------------------------
std::string vtkXdmf3LightDataHandler::UniqueName(const std::string& name, bool ForGrid)
{
std::string gridName = name;
unsigned int count=1;
vtkXdmf3ArraySelection* cache = (ForGrid?this->GridsCache:this->SetsCache);
while (cache->HasArray(gridName.c_str()))
{
std::ostringstream str;
str << name << "[" << count << "]";
gridName = str.str();
count++;
}
return gridName;
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::AddNamedBlock
(vtkIdType parentVertex, std::string originalName, std::string uniqueName)
{
this->GridsCache->AddArray(uniqueName.c_str());
vtkIdType silVertex = this->SILBuilder->AddVertex(uniqueName.c_str());
this->SILBuilder->AddChildEdge(this->SILBuilder->GetBlocksRoot(), silVertex);
vtkIdType hierarchyVertex = this->SILBuilder->AddVertex(originalName.c_str());
this->SILBuilder->AddChildEdge(parentVertex, hierarchyVertex);
this->SILBuilder->AddCrossEdge(hierarchyVertex, silVertex);
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::AddNamedSet
(std::string uniqueName)
{
this->SetsCache->AddArray(uniqueName.c_str());
}
//records times that xdmf grids supply data at
//if timespecs are only implied we add them to make things simpler later on
void vtkXdmf3LightDataHandler::InspectTime(shared_ptr<XdmfItem> item)
{
shared_ptr<XdmfGridCollection> gc =
shared_dynamic_cast<XdmfGridCollection>(item);
if (gc && gc->getType() == XdmfGridCollectionType::Temporal())
{
unsigned int cnt = 0;
for (unsigned int i = 0; i < gc->getNumberGridCollections(); i++)
{
shared_ptr<XdmfGrid> child = gc->getGridCollection(i);
this->GetSetTime(child, cnt);
}
for (unsigned int i = 0; i < gc->getNumberUnstructuredGrids(); i++)
{
shared_ptr<XdmfGrid> child = gc->getUnstructuredGrid(i);
this->GetSetTime(child, cnt);
}
for (unsigned int i = 0; i < gc->getNumberRectilinearGrids(); i++)
{
shared_ptr<XdmfGrid> child = gc->getRectilinearGrid(i);
this->GetSetTime(child, cnt);
}
for (unsigned int i = 0; i < gc->getNumberCurvilinearGrids(); i++)
{
shared_ptr<XdmfGrid> child = gc->getCurvilinearGrid(i);
this->GetSetTime(child, cnt);
}
for (unsigned int i = 0; i < gc->getNumberRegularGrids(); i++)
{
shared_ptr<XdmfGrid> child = gc->getRegularGrid(i);
this->GetSetTime(child, cnt);
}
for (unsigned int i = 0; gc->getNumberGraphs(); i++)
{
shared_ptr<XdmfGraph> child = gc->getGraph(i);
this->GetSetTime(child, cnt);
}
}
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::GetSetTime
(shared_ptr<XdmfGrid> child, unsigned int &cnt)
{
if (!child->getTime())
{
//grid collections without explicit times are implied to go 0...N
//so we add them here if not present
shared_ptr<XdmfTime> time = XdmfTime::New(cnt++);
child->setTime(time);
}
times.insert(child->getTime()->getValue());
}
//------------------------------------------------------------------------------
void vtkXdmf3LightDataHandler::GetSetTime
(shared_ptr<XdmfGraph> child, unsigned int &cnt)
{
if (!child->getTime())
{
//grid collections without explicit times are implied to go 0...N
//so we add them here if not present
shared_ptr<XdmfTime> time = XdmfTime::New(cnt++);
child->setTime(time);
}
times.insert(child->getTime()->getValue());
}
//------------------------------------------------------------------------------
bool vtkXdmf3LightDataHandler::ShouldRead
(unsigned int piece, unsigned int npieces)
{
if (this->NumProcs<1)
{
//no parallel information given to us, assume serial
return true;
}
if (npieces == 1)
{
return true;
}
if (npieces < this->NumProcs)
{
if (piece == this->Rank)
{
return true;
}
return false;
}
#if 1
unsigned int mystart = this->Rank*npieces/this->NumProcs;
unsigned int myend = (this->Rank+1)*npieces/this->NumProcs;
if (piece >= mystart)
{
if (piece < myend || (this->Rank==this->NumProcs-1))
{
return true;
}
}
return false;
#else
if ((piece % this->NumProcs) == this->Rank)
{
return true;
}
else
{
return false;
}
#endif
}