#!/usr/bin/env python3
"""
Dense cylinders, approximated as ideal gas or as hard disks.
Horizontal cut through GISAS image.
"""
import bornagain as ba
from bornagain import ba_plot as bp, deg, nm, nm2


def get_sample(approx):
    # materials
    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, 3e-5, 2e-8)

    # particle
    ff = ba.Cylinder(5*nm, 1*nm)
    particle = ba.Particle(particle_mat, ff)

    # layers
    toplayer = ba.Layer(vacuum)
    density = 0.008/nm2
    if approx == "Dilute":
        toplayer.deposit2D(ba.Dilute2D(density, particle))
    else:
        toplayer.deposit2D(ba.Dense2D(density, particle))
    substrate = ba.Layer(substrate_mat)

    sample = ba.Sample()
    sample.addLayer(toplayer)
    sample.addLayer(substrate)
    return sample


def simulate(sample, title):
    beam = ba.Beam(1e9, 0.03*nm, 0.2*deg)
    n = 41
    detector = ba.SphericalDetector(n, -1*deg, 1*deg, 1, 0.78*deg, 0.82*deg)
    simulation = ba.ScatteringSimulation(beam, sample, detector)
    field = simulation.simulate()
    field.setTitle(title)
    return field.flat()


if __name__ == '__main__':
    results = [
        simulate(get_sample("Dilute"), "ideal gas"),
        simulate(get_sample("Dense"), "hard disks"),
    ]
    from bornagain import ba_check
    ba_check.persistence_test(results)
