vtkXdmfReader.cxx 18.3 KB
Newer Older
1
/*=========================================================================
Jerry Clarke's avatar
Jerry Clarke committed
2

3
4
  Program:   Visualization Toolkit
  Module:    vtkXdmfReader.cxx
Jerry Clarke's avatar
Jerry Clarke committed
5

6
  Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
Berk Geveci's avatar
Berk Geveci committed
7
  All rights reserved.
8
  See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
Berk Geveci's avatar
Berk Geveci committed
9

10
11
12
     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.
Berk Geveci's avatar
Berk Geveci committed
13

14
=========================================================================*/
Andy Cedilnik's avatar
Andy Cedilnik committed
15
#include "vtkXdmfReader.h"
16
17
#include "vtkXdmfReaderInternal.h"
#include "vtkXdmfHeavyData.h"
18

19
20
21
#include "vtkCharArray.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkDataObjectTypes.h"
22
23
#include "vtkInformation.h"
#include "vtkInformationVector.h"
24
#include "vtkMultiBlockDataSet.h"
25
#include "vtkObjectFactory.h"
26
#include "vtkXMLParser.h"
27
28
29
30
31
32
33
34
35
36
37

//============================================================================
class vtkXdmfReaderTester : public vtkXMLParser
{
public:
  vtkTypeMacro(vtkXdmfReaderTester, vtkXMLParser);
  static vtkXdmfReaderTester* New();
  int TestReadFile()
    {
      this->Valid = 0;
      if(!this->FileName)
38
39
40
        {
        return 0;
        }
41
42
43

      ifstream inFile(this->FileName);
      if(!inFile)
44
        {
45
        return 0;
46
        }
47

48
49
50
51
52
53
54
55
56
57
58
59
      this->SetStream(&inFile);
      this->Done = 0;

      this->Parse();

      if(this->Done && this->Valid )
        {
        return 1;
        }
      return 0;
    }
  void StartElement(const char* name, const char**)
60
    {
61
62
63
64
65
      this->Done = 1;
      if(strcmp(name, "Xdmf") == 0)
        {
        this->Valid = 1;
        }
66
67
    }

68
69
protected:
  vtkXdmfReaderTester()
70
    {
71
72
      this->Valid = 0;
      this->Done = 0;
73
    }
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

private:
  void ReportStrayAttribute(const char*, const char*, const char*) {}
  void ReportMissingAttribute(const char*, const char*) {}
  void ReportBadAttribute(const char*, const char*, const char*) {}
  void ReportUnknownElement(const char*) {}
  void ReportXmlParseError() {}

  int ParsingComplete() { return this->Done; }
  int Valid;
  int Done;
  vtkXdmfReaderTester(const vtkXdmfReaderTester&); // Not implemented
  void operator=(const vtkXdmfReaderTester&); // Not implemented
};
vtkStandardNewMacro(vtkXdmfReaderTester);

90
vtkStandardNewMacro(vtkXdmfReader);
91
//----------------------------------------------------------------------------
92
vtkXdmfReader::vtkXdmfReader()
93
{
94
  this->DomainName = 0;
95
96
97
98
  this->Stride[0] = this->Stride[1] = this->Stride[2] = 1;
  this->XdmfDocument = new vtkXdmfDocument();
  this->LastTimeIndex = 0;
  this->SILUpdateStamp = 0;
99
100
101
102
103

  this->PointArraysCache = new vtkXdmfArraySelection;
  this->CellArraysCache = new vtkXdmfArraySelection;
  this->GridsCache = new vtkXdmfArraySelection;
  this->SetsCache = new vtkXdmfArraySelection;
104
105
106
}

//----------------------------------------------------------------------------
107
vtkXdmfReader::~vtkXdmfReader()
108
{
109
110
111
  this->SetDomainName(0);
  delete this->XdmfDocument;
  this->XdmfDocument = 0;
112
113
114
115
116

  delete this->PointArraysCache;
  delete this->CellArraysCache;
  delete this->GridsCache;
  delete this->SetsCache;
117
118
119
}

//----------------------------------------------------------------------------
120
int vtkXdmfReader::CanReadFile(const char* filename)
121
{
122
123
124
125
126
  vtkXdmfReaderTester* tester = vtkXdmfReaderTester::New();
  tester->SetFileName(filename);
  int res = tester->TestReadFile();
  tester->Delete();
  return res;
127
128
129
}

