diff --git a/.github/workflows/validate_refnx.yml b/.github/workflows/validate_refnx.yml index 91e9cdd..a728dd0 100644 --- a/.github/workflows/validate_refnx.yml +++ b/.github/workflows/validate_refnx.yml @@ -31,7 +31,7 @@ jobs: python3 -m pip install --upgrade pip python3 -m pip install wheel setuptools python3 -m pip install numpy scipy cython pytest black - python3 -m pip install refnx + python3 -m pip install git+https://github.com/refnx/refnx.git@main - name: Validate run: | diff --git a/validation/scripts/test_genx.py b/validation/scripts/test_genx.py index 91831f4..1a3c914 100644 --- a/validation/scripts/test_genx.py +++ b/validation/scripts/test_genx.py @@ -201,8 +201,7 @@ def kernel_test_pol(slabs, data, backend): magn_ang=theta, ) ) - # layers.reverse() # the layer order in pol tests - # is currently reversed compared to unpol + layers.reverse() stack = model.Stack(Layers=list(layers[1:-1]), Repetitions=1) sample = model.Sample( Stacks=[stack], Ambient=layers[-1], Substrate=layers[0] diff --git a/validation/scripts/test_refl1d.py b/validation/scripts/test_refl1d.py index d06a13b..7757383 100644 --- a/validation/scripts/test_refl1d.py +++ b/validation/scripts/test_refl1d.py @@ -132,7 +132,7 @@ def test_pol_refl1d(nsd, backend): """ test_name, slabs, data, AGUIDE, H = nsd - Rmm, Rmp, Rpm, Rpp = pol_kernel_test(slabs[::-1], data, AGUIDE, H, backend) + Rmm, Rmp, Rpm, Rpp = pol_kernel_test(slabs, data, AGUIDE, H, backend) def pol_kernel_test(slabs, data, AGUIDE, H, backend): diff --git a/validation/scripts/test_refnx.py b/validation/scripts/test_refnx.py index 273d6b3..b401153 100644 --- a/validation/scripts/test_refnx.py +++ b/validation/scripts/test_refnx.py @@ -2,9 +2,16 @@ import warnings import pytest import numpy as np -from test_discovery import get_test_data +from test_discovery import get_test_data, get_polarised_test_data -from refnx.reflect import use_reflect_backend, SLD, ReflectModel, Structure +from refnx.reflect import ( + use_reflect_backend, + SLD, + ReflectModel, + Structure, + MagneticSlab, + PolarisedReflectModel, +) # cython backend may or may not be present on all systems @@ -78,6 +85,75 @@ def resolution_test(slabs, data, backend): np.testing.assert_allclose(R, data[:, 1], rtol=0.03) +pol_tests = list(get_polarised_test_data()) +pol_ids = [f"{t[0]}" for t in pol_tests] + + +@pytest.mark.parametrize("nsd", pol_tests, ids=pol_ids) +def test_refnx_pol(nsd): + """ + Run validation for genx. + + Parameters + ---------- + nsd: tuple + test_name, slabs, data + """ + # NCOLS of data: + # 2 - test kernel only + # 3 - test kernel and chi2 calculation + # 4 - test resolution smearing and chi2 calculation + test_name, slabs, data, AGUIDE, H = nsd + if H > 0: + pytest.skip("refnx does not support Zeeman splitting (yet)") + return + + slabs = np.array(slabs) + kernel_test_pol(slabs, data, AGUIDE) + + +def kernel_test_pol(slabs, data, AGUIDE): + """ + Test the polarised reflectivity kernel for refnx. + + Parameters + ---------- + slabs: np.ndarray + Slab representation of the system + data: np.ndarray + Q, R arrays (--, -+, +-, ++) + AGUIDE: float + AGUIDE (degrees) + """ + Q = data[:, 0] + npts = len(Q) + + layers = [] + for thickness, rsld, isld, theta, sldm, sigma in slabs: + slab = MagneticSlab( + thickness, rsld + 1j * isld, sigma, sldm, thetaM=theta + ) + layers.append(slab) + s = Structure(components=layers) + model = PolarisedReflectModel(s, bkgs=0.0, dq=0.0, Aguide=AGUIDE) + qq = np.full((len(Q) * 4, 4), np.nan) + qq[0:npts, 0] = Q + qq[npts : 2 * npts, 1] = Q + qq[2 * npts : 3 * npts, 2] = Q + qq[3 * npts : 4 * npts, 3] = Q + R = model(qq) + Ruu = R[0:npts] + Rud = R[npts : 2 * npts] + Rdu = R[2 * npts : 3 * npts] + Rdd = R[3 * npts : 4 * npts] + np.testing.assert_allclose(Ruu, data[:, 4], rtol=8e-5) + np.testing.assert_allclose(Rdd, data[:, 1], rtol=8e-5) + np.testing.assert_allclose(Rud, data[:, 3], rtol=8e-5) + np.testing.assert_allclose(Rdu, data[:, 2], rtol=8e-5) + + if __name__ == "__main__": for nsd, backend in tests_backends: test_refnx(nsd, backend) + for nsd, backend in pol_tests_backends: + test_refnx_pol(nsd, backend) diff --git a/validation/test/polarised/layers/test0.layers b/validation/test/polarised/layers/test0.layers index 67a0d7c..854570d 100644 --- a/validation/test/polarised/layers/test0.layers +++ b/validation/test/polarised/layers/test0.layers @@ -1,5 +1,5 @@ #thickness sld mu thetaM sldm roughness -0.000 0.000 0.000 90.00 0.000 0.000 -50.00 4.000 0.000 90.00 0.000 0.000 +0.000 2.070 0.000 90.00 0.000 0.000 825.0 9.060 0.000 90.00 0.000 0.000 -0.000 2.070 0.000 90.00 0.000 0.000 \ No newline at end of file +50.00 4.000 0.000 90.00 0.000 0.000 +0.000 0.000 0.000 90.00 0.000 0.000 diff --git a/validation/test/polarised/layers/test1.layers b/validation/test/polarised/layers/test1.layers index edf230e..5587c93 100644 --- a/validation/test/polarised/layers/test1.layers +++ b/validation/test/polarised/layers/test1.layers @@ -1,5 +1,5 @@ #thickness sldn sldi thetaM sldm roughness -0 4 0 90.00 0 0 -200 2 0 90.00 1 0 -200 4 0 180.0 1 0 0 0 0 90.00 0 0 +200 4 0 180.0 1 0 +200 2 0 90.00 1 0 +0 4 0 90.00 0 0