Finite Differences

Finite difference example problems.

  • Diffusive 1D

    \[u_{t} - \mu u_{xx} = 0 \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]
  • Advective 1D

    \[u_{t} + c {u}_{x} = 0 \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]
  • Diffusive-Advective 1D

    \[u_{t} + c {u}_{x} - \mu u_{xx} = 0 \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]
  • Poisson in 1D

    \[- c u_{xx} = f(x) \qquad \forall \, x \in \Omega = [0, L]\]

Bram Lagerweij COHMAS Mechanical Engineering KAUST 2021

finitedifference.Dx(dof, dx, bc='periodic')

Return the central differences matrix for the first derivative. That is the matrix \(D_{x}\) represents the central difference approximation of \(\partial_{x}\) in 1D axis systems.

Parameters
  • dof (int) – Number of spacial degrees of freedom.

  • dx (float) – Spacial step size.

  • bc (str, optional) – The type of boundary condition to be used. The default is ‘periodic’.

Raises

NotImplementedError – Is raised when the requested boundary condition is not implemented.

Returns

The central difference approximation of the first derivative.

Return type

matrix (sparse csr format)

Notes

The following boundary conditions are possible:

  • ‘periodic’ (default) that the first and last dofs are representing the same point. As a result the derivative of the first point depends on the second last point and the derivative of the last point will depend on the second point as well.

  • ‘none’ means that the row of the first and last degree of freedom are left empty. This will result in a singular matrix, thus extra constraints will have to be defined before solving a system with such a matrix.

finitedifference.Dxx(dof, dx, bc='periodic')

Return the central differences matrix for the second derivative. That is the matrix \(D_{xx}\) represents the central difference approximation of \(\partial_{xx}\) in 1D axis systems.

Parameters
  • dof (int) – Number of spacial degrees of freedom.

  • dx (float) – Spacial step size.

  • bc (str, optional) – The type of boundary condition to be used. The default is ‘periodic’.

Raises

NotImplementedError – Is raised when the requested boundary condition is not implemented.

Returns

The central difference approximation of the first derivative.

Return type

matrix (sparse csr format)

Notes

The following boundary conditions are possible:

  • ‘periodic’ (defeat) that the first and last dofs are representing the same point. As a result the derivative of the first point depends on the second last point and the derivative of the last point will depend on the second point as well.

  • ‘none’ means that the row of the first and last degree of freedom are left empty. This will result in a singular matrix, thus extra constraints will have to be defined before solving a system with such a matrix.

finitedifference.advective(dof, dx, c)

Time derivative of the PDE for advective diffusive problems.

\[u_{t} + c u_{x} = 0 \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]

Thus this returns:

\[u_{t} = - c u_{x}\]

Because we use finite difference based matrix products we can convert this into a matrix vector product, where \(D_x\) is the central difference approximation of \(\partial_x\):

\[u_{t} = -c D_{x} u = K u\]

This function calculates the matrix \(K\). Because it should be compatible with general, non-homogeneous formulation, a part that is independent of \(u\) is also included.

Parameters
  • dof (int) – Number of degrees of freedom.

  • dx (float) – Step size in the of spatial discretization.

  • c (float) – The advective coefficient.

Returns

  • M (matrix (sparse csr format)) – The mass matrix, which will equal the identity matrix in finite differenc problems.

  • K (matrix (sparse csr format)) – The time derivative part of the pde obtained from the spatial part.

  • b (vector (dense array)) – The remaining term, in this homogeneous case it is a zero array.

finitedifference.advectivediffusive(dof, dx, mu, c)

Time derivative of the PDE for advective diffusive problems.

\[u_{t} + c u_{x} = \mu u_{xx} \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]

Thus this returns:

\[u_{t} = - c u_{x} + \mu u_{xx}\]

Because we use finite difference based matrix products we can convert this into a matrix vector product, where \(D_x\) is the central difference approximation of \(\partial_x\) and similarly \(D_{xx}\) the central difference approximation of \(\partial_{xx}\):

\[u_{t} = -c D_{x} u + \mu D_{xx} u = (-c D_{x} + \mu D_{xx})\, u = K u\]

This function calculates the matrix \(K\). Because it should be compatible with general, non-homogeneous formulation, a part that is independent of \(u\) is also included.

Parameters
  • dof (int) – Number of degrees of freedom.

  • dx (float) – Step size in the of spatial discretization.

  • mu (float) – The diffusive coefficient.

  • c (float) – The advective coefficient.

Returns

  • M (matrix (sparse csr format)) – The mass matrix, which will equal the identity matrix in finite differenc problems.

  • K (matrix (sparse csr format)) – The time derivative part of the pde obtained from the spatial part.

  • b (vector (dense array)) – The remaining term, in this homogeneous case it is a zero array.

finitedifference.diffusive(dof, dx, mu)

Time derivative of the PDE for advective diffusive problems.

\[u_{t} = \mu u_{xx} \qquad \forall \, x \in \Omega = [0, L] \quad \& \quad t>0\]

Thus this returns:

\[u_{t} = \mu u_{xx}\]

Because we use finite difference based matrix products we can convert this into a matrix vector product, where \(D_xx\) is the central difference approximation of \(\partial_{xx}\):

\[u_{t} = \mu D_{xx} u = K u\]

This function calculates the matrix \(K\). Because it should be compatible with general, non-homogeneous formulation, a part that is independent of \(u\) is also included.

Parameters
  • dof (int) – Number of degrees of freedom.

  • dx (float) – Step size in the of spatial discretization.

  • mu (float) – The diffusive coefficient.

Returns

  • M (matrix (sparse csr format)) – The mass matrix, which will equal the identity matrix in finite differenc problems.

  • K (matrix (sparse csr format)) – The time derivative part of the pde obtained from the spatial part.

  • b (vector (dense array)) – The remaining term, in this homogeneous case it is a zero array.

finitedifference.poisson(dof, dx, f, c=1)

Problem formulation of a Poisson equation.

\[-c u_{xx} = f(x) \qquad \forall \, x \in \Omega = [0, L]\]

Because we use finite difference based matrix products we can convert this into a matrix vector product, where \(D_{xx}\) the is the central difference approximation of \(\partial_{xx}\):

\[D_{xx} u = K u = f/c\]

This function calculates the matrix \(K\) and the forcing vector \(f\). The matrix is however singular as no boundary conditions are specified.

Parameters
  • dof (int) – Number of degrees of freedom.

  • dx (float) – Step size in the of spatial discretization.

  • f (callable) – A function to calculate the forcing term for any location \(x\).

  • c (float, optional) – A scalar multiplying the derivative.

Returns

  • K (matrix (sparse csr format)) – The stiffness matrix.

  • b (vector (dense array)) – The right hand side, caused by the non-homogeneous behavior.