/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2015, All rights reserved
*         DTU Physics, Kongens Lyngby, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: SNS_ARCS
*
* %Identification
* Written by: G. Granroth
* Date: Nov 2014
* Origin: SNS
* %INSTRUMENT_SITE:SNS
*
* Model of the ARCS spectrometer from SNS.
*
* %Description
* When using this ARCS instrument model, please reference
* <ul>
* <li>Granroth, G. E. and Abernathy, D. L., "Performance Comparisons of Four Direct Geometry
* Spectrometers Planned for Spallation Neutron Source",  Proceedings of ICANS-XVI, 289 (2003).
* <li>D. L. Abernathy,  M. B. Stone,  M. J. Loguillo, M. S. Lucas, O. Delaire, X.Tang, J. Y. Y. Lin, and
* B. Fultz,"Design and operation of the wide angular-range chopper spectrometer ARCS at the
* Spallation Neutron Source", Review of Scientific Instruments, 83 , 015114 (2012)</ul>
*
* %Parameters
* Fermi_nu: [Hz]  Frequency of the Fermi chopper
* T0_nu: [Hz]     Frequency of the T0 chopper
* nrad: [m]       Radius of the Fermi chopper blades
* nchans: [1]     Number of channels in the Fermi chopper
* Edes: [meV]     Desired/target energy
* Et: [meV]       Energy transfer of the Spot_sample
* ttheta: [deg]   Scattering angle of the Spot_sample
* sxmin: [m]      Sample slit horz min value
* sxmax: [m]      Sample slit horz max value
* symin: [m]      Sample slit vert min value
* symax: [m]      Sample slit vert max value
* run_num: [1]    Virtual source run number (unused at present)
* T_off: []       
*
* %Link
* <br>Granroth, G. E. and Abernathy, D. L., "Performance Comparisons of Four Direct Geometry Spectrometers Planned for Spallation Neutron Source",  Proceedings of ICANS-XVI, 289 (2003).
* <br>D. L. Abernathy,  M. B. Stone,  M. J. Loguillo, M. S. Lucas, O. Delaire, X.Tang, J. Y. Y. Lin, and  B. Fultz,"Design and operation of the wide angular-range chopper spectrometer ARCS at the Spallation Neutron Source", Review of Scientific Instruments, 83 , 015114 (2012)
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT SNS_ARCS(string filename="source_sct521_bu_17_1.dat",Fermi_nu=420,T0_nu=90,nrad=0.58,nchans=40,Edes=50,Et=25,ttheta=25,T0_off=0,sxmin=-0.04,sxmax=0.04,symin=-0.04,symax=0.04,run_num=1)

