finite-difference-stencil

1.0.2 • Public • Published

finite-difference-stencil Build Status npm version js-standard-style

Compute the coefficients of explicit or implicit finite difference schemes

Introduction

This module uses a Taylor series expansion to compute the coefficients of finite difference schemes. It generates schemes of any derivative and order of accuracy and generates either explicit or implicit schemes.

For example, a staggered scheme with four points treated explicitly and two implicitly would be written as:

\alpha f'_{i-1} + f'_i + \beta f'_{i + 1} = a \, f_{i - \frac{3}{2}} + b \, f_{i - \frac{1}{2}} + c \, f_{i + \frac{1}{2}} + d \, f_{i + \frac{3}{2}}

In this case, the input would be represented as:

stencil(1, [-1, 1, -1.5, -0.5, 0.5, 1.5], 2)

where the 1 indicates a first derivative and the final 2 indicates that the first two coordinate points will be treated implicitly. Since the grid spacing is just a scale factor that doesn't affect the solution, it is excluded.

For explicit schemes, the resulting coefficients are multiplied by the respective grid points to compute the derivative. Making use of implicit Padé-type (compact) schemes requires simultaneous solution for the desired derivative at each grid point. In one dimension, this typically leads to a tridiagonal or pentadiagonal system of equations that must be inverted. In two or more dimensions, it tends to result in a block-diagonal system.

Examples

// Average two points (zeroth derivative):
var c = [-1, 1]
stencil(0, c)
// => c = [ 0.5, 0.5 ]
 
 
// Central first derivative:
var c = [-1, 0, 1]
stencil(1, c)
// => c = [ -0.5, 0, 0.5 ]
 
 
// One-sided first derivative:
var c = [0, 1, 2]
stencil(1, c)
// => c = [ -1.5, 2, -0.5 ]
 
 
// Sixth order compact second derivative:
var c = [-2, -1, 1, 2, -2, -1, 0, 1, 2]
stencil(1, c, 4)
// => c = [ 0.027777777777778206,
//          0.44444444444444775,
//          0.4444444444444405,
//          0.027777777777777398,
//         -0.11574074074074253,
//         -0.7407407407407423,
//          7.771561172376096e-15,
//          0.7407407407407378,
//          0.11574074074073923 ]

Installation

$ npm install finite-difference-stencil

Usage

require('finite-difference-stencil')(derivative, points[, numImplicit[, A[, P]]])

Given a list of coordinate points, generates a finite difference stencil giving the specified derivative.

Arguments:

  • derivative: A non-negative integer specifying the desired derivative. If zero, it will generate an interpolation scheme. One and two would indicate the first and second derivatives, respectively.
  • points: A list of coordinate points included in the stencil. The first numImplicit points are treated as coefficients of implicit terms (left-hand side in the equation above). The remaining points are treated explicitly.
  • numImplicit (optional): An integer indicating the number of points treated explicitly. If numImplicit is omitted, all points are treated explicitly.
  • A (optional): Given array points of length n, A may be provided as an n×n ndarray work matrix. If not provided, it will be allocated and freed automatically.
  • P (optional): A permutation vector for the matrix inverstion. May simply be an empty Array.

References

Lele, S. K. (1992). Compact Finite Difference Schemes with Spectral-like Resolution. Journal of Computational Physics, 103, 16-42.

License

© 2016 Ricky Reusser. MIT License.

Package Sidebar

Install

npm i finite-difference-stencil

Weekly Downloads

2

Version

1.0.2

License

MIT

Last publish

Collaborators

  • rreusser