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

  Program:   Visualization Toolkit
  Module:    TestAMRUtilities.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.

=========================================================================*/

#include <iostream>
17
#include <sstream>
18
19
20
21
22
23
24
25
26
27
#include <cassert>
#include <mpi.h>

#include "vtkAMRUtilities.h"
#include "vtkAMRBox.h"
#include "vtkHierarchicalBoxDataSet.h"
#include "vtkMultiProcessController.h"
#include "vtkMPIController.h"
#include "vtkUniformGrid.h"
#include "vtkImageToStructuredGrid.h"
28
#include "vtkStructuredGridWriter.h"
29
#include "vtkXMLHierarchicalBoxDataWriter.h"
30

31
//-----------------------------------------------------------------------------
32
//           H E L P E R   M E T H O D S  &   M A C R O S
33
//-----------------------------------------------------------------------------
34

35
#define CHECK_TEST( P, testName, rval ) {                                 \
36
37
38
  if( !P ) {                                                              \
    std::cerr << "ERROR:" << testName << " FAILED!\n";                    \
    std::cerr << "Location:" << __FILE__ << ":" << __LINE__ << std::endl; \
39
    std::cerr.flush();                                                    \
40
    ++rval;                                                               \
41
42
  }                                                                       \
}
43

44
45
46
47
48
49
50
51
52
#define CHECK_CONDITION( P, shortMessage, status ) {                      \
  if( !P ) {                                                              \
    std::cerr << "ERROR:" << shortMessage << std::endl;                   \
    std::cerr << "Location: " << __FILE__ << ":" << __LINE__ << std::endl;\
    status = 0;                                                           \
    return status;                                                        \
  }                                                                       \
}

53
54
55
56
57
58
59
60
61
62
63
64
//-----------------------------------------------------------------------------
// Description:
// Write the AMR data in XML
void WriteAMRData(
    vtkHierarchicalBoxDataSet *myAMRData,
    vtkMultiProcessController *controller )
{
  // Sanity check
  assert( "AMR dataset is NULL" && (myAMRData != NULL) );

  vtkXMLHierarchicalBoxDataWriter *myAMRWriter =
      vtkXMLHierarchicalBoxDataWriter::New();
George Zagaris's avatar
George Zagaris committed
65
66
67
68
69

  std::ostringstream oss;
  oss << "AMR_PROCESS_" << controller->GetLocalProcessId() << "."
      << myAMRWriter->GetDefaultFileExtension();

70
  myAMRWriter->SetFileName( oss.str().c_str() );
George Zagaris's avatar
George Zagaris committed
71
  myAMRWriter->SetInputData( myAMRData );
George Zagaris's avatar
George Zagaris committed
72
  myAMRWriter->Write();
73
  myAMRWriter->Delete();
George Zagaris's avatar
George Zagaris committed
74
  controller->Barrier();
75
76
}

77
//-----------------------------------------------------------------------------
78
79
// Description:
// Gets the grid for the given process
80
void WriteUniformGrid( vtkUniformGrid *myGrid, std::string prefix )
81
82
83
84
85
{
  assert( "Input Grid is not NULL" && (myGrid != NULL) );

  vtkImageToStructuredGrid* myImage2StructuredGridFilter =
      vtkImageToStructuredGrid::New();
86
  assert( "Cannot create Image2StructuredGridFilter" &&
87
88
          (myImage2StructuredGridFilter != NULL) );

George Zagaris's avatar
George Zagaris committed
89
  myImage2StructuredGridFilter->SetInputData( myGrid );
90
  myImage2StructuredGridFilter->Update();
91
92
  vtkStructuredGrid* myStructuredGrid =
      myImage2StructuredGridFilter->GetOutput();
93
94
95
96
97
  assert( "Structured Grid output is NULL!" &&
          (myStructuredGrid != NULL) );

  vtkStructuredGridWriter *myWriter = vtkStructuredGridWriter::New();
  myWriter->SetFileName( prefix.c_str() );
George Zagaris's avatar
George Zagaris committed
98
  myWriter->SetInputData( myStructuredGrid );
99
100
101
  myWriter->Update();
  myWriter->Delete();
  myImage2StructuredGridFilter->Delete();
102
103
}