DECLARE %{
   
   double Emin,Emax;
   /* guide reflectivity profile parameters */
   double Gu_m, Gu_R, Gu_alpha, Gu_Qc, Gu_W;
   double phaseoff,phasefc1,phase_T0,phase_sam, phase_det,toffset,phase_m1,phase_m2;
   double tplotmin,tplotmax;
      /* critical distances
  LF moderator to Fermi chopper
  LT0 moderator to T0 chopper
  LM1 moderator to monitor 1
  LM2 moderator to monitor 2
  LS  moderator to sample
  L3  approximate distance to in plane detectors */
   double LF,LT0,LM1,LM2,LS,L3;
   double ch_x,ch_y;
   char *v_fname;
   char *detoptstr;
   char *tfilename;
   char *detsampoptstr;
   char *detdetoptstr;
   char *mon1optstr;
   char *mon2optstr;
%}
INITIALIZE
%{
  
   detoptstr=malloc(150*sizeof(char));
   tfilename=malloc(50*sizeof(char));
   detsampoptstr=malloc(200*sizeof(char));
   detdetoptstr=malloc(200*sizeof(char));
   mon1optstr=malloc(200*sizeof(char));
   mon2optstr=malloc(200*sizeof(char));
   v_fname=malloc(200*sizeof(char));
   /*set lengths defined above */
   LT0=8.77;LF=11.61;LM1=11.82;LM2=18.50;LS=13.61;L3=3.5;
   /*determine emission time of certain energy neutrons after the prompt pulse */
   ch_x=log10(Edes*1e-3);  ch_y=-0.4420*ch_x*(1+tanh((ch_x+1.1197)/0.4042))/2-0.1235*ch_x*(1-tanh((ch_x+1.1197)/0.4042))/2-0.4189*tanh((ch_x+1.1197)/0.4042)+0.5612;
    // toffset=0.0;
   toffset=pow(10,ch_y)/1.0e6;
   /*set phases for the critical lengths */
   phasefc1=(LF)/(sqrt(Edes)*SE2V)+toffset;
   phase_T0=(LT0)/(sqrt(Edes)*SE2V)+toffset;
   phase_sam=(LS)/(sqrt(Edes)*SE2V)+toffset;
   phase_det=(LS+L3)/(sqrt(Edes)*SE2V)+toffset;
   phase_m1=(LM1)/(sqrt(Edes)*SE2V)+toffset;
   phase_m2=(LM2)/(sqrt(Edes)*SE2V)+toffset;
   /* set parameters for guide reflectivity profile */
   Gu_R=0.98;Gu_alpha=5.5;Gu_m=3.6;Gu_Qc=0.02;Gu_W=2e-3;
  /* set energy range to examine  set to +/- 20% of Edes value*/
   Emin=Edes*0.8;Emax=Edes*1.2;
   //tplotw=((0.06-0.0005)/nchans-0.0005)/(4.0*PI*Fermi_nu*0.05)*4.0;
   tplotmin=LM1/(sqrt(Emax)*SE2V)+toffset;
   tplotmax=LM1/(sqrt(Emin)*SE2V)+toffset;
   sprintf(detoptstr,"ARCS_Sam_mon_Edes_%1.3e_Ferminu_%1.2e_nrad_%1.5g_nchans_%g_T0_off_%1.3e_T0_nu_%1.1e%s",Edes,Fermi_nu,nrad,nchans,T0_off,T0_nu,".Edat");
   sprintf(detsampoptstr,"ARCS_Sam_mon_Edes_%1.3e_Ferminu_%1.2e_nrad_%1.5g_nchans_%g_T0_off_%1.3e_T0_nu_%1.1e%s",Edes,Fermi_nu,nrad,nchans,T0_off,T0_nu,".tdat");
   sprintf(detdetoptstr,"ARCS_det_mon_Edes_%1.3e_Ferminu_%1.2e_nrad_%1.5g_nchans_%g_T0_off_%1.3e_T0_nu_%1.1e%s",Edes,Fermi_nu,nrad,nchans,T0_off,T0_nu,".tdat");
   sprintf(mon1optstr,"ARCS_mon1_Edes_%1.3e_Ferminu_%1.2e_nrad_%1.5g_nchans_%g_T0_off_%1.3e_T0_nu_%1.1e%s",Edes,Fermi_nu,nrad,nchans,T0_off,T0_nu,".tdat");
   sprintf(mon2optstr,"ARCS_mon2_Edes_%1.3e_Ferminu_%1.2e_nrad_%1.5g_nchans_%g_T0_off_%1.3e_T0_nu_%1.1e%s",Edes,Fermi_nu,nrad,nchans,T0_off,T0_nu,".tdat");
   sprintf(tfilename,"%s%1.0f%s","tdet_E_",Edes,".dat");
   sprintf(v_fname,"virt_src_ARCS_E%1.2f_F_nu_%g_T0_nu_%g_%g.dat",Edes,Fermi_nu,T0_nu,run_num);
  %}
TRACE
COMPONENT mod=SNS_source(filename=filename,
                         xwidth=0.1,
                         yheight=0.12,
                         dist=2.5,
                         focus_xw=0.1,
                         focus_yh=0.12,
                         Emin=Emin,
                         Emax=Emax)
