/*******************************************************************************
* McStas instrument definition URL=http://mcstas.risoe.dk
*
* Instrument: ILL_H22_D1B_noenv
*
* %Identification
* Written by: FARHI Emmanuel (farhi@ill.fr) and SANCHEZ Javier (sanchez-montero@ill.fr)
* Date: June, 2008
* Origin:ILL
* %INSTRUMENT_SITE: ILL
*
* The D1B diffractometer on the H22 curved thermal guide at the ILL
*
* %Description
* The D1B diffractometer on the H22 curved thermal guide at the ILL.
*
* D1B is a two-axis spectrometer dedicated to diffraction experiments requesting a
* high neutron flux. A great number of experiments performed on D1B concern the
* determination of magnetic structures. At small angles where the magnetic peaks
* are expected, a high spatial resolution can be achieved, the FWHM reaches 0.2°
* (for a sample with = 8 mm). Three pyrolitic graphite monochromators focusing
* onto the sample position provide a flux of 6.5·106 n cm-2s-1. A second
* wavelength with = 1.28 Å is available by using a germanium monochromator. D1B is
* equipped with 3He/Xe position-sensitive detector composed of a system of
* multi-electrodes with 400 cells, which span a 2 range of 80°. The detector can
* be moved so that an angular range of 2°<2<130° can be covered. The specially
* designed cryostat is known for its low background crucial for some experiments
* with small intensity changes. Because of its high flux at = 2.52 Å together with
* the large multi-detector, surface studies such as adsorbed phases as well as
* real-time diffraction experiments are possible. Collecting a diffraction pattern
* with sufficient statistics in minutes (1-5 min) even seconds allows in situ
* studies of reaction kinetics. A fast detection of phase transitions can also be
* obtained by scanning the temperature. A complete thermal variation of the
* diffraction patterns (1.5 - 300 K) can be achieved in few hours (3-5h).
*
* %Example: lambda=2.52 Detector: D1B_BananaTheta_I=5415.58
*
* %Parameters:
* lambda: [AA]     mean incident wavelength.
* dlambda: [AA]    wavelength full width.
* RV: [m]          Monochromator vertical curvature, 0 for flat, -1 for automatic setting
* L1: [m]          Source-Monochromator distance
* L2: [m]          Monochromator-Sample distance
* L3: [m]          Sample-Detector distance
* Powder: [str]    File name for powder description, LAZ/LAU/Fullprof
* verbose: [1]     Print DIF configuration. 0 to be quiet
* R_pitch: [deg]   Angular pitch between the absorbing blades
* R_ri: [m]        Inner radius of the collimator
* R_ro: [m]        Outer radius of the collimator
* R_h: [m]         Height of the collimator
* R_ttmin: [deg]   Lower scattering angle limit
* R_ttmax: [deg]   Higher scattering angle limit
* R_present: [1]   Presence flag for the radial collimator. 0: inactivate component.
* Inc_Cryo: [1]    Cryostat incoherent fraction
* Trans_Cryo: [1]  Cryostat event transmission
* Trans_Spl: [1]   Sample event transmission
* Inc_Spl: [1]     Sample incoherent fraction
* TILT: [deg]      Monochromator additional tilt, for rocking curves
* TRAS_X: [m]      Additional monochromator translation along X, left/righ w.r.t beam
* TRAS_Z: [m]      Additional monochromator translation along Z, along guide
* DM: [AA]         Mono lattice spacing
* THETA_M: [deg]   Mono takeoff angle
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT ILL_H22_D1B_noenv(lambda=2.52, dlambda=0.03, DM=0, string Powder="Na2Ca3Al2F14.laz", RV=2.2, L1=0.25, L2=3.0, L3=1.500, TRAS_X=-0, TRAS_Z=0, TILT=0, THETA_M=22.11, R_pitch=.42, R_ri=0.324, R_ro=0.419, R_h=0.090, R_ttmin=-130, R_ttmax=-2, R_present=1, verbose=1, Inc_Cryo=0.02, Trans_Cryo=0.85, Trans_Spl=0.2, Inc_Spl=0.05)

/* The DECLARE section allows us to declare variables or  small      */
/* functions in C syntax. These may be used in the whole instrument. */
DECLARE
%{
%}

USERVARS
%{
/* flags to separate scattering processes */
double flag_sample;
double flag_env;
%}