104
105
106
107
108
109
110
111
112
113
114
115
116
117
//-----------------------------------------------------------------------------
// Description:
// Gets the grid for the given process
int CheckMetaData( vtkHierarchicalBoxDataSet *myAMRData )
{

  int status = 1;
  vtkAMRBox myBox;

  // STEP 0: Check metadata @(0,0)
  if( myAMRData->GetMetaData( 0, 0, myBox ) == 1 )
    {
       CHECK_CONDITION((myBox.GetBlockId()==0),"BlockId mismatch", status );
       CHECK_CONDITION((myBox.GetLevel()==0),"Level mismatch", status);
118
119
120
121
122
123
124
       CHECK_CONDITION((myBox.GetProcessId()==0),"Process ID mismatch",status);

       int lo[3]; int hi[3];
       myBox.GetLoCorner( lo );
       myBox.GetHiCorner( hi );
       CHECK_CONDITION((lo[0]==0),"LoCorner mismatch",status);
       CHECK_CONDITION((lo[1]==0),"LoCorner mismatch",status);
125
      /* CHECK_CONDITION((lo[2]==0),"LoCorner mismatch",status); */
126
127
       CHECK_CONDITION((hi[0]==2),"HiCorner mismatch",status);
       CHECK_CONDITION((hi[1]==2),"HiCorner mismatch",status);
128
      /* CHECK_CONDITION((hi[2]==0),"HiCorner mismatch",status); */
129
130
131
132
133

       double spacing[3];
       myBox.GetGridSpacing( spacing );
       CHECK_CONDITION((spacing[0]==1.0),"Check grid spacing",status);
       CHECK_CONDITION((spacing[1]==1.0),"Check grid spacing",status);
134
      /* CHECK_CONDITION((spacing[2]==1.0),"Check grid spacing",status); */
135
136
137
138
139
140
141
142
143
144
145
    }
  else
    {
      std::cerr << "Could not retrieve metadata for item @(0,0)!\n";
      std::cerr.flush( );
      status = 0;
    }

  // STEP 1: Check metadata @(1,0)
  if( myAMRData->GetMetaData(1,0,myBox) == 1 )
    {
146
147
148
149
150
151
152
153
154
      CHECK_CONDITION((myBox.GetBlockId()==0),"BlockId mismatch", status );
      CHECK_CONDITION((myBox.GetLevel()==1),"Level mismatch", status);
      CHECK_CONDITION((myBox.GetProcessId()==1),"Process ID mismatch",status);

      int lo[3]; int hi[3];
      myBox.GetLoCorner( lo );
      myBox.GetHiCorner( hi );
      CHECK_CONDITION((lo[0]==2),"LoCorner mismatch",status);
      CHECK_CONDITION((lo[1]==2),"LoCorner mismatch",status);
155
     /* CHECK_CONDITION((lo[2]==0),"LoCorner mismatch",status); */
156
157
      CHECK_CONDITION((hi[0]==5),"HiCorner mismatch",status);
      CHECK_CONDITION((hi[1]==3),"HiCorner mismatch",status);
158
     /* CHECK_CONDITION((hi[2]==0),"HiCorner mismatch",status); */
159
160
161
162
163

      double spacing[3];
      myBox.GetGridSpacing( spacing );
      CHECK_CONDITION((spacing[0]==0.5),"Check grid spacing",status);
      CHECK_CONDITION((spacing[1]==0.5),"Check grid spacing",status);
164
      /* CHECK_CONDITION((spacing[2]==0.5),"Check grid spacing",status); */
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
    }
  else
    {
      std::cerr << "Could not retrieve metadata for item @(0,0)!\n";
      std::cerr.flush( );
      status = 0;
    }

  return( status );
}

//-----------------------------------------------------------------------------
// Description:
// Gets the grid for the given process
int CheckProcessData0( vtkHierarchicalBoxDataSet *myAMRData )
{
  // Sanity Check
  assert( "Input AMR dataset is NULL" && (myAMRData != NULL) );

  int status = 1;

  if( myAMRData->GetDataSet(0,0) == NULL )
    {
      std::cerr << "ERROR: Expected data to be non-NULL, but, data is NULL!\n";
      std::cerr.flush();
      status = 0;
    }
  else if( myAMRData->GetDataSet(1,0) != NULL )
    {
      std::cerr << "ERROR: Expected data to be NULL, but, data is NOT NULL!\n";
      std::cerr.flush( );
      status = 0;
    }
  else
    {
      status = CheckMetaData( myAMRData );
    }

  return status;
}

//-----------------------------------------------------------------------------
// Description:
// Gets the grid for the given process
int CheckProcessData1( vtkHierarchicalBoxDataSet *myAMRData )
{
  // Sanity Check
  assert( "Input AMR dataset is NULL" && (myAMRData != NULL) );

  int status = 1;

  if( myAMRData->GetDataSet(0,0) != NULL )
    {
      std::cerr << "ERROR: Expected data to be NULL, but, data is NOT NULL!\n";
      std::cerr.flush();
      status = 0;
    }
  else if( myAMRData->GetDataSet(1,0) == NULL )
    {
      std::cerr << "ERROR: Expected data to be non-NULL, but, data is NULL!\n";
      std::cerr.flush( );
      status = 0;
    }
  else
    {
      status = CheckMetaData( myAMRData );
    }

  return status;
}

