-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathstencil2.cpp
More file actions
50 lines (36 loc) · 1.39 KB
/
stencil2.cpp
File metadata and controls
50 lines (36 loc) · 1.39 KB
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
#include <blitz/array.h>
#include <blitz/array/stencil-et.h>
using namespace blitz;
// This example illustrates a simple use of stencils.
// It applies a Laplacian operator to an array which is
// all zero, except for a single 1 in the centre.
// This results in the stencil pattern (although reversed
// and upside-down).
int main()
{
const int N = 9;
// Create some arrays
Array<float,2> A(N,N), B(N,N), C(N,N), D(N,N);
// Initialize A to 1 in the centre, 0 elsewhere
A = 0;
A(N/2,N/2) = 1.0;
// Get the interior of the arrays. Since central42 is the
// stencil [ 1 -4 6 -4 1 ], we have to leave a padding of 2
// cells around the interior. The range 2..N-3 does this.
Range I(2,N-3);
Array<float,2> Ai = A(I,I), Bi = B(I,I), Ci = C(I,I), Di = D(I,I);
// These declarations let us call the first dimension "x"
// and the second dimension "y".
const int x = firstDim, y = secondDim;
// Apply the stencils. Note that Laplacian2D is identical to
// central22(Ai,x) + central22(Ai,y).
Bi = Laplacian2D(Ai);
Ci = central42(Ai,x) + central42(Ai,y);
Di = central22(Ai,x) + central22(Ai,y);
// Display some of the results
Range middle(N/4,3*N/4);
cout << "Laplacian2D: " << B(middle,middle) << endl
<< "central42: " << C(middle,middle) << endl
<< "central22: " << D(middle,middle) << endl;
return 0;
}