XdmfTestVTKIO.cxx 7.47 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
/*=========================================================================

  Program:   Visualization Toolkit
  Module:    XdmfTestVTKIO.cxx

  Copyright (c) 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.

=========================================================================*/
//Description:
16
//This tests vtkXdmfWriter and vtkXdmfReader
17
18
19
20
21
22
23
//It manufactures/reads a bunch of test data objects, writes them to disk 
//via the xdmfwriter, reads the files back in with the xdmfreader, and 
//compares the output of the reader against the input to the reader. If 
//differences are found it fails and stops immediately, leaving any failed
//files around for inspection. Otherwise it deletes the temporary files it
//creates and returns success.

24
#include "vtkXdmfWriter.h"
25
26
27
28
29
30
31
32
33
34
35
36
37

#include "vtkCellData.h"
#include "vtkDataObject.h"
#include "vtkDataObjectGenerator.h"
#include "vtkDataSet.h"
#include "vtkDataSetWriter.h"
#include "vtkFieldData.h"
#include "vtkPointData.h"
#include "vtkDataSetReader.h"
#include "vtksys/SystemTools.hxx"
//#include "vtkTemporalFractal.h"
//#include "vtkTestUtilities.h"
#include "vtkTimeSourceExample.h"
38
#include "vtkXdmfReader.h"
39

40
#define NUMTESTS 20
41
42
43

const char testobject[NUMTESTS][40] = {
    "ID1",
Dave Demarle's avatar
Dave Demarle committed
44
    "ID2",
45
46
47
48
    "UF1",
    "RG1",
    "SG1",
    "PD1",
49
    "PD2",
50
    "UG1",
Dave Demarle's avatar
Dave Demarle committed
51
52
    "UG2",
    "UG3",
53
    "UG4",
54
55
56
57
58
59
60
61
62
63
64
    "MB{}",
    "MB{ID1}",
    "MB{UF1}",
    "MB{RG1}",
    "MB{SG1}",
    "MB{PD1}",
    "MB{UG1}",
    "MB{ ID1 UF1 RG1 SG1 PD1 UG1 }",
    "HB[ (UF1)(UF1)(UF1) ]",
};

65
bool CleanUpGood = true;
66
67
68
69
70
71
72
73
74

bool DoFilesExist(const char*xdmffile, const char*hdf5file, bool deleteIfSo)
{
  bool xexists = (xdmffile?vtksys::SystemTools::FileExists(xdmffile):true);
  bool hexists = (hdf5file?vtksys::SystemTools::FileExists(hdf5file):true);
  bool xlenOK = (xdmffile?vtksys::SystemTools::FileLength(xdmffile)!=0:true);
  bool hlenOK = (hdf5file?vtksys::SystemTools::FileLength(hdf5file)!=0:true);

  bool theyDo = xexists && xlenOK && hexists && hlenOK;
75
76
77
78
79
  if (theyDo && deleteIfSo && CleanUpGood)
    {
    unlink(xdmffile);
    unlink(hdf5file);
    }
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96

  return theyDo;
}

bool DoDataObjectsDiffer(vtkDataObject *dobj1, vtkDataObject *dobj2)
{
  if (strcmp(dobj1->GetClassName(), dobj2->GetClassName()))
  {
    cerr << "Class name test failed " << dobj1->GetClassName() << " != " << dobj2->GetClassName() << endl;
    //return true; //disable for now
  }
  if (dobj1->GetFieldData()->GetNumberOfArrays() !=
      dobj2->GetFieldData()->GetNumberOfArrays())
  {
    cerr << "Number of field arrays test failed" << endl;
    return true;
  }
97
98
99
  if (!dobj1->IsA("vtkPolyData") &&
      !dobj1->IsA("vtkMultiBlockDataSet") &&
      dobj1->GetActualMemorySize()!=dobj2->GetActualMemorySize())
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
  {
    cerr << "Mem size test failed" << endl;
    return true;
  }
  vtkDataSet *ds1 = vtkDataSet::SafeDownCast(dobj1);
  vtkDataSet *ds2 = vtkDataSet::SafeDownCast(dobj2);
  if (ds1 && ds2)
  {
    if ((ds1->GetNumberOfCells() != ds2->GetNumberOfCells()) ||
        (ds1->GetNumberOfPoints() != ds2->GetNumberOfPoints()))
    {
      cerr << "Number of Cells/Points test failed" << endl;
      return true;
    }
    double *bds1 = ds1->GetBounds();
    double *bds2 = ds2->GetBounds();
    if ((bds1[0]!=bds2[0]) ||
        (bds1[1]!=bds2[1]) ||    
        (bds1[2]!=bds2[2]) ||
        (bds1[3]!=bds2[3]) ||
        (bds1[4]!=bds2[4]) ||
        (bds1[5]!=bds2[5]))
    {
      cerr << "Bounds test failed" << endl;
      return true;
    }
    if ((ds1->GetPointData()->GetNumberOfArrays() !=
        ds2->GetPointData()->GetNumberOfArrays()) ||
        (ds1->GetCellData()->GetNumberOfArrays() !=
         ds2->GetCellData()->GetNumberOfArrays()))
    {
     cerr << "Number of data arrays test failed" << endl;
     return true;
    }
    //TODO:Check array names, types, widths and ranges
  }
  return false;
}

