#!/usr/bin/env python3
"""
Specular reflectometry of a Ti/Ni multilayer with a non-unit beam
intensity factor. Demonstrates that angle-scan and Q-scan yield
consistent results when the same intensity factor is applied.

10 bilayers of Ti (3 nm) and Ni (7 nm) on a Si substrate.
"""
import numpy as np
from math import pi, sin
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm


INTENSITY = 1.05


def get_sample():
    vacuum = ba.Vacuum()
    si_color = (0.28, 0.57, 0.82)
    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)

    stack = ba.LayerStack(10)
    stack.addLayer(ba.Layer(ti_mat, 3*nm))
    stack.addLayer(ba.Layer(ni_mat, 7*nm))

    sample = ba.Sample()
    sample.addLayer(ba.Layer(vacuum))
    sample.addStack(stack)
    sample.addLayer(ba.Layer(si_mat))
    return sample


def simulate(sample):
    n = 500
    lam = 1.54*angstrom

    scan_a = ba.AlphaScan(n, 5*deg/n, 5*deg)
    scan_a.setWavelength(lam)
    scan_a.setIntensity(INTENSITY)
    result_a = ba.SpecularSimulation(scan_a, sample).simulate()
    result_a.setTitle("angle scan")

    qs = [4*pi*sin(a)/lam
          for a in np.linspace(5*deg/n, 5*deg, n)]
    scan_q = ba.QzScan(qs)
    scan_q.setIntensity(INTENSITY)
    result_q = ba.SpecularSimulation(scan_q, sample).simulate()
    result_q.setTitle("Q scan")

    return [result_a, result_q]


if __name__ == '__main__':
    sample = get_sample()
    ba.showSample3D(sample, sample_size=120*nm, seed=0)
    results = simulate(sample)
    bp.plot_multicurve(results)
    bp.plt.show()