//----------------------------------------------------------------------------
130
int vtkXdmfReader::FillOutputPortInformation(int, vtkInformation *info)
131
{
132
133
  info->Set(vtkDataObject::DATA_TYPE_NAME(), "vtkDataObject");
  return 1;
134
135
136
}

//----------------------------------------------------------------------------
137
138
139
int vtkXdmfReader::ProcessRequest(vtkInformation *request,
    vtkInformationVector **inputVector,
    vtkInformationVector *outputVector)
140
{
141
142
143
144
145
  // create the output
  if (request->Has(vtkDemandDrivenPipeline::REQUEST_DATA_OBJECT()))
    {
    return this->RequestDataObject(outputVector);
    }
146

147
  return this->Superclass::ProcessRequest(request, inputVector, outputVector);
148
149
150
}

//----------------------------------------------------------------------------
151
bool vtkXdmfReader::PrepareDocument()
152
{
153
154
155
  // Calling this method repeatedly is okay. It does work only when something
  // has changed.
  if (this->GetReadFromInputString())
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
    const char* data=0;
    unsigned int data_length=0;
    if (this->InputArray)
      {
      data = this->InputArray->GetPointer(0);
      data_length = static_cast<unsigned int>(
        this->InputArray->GetNumberOfTuples()*
        this->InputArray->GetNumberOfComponents());
      }
    else if (this->InputString)
      {
      data = this->InputString;
      data_length = this->InputStringLength;
      }
    else
      {
      vtkErrorMacro("No input string specified");
      return false;
      }
    if (!this->XdmfDocument->ParseString(data, data_length))
      {
      vtkErrorMacro("Failed to parse xmf.");
      return false;
      }
181
    }
182
  else
183
    {
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
    // Parse the file...
    if (!this->FileName )
      {
      vtkErrorMacro("File name not set");
      return false;
      }

    // First make sure the file exists.  This prevents an empty file
    // from being created on older compilers.
    if (!vtksys::SystemTools::FileExists(this->FileName))
      {
      vtkErrorMacro("Error opening file " << this->FileName);
      return false;
      }

    if (!this->XdmfDocument->Parse(this->FileName))
      {
      vtkErrorMacro("Failed to parse xmf file: " << this->FileName);
      return false;
      }
204
    }
205

206
  if (this->DomainName)
207
    {
208
209
210
211
212
    if (!this->XdmfDocument->SetActiveDomain(this->DomainName))
      {
      vtkErrorMacro("Invalid domain: " << this->DomainName);
      return false;
      }
213
    }
214
215
  else
    {
216
    this->XdmfDocument->SetActiveDomain(static_cast<int>(0));
217
    }
218

219
220
221
222
223
224
  if (this->XdmfDocument->GetActiveDomain() &&
    this->XdmfDocument->GetActiveDomain()->GetSIL()->GetMTime() >
    this->GetMTime())
    {
    this->SILUpdateStamp++;
    }
225

226
227
  this->LastTimeIndex = 0; // reset time index when the file changes.
  return (this->XdmfDocument->GetActiveDomain() != 0);
228
229
230
}

//----------------------------------------------------------------------------
231
int vtkXdmfReader::RequestDataObject(vtkInformationVector *outputVector)
232
{
233
  if (!this->PrepareDocument())
234
    {
235
    return 0;
236
    }
237

238
239
240
  int vtk_type = this->XdmfDocument->GetActiveDomain()->GetVTKDataType();
  if (this->XdmfDocument->GetActiveDomain()->GetSetsSelection()->
     GetNumberOfArrays() > 0)
241
    {
242
243
    // if the data has any sets, then we are forced to using multiblock.
    vtk_type = VTK_MULTIBLOCK_DATA_SET;
244
    }
245
246
247

  vtkDataObject* output = vtkDataObject::GetData(outputVector, 0);
  if (!output || output->GetDataObjectType() != vtk_type)
248
    {
249
250
251
252
253
    output = vtkDataObjectTypes::NewDataObject(vtk_type);
    output->SetPipelineInformation(outputVector->GetInformationObject(0));
    this->GetOutputPortInformation(0)->Set(
      vtkDataObject::DATA_EXTENT_TYPE(), output->GetExtentType());
    output->Delete();
254
    }
255
  return 1;
256
}
257

