A Python implementation of Finite Difference Time Domain (FDTD) electromagnetic wave simulation.
The finite difference time domain method, in short FDTD, is used to numerically compute the propagation of electromagnetic waves, that is, to solve the Maxwell equations for arbitrary environments.
This is a sample image of a FDTD simulation in 2 dimensions. Features of note:
- a pulsing point source at
(50, 50) - a Luneburg lens with a focal point around
(20, 90) - a "bar" of low (high?) permittivity from
(25, 40)to(40, 25) - an area of perfect conductivity in the bottom right corner
- an absorbing boundary (otherwise, the limits of the simulation act like perfect reflectors, c.f. the following image)
- Multiple PML implementations: Berenger PML (BPML), Convolutional PML (CPML), and no PML
- Flexible configuration: Command-line interface with customizable parameters
- Visualization: Animated 2D plots and comparison plots
- Performance benchmarking: Built-in timing and comparison tools
- Type hints: Modern Python with full type annotations
- Python 3.7 or higher
- pip
pip install -r requirements.txtThis will install:
numpy(>=1.20.0)scipy(>=1.7.0)matplotlib(>=3.4.0)pytest(>=7.0.0) - for running unit testspytest-cov(>=4.0.0) - for test coverage
The simulation can be run from the command line with various options:
cd src
python main.py <action> [options]Run a normal simulation with BPML:
python main.py normal --pml bpmlRun with custom grid size:
python main.py normal --pml cpml --nx 150 --ny 150 --nt 300Compare BPML and CPML:
python main.py comparisonRun performance benchmark:
python main.py time --repetitions 50Test PML effectiveness:
python main.py pml --pml cpmlTest lossy materials:
python main.py lossnormal- Run a standard simulation with visualizationpml- Compare small vs large simulation with PMLloss- Test effect of different conductivity valuescomparison- Compare BPML vs CPML implementationstime- Benchmark performance of different PML typestrivial- Benchmark source function overheadall- Run all tests sequentially
PML Options:
--pml {no,bpml,cpml}- PML implementation type (default: bpml)
Simulation Parameters:
--nx INT- Number of cells in x direction (default: 100)--ny INT- Number of cells in y direction (default: 100)--dx FLOAT- Spatial discretization in x (meters, default: 0.05)--dy FLOAT- Spatial discretization in y (meters, default: 0.05)--nt INT- Number of time steps (default: 200)--source-x INT- Source x position (default: center)--source-y INT- Source y position (default: center)
PML Configuration:
--pml-thickness INT- PML thickness in cells (default: 10)--pml-r0 FLOAT- PML reflection factor R0 (default: 1e-6)--pml-grading INT- PML grading parameter (default: 3)
Test Parameters:
--repetitions INT- Number of repetitions for timing tests (default: 100)
For detailed help and examples:
python main.py --help
python main.py <action> --helpsrc/
├── main.py # CLI entry point
├── config.py # Configuration dataclasses and enums
├── sources.py # Source function definitions
├── common.py # Common utilities (fields, plotting, environment)
├── callers.py # Wrapper functions for PML implementations
├── tests.py # Test and comparison functions
├── no_pml.py # FDTD without PML
├── bpml.py # Berenger PML implementation
└── cpml.py # Convolutional PML implementation
The project includes a comprehensive unit test suite using pytest.
Run all tests:
pytestRun with verbose output:
pytest -vRun specific test file:
pytest tests/test_common.pyRun specific test:
pytest tests/test_common.py::TestFieldCreation::test_make_fieldsRun with coverage report:
pytest --cov=src --cov-report=htmlThis will generate an HTML coverage report in htmlcov/index.html.
The test suite includes:
-
Unit tests for core functions:
test_sources.py- Source function teststest_config.py- Configuration dataclass teststest_common.py- Common utility function teststest_no_pml.py- No-PML implementation teststest_bpml.py- Berenger PML teststest_cpml.py- Convolutional PML tests
-
Integration tests:
test_callers.py- Integration tests for caller functions
All tests are designed to run quickly and verify correctness of the core FDTD algorithms.
This project has been modernized with:
- Full type hints throughout
- Proper error handling
- Command-line interface
- Clean separation of concerns
- Comprehensive documentation
- Modern Python best practices
- Comprehensive unit test suite
This code was written for a bachelor thesis in physics. Please respect the original author's work.