AT(0,0,-13.61) ABSOLUTE

COMPONENT sourceMantid=Arm()
AT(0,0,0) RELATIVE mod
COMPONENT core_ves=Guide_channeled(w1=0.094285,h1=0.11323,w2=0.084684,h2=0.102362,l=1.2444,
            R0=0.0,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
                          W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,1.0106) RELATIVE mod

COMPONENT shutter_guide=Guide_channeled(w1=0.074930,h1=.094040,w2=0.070880,h2=0.086880,
                          l=1.853,
                          R0=Gu_R,mx=2.5,my=2.5,Qcx=Gu_Qc,Qcy=Gu_Qc,
                          W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,2.26790) RELATIVE mod
COMPONENT Guide_1_1_1=Guide_channeled(w1=0.07088,h1=0.08688,w2=0.07019,h2=0.08573,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,4.17230) RELATIVE mod
COMPONENT Guide_1_1_2=Guide_channeled(w1=0.07019,h1=0.08573,w2=0.06947,h2=0.08454,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,4.65589) RELATIVE mod
COMPONENT Guide_1_1_3=Guide_channeled(w1=0.06947,h1=0.08454,w2=0.06871,h2=0.08329,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,5.13948) RELATIVE mod
COMPONENT Guide_1_2_1=Guide_channeled(w1=0.06871,h1=0.08329,w2=0.06792,h2=0.08197,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,5.62331) RELATIVE mod
COMPONENT Guide_1_2_2=Guide_channeled(w1=0.06792,h1=0.08197,w2=0.06710,h2=0.08060,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,6.10690) RELATIVE mod
COMPONENT Guide_1_2_3=Guide_channeled(w1=0.06710,h1=0.08060,w2=0.06624,h2=0.07917,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,6.59049) RELATIVE mod
COMPONENT Guide_1_3_1=Guide_channeled(w1=0.06624,h1=0.07917,w2=0.06534,h2=0.07766,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,7.07433) RELATIVE mod
COMPONENT Guide_1_3_2=Guide_channeled(w1=0.06534,h1=0.07766,w2=0.06440,h2=0.07609,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,7.55792) RELATIVE mod
COMPONENT Guide_1_3_3=Guide_channeled(w1=0.06440,h1=0.07609,w2=0.06342,h2=0.07443,
            l=0.48354,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,8.04145) RELATIVE mod

COMPONENT t0_chopp=Vertical_T0a(len=0.474,w1=0.08,w2=0.101,nu=T0_nu,delta=0.0,tc=phase_T0,
                 ymin=-0.045,ymax=0.045)
AT(0,0,LT0)RELATIVE mod

COMPONENT Guide_2_1_1=Guide_channeled(w1=0.06136,h1=0.07094,w2=0.06044,h2=0.06936,
            l=0.40204,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,9.47504) RELATIVE mod

COMPONENT Guide_2_1_2=Guide_channeled(w1=0.06044,h1=0.06936,w2=0.05948,h2=0.06771,
            l=0.40204,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,9.87713) RELATIVE mod
COMPONENT Guide_2_3_3=Guide_channeled(w1=0.05948,h1=0.06771,w2=0.05848,h2=0.06598,
            l=0.40204,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,10.27922) RELATIVE mod
COMPONENT Guide_1_3_4=Guide_channeled(w1=0.05848,h1=0.06598,w2=0.05745,h2=0.06417,
            l=0.40204,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,10.68131) RELATIVE mod
COMPONENT Guide_2_1_5=Guide_channeled(w1=0.05745,h1=0.06417,w2=0.05637,h2=0.06227,
            l=0.40204,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,11.08340) RELATIVE mod

  COMPONENT fermi_chopp=Fermi_chop2a(len=0.10,w=0.06,ymin=-.0325,ymax=.0325,
                                        nu=Fermi_nu,delta=0.0,tc=phasefc1
                                       ,nchan=nchans,bw=0.0005,blader=nrad)
