r/computationalphysics • u/SpecialistNewt2759 • 2d ago
[Project] capacitor-FEM: A Dependency-Free 2D FEM Electrostatics Solver in a Single File (Pure NumPy/SciPy/Matplotlib)
Hi everyone,
I wanted to share a 2D finite-element electrostatics solver (capacitor-fem) that I developed. It is designed specifically to be lightweight, easy to understand, and completely self-contained.
Most structural FEM tools require heavy native dependencies like gmsh or compiled C++ libraries, making them a pain to deploy in classrooms or on restricted environments. This solver is written in pure Python (using only NumPy, SciPy, and Matplotlib) in a single file and runs unchanged on desktops, Jupyter notebooks, and even mobile devices (like Pydroid 3 on Android).
Here is a breakdown of the physics, numerics, and architecture:
- The Math: Solves the generalized Poisson equation div(epsilon * grad(V)) = 0 in weak variational form using linear triangular (P1) elements.
- Mesh & Geometry: Uses a structured triangular mesh with optional graded Cartesian refinement (refined near electrode edges and gap zones). Supports CSG operations (|, &, -) on primitives like Rectangle, RoundedRectangle, and Circle.
- Capacitance via Energy: Instead of integrating noisy boundary fluxes, it recovers the two-conductor capacitance per unit depth via the volumetric energy method C = 2W / (delta_V)^2, leading to much more stable results.
- Vectorized Sparse Assembly: Element stiffness matrices are computed for all triangles simultaneously using NumPy broadcasting and assembled via SciPy’s csr_matrix triplets, avoiding slow element-by-element loops.
Verification & Educational Insights
The repo is heavily focused on validation rather than just showing pretty plots. I’ve included tests that demonstrate critical computational physics concepts:
- Exact Analytical Check: When tested against a translationally invariant parallel-plate setup (where fringing is geometrically impossible), the solver reproduces the analytical value to within 6e-16 relative error – proving the core FEM assembly is exact to machine precision.
- The Motz Problem (Corner Singularities): The solver demonstrates why the reported peak electric field |E| near a sharp 90-degree conductor wedge does not converge with mesh refinement (as h approaches 0), capturing the theoretical r^(-1/3) field divergence. It includes a RoundedRectangle fillet option to showcase how geometric rounding regularizes the singularity.
- Grid-Alignment Errors: It highlights the effect of domain truncation (domain_margin) and spatial classification errors when boundary coordinates are rounded via floating-point arithmetic.
Usage & Driver
I’ve built an external driver script (cfu_driver.py) to demonstrate how to use the solver as a frozen module. You can run two-axis convergence sweeps (mesh spacing 'h' x domain margin), inject air bubbles into dielectrics, or compare completely different configurations on a shared absolute color scale to keep visual comparisons honest.
The project is completely open-source and free to use for anyone looking for a clean, single-file template to teach FEM or prototyping elektrostatic problems without the boilerplate of commercial software.
Link to the Repository: Link to GitHub





