implementations.problem_classes.generic_ND_FD module

Created on Sat Feb 11 22:39:30 2023

class GenericNDimFinDiff(nvars=512, coeff=1.0, derivative=1, freq=2, stencil_type='center', order=2, lintol=1e-12, liniter=10000, solver_type='direct', bc='periodic', bcParams=None)[source]

Bases: ptype

Base class for finite difference spatial discretisation in \(N\) dimensions

\[\frac{d u}{dt} = A u,\]

where \(A \in \mathbb{R}^{nN \times nN}\) is a matrix arising from finite difference discretisation of spatial derivatives with \(n\) degrees of freedom per dimension and \(N\) dimensions. This generic class follows the MOL (method-of-lines) approach and can be used to discretize partial differential equations such as the advection equation and the heat equation.

Parameters:
  • nvars (int, optional) – Spatial resolution for the ND problem. For \(N = 2\), set nvars=(16, 16).

  • coeff (float, optional) – Factor for finite difference matrix \(A\).

  • derivative (int, optional) – Order of the spatial derivative.

  • freq (tuple of int, optional) – Spatial frequency, can be a tuple.

  • stencil_type (str, optional) – Stencil type for finite differences.

  • order (int, optional) – Order of accuracy of the finite difference discretization.

  • lintol (float, optional) – Tolerance for spatial solver.

  • liniter (int, optional) – Maximum number of iterations for linear solver.

  • solver_type (str, optional) – Type of solver. Can be 'direct', 'GMRES' or 'CG'.

  • bc (str or tuple of 2 string, optional) – Type of boundary conditions. Default is 'periodic'. To define two different types of boundary condition for each side, you can use a tuple, for instance bc=("dirichlet", "neumann") uses Dirichlet BC on the left side, and Neumann BC on the right side.

  • bcParams (dict, optional) –

    Parameters for boundary conditions, that can contains those keys :

    • val : value for the boundary value (Dirichlet) or derivative (Neumann), default to 0

    • reduce : if true, reduce the order of the A matrix close to the boundary. If false (default), use shifted stencils close to the boundary.

    • neumann_bc_order : finite difference order that should be used for the neumann BC derivative. If None (default), uses the same order as the discretization for A.

    Default is None, which takes the default values for each parameters. You can also define a tuple to set different parameters for each side.

A

FD discretization matrix of the ND operator.

Type:

sparse matrix (CSC)

Id

Identity matrix of the same dimension as A.

Type:

sparse matrix (CSC)

xvalues

Values of spatial grid.

Type:

np.1darray

dtype_f

alias of mesh

dtype_u

alias of mesh

property dx

Size of the mesh (in all dimensions)

eval_f(u, t)[source]

Routine to evaluate the right-hand side of the problem.

Parameters:
  • u (dtype_u) – Current values.

  • t (float) – Current time.

Returns:

f – Values of the right-hand side of the problem.

Return type:

dtype_f

classmethod get_default_sweeper_class()[source]
property grids

ND grids associated to the problem

property ndim

Number of dimensions of the spatial problem

solve_system(rhs, factor, u0, t)[source]

Simple linear solver for \((I-factor\cdot A)\vec{u}=\vec{rhs}\).

Parameters:
  • rhs (dtype_f) – Right-hand side for the linear system.

  • factor (float) – Abbrev. for the local stepsize (or any other factor required).

  • u0 (dtype_u) – Initial guess for the iterative solver.

  • t (float) – Current time (e.g. for time-dependent BCs).

Returns:

sol – The solution of the linear solver.

Return type:

dtype_u