Skip to content

SourceGrid

SourceGrid is the structured rectangular source-node container used by gbmsc_pde.

from gbmsc_pde import SourceGrid

grid = SourceGrid(
    grid_shape=(31, 31),
    subgrid_shape=(5, 5),
    limits=((0.0, 1.0), (0.0, 1.0)),
)

Purpose

SourceGrid stores:

  • the full rectangular source grid,
  • flattened source-node coordinates,
  • sliding local tensor-product subgrids,
  • one-dimensional coordinate axes,
  • rectangular boundary node ids,
  • pairwise coordinate differences and squared distances used by Shepard weights.

The class is source-node oriented: LinearPDE unknowns, LinearPDE rows, and boundary rows are indexed by flattened grid-node ids.

Constructor

SourceGrid(
    grid_shape=(10, 10),
    subgrid_shape=(3, 3),
    xlim=(0.0, 1.0),
    ylim=(0.0, 1.0),
    limits=None,
)

Parameters:

  • grid_shape: number of source nodes in the x and y directions.
  • subgrid_shape: local tensor-product interpolation window size.
  • xlim, ylim: rectangular coordinate limits.
  • limits: optional alias for (xlim, ylim).

subgrid_shape must be no larger than grid_shape in both directions.

Important Attributes

  • grid.N: total number of source nodes.
  • grid.grid_shape: (N_x, N_y).
  • grid.subgrid_shape: (n_x, n_y).
  • grid.coords: flattened coordinates with shape (N, 2).
  • grid.x_coords_flat, grid.y_coords_flat: flattened coordinate components.
  • grid.X, grid.Y: grid-shaped coordinate arrays.
  • grid.axes: one-dimensional coordinate axes.
  • grid.grid: grid-shaped flattened node-id array.
  • grid.subgrids: sliding local subgrid view.
  • grid.diff_x, grid.diff_y, grid.dist: pairwise data.

Subgrid Sampling

Use subgrids_with_step to sample sliding subgrids:

subgrids, subgrids_x, subgrids_y = grid.subgrids_with_step((2, 2))

The step must align with the grid:

N_x = step_x * k_x + n_x
N_y = step_y * k_y + n_y

for integers k_x, k_y.

Boundary Node Ids

boundary = grid.boundary_indices()

Available keys:

  • "left", "right", "bottom", "top"
  • "x_min", "x_max", "y_min", "y_max"
  • "all"

Example:

all_boundary_nodes = grid.boundary_indices()["all"]

Function Evaluation

values = grid.eval_function_at_nodes(lambda x, y: x**2 + y**2)

The callable must accept two one-dimensional NumPy arrays and return an array of shape (grid.N,).

Plotting

fig, ax = grid.plot(solution, view="2d")

Supported views:

  • "2d"
  • "contour"
  • "contourf"
  • "3d"

Plotting requires matplotlib.