/*******************************************************************************
* Instrument: Test_Phonon_BvK_PG
*
* %I
* Written by: Kim Lefmann
* Date: 20.07.2025
* Origin: NBI, KU
* %INSTRUMENT_SITE: Generic
*
* Simple thermal triple axis spectrometer for test of Phonon_BvK_PG component
* Modified from older version by Dorothy Wang, Kim Lefmann
*
* %D
* Simple TAS to test the Phonon_BvK_PG component
*
* %Test: h=0   l=2.2 E=4.9   Temp=2   phononmode=2  Detector: e_mon_beforeana_zoom_I=2.28e-10
* %Test: h=0   l=2.2 E=1.795 Temp=2   phononmode=1  Detector: e_monitor_beforeana_I=2.0e-13
* %Test: h=0   l=2.2 E=4.9   Temp=2   phononmode=12 Detector: e_mon_beforeana_zoom_I=2.43e-10
* %Test: h=0   l=2.2 E=4.9   Temp=300 phononmode=2  Detector: e_mon_beforeana_zoom_I=1.301e-9
* %Test: h=0   l=2.8 E=4.9   Temp=2   phononmode=2  Detector: e_mon_beforeana_zoom_I=1.037e-19
* %Test: h=2.4 l=0   E=44.95 Temp=2   phononmode=0  Detector: e_mon_beforeana_zoom_I=9.33e-19
* %Test: h=0.4 l=6   E=44.95 Temp=2   phononmode=0  Detector: e_mon_beforeana_zoom_I=8.81e-11
* %Test: h=0   l=2.1 E=15.8  Temp=2   phononmode=5  Detector: e_mon_beforeana_zoom_I=4.02e-18
* %Test: h=0   l=2.9 E=15.8  Temp=2   phononmode=5  Detector: e_mon_beforeana_zoom_I=1.86e-10
* %Test: h=2   l=0   E=5.78  Temp=2   phononmode=12 Detector: e_mon_beforeana_zoom_I=7.8e-10
*
* %P
* E:       [meV] Energy transfer
* Ef:      [meV] Final energy
* Dlambda: [AA] width of wavelength band
* h:       [rlu] q-value in hexagonal plane
* l:       [rlu] q-value out of hexagonal plane
* dA3:     [deg] A3 offset
* Temp:    [K] Sample temperature
* width:   [m] Beam and sample size scale
* coll:    [arcmin] Collimation before the sample
* phononmode: [1] Phonon mode 0..11, or 12 for random choice
* E_steps_high: [1] Root-search intervals above the elastic line
* E_steps_low:  [1] Root-search intervals below the elastic line
* Verbose: [1] Verbosity level of the sample component
* DISP:    [1] Dispersion output mode of the sample component
*
* %E
*******************************************************************************/
DEFINE INSTRUMENT Test_Phonon_BvK_PG(E=4.5,Ef=25,Dlambda=0.05,h=0,l=3.5,dA3=90, Temp=2, width = 0.001, coll = 20, int phononmode=0,int E_steps_high=100, int E_steps_low=100, int Verbose=0, int DISP=0)

DECLARE
%{
  double Ei;
  double thetaM;
  double twothetaS;
  double thetaA;
  double A3;
  double QM;
  double alpha;
  double lambda_i;
  double SMALL__NUMBER;
%}

INITIALIZE
%{
  // Set monochromator/analyzer Q-value for PG
  QM = 1.8734;
  SMALL__NUMBER = 1e-6;

  double a = 2.461;
  double c = 6.708;
  // PG lattice constants, in AA, same as in Phonon_BvK_PG.comp

  double astar = 4 * PI / (sqrt (3) * a);
  double cstar = 2 * PI / c;
  // PG reciprocal lattice vectors, in 1/AA

  // calculate Ei
  Ei = Ef + E;

  // calculate ki, kf, lambda_i, q
  double ki = V2K * SE2V * sqrt (Ei);
  double kf = V2K * SE2V * sqrt (Ef);
  lambda_i = 2 * PI / ki;
  double q = sqrt (h * h * astar * astar + l * l * cstar * cstar);

  // calculate 2thetaM and 2thetaA
  thetaM = asin (QM / (2 * ki)) * RAD2DEG;
  thetaA = asin (QM / (2 * kf)) * RAD2DEG;

  // calculate scattering angle and sample rotation
  twothetaS = acos ((q * q - ki * ki - kf * kf) / (-2 * ki * kf)) * RAD2DEG;
  alpha = acos ((kf * kf - ki * ki - q * q) / (-2 * ki * q));
  A3 = (asin (l * cstar / (q + SMALL__NUMBER)) - alpha) * RAD2DEG;

  // Printout calculations
  printf ("a = %g c = %g astar = %g cstar = %g \n", a, c, astar, cstar);
  printf ("ki = %g kf = %g q = %g lambda_i = %g\n", ki, kf, q, lambda_i);
  printf ("thetaA = %g thetaM = %g\n", thetaA, thetaM);
  printf ("twothetaS = %g \n", twothetaS);
  printf ("A3 = %g \n", A3);
  printf ("alpha = %g \n", alpha);
%}

TRACE

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

