Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Adds tests for multiple backends(numpy, jax, array_api_strict, dask).
  • Loading branch information
purepani committed Jun 2, 2025
commit de2950cfa1ee8a6c8a5d260a033388a8dc371a3c
4 changes: 4 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ dependencies = [
"plotly>=5.16",
"array-api-compat>=1.11.2",
"array-api-extra>=0.7.1",
"typing-extensions>=4.13.2",
]

[dependency-groups]
Expand Down Expand Up @@ -71,6 +72,9 @@ test = [
"jupyterlab",
"anywidget",
"array-api-strict>=2.3.1",
"jax",
"numpy",
"dask[array]",
]
binder = [
"jupytext",
Expand Down
62 changes: 41 additions & 21 deletions tests/test_core.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
# here all core functions should be tested properly - ideally against FEM
from __future__ import annotations

import array_api_strict as xp
import array_api_strict
import dask.array
import jax
import numpy as np
import pytest
from array_api_extra.testing import lazy_xp_function, patch_lazy_xp_functions

from magpylib.core import (
current_circle_Hfield,
Expand All @@ -16,30 +20,46 @@
triangle_Bfield,
)

lazy_xp_function(current_circle_Hfield)
lazy_xp_function(current_polyline_Hfield)
lazy_xp_function(dipole_Hfield)
lazy_xp_function(magnet_cuboid_Bfield)
lazy_xp_function(magnet_cylinder_axial_Bfield)
lazy_xp_function(magnet_cylinder_diametral_Hfield)
lazy_xp_function(magnet_cylinder_segment_Hfield)
lazy_xp_function(magnet_sphere_Bfield)
lazy_xp_function(triangle_Bfield)

def test_magnet_sphere_Bfield():

@pytest.fixture(params=[np, array_api_strict, jax.numpy, dask.array])
def xp(request, monkeypatch):
patch_lazy_xp_functions(request, monkeypatch, xp=request.param)
return request.param


def test_magnet_sphere_Bfield(xp):
"magnet_sphere_Bfield test"
B = magnet_sphere_Bfield(
observers=xp.asarray([(0, 0, 0)]),
diameters=xp.asarray([1]),
polarizations=xp.asarray([(0, 0, 1)]),
)
Btest = xp.asarray([(0, 0, 2 / 3)])
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, rtol=1e-4)


def test_current_circle_Hfield():
def test_current_circle_Hfield(xp):
Htest = xp.asarray([[0.09098208, 0.09415448], [0.0, 0.0], [0.07677892, 0.22625335]])
H = current_circle_Hfield(
r0=xp.asarray([1, 2]),
r=xp.asarray([1, 1]),
z=xp.asarray([1, 2]),
i0=xp.asarray([1, 3]),
)
np.testing.assert_allclose(H, Htest)
np.testing.assert_allclose(H, Htest, rtol=1e-4)


def test_current_polyline_Hfield():
def test_current_polyline_Hfield(xp):
Htest = xp.asarray([[0.0, -2.29720373, 2.29720373], [0.0, 0.59785204, -0.59785204]])

H = current_polyline_Hfield(
Expand All @@ -48,10 +68,10 @@ def test_current_polyline_Hfield():
segments_end=xp.asarray([(1, 0, 0), (-1, 0, 0)]),
currents=xp.asarray([100, 200]),
)
np.testing.assert_allclose(H, Htest)
np.testing.assert_allclose(H, Htest, rtol=1e-4)


def test_dipole_Hfield():
def test_dipole_Hfield(xp):
Htest = xp.asarray(
[
[2.89501155e-13, 1.53146915e03, 1.53146915e03],
Expand All @@ -62,35 +82,35 @@ def test_dipole_Hfield():
observers=xp.asarray([(1, 1, 1), (2, 2, 2)]),
moments=xp.asarray([(1e5, 0, 0), (0, 0, 1e5)]),
)
np.testing.assert_allclose(H, Htest)
np.testing.assert_allclose(H, Htest, atol=1e-6)


def test_magnet_cuboid_Bfield():
def test_magnet_cuboid_Bfield(xp):
Btest = xp.asarray(
[
[1.56103722e-02, 1.56103722e-02, -3.53394965e-17],
[7.73243250e-03, 6.54431406e-03, 1.04789520e-02],
[1.5610372220113404e-02, 1.5610372220113404e-02, -3.5339496460705743e-17],
[7.7324325000007145e-03, 6.5443140645650129e-03, 1.0478952028501879e-02],
]
)
B = magnet_cuboid_Bfield(
observers=xp.asarray([(1, 1, 1), (2, 2, 2)]),
dimensions=xp.asarray([(1, 1, 1), (1, 2, 3)]),
polarizations=xp.asarray([(0, 0, 1), (0.5, 0.5, 0)]),
)
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, atol=1e-6)


def test_magnet_cylinder_axial_Bfield():
def test_magnet_cylinder_axial_Bfield(xp):
Btest = xp.asarray([[0.05561469, 0.04117919], [0.0, 0.0], [0.06690167, 0.01805674]])
B = magnet_cylinder_axial_Bfield(
z0=xp.asarray([1, 2]),
r=xp.asarray([1, 2]),
z=xp.asarray([2, 3]),
)
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, rtol=1e-4)


def test_magnet_cylinder_diametral_Hfield():
def test_magnet_cylinder_diametral_Hfield(xp):
Btest = xp.asarray(
[
[-0.020742122169014, 0.007307203574376],
Expand All @@ -104,10 +124,10 @@ def test_magnet_cylinder_diametral_Hfield():
z=xp.asarray([2, 3]),
phi=xp.asarray([0.1, np.pi / 4]),
)
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, rtol=1e-4)


def test_magnet_cylinder_segment_Hfield():
def test_magnet_cylinder_segment_Hfield(xp):
Btest = xp.asarray(
[
[-1948.14367497, 32319.94437208, 17616.88571231],
Expand All @@ -119,10 +139,10 @@ def test_magnet_cylinder_segment_Hfield():
dimensions=xp.asarray([(1, 2, 0.1, 0.2, -1, 1), (1, 2, 0.3, 0.9, 0, 1)]),
magnetizations=xp.asarray([(1e7, 0.1, 0.2), (1e6, 1.1, 2.2)]),
)
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, rtol=1e-4)


def test_triangle_Bfield():
def test_triangle_Bfield(xp):
Btest = xp.asarray(
[[7.45158965, 4.61994866, 3.13614132], [2.21345618, 2.67710148, 2.21345618]]
)
Expand All @@ -136,4 +156,4 @@ def test_triangle_Bfield():
),
polarizations=xp.asarray([(1.0, 1.0, 1.0), (1.0, 1.0, 0.0)]) * 1e3,
)
np.testing.assert_allclose(B, Btest)
np.testing.assert_allclose(B, Btest, rtol=1e-4)