bool TestXDMFConversion(vtkDataObject*input, char *prefix)
{
  char xdmffile[256];
  char hdf5file[256];
  char vtkfile[256];
  sprintf(xdmffile, "%s.xmf", prefix);
Dave Demarle's avatar
Dave Demarle committed
145
  sprintf(hdf5file, "%s.h5", prefix);
146
147
  sprintf(vtkfile, "%s.vtk", prefix);

148
  vtkXdmfWriter *xwriter = vtkXdmfWriter::New();
149
150
151
  xwriter->SetLightDataLimit(10000);
  xwriter->WriteAllTimeStepsOn();
  xwriter->SetFileName(xdmffile);
152
  xwriter->SetInputData(input);
153
154
155
156
157
158
159
160
  xwriter->Write();

  xwriter->Delete();
  vtkDataSet *ds = vtkDataSet::SafeDownCast(input);
  if (ds)
    {    
    vtkDataSetWriter *dsw = vtkDataSetWriter::New();
    dsw->SetFileName(vtkfile);
161
    dsw->SetInputData(ds);
162
163
164
165
166
167
168
169
170
171
172
    dsw->Write();
    dsw->Delete();
    }

  if (!DoFilesExist(xdmffile, NULL, false))
  {
    cerr << "Writer did not create " << xdmffile << endl;
    return true;
  }

  //TODO: Once it works, enable this
173
  vtkXdmfReader *xreader = vtkXdmfReader::New();
174
175
176
177
178
  xreader->SetFileName(xdmffile);
  xreader->Update();
  vtkDataObject *rOutput = xreader->GetOutputDataObject(0);

  bool fail = DoDataObjectsDiffer(input, rOutput);
179
  if (!fail && CleanUpGood)
180
181
  {
   //test passed!
182
183
184
   unlink(xdmffile);
   unlink(hdf5file);
   unlink(vtkfile);
185
186
187
188
189
190
  }

  xreader->Delete();
  return fail;
}

191
int main (int ac, char **av)
192
{
193
194
195
196
197
198
199
200
  
  for (int i = 1; i < ac; i++)
    {
    if (!strcmp(av[i], "--dont-clean"))
      {
      CleanUpGood = false;
      }
    }
201
202
203
204
205
206
207
208
209
210

  bool fail = false; 

  //TEST SET 1
  vtkDataObjectGenerator *dog = vtkDataObjectGenerator::New();
  int i = 0;
  while (!fail && i<NUMTESTS)
  {
    char filename[256];
    sprintf(filename, "xdmfIOtest_%d", i);
Dave Demarle's avatar
Dave Demarle committed
211
    cerr << "Test vtk object " << testobject[i] << endl;
212
213
214
215
216
217
218
219
220
221
222
223
224
225
    dog->SetProgram(testobject[i]);
    dog->Update();
    fail = TestXDMFConversion(dog->GetOutput(), filename);
    i++;
  }

  dog->Delete();

  if (fail)
  {
    return VTK_ERROR;
  }

  //TEST SET 2
Dave Demarle's avatar
Dave Demarle committed
226
  cerr << "Test temporal data" << endl;
227
228
229
230
  vtkTimeSourceExample *tsrc = vtkTimeSourceExample::New();
  tsrc->GrowingOn();
  tsrc->SetXAmplitude(2.0);

231
  vtkXdmfWriter *xwriter = vtkXdmfWriter::New();
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
  xwriter->SetLightDataLimit(10000);
  xwriter->WriteAllTimeStepsOn();
  xwriter->SetFileName("xdmfIOtest_temporal_1.xmf");
  xwriter->SetInputConnection(0,tsrc->GetOutputPort(0));
  xwriter->Write();

  xwriter->Delete();
  tsrc->Delete();

  fail = !DoFilesExist("xdmfIOtest_temporal_1.xmf", NULL, true);
  if (fail)
  {
    cerr << "Failed Temporal Test 1" << endl;
    return VTK_ERROR;
  }

#if 0
  if (!vtkTestUtilities::GetDataRoot(ac,av))
  {
      cerr << "NO DATA ROOT" << endl;
      return 0;
  }

  //TEST SET 3
  char* fname = 
   vtkTestUtilities::ExpandDataFileName(
    ac, av, "Data/RectGrid2.vtk");
  if (DoFilesExist(fname, NULL, false))
  {
    vtkDataSetReader *dsr = vtkDataSetReader::New();
    dsr->SetFileName(fname);
    dsr->Update();
    fail = TestXDMFConversion(dsr->GetOutput(), "xdmfIOtest_DSR_1");
    dsr->Delete();
    delete[] fname;
    if (fail)
    {
      cerr << "Failed DataSetReader Test 1" << endl;
      return VTK_ERROR;
    }

    dsr = vtkDataSetReader::New();
    fname = 
     vtkTestUtilities::ExpandDataFileName(
      ac, av, "Data/uGridEx.vtk");
    dsr->SetFileName(fname);
    dsr->Update();
    fail = TestXDMFConversion(dsr->GetOutput(), "xdmfIOtest_DSR_2");
    dsr->Delete();
    delete[] fname;
    if (fail)
    {
      cerr << "Failed DataSetReader Test 2" << endl;
      return VTK_ERROR;
    }
  }
  else
  {
    delete[] fname;
  }

#endif

  //ETC.
  return 0;
}