/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2008, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Instrument: ISIS_GEM
*
* %Identification
* Written by: E. Farhi, G. Cuello, M. Tucker
* Date: September 20, 2006
* Origin: ISIS
* %INSTRUMENT_SITE: ISIS
*
* McStas instrument for simulating the GEM diffractometer at ISIS TS1.
*
* %Description
* McStas instrument for simulating GEM at ISIS TS1.
* The sample is a powder. The detector is simplified as a banana shaped one.
*
* The General Materials Diffractometer is a new generation neutron diffractometer
* recently constructed at the ISIS pulsed neutron source. GEM can be used to
* perform high intensity, high resolution experiments to study the structure of
* disordered materials and crystalline powders.
*
* %Example: sample="Y2O3.laz" Detector: monzns_I=52.689
*
* %Parameters:
* l_min: [AA] Minimum wavelength produced at source
* l_max: [AA] Maximum wavelength produced at source  
* dist: [m] Sample-detector distance (detector radius)  
* sample: [string] Reflection list for powder sample  
*
* %Link
* The <a href="http://essworkshop.org">ESS Instrument workshops</a> website.
* %Link
* The <a href="http://www.isis.stfc.ac.uk/instruments/gem/">GEM instrument</a> at ISIS
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT ISIS_GEM(l_min=0.1, l_max=4.2, dist=1.3795, string sample="Y2O3.laz")
DECLARE
%{
  double emin, emax;
%}
INITIALIZE
%{
  emin=81.799/l_max/l_max;
  emax=81.799/l_min/l_min;
%}
TRACE

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

COMPONENT Source = ISIS_moderator(
    Face = "gem", Emin = emin, Emax = emax, target_index=1, focus_xw = 0.008,
    focus_yh = 0.04, xwidth = 0.09, yheight = 0.084, CAngle = 0,dist=0,
    SAC = 1)
AT (0, 0, 0) ABSOLUTE

SPLIT COMPONENT Sample = PowderN(
    yheight=0.04, radius=8e-3/2,p_interact=1,
    reflections = sample,d_phi=atan(.20/dist)*RAD2DEG)
AT (0, 0, 17.00) RELATIVE Source
EXTEND %{
  /* take into account ZnS scintillator efficiency */
  if (!SCATTERED) ABSORB;
  double eff=0.5;
  double vloc=sqrt(vx*vx+vy*vy+vz*vz);
  p *= 1.0-exp(3960.0/vloc*log(1.0-eff/100.0));
%}

COMPONENT sample_out = Arm()
AT (0,0,0) RELATIVE Sample ROTATED (0,0, 0) RELATIVE Sample

COMPONENT monzns = Monitor_nD(
    radius=dist, yheight=.20, options="banana, angle bins=40 limits=[80 110], time bins=128 limits=[0.001 0.019]")
  AT (0, 0, 0) RELATIVE sample_out

END
