vtkXdmf3HeavyDataHandler.cxx 18.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    vtkXdmf3HeavyDataHandler.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 "vtkXdmf3HeavyDataHandler.h"

19
#include "vtkCompositeDataSet.h"
20 21
#include "vtkDataObject.h"
#include "vtkImageData.h"
22
#include "vtkInformation.h"
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
#include "vtkMultiBlockDataSet.h"
#include "vtkMutableDirectedGraph.h"
#include "vtkRectilinearGrid.h"
#include "vtkStructuredGrid.h"
#include "vtkUniformGrid.h"
#include "vtkUnstructuredGrid.h"
#include "vtkXdmf3ArrayKeeper.h"
#include "vtkXdmf3ArraySelection.h"
#include "vtkXdmf3DataSet.h"

#include "XdmfCurvilinearGrid.hpp"
#include "XdmfDomain.hpp"
#include "XdmfGraph.hpp"
#include "XdmfGrid.hpp"
#include "XdmfGridCollection.hpp"
#include "XdmfGridCollectionType.hpp"
#include "XdmfItem.hpp"
#include "XdmfRectilinearGrid.hpp"
#include "XdmfRegularGrid.hpp"
#include "XdmfSet.hpp"
#include "XdmfUnstructuredGrid.hpp"

//------------------------------------------------------------------------------
shared_ptr<vtkXdmf3HeavyDataHandler> vtkXdmf3HeavyDataHandler::New (
   vtkXdmf3ArraySelection *fs,
   vtkXdmf3ArraySelection *cs,
   vtkXdmf3ArraySelection *ps,
   vtkXdmf3ArraySelection *gc,
   vtkXdmf3ArraySelection *sc,
   unsigned int processor, unsigned int nprocessors,
   bool dt, double t,
   vtkXdmf3ArrayKeeper *keeper,
   bool asTime)
{
  shared_ptr<vtkXdmf3HeavyDataHandler> p(new vtkXdmf3HeavyDataHandler());
  p->FieldArrays = fs;
  p->CellArrays = cs;
  p->PointArrays = ps;
  p->GridsCache = gc;
  p->SetsCache = sc;
  p->Rank = processor;
  p->NumProcs = nprocessors;
  p->doTime = dt;
  p->time = t;
  p->Keeper = keeper;
  p->AsTime = asTime;
  return p;
}

//------------------------------------------------------------------------------
vtkXdmf3HeavyDataHandler::vtkXdmf3HeavyDataHandler()
{
}

