Architecture and Source Map¶
This page is the shortest path from the public API to the underlying analytical kernels.
Layered structure¶
The repository is organized in layers.
Public API layer¶
src/magpylib_jax/__init__.py— package surface (does not mutate the global JAX config).src/magpylib_jax/functional.py— thin re-export facade over thefields/package (getB/getH/getJ/getM/getFT).
This layer handles:
object construction,
compatibility-oriented
getB/getH/getJ/getMbehavior,source/sensor formatting,
path and orientation semantics,
pixel aggregation,
squeeze and broadcasting behavior.
Field engine layer (fields/)¶
fields/api.py— publicgetB/getH/getJ/getMand the_compute_fieldrouter.fields/prepare.py— source/sensor/observer preparation and padding.fields/engine.py— the vectorized JIT evaluation engine (default path).fields/eager.py— eager reference evaluator for output modes outside JIT.fields/force.py—getFT, autodiff force and torque.
Object base layer¶
This layer handles:
shared object state,
path mutation semantics,
orientation storage and caching,
validation of constructor and motion inputs,
lightweight style compatibility.
Geometry and kernel layer¶
src/magpylib_jax/core/geometry.py— frame transforms, cartesian↔cylindrical, pose broadcasting.src/magpylib_jax/core/kernels/— the analytic field kernels, one module per source family (dipole,circle,cuboid,cylinder,sphere,polyline,triangle,tetrahedron,trimesh,cylinder_segment,current_sheet,current_strip), pluselliptic(Bulirschcel),_raycast(mesh inside-tests), and_safe/_commonhelpers.
This is where the actual field formulas live. Each kernel is a pure function of origin-local observer coordinates and is differentiable in JAX.
Source wrappers¶
Current-driven sources:
Magnet sources:
Miscellaneous sources:
How getB flows through the code¶
A typical high-level getB call follows this path:
Validate inputs and normalize source/sensor descriptors.
Prepare source tensors and sensor tensors, reusing caches where possible.
Group homogeneous source families for efficient batched evaluation.
Call the matching analytical kernel.
Rotate the resulting field back to the global frame.
Apply sensor aggregation and Magpylib-compatible squeeze behavior.
The orchestration lives in the fields/
package: api.py routes the call, prepare.py builds the batched tensors, and engine.py
runs the vectorized JIT kernel. Force and torque follow the same field path through
fields/force.py,
adding a jax.jacfwd of the field for the magnet gradient term.
Where to profile¶
kernel compile/runtime:
scripts/profile_kernels.pyhigh-level
getBoverhead:scripts/profile_getB_jit.pyfigures/benchmarks:
scripts/make_figures.py