"""
Parametric model of a GISAS simulation.
The idealized sample model consists of dilute cylinders on a substrate.
"""

import bornagain as ba
from bornagain import deg, nm, nm2
import lmfit

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)


def get_sample(P):
    cylinder_height = P["cylinder_height"]
    cylinder_radius = P["cylinder_radius"]

    ff = ba.Cylinder(cylinder_radius, cylinder_height)
    particle = ba.Particle(particle_mat, ff)

    layer_1 = ba.Layer(vacuum)
    layer_1.deposit2D(ba.Dilute2D(0.01/nm2, particle))
    layer_2 = ba.Layer(substrate_mat)

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


def get_simulation(P):
    beam = ba.Beam(10**P['lg_intensity'], 0.1*nm, 0.2*deg)
    n = 100
    det = ba.SphericalDetector(n, -1.5*deg, 1.5*deg, n, 0, 3*deg)
    sample = get_sample(P)

    simulation = ba.ScatteringSimulation(beam, sample, det)
    simulation.setBackground(ba.ConstantBackground(
        10**P['lg_background']))
    simulation.options().setUseAvgMaterials(False)

    return simulation


def start_parameters_1():
    P = lmfit.Parameters()
    P.add("lg_intensity", 5, min=0, max=15)
    P.add("lg_background", 1, min=-2, max=10)
    P.add("cylinder_height", 6.*nm, min=0.01)
    P.add("cylinder_radius", 6.*nm, min=0.01)
    return P


if __name__ == '__main__':
    raise Exception(
        "This source file is a module for use from fitting examples."
        " It is not meant to be called as a main program.")
