/*******************************************************************************
* Instrument: TAS simple
*  
* %I 
* Written by: Kim Lefmann (lefmann@nbi.ku.dk)
* Date: 22.07.2018
* Origin: UCPH 
* %INSTRUMENT_SITE: Tests_samples
*
* Generic TAS instrument for test of samples with dispersions.
*
* %D
* Generic TAS instrument for test of samples with dispersions. Modeled over the RITA-2 instrument at PSI (one analyzer only). The instrument is able to position in q-space at q=(h 0 0) and fixed Ei and Ef. This can be used to longitudinal constant-E scans or constant-q scans. In addition, transverse constant-E scans can be made by scanning the sample orientation A3.
* 
* %Example: Test_Magnon_bcc_TAS.instr hw=0.5 Detector: e_monitor1_I=499433
* Example: Test_Magnon_bcc_TAS.instr hw=0,1 -N21
* 
* %P 
* hw:        [meV] Neutron energy transfer
* Ef:        [meV] Final neutron energy
* A3auto:      [1] Flag for selecting automatic setting of A3 to have q-vector along [h 0 0] (0=manual; 1=auto)
* A3offset:  [deg] sample rotation around a vertical axis
* aa:         [AA] lattice parameter for cubic system (passed on to the sample component)
* qh:          [1] Length of the q-vector in r.l.u., sets the value of scattering angle A4. If A3auto=1, then A3 is set to q=[qh 0 0]
* highres:     [1] Flag for setting unrealistic high resolution (used for fine testing) (0=standard TAS / RITA2; 1=high resolution TAS)
* sample_J:  [meV] Nearest Neighbor magnetic interaction in sample
* FerroMagnet: [ ] Flag: 1 if ferromagnet, 0 if AFM    
* TT:          [K] temperature 
* Verbose:     [1] Flag for printing of test statements from magnon component (0=silent, 1=print) 
* 
*   
* %L  
* <reference/HTML link>
*  
* %E
*******************************************************************************/
DEFINE INSTRUMENT Test_Magnon_bcc_TAS(hw=0.5,Ef=5,A3offset=0,A3auto=1,aa=4.5,qh=1.1,highres=0,sample_J=0.2,TT=300,FerroMagnet=0,Verbose=0)

DECLARE
%{
    double xx, yy, lf, li, dli, kf, ki, Ei, q, A1, A3, A4, A5, foc_xw, foc_yh, mod_l, eta, sample_r, sample_h;
%}

INITIALIZE
%{ 
    Ei = Ef + hw;
    kf = SE2V*V2K*sqrt(Ef);
    ki = SE2V*V2K*sqrt(Ei);
    lf = 2*PI/kf;
    li = 2*PI/ki; 
    q = qh*2*PI/aa; 
    A1 = RAD2DEG*asin(1.87325/(2*ki)); 
    if (A3auto == 1) 
      A3 = RAD2DEG*acos((ki*ki+q*q-kf*kf)/(2*q*ki))+A3offset;
    else
      A3 = A3offset;
    A4 = RAD2DEG*acos((ki*ki+kf*kf-q*q)/(2*ki*kf));
    A5 = RAD2DEG*asin(1.87325/(2*kf));
    printf("ki %g , kf %g , q %g , A1 %g , A3 %g , A4 %g , A5 %g \n",ki,kf,q,A1,A3,A4,A5);
    
    
    if (highres == 1)
    {    
        yy = 0.001; 
        xx = 0.001;
        dli = 0.0001;
        foc_xw = 0.0001;
        foc_yh = 0.0001;
        mod_l = 26.4;
        eta = 0.1;
        sample_r = 0.001;
        sample_h = 0.002;
    } 
    else 
    {
        yy = 0.16;
        xx = 0.085;
        dli = 0.15;
        foc_xw = 0.03;
        foc_yh = 0.12;
        mod_l = 1.5;
        eta = 37;
        sample_r = 0.005;
        sample_h = 0.02;
     }
%}

TRACE 

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

COMPONENT source_maxwell_3 = Source_Maxwell_3( 
    yheight=yy, 
    xwidth=xx, 
    Lmin=li-dli,
    Lmax=li+dli, 
    dist=mod_l, 
    focus_xw=foc_xw, 
    focus_yh=foc_yh, 
    T1=150.42, 
    T2=38.74,
    T3=14.84, 
    I1=3.67E11, 
    I2=3.64E11, 
    I3=0.95E11)
AT (0, 0, 0) RELATIVE origin

COMPONENT guide = Guide(
    w1=0.03, 
    h1=0.120, 
    w2=0.03, 
    h2=0.120, 
    l=24.5, 
    alpha=4.38)
AT (0, 0, 1.5) RELATIVE source_maxwell_3

COMPONENT monochromator_flat = Monochromator_flat(
    zwidth=0.15, 
    yheight=0.125, 
    mosaich=eta, 
    mosaicv=eta, 
    Q=1.87325)
AT (0, 0, 24.9) RELATIVE guide
ROTATED(0, A1, 0) RELATIVE guide

COMPONENT arm = Arm()
AT (0, 0, 0) RELATIVE monochromator_flat
ROTATED (0, 2*A1, 0) RELATIVE guide


COMPONENT collimator_linear = Collimator_linear(
    xwidth=0.05, 
    yheight=0.10, 
    length=0.20,   
    divergence=40)
AT (0, 0, 0.60) RELATIVE arm

COMPONENT l_monitor = L_monitor(
    filename="lm1",
    nL=200,
    xwidth=2*sample_r+0.01,  
    yheight=sample_h+0.01, 
    Lmin=li-0.2, 
    Lmax=lf+0.2, 
    restore_neutron=1)
AT (0, 0, 1.2) RELATIVE arm

COMPONENT magnon_simple = Magnon_bcc(
    radius=sample_r,
    yheight=sample_h,
    sigma_abs=0, 
    sigma_inc=1, 
    a=aa,  
    J1=sample_J, 
    J2=0,
    D=0.0,
    s=1,
    DW=1,
    verbose=Verbose, 
    T=TT, 
    FM = FerroMagnet,
    target_index=4,
    focus_xw=foc_xw, 
    focus_yh=foc_yh)
AT (0, 0, 1.26) RELATIVE arm
ROTATED (0, A3, 0) RELATIVE arm

COMPONENT arm1 = Arm()
AT (0, 0, 0) RELATIVE magnon_simple
ROTATED (0, -A4, 0) RELATIVE arm

COMPONENT slit1 = Slit(
     yheight = 0.1,
     xwidth = 0.1
)
AT (0, 0, 0.5) RELATIVE arm1

COMPONENT e_monitor = E_monitor(
    filename="energy",
    xwidth=0.1, 
    yheight=0.1,
    nE=300, 
    Emin=0, 
    Emax=30, 
    restore_neutron=1)
AT (0, 0, 1) RELATIVE arm1

COMPONENT monochromator_flat1 = Monochromator_flat(
    zwidth=0.025, 
    yheight=0.150, 
    mosaich=eta, 
    mosaicv=eta, 
    Q=1.87325)
AT (0, 0, 1.26) RELATIVE arm1
ROTATED(0, A5, 0) RELATIVE arm1

COMPONENT arm2 = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0, 2*A5, 0) RELATIVE arm1

COMPONENT e_monitor1 = E_monitor(
    filename="energy1",
    xwidth=0.1, 
    yheight=0.2, 
    Emin=0, 
    Emax=30, 
    nE=300,
    restore_neutron=1)
AT (0, 0, 1) RELATIVE PREVIOUS



FINALLY
%{
%}


END