236
//-----------------------------------------------------------------------------
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Description:
// Gets the grid for the given process
void GetGrid( vtkUniformGrid *myGrid, int &level, int &index,
              vtkMultiProcessController *myController )
{
  assert( "Input Grid is not NULL" && (myGrid != NULL) );
  assert( "Null Multi-process controller encountered" &&
          (myController != NULL) );

  switch( myController->GetLocalProcessId() )
    {
      case 0:
        {
          level=index=0;
          double myOrigin[3] = {0.0,0.0,0.0};
252
          int ndim[3] = {4,4,1};
253
254
255
256
257
258
259
260
261
262
263
          double spacing[3] = {1,1,1};
          myGrid->Initialize();
          myGrid->SetOrigin( myOrigin );
          myGrid->SetSpacing( spacing );
          myGrid->SetDimensions( ndim );
        }
        break;
      case 1:
        {
          level=1;index=0;
          double myOrigin[3] = {1.0,1.0,0.0};
264
265
          int ndim[3] = {5,3,1};
          double spacing[3] = {0.5,0.5,0.5};
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
          myGrid->Initialize();
          myGrid->SetOrigin( myOrigin );
          myGrid->SetSpacing( spacing );
          myGrid->SetDimensions( ndim );
        }
        break;
      default:
        std::cerr << "Undefined process!\n";
        std::cerr.flush();
        myGrid->Delete();
        myGrid=NULL;
    }

}

281
//-----------------------------------------------------------------------------
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
// Description:
// Get the AMR data-structure for the given process
void GetAMRDataSet(
    vtkHierarchicalBoxDataSet *amrData,
    vtkMultiProcessController *myController )
{
  assert( "Input AMR Data is NULL!" && (amrData != NULL) );
  assert( "Null Multi-process controller encountered" &&
           (myController != NULL) );

  int level = -1;
  int index = -1;
  vtkUniformGrid* myGrid = vtkUniformGrid::New();
  GetGrid( myGrid, level, index, myController );
  assert( "Invalid level" && (level >= 0) );
  assert( "Invalid index" && (index >= 0) );
298
299
300
301
302
303
  std::ostringstream oss;
  oss.clear();
  oss.str("");
  oss << "Process_" << myController->GetLocalProcessId() << "_GRID_";
  oss << "L" << level << "_" << index << ".vtk";
  WriteUniformGrid( myGrid, oss.str( ) );
304
305
306
307
308
309
310
311
312

  amrData->SetDataSet( level, index, myGrid );
  myGrid->Delete();
}

//-----------------------------------------------------------------------------
//                   T E S T   M E T H O D S
//-----------------------------------------------------------------------------

313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
//-----------------------------------------------------------------------------
// Description:
// Tests the functionality for computing the global bounds.
bool TestGenerateMetaData( vtkMultiProcessController *myController )
{
  assert( "Null Multi-process controller encountered!" &&
          (myController != NULL ) );

  vtkHierarchicalBoxDataSet* myAMRData = vtkHierarchicalBoxDataSet::New();
  GetAMRDataSet( myAMRData, myController );
  vtkAMRUtilities::GenerateMetaData( myAMRData, myController );

  int status    = 1;
  int statusSum = 0;
  for( unsigned int i=0; i < myAMRData->GetNumberOfLevels(); ++i )
    {
      if( myAMRData->GetRefinementRatio( i ) != 2 )
        {
          status =0;
          break;
        }
    }

George Zagaris's avatar
George Zagaris committed
336
337
338
339
340
//  std::cout << "Calling write AMR Data...";
//  std::cout.flush();
  WriteAMRData( myAMRData, myController );
//  std::cout << "[DONE]\n";
//  std::cout.flush();
341
342
//  myController->Barrier();

343
344
345
346
347
  myAMRData->Delete();
  myController->AllReduce( &status, &statusSum, 1, vtkCommunicator::SUM_OP );
  return( ( (statusSum==2)? true : false ) );
}

