Skip to content

Examples

The examples/gbmsc_pde directory contains notebooks and short Python scripts that demonstrate the public API.

Minimal Solve

import numpy as np

from gbmsc_pde import (
    BoundaryConditions,
    GBMSCApproximation,
    LinearPDE,
    LinearSolver,
    SourceGrid,
)

grid = SourceGrid(grid_shape=(31, 31), subgrid_shape=(5, 5))
approximation = GBMSCApproximation(grid, step=(2, 2), support_mode="nodal_limit")

pde = LinearPDE(approximation)
pde.add_diffusion_term(-1.0)
pde.add_source_term(
    lambda x, y: 2.0 * np.pi**2 * np.sin(np.pi * x) * np.sin(np.pi * y)
)

boundary_conditions = BoundaryConditions(grid)
boundary_conditions.add_dirichlet(grid.boundary_indices()["all"], 0.0)

solution = LinearSolver(pde, boundary_conditions).solve()
solution_grid = solution.reshape(grid.grid_shape)

Script Examples

Run the script examples from the repository root:

python examples/gbmsc_pde/poisson_dirichlet.py
python examples/gbmsc_pde/poisson_mixed_conditions.py

poisson_dirichlet.py solves a Poisson problem with homogeneous Dirichlet conditions on every boundary node.

poisson_mixed_conditions.py solves a Poisson problem with Dirichlet conditions on the top and bottom boundaries and Neumann conditions on the left and right boundaries.

Notebooks

The notebook examples are:

  • examples/gbmsc_pde/Poisson_Dirichlet_conditions.ipynb
  • examples/gbmsc_pde/Poisson_mixed_conditions_Neu-Dir.ipynb

Install the example dependencies before opening them:

python -m pip install -e ".[examples]"