#!/usr/bin/env python3
"""
Consistence tests: verify structural properties of simulations that cannot
be expressed as persistence tests (exact-zero properties).
"""
import math
import unittest
import bornagain as ba
from bornagain import deg, angstrom, nm, nm2, R3

# ---------------------------------------------------------------------------
# Inline sample builders
# ---------------------------------------------------------------------------


def _make_plain_multilayer_bySLD(n_layers=10, thick_ti=3.5):
    """Non-magnetic Ti/Ni multilayer on Si substrate."""
    thick_ni = 7  # nm
    si_color = (0.55, 0.55, 0.55)
    si_mat = ba.SLDMaterial("Si_substrate", si_color, 2.0704e-6, 2.3726e-11)
    ni_color = (0.93, 0.48, 0.14)
    ni_mat = ba.SLDMaterial("Ni", ni_color, 9.4245e-6, 1.1423e-9)
    ti_color = (0.05, 0.62, 0.55)
    ti_mat = ba.SLDMaterial("Ti", ti_color, -1.9493e-6, 9.6013e-10)
    vacuum = ba.Vacuum()

    sample = ba.Sample()
    sample.addLayer(ba.Layer(vacuum))
    for _ in range(n_layers):
        sample.addLayer(ba.Layer(ti_mat, thick_ti))
        sample.addLayer(ba.Layer(ni_mat, thick_ni))
    sample.addLayer(ba.Layer(si_mat))
    return sample


def _make_cylinders_and_prisms():
    """Mixture of cylinders and prisms on a substrate."""
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-4, 2e-8)
    substrate_color = (0.55, 0.55, 0.55)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-6, 2e-8)
    vacuum = ba.Vacuum()

    cyl = ba.Particle(particle_mat, ba.Cylinder(5*nm, 5*nm))
    prism = ba.Particle(particle_mat, ba.Prism3(10*nm, 5*nm))

    layer_top = ba.Layer(vacuum)
    layer_top.deposit2D(ba.Dilute2D(0.0004/nm2, cyl))
    layer_top.deposit2D(ba.Dilute2D(0.0004/nm2, prism))

    sample = ba.Sample()
    sample.addLayer(layer_top)
    sample.addLayer(ba.Layer(substrate_mat))
    return sample


# ---------------------------------------------------------------------------
# Tests: zero spin-flip on non-magnetic samples
# ---------------------------------------------------------------------------


class ConsistenceTests(unittest.TestCase):

    def test_polarized_scalar_spinflip(self):
        """Spin-flip channel of a scalar (non-magnetic) SLD multilayer must be zero."""
        sample = _make_plain_multilayer_bySLD()
        n = 2000
        max_angle = 5*deg
        scan = ba.AlphaScan(n, max_angle/n, max_angle)
        scan.setWavelength(1.54*angstrom)
        scan.setPolarization(R3(0, 1, 0))  # +Y polarizer  (PM case)
        scan.setAnalyzer(R3(0, -1, 0))  # -Y analyzer
        result = ba.SpecularSimulation(scan, sample).simulate()
        for r in result.intensities():
            self.assertLess(math.fabs(r), 1e-31)

    def test_polarized_scalar_spinflip_particles(self):
        """Spin-flip channel of a scalar (non-magnetic) GISAS sample must be exactly zero."""
        sample = _make_cylinders_and_prisms()
        beam = ba.Beam(1, 1*angstrom, 0.2*deg)
        beam.setPolarization(R3(0, 0, 1))  # +Z polarizer  (PM case)
        detector = ba.SphericalDetector(25, -2*deg, 2*deg, 25, 0, 2*deg)
        detector.setAnalyzer(R3(0, 0, -1))  # -Z analyzer
        result = ba.ScatteringSimulation(beam, sample, detector).simulate()
        for r in result.flatVector():
            self.assertEqual(r, 0)


if __name__ == '__main__':
    unittest.main()