/* The INITIALIZE section is executed when the simulation starts     */
/* (C code). You may use them as component parameter values. */
INITIALIZE
%{
/* transfert instrument parameters for components */
double KI, Vi, EI;

if (!THETA_M && lambda && DM)
	THETA_M =asin(lambda/(2*DM))*RAD2DEG;
else if (THETA_M && !lambda && DM)
	lambda = fabs(sin(THETA_M*DEG2RAD))*2*DM;
else if (THETA_M && lambda)
	DM = fabs(lambda/sin(DEG2RAD*THETA_M)/2.0);

/* test input parameters */
if (!THETA_M || !DM || !lambda) exit(fprintf(stderr,
      "%s: ERROR: Monochromator take-off, d-spacing or wavelength is null (THETA_M=%g, DM=%g, lambda=%g). Abort.\n",
      NAME_INSTRUMENT, THETA_M, DM, lambda));

if (RV < 0) RV=2*L2*sin(DEG2RAD*THETA_M);
KI = 2*PI/lambda;
Vi = K2V*fabs(KI);
EI = VS2E*Vi*Vi;

if (verbose) {
  printf("%s: Detailed D1B 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);
  printf("* Monochromator:  DM=%.4g [Angs] RV=%.4g [m] %s, take-off THETA_M=%.4g [deg]\n",
    DM, RV, (!RV ? "flat" : "curved"), THETA_M);
  printf("* Sample: '%s' in Al cryostat.\n", Powder);
}

%}

/* Here comes the TRACE section, where the actual      */
/* instrument is defined as a sequence of components.  */
TRACE

%include "ILL/ILL_H22/ILL_H22.instr"

/* additional horizontal divergence monitor at end of guide */
/* also defines a static position to orient remaining instrument */
COMPONENT D2B_Mono_Dx = Monitor_nD(
  xwidth=0.03, yheight=0.2, restore_neutron=1,
  options="dx, all auto, per cm2, slit", restore_neutron=1)
AT (0, 0, L1) RELATIVE PREVIOUS
EXTEND 
%{
  flag_env=0;
  flag_sample=0;
%}
/* TIP: monochromator cradle */
SPLIT COMPONENT mono_cradle = Arm()
  AT (TRAS_X, 0, TRAS_Z) RELATIVE D2B_Mono_Dx

/* TIP: curved monochromator with NH>1 NV>1 et RH>0 RV>0 */
COMPONENT Monok = Monochromator_curved(
    width = 0.060, height = 0.15, NH = 1, NV = 3, RV=RV,
    mosaich = 20, mosaicv = 20, DM = DM)
AT (0, 0, 0) RELATIVE mono_cradle
ROTATED (0, THETA_M+TILT, 0) RELATIVE mono_cradle

/* TIP: positioning diffraction direction for monok (order 1) */
COMPONENT mono_out = Arm()
AT (0, 0, 0) RELATIVE D2B_Mono_Dx
ROTATED (0, 2*THETA_M, 0) RELATIVE D2B_Mono_Dx

/* Monitor a la altura del shutter  */
COMPONENT D1B_monitor_shutter = Monitor_nD(
  options="x y", bins=50, xwidth = 0.030,
  yheight = 0.20)
AT (0,0,0.75) RELATIVE mono_out

/* filtro de grafito -ocupa toda la linea- */
COMPONENT filtro_D1B = Filter_graphite(
	xmin=-0.05, xmax=0.05, ymin=-.10, ymax=.10, length=0.20)
AT (0,0,0.75) RELATIVE mono_out


/* Linea propia de D1B, desde el monocromador hasta Slits*/
/*							distancia desde el monocromador		ancho		alto   */
/*slit_fijo			985.5															50			118    */
/*Ventana_1			1176															50			112    */
/*Ventana_2			1436															50			112    */
/*Ventana_3			1753															50			112    */


/* slit fija  */
COMPONENT slit_fija = Slit(
  xmin = -0.025, xmax = 0.025,
  ymin = -0.059, ymax = 0.059)
  AT (0, 0, 0.9855) RELATIVE mono_out

/* ventana 1  */
COMPONENT ventana_1 = Slit(
  xmin = -0.025, xmax = 0.025,
  ymin = -0.056, ymax = 0.056)
  AT (0, 0, 1.176) RELATIVE mono_out

/* ventana 2  */
COMPONENT ventana_2 = Slit(
  xmin = -0.025, xmax = 0.025,
  ymin = -0.056, ymax = 0.056)
  AT (0, 0, 1.436) RELATIVE mono_out

/* ventana 3  */
COMPONENT ventana_3 = Slit(
  xmin = -0.025, xmax = 0.025,
  ymin = -0.056, ymax = 0.056)
  AT (0, 0, 1.753) RELATIVE mono_out

/* Monitor de control que coincide con las cuentas de monitor del D1B. Se ha introducido el comando SPLIT en los dos siguientes */
COMPONENT monitor_D1B = Monitor_nD(
     options="auto lambda", bins=50, xwidth = 0.100,
     yheight = 0.100)
     AT (0, 0, 2.478) RELATIVE mono_out

COMPONENT PSD_D1B = Monitor_nD(
     options="x y", bins=50, xwidth = 0.100,
     yheight = 0.100)
   	 AT (0, 0, 2.478) RELATIVE mono_out


/* Slits ... Valores anteriores ~ xmin=-0.020, xmax=0.020,  */
SPLIT COMPONENT slit_D1B = Slit(
  	xmin=-0.010, xmax=0.010,
  	ymin = -0.015, ymax = 0.015)
  	AT (0, 0, 2.5) RELATIVE mono_out
EXTEND %{
  flag_sample=flag_env=0;
%}

/* Definicion del entorno.     */
/* Se fuerza que la difraccion se concentre en el detector, para aumentar la eficiencia.   */


/* COMPONENT Cryo_1 = PowderN(reflections="Al.laz", */
/*     radius = 0.061, thickness = 0.001, yheight = 0.10, */
/*     concentric = 1, d_phi=RAD2DEG*atan(R_h/L3), */
/*     p_inc=Inc_Cryo, p_transmit=Trans_Cryo) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT Cryo_2 = COPY(Cryo_1)(radius = 0.0545, thickness = 0.001) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT Cryo_3 = COPY(Cryo_1)(radius = 0.039, thickness = 0.001) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT Cryo_4 = COPY(Cryo_1)(radius = 0.036, thickness = 0.001) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

COMPONENT Sample = PowderN(
    reflections = Powder, radius = 0.0030,
    p_transmit=Trans_Spl, p_inc=Inc_Spl,
    yheight = 0.040, d_phi=RAD2DEG*atan(R_h/L3))
    AT (0, 0, L2) RELATIVE mono_out
EXTEND
%{
  flag_sample=SCATTERED;
%}

/* COMPONENT COPY(Cryo_4)=COPY(Cryo_4)(concentric = 0) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT COPY(Cryo_3)=COPY(Cryo_3)(concentric = 0) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT COPY(Cryo_2)=COPY(Cryo_2)(concentric = 0) */
/* 		AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/* %} */

/* COMPONENT COPY(Cryo_1)=COPY(Cryo_1)(concentric = 0) */
/* AT (0,0,L2) RELATIVE mono_out */
/* EXTEND */
/* %{ */
/*   flag_env+=SCATTERED; */
/*   if (!flag_sample && !flag_env) ABSORB;  /\* TIP: perfect beamstop *\/ */
/* %} */


/* se introduce el Collimator radial, he introducido el comando ROC       */
/* http://www.mcstas.org/download/components/contrib/Collimator_ROC.comp  */
/* ROC_pitch:  [deg] Angular pitch between the absorbing blades (1)       */
/* ROC_ri:     [m]   Inner radius of the collimator     (0.4)     */
/* ROC_ro:     [m]   Outer radius of the collimator     (1.2)     */
/* ROC_h:      [m]   Height of the collimator     (0.153)   */
/* ROC_ttmin:  [deg] Lower scattering angle limit (0)       */
/* ROC_ttmax:  [deg] Higher scattering angle limit(100)     */
/* ROC_sign:   [1]   Chirality/takeoff sign       (1)       */
/* ROC_present:[1]   Presence flag of this component    (1)       */

COMPONENT collimador_radial = Exact_radial_coll(
     nslit=ceil(128/R_pitch), radius=R_ri, length=R_ro-R_ri, h_in=R_h, h_out=R_h,
     theta_min=R_ttmin, theta_max=R_ttmax, verbose=1)
  WHEN (R_present)
	AT (0, 0, 0) RELATIVE Sample

/* perfect detector: 1D(theta). Se ha modificado los limites. Originalm ~ [-4 -130]*/
COMPONENT D1B_BananaTheta = Monitor_nD(
    options = "banana, theta limits=[-130 -2], bins=1280",
    xwidth = L3*2, yheight = R_h, restore_neutron=1)
  AT (0, 0, 0) RELATIVE Sample

COMPONENT D1B_BananaTheta_Sample = Monitor_nD(
    options = "banana, theta limits=[-130 -2], bins=1280",
    xwidth = L3*2, yheight = R_h)
  WHEN (flag_sample)
  AT (0, 0, 0) RELATIVE Sample

/* perfect detector: 2D(theta,y) to see diffraction rings. Originalmente: y bins=25 */
/* COMPONENT BananaPSD = Monitor_nD(  */
/*    options = "banana, theta limits=[-130 -2] bins=1280, y bins=150",   */
/*    xwidth = L3*2*1.005, yheight = 0.3) */
/*  AT (0, 0, 0) RELATIVE Sample*/



END
