#!/usr/bin/env python3
"""
Spin-flip scattering by magnetic spheres
"""
import bornagain as ba
from bornagain import ba_plot as bp, deg, nm, nm2, R3


def get_sample():
    # Materials
    B = R3(0, 0, 1e7)
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 2e-05, 4e-07, B)
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial(
        "Substrate", substrate_color, 7e-06, 1.8e-07)
    vacuum = ba.Vacuum()

    # Particles
    ff = ba.Sphere(5*nm)
    particle = ba.Particle(particle_mat, ff)

    # Multilayer
    layer_1 = ba.Layer(vacuum)
    layer_2 = ba.Layer(substrate_mat)
    layer_2.suspend2D(ba.Dilute2D(0.01/nm2, particle))

    sample = ba.Sample()
    sample.addLayer(layer_1)
    sample.addLayer(layer_2)

    return sample


def get_simulation(sample):
    beam = ba.Beam(1e12, 0.1*nm, 0.5*deg)
    n = 11
    detector = ba.SphericalDetector(n, -3*deg, 3*deg, n, 0, 4*deg)

    polarizer_vec = R3(0, 0, 1)
    analyzer_vec = R3(0, 0, -1)
    beam.setPolarization(polarizer_vec)
    detector.setAnalyzer(analyzer_vec)

    return ba.ScatteringSimulation(beam, sample, detector)


if __name__ == '__main__':
    sample = get_sample()
    simulation = get_simulation(sample)
    result = simulation.simulate()
    from bornagain import ba_check
    ba_check.persistence_test(result)
