Please see instructions in the book.
This repository contains a small framework in galerkin.py for assembling mass/stiffness-like operators and projecting/solving with different polynomial and trigonometric bases and their boundary-condition lifts. Classes are used for code reuse and clarity, kept as simple as possible for educational purposes.
There is no manual grading. The assignment is passed once your fork of this repository shows a green badge, i.e. pytest galerkin.py runs test_project, test_convection_diffusion and test_helmholtz without errors on GitHub Actions.
Check locally before pushing:
uv sync
uv run ty check galerkin.py # optional, but should pass — see "Type hints" below
uv run pytest -v galerkin.py
# or simply:
uv run python galerkin.pygalerkin.py marks unfinished code two different ways, and they mean different things:
NotImplementedErrormarks a concrete method for you to fill in — mirror the pattern of the sibling classes/methods that are already implemented.@abstractmethod(fromabc) marks a method that's never meant to be implemented at the class where it's declared — only overridden by a concrete subclass further down the hierarchy, which every subclass eventually does (directly, or via one of theNotImplementedErrorblanks below). If you see@abstractmethodabove a method with a bare...body, leave it alone.
Implement:
Polynomial.reference_domain— return the default reference domain(-1, 1)shared byLegendreandChebyshev. Nothing involving either works until this is filled in.Polynomial.derivative_basis_function— the k'th derivative ofbasis_function(j); bothLegendre's andChebyshev's basis functions arenumpy.polynomialobjects with a.deriv(k)method, so one implementation here covers both.Polynomial.mass_matrix— the (diagonal) mass matrix built fromself.L2_norm_sq, again shared byLegendreandChebyshev.Legendre.L2_norm_sqandChebyshev.L2_norm_sq— the L2 norms of the Legendre/Chebyshev polynomials. These stay separate (unlike the three above) since the closed-form formula differs per basis.Chebyshev.eval— mirrorLegendre.eval, usingnp.polynomial.chebyshev.chebvalinstead oflegval.Cosines(__init__,basis_function,derivative_basis_function,L2_norm_sq) — mirrorSines, but withcos(jπx)and itsj=0mode handled separately (its derivative vanishes identically for any order).DirichletLegendre.basis_function— mirrorDirichletChebyshev.basis_function, using Legendre polynomials instead of Chebyshev.NeumannLegendreandNeumannChebyshev(__init__,basis_function) — composite spaces enforcing homogeneous derivative (Neumann) boundary conditions via a stencil matrixS, following the same pattern asDirichletLegendre/DirichletChebyshev, but solving for a stencil coefficient that makesψ_j'(±1) = 0. See lecture 11 for the boundary-derivative values to use.
The module docstring and class docstrings in galerkin.py describe the architecture — mapping helpers, FunctionSpace and its orthogonal/trigonometric/composite subclasses, the Dirichlet/Neumann boundary lifts, and the TrialFunction/TestFunction/inner weak-form domain-specific language (DSL). Start there (python -c "help(__import__('galerkin'))", or just read top-to-bottom) before diving into the blanks above.
- Domain mapping. Always go through
map_reference_domain/map_true_domainbefore evaluating a basis function or its derivative — don't evaluate directly in the physical domain. - Chebyshev weight.
Chebyshev.inner_productsubstitutesx = cos(θ)and integrates over[0, π]to handle the1/√(1−x²)weight; don't use plainquadon[-1, 1]there.assemble_generic_matrixinstead passesweight="alg", wvar=(-0.5, -0.5)toquadfor the same reason. - Coefficient lengths. Simple spaces use
N+1coefficients; composite spaces use a stencilSof shape(N+1, N+3)and a diagonalMof lengthN+3in the underlying orthogonal basis. - Trigonometric derivatives.
Sines.derivative_basis_functionshows the scale/sign pattern for((j+1)π)^k;Cosinesneeds the analogous pattern for(jπ)^k, with thej=0mode's derivative identically zero for any order. - Boundary lifts in the RHS. When assembling
b = inner(f - operator(B.x), v), remember to subtract the same differential operator applied to the liftB.x, as shown intest_helmholtz/test_convection_diffusion. - Type hints &
@overload. Method signatures are fully type-annotated, so they describe exactly what each blank should accept and return.basis_function(j, sympy=False)returns a different type depending onsympy— a callable whensympy=False, a sympy expression whensympy=True— which a plain type hint can't express precisely. It's declared with two@overloadstubs (one per literal value ofsympy), followed by the actual (untyped) implementation; only the two stubs are seen by the type checker, so it can pick the right return type at each call site instead of an imprecise union of both. Your implementation should keepuv run ty check galerkin.pypassing — it catches a wrong return type or shape immediately, well before the (much slower) numerical tests would.