Class FiniteDifferenceGridLayout

java.lang.Object
net.finmath.finitedifference.utilities.FiniteDifferenceGridLayout

public final class FiniteDifferenceGridLayout extends Object
Layout helper for finite-difference value vectors on multi-dimensional grids.

Finite-difference products return values as flattened spatial vectors. This class centralizes the flattening convention used by the library. For a spatial grid with shape n0, n1, ..., nd, the flattened index is

i0 + n0 * i1 + n0 * n1 * i2 + ...

The layout is inferred from a SpaceTimeDiscretization. It can be used to validate value vectors and value surfaces, and to convert between multi-index coordinates and flattened vector indices.

Author:
Alessandro Gnoatto
  • Constructor Details

    • FiniteDifferenceGridLayout

      public FiniteDifferenceGridLayout(SpaceTimeDiscretization discretization)
      Creates a grid layout from a space-time discretization.
      Parameters:
      discretization - The space-time discretization.
      Throws:
      IllegalArgumentException - Thrown if discretization is null or if one of its spatial grids is empty.
  • Method Details

    • of

      public static FiniteDifferenceGridLayout of(SpaceTimeDiscretization discretization)
      Creates a grid layout from a space-time discretization.
      Parameters:
      discretization - The space-time discretization.
      Returns:
      The corresponding grid layout.
    • getDiscretization

      public SpaceTimeDiscretization getDiscretization()
      Returns the underlying space-time discretization.
      Returns:
      The space-time discretization.
    • getDimension

      public int getDimension()
      Returns the number of spatial dimensions.
      Returns:
      The number of spatial dimensions.
    • getShape

      public int[] getShape()
      Returns the spatial shape.
      Returns:
      A copy of the spatial shape.
    • getShape

      public int getShape(int dimension)
      Returns the number of grid points in a given spatial dimension.
      Parameters:
      dimension - The spatial dimension.
      Returns:
      The number of grid points in the given dimension.
    • getStride

      public int getStride(int dimension)
      Returns the flattening stride for a given spatial dimension.
      Parameters:
      dimension - The spatial dimension.
      Returns:
      The flattening stride.
    • getNumberOfSpatialPoints

      public int getNumberOfSpatialPoints()
      Returns the total number of spatial grid points.
      Returns:
      The total number of spatial grid points.
    • flatten

      public int flatten(int... indices)
      Flattens a multi-dimensional spatial index.
      Parameters:
      indices - The spatial indices, one per dimension.
      Returns:
      The flattened vector index.
      Throws:
      IllegalArgumentException - Thrown if the number of indices does not match the spatial dimension or if an index is out of range.
    • unflatten

      public int[] unflatten(int flatIndex)
      Converts a flattened vector index into a multi-dimensional spatial index.
      Parameters:
      flatIndex - The flattened vector index.
      Returns:
      The corresponding spatial multi-index.
      Throws:
      IllegalArgumentException - Thrown if flatIndex is out of range.
    • validateVector

      public void validateVector(double[] values)
      Validates that a value vector matches the spatial layout.
      Parameters:
      values - The value vector.
      Throws:
      IllegalArgumentException - Thrown if values is null or if its length does not match the number of spatial points.
    • validateSurface

      public void validateSurface(double[][] values)
      Validates that a value surface matches the spatial layout.
      Parameters:
      values - The value surface indexed by flattened space point and time index.
      Throws:
      IllegalArgumentException - Thrown if values is null or if its first dimension does not match the number of spatial points.