#!/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 = 555
    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__':
    ba.showSample3D(get_sample("Dilute"), sample_size=80*nm, seed=0)
    results = [
        simulate(get_sample("Dilute"), "ideal gas"),
        simulate(get_sample("Dense"), "hard disks"),
    ]
    bp.plot_multicurve(results)
    bp.plt.show()
