#!/usr/bin/env python3
"""
Specular reflectometry with sliced cylinders (refractive-index variant).

A layer of cylinders defined with RefractiveMaterial is subdivided into
N horizontal slabs. Results must agree with SpecularWithSlicing2.py
(SLD-material variant), demonstrating the numerical equivalence of both
material representations.
"""
import bornagain as ba
from bornagain import angstrom, ba_plot as bp, deg, nm

HEIGHT = 5 * nm
RADIUS = 5 * nm
SURF_DENSITY = 0.01   # nm^-2
N_SLICES = 3
WAVELENGTH = 1.54 * angstrom


def get_sample():
    vacuum = ba.Vacuum()
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-6, 2e-8)
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-4, 2e-8)

    cylinder = ba.Cylinder(RADIUS, HEIGHT)
    particle = ba.Particle(particle_mat, cylinder)

    top_layer = ba.Layer(vacuum)
    top_layer.deposit2D(ba.Dilute2D(SURF_DENSITY, particle))
    top_layer.setNumberOfSlices(N_SLICES)

    sample = ba.Sample()
    sample.addLayer(top_layer)
    sample.addLayer(ba.Layer(substrate_mat))
    return sample


def get_simulation(sample):
    n = 50
    scan = ba.AlphaScan(n, 5*deg/n, 5*deg)
    scan.setWavelength(WAVELENGTH)
    return ba.SpecularSimulation(scan, sample)


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