¶
Object Placement and Parameters¶
fdtdx.fdtd.place_objects(volume, config, constraints, key)
¶
Places simulation objects according to specified constraints and initializes containers.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
volume
|
SimulationObject
|
The volume object defining the simulation boundaries |
required |
config
|
SimulationConfig
|
The simulation configuration |
required |
constraints
|
Sequence[PositionConstraint | SizeConstraint | SizeExtensionConstraint | GridCoordinateConstraint | RealCoordinateConstraint]
|
Sequence of positioning and sizing constraints for objects |
required |
key
|
Array
|
JAX random key for initialization |
required |
Returns:
Type | Description |
---|---|
tuple[ObjectContainer, ArrayContainer, ParameterContainer, SimulationConfig, dict[str, Any]]
|
A tuple containing: - ObjectContainer with placed simulation objects - ArrayContainer with initialized field arrays - ParameterContainer with device parameters - Updated SimulationConfig - Dictionary with additional initialization info |
Source code in src/fdtdx/fdtd/initialization.py
Main entry point for placing and initializing simulation objects.
fdtdx.fdtd.apply_params(arrays, objects, params, key)
¶
Applies parameters to devices and updates source states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
arrays
|
ArrayContainer
|
Container with field arrays |
required |
objects
|
ObjectContainer
|
Container with simulation objects |
required |
params
|
ParameterContainer
|
Container with device parameters |
required |
key
|
Array
|
JAX random key for source updates |
required |
Returns:
Type | Description |
---|---|
tuple[ArrayContainer, ObjectContainer, dict[str, Any]]
|
A tuple containing: - Updated ArrayContainer with applied device parameters - Updated ObjectContainer with new source states - Dictionary with parameter application info |
Source code in src/fdtdx/fdtd/initialization.py
Applies parameters to devices and updates source states to be ready for simulation.
Core FDTD Algorithms¶
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 memory-efficient autodiff.
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 when using autodiff. In most use-cases this performs worse than the reversible FDTD.
fdtdx.fdtd.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. This can be used to check if the compression of boundary interfaces still lead to a physically accurate backward pass.
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. If used smartly, this can make simulation a bit faster, but in most use-cases this is not necessary.
Python Objects used for FDTD simulation¶
fdtdx.fdtd.ArrayContainer
¶
Bases: ExtendedTreeClass
Container for simulation field arrays and states.
This class holds the electromagnetic field arrays and various state information needed during FDTD simulation. It includes the E and H fields, material properties, and states for boundaries, detectors and recordings.
Attributes:
Name | Type | Description |
---|---|---|
E |
Array
|
Electric field array. |
H |
Array
|
Magnetic field array. |
inv_permittivities |
Array
|
Inverse permittivity values array. |
inv_permeabilities |
Array | float
|
Inverse permeability values array. |
boundary_states |
dict[str, BaseBoundaryState]
|
Dictionary mapping boundary names to their states. |
detector_states |
dict[str, DetectorState]
|
Dictionary mapping detector names to their states. |
recording_state |
RecordingState | None
|
Optional state for recording simulation data. |
Source code in src/fdtdx/fdtd/container.py
Container holding the electric/magnetic fields as well as permittivity/permeability arrays for simulation
fdtdx.fdtd.ObjectContainer
¶
Bases: ExtendedTreeClass
Container for managing simulation objects and their relationships.
This class provides a structured way to organize and access different types of simulation objects like sources, detectors, PML/periodic boundaries and devices. It maintains object lists and provides filtered access to specific object types.
Attributes:
Name | Type | Description |
---|---|---|
object_list |
list[SimulationObject]
|
List of all simulation objects in the container. |
volume_idx |
int
|
Index of the volume object in the object list. |
Source code in src/fdtdx/fdtd/container.py
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 |
|
Container holding all the objects in a simulation scene
fdtdx.fdtd.ParameterContainer = dict[str, dict[str, jax.Array] | jax.Array]
module-attribute
¶
Dictionary holding the parameters for every device in the simulation
fdtdx.fdtd.SimulationState = tuple[jax.Array, ArrayContainer]
module-attribute
¶
Simulation state returned by the FDTD simulations. This is a tuple of the simulation time step and an array container.