/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2017, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: ISIS_MERLIN
*
* %Identification
* Written by: Rob Bewley
* Date: May 2017.
* Origin: <a href="http://www.isis.stfc.ac.uk">ISIS (UK)</a>
* %INSTRUMENT_SITE: ISIS
*
* MERLIN: High count rate, medium energy resolution, direct geometry chopper spectrometer.
*
* %Description
* Merlin has been in user operation since 2008. It was designed to be a high intensity, medium energy 
* resolution spectrometer that would complement the high-resolution MAPS spectrometer, and replace 
* the HET spectrometer. Merlin's design exploited recent advances in technology with a supermirror 
* guide to enhance flux, as well as 3 m long position-sensitive detectors in a vacuum, making it 
* ideal for the study of single crystals. The detector bank covers a massive \pi steradians of solid 
* angle with an angular range from -45 to 135 degrees in the horizontal plane, and \pm 30 degrees in 
* the vertical plane. This allows large swathes of Q,\omega space to be accessed in a single run. 
*
* Since 2014, Merlin has been running in event mode, and by using the new Gd chopper combined with a 
* disk chopper, it is able to run in repetition-rate multiplication (RRM) mode, allowing users to 
* simultaneously measure with several incident energies.
*
* %Parameters
* INPUT PARAMETERS:
* E_min: [meV]   lowest energy sampled      [meV]
* E_max: [meV]   highest energy sampled     [meV]
* E_foc: [meV]   energy selected by chopper [meV]
* nu_hz: [Hz]    frequency of chopper rotation [Hz]
* type:  [int]   chopper package selected sloppy chopper = 1, B chopper = 2, Gd chopper = 3
* m:     [float] m value of guides
* 
* %L 
* <a href="http://www.isis.stfc.ac.uk/instruments/merlin">http://www.isis.stfc.ac.uk/instruments/merlin</a>
* %E
*******************************************************************************/
DEFINE INSTRUMENT ISIS_MERLIN(E_min=1.0, E_max=2000.0, m=3.0,
E_foc=80.0,nu_hz=200.0,int type=3)

DECLARE
%{
double slit_curv,num_slits,width,len;
double lam,phase_time,t_det,t_fer;
%}

// #include <math.h>

INITIALIZE 
%{ 

if (type==1)
  {slit_curv=1/0.82;
   num_slits=55;
   width=0.00114;
   len=0.099;
   fprintf(stderr,"MERLIN b chopper selected");
  }
if (type==2)
  {slit_curv=1/1.3;
   num_slits=28;
   width=0.00228;
   len=0.099;
   fprintf(stderr,"MERLIN Sloppy selected selected");
  }
if (type==3)
  {slit_curv=0;
   num_slits=350;
   width=0.0002;
   len=0.01;
   fprintf(stderr,"Merlin Gd selected selected");
  }

/* calculate opening time of the fermi chopper at 10m from moderator*/
phase_time = (2.28e-3*10.0/sqrt(E_foc));
printf("\nphase_time: %f\n\n",phase_time);
t_det=1.43*phase_time;
t_fer=1.02*phase_time;
lam = sqrt(81.81/E_foc);
%}

TRACE

/* define origin */
COMPONENT Origin = Arm()
  AT (0,0,0) ABSOLUTE

/* The moderator */
COMPONENT isis_mod =
ViewModISIS(Face="TS1_S04_Merlin.mcstas", E0 = E_min, E1 = E_max,
dist = 1.7, focus_xw = 0.094, focus_yh = 0.094, modPosition=0,xw=0.12,yh=0.12)
 AT (0,0,0) RELATIVE Origin

/* TOF monitor for getting moderator time structure */
COMPONENT TOF_mod= TOF_monitor(
    nt = 150, filename = "mod.m",xmin = -0.025,
    xmax = 0.025, ymin = -0.025, ymax = 0.025, tmin=0, tmax =150 )
  AT (0, 0, 0.001) RELATIVE Origin

/* 1st piece of guide in shutter */
COMPONENT shutter_guide = Guide_channeled(
    w1 = 0.094, h1 = 0.094, w2 = 0.094, h2 = 0.094, l = 2.0, alphax = 4.38, alphay = 4.38 ,
 W=3e-3, mx = m, my = m)
  AT (0, 0, 1.7) RELATIVE Origin