//------------------------------------------------------------------------------
vtkXdmf3HeavyDataHandler::~vtkXdmf3HeavyDataHandler()
{
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::Populate(
  shared_ptr<XdmfItem> item, vtkDataObject *toFill)
{
  assert(toFill);

  shared_ptr<XdmfDomain> group = shared_dynamic_cast<XdmfDomain>(item);

  if (!group)
91
  {
92 93 94
    shared_ptr<XdmfUnstructuredGrid> unsGrid =
      shared_dynamic_cast<XdmfUnstructuredGrid>(item);
    if (unsGrid)
95
    {
96 97
      unsigned int nSets = unsGrid->getNumberSets();
      if (nSets > 0)
98
      {
99 100 101 102 103 104 105
        vtkMultiBlockDataSet *mbds =
          vtkMultiBlockDataSet::SafeDownCast(toFill);
        vtkUnstructuredGrid *child = vtkUnstructuredGrid::New();
        mbds->SetBlock
          (0,
           this->MakeUnsGrid
           (unsGrid, child, this->Keeper));
106 107
        mbds->GetMetaData((unsigned int)0)->Set(vtkCompositeDataSet::NAME(),
          unsGrid->getName().c_str());
108
        for (unsigned int i = 0; i < nSets; i++)
109
        {
110 111 112 113 114
          vtkUnstructuredGrid *sub = vtkUnstructuredGrid::New();
          mbds->SetBlock
            (i+1,
             this->ExtractSet
             (i, unsGrid, child, sub, this->Keeper));
115 116
          mbds->GetMetaData(i+1)->Set(vtkCompositeDataSet::NAME(),
            unsGrid->getSet(i)->getName().c_str());
117
          sub->Delete();
118
        }
119 120
        child->Delete();
        return mbds;
121
      }
122 123 124
      return this->MakeUnsGrid(unsGrid,
                               vtkUnstructuredGrid::SafeDownCast(toFill),
                               this->Keeper);
125
    }
126 127 128 129

    shared_ptr<XdmfRectilinearGrid> recGrid =
      shared_dynamic_cast<XdmfRectilinearGrid>(item);
    if (recGrid)
130
    {
131 132
      unsigned int nSets = recGrid->getNumberSets();
      if (nSets > 0)
133
      {
134 135 136 137 138 139 140
        vtkMultiBlockDataSet *mbds =
          vtkMultiBlockDataSet::SafeDownCast(toFill);
        vtkRectilinearGrid *child = vtkRectilinearGrid::New();
        mbds->SetBlock
          (0,
           this->MakeRecGrid
           (recGrid, child, this->Keeper));
141 142
        mbds->GetMetaData((unsigned int)0)->Set(vtkCompositeDataSet::NAME(),
          recGrid->getName().c_str());
143
        for (unsigned int i = 0; i < nSets; i++)
144
        {
145 146 147 148 149
          vtkUnstructuredGrid *sub = vtkUnstructuredGrid::New();
          mbds->SetBlock
            (i+1,
             this->ExtractSet
             (i, recGrid, child, sub, this->Keeper));
150 151
          mbds->GetMetaData(i+1)->Set(vtkCompositeDataSet::NAME(),
            recGrid->getSet(i)->getName().c_str());
152
          sub->Delete();
153
        }
154 155
        child->Delete();
        return mbds;
156
      }
157 158 159
      return this->MakeRecGrid(recGrid,
                               vtkRectilinearGrid::SafeDownCast(toFill),
                               this->Keeper);
160
    }
161 162 163 164

    shared_ptr<XdmfCurvilinearGrid> crvGrid =
      shared_dynamic_cast<XdmfCurvilinearGrid>(item);
    if (crvGrid)
165
    {
166 167
      unsigned int nSets = crvGrid->getNumberSets();
      if (nSets > 0)
168
      {
169 170 171 172 173 174 175
        vtkMultiBlockDataSet *mbds =
          vtkMultiBlockDataSet::SafeDownCast(toFill);
        vtkStructuredGrid *child = vtkStructuredGrid::New();
        mbds->SetBlock
          (0,
           this->MakeCrvGrid
           (crvGrid, child, this->Keeper));
176 177
        mbds->GetMetaData((unsigned int)0)->Set(vtkCompositeDataSet::NAME(),
          crvGrid->getName().c_str());
178
        for (unsigned int i = 0; i < nSets; i++)
179
        {
180 181 182 183 184
          vtkUnstructuredGrid *sub = vtkUnstructuredGrid::New();
          mbds->SetBlock
            (i+1,
             this->ExtractSet
             (i, crvGrid, child, sub, this->Keeper));
185 186
          mbds->GetMetaData(i+1)->Set(vtkCompositeDataSet::NAME(),
            crvGrid->getSet(i)->getName().c_str());
187
          sub->Delete();
188
        }
189 190
        child->Delete();
        return mbds;
191
      }
192 193 194
      return this->MakeCrvGrid(crvGrid,
                               vtkStructuredGrid::SafeDownCast(toFill),
                               this->Keeper);
195
    }
196 197 198 199

    shared_ptr<XdmfRegularGrid> regGrid =
      shared_dynamic_cast<XdmfRegularGrid>(item);
    if (regGrid)
200
    {
201 202
      unsigned int nSets = regGrid->getNumberSets();
      if (nSets > 0)
203
      {
204 205 206 207 208 209 210
        vtkMultiBlockDataSet *mbds =
          vtkMultiBlockDataSet::SafeDownCast(toFill);
        vtkImageData *child = vtkImageData::New();
        mbds->SetBlock
          (0,
           this->MakeRegGrid
           (regGrid, child, this->Keeper));
211 212
        mbds->GetMetaData((unsigned int)0)->Set(vtkCompositeDataSet::NAME(),
            regGrid->getName().c_str());
213
        for (unsigned int i = 0; i < nSets; i++)
214
        {
215 216 217 218 219
          vtkUnstructuredGrid *sub = vtkUnstructuredGrid::New();
          mbds->SetBlock
            (i+1,
             this->ExtractSet
             (i, regGrid, child, sub, this->Keeper));
220 221
          mbds->GetMetaData(i+1)->Set(vtkCompositeDataSet::NAME(),
            crvGrid->getSet(i)->getName().c_str());
222
          sub->Delete();
223
        }
224 225
        child->Delete();
        return mbds;
226
      }
227 228 229
      return this->MakeRegGrid(regGrid,
                               vtkImageData::SafeDownCast(toFill),
                               this->Keeper);
230
    }
231 232 233

    shared_ptr<XdmfGraph> graph = shared_dynamic_cast<XdmfGraph>(item);
    if (graph)
234
    {
235 236 237
      return this->MakeGraph(graph,
                             vtkMutableDirectedGraph::SafeDownCast(toFill),
                             this->Keeper);
238
    }
239 240

    return NULL; //already spit a warning out before this
241
  }
242 243 244 245 246 247

  shared_ptr<XdmfGridCollection> asGC =
    shared_dynamic_cast<XdmfGridCollection>(item);
  bool isDomain = asGC?false:true;
  bool isTemporal = false;
  if (asGC && asGC->getType() == XdmfGridCollectionType::Temporal())
248
  {
249
    isTemporal = true;
250
  }
251 252 253 254 255

  //ignore groups that are not in timestep we were asked for
  //but be sure to return everything within them
  bool lastTime = this->doTime;
  if (this->doTime && !(isDomain || isTemporal) && asGC->getTime())
256
  {
257
    if (asGC->getTime()->getValue() != this->time)
258
    {
259 260
      //don't return MB that doesn't match the requested time
      return NULL;
261
    }
262 263 264

    //inside a match, make sure we get everything underneath
    this->doTime = false;
265
  }
266 267 268 269 270 271 272

  vtkMultiBlockDataSet *topB = vtkMultiBlockDataSet::SafeDownCast(toFill);
  vtkDataObject *result;
  unsigned int cnt = 0;
  unsigned int nGridCollections = group->getNumberGridCollections();

  for (unsigned int i = 0; i < nGridCollections; i++)
273
  {
274
    if (!this->AsTime)
275
    {
276
      if (isDomain && !this->ShouldRead(i,nGridCollections))
277
      {
278 279
        topB->SetBlock(cnt++, NULL);
        continue;
280
      }
281 282 283 284
      vtkMultiBlockDataSet *child = vtkMultiBlockDataSet::New();
      result = this->Populate(group->getGridCollection(i), child);
      topB->SetBlock(cnt++, result);
      child->Delete();
285
    }
286
    else
287
    {
288 289 290
      vtkMultiBlockDataSet *child = vtkMultiBlockDataSet::New();
      result = this->Populate(group->getGridCollection(i), child);
      if (result)
291
      {
292 293
        topB->SetBlock(cnt++, result);
      }
294
      child->Delete();
295
    }
296
  }
297 298
  unsigned int nUnstructuredGrids = group->getNumberUnstructuredGrids();
  for (unsigned int i = 0; i < nUnstructuredGrids; i++)
299
  {
300
    if (this->AsTime && !isTemporal && !this->ShouldRead(i,nUnstructuredGrids))
301
    {
302
      topB->SetBlock(cnt++, NULL);
303
      continue;
304
    }
305 306 307 308
    shared_ptr<XdmfUnstructuredGrid> cGrid = group->getUnstructuredGrid(i);
    unsigned int nSets = cGrid->getNumberSets();
    vtkDataObject *child;
    if (nSets > 0)
309
    {
310
      child = vtkMultiBlockDataSet::New();
311
    }
312
    else
313
    {
314
      child = vtkUnstructuredGrid::New();
315
    }
316 317
    result = this->Populate(group->getUnstructuredGrid(i), child);
    if (result)
318
    {
319 320 321 322
      topB->SetBlock(cnt, result);
      topB->GetMetaData(cnt)->Set(vtkCompositeDataSet::NAME(),
        cGrid->getName().c_str());
      cnt++;
323
    }
324 325
    child->Delete();
  }
326 327
  unsigned int nRectilinearGrids = group->getNumberRectilinearGrids();
  for (unsigned int i = 0; i < nRectilinearGrids; i++)
328
  {
329
    if (this->AsTime && !isTemporal && !this->ShouldRead(i,nRectilinearGrids))
330
    {
331
      topB->SetBlock(cnt++, NULL);
332
      continue;
333
    }
334 335 336 337
    shared_ptr<XdmfRectilinearGrid> cGrid = group->getRectilinearGrid(i);
    unsigned int nSets = cGrid->getNumberSets();
    vtkDataObject *child;
    if (nSets > 0)
338
    {
339
      child = vtkMultiBlockDataSet::New();
340
    }
341
    else
342
    {
343
      child = vtkRectilinearGrid::New();
344
    }
345 346
    result = this->Populate(cGrid, child);
    if (result)
347
    {
348 349 350 351
      topB->SetBlock(cnt, result);
      topB->GetMetaData(cnt)->Set(vtkCompositeDataSet::NAME(),
        cGrid->getName().c_str());
      cnt++;
352
    }
353 354
    child->Delete();
  }
355 356
  unsigned int nCurvilinearGrids= group->getNumberCurvilinearGrids();
  for (unsigned int i = 0; i < nCurvilinearGrids; i++)
357
  {
358
    if (this->AsTime && !isTemporal && !this->ShouldRead(i,nCurvilinearGrids))
359
    {
360
      topB->SetBlock(cnt++, NULL);
361
      continue;
362
    }
363 364 365 366
    shared_ptr<XdmfCurvilinearGrid> cGrid = group->getCurvilinearGrid(i);
    unsigned int nSets = cGrid->getNumberSets();
    vtkDataObject *child;
    if (nSets > 0)
367
    {
368
      child = vtkMultiBlockDataSet::New();
369
    }
370
    else
371
    {
372
      child = vtkStructuredGrid::New();
373
    }
374 375
    result = this->Populate(cGrid, child);
    if (result)
376
    {
377 378 379 380
      topB->SetBlock(cnt, result);
      topB->GetMetaData(cnt)->Set(vtkCompositeDataSet::NAME(),
        cGrid->getName().c_str());
      cnt++;
381
    }
382 383
    child->Delete();
  }
384 385
  unsigned int nRegularGrids = group->getNumberRegularGrids();
  for (unsigned int i = 0; i < nRegularGrids; i++)
386
  {
387
    if (this->AsTime && !isTemporal && !this->ShouldRead(i,nRegularGrids))
388
    {
389
      topB->SetBlock(cnt++, NULL);
390
      continue;
391
    }
392 393 394 395
    shared_ptr<XdmfRegularGrid> cGrid = group->getRegularGrid(i);
    unsigned int nSets = cGrid->getNumberSets();
    vtkDataObject *child;
    if (nSets > 0)
396
    {
397
      child = vtkMultiBlockDataSet::New();
398
    }
399
    else
400
    {
401
      child = vtkUniformGrid::New();
402
    }
403 404
    result = this->Populate(cGrid, child);
    if (result)
405
    {
406 407 408 409
      topB->SetBlock(cnt, result);
      topB->GetMetaData(cnt)->Set(vtkCompositeDataSet::NAME(),
        cGrid->getName().c_str());
      cnt++;
410
    }
411 412
    child->Delete();
  }
413 414
  unsigned int nGraphs = group->getNumberGraphs();
  for (unsigned int i = 0; i < nGraphs; i++)
415
  {
416
    if (this->AsTime && !isTemporal && !this->ShouldRead(i,nGraphs))
417
    {
418
      topB->SetBlock(cnt++, NULL);
419
      continue;
420
    }
421 422 423
    vtkMutableDirectedGraph *child = vtkMutableDirectedGraph::New();
    result = this->Populate(group->getGraph(i), child);
    if (result)
424
    {
425 426 427 428
      topB->SetBlock(cnt, result);
      topB->GetMetaData(cnt)->Set(vtkCompositeDataSet::NAME(),
        group->getGraph(i)->getName().c_str());
      cnt++;
429
    }
430 431
    child->Delete();
  }
432 433

  if (lastTime)
434
  {
435 436
    //restore time search now that we've done the group contents
    this->doTime = true;
437
  }
438 439

  if (isTemporal && topB->GetNumberOfBlocks()==1)
440
  {
441 442
    //temporal collection is just a place holder for its content
    return topB->GetBlock(0);
443
  }
444

445
  return topB;
446 447 448 449 450 451 452
}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::ShouldRead(unsigned int piece,
                                          unsigned int npieces)
{
  if (this->NumProcs<1)
453
  {
454 455
    //no parallel information given to us, assume serial
    return true;
456
  }
457
  if (npieces == 1)
458
  {
459
    return true;
460
  }
461
  if (npieces < this->NumProcs)
462
  {
463
    if (piece == this->Rank)
464
    {
465 466
      return true;
    }
467 468
    return false;
  }
469 470 471 472 473

#if 1
  unsigned int mystart = this->Rank*npieces/this->NumProcs;
  unsigned int myend = (this->Rank+1)*npieces/this->NumProcs;
  if (piece >= mystart)
474
  {
475
    if (piece < myend || (this->Rank==this->NumProcs-1))
476
    {
477 478
      return true;
    }
479
  }
480 481 482
  return false;
#else
  if ((piece % this->NumProcs) == this->Rank)
483
  {
484
    return true;
485
  }
486
  else
487
  {
488
    return false;
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
#endif

}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::GridEnabled(shared_ptr<XdmfGrid> grid)
{
  return this->GridsCache->ArrayIsEnabled(grid->getName().c_str());
}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::GridEnabled(shared_ptr<XdmfGraph> graph)
{
  return this->GridsCache->ArrayIsEnabled(graph->getName().c_str());
}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::SetEnabled(shared_ptr<XdmfSet> set)
{
  return this->SetsCache->ArrayIsEnabled(set->getName().c_str());
}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::ForThisTime(shared_ptr<XdmfGrid> grid)
{
  return (!this->doTime ||
          (grid->getTime() &&
           grid->getTime()->getValue() == this->time));
}

//------------------------------------------------------------------------------
bool vtkXdmf3HeavyDataHandler::ForThisTime(shared_ptr<XdmfGraph> graph)
{
  return (!this->doTime ||
          (graph->getTime() &&
           graph->getTime()->getValue() == this->time));
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::MakeUnsGrid
 (
  shared_ptr<XdmfUnstructuredGrid> grid,
  vtkUnstructuredGrid *dataSet,
  vtkXdmf3ArrayKeeper *keeper)
{
535
  if (dataSet && this->GridEnabled(grid) && this->ForThisTime(grid))
536
  {
537 538 539 540 541
    vtkXdmf3DataSet::XdmfToVTK
      (
       this->FieldArrays, this->CellArrays, this->PointArrays,
       grid.get(), dataSet, keeper);
    return dataSet;
542
  }
543 544 545 546 547 548 549 550 551 552
  return NULL;
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::MakeRecGrid
  (
   shared_ptr<XdmfRectilinearGrid> grid,
   vtkRectilinearGrid *dataSet,
   vtkXdmf3ArrayKeeper *keeper)
{
553
  if (dataSet && this->GridEnabled(grid) && this->ForThisTime(grid))
554
  {
555 556 557 558 559
    vtkXdmf3DataSet::XdmfToVTK
      (
       this->FieldArrays, this->CellArrays, this->PointArrays,
       grid.get(), dataSet, keeper);
    return dataSet;
560
  }
561 562 563 564 565 566 567 568 569 570
  return NULL;
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::MakeCrvGrid
  (
   shared_ptr<XdmfCurvilinearGrid> grid,
   vtkStructuredGrid *dataSet,
   vtkXdmf3ArrayKeeper *keeper)
{
571
  if (dataSet && this->GridEnabled(grid) && this->ForThisTime(grid))
572
  {
573 574 575 576 577
    vtkXdmf3DataSet::XdmfToVTK
      (
       this->FieldArrays, this->CellArrays, this->PointArrays,
       grid.get(), dataSet, keeper);
    return dataSet;
578
  }
579 580 581 582 583 584 585 586 587 588
  return NULL;
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::MakeRegGrid
  (
   shared_ptr<XdmfRegularGrid> grid,
   vtkImageData *dataSet,
   vtkXdmf3ArrayKeeper *keeper)
{
589
  if (dataSet && this->GridEnabled(grid) && this->ForThisTime(grid))
590
  {
591 592 593 594 595
    vtkXdmf3DataSet::XdmfToVTK
      (
       this->FieldArrays, this->CellArrays, this->PointArrays,
       grid.get(), dataSet, keeper);
    return dataSet;
596
  }
597 598 599 600 601 602 603 604 605 606
  return NULL;
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::MakeGraph
  (
   shared_ptr<XdmfGraph> grid,
   vtkMutableDirectedGraph *dataSet,
   vtkXdmf3ArrayKeeper *keeper)
{
607
  if (dataSet && this->GridEnabled(grid) && this->ForThisTime(grid))
608
  {
609 610 611 612 613
    vtkXdmf3DataSet::XdmfToVTK
      (
       this->FieldArrays, this->CellArrays, this->PointArrays,
       grid.get(), dataSet, keeper);
    return dataSet;
614
  }
615 616 617 618 619 620 621 622 623 624 625
  return NULL;
}

//------------------------------------------------------------------------------
vtkDataObject *vtkXdmf3HeavyDataHandler::ExtractSet
  (
   unsigned int setnum, shared_ptr<XdmfGrid> grid,
   vtkDataSet *dataSet,
   vtkUnstructuredGrid *subSet, vtkXdmf3ArrayKeeper *keeper)
{
  shared_ptr<XdmfSet> set = grid->getSet(setnum);
626
  if (dataSet && subSet && SetEnabled(set) && this->ForThisTime(grid))
627
  {
628 629 630 631
    vtkXdmf3DataSet::XdmfSubsetToVTK
      (
       grid.get(), setnum, dataSet, subSet, keeper);
    return subSet;
632
  }
633 634
  return NULL;
}