A Julia implementation of the Spectral Element Method (SEM) for solving the 2D Poisson equation:
-∇²u = f in Ω
with Dirichlet boundary conditions.
This solver uses high-order spectral elements with Gauss-Legendre quadrature for accurate numerical solutions. The implementation features:
- Higher-order polynomial basis functions (configurable order r)
- Gauss-Legendre quadrature for numerical integration
- Structured rectangular mesh generation
- Support for Dirichlet and periodic boundary conditions
- Error computation against known analytical solutions
Poisson.jl
- Main solver implementationMesh2Drect.jl
- Rectangular mesh generatorintNodes.jl
- Internal node generation for higher-order elementsID.jl
- Global degree of freedom assignment
basis.jl
- Lagrange polynomial basis functionsbasisx.jl
- Derivatives of basis functionsGLpw.jl
- Gauss-Lobatto points and weights computation
KMtrx.jl
- Stiffness matrix assemblyMMtrx.jl
- Mass matrix assemblyintWeights.jl
- Integration weights computation
ChkNodeEx.jl
- Node existence checkerbubble_sort.jl
- Sorting utilityf_ex.jl
- Source term functionex_sol.jl
- Exact solution for error computationfemerror.jl
- Error calculation functions
-
Set problem parameters in
Poisson.jl
:# Problem size Nx = 4 # Number of elements in x direction Ny = 4 # Number of elements in y direction r = 4 # Polynomial order # Domain size Lx = 1.0 # Length in x direction Ly = 1.0 # Length in y direction
-
Run the solver:
include("Poisson.jl")
-
The solution and error metrics will be computed and displayed.
-
The degree of freedom (DOF) generation process in
ID.jl
andChkNodeEx.jl
currently uses a naive approach that:- Assigns DOFs sequentially to all nodes
- Uses
ChkNodeEx.jl
to check for existing nodes through direct array comparisons - Then searches for and eliminates duplicate DOFs using nested loops This becomes a significant bottleneck for large meshes or high polynomial orders.
-
The DOF assignment and node checking could be optimized by:
- Using more efficient data structures (e.g., hash sets) to track unique DOFs and nodes
- Implementing a smarter initial assignment that avoids duplicates
- Replacing the direct array comparisons in
ChkNodeEx.jl
with spatial partitioning - Parallelizing the duplicate detection process
This node/DOF handling is currently the main performance bottleneck when scaling to larger problems or higher orders.
This code is released under the MIT License. See LICENSE file for details.