/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: HZB_NEAT (1995)
*
* %Identification
* Written by: <a href="mailto:farhi@ill.fr">Emmanuel Farhi</a> and R. Lechner
* Date: 2011
* Origin: <a href="http://www.ill.fr">ILL (France)</a>/<a href="http://www.helmholtz-berlin.de/user/neutrons/index_en.html">BENSC (Germany)</a>
* %INSTRUMENT_SITE: HZB
*
* V3 Time-of-Flight-Spectrometer (NEAT) at BENSC, 1995 version.
*
* %Description
*
* In time-of-flight spectrometers neutron pulses are created by us-
* ing mechanical chopper devices, realised on NEAT by fast rotating
* discs with speeds up to 20000 RPM. The discs are coated with neu-
* tron absorbing materials except for the narrow windows. Phased to
* each other according to the flight time of neutrons between them,
* the choppers 'cut out' pulses of neutrons with a desired wave-
* length from the white beam. An interaction with moving atoms in
* the sample changes the velocities of the scattered neutrons and
* this is detected by the secondary part of the spectrometer on the
* basis of the time of flight between the sample and the neutron de-
* tectors at 2.5 m distance. The secondary spectrometer of NEAT
* contains an array of 388 3He 60 cm2 area single counter detectors
* (SD) for the large-angle scattering.
*
* The NL 2 (upper part) guide is modelled.
* This model only contains the first and last choppers, and has only a single
* pulse (no frame overlap). The sample is a 2mm thick plate rotated by 45 degrees,
* which material can be any powder/liquid/amorphous sample.
*
* %Example: lambda=6                       Detector: Detector_I=2900
* %Example: lambda=6 coh=Y2O3.laz inc=NULL Detector: Detector_I=1080
*
* %Parameters
* lambda: [Angs]   incident wavelength on sample
* dlambda: [Angs]  wavelength spread shot from the source
* rpm: [rpm]       disk chopper rotation speed, setting the resolution, Hz=rpm/60.
* coh: [str]       sample coherent S(q,w) file name. Use LAZ/LAU or SQW file
* inc: [str]       sample incoherent S(q,w) file name. Use NULL to scatter incoherently
* tmin: [s]        Minimum ToF measured
* tmax: [s]        Maximum ToF measured
*
* %Link
* <a href="http://www.helmholtz-berlin.de/user/neutrons/instrumentation/neutron-instruments/v3/index_en.html">NEAT at HZB/BENSC</a>
* %End
*******************************************************************************/
DEFINE INSTRUMENT HZB_NEAT(lambda=6, dlambda=0.05, rpm=10000, string coh="Rb_liq_coh.sqw", string inc="Rb_liq_inc.sqw", tmin=0.01806, tmax=0.01826)

DECLARE %{
  double time_elastic;
  char   detector_options[128];
  char   lmon_options[128];
  char   tmon_options[128];
%}

INITIALIZE %{
  double Chopper2Detector = 11.97+2.143+0.165;
  double KI, Vi, EI;
  double Emin, Emax, Vmin, Vmax, Tmin, Tmax;

  KI= 2*PI/lambda;
  Vi = K2V*fabs(KI);
  EI = VS2E*Vi*Vi;

  printf("%s: Detailed NEAT/TOF configuration\n", NAME_INSTRUMENT);
  printf("* Incoming beam: lambda=%.4g [Angs] EI=%.4g [meV]  KI=%.4g [Angs-1] Vi=%g [m/s]\n",
    lambda, EI, KI, Vi);
  time_elastic = 252.78*Chopper2Detector*lambda*1e-6; /* time from Chopper1 to reach Detector */
  printf("  Sample: coh=%s inc=%s\n", coh, inc);
  printf("          Elastic line time: %g [us]\n", time_elastic*1e6);

  /* compute a +/- 20 meV */
  Emin = EI-20; if (Emin < EI/3) Emin=EI/3;
  Emax = EI+20;
  Vmin = sqrt(Emin/VS2E);
  Vmax = sqrt(Emax/VS2E);
  Tmin = 2.5/Vmax;
  Tmax = 2.5/Vmin;

  sprintf(detector_options,
    "banana, abs angle limits=[13 136] bins=300, time limits=[%g %g] bins=256",
    time_elastic+Tmin, time_elastic+Tmax);
  sprintf(lmon_options,"lambda limits=[%g %g] bins=100",lambda-dlambda/2.0,lambda+dlambda/2.0);
  sprintf(tmon_options,"tof limits=[%g %g] bins=100",tmin,tmax);
%}

TRACE

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

COMPONENT Source = Source_gen(T1=43.7 ,I1=1.4e12, T2=137.2,I2=2.08e12,radius=.155,
  lambda0=lambda, dlambda=dlambda,
  focus_xw=0.03, focus_yh=0.055)