/* 2nd piece of guide converging to chopper pit */
COMPONENT conv_guide = Guide_channeled(
    w1 = 0.094, h1 = 0.094, w2 = 0.067, h2 = 0.067, l = 4.760, alphax = 4.38, alphay = 4.38 ,
 W=3e-3, mx = m, my = m)
  AT (0, 0, 3.704) RELATIVE Origin

/* 3rd piece of guide leading to fermi chopper*/
COMPONENT between_guide = Guide_channeled(
    w1 = 0.0629, h1 = 0.0629, w2 = 0.0594, h2 = 0.0594, l = 0.640, alphax = 4.38, alphay = 4.38 ,
 W=3e-3, mx = m, my = m)
  AT (0, 0, 9.182) RELATIVE Origin

/* the fermi chopper */
COMPONENT fermi = FermiChopper(delay=phase_time, radius=0.055, nu=-nu_hz,
  yheight=0.07, w=width ,nslit=num_slits, R0=0.0,
  Qc=0.02176, alpha=2.33, m=0, length=len, eff=0.95, curvature=-slit_curv, zero_time=0)
  AT (0,0,10) RELATIVE Origin

/* TOF monitor after fermi chopper */
COMPONENT TOF_fermi= TOF_monitor(
    nt = 50, filename = "fermi.m",xmin = -0.025,
    xmax = 0.025, ymin = -0.025, ymax = 0.025, tmin=(1e6*t_fer)-12, tmax =(1e6*t_fer)+22 )
  AT (0, 0, 10.2) RELATIVE Origin

/* final guide section just bebore sample */
COMPONENT final_Guide = Guide_channeled(
    w1 = 0.0568, h1 = 0.0568, w2 = 0.0506, h2 = 0.0506, l = 1.10, alphax = 4.38, alphay = 4.38 ,
 W=3e-3, mx = m, my = m)
  AT (0, 0, 10.277) RELATIVE Origin

/* horizontal beam divergence monitor at sample */
COMPONENT horz_div = Div1D_monitor(ndiv=30, filename="horz.div.m",
          xmin=-0.025, xmax=0.025, ymin=-0.025, ymax=0.025,
          maxdiv=0.3*lam+.4)
 AT (0, 0, 11.79) RELATIVE Origin

/* Position sentive detector*/
COMPONENT PSD = PSD_monitor(xmin=-0.04, xmax=0.04, ymin=-0.04, ymax=0.04,
            nx=20, ny=20, filename="Output.psd")
 AT (0, 0, 11.795) RELATIVE Origin

COMPONENT det_lmon = L_monitor(xmin=-0.005, xmax=0.005,
 ymin=-0.005, ymax=0.005, nL=100, filename="det_Lmon.m", Lmin=0.0, Lmax=10)
AT (0,0,11.796) RELATIVE Origin

/* incoherent scattering sample */
COMPONENT sample = Incoherent(radius=0.02, thickness=0.001,yheight=0.04,
      focus_xw=0.025, focus_yh=0.1,sigma_abs=0.0,sigma_inc=100, target_index=2)
 AT (0, 0, 11.8) RELATIVE Origin

/* define a new origin at sample */
COMPONENT detectorarm = Arm()
  AT (0, 0, 0) RELATIVE sample
  ROTATED (0,20.0,0) RELATIVE sample

/* a detector */
COMPONENT detector= TOF_monitor(
    nt = 25, filename = "detector.m",xmin = -0.0125,
    xmax = 0.0125, ymin = -0.05, ymax = 0.05, tmin=(1e6*t_det)-20, tmax =(1e6*t_det)+30)
  AT (0, 0, 2.5) RELATIVE detectorarm

END

/* horizontal beam divergence monitor at sample */
/*COMPONENT div = Divergence_monitor(nh=20, nv=20, filename="div.m",
          xmin=-0.025, xmax=0.025, ymin=-0.025, ymax=0.025,
         maxdiv_h=0.3*lam+.4,  maxdiv_v=0.3*lam+.4)
 AT (0, 0, 11.795) RELATIVE Origin*/
