forked from Kitware/VTK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vtkAMRGaussianPulseSource.cxx
358 lines (299 loc) · 11.2 KB
/
vtkAMRGaussianPulseSource.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
/*=========================================================================
Program: Visualization Toolkit
Module: vtkAMRGaussianPulseSource.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 "vtkAMRGaussianPulseSource.h"
#include "vtkObjectFactory.h"
#include "vtkOverlappingAMR.h"
#include "vtkUniformGrid.h"
#include "vtkCellData.h"
#include "vtkDoubleArray.h"
#include "vtkAMRUtilities.h"
#include "vtkInformation.h"
#include "vtkInformationVector.h"
#include "vtkStructuredExtent.h"
#include "vtkCell.h"
#include "vtkMath.h"
#include "vtkNew.h"
#include "vtkAMRBox.h"
#include <cassert>
vtkStandardNewMacro(vtkAMRGaussianPulseSource);
//------------------------------------------------------------------------------
vtkAMRGaussianPulseSource::vtkAMRGaussianPulseSource()
{
this->SetNumberOfInputPorts(0);
this->SetNumberOfOutputPorts(1);
this->RootSpacing[0] =
this->RootSpacing[1] =
this->RootSpacing[2] = 0.5;
this->PulseOrigin[0] =
this->PulseOrigin[1] =
this->PulseOrigin[2] = 0.0;
this->PulseWidth[0] =
this->PulseWidth[1] =
this->PulseWidth[2] = 0.5;
this->NumberOfLevels = 1;
this->Dimension = 3;
this->RefinmentRatio = 2;
this->PulseAmplitude = 0.0001;
}
//------------------------------------------------------------------------------
vtkAMRGaussianPulseSource::~vtkAMRGaussianPulseSource()
{
}
//------------------------------------------------------------------------------
void vtkAMRGaussianPulseSource::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf( os, indent );
}
//------------------------------------------------------------------------------
void vtkAMRGaussianPulseSource::ComputeCellCenter(
vtkUniformGrid *grid, vtkIdType cellIdx, double centroid[3] )
{
assert("pre: input grid instance is nullptr" && (grid != nullptr));
assert("pre: cell index is out-of-bounds!" &&
(cellIdx >= 0) && (cellIdx < grid->GetNumberOfCells()));
vtkCell *myCell = grid->GetCell( cellIdx );
assert( "ERROR: Cell is nullptr" && (myCell != nullptr) );
double pcenter[3];
double *weights = new double[ myCell->GetNumberOfPoints() ];
int subId = myCell->GetParametricCenter( pcenter );
myCell->EvaluateLocation( subId, pcenter, centroid, weights );
delete [] weights;
}
//------------------------------------------------------------------------------
void vtkAMRGaussianPulseSource::GeneratePulseField(vtkUniformGrid* grid)
{
assert("pre: grid is nullptr!" && (grid != nullptr));
assert("pre: grid is empty!" && (grid->GetNumberOfCells() >= 1) );
vtkDoubleArray *centroidArray = vtkDoubleArray::New();
centroidArray->SetName("Centroid");
centroidArray->SetNumberOfComponents( 3 );
centroidArray->SetNumberOfTuples( grid->GetNumberOfCells() );
vtkDoubleArray *pulseField = vtkDoubleArray::New();
pulseField->SetName( "Gaussian-Pulse" );
pulseField->SetNumberOfComponents( 1 );
pulseField->SetNumberOfTuples( grid->GetNumberOfCells() );
double centroid[3];
vtkIdType cellIdx = 0;
for(; cellIdx < grid->GetNumberOfCells(); ++cellIdx )
{
this->ComputeCellCenter(grid,cellIdx,centroid);
centroidArray->SetComponent(cellIdx,0,centroid[0]);
centroidArray->SetComponent(cellIdx,1,centroid[1]);
centroidArray->SetComponent(cellIdx,2,centroid[2]);
double pulse = this->ComputePulseAt(centroid);
pulseField->SetComponent(cellIdx,0,pulse);
} // END for all cells
grid->GetCellData()->AddArray( centroidArray );
centroidArray->Delete();
grid->GetCellData()->AddArray( pulseField );
pulseField->Delete();
}
#define PRINTEXT( extName, ext ) { \
std::cout << extName << ": "; \
std::cout.flush(); \
for( int ii=0; ii < 6; ++ii ) { \
std::cout << ext[ii] << " "; \
} \
std::cout << std::endl; \
std::cout.flush(); \
}
//------------------------------------------------------------------------------
vtkUniformGrid* vtkAMRGaussianPulseSource::RefinePatch(
vtkUniformGrid* parent, int patchExtent[6] )
{
assert("pre: parent grid is nullptr!" && (parent!=nullptr) );
assert("dimension must be 2 or 3" && (this->Dimension<=3));
int ext[6];
parent->GetExtent(ext);
assert("pre: patchExtent must be within the parent extent!" &&
vtkStructuredExtent::Smaller(patchExtent,ext));
// PRINTEXT("Parent",ext);
// PRINTEXT("Patch", patchExtent);
double min[3];
double max[3];
double h[3];
double h0[3];
int ndim[3];
// Set some nominal values to ensure proper initialization
ndim[0] = ndim[1] = ndim[2] = 1;
min[0] = min[1] = min[2] =
max[0] = max[1] = max[2] = 0.0;
h[0] = h[1] = h[2] =
h0[0] = h0[1] = h0[2] = 0.5;
// STEP 0: Get min
int minIJK[3];
minIJK[0] = patchExtent[0];
minIJK[1] = patchExtent[2];
minIJK[2] = patchExtent[4];
vtkIdType minIdx = vtkStructuredData::ComputePointIdForExtent(ext,minIJK);
// std::cout << "min: " << minIdx << std::endl;
// std::cout.flush();
parent->GetPoint( minIdx, min );
// STEP 1: Get max
int maxIJK[3];
maxIJK[0] = patchExtent[1];
maxIJK[1] = patchExtent[3];
maxIJK[2] = patchExtent[5];
vtkIdType maxIdx = vtkStructuredData::ComputePointIdForExtent(ext,maxIJK);
// std::cout << "max: " << maxIdx << std::endl;
// std::cout.flush();
parent->GetPoint( maxIdx, max );
int patchdims[3];
patchdims[0] = patchExtent[1]-patchExtent[0]+1;
patchdims[1] = patchExtent[3]-patchExtent[2]+1;
patchdims[2] = patchExtent[5]-patchExtent[4]+1;
// STEP 2: Compute the spacing of the refined patch and its dimensions
parent->GetSpacing( h0 );
for( int i=0; i < this->Dimension; ++i )
{
h[i] = h0[i]/static_cast<double>(this->RefinmentRatio);
ndim[i] = this->RefinmentRatio*patchdims[i]-(this->RefinmentRatio-1);
} // END for all dimensions
// std::cout << "Computed h:" << h[0] << " " << h[1] << " " << h[2] << "\n";
// std::cout << "Computed ndim:" << ndim[0] << " " << ndim[1] << " " << ndim[2];
// std::cout << std::endl;
// std::cout.flush();
// STEP 3: Construct uniform grid for requested patch
vtkUniformGrid *grid = vtkUniformGrid::New();
grid->Initialize();
grid->SetOrigin( min );
grid->SetSpacing( h );
grid->SetDimensions(ndim);
// STEP 4: Compute Gaussian-Pulse field on patch
this->GeneratePulseField(grid);
return(grid);
}
//------------------------------------------------------------------------------
vtkUniformGrid* vtkAMRGaussianPulseSource::GetGrid(
double origin[3], double h[3], int ndim[3] )
{
vtkUniformGrid *grid = vtkUniformGrid::New();
grid->Initialize();
grid->SetOrigin(origin);
grid->SetSpacing(h);
grid->SetDimensions(ndim);
this->GeneratePulseField(grid);
return(grid);
}
//------------------------------------------------------------------------------
void vtkAMRGaussianPulseSource::Generate2DDataSet( vtkOverlappingAMR *amr )
{
assert( "pre: input amr dataset is nullptr" && (amr!=nullptr) );
int ndim[3];
double origin[3];
double h[3];
int blockId = 0;
int level = 0;
// Define the patches to be refined apriori
int patches[2][6] = {
{0,2,0,3,0,0},
{3,5,2,5,0,0}
};
// Root Block -- Block 0,0
ndim[0] = 6; ndim[1] = 6; ndim[2] = 1;
h[0] = h[1] = h[2] = this->RootSpacing[0];
origin[0] = origin[1] = -2.0; origin[2] = 0.0;
blockId = 0;
level = 0;
std::vector<int> blocksPerLevel(2);
blocksPerLevel[0]=1;
blocksPerLevel[1]=2;
vtkUniformGrid *grid = this->GetGrid(origin, h, ndim);
vtkAMRBox box(grid->GetOrigin(), grid->GetDimensions(), grid->GetSpacing(),origin,amr->GetGridDescription());
amr->Initialize(2,&blocksPerLevel[0]);
amr->SetOrigin(grid->GetOrigin());
amr->SetGridDescription(grid->GetGridDescription());
amr->SetSpacing(level,grid->GetSpacing());
amr->SetAMRBox(level,blockId,box);
amr->SetDataSet(level,blockId,grid);
vtkUniformGrid *refinedPatch = nullptr;
for( int patchIdx=0; patchIdx < 2; ++patchIdx )
{
refinedPatch = RefinePatch( grid, patches[patchIdx] );
assert("pre: refined grid is nullptr" && (refinedPatch != nullptr) );
box = vtkAMRBox(refinedPatch->GetOrigin(), refinedPatch->GetDimensions(), refinedPatch->GetSpacing(),origin,amr->GetGridDescription());
amr->SetSpacing(level+1,refinedPatch->GetSpacing());
amr->SetAMRBox(level+1,patchIdx,box);
amr->SetDataSet(level+1,patchIdx,refinedPatch);
refinedPatch->Delete();
refinedPatch = nullptr;
}
grid->Delete();
}
//------------------------------------------------------------------------------
void vtkAMRGaussianPulseSource::Generate3DDataSet( vtkOverlappingAMR *amr )
{
assert("pre: input AMR dataset is nullptr" && (amr != nullptr) );
int ndim[3];
double origin[3];
double h[3];
int blockId = 0;
int level = 0;
// Define the patches to be refined apriori
int patches[2][6] = {
{0,2,0,3,0,5},
{3,5,2,5,0,5}
};
// Root Block -- Block 0,0
ndim[0] = 6; ndim[1] = 6; ndim[2] = 6;
h[0] = h[1] = h[2] = this->RootSpacing[0];
origin[0] = origin[1] = -2.0; origin[2] = 0.0;
blockId = 0;
level = 0;
std::vector<int> blocksPerLevel(2);
blocksPerLevel[0]=1;
blocksPerLevel[1]=2;
vtkUniformGrid *grid = this->GetGrid(origin, h, ndim);
vtkAMRBox box (grid->GetOrigin(), grid->GetDimensions(), grid->GetSpacing(),origin,amr->GetGridDescription());
amr->Initialize(2, &blocksPerLevel[0]);
amr->SetOrigin(grid->GetOrigin());
amr->SetGridDescription(grid->GetGridDescription());
amr->SetSpacing(level,grid->GetSpacing());
amr->SetAMRBox(level,blockId,box);
amr->SetDataSet(level,blockId,grid);
vtkUniformGrid *refinedPatch = nullptr;
for( int patchIdx=0; patchIdx < 2; ++patchIdx )
{
refinedPatch = RefinePatch( grid, patches[patchIdx] );
assert("pre: refined grid is nullptr" && (refinedPatch != nullptr) );
box = vtkAMRBox(refinedPatch->GetOrigin(), refinedPatch->GetDimensions(), refinedPatch->GetSpacing(),amr->GetOrigin(),amr->GetGridDescription());;
amr->SetSpacing(level+1,refinedPatch->GetSpacing());
amr->SetAMRBox(level+1,patchIdx,box);
amr->SetDataSet(level+1,patchIdx,refinedPatch);
refinedPatch->Delete();
refinedPatch = nullptr;
}
grid->Delete();
}
//------------------------------------------------------------------------------
int vtkAMRGaussianPulseSource::RequestData(
vtkInformation * vtkNotUsed(request),
vtkInformationVector **vtkNotUsed(inputVector),
vtkInformationVector *outputVector )
{
vtkInformation *info = outputVector->GetInformationObject(0);
vtkOverlappingAMR *output =
vtkOverlappingAMR::SafeDownCast(info->Get(vtkDataObject::DATA_OBJECT()));
assert("pre: output should not be nullptr!" && (output != nullptr) );
switch( this->Dimension )
{
case 2:
this->Generate2DDataSet( output );
break;
case 3:
this->Generate3DDataSet( output );
break;
default:
vtkErrorMacro("Dimensions must be either 2 or 3!");
}
vtkAMRUtilities::BlankCells(output);
return 1;
}