/*******************************************************************************
* Instrument: <instrument name>
*
* %I
* Written by: Erik B Knudsen (erkn@fysik.dtu.dk)
* Date: Dec '21
* Origin: DTU Physics
* %INSTRUMENT_SITE: Tests_optics
*
* Unit test instrument for Conics Pairs components
*
* %D
 * Example instrument that shows some ways of using the Conics pair components
*
 * %Example: Test_Conics_pairs OPTIC=1 Detector: psd_i_I=6.91402e-13
 * %Example: Test_Conics_pairs OPTIC=1 Detector: psd_i_reflections_2_I=6.91402e-13
 * %Example: Test_Conics_pairs OPTIC=2 fs=100000 Detector: psd_i_I=2.85546e-21
 * %Example: Test_Conics_pairs OPTIC=2 fs=100000 Detector: psd_i_reflections_2_I=2.85515e-21
 * %Example: Test_Conics_pairs OPTIC=3 Detector: psd_i_I=6.91402e-13
 * %Example: Test_Conics_pairs OPTIC=4 ssize=1e-4 Detector: psd_i_I=8.66267e-09
 * %Example: Test_Conics_pairs quadratic=1 Detector: psd_i_I=5.03026e-13
 * %Example: Test_Conics_pairs quadratic=1 fs=8 fi=6 Detector: psd_i_I=1.26085e-12
 * %Example: Test_Conics_pairs OPTIC=2 quadratic=1 fs=100000 Detector: psd_i_I=2.09716e-21
 * %Example: Test_Conics_pairs OPTIC=3 quadratic=1 Detector: psd_i_I=5.03026e-13
 * %Example: Test_Conics_pairs OPTIC=3 quadratic=1 fs=8 fi=6 Detector: psd_i_I=9.00287e-13
 * %Example: Test_Conics_pairs OPTIC=4 quadratic=1 ssize=1e-4 Detector: psd_i_I=6.56678e-09
*
* %P
* OPTIC: [ ]     Flag to choose between 1: EH pair, 2: PH pair, 3: HE pair, 4: PP pair
* ssize: [m]     Source radius
* fs: [m]        Distance betwee nsource and optic mid plane
* fi: [m]        Distance between optics mid plane and focal point.
* R0: [1]        Mirror substrate reflectivity
* m:  [1]        m-value of supermirrors
* W:  [AA^-1]     Width of supermirror cut-off
* alpha: [AA]    Slope of reflectivity for reflectivity curve approximation
* nshells: [1]   Number of Wolter-optic shells
* rmin: [m] Radius of the innermost shell
* rmax: [m] Radius of the outermost shell
* quadratic: [ ] Use the quadratic shell-radius mode instead of the radii vector
*
* %L
* <reference/HTML link>
*
* %E
*******************************************************************************/
DEFINE INSTRUMENT Test_Conics_pairs(int OPTIC=1, ssize=1e-6, fs=10, fi=10, R0=0.99, m=3, W=0.003, alpha=6.07, int nshells=4, rmin=0.0031416, rmax=0.05236, int quadratic=0)

DECLARE
%{
  double* radii;
%}

USERVARS
%{
  int number_of_reflections;
%}

INITIALIZE
%{
  radii = malloc (nshells * sizeof (double));
  if (!radii) {
    fprintf (stderr, "Error allocating array of raddi in %s. Exit!\n", NAME_INSTRUMENT);
    exit (-1);
  }

  double dr = (rmax - rmin) / (nshells - 1);

  int i;
  for (i = 0; i < nshells; i++) {
    radii[i] = rmax - i * dr;
    printf ("Radius %i: %g\n", i, radii[i]);
  }
%}

TRACE

COMPONENT origin = Progress_bar()
AT (0, 0, 0) RELATIVE ABSOLUTE

