#!/usr/bin/env python3
"""
Off-specular scattering from a Ti/Pt resonator on Si with D2O supernatant.

Sample: Si | 3x(Ti(13nm) + Pt(32nm)) | Ti_top(10nm) | TiO2(3nm) | D2O
All interfaces share SelfAffineFractal roughness (sigma=2nm, Hurst=0.8,
corrLen=1e4nm) with TanhTransient and CommonDepthCrosscorrelation (400nm).
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm


def get_sample():
    m_si_color = (0.30, 0.62, 0.86)
    m_si = ba.RefractiveMaterial("Si", m_si_color, 8.25218379931e-06, 0)
    m_ti_color = (0.05, 0.62, 0.55)
    m_ti = ba.RefractiveMaterial(
        "Ti", m_ti_color, -7.6593316363e-06, 3.81961616312e-09)
    m_tio2_color = (0.48, 0.32, 0.80)
    m_tio2 = ba.RefractiveMaterial(
        "TiO2", m_tio2_color, 1.04803530026e-05, 2.03233519385e-09)
    m_pt_color = (0.93, 0.72, 0.25)
    m_pt = ba.RefractiveMaterial(
        "Pt", m_pt_color, 2.52936993309e-05, 7.54553992473e-09)
    m_d2o_color = (0.90, 0.93, 0.97)
    m_d2o = ba.RefractiveMaterial(
        "D2O", m_d2o_color, 2.52897204573e-05, 4.5224432814e-13)

    autocorr = ba.SelfAffineFractalModel(2*nm, 0.8, 1e4*nm)
    transient = ba.TanhTransient()
    crosscorr = ba.CommonDepthCrosscorrelation(400*nm)
    roughness = ba.Roughness(autocorr, transient, crosscorr)

    ti_thickness = 13*nm
    l_ti = ba.Layer(m_ti, ti_thickness, roughness)
    l_pt = ba.Layer(m_pt, 32*nm, roughness)
    l_ti_top = ba.Layer(m_ti, 10*nm, roughness)
    l_tio2 = ba.Layer(m_tio2, 3*nm, roughness)

    sample = ba.Sample()
    sample.addLayer(ba.Layer(m_si))
    for _ in range(3):
        sample.addLayer(l_ti)
        sample.addLayer(l_pt)
    sample.addLayer(l_ti_top)
    sample.addLayer(l_tio2)
    sample.addLayer(ba.Layer(m_d2o, roughness))
    return sample


def get_simulation(sample):
    nscan = 20
    ndet = 25
    scan = ba.AlphaScan(nscan, 0.2*deg, 4*deg)
    scan.setWavelength(5*angstrom)
    scan.setIntensity(1e9)
    detector = ba.OffspecDetector(ndet, 0.1*deg, 5.1*deg, -0.1*deg, 0.1*deg)
    simulation = ba.OffspecSimulation(scan, sample, detector)
    simulation.options().setIncludeSpecular(True)
    return simulation


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