Giter Site home page Giter Site logo

finite-difference-stencil's Introduction

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(2, 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.

finite-difference-stencil's People

Contributors

rreusser avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.