348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
//-----------------------------------------------------------------------------
// Description:
// Tests the functionality for computing the global bounds.
bool TestComputeGlobalBounds( vtkMultiProcessController *myController )
{
  assert( "Null Multi-process controller encountered!" &&
          (myController != NULL ) );

  vtkHierarchicalBoxDataSet* myAMRData = vtkHierarchicalBoxDataSet::New();
  GetAMRDataSet( myAMRData, myController );
  double bounds[6];
  vtkAMRUtilities::ComputeGlobalBounds( bounds, myAMRData, myController );
  myAMRData->Delete();

  int status    = 0;
  int statusSum = 0;
  if( (bounds[0]== 0.0) && (bounds[1]==0.0) && /*(bounds[2]==0.0) &&*/
      (bounds[3]== 3.0) && (bounds[4]==3.0) /*&& (bounds[5]==0.0)*/ )
    {
      status = 1;
    }
  else
    {
      std::cerr << "ERROR: The bounds are:";
      for( int i=0; i < 6; std::cerr << bounds[i++] << " " );
      std::cerr << std::endl;
      std::cerr.flush();
    }

  myController->AllReduce( &status, &statusSum, 1, vtkCommunicator::SUM_OP );
  return( ( (statusSum==2)? true : false ) );
}

381
//-----------------------------------------------------------------------------
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Description:
// Tests the functionality for computing the global data-set origin.
bool TestComputeDataSetOrigin( vtkMultiProcessController *myController )
{
  assert( "Null Multi-process controller encountered" &&
          (myController != NULL) );
  myController->Barrier();

  vtkHierarchicalBoxDataSet* myAMRData = vtkHierarchicalBoxDataSet::New();
  GetAMRDataSet( myAMRData, myController );
  double origin[3];
  vtkAMRUtilities::ComputeDataSetOrigin( origin, myAMRData, myController );
  myAMRData->Delete();

396
397
  int status    = 0;
  int statusSum = 0;
398
  if( (origin[0] == 0.0) && (origin[1] == 0.0) && (origin[2] == 0.0) )
399
400
401
402
403
    status = 1;

  myController->AllReduce( &status, &statusSum, 1, vtkCommunicator::SUM_OP  );

  return( ( (statusSum==2)? true : false ) );
404
405
}

406
407
408
409
410
411
412
413
414
415
416
417
418
419
//-----------------------------------------------------------------------------
// Description:
// Tests the functionality for computing the global data-set origin.
bool TestCollectMetaData( vtkMultiProcessController *myController )
{

  assert( "Null Multi-process controller encountered" &&
          (myController != NULL) );
  myController->Barrier();

  vtkHierarchicalBoxDataSet* myAMRData = vtkHierarchicalBoxDataSet::New();
  GetAMRDataSet( myAMRData, myController );

  vtkAMRUtilities::CollectAMRMetaData( myAMRData, myController );
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

  int status    = 0;
  int statusSum = 0;
  switch( myController->GetLocalProcessId() )
    {
      case 0:
        status = CheckProcessData0( myAMRData );
        break;
      case 1:
        status = CheckProcessData1( myAMRData );
        break;
      default:
        std::cerr << "ERROR: This test must be run with 2 MPI processes!\n";
        std::cerr.flush( );
        status = 0;
    }
436
  myAMRData->Delete();
437
438
439

  myController->AllReduce( &status, &statusSum, 1, vtkCommunicator::SUM_OP );
  return ( (statusSum==2)? true : false );
440
441
442
}

//-----------------------------------------------------------------------------
443
444
445
446
447
448
449
450
451
// Description:
// Main Test driver
int TestAMRUtilities(int,char*[])
{
  vtkMultiProcessController *myController =
      vtkMultiProcessController::GetGlobalController();
  assert( "Null Multi-process controller encountered" &&
            (myController != NULL) );

452
  // Synchronize Processes
453
454
455
  myController->Barrier();

  int rval=0;
456
  CHECK_TEST( TestComputeDataSetOrigin(myController),"ComputeOrigin", rval );
George Zagaris's avatar
George Zagaris committed
457
458
  myController->Barrier();

459
  CHECK_TEST( TestCollectMetaData(myController), "CollectMetaData", rval );
George Zagaris's avatar
George Zagaris committed
460
461
  myController->Barrier();

462
  CHECK_TEST( TestComputeGlobalBounds(myController),"ComputeBounds",rval );
George Zagaris's avatar
George Zagaris committed
463
  myController->Barrier();
464

George Zagaris's avatar
George Zagaris committed
465
  CHECK_TEST( TestGenerateMetaData(myController),"GenerateMetaData",rval );
466
  myController->Barrier();
George Zagaris's avatar
George Zagaris committed
467
468


469
  return( rval );
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
}

//-----------------------------------------------------------------------------
//                   P R O G R A M    M A I N
//-----------------------------------------------------------------------------

int main( int argc, char **argv )
{
  MPI_Init(&argc,&argv);

  vtkMPIController* contr = vtkMPIController::New();
  contr->Initialize( &argc, &argv, 1);

  vtkMultiProcessController::SetGlobalController(contr);

  int rc = TestAMRUtilities( argc, argv );
  contr->Barrier();

  contr->Finalize();
  contr->Delete();
  return rc;
}