// insert components here (e.g. Insert -> Source -> ...)
COMPONENT ss = Source_simple(
    radius=ssize, 
    focus_xw=0.1, 
    focus_yh=0.1, 
    lambda0=5, 
    dlambda=4.9, dist=fs)
AT (0, 0, 0) RELATIVE PREVIOUS
EXTEND
%{
  number_of_reflections = 0;
%}


COMPONENT psd_s = PSD_monitor(xwidth=ssize*2, yheight=ssize*2, filename="psd_s")
AT(0,0,1e-6) RELATIVE PREVIOUS

COMPONENT Lmon_s = L_monitor(xwidth=ssize*2, yheight=ssize*2, filename="L_source",Lmin=0,Lmax=10)
AT(0,0,1e-6) RELATIVE PREVIOUS
  
COMPONENT psd_o0 = PSD_monitor(xwidth=0.12, yheight=0.12, restore_neutron=1,
	filename="psd_o0")
AT(0,0,fs) RELATIVE PREVIOUS

COMPONENT cEH = Conics_EH(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, radii=radii, le=0.25, lh=0.25, disk=1
)
WHEN(OPTIC==1 && !quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cEH_quadratic = Conics_EH(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, rmin=rmin, rmax=rmax, le=0.25, lh=0.25, disk=1
)
WHEN(OPTIC==1 && quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cPH = Conics_PH(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length=fi, radii=radii, lp=0.25, lh=0.25, disk=0
)
WHEN(OPTIC==2 && !quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cPH_quadratic = Conics_PH(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length=fi, rmin=rmin, rmax=rmax, lp=0.25, lh=0.25, disk=0
)
WHEN(OPTIC==2 && quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cHE = Conics_HE(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, radii=radii, le=0.25, lh=0.25, disk=1
)
WHEN(OPTIC==3 && !quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cHE_quadratic = Conics_HE(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, rmin=rmin, rmax=rmax, le=0.25, lh=0.25, disk=1
)
WHEN(OPTIC==3 && quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cPP = Conics_PP(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, radii=radii, lp1=0.25, lp2=0.25, disk=0
)
WHEN(OPTIC==4 && !quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT cPP_quadratic = Conics_PP(R0=R0,alpha=alpha,W=W,m=m,
    nshells=nshells, focal_length_u=fs, focal_length_d=fi, rmin=rmin, rmax=rmax, lp1=0.25, lp2=0.25, disk=0
)
WHEN(OPTIC==4 && quadratic) AT(0,0,fs) RELATIVE ss
EXTEND
%{
  number_of_reflections = SCATTERED - 1;
%}

COMPONENT psd_o1 = COPY(psd_o0)(filename="psd_o1")
AT(0,0,0.3+1e-3) RELATIVE PREVIOUS


 COMPONENT psd_i = COPY(psd_s)(xwidth=100*ssize, yheight=100*ssize,filename="psd_i")
AT(0,0,fi) RELATIVE cEH

COMPONENT psd_i_reflections_0 = COPY(psd_i)(filename="psd_i_reflections_0")
WHEN(number_of_reflections == 0)
AT(0,0,0) RELATIVE psd_i

COMPONENT psd_i_reflections_1 = COPY(psd_i)(filename="psd_i_reflections_1")
WHEN(number_of_reflections == 1)
AT(0,0,0) RELATIVE psd_i

COMPONENT psd_i_reflections_2 = COPY(psd_i)(filename="psd_i_reflections_2")
WHEN(number_of_reflections == 2)
AT(0,0,0) RELATIVE psd_i

COMPONENT psd_i_reflections_3 = COPY(psd_i)(filename="psd_i_reflections_3")
WHEN(number_of_reflections == 3)
AT(0,0,0) RELATIVE psd_i

COMPONENT Lmon_o = L_monitor(xwidth=100*ssize, yheight=100*ssize, filename="L_end",Lmin=0,Lmax=10)
AT(0,0,1e-6) RELATIVE PREVIOUS
  
FINALLY
%{
%}

END