AT (0,0,LF) RELATIVE mod
/* COMPONENT Monitor1=TOF_monitor(xmin=-0.035,xmax=0.035,ymin=-0.035,ymax=0.035, */
/*                 tmin=tplotmin, */
/*                 tmax=tplotmax, */
/*                 nt=100, */
/*                 filename=mon1optstr) */
/* AT (0,0,LM1) RELATIVE mod */
/* reomvable guide section that can be replaced with a collimator immediately after the the Fermichopper */
COMPONENT Guide_3_1_1=Guide_channeled(w1=0.05536,h1=0.06046,w2=0.05473,h2=0.05931,
            l=0.225,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,11.84975) RELATIVE mod
/* Section 4: fixed guide section between Fermi choppers and sample vessel */
COMPONENT Guide_4_1_1=Guide_channeled(w1=0.05468,h1=0.05924,w2=0.05331,h2=0.05674,
            l=0.46275,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,12.08825) RELATIVE mod

COMPONENT Guide_4_1_2=Guide_channeled(w1=0.05331,h1=0.05674,w2=0.05187,h2=0.05408,
            l=0.46275,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,12.55105) RELATIVE mod
/* Section 5 removable guide */
// This section is usually removed on ARCS
/*COMPONENT Guide_5_1_1=Guide_channeled(w1=0.05186,h1=0.05405,w2=0.05062,h2=0.05172,
            l=0.37920,
            R0=Gu_R,mx=Gu_m,my=Gu_m,Qcx=Gu_Qc,Qcy=Gu_Qc,
            W=Gu_W,nslit=1,d=0.0,alphax=Gu_alpha,alphay=Gu_alpha)
AT(0,0,13.01830) RELATIVE mod*/

/* In Vacuum Slits */
COMPONENT Slits2=Slit(xmin=sxmin,xmax=sxmax,ymin=symin,ymax=symax)
AT(0,0,13.328) RELATIVE mod



/* If you split into a front end and a back end,  split it here
COMPONENT vout = Virtual_output(file = v_fname,bufsize=0,type="binary")
  AT (0, 0, LS) RELATIVE mod */

COMPONENT Sample_Arm=Arm()
AT(0,0,LS)  RELATIVE mod
COMPONENT sampleMantid = Spot_sample(radius_o=0.01, h=0.05, pack = 1,xwidth=0, yheight=0, zthick=0, Eideal=Edes,w=Et,two_theta=ttheta,n_spots=4)
AT (0.0,0.0,0.0) RELATIVE Sample_Arm

  COMPONENT  t_mon_detB= Monitor_nD(xwidth=5.9817,yheight=3.2,restore_neutron=1, filename="this",
                          options="mantid banana, theta limits=[-28.0 135.38] bins=389 ,y limits=[-0.550 0.450] bins=128, neutron pixel min=0 t, list all neutrons", restore_neutron=1)
AT (0,0,0.00005) RELATIVE Sample_Arm

COMPONENT  t_mon_detA= Monitor_nD(xwidth=6.26,yheight=3.2,restore_neutron=1,filename="this",
                          options="mantid banana, theta limits=[-28.0 135.38] bins=389 ,y limits=[-1.600 -0.600] bins=128, neutron pixel min=50000 t, list all neutrons", restore_neutron=1)
AT (0,0,0.00005) RELATIVE Sample_Arm

COMPONENT  t_mon_detC= Monitor_nD(xwidth=6.626,yheight=3.2,restore_neutron=1,filename="this",
                          options="mantid banana, theta limits=[-28.0 135.38] bins=389, y limits=[0.495 1.495] bins=128, neutron pixel min=100000 t, list all neutrons",  restore_neutron=1)
AT (0,0,0.00005) RELATIVE Sample_Arm




FINALLY
%{
   free(detoptstr);free(tfilename);free(detsampoptstr);free(detdetoptstr);free(mon1optstr);free(mon2optstr);
%}
END
