/*****************************************************************************
*         McXtrace instrument definition URL=http://www.mcxtrace.org
*
* Instrument: Test_Monochromators
*
* %Identification
* Written by: A. Vickery, A. Prodi and E. Knudsen
* Date: July 2011.
* Origin: NBI,Ris&oslash; DTU Physics
* %INSTRUMENT_SITE: Tests_optics
*
* Compares intensities of Monochromator components.
*
* %Description
*
* Very simple setup to compare intensities diffracted by Monochromators. 
* It shows that implementations are equivalent.
*
* The input parameter 'Mono' chooses which model to use:
* 0: Obsolete Perfect_crystal
* 1: Bragg_crystal - validated component
* 2: Bragg_crystal_bent - bent version of the above
* 3: Bragg_crystal_BC - variant of the above based on Mendenhall et. al., J. Appl. Cryst 2019
* 4: Bragg_crystal_bent_BC - variant of the above based on Mendenhall et. al., J. Appl. Cryst 2019
* 5: Bragg_crystal_simple - a simple mono with a static (flat) Darwin width.
*
* %Example: Test_Mono.instr Mono=1 Detector: emon_I=5.39036e-17
* %Example: Test_Mono.instr Mono=2 Detector: emon_I=5.41029e-17
* %Example: Test_Mono.instr Mono=3 Detector: emon_I=5.33687e-17
* %Example: Test_Mono.instr Mono=4 Detector: emon_I=5.33687e-17
* %Example: Test_Mono.instr Mono=5 Detector: emon_I=5.33687e-17
*
* %Parameters
* INPUT PARAMETERS:
* Mono:   [ ]   Which monochromator model to use.
* lambda: [AA]  Source wavelength.
* L1:     [m]   Distance from source to monochromator.
* OMM:    [deg] Rotation angle of monochromator, i.e. angle of incidence.
* TTM:    [deg] Scattering angle at which to put the detector.
* BEND:   [m]   Curvature radius of the monochromator crystal lattice. Only relevant for Mono==2.
* monsiz: [m]   Size (h and w) of PSD measuring diffracted beam.
*
* %End
****************************************************************************/

DEFINE INSTRUMENT Test_Mono(int Mono=1, lambda=1.0, L1=10.0,OMM=14.2208,TTM=28.4416, BEND=1, monsiz=5e-5)

DECLARE
%{
  double DM  = 3.1356 ; /* Monochromator d-spacing in Angs */
			/* Si(111):           3.1356 */
      /* PG002 Orders : 1st 3.355 2e 1.6775, 3e 1.1183 */
  
  /* This variable helps us switch on and off the different setups*/
  double filterProb;
%}
/* end of DECLARE */

INITIALIZE
%{
  int    ORDER = 1;
  double Ki, mono_q, SM;
  
  /* SM : scattering at mono to the right (-1)/left(+1) */
  SM = -1;
  
  mono_q = 2*PI*ORDER/DM;  /* Q mono in Angs-1 */

  Ki = 2*PI/lambda; 
  
  if (!TTM)
    TTM = asin(mono_q/2/Ki)*RAD2DEG*2*SM;
    
  if (!OMM)
    OMM = TTM/2;

%}
/* end of INITIALIZE */

TRACE
/* Source description */

COMPONENT Origin = Progress_bar()
AT (0,0,0) ABSOLUTE

/* a flat constant source */

COMPONENT generic_source=Source_flat(
    E0=8.05, dE=0.9,
    xwidth=1.0e-5, yheight=1.0e-5,
    focus_xw=1.0e-5,focus_yh=1.0e-5, dist=L1
)
AT (0,0,0) RELATIVE PREVIOUS

COMPONENT psd_pre = PSD_monitor(xwidth = 0.10, yheight = 0.10,
        nx=20, ny=20, filename="psd2.dat")
AT (0, 0, 0.5) RELATIVE generic_source

COMPONENT emon_pre = E_monitor(nE=200, xwidth=0.1, yheight=0.1,
        xwidth=0, yheight=0, nE=200, Emin=8,Emax=8.1, restore_xray=1,filename="emon_pre.dat")
AT (0, 0, 0.6) RELATIVE generic_source

COMPONENT Mono_Arm = Arm()
AT (0, 0, L1) RELATIVE generic_source
ROTATED (-OMM, 0, 0) RELATIVE generic_source

% ------------------------------------------------------------------------------
% rotation along X, monochromator surface is in the XZ plane

COMPONENT Mono_obso = Perfect_crystal(
        length=0.05, width=0.02, V=160.1826, h=1, k=1, l=1, alpha=0)
WHEN Mono==0 AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT Mono1 = Bragg_crystal(
        length=0.05, width=0.02, V=160.1826, h=1, k=1, l=1, alpha=0)
WHEN Mono==1 AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT Mono1b = Bragg_crystal_bent(
        length=0.05, width=0.02, V=160.1826, h=1, k=1, l=1, alpha=0,y_b=BEND, lattice_y_b=BEND, z_c=BEND, lattice_z_c=BEND)
WHEN Mono==2 AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT MonoBC = Bragg_crystal_BC(
        length=0.05, width=0.02, V=160.1826, h=1, k=1, l=1, alphay=1)
WHEN Mono==3 AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT MonoBCb = Bragg_crystal_bent_BC(
        length=0.05, width=0.02, V=160.1826, h=1, k=1, l=1, alpha=0)
WHEN Mono==4 AT (0, 0, 0) RELATIVE Mono_Arm

COMPONENT MonoBCs = Bragg_crystal_simple(
    yheight=0.05, xwidth=0.02, DM=DM, err_Q= 0.000075, r0=1.0)
WHEN Mono==5 AT (0,0,0) RELATIVE Mono_Arm
ROTATED(90,0,0) RELATIVE Mono_Arm

COMPONENT MonoBCsx = Single_crystal(xwidth=0.02, yheight=0.0001, zdepth=0.05,
      delta_d_d=1e-4, mosaic = 5,
      reflections="Si.lau")
WHEN Mono==6 AT (0,0,0) RELATIVE Mono_Arm
ROTATED(0,0,0) RELATIVE Mono_Arm

% ------------------------------------------------------------------------------

COMPONENT Mono_Out = Arm()
AT (0, 0, 0) RELATIVE Mono_Arm
ROTATED (-TTM, 0, 0) RELATIVE generic_source

COMPONENT fpi = PSD_monitor_4PI(radius=0.1, filename="fpi",nx=51,ny=51)
AT(0,0,0) RELATIVE Mono_Out

COMPONENT psd1 = PSD_monitor(xwidth = monsiz, yheight = monsiz,
        nx=50, ny=50, filename="psd1.dat")
AT (0, 0, 1) RELATIVE Mono_Out

COMPONENT emon = E_monitor(nE=200, xwidth=0.1, yheight=0.1,
        xwidth=0, yheight=0, nE=200, Emin=8,Emax=8.1, restore_xray=1,filename="emon.dat")
AT (0, 0, 1.1) RELATIVE Mono_Out

COMPONENT emon_zm = E_monitor(nE=50, xwidth=0.1, yheight=0.1,
        xwidth=0, yheight=0, nE=200, Emin=8.0475,Emax=8.05, restore_xray=1,filename="emonzm.dat")
AT (0, 0, 1.1) RELATIVE Mono_Out

END

