Skip to content

GradientStencil

The GradientStencil is the storage container for storing the gradient stencil components. It stores only non-zero stencil components for 1D, 2D and 3D stencils and offers iterators allowing convenient evaluation of the gradient operators which is beneficial for compact stencils.

The GradientStencil can be initialized to use the following stencils:

  • Simple - simple stencils in 1D, 2D and 3D.
  • Isotropic - isotropic stencils in 1D, 2D and 3D.
  • LB - isotropic stencils based on lattice Boltzmann stencils.

The available stencils for 1D, 2D and 3D cases are given below. Note that 1D and 2D stencils for convenience defined in 3D arrays but should only be used with inactive dimensions suppressed. Also, the gradient stencils have no negative coefficients and their respective components should be multiplied by -1 manually at the point of use.

Simple gradient stencil

cpp
const double GradientStencil1D[3][3][3] = 
          {{{ 0.0, 0.0, 0.0},
            { 0.0, 0.5, 0.0},
            { 0.0, 0.0, 0.0}},

           {{ 0.0, 0.5, 0.0},
            { 0.5, 0.0, 0.5},
            { 0.0, 0.5, 0.0}},

           {{ 0.0, 0.0, 0.0},
            { 0.0, 0.5, 0.0},
            { 0.0, 0.0, 0.0}}};

2-point simple 1D/2D/3D Gradient stencil (standard finite differences stencil)

Isotropic gradient stencils

cpp
const double GradientStencil2D[3][3][3] = 
          {{{      0.0, 1.0/12.0,      0.0},
            { 1.0/12.0,  1.0/3.0, 1.0/12.0},
            {      0.0, 1.0/12.0,      0.0}},

           {{ 1.0/12.0,  1.0/3.0, 1.0/12.0},
            {  1.0/3.0,      0.0,  1.0/3.0},
            { 1.0/12.0,  1.0/3.0, 1.0/12.0}},

           {{      0.0, 1.0/12.0,      0.0},
            { 1.0/12.0,  1.0/3.0, 1.0/12.0},
            {      0.0, 1.0/12.0,      0.0}}};

8-point 2D gradient stencil from "M.Alfaraj, Y. Wang and Y. Luo, Geophysical prospecting 62 (2014) 507-517"

cpp
const double GradientStencil2D_2[3][3][3] = 
          {{{   0.0, 0.125,   0.0},
            { 0.125,  0.25, 0.125},
            {   0.0, 0.125,   0.0}},

           {{ 0.125,  0.25, 0.125},
            {  0.25,   0.0,  0.25},
            { 0.125,  0.25, 0.125}},

           {{   0.0, 0.125,   0.0},
            { 0.125,  0.25, 0.125},
            {   0.0, 0.125,   0.0}}};

8 point 2D gradient stencil by Sobel

cpp
const double GradientStencil3D[3][3][3] = 
          {{{ 0.085/4.64, 0.245/4.64, 0.085/4.64},
            { 0.245/4.64,   1.0/4.64, 0.245/4.64},
            { 0.085/4.64, 0.245/4.64, 0.085/4.64}},

           {{ 0.245/4.64, 1.0/4.64, 0.245/4.64},
            {   1.0/4.64,      0.0,   1.0/4.64},
            { 0.245/4.64, 1.0/4.64, 0.245/4.64}},

           {{ 0.085/4.64, 0.245/4.64, 0.085/4.64},
            { 0.245/4.64,   1.0/4.64, 0.245/4.64},
            { 0.085/4.64, 0.245/4.64, 0.085/4.64}}};

26 point 3D Gradient stencil from "M.Alfaraj, Y. Wang and Y. Luo, Geophysical prospecting 62 (2014) 507-517".

Gradient stencils based on lattice Boltzmann stencils

cpp
const double GradientStencil2D_LB[3][3][3] = 
          {{{     0.0, 1.0/12.0,      0.0},
            {1.0/12.0, 1.0/3.0,  1.0/12.0},
            {     0.0, 1.0/12.0,      0.0}},

           {{1.0/12.0,   1.0/3.0, 1.0/12.0},
            {1.0/3.0,        0.0, 1.0/3.0},
            {1.0/12.0,   1.0/3.0, 1.0/12.0}},

           {{     0.0, 1.0/12.0,      0.0},
            {1.0/12.0, 1.0/3.0,  1.0/12.0},
            {     0.0, 1.0/12.0,      0.0}}};

Isotropic gradient stencil based on the D2Q9 lattice Boltzmann stencil. It is the same as the 8 point 2D gradient stencil from "M.Alfaraj, Y. Wang and Y. Luo, Geophysical prospecting 62 (2014) 507-517".

cpp
const double GradientStencil3D_LB[3][3][3] = 
          {{{1.0/72.0, 1.0/18.0, 1.0/72.0},
            {1.0/18.0, 2.0/9.0,  1.0/18.0},
            {1.0/72.0, 1.0/18.0, 1.0/72.0}},

           {{1.0/18.0, 2.0/9.0, 1.0/18.0},
            {2.0/9.0,      0.0, 2.0/9.0},
            {1.0/18.0, 2.0/9.0, 1.0/18.0}},

           {{1.0/72.0, 1.0/18.0, 1.0/72.0},
            {1.0/18.0, 2.0/9.0,  1.0/18.0},
            {1.0/72.0, 1.0/18.0, 1.0/72.0}}};

Isotropic gradient stencil based on the D3Q27 lattice Boltzmann stencil. It is very close to the 27 point 2D gradient stencil from "M.Alfaraj, Y. Wang and Y. Luo, Geophysical prospecting 62 (2014) 507-517".

GradientStencilEntry

The stencil components inside of the GradientStencil are stored using the corresponding GradientStencilEntry structure which contains three stencil direction coordinates and their weights:

cpp
int di;

X coordinate of the stencil element.

cpp
int dj;

Y coordinate of the stencil element.

cpp
int dk;

Z coordinate of the stencil element.

cpp
double weightX;

Weight associated with the X component of the stencil element.

cpp
double weightY;

Weight associated with the Y component of the stencil element.

cpp
double weightZ;

Weight associated with the Z component of the stencil element.

GradientStencil construction methods

cpp
void Set(const double UserStencil[3][3][3], double dx,
         int dNx = 1, int dNy = 1, int dNz = 1);

Sets nonzero stencil entries for gradient components using user specified stencil. dNx, dNy and dNz are used to enable or suppress the stencil components for correpsonding dimension X, Y and/or Z. The value 0 suppresses the corresponding stencil components, value 1 enables them.

cpp
void Set(const double UserStencil[3][3][3], GridParameters& Dimensions)

Sets nonzero stencil entries for gradient components using user specified stencil.

Container properties

cpp
size_t size() const;

Returns the size of the stencil storage (considers only non-zero entries).

Iterators

cpp
iterator  begin();

Iterator to the begin of the stencil.

cpp
iterator  end();

Iterator to the end of the stencil.

cpp
citerator cbegin() const;

Constant iterator to the begin of the stencil.

cpp
citerator cend() const;

Constant iterator to the end of the stencil.

Data manipulation methods

cpp
iterator erase(iterator it);

Erases the entry pointed to by the given iterator it.

Released under the GNU GPLv3 License.