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
    output = vtkDataObjectTypes::NewDataObject(vtk_type);
250
251
    outputVector->GetInformationObject(0)->Set(
        vtkDataObject::DATA_OBJECT(), output );
252
253
254
    this->GetOutputPortInformation(0)->Set(
      vtkDataObject::DATA_EXTENT_TYPE(), output->GetExtentType());
    output->Delete();
255
    }
256
  return 1;
257
}
258

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

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

271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
  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)
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
    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);
      }
314
    }
315

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

  // * Publish time information.
320
  std::vector<double> time_steps(domain->GetTimeSteps().begin(),
321
322
323
    domain->GetTimeSteps().end());

  if (time_steps.size() > 0)
324
    {
325
    outInfo->Set(vtkStreamingDemandDrivenPipeline::TIME_STEPS(),
326
327
328
329
330
331
332
333
      &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;
334
335
336
}

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

  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()))
353
    {
354
355
356
357
    updatePiece = static_cast<unsigned int>(
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_PIECE_NUMBER()));
    updateNumPieces =  static_cast<unsigned int>(
      outInfo->Get(vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_PIECES()));
358
    }
359
360
361
362
363
  if (outInfo->Has(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS()))
    {
    ghost_levels = outInfo->Get(
      vtkStreamingDemandDrivenPipeline::UPDATE_NUMBER_OF_GHOST_LEVELS());
364
365
    }

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

  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)
394
    {
395
396
    vtkErrorMacro("Failed to read data.");
    return 0;
397
    }
398

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

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

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

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

425
426
  // if no timestep was requested, just return what we read last.
  return this->LastTimeIndex;
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
//----------------------------------------------------------------------------
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;
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

//----------------------------------------------------------------------------
556
const char* vtkXdmfReader::GetSetName(int index)
Ken Martin's avatar
Ken Martin committed
557
{
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
  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
579
}
580
581

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