258
//----------------------------------------------------------------------------
259
260
int vtkXdmfReader::RequestInformation(vtkInformation *, vtkInformationVector **,
  vtkInformationVector *outputVector)
261
{
262
  if (!this->PrepareDocument())
263
    {
264
    return 0;
265
    }
266

267
268
269
  // Pass any cached user-selections to the active domain.
  this->PassCachedSelections();

270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  vtkInformation* outInfo = outputVector->GetInformationObject(0);
  vtkXdmfDomain* domain = this->XdmfDocument->GetActiveDomain();

  // * Publish the fact that this reader can satisfy any piece request.
  outInfo->Set(vtkStreamingDemandDrivenPipeline::MAXIMUM_NUMBER_OF_PIECES(), -1);

  this->LastTimeIndex = this->ChooseTimeStep(outInfo);

  // Set the requested time-step on the domain. Thus, now when we go to get
  // information, we can (ideally) get information about that time-step.
  // this->XdmfDocument->GetActiveDomain()->SetTimeStep(this->LastTimeIndex);

  // * If producing structured dataset put information about whole extents etc.
  if (domain->GetNumberOfGrids() == 1 &&
    domain->IsStructured(domain->GetGrid(0)) &&
    domain->GetSetsSelection()->GetNumberOfArrays() == 0)
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
    XdmfGrid* xmfGrid = domain->GetGrid(0);
    // just in the case the top-level grid is a temporal collection, then pick
    // the sub-grid to fetch the extents etc.
    xmfGrid = domain->GetGrid(xmfGrid,
      domain->GetTimeForIndex(this->LastTimeIndex));
    int whole_extent[6];
    if (domain->GetWholeExtent(xmfGrid, whole_extent))
      {
      // re-scale the whole_extent using the stride.
      whole_extent[1] /= this->Stride[0];
      whole_extent[3] /= this->Stride[1];
      whole_extent[5] /= this->Stride[2];

      outInfo->Set(vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT(),
        whole_extent, 6);
      }
    double origin[3];
    double spacing[3];
    if (domain->GetOriginAndSpacing(xmfGrid, origin, spacing))
      {
      spacing[0] *= this->Stride[0];
      spacing[1] *= this->Stride[1];
      spacing[2] *= this->Stride[2];
      outInfo->Set(vtkDataObject::ORIGIN(), origin, 3);
      outInfo->Set(vtkDataObject::SPACING(), spacing, 3);
      }
313
    }
314

315
316
317
318
319
320
321
322
  // * Publish the SIL which provides information about the grid hierarchy.
  outInfo->Set(vtkDataObject::SIL(), domain->GetSIL());

  // * Publish time information.
  vtkstd::vector<double> time_steps(domain->GetTimeSteps().begin(),
    domain->GetTimeSteps().end());

  if (time_steps.size() > 0)
323
    {
324
325
326
327
328
329
330
331
332
    outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 
      &time_steps[0], static_cast<int>(time_steps.size()));
    double timeRange[2];
    timeRange[0] = time_steps.front();
    timeRange[1] = time_steps.back();
    outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_RANGE(), timeRange, 2);
    }

  return 1;
333
334
335
}

//----------------------------------------------------------------------------
336
337
int vtkXdmfReader::RequestData(vtkInformation *, vtkInformationVector **,
  vtkInformationVector *outputVector)
338
{
339
  if (!this->PrepareDocument())
340
    {
341
    return 0;
342
    }
343
344
345
346
347
348
349
350
351

  vtkInformation* outInfo = outputVector->GetInformationObject(0);

  // * Collect information about what part of the data is requested.
  unsigned int updatePiece = 0;
  unsigned int updateNumPieces = 1;
  int ghost_levels = 0;
  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()) &&
    outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()))
352
    {
353
354
355
356
    updatePiece = static_cast<unsigned int>(
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()));
    updateNumPieces =  static_cast<unsigned int>(
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()));
357
    }
358
359
360
361
362
  if (outInfo->Has(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS()))
    {
    ghost_levels = outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
363
364
    }

365
366
367
  // will be set for structured datasets only.
  int update_extent[6] = {0, -1, 0, -1, 0, -1};
  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT()))
368
    {
369
370
    outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
      update_extent);
