Compare NumPy and Rust

Choose backend namespaces explicitly without changing global state. This notebook requires the compiled Rust extension and checks both fields and gradients at exterior points.

import numpy as np

from microcubed import get_backend

points = np.array([[0, 0, -150], [80, 30, -120], [-90, 50, 160]]).T
results = {}
for name in ("numpy", "rust"):
    backend = get_backend(name)
    magnet = backend.Magnet([100, 80, 40], [0, 0, 0], [2e5, 1e5, 8e5])
    results[name] = (magnet.Bfield(points), magnet.dBfield(points))

for reference, compiled in zip(results["numpy"], results["rust"]):
    np.testing.assert_allclose(compiled, reference, rtol=1e-9, atol=1e-13)
print("Fields and gradients agree.")
Fields and gradients agree.

Verify the result

These assertions also run in GitHub Actions.

assert all(np.isfinite(value).all() for pair in results.values() for value in pair)