AT (0,0,0) ABSOLUTE

COMPONENT Guide0 = Guide_gravity(w1=0.03, h1=0.055, l=2, m=1.2)
AT (0,0,2) RELATIVE Source

COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS
COMPONENT COPY(Guide0) = COPY(Guide0)
AT (0,0,2+1e-4) RELATIVE PREVIOUS ROTATED (0, 2.0/2500*RAD2DEG, 0) RELATIVE PREVIOUS

COMPONENT Guide_PSD    = Monitor_nD(xwidth=0.03, yheight=0.055, options="y x")
AT (0,0,2+1e-4) RELATIVE PREVIOUS
COMPONENT Guide_Lambda = Monitor_nD(xwidth=0.03, yheight=0.055, options=lmon_options)
AT (0,0,  1e-4) RELATIVE PREVIOUS

/* time set here */
COMPONENT Chopper1     = DiskChopper(radius=0.24,
  yheight=0.06, xwidth=0.03, nu=rpm/60, isfirst=1)
AT (0,0,  1.2e-2/2) RELATIVE Guide_Lambda

COMPONENT Guide_SGS1 = COPY(Guide0)(l=2.41-2*6e-3)
AT (0,0,  1.2e-2)   RELATIVE Guide_Lambda

COMPONENT Guide_SGS2 = COPY(Guide0)(l=5.065-2*6e-3)
AT (0,0,  2.41) RELATIVE Guide_SGS1

COMPONENT Guide_SGS3 = COPY(Guide0)(l=2.352-2*6e-3)
AT (0,0,  5.065)   RELATIVE Guide_SGS2

COMPONENT Guide_CGS  = COPY(Guide0)(l=2.143-2*6e-3, w2=0.015, m=2.4)
AT (0,0,  2.352)   RELATIVE Guide_SGS3

COMPONENT Guide2_PSD    = Monitor_nD(xwidth=0.03, yheight=0.055, options="x y")
AT (0,0,2.143-2*6e-3+1e-4) RELATIVE PREVIOUS
COMPONENT Guide2_Lambda = Monitor_nD(xwidth=0.03, yheight=0.055, options=lmon_options)
AT (0,0,  1e-4) RELATIVE PREVIOUS
COMPONENT Guide2_dXY    = Monitor_nD(xwidth=0.03, yheight=0.055, options="dx dy")
AT (0,0,  1e-4) RELATIVE PREVIOUS

/* distance 11.970 from Chopper 1 */
COMPONENT Chopper6     = DiskChopper(radius=0.24,
  yheight=0.06, xwidth=0.03, nu=rpm/60, delay=252.78*11.97*lambda*1e-6)
AT (0,0,  1.2e-2/2) RELATIVE Guide2_dXY

COMPONENT Guide_time   = COPY(Guide_PSD)(options=tmon_options)
AT (0,0,  1.2e-2/2) RELATIVE Chopper6

COMPONENT Guide_DGS  = COPY(Guide0)(w1=0.015, l=1.0-2*6e-3, w2=0.023, m=2.4)
AT (0,0,  2.143)    RELATIVE Guide_CGS

COMPONENT Sample_pos = Arm()
AT (0,0,2.143+0.165)RELATIVE Guide_DGS

/* sample at 2.143+0.165 m from Chopper 6 */
SPLIT COMPONENT Sample = Isotropic_Sqw(
  Sqw_coh=coh, Sqw_inc=inc, p_interact=0.99,
  xwidth=0.05, yheight=0.06, zdepth=0.002)
AT (0,0,0)          RELATIVE Sample_pos ROTATED (0,45,0) RELATIVE Sample_pos
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

/* detector at 2.5 m from sample */
COMPONENT Ideal_Det= Monitor_nD(radius=2.5, yheight=2,
  options=detector_options, restore_neutron=1)
AT (0,0,0)          RELATIVE Sample_pos

/* total distance from Chopper1: 16.778 */
COMPONENT Detector = PSD_Detector(angle=136-13, yheight=2.0, nx=640, ny=256,
                      radius=2.5, zdepth=0.01, dc=0, threshold=100,
                      borderx=-1, bordery=-1,
                      PressureConv=8, PressureStop=1,
                      FN_Conv="Gas_tables/He3inHe.table", FN_Stop="Gas_tables/He3inCF4.table",
                      xChDivRelSigma=0, yChDivRelSigma=0.0037,
                      filename="NEAT.psd")
AT (0,0,0)          RELATIVE Sample_pos ROTATED (0,(136-13)/2+13, 0) RELATIVE Sample_pos

END