371
    }
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392

  this->LastTimeIndex = this->ChooseTimeStep(outInfo);

  vtkXdmfHeavyData dataReader(this->XdmfDocument->GetActiveDomain(), this);
  dataReader.Piece = updatePiece;
  dataReader.NumberOfPieces = updateNumPieces;
  dataReader.GhostLevels = ghost_levels;
  dataReader.Extents[0] = update_extent[0]*this->Stride[0];
  dataReader.Extents[1] = update_extent[1]*this->Stride[0];
  dataReader.Extents[2] = update_extent[2]*this->Stride[1];
  dataReader.Extents[3] = update_extent[3]*this->Stride[1];
  dataReader.Extents[4] = update_extent[4]*this->Stride[2];
  dataReader.Extents[5] = update_extent[5]*this->Stride[2];
  dataReader.Stride[0] = this->Stride[0];
  dataReader.Stride[1] = this->Stride[1];
  dataReader.Stride[2] = this->Stride[2];
  dataReader.Time = this->XdmfDocument->GetActiveDomain()->GetTimeForIndex(
    this->LastTimeIndex);

  vtkDataObject* data = dataReader.ReadData();
  if (!data)
393
    {
394
395
    vtkErrorMacro("Failed to read data.");
    return 0;
396
    }
397

398
399
400
  vtkDataObject* output = vtkDataObject::GetData(outInfo);
  output->ShallowCopy(data);
  data->Delete();
Jerry Clarke's avatar
Jerry Clarke committed
401

402
403
  if (this->LastTimeIndex <
    this->XdmfDocument->GetActiveDomain()->GetTimeSteps().size())
404
    {
405
406
407
    double time = 
      this->XdmfDocument->GetActiveDomain()->GetTimeForIndex(this->LastTimeIndex);
    output->GetInformation()->Set(vtkDataObject::DATA_TIME_STEPS(), &time, 1);
408
    }
409
  return 1;
410
}
411

412
//----------------------------------------------------------------------------
413
int vtkXdmfReader::ChooseTimeStep(vtkInformation* outInfo)
414
{
415
  if (outInfo->Has(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS()))
416
    {
417
418
419
    // we do not support multiple timestep requests.
    double time =
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_TIME_STEPS())[0];
420

421
    return this->XdmfDocument->GetActiveDomain()->GetIndexForTime(time);
422
    }
423

424
425
  // if no timestep was requested, just return what we read last.
  return this->LastTimeIndex;
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
//----------------------------------------------------------------------------
vtkXdmfArraySelection* vtkXdmfReader::GetPointArraySelection()
{
  return this->XdmfDocument->GetActiveDomain()?
    this->XdmfDocument->GetActiveDomain()->GetPointArraySelection() :
    this->PointArraysCache;
}

//----------------------------------------------------------------------------
vtkXdmfArraySelection* vtkXdmfReader::GetCellArraySelection()
{
  return this->XdmfDocument->GetActiveDomain()?
    this->XdmfDocument->GetActiveDomain()->GetCellArraySelection() :
    this->CellArraysCache;
}

//----------------------------------------------------------------------------
vtkXdmfArraySelection* vtkXdmfReader::GetGridSelection()
{
  return this->XdmfDocument->GetActiveDomain()?
    this->XdmfDocument->GetActiveDomain()->GetGridSelection() :
    this->GridsCache;
}

//----------------------------------------------------------------------------
vtkXdmfArraySelection* vtkXdmfReader::GetSetsSelection()
{
  return this->XdmfDocument->GetActiveDomain()?
    this->XdmfDocument->GetActiveDomain()->GetSetsSelection() :
    this->SetsCache;
}

460
461
462
//----------------------------------------------------------------------------
int vtkXdmfReader::GetNumberOfGrids()
{
463
  return this->GetGridSelection()->GetNumberOfArrays();
464
465
466
}

//----------------------------------------------------------------------------
467
void vtkXdmfReader::SetGridStatus(const char* gridname, int status)
468
{
469
  this->GetGridSelection()->SetArrayStatus(gridname, status !=0);
470
  this->Modified();
471
}
472

473
//----------------------------------------------------------------------------
474
int vtkXdmfReader::GetGridStatus(const char* arrayname)
475
{
476
  return this->GetGridSelection()->GetArraySetting(arrayname);
477
478
479
}

//----------------------------------------------------------------------------
480
const char* vtkXdmfReader::GetGridName(int index)
481
{
482
  return this->GetGridSelection()->GetArrayName(index);
483
484
}

