## 4 Stencils ¶

Blitz++ provides an implementation of stencil objects which is currently experimental. This means that the exact details of how they are declared and used may change in future releases. Use at your own risk.

### 4.1 Motivation: a nicer notation for stencils ¶

Suppose we wanted to implement the 3-D acoustic wave equation using finite differencing. Here is how a single iteration would look using subarray syntax:

    Range I(1,N-2), J(1,N-2), K(1,N-2);

    P3(I,J,K) = (2-6*c(I,J,K)) * P2(I,J,K)
                + c(I,J,K)*(P2(I-1,J,K) + P2(I+1,J,K) + P2(I,J-1,K) + P2(I,J+1,K)
                + P2(I,J,K-1) + P2(I,J,K+1)) - P1(I,J,K);

This syntax is a bit klunky. With stencil objects, the implementation becomes:

    BZ_DECLARE_STENCIL4(acoustic3D_stencil,P1,P2,P3,c)
      P3 = 2 * P2 + c * Laplacian3D(P2) - P1;
    BZ_END_STENCIL

      .
      .

    applyStencil(acoustic3D_stencil(), P1, P2, P3, c);