COMPONENT source = Source_Maxwell_3(
    xwidth=width,
    yheight=width,
    Lmin=lambda_i-Dlambda/2, 
    Lmax=lambda_i+Dlambda/2, 
    dist=7.5, 
    focus_yh=width, 
    focus_xw=width, 
    T1=300,     T2=300,     T3=300, 
    I1=1E15,    I2=1E15,    I3=1E15)
AT (0, 0, 0) RELATIVE PREVIOUS

COMPONENT l_monitor_before_mono = L_monitor(
    nL=200, 
    filename="L_before_mono", 
    Lmin=0, 
    Lmax=4,   
    xwidth=0.16, 
    yheight=0.25, 
    restore_neutron=1)
AT (0, 0, 7) RELATIVE PREVIOUS

COMPONENT monochromator_flat = Monochromator_flat(
     mosaicv=30,
     mosaich=30,
     zwidth = width,
     yheight = width,
     Q=QM)
AT (0, 0, 7.5) RELATIVE source
ROTATED (0, thetaM, 0) RELATIVE source

COMPONENT arm1 = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0, thetaM, 0) RELATIVE PREVIOUS

COMPONENT collimator_linear1 = Collimator_linear(
    xwidth=0.1, 
    yheight=0.2, 
    length=0.2, 
    divergence=coll)
AT (0, 0, 0.6) RELATIVE arm1

COMPONENT l_monitor_before_sample_big = L_monitor(
    nL=300, 
    filename="L_before_sample", 
    Lmin=lambda_i - Dlambda, 
    Lmax=lambda_i + Dlambda,
    xwidth = 0.1,
    yheight = 0.1,
    restore_neutron=1)
AT (0, 0, 0.9) RELATIVE arm1

COMPONENT e_monitor_before_sample_big = E_monitor(
    nE=100, 
    filename="E_before_sample", 
    Emin=Ei-3, 
    Emax=Ei+3, 
    xwidth = 0.1,
    yheight = 0.1,
   restore_neutron=1)
AT (0, 0, 0.01) RELATIVE PREVIOUS

COMPONENT PSD_monitor_before_sample = PSD_monitor(
    nx = 200,
    ny = 200,
    xwidth = width/2,
    yheight = width/2,
    filename="PSD_before_sample", 
    restore_neutron=1)
AT (0, 0, 0.998) RELATIVE arm1

COMPONENT arm2 = Arm()
AT (0, 0, 1) RELATIVE arm1
ROTATED (0, -A3+dA3, 0) RELATIVE arm1

SPLIT 10
COMPONENT phonon_bvk_pg = Phonon_BvK_PG(
//    radius=width/2,
    xwidth=width/2, zdepth=width/20,
    yheight=width/2, 
    sigma_abs=0, 
    sigma_inc=0, 
    DW=1, 
    T=Temp, 
    mode_input=phononmode,
    target_index=3,
    focus_xw=width,
    focus_yh=width,
    e_steps_low = E_steps_low,
    e_steps_high = E_steps_high,
    verbose_input=Verbose,
    hh = h, kk = 0, ll = l, dispersion = DISP
)
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0, 0, 0) RELATIVE PREVIOUS  // To use the (0,k,l) scattering plane, rotate (0, 0, -60); for (h,k,0) rotate (90, 0, 0)

COMPONENT arm3 = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0, -twothetaS, 0) RELATIVE arm1

COMPONENT PSD_monitor_after_sample = PSD_monitor(
    nx=200, 
    ny=200, 
    filename="PSD_after_sample", 
    restore_neutron=1)
AT (0, 0, 0.9999) RELATIVE arm3

COMPONENT slit1 = Slit(
    xwidth=width, 
    yheight=width)
AT (0, 0, 1) RELATIVE arm3

COMPONENT e_monitor_beforeana = E_monitor(
    nE=750,
    filename="E_before_ana", 
    xwidth=2*width, 
    yheight=2*width, 
    Emin=0, 
    Emax=75)
AT (0, 0, 0.001) RELATIVE PREVIOUS

COMPONENT e_mon_beforeana_zoom = E_monitor(
    nE=750,
    filename="E_before_ana_zoom", 
    xwidth=2*width, 
    yheight=2*width, 
    Emin=Ef*0.9, 
    Emax=Ef*1.1)
AT (0, 0, 0.001) RELATIVE PREVIOUS
COMPONENT analyzer = Monochromator_flat(
    zwidth=4*width,
    yheight=4*width,
    mosaicv=30,
    mosaich=30,
    Q=QM
    )
AT (0, 0, 0.1) RELATIVE PREVIOUS
ROTATED (0, thetaA, 0) RELATIVE PREVIOUS

COMPONENT arm4 = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0, thetaA, 0) RELATIVE PREVIOUS

COMPONENT slit2 = Slit(
    xwidth=width,
    yheight=width)
AT (0, 0, 1) RELATIVE PREVIOUS

COMPONENT Emon_after_analyzer = E_monitor(
    nE=750,
    filename="E_after_analyzer", 
    xwidth=0.05,
    yheight=0.10, 
    Emin=0, 
    Emax=250)
AT (0, 0, 0.001) RELATIVE PREVIOUS

COMPONENT Emon_1storder = E_monitor(
    nE=50,
    filename="E_1st_order", 
    xwidth=width,
    yheight=width, 
    Emin=Ef*0.9, 
    Emax=Ef*1.1)
AT (0, 0, 0.001) RELATIVE PREVIOUS

END