485
//----------------------------------------------------------------------------
486
int vtkXdmfReader::GetNumberOfPointArrays()
487
{
488
  return this->GetPointArraySelection()->GetNumberOfArrays();
489
490
491
}

//----------------------------------------------------------------------------
492
void vtkXdmfReader::SetPointArrayStatus(const char* arrayname, int status)
493
{
494
  this->GetPointArraySelection()->SetArrayStatus(arrayname, status != 0);
495
  this->Modified();
496
497
}

498
//----------------------------------------------------------------------------
499
int vtkXdmfReader::GetPointArrayStatus(const char* arrayname)
500
{
501
  return this->GetPointArraySelection()->GetArraySetting(arrayname);
502
503
504
}

//----------------------------------------------------------------------------
505
const char* vtkXdmfReader::GetPointArrayName(int index)
506
{
507
  return this->GetPointArraySelection()->GetArrayName(index);
508
509
510
}

//----------------------------------------------------------------------------
511
int vtkXdmfReader::GetNumberOfCellArrays()
512
{
513
  return this->GetCellArraySelection()->GetNumberOfArrays();
514
515
516
}

//----------------------------------------------------------------------------
517
void vtkXdmfReader::SetCellArrayStatus(const char* arrayname, int status)
518
{
519
  this->GetCellArraySelection()->SetArrayStatus(arrayname, status != 0);
520
  this->Modified();
521
522
523
}

//----------------------------------------------------------------------------
524
int vtkXdmfReader::GetCellArrayStatus(const char* arrayname)
525
{
526
  return this->GetCellArraySelection()->GetArraySetting(arrayname);
527
528
529
}

//----------------------------------------------------------------------------
530
const char* vtkXdmfReader::GetCellArrayName(int index)
Ken Martin's avatar
Ken Martin committed
531
{
532
  return this->GetCellArraySelection()->GetArrayName(index);
Jerry Clarke's avatar
Jerry Clarke committed
533
534
535
}

//----------------------------------------------------------------------------
536
int vtkXdmfReader::GetNumberOfSets()
Ken Martin's avatar
Ken Martin committed
537
{
538
  return this->GetSetsSelection()->GetNumberOfArrays();
Jerry Clarke's avatar
Jerry Clarke committed
539
}
540

Jerry Clarke's avatar
Jerry Clarke committed
541
//----------------------------------------------------------------------------
542
void vtkXdmfReader::SetSetStatus(const char* arrayname, int status)
Ken Martin's avatar
Ken Martin committed
543
{
544
  this->GetSetsSelection()->SetArrayStatus(arrayname, status != 0);
545
  this->Modified();
Jerry Clarke's avatar
Jerry Clarke committed
546
}
547

Jerry Clarke's avatar
Jerry Clarke committed
548
//----------------------------------------------------------------------------
549
int vtkXdmfReader::GetSetStatus(const char* arrayname)
Ken Martin's avatar
Ken Martin committed
550
{
551
  return this->GetSetsSelection()->GetArraySetting(arrayname);
Jerry Clarke's avatar
Jerry Clarke committed
552
553
554
}

//----------------------------------------------------------------------------
555
const char* vtkXdmfReader::GetSetName(int index)
Ken Martin's avatar
Ken Martin committed
556
{
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
  return this->GetSetsSelection()->GetArrayName(index);
}

//----------------------------------------------------------------------------
void vtkXdmfReader::PassCachedSelections()
{
  if (!this->XdmfDocument->GetActiveDomain())
    {
    return;
    }

  this->GetPointArraySelection()->Merge(*this->PointArraysCache);
  this->GetCellArraySelection()->Merge(*this->CellArraysCache);
  this->GetGridSelection()->Merge(*this->GridsCache);
  this->GetSetsSelection()->Merge(*this->SetsCache);

  // Clear the cache.
  this->PointArraysCache->clear();
  this->CellArraysCache->clear();
  this->GridsCache->clear();
  this->SetsCache->clear();
Jerry Clarke's avatar
Jerry Clarke committed
578
}
579
580

//----------------------------------------------------------------------------
581
void vtkXdmfReader::PrintSelf(ostream& os, vtkIndent indent)
582
{
583
  this->Superclass::PrintSelf(os, indent);
584
}
585
586
587
588
589
//----------------------------------------------------------------------------
vtkGraph* vtkXdmfReader::GetSIL()
{
  return this->XdmfDocument->GetActiveDomain()->GetSIL();
}