- C++ 56%
- Python 42%
- CMake 2%
Is this slop? #
Yes.
fdtdz-metal #
This repository is a port of SPINS Photonics fdtd-z simulation software, which uses CUDA, to Apple's Metal. This package, fdtdz-metal, uses the finite-difference time-domain (FDTD) method. It calculates electric and magnetic fields on a grid. You can use it for photonic parts, such as a splitter.
The code runs on macOS with Apple Silicon. It uses Metal for the graphics processing unit (GPU). It has no CUDA or JAX code. The C++ build does not need Python.
The project includes a central processing unit (CPU) calculation for tests and speed measurements. There is no CPU-only build.
Before you start #
Before the C++ build, make sure that you have:
- A Mac with Apple Silicon and macOS.
- Apple Command Line Tools with C++20 support.
- CMake 3.18 or later.
Metal makes the GPU programs when the calculation starts. The Xcode offline Metal compiler is not necessary.
Make the programs #
Use these commands from the project directory:
cmake -S . -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j
ctest --test-dir build --output-on-failure
The last command does the tests.
Some tools can block GPU access.
If this occurs, start ./build/fdtdz_metal_tests in Terminal.
Start a small calculation #
./build/fdtdz_benchmark \
--nx 32 --ny 32 --nz 32 --steps 20 --repetitions 1
This command compares the CPU calculation with Metal FP32 and FP16. It prints the times, rates, and field errors. FP32 stores each field value in 32 bits. FP16 stores each field value in 16 bits. Use FP32 first. FP16 can give a larger error.
Use the C++ library #
Save this code as build/minimal.cpp:
#include <fdtdz/metal.hpp>
#include <iostream>
#include <vector>
int main() {
fdtdz::Grid grid{32, 32, 32};
auto state = fdtdz::State::zero(grid);
auto coefficients = fdtdz::Coefficients::vacuum(grid);
constexpr float dt = 0.35f;
constexpr std::uint32_t steps = 100;
coefficients.setPolynomialCPML(4, 4, dt);
fdtdz::CurrentSource source{
16, 16, 16, fdtdz::Component::x,
std::vector<float>(steps, 0.0f)};
source.waveform[0] = 1.0f;
fdtdz::MetalBackend metal;
const auto statistics = metal.run(
state, coefficients, dt, steps, fdtdz::Precision::float32, &source);
std::cout << metal.deviceName() << '\n'
<< "GPU time: " << statistics.gpuSeconds << " s\n";
return 0;
}
Make the example program:
c++ -std=c++20 -O2 -I include build/minimal.cpp build/libfdtdz_metal.a \
-framework Foundation -framework Metal -o build/minimal
Start the example program:
./build/minimal
The example starts with zero fields.
The source adds one pulse to the electric field.
The program calculates 100 time steps.
The final fields are in state.electric and state.magnetic.
The source adds each waveform value to the electric field.
Grid lengths use cells as the unit.
Time uses units in which the vacuum speed of light is one.
The value of dt is a time step in these units.
It is not a time in seconds.
Do not use this time step for all materials.
Make the splitter video and plots #
This example uses a flat waveguide with one input and two outputs. It is a 2D model, not a round 3D fiber. The calculation uses FP32.
Python, NumPy, SciPy, Matplotlib, Pillow, and FFmpeg are necessary for this example. We used Python 3.12 for this example. The FFmpeg program must be on your PATH.
Install the Python packages:
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
The next commands replace the saved results in artifacts/y_splitter/.
The frame file uses approximately $1,\mathrm{GB}$ of disk space.
-
Make the input files:
.venv/bin/python examples/y_splitter.py --stage prepare --work build/y_splitter_long -
Start the Metal calculation:
./build/fdtdz_planar build/y_splitter_long/problem.bin build/y_splitter_long -
Calculate the fields with the finite-difference frequency-domain (FDFD) method:
.venv/bin/python examples/y_splitter.py --stage fdfd --work build/y_splitter_long -
Make the video and plots:
.venv/bin/python examples/y_splitter.py --stage render --work build/y_splitter_long
The FDFD calculation is separate from the FDTD calculation. Both methods use the same grid, materials, and source. The program compares their field and power results. This comparison does not show the error from the cell size. See the example instructions for a check with smaller cells.
The output directory is artifacts/y_splitter/:
y_splitter.mp4: The video from start to end.fdfd_steady_state.png: The FDFD field and power plot.fdtd_fdfd_comparison.png: The field comparison.metrics.json: The power results and field errors.steady_state_fields.npz: The field data and grid.
Saved splitter results #
The wavelength is $1.55,\mu\mathrm{m}$. The splitter sends $99.23%$ of the input guided power to its two outputs. Each output receives half of this power. The output power split is $50{:}50$.

The grid has $1800\times360$ cells. The calculation has $12{,}288$ time steps. These results use FP32 on an Apple M1 Max. Each value below is the middle result from five runs.
| Measurement | Result |
|---|---|
| GPU time | $0.935,\mathrm{s}$ |
| Program time | $2.81,\mathrm{s}$ |
| GPU rate | $8.51\times10^9$ cell updates per second |
One cell update changes both the electric and magnetic fields. GPU time includes the field updates and the sums for the frequency fields. Program time also includes the work to start the program and write output files. Neither time includes input preparation, FDFD, plots, or video creation. File writes use the system cache without a forced disk write. See the saved measurements for the full record.
More examples #
The bend video, 3D coupler video, and ring video show three more examples. The bend sends $80.58%$ of input guided power to its main output mode. The 3D coupler gives a near-equal split and $98.65%$ of the reference output power. The ring shows decay, but its resonance changes with cell size. On Apple M1 Max, their GPU rates are $5.72$, $2.06$, and $4.84$ billion cell updates per second. The benchmark report gives power checks, memory-limit tests, and all saved results.
Measure speed #
For the 3D calculation, use:
./build/fdtdz_benchmark \
--nx 128 --ny 128 --nz 64 --steps 120 --repetitions 5
This program compares the CPU calculation with Metal FP32 and FP16.
For Metal, kernel s is the GPU time.
The wall s column includes the work to send commands and wait for the GPU.
Neither Metal column includes the work to prepare materials, get memory, or copy fields.
For the splitter, use this command after you make its input files:
python3 examples/benchmark_y_splitter.py --repetitions 5
This command does one full run before five measured runs.
It compares the final field files from these runs.
It replaces artifacts/y_splitter/benchmark.json.
It does not make the video.
The 2D and 3D rates describe different calculations.
See the method notes for more information.
Files #
- include/fdtdz/: C++ types and functions.
- src/: Metal code and the CPU calculation.
- tests/: Field and source tests.
- benchmarks/: Speed measurements.
- examples/: Splitter input, FDFD, plots, and video.
- docs/metal-port.md: Model, methods, and limits.
- artifacts/y_splitter/: Saved results.
Limits #
The C++ library calculates 3D fields with FP32 or FP16. The 2D function uses FP32 and can write frame files and frequency fields. There is no Python package or JAX interface. The example sends its input to a separate Metal program. The code does not yet change a design to meet a target.
Source and license #
This project uses methods from SPINS Photonics fdtdz. The original CUDA and JAX code is not included. The original copyright notice and MIT license remain in LICENSE.
