#!/usr/bin/env python3
import bornagain as ba
from bornagain import ba_plot as bp, deg, nm


def get_sample():
    # Define materials
    hmdso_color = (0.25, 0.65, 0.35)
    hmdso_mat = ba.RefractiveMaterial("HMDSO", hmdso_color, 2.0888e-06, 1.3261e-08)
    ptfe_color = (0.93, 0.72, 0.25)
    ptfe_mat = ba.RefractiveMaterial("PTFE", ptfe_color, 5.20509e-06, 1.9694e-08)
    si_color = (0.30, 0.62, 0.86)
    si_mat = ba.RefractiveMaterial("Si", si_color, 5.7816e-06, 1.0229e-07)
    vacuum_mat = ba.Vacuum()

    # Define roughness
    autocorr_1 = ba.SelfAffineFractalModel(1.1, 0.3, 5*nm)
    autocorr_2 = ba.SelfAffineFractalModel(2.3, 0.3, 5*nm)

    transient = ba.TanhTransient()

    roughness_1 = ba.Roughness(autocorr_1, transient)
    roughness_2 = ba.Roughness(autocorr_2, transient)

    # Define layers
    layer_1 = ba.Layer(vacuum_mat)
    layer_2 = ba.Layer(hmdso_mat, 18.5*nm, roughness_1)
    layer_3 = ba.Layer(ptfe_mat, 22.1*nm, roughness_2)
    layer_4 = ba.Layer(si_mat)

    # Define sample
    sample = ba.Sample()
    sample.addLayer(layer_1)
    sample.addLayer(layer_2)
    sample.addLayer(layer_3)
    sample.addLayer(layer_4)

    return sample


def get_simulation(sample):
    beam = ba.Beam(1e9, 0.1*nm, 0.4*deg)
    n = 11
    detector = ba.SphericalDetector(n, -2*deg, 2*deg, n, -.6*deg, 1.4*deg)
    detector.setResolutionFunction(
        ba.ResolutionFunction2DGaussian(0.02*deg, 0.02*deg))
    simulation = ba.ScatteringSimulation(beam, 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)
