#!/usr/bin/env python3
"""
Spheres on two hexagonal close packed layers
"""
import bornagain as ba
from bornagain import ba_plot as bp, deg, nm, R3


def get_sample():
    """
    A sample with spheres on a substrate,
    forming two hexagonal close packed layers.
    """

    # Materials
    particle_color = (0.86, 0.24, 0.18)
    particle_mat = ba.RefractiveMaterial("Particle", particle_color, 6e-7, 2e-08)
    substrate_color = (0.28, 0.57, 0.82)
    substrate_mat = ba.RefractiveMaterial("Substrate", substrate_color, 6e-06, 2e-08)
    vacuum = ba.Vacuum()

    # Particles
    ff = ba.Sphere(10*nm)
    particle_1 = ba.Particle(particle_mat, ff)
    particle_2 = ba.Particle(particle_mat, ff)
    particle_2_position = R3(10*nm, 10*nm, 17.3205080757*nm)
    particle_2.translate(particle_2_position)

    # Lattice basis as compound particle
    basis = ba.Compound()
    basis.addComponent(particle_1)
    basis.addComponent(particle_2)

    # 2D lattice
    lattice = ba.HexagonalLattice2D(20*nm, 0)
    layout = ba.Crystal2D(basis, lattice)
    profile = ba.Profile2DCauchy(100*nm, 100*nm, 0)
    layout.setDecayFunction(profile)

    # Layers
    layer_1 = ba.Layer(vacuum)
    layer_1.deposit2D(layout)
    layer_2 = ba.Layer(substrate_mat)

    # Sample
    sample = ba.Sample()
    sample.addLayer(layer_1)
    sample.addLayer(layer_2)

    return sample


def get_simulation(sample):
    beam = ba.Beam(1e9, 0.1*nm, 0.2*deg)
    n = 11
    detector = ba.SphericalDetector(n, -1*deg, 1*deg, n, 0, 1*deg)
    simulation = ba.ScatteringSimulation(beam, sample, detector)
    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)
