Core FDTD Algorithms¶
Memory-Efficient Implementations¶
Reversible FDTD¶
fdtdx.fdtd.reversible_fdtd(arrays, objects, config, key)
¶
Run a memory-efficient differentiable FDTD simulation leveraging time-reversal symmetry.
This implementation exploits the time-reversal symmetry of Maxwell's equations to perform backpropagation without storing the electromagnetic fields at each time step. During the backward pass, the fields are reconstructed by running the simulation in reverse, only requiring O(1) memory storage instead of O(T) where T is the number of time steps.
The only exception is boundary conditions which break time-reversal symmetry - these are recorded during the forward pass and replayed during backpropagation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays
|
ArrayContainer
|
Initial state of the simulation containing: - E, H: Electric and magnetic field arrays - inv_permittivities, inv_permeabilities: Material properties - boundary_states: Dictionary of boundary conditions - detector_states: Dictionary of field detectors - recording_state: Optional state for recording field evolution |
required |
objects
|
ObjectContainer
|
Collection of physical objects in the simulation (sources, detectors, boundaries, etc.) |
required |
config
|
SimulationConfig
|
Simulation parameters including: - time_steps_total: Total number of steps to simulate - invertible_optimization: Whether to record boundaries for backprop |
required |
key
|
Array
|
JAX PRNGKey for any stochastic operations |
required |
Returns:
Name | Type | Description |
---|---|---|
SimulationState |
SimulationState
|
Tuple containing: - Final time step (int) - ArrayContainer with the final state of all fields and components |
Notes
The implementation uses custom vector-Jacobian products (VJPs) to enable efficient backpropagation through the entire simulation while maintaining numerical stability. This makes it suitable for gradient-based optimization of electromagnetic designs.
Source code in src/fdtdx/fdtd/fdtd.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
|
Time-reversal symmetric FDTD implementation with O(1) memory usage.
Checkpointed FDTD¶
fdtdx.fdtd.checkpointed_fdtd(arrays, objects, config, key)
¶
Run an FDTD simulation with gradient checkpointing for memory efficiency.
This implementation uses checkpointing to reduce memory usage during backpropagation by only storing the field state at certain intervals and recomputing intermediate states as needed.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays
|
ArrayContainer
|
Initial state of the simulation containing fields and materials |
required |
objects
|
ObjectContainer
|
Collection of physical objects in the simulation |
required |
config
|
SimulationConfig
|
Simulation parameters including checkpointing settings |
required |
key
|
Array
|
JAX PRNGKey for any stochastic operations |
required |
Returns:
Name | Type | Description |
---|---|---|
SimulationState |
SimulationState
|
Tuple containing final time step and ArrayContainer with final state |
Notes
The number of checkpoints can be configured through config.gradient_config.num_checkpoints. More checkpoints reduce recomputation but increase memory usage.
Source code in src/fdtdx/fdtd/fdtd.py
Gradient checkpointing FDTD implementation for memory-performance tradeoff.
Custom Time Evolution¶
fdtdx.fdtd.custom_fdtd_forward(arrays, objects, config, key, reset_container, record_detectors, start_time, end_time)
¶
Run a customizable forward FDTD simulation between specified time steps.
This function provides fine-grained control over the simulation execution, allowing partial time evolution and customization of recording behavior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays
|
ArrayContainer
|
Initial state of the simulation |
required |
objects
|
ObjectContainer
|
Collection of physical objects |
required |
config
|
SimulationConfig
|
Simulation parameters |
required |
key
|
Array
|
JAX PRNGKey for stochastic operations |
required |
reset_container
|
bool
|
Whether to reset the array container before starting |
required |
record_detectors
|
bool
|
Whether to record detector readings |
required |
start_time
|
int | Array
|
Time step to start from |
required |
end_time
|
int | Array
|
Time step to end at |
required |
Returns:
Name | Type | Description |
---|---|---|
SimulationState |
SimulationState
|
Tuple containing final time step and ArrayContainer with final state |
Notes
This function is useful for implementing custom simulation strategies or running partial simulations for analysis purposes.
Source code in src/fdtdx/fdtd/fdtd.py
Customizable FDTD implementation for partial time evolution and analysis.
Forward Propagation¶
fdtdx.fdtd.forward.forward(state, config, objects, key, record_detectors, record_boundaries, simulate_boundaries)
¶
Performs one forward time step of the FDTD simulation.
Implements the core FDTD update scheme based on Maxwell's equations discretized on the Yee grid. Updates include: 1. Electric field update using curl of H field 2. Magnetic field update using curl of E field 3. Optional PML boundary conditions 4. Optional detector state updates 5. Optional recording of boundary values for gradient computation
The implementation leverages JAX for automatic compilation and GPU acceleration. Field updates follow the standard staggered time stepping of the Yee scheme.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
SimulationState
|
Current simulation state (time step and field values) |
required |
config
|
SimulationConfig
|
Simulation configuration parameters |
required |
objects
|
ObjectContainer
|
Container with sources, PML and other simulation objects |
required |
key
|
Array
|
Random key for compression |
required |
record_detectors
|
bool
|
Whether to record detector values |
required |
record_boundaries
|
bool
|
Whether to record boundary values for gradients |
required |
simulate_boundaries
|
bool
|
Whether to apply PML boundary conditions |
required |
Returns:
Type | Description |
---|---|
SimulationState
|
Updated simulation state for the next time step |
Source code in src/fdtdx/fdtd/forward.py
Standard forward FDTD time stepping implementation.
fdtdx.fdtd.forward.forward_single_args_wrapper(time_step, E, H, inv_permittivities, inv_permeabilities, boundary_states, detector_states, recording_state, config, objects, key, record_detectors, record_boundaries, simulate_boundaries)
¶
Wrapper function that unpacks ArrayContainer into individual arrays for JAX transformations.
This function provides a JAX-compatible interface by handling individual arrays instead of container objects. It converts between the array-based interface required by JAX and the object-oriented ArrayContainer interface used by the rest of the FDTD implementation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
time_step
|
Array
|
Current simulation time step |
required |
E
|
Array
|
Electric field array |
required |
H
|
Array
|
Magnetic field array |
required |
inv_permittivities
|
Array
|
Inverse permittivity values |
required |
inv_permeabilities
|
Array
|
Inverse permeability values |
required |
boundary_states
|
dict[str, BoundaryState]
|
PML boundary conditions state |
required |
detector_states
|
dict[str, DetectorState]
|
States of field detectors |
required |
recording_state
|
RecordingState | None
|
Optional state for recording field values |
required |
config
|
SimulationConfig
|
Simulation configuration parameters |
required |
objects
|
ObjectContainer
|
Container with sources and other simulation objects |
required |
key
|
Array
|
Random key for compression |
required |
record_detectors
|
bool
|
Whether to record detector values |
required |
record_boundaries
|
bool
|
Whether to record boundary values |
required |
simulate_boundaries
|
bool
|
Whether to apply PML boundary conditions |
required |
Returns:
Type | Description |
---|---|
tuple[Array, Array, Array, Array, Array, dict[str, BoundaryState], dict[str, DetectorState], RecordingState | None]
|
Tuple containing: - Updated time step - Updated E field array - Updated H field array - Updated inverse permittivities - Updated inverse permeabilities - Updated boundary states - Updated detector states - Updated recording state |
Source code in src/fdtdx/fdtd/forward.py
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 |
|
JAX-compatible wrapper for forward propagation.
Backward Propagation¶
fdtdx.fdtd.backward.full_backward(state, objects, config, key, record_detectors, reset_fields, start_time_step=0)
¶
Perform full backward FDTD propagation from current state to start time.
Uses a while loop to repeatedly call backward() until reaching start_time_step. Leverages time-reversibility of Maxwell's equations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
SimulationState
|
Current simulation state tuple (time_step, arrays) |
required |
objects
|
ObjectContainer
|
Container with simulation objects (sources, detectors, etc) |
required |
config
|
SimulationConfig
|
Simulation configuration parameters |
required |
key
|
Array
|
JAX PRNG key for random operations |
required |
record_detectors
|
bool
|
Whether to record detector states |
required |
reset_fields
|
bool
|
Whether to reset fields after each step |
required |
start_time_step
|
int
|
Time step to propagate back to (default: 0) |
0
|
Returns:
Name | Type | Description |
---|---|---|
SimulationState |
SimulationState
|
Final state after backward propagation |
Source code in src/fdtdx/fdtd/backward.py
Complete backward FDTD propagation from current state to start time.
fdtdx.fdtd.backward.backward(state, config, objects, key, record_detectors, reset_fields, fields_to_reset=('E', 'H'))
¶
Perform one step of backward FDTD propagation.
Updates fields from time step t to t-1 using time-reversed Maxwell's equations. Handles interfaces, field updates, optional field resetting, and detector recording.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
SimulationState
|
Current simulation state tuple (time_step, arrays) |
required |
config
|
SimulationConfig
|
Simulation configuration parameters |
required |
objects
|
ObjectContainer
|
Container with simulation objects (sources, detectors, etc) |
required |
key
|
Array
|
JAX PRNG key for random operations |
required |
record_detectors
|
bool
|
Whether to record detector states |
required |
reset_fields
|
bool
|
Whether to reset fields after updates |
required |
fields_to_reset
|
Sequence[str]
|
Which fields to reset if reset_fields is True |
('E', 'H')
|
Returns:
Name | Type | Description |
---|---|---|
SimulationState |
SimulationState
|
Updated state after one backward step |
Source code in src/fdtdx/fdtd/backward.py
Single step backward FDTD propagation.