#!/usr/bin/env python3
"""
Polarized neutron reflectometry of a simple magnetic layer.

Two-channel (PP, MM) angle-scan with Y-axis polarization.
Compare PolarizedQAngleReflectivity.py for the Q-space equivalent.
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm, R3


def get_sample():
    vacuum = ba.Vacuum()
    layer_color = (0.86, 0.24, 0.18)
    layer_mat = ba.SLDMaterial("MagLayer", layer_color, 1e-4, 1e-8,
                                    R3(0, 1e8, 0))
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.SLDMaterial("Substrate", substrate_color, 7e-5, 2e-6)

    sample = ba.Sample()
    sample.addLayer(ba.Layer(vacuum))
    sample.addLayer(ba.Layer(layer_mat, 10*nm))
    sample.addLayer(ba.Layer(substrate_mat))
    return sample


def simulate(sample, pol, an, title):
    n = 500
    scan = ba.AlphaScan(n, 5*deg/n, 5*deg)
    scan.setWavelength(1.54*angstrom)
    scan.setPolarization(pol)
    scan.setAnalyzer(an)
    result = ba.SpecularSimulation(scan, sample).simulate()
    result.setTitle(title)
    return result


if __name__ == '__main__':
    sample = get_sample()
    results = [
        simulate(sample, R3(0, +1, 0), R3(0, +1, 0), "PP"),
        simulate(sample, R3(0, -1, 0), R3(0, -1, 0), "MM"),
    ]
    plotargs = bp.parse_commandline()
    bp.plot_multicurve(results, **plotargs)
    bp.export(**plotargs)
