1 /*=========================================================================
2 
3   Program:   Visualization Toolkit
4   Module:    TestTRUCHASReader.cxx
5 
6   Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
7   All rights reserved.
8   See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
9 
10      This software is distributed WITHOUT ANY WARRANTY; without even
11      the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
12      PURPOSE.  See the above copyright notice for more information.
13 
14 =========================================================================*/
15 
16 // Input test to validate ability to read GE TRUCHAS files
17 #include "vtkCellData.h"
18 #include "vtkDoubleArray.h"
19 #include "vtkMultiBlockDataSet.h"
20 #include "vtkNew.h"
21 #include "vtkPointData.h"
22 #include "vtkTesting.h"
23 #include "vtkTestUtilities.h"
24 #include "vtkTRUCHASReader.h"
25 #include "vtkUnstructuredGrid.h"
26 
27 #include "vtkInformation.h"
28 #include "vtkStreamingDemandDrivenPipeline.h"
29 
30 namespace {
AE(double v1,double v2)31   bool AE(double v1, double v2)
32   {
33     if (fabs(v2-v1) > 0.001)
34     {
35       cerr << v2 << "!=" << v1 << endl;
36       return false;
37     }
38     return true;
39   }
40 }
41 
TestTRUCHASReader(int argc,char * argv[])42 int TestTRUCHASReader(int argc, char *argv[])
43 {
44   vtkNew<vtkTesting> testing;
45   testing->AddArguments(argc, argv);
46   char *fileName =
47     vtkTestUtilities::ExpandDataFileName(argc,argv,
48         "Data/TRUCHAS/viscoplastic-ring.h5");
49 
50   vtkNew<vtkTRUCHASReader> reader;
51   reader->SetFileName(fileName);
52   reader->UpdateInformation();
53   int nb = reader->GetNumberOfBlockArrays();
54   cerr << nb << " BLOCKS" << endl;
55   for (int b = 0; b < nb; b++)
56   {
57     cerr << "BLOCK ID " << b
58          << " named " << reader->GetBlockArrayName(b) << endl;
59   }
60   reader->SetBlockArrayStatus("1", 0); //block nums start at 1
61   reader->SetBlockArrayStatus("2", 1); //block nums start at 1
62   reader->SetBlockArrayStatus("3", 0); //block nums start at 1
63 
64   int nca = reader->GetNumberOfCellArrays();
65   cerr << nca << " CELL ARRAYS" << endl;
66   for (int a = 0; a < nca; a++)
67   {
68     cerr << "ARRAY " << a
69          << " named " << reader->GetCellArrayName(a) << endl;
70   }
71   cerr << "IGNORE VOF" << endl;
72   reader->SetCellArrayStatus("VOF", 0);
73 
74   int npa = reader->GetNumberOfPointArrays();
75   cerr << npa << " POINT ARRAYS" << endl;
76   for (int a = 0; a < npa; a++)
77   {
78     cerr << "ARRAY " << a
79          << " named " << reader->GetPointArrayName(a) << endl;
80   }
81   cerr << "IGNORE Displacement" << endl;
82   reader->SetPointArrayStatus("Displacement", 0);
83   reader->Update();
84 
85   vtkUnstructuredGrid *grid = vtkUnstructuredGrid::SafeDownCast
86     (reader->GetOutput()->GetBlock(1));
87   if (!grid)
88   {
89     cerr << "Could not open first block of known good file" << endl;
90     return EXIT_FAILURE;
91   }
92   int rnb = 0; //we produce empty blocks when deselected
93   for (int b = 0; b < nb; b++)
94   {
95   if (reader->GetOutput()->GetBlock(b))
96     {
97     rnb++;
98     }
99   }
100   if ( (rnb != 1) ||
101        (reader->GetOutput()->GetNumberOfBlocks() != 3) )
102   {
103     cerr << "Got unexpected number of blocks, found "
104          << rnb << "/" << reader->GetOutput()->GetNumberOfBlocks()
105          << " instead of "
106          << 1 << "/" << 3
107          << endl;
108   }
109 
110 
111   cerr << "---- CELL ARRAYS ----" << endl;
112   const int expectedNumCArrays = nca-1;
113   if (nca > 0 &&
114       grid->GetCellData()->GetNumberOfArrays() != expectedNumCArrays)
115   {
116     cerr << "Got unexpected number of cell arrays, found "
117          << grid->GetCellData()->GetNumberOfArrays()
118          << " instead of "
119          << expectedNumCArrays
120          << endl;
121     return EXIT_FAILURE;
122   }
123   for (int a = 0; a < grid->GetCellData()->GetNumberOfArrays(); a++)
124     {
125     vtkDataArray *da = grid->GetCellData()->GetArray(a);
126     cerr << da->GetName() << endl;
127     }
128 
129   cerr << "---- POINT ARRAYS ----" << endl;
130   const int expectedNumPArrays = npa-1;
131   if (npa > 0 &&
132       grid->GetPointData()->GetNumberOfArrays() != expectedNumPArrays)
133   {
134     cerr << "Got unexpected number of point arrays, found "
135          << grid->GetPointData()->GetNumberOfArrays()
136          << " instead of "
137          << expectedNumPArrays
138          << endl;
139     return EXIT_FAILURE;
140   }
141   for (int a = 0; a < grid->GetPointData()->GetNumberOfArrays(); a++)
142     {
143     vtkDataArray *da = grid->GetPointData()->GetArray(a);
144     cerr << da->GetName() << endl;
145     }
146 
147   const int expectedNumPoints = 496;
148   if (grid->GetNumberOfPoints()!=expectedNumPoints)
149   {
150     cerr << "Got unexpected number of points from file "
151          << grid->GetNumberOfPoints()
152          << " instead of "
153          << expectedNumPoints
154          << endl;
155     return EXIT_FAILURE;
156   }
157   const int expectedNumCells = 180;
158   if (grid->GetNumberOfCells()!=expectedNumCells)
159   {
160     cerr << "Got unexpected number of cells from file "
161          << grid->GetNumberOfCells()
162          << " instead of "
163          << expectedNumCells
164          << endl;
165     return EXIT_FAILURE;
166   }
167 
168   vtkDoubleArray *da = vtkDoubleArray::SafeDownCast
169     (grid->GetCellData()->GetArray("Grad_T"));
170   if (!da)
171     {
172     cerr << "Couldn't get " << "Grad_T" << " array" << endl;
173     return EXIT_FAILURE;
174     }
175 
176   double *ptr = da->GetTuple(42);
177   const double eVals[3] = {
178     -10.4436, -4.32586, -10.4913
179   };
180 
181   if (!AE(*ptr,eVals[0]) || !AE(*(ptr+1),eVals[1]) || !AE(*(ptr+2),eVals[2]))
182   {
183     cerr << "Got unexpected values from Grad_T array for cell 42 "
184          << *ptr << "," << *(ptr+1) << "," <<  *(ptr+2)
185          << " instead of "
186          << eVals[0] << "," << eVals[1] << "," << eVals[2]
187          << endl;
188     return EXIT_FAILURE;
189   }
190   reader->SetCellArrayStatus("VOF", 1);
191   reader->Update();
192   if (grid->GetCellData()->GetNumberOfArrays() != expectedNumCArrays+1)
193   {
194     cerr << "Got unexpected number of cell arrays, found "
195          << grid->GetCellData()->GetNumberOfArrays()
196          << " instead of "
197          << expectedNumCArrays+1
198          << endl;
199     return EXIT_FAILURE;
200   }
201 
202   vtkInformation *inf = reader->GetExecutive()->GetOutputInformation(0);
203   int numTimes = inf->Length(vtkStreamingDemandDrivenPipeline::TIME_STEPS());
204   cerr << "FOUND " << numTimes << " timesteps " << endl;
205   double tAlpha = inf->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), 0);
206   double tOmega = inf->Get(vtkStreamingDemandDrivenPipeline::TIME_STEPS(), numTimes-1);
207   const int expectedNumTimes = 2;
208   const double expectedMinT = 0.0;
209   const double expectedMaxT = 0.5;
210   if (numTimes != 2 || !AE(tAlpha, expectedMinT) || !AE(tOmega, expectedMaxT))
211   {
212     cerr << "Got unexpected times." << endl;
213     cerr << numTimes << " not " <<  expectedNumTimes << " times ";
214     cerr << tAlpha << " not " <<  expectedMinT << " first time";
215     cerr << tOmega << " not " <<  expectedMaxT << " last time";
216     return EXIT_FAILURE;
217   }
218   const int divs = 3;
219   const double expectedRanges[divs][2] =
220   {
221     {0,0}, //before
222     {0,0}, //after first
223     {-1.99025,-0.85729}, //after second
224   };
225 
226   for (int i = 0; i < divs; i++)
227   {
228     double tNext = tAlpha-0.1 + i*(tOmega-tAlpha)*2.5/divs;
229     reader->UpdateTimeStep(tNext);
230     grid = vtkUnstructuredGrid::SafeDownCast
231       (reader->GetOutput()->GetBlock(1));
232     da = vtkDoubleArray::SafeDownCast(grid->GetCellData()->GetArray("dTdt"));
233     double *mM = da->GetRange();
234     cerr << "ts " << i << ":" << tNext
235          << " got " << mM[0] << "," << mM[1] << endl;
236     if (!AE(mM[0], expectedRanges[i][0]) ||
237         !AE(mM[1], expectedRanges[i][1]))
238     {
239       cerr << "Got unexpected ranges at time " << tNext << " "
240            << mM[0] <<"," << mM[1] << " instead of "
241            << expectedRanges[i][0] << "," << expectedRanges[i][1] << endl;
242       return EXIT_FAILURE;
243     }
244   }
245 
246   return EXIT_SUCCESS;
247 }
248