forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkAMRResampleFilter.cxx
1318 lines (1151 loc) · 41.3 KB
/
vtkAMRResampleFilter.cxx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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
145
146
147
148
149
150
151
152
153
154
155
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
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
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAMRToUniformGrid.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 "vtkAMRResampleFilter.h"
#include "vtkAMRInformation.h"
#include "vtkObjectFactory.h"
#include "vtkStreamingDemandDrivenPipeline.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkMultiBlockDataSet.h"
#include "vtkOverlappingAMR.h"
#include "vtkMultiProcessController.h"
#include "vtkUniformGrid.h"
#include "vtkIndent.h"
#include "vtkAMRUtilities.h"
#include "vtkBoundingBox.h"
#include "vtkMath.h"
#include "vtkCompositeDataPipeline.h"
#include "vtkFieldData.h"
#include "vtkCellData.h"
#include "vtkPointData.h"
#include "vtkCell.h"
#include "vtkExtentRCBPartitioner.h"
#include "vtkUniformGridPartitioner.h"
#include "vtkDataArray.h"
#include "vtkTimerLog.h"
#include <cassert>
#include <cmath>
#include <sstream>
#include <cmath>
#include <algorithm>
vtkStandardNewMacro( vtkAMRResampleFilter );
//-----------------------------------------------------------------------------
vtkAMRResampleFilter::vtkAMRResampleFilter()
{
this->TransferToNodes = 1;
this->DemandDrivenMode = 0;
this->NumberOfPartitions = 1;
this->LevelOfResolution = 0;
this->AMRMetaData = nullptr;
this->NumberOfSamples[0] = this->NumberOfSamples[1] = this->NumberOfSamples[2] = 10;
this->Controller = vtkMultiProcessController::GetGlobalController();
this->ROI = vtkMultiBlockDataSet::New();
for( int i=0; i < 3; ++i )
{
this->Min[i] = 0.;
this->Max[i] = 1.;
}
this->SetNumberOfInputPorts( 1 );
this->SetNumberOfOutputPorts( 1 );
this->UseBiasVector = false;
this->BiasVector[0] = this->BiasVector[1] = this->BiasVector[2] = 0.0;
}
//-----------------------------------------------------------------------------
vtkAMRResampleFilter::~vtkAMRResampleFilter()
{
this->BlocksToLoad.clear();
if( this->ROI != nullptr )
{
this->ROI->Delete();
}
this->ROI = nullptr;
// if( this->AMRMetaData != nullptr )
// {
// this->AMRMetaData->Delete();
// }
// this->AMRMetaData = nullptr;
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::PrintSelf( std::ostream &oss, vtkIndent indent )
{
this->Superclass::PrintSelf( oss, indent );
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::FillInputPortInformation(
int vtkNotUsed(port), vtkInformation *info )
{
assert( "pre: information object is nullptr" && (info != nullptr) );
info->Set(
vtkAlgorithm::INPUT_REQUIRED_DATA_TYPE(), "vtkOverlappingAMR" );
return 1;
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::FillOutputPortInformation(
int vtkNotUsed(port), vtkInformation *info )
{
assert( "pre: information object is nullptr" && (info != nullptr) );
info->Set(vtkDataObject::DATA_TYPE_NAME(),"vtkMultiBlockDataSet");
return 1;
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestUpdateExtent(
vtkInformation*, vtkInformationVector **inputVector,
vtkInformationVector* vtkNotUsed(outputVector) )
{
assert( "pre: inputVector is nullptr" && (inputVector != nullptr) );
vtkInformation *info = inputVector[0]->GetInformationObject(0);
assert( "pre: info is nullptr" && (info != nullptr) );
if( this->DemandDrivenMode == 1 )
{
// Tell reader to load all requested blocks.
info->Set( vtkCompositeDataPipeline::LOAD_REQUESTED_BLOCKS(), 1 );
// Tell reader which blocks this process requires
info->Set(
vtkCompositeDataPipeline::UPDATE_COMPOSITE_INDICES(),
&this->BlocksToLoad[0], static_cast<int>(this->BlocksToLoad.size()));
}
return 1;
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestInformation(
vtkInformation* vtkNotUsed(rqst),
vtkInformationVector** inputVector,
vtkInformationVector* vtkNotUsed(outputVector) )
{
assert( "pre: inputVector is nullptr" && (inputVector != nullptr) );
vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
assert( "pre: input is nullptr" && (input != nullptr) );
if( this->DemandDrivenMode == 1 &&
input->Has(vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA() ) )
{
// this->AMRMetaData = vtkOverlappingAMR::New();
this->AMRMetaData =
vtkOverlappingAMR::SafeDownCast(
input->Get( vtkCompositeDataPipeline::COMPOSITE_DATA_META_DATA() ) );
// Get Region
double h[3];
this->ComputeAndAdjustRegionParameters( this->AMRMetaData, h );
this->GetRegion( h );
// Compute which blocks to load
this->ComputeAMRBlocksToLoad( this->AMRMetaData );
}
// Don't we need to call this->Modified() here?
// this->Modified();
return 1;
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::RequestData(
vtkInformation* vtkNotUsed(rqst), vtkInformationVector** inputVector,
vtkInformationVector* outputVector )
{
cerr << "Running Resampler\n";
// STEP 0: Get input object
vtkInformation *input = inputVector[0]->GetInformationObject( 0 );
assert( "pre: Null information object!" && (input != nullptr) );
vtkOverlappingAMR *amrds=
vtkOverlappingAMR::SafeDownCast(
input->Get(vtkDataObject::DATA_OBJECT()));
assert( "pre: input AMR dataset is nullptr" && (amrds != nullptr) );
// STEP 1: Get output object
vtkInformation *output = outputVector->GetInformationObject( 0 );
assert( "pre: Null output information object!" && (output != nullptr) );
vtkMultiBlockDataSet *mbds =
vtkMultiBlockDataSet::SafeDownCast(
output->Get(vtkDataObject::DATA_OBJECT() ) );
assert( "pre: output grid is nullptr" && (mbds != nullptr) );
// STEP 2: Get Metadata
if( this->DemandDrivenMode == 1 )
{
assert( "pre: Metadata must have been populated in RqstInfo" &&
(this->AMRMetaData != nullptr) );
this->ExtractRegion( amrds, mbds, this->AMRMetaData );
}
else
{
// GetRegion
double h[3];
this->ComputeAndAdjustRegionParameters(amrds, h );
this->GetRegion( h );
this->ExtractRegion( amrds, mbds, amrds);
}
return 1;
}
//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::FoundDonor(
double q[3],vtkUniformGrid *&donorGrid,int &cellIdx)
{
assert( "pre: donor grid is nullptr" && (donorGrid != nullptr) );
double gbounds[6];
// Lets do a trival spatial check
this->NumberOfBlocksTested++;
donorGrid->GetBounds(gbounds);
if ((q[0] < gbounds[0]) || (q[0] > gbounds[1]) ||
(q[1] < gbounds[2]) || (q[1] > gbounds[3]) ||
(q[2] < gbounds[4]) || (q[2] > gbounds[5]))
{
return false;
}
int ijk[3];
double pcoords[3];
int status = donorGrid->ComputeStructuredCoordinates( q, ijk, pcoords );
if( status == 1 )
{
cellIdx=vtkStructuredData::ComputeCellId(donorGrid->GetDimensions(),ijk);
return true;
}
return false;
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::InitializeFields(
vtkFieldData *f, vtkIdType size, vtkCellData *src )
{
assert( "pre: field data is nullptr!" && (f != nullptr) );
assert( "pre: source cell data is nullptr" && (src != nullptr) );
for( int arrayIdx=0; arrayIdx < src->GetNumberOfArrays(); ++arrayIdx )
{
int dataType = src->GetArray( arrayIdx )->GetDataType();
vtkDataArray *array = vtkDataArray::CreateDataArray( dataType );
assert( "pre: failed to create array!" && (array != nullptr) );
array->SetName( src->GetArray(arrayIdx)->GetName() );
array->SetNumberOfComponents(
src->GetArray(arrayIdx)->GetNumberOfComponents() );
array->SetNumberOfTuples( size );
assert( "post: array size mismatch" &&
(array->GetNumberOfTuples() == size) );
f->AddArray( array );
array->Delete();
// int myIndex = -1;
// vtkDataArray *arrayPtr = f->GetArray(
// src->GetArray(arrayIdx)->GetName(), myIndex );
// assert( "post: array index mismatch" && (myIndex == arrayIdx) );
// assert( "post: array size mismatch" &&
// (arrayPtr->GetNumberOfTuples()==size) );
assert( "post: array size mismatch" &&
(f->GetArray( arrayIdx)->GetNumberOfTuples() == size) );
} // END for all arrays
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::CopyData(
vtkFieldData *target, vtkIdType targetIdx,
vtkCellData *src, vtkIdType srcIdx )
{
assert( "pre: target field data is nullptr" && (target != nullptr) );
assert( "pre: source field data is nullptr" && (src != nullptr) );
assert( "pre: number of arrays does not match" &&
(target->GetNumberOfArrays() == src->GetNumberOfArrays() ) );
int arrayIdx = 0;
for( ; arrayIdx < src->GetNumberOfArrays(); ++arrayIdx )
{
vtkDataArray *targetArray = target->GetArray( arrayIdx );
vtkDataArray *srcArray = src->GetArray( arrayIdx );
assert( "pre: target array is nullptr!" && (targetArray != nullptr) );
assert( "pre: source array is nullptr!" && (srcArray != nullptr) );
assert( "pre: targer/source array number of components mismatch!" &&
(targetArray->GetNumberOfComponents()==
srcArray->GetNumberOfComponents() ) );
assert( "pre: target/source array names mismatch!" &&
(strcmp(targetArray->GetName(),srcArray->GetName()) == 0) );
assert( "pre: source index is out-of-bounds" &&
(srcIdx >=0) &&
(srcIdx < srcArray->GetNumberOfTuples() ) );
assert( "pre: target index is out-of-bounds" &&
(targetIdx >= 0) &&
(targetIdx < targetArray->GetNumberOfTuples() ) );
int c=0;
for( ; c < srcArray->GetNumberOfComponents(); ++c )
{
double f = srcArray->GetComponent( srcIdx, c );
targetArray->SetComponent( targetIdx, c, f );
} // END for all componenents
} // END for all arrays
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeCellCentroid(
vtkUniformGrid *g, const vtkIdType cellIdx, double c[3] )
{
assert( "pre: uniform grid is nullptr" && (g != nullptr) );
assert( "pre: centroid is nullptr" && (c != nullptr) );
assert( "pre: cell index out-of-bounds" &&
(cellIdx >= 0) && (cellIdx < g->GetNumberOfCells()) );
vtkCell *myCell = g->GetCell( cellIdx );
assert( "post: cell is nullptr!" && (myCell != nullptr) );
double pc[3]; // the parametric center
double *weights = new double[ myCell->GetNumberOfPoints() ];
assert( "post: weights vector is nullptr" && (weights != nullptr) );
int subId = myCell->GetParametricCenter( pc );
myCell->EvaluateLocation( subId, pc, c, weights );
delete [] weights;
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferToCellCenters(
vtkUniformGrid *g, vtkOverlappingAMR *amrds )
{
assert( "pre: uniform grid is nullptr" && (g != nullptr) );
assert( "pre: AMR data-strucutre is nullptr" && (amrds != nullptr) );
// STEP 0: Get the first block so that we know the arrays
// vtkUniformGrid *refGrid = amrds->GetDataSet(0,0);
// assert( "pre: Block(0,0) is nullptr!" && (refGrid != nullptr) );
vtkUniformGrid *refGrid = this->GetReferenceGrid( amrds );
// STEP 1: Get the cell-data of the reference grid
vtkCellData *CD = refGrid->GetCellData();
assert( "pre: Donor CellData is nullptr!" && (CD != nullptr) );
// STEP 2: Get the cell data of the resampled grid
vtkCellData *fieldData = g->GetCellData();
assert( "pre: Target PointData is nullptr!" && (fieldData != nullptr) );
// STEP 3: Initialize the fields on the resampled grid
this->InitializeFields( fieldData, g->GetNumberOfCells(), CD );
if(fieldData->GetNumberOfArrays() == 0)
{
return;
}
// TODO: this is a very naive implementation and should be optimized. However,
// mostly this filter is used to transfer the solution to the grid nodes and
// not on the cell nodes.
vtkIdType cellIdx = 0;
for( ; cellIdx < g->GetNumberOfCells(); ++cellIdx )
{
double qPoint[3];
this->ComputeCellCentroid( g, cellIdx, qPoint );
unsigned int level=0;
for( ; level < amrds->GetNumberOfDataSets( level ); ++level )
{
unsigned int dataIdx = 0;
for( ; dataIdx < amrds->GetNumberOfDataSets( level ); ++dataIdx )
{
int donorCellIdx = -1;
vtkUniformGrid *donorGrid = amrds->GetDataSet(level,dataIdx);
if( (donorGrid!=nullptr) &&
this->FoundDonor(qPoint,donorGrid,donorCellIdx) )
{
assert( "pre: donorCellIdx is invalid" &&
(donorCellIdx >= 0) &&
(donorCellIdx < donorGrid->GetNumberOfCells()) );
CD = donorGrid->GetCellData();
this->CopyData( fieldData, cellIdx, CD, donorCellIdx );
} // END if
} // END for all datasets
} // END for all levels
} // END for all cells
}
//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::SearchForDonorGridAtLevel(
double q[3], vtkOverlappingAMR *amrds,
unsigned int level, unsigned int& donorGridId,
int &donorCellIdx )
{
assert( "pre: AMR dataset is nullptr" && (amrds != nullptr) );
this->NumberOfBlocksTestedForLevel = 0;
std::ostringstream oss;
oss << "SearchLevel-" << level;
vtkTimerLog::MarkStartEvent( oss.str().c_str() );
for(donorGridId = 0; donorGridId < amrds->GetNumberOfDataSets(level); ++donorGridId )
{
donorCellIdx = -1;
this->NumberOfBlocksTestedForLevel++;
if(amrds->GetAMRInfo()->FindCell(q, level,donorGridId,donorCellIdx))
{
assert( "pre: donorCellIdx is invalid" &&
(donorCellIdx >= 0));// &&
// (donorCellIdx < donorGrid->GetNumberOfCells()) );
vtkTimerLog::MarkEndEvent( oss.str().c_str() );
return true;
} // END if
} // END for all data at level
// No suitable grid is found at the requested level, set donorGrid to nullptr
// to indicate that to the caller.
vtkTimerLog::MarkEndEvent( oss.str().c_str() );
return false;
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::ProbeGridPointInAMR(
double q[3], unsigned int &donorLevel, unsigned int& donorGridId,
vtkOverlappingAMR *amrds, unsigned int maxLevel, bool hadDonorGrid)
{
assert( "pre: AMR dataset is nullptr" && amrds != nullptr );
vtkUniformGrid *currentGrid = nullptr;
int currentCellIdx = -1;
int donorCellIdx = -1;
unsigned int currentLevel = 0;
unsigned int currentGridId = 0;
vtkUniformGrid* donorGrid = hadDonorGrid? amrds->GetDataSet(donorLevel,donorGridId): nullptr;
// STEP 0: Check the previously cached donor-grid
if( hadDonorGrid)
{
this->NumberOfBlocksTested++;
bool res(true);
if(!amrds->GetAMRInfo()->FindCell( q, donorLevel, donorGridId, donorCellIdx ) )
{
// Lets see if the point is contained by a grid at the same donar level
res = this->SearchForDonorGridAtLevel(q,amrds,donorLevel,donorGridId,
donorCellIdx);
donorGrid = res? amrds->GetDataSet(donorLevel,donorGridId) : nullptr;
this->NumberOfBlocksTested += this->NumberOfBlocksTestedForLevel;
}
// If donorGrid is still not nullptr then we found the grid and potential starting
// level
if (res)
{
assert( "pre: donorCellIdx is invalid" &&
(donorCellIdx >= 0) && (donorCellIdx < donorGrid->GetNumberOfCells()) );
this->NumberOfTimesFoundOnDonorLevel++;
// Initialize values for step 1 s.t. that the search will start from the
// current donorLevel
currentGrid = donorGrid;
currentGridId = donorGridId;
currentCellIdx = donorCellIdx;
currentLevel = donorLevel;
assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
}
else if (donorLevel == 0)
{
//if we are here then the point is not contained in any of the level 0
// blocks!
this->NumberOfFailedPoints++;
donorGrid = nullptr;
donorLevel = 0;
return -1;
}
else
{
// If we are here then we know the point is not on the donor level
// and therefore not contained in any of the more refined levels -
// Base on the assumption of overlapping AMR
assert("pre:Donor Level is 0" && donorLevel != 0);
// Initialize values for step 1 s.t. the search will start from level 0.
donorGrid = nullptr;
maxLevel = donorLevel;
donorLevel = 0;
currentLevel = 0;
}
}
// If we didn't have an initial donor grid or if we still have one
// we need to test higher res grids
int startLevel, endLevel;
int incLevel;
if (!((donorGrid == nullptr) && hadDonorGrid))
{
startLevel = donorGrid==nullptr? currentLevel : currentLevel+1;
endLevel = maxLevel;
incLevel = 1;
}
else
{
startLevel = maxLevel-1;
endLevel = -1;
incLevel = -1;
}
// STEP 1: Search in the AMR hierarchy for the donor-grid
for( int level=startLevel; level != endLevel; level += incLevel )
{
if (incLevel == 1)
{
this->NumberOfTimesLevelUp++;
}
else
{
this->NumberOfTimesLevelDown++;
}
bool res = this->SearchForDonorGridAtLevel(q,amrds,level,donorGridId,donorCellIdx);
donorGrid = res? amrds->GetDataSet(level,donorGridId) : nullptr;
this->NumberOfBlocksTested += this->NumberOfBlocksTestedForLevel;
if( res )
{
donorLevel = level;
// if we are going from fine to coarse then we can stop the search
if (incLevel == -1)
{
assert(amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
return donorCellIdx;
}
// Lets see if this is the highest resolution grid that contains the
// point
if (donorGrid->IsCellVisible(donorCellIdx))
{
//return donorCellIdx;
}
// we found a grid that contains the point at level l, let's store it
// here temporatily in case there is a grid at a higher resolution that
// we need to use.
currentGrid = donorGrid;
currentCellIdx = donorCellIdx;
currentLevel = level;
currentGridId = donorGridId;
}
else if( currentGrid != nullptr )
{
// we did not find the point at a higher res, but, we did find at a lower
// resolution, so we will use the solution we found previously
// THIS SHOULD NOW NOT HAPPEN!!
//vtkErrorMacro("Could not find point in an unblanked cell.");
this->NumberOfBlocksVisSkipped += this->NumberOfBlocksTestedForLevel;
donorGrid = currentGrid;
donorCellIdx = currentCellIdx;
donorLevel = currentLevel;
donorGridId = currentGridId;
assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
break;
}
else
{
// we are not able to find a grid/cell that contains the query point, in
// this case we will just return.
this->NumberOfFailedPoints++;
donorCellIdx = -1;
donorGrid = nullptr;
donorLevel = 0;
break;
}
} // END for all levels
assert(!donorGrid || amrds->GetDataSet(donorLevel,donorGridId)==donorGrid);
return( donorCellIdx );
}
//-----------------------------------------------------------------------------
bool vtkAMRResampleFilter::SearchGridAncestors(double q[3],
vtkOverlappingAMR *amrds,
unsigned int &level,
unsigned int &gridId,
int &cellId )
{
assert( "pre: AMR dataset is nullptr" && (amrds != nullptr) );
unsigned int *parents, plevel;
for (; level > 0; --level)
{
++this->NumberOfTimesLevelUp;
// Get the parents of the grid
unsigned int numParents;
parents = amrds->GetParents(level, gridId,numParents);
plevel = level - 1;
// There should be at least 1 parent
assert( "Found non-level 0 grid with no parents" && (parents != nullptr) && (numParents > 0) );
if (numParents > 1)
{
vtkDebugMacro( "Number of parents: " << numParents << " - Only processing 1 route");
}
gridId = parents[0];
if (amrds->GetAMRInfo()->FindCell( q, plevel,gridId, cellId ))
{
level = plevel;
return true;
}
}
// If we are here then we could not find an ancestor
cellId = -1;
return false;
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::SearchGridDecendants(double q[3],
vtkOverlappingAMR *amrds,
unsigned int maxLevel,
unsigned int &level,
unsigned int &gridId,
int &cellId)
{
assert( "pre: AMR dataset is nullptr" && (amrds != nullptr) );
unsigned int *children, clevel, n, i;
for (; level < maxLevel-1; ++level)
{
// Get the children of the grid
children = amrds->GetChildren(level, gridId,n);
clevel = level + 1;
// If there are no children then we found the grid!
if (children == nullptr)
{
return;
}
// assert(n == children[0]);
for (i = 0; i < n; ++i)
{
if (amrds->GetAMRInfo()->FindCell( q, clevel,children[i], cellId ))
{
// We found a descendant so stop searching the
// children and can instead search that grid's
// children
gridId = children[i];
++this->NumberOfTimesLevelDown;
break;
}
}
if (i >= n)
{
// We tested some children that we didn't need to if
// we had visibility info
this->NumberOfBlocksVisSkipped += n;
// If we are here then no child contains the point
// so don't search any further
return;
}
}
}
//-----------------------------------------------------------------------------
int vtkAMRResampleFilter::
ProbeGridPointInAMRGraph(double q[3],
unsigned int &donorLevel, unsigned int &donorGridId,
vtkOverlappingAMR *amrds, unsigned int maxLevel, bool useCached)
{
assert( "pre: AMR dataset is nullptr" && amrds != nullptr );
int donorCellIdx = -1;
vtkUniformGrid* donorGrid = nullptr;
// STEP 0: Check the previously cached donor-grid
if( useCached)
{
if(!amrds->GetAMRInfo()->FindCell( q, donorLevel,donorGridId, donorCellIdx ) )
{
// Lets find the grid's ancestor that contains the point
bool res = this->SearchGridAncestors(q,amrds,donorLevel,donorGridId,donorCellIdx);
donorGrid = res? amrds->GetDataSet(donorLevel,donorGridId) : nullptr;
}
else
{
donorGrid = amrds->GetDataSet(donorLevel,donorGridId);
++this->NumberOfTimesFoundOnDonorLevel;
}
// if the point is not contained in an ancestor then lets just assume its on level
// 0 which is the default
}
// If there is no initial donor grid then search level 0
if (donorGrid == nullptr)
{
bool res = SearchForDonorGridAtLevel(q,amrds,0,donorGridId,donorCellIdx);
// If we still can't find a grid then the point is not contained in the
// AMR Data
if (!res)
{
this->NumberOfFailedPoints++;
donorLevel = 0;
return -1;
}
}
// Now search the descendants of the donor grid
this->SearchGridDecendants(q,amrds,maxLevel,donorLevel,donorGridId,donorCellIdx);
return( donorCellIdx );
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferToGridNodes(
vtkUniformGrid *g, vtkOverlappingAMR *amrds )
{
this->NumberOfBlocksTested = 0;
this->NumberOfBlocksVisSkipped = 0;
this->NumberOfTimesFoundOnDonorLevel = 0;
this->NumberOfTimesLevelUp = 0;
this->NumberOfTimesLevelDown = 0;
this->NumberOfFailedPoints = 0;
this->AverageLevel = 0.0;
assert( "pre: uniform grid is nullptr" && (g != nullptr) );
assert( "pre: AMR data-structure is nullptr" && (amrds != nullptr) );
// STEP 0: Initialize the fields on the grid
vtkUniformGrid *refGrid = this->GetReferenceGrid( amrds );
vtkCellData *CD = refGrid->GetCellData();
assert( "pre: Donor CellData is nullptr!" && (CD != nullptr) );
vtkPointData *PD = g->GetPointData();
assert( "pre: Target PointData is nullptr!" && (PD != nullptr) );
// STEP 0: Initialize the fields on the grid
this->InitializeFields( PD, g->GetNumberOfPoints(), CD );
// STEP 1: If no arrays are selected, there is no need to interpolate
// anything on the grid, just return
if(PD->GetNumberOfArrays() == 0)
{
return;
}
// STEP 2: Fix the maximum level at which the search algorithm will operate
unsigned int maxLevelToLoad = 0;
if( this->LevelOfResolution < static_cast<int>(amrds->GetNumberOfLevels()) &&
this->DemandDrivenMode == 1)
{
maxLevelToLoad = this->LevelOfResolution+1;
}
else
{
maxLevelToLoad = amrds->GetNumberOfLevels();
}
// STEP 3: Loop through all the points and find the donors.
int numPoints = 0;
unsigned int donorLevel = 0;
unsigned int donorGridId = 0;
double qPoint[3];
vtkIdType pIdx;
int donorCellIdx;
bool useCached(false);
// Do we have parent/child meta information (yes, we always do)
if (this->AMRMetaData)
{
for(pIdx = 0; pIdx < g->GetNumberOfPoints(); ++pIdx )
{
g->GetPoint( pIdx, qPoint );
donorCellIdx =
this->ProbeGridPointInAMRGraph(qPoint,
donorLevel, donorGridId,
amrds,maxLevelToLoad, useCached);
if( donorCellIdx != -1 )
{
useCached = true;
vtkUniformGrid *amrGrid = amrds->GetDataSet(donorLevel,donorGridId);
this->AverageLevel += donorLevel;
CD = amrGrid->GetCellData();
this->CopyData( PD, pIdx, CD, donorCellIdx );
}
else
{
useCached = false;
// Point is outside the domain, blank it
++ numPoints;
g->BlankPoint( pIdx );
}
} // END for all grid nodes
}
else
{
for(pIdx = 0; pIdx < g->GetNumberOfPoints(); ++pIdx )
{
g->GetPoint( pIdx, qPoint );
donorCellIdx =
this->ProbeGridPointInAMR(qPoint,donorLevel,donorGridId,
amrds,maxLevelToLoad, useCached);
if( donorCellIdx != -1 )
{
useCached = true;
this->AverageLevel += donorLevel;
vtkUniformGrid* donorGrid = amrds->GetDataSet(donorLevel,donorGridId);
assert(donorGrid != nullptr);
CD = donorGrid->GetCellData();
this->CopyData( PD, pIdx, CD, donorCellIdx );
}
else
{
useCached = false;
// Point is outside the domain, blank it
++ numPoints;
g->BlankPoint( pIdx );
}
} // END for all grid nodes
}
std::cerr << "********* Resample Stats *************\n";
double c = this->NumberOfSamples[0] * this->NumberOfSamples[1] * this->NumberOfSamples[2];
double b = g->GetNumberOfPoints();
std::cerr << "Number of Requested Points: " << c << " Number of Actual Points: " << b << "\n";
std::cerr << " Percentage of Requested Points in Grid: " << 100.0 * b / c << "\n";
std::cerr << "Total Number of Blocks Tested: " << this->NumberOfBlocksTested << "\n";
std::cerr << " Number of Blocks that could be skipped by Visibility: " << this->NumberOfBlocksVisSkipped << "\n";
double a = 100.0 * (double)(this->NumberOfBlocksVisSkipped) / (double) this->NumberOfBlocksTested;
std::cerr << "Percentage of Blocks skipped via Visibility: " << a << "\n";
a = (double) this->NumberOfBlocksTested / b;
std::cerr << "Ave Number of Blocks Tested per Point: " << a << "\n";
a = 100.0 * (double) this->NumberOfTimesFoundOnDonorLevel / b;
std::cerr << "Percentage of Times we found point on Previous Level: " << a << "\n";
a = 100.0 * (double) this->NumberOfTimesLevelUp / b;
std::cerr << "Percentage of Times went to finer level: " << a << "\n";
a = 100.0 * (double) this->NumberOfTimesLevelDown / b;
std::cerr << "Percentage of Times went to coarser level: " << a << "\n";
a = this->AverageLevel / b;
std::cerr << "Average Level: " << a << "\n";
std::cerr << "Number Of Failed Points: " << this->NumberOfFailedPoints << "\n";
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::TransferSolution(
vtkUniformGrid *g, vtkOverlappingAMR *amrds)
{
assert( "pre: uniform grid is nullptr" && (g != nullptr) );
assert( "pre: AMR data-strucutre is nullptr" && (amrds != nullptr) );
if( this->TransferToNodes == 1 )
{
this->TransferToGridNodes( g, amrds );
}
else
{
this->TransferToCellCenters( g, amrds );
}
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ExtractRegion(
vtkOverlappingAMR *amrds, vtkMultiBlockDataSet *mbds,
vtkOverlappingAMR * vtkNotUsed(metadata) )
{
assert( "pre: input AMR data-structure is nullptr" && (amrds != nullptr) );
assert( "pre: resampled grid should not be nullptr" && (mbds != nullptr) );
// std::cout << "NumBlocks: " << this->ROI->GetNumberOfBlocks() << std::endl;
// std::cout << "NumProcs: " << this->Controller->GetNumberOfProcesses() << std::endl;
// std::cout.flush();
assert( "pre: NumProcs must be less than or equal to NumBlocks" &&
(!this->Controller ||
(static_cast<int>(this->ROI->GetNumberOfBlocks()) <= this->Controller->GetNumberOfProcesses())));
mbds->SetNumberOfBlocks( this->ROI->GetNumberOfBlocks( ) );
for( unsigned int block=0; block < this->ROI->GetNumberOfBlocks(); ++block )
{
if( this->IsRegionMine( block ) )
{
vtkUniformGrid *grid = vtkUniformGrid::New();
grid->ShallowCopy( this->ROI->GetBlock( block ) );
this->TransferSolution( grid, amrds );
mbds->SetBlock( block, grid );
grid->Delete();
}
else
{
mbds->SetBlock( block, nullptr );
}
} // END for all blocks
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeAMRBlocksToLoad(
vtkOverlappingAMR *metadata )
{
assert( "pre: metadata is nullptr" && (metadata != nullptr) );
this->BlocksToLoad.clear();
unsigned int maxLevelToLoad = 0;
if( this->LevelOfResolution < static_cast<int>(metadata->GetNumberOfLevels()))
{
maxLevelToLoad = this->LevelOfResolution+1;
}
else
{
maxLevelToLoad = metadata->GetNumberOfLevels();
}
unsigned int level=0;
for( ;level < maxLevelToLoad; ++level )
{
unsigned int dataIdx = 0;
for( ; dataIdx < metadata->GetNumberOfDataSets( level ); ++dataIdx )
{
double grd[6];
metadata->GetBounds(level,dataIdx,grd);
if( this->IsBlockWithinBounds( grd ) )
{
this->BlocksToLoad.push_back(
metadata->GetCompositeIndex(level,dataIdx) );
} // END check if the block is within the bounds of the ROI
} // END for all data
} // END for all levels
std::sort( this->BlocksToLoad.begin(), this->BlocksToLoad.end() );
cerr << "Number Levels Loaded = " << maxLevelToLoad << " Number of Blocks = " << BlocksToLoad.size() << "\n";
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::GetDomainParameters(
vtkOverlappingAMR *amr,
double domainMin[3], double domainMax[3], double h[3],
int dims[3], double &rf )
{
assert( "pre: AMR dataset is nullptr!" && (amr != nullptr) );
rf = amr->GetRefinementRatio(1);
amr->GetAMRInfo()->GetAMRBox(0,0).GetNumberOfNodes(dims);
amr->GetMin(domainMin);
amr->GetMax(domainMax);
amr->GetSpacing(0,h);
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::SnapBounds(
const double* vtkNotUsed(h0[3]),
const double domainMin[3],
const double domainMax[3],
const int* vtkNotUsed(dims[3]), bool outside[6] )
{
int i, j;
for(i=0, j=0; i < 3; ++i )
{
// Snap the parts of the bounds that lie outside of the AMR data
if (this->Min[i] < domainMin[i])
{
outside[j++] = true;
this->GridMin[i] = domainMin[i];
}
else
{
outside[j++] = false;
this->GridMin[i] = this->Min[i];
}
if (this->Max[i] > domainMax[i])
{
outside[j++] = true;
this->GridMax[i] = domainMax[i];
}
else
{
outside[j++] = false;
this->GridMax[i] = this->Max[i];
}
}
}
//-----------------------------------------------------------------------------
void vtkAMRResampleFilter::ComputeLevelOfResolution(
const int N[3], const double h0[3], const double L[3], const double rf )
{
this->LevelOfResolution = 0;
for( int i=0; i < 3; ++i )
{
double c1 = ( ( N[i]*h0[i] )/L[i] );
int currentLevel = vtkMath::Floor( 0.5+(log(c1)/log(rf)) );
if( currentLevel > this->LevelOfResolution )