/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Guides
*
* %Identification
* Written by: E. Farhi [farhi@ill.fr]
* Date: Sept 1st, 2008
* Origin: ILL
* %INSTRUMENT_SITE: Tests_optics
*
* Cross comparison of Guide components
*
* %Description
* Cross comparison of Guide components, using McStas and
* contributed components. It shows that all implementations are equivalent,
* except the Guide_honeycomb which has a different geometry.
*
* %Example: Guide=1 Detector: Monitor2_xy_I=0.00726875
* %Example: Guide=2 Detector: Monitor2_xy_I=0.00725213
* %Example: Guide=3 Detector: Monitor2_xy_I=0.00727304
* %Example: Guide=4 Detector: Monitor2_xy_I=0.00721742
* %Example: Guide=5 Detector: Monitor2_xy_I=0.00721742
* %Example: Guide=6 Detector: Monitor2_xy_I=0.00721742
* %Example: Guide=7 Detector: Monitor2_xy_I=0.00724912
* %Example: Guide=8 Detector: Monitor2_xy_I=0.00683072
* %Example: Guide=9 Detector: Monitor2_xy_I=0.00724912
*
* %Parameters
* Guide: [1]  Choice of Guide component to test, with 1=Guide, 2=Guide_channeled, 3=Guide_gravity, 4=Guide_wavy, 5=Guide_curved 6=Elliptic_guide_gravity 7=Guide_honeycomb
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_Guides(int Guide=1)
DEPENDENCY " -DUSE_OFF "

DECLARE %{
  int _Guide;
  #pragma acc declare create(_Guide)
%}

USERVARS
%{
  int Flag_anyshape_scatter;
  int Flag_anyshape_r_scatter;
%}

INITIALIZE
%{
  if(Guide==1) printf("Using Guide\n");
  if(Guide==2) printf("Using Guide_channeled\n"); 
  if(Guide==3) printf("Using Guide_gravity\n"); 
  if(Guide==4) printf("Using Guide_wavy\n"); 
  if(Guide==5) printf("Using Guide_curved\n");
  if(Guide==6) printf("Using Elliptic_guide_gravity\n"); 
  if(Guide==7) printf("Using Guide_anyshape\n");
  if(Guide==8) printf("Using Guide_honeycomb\n");
  if(Guide==9) printf("Using Guide_r_anyshape\n");

  _Guide=Guide;
  #pragma acc update device(_Guide)
%}

TRACE

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

COMPONENT Source = Source_gen(
   focus_xw = 0.05, focus_yh = 0.05, lambda0 = 3.39, dlambda = 0.3,
   yheight = 0.05, xwidth = 0.05)
  AT (0, 0, 0) RELATIVE PREVIOUS
EXTEND %{
	t = randtriangle()*1e-3;	/* 1 ms triangle time window */
	Flag_anyshape_scatter=0;
%}

COMPONENT Monitor1_xt = Monitor_nD(
    options = "x y", xwidth = 0.05, yheight = 0.05)
  AT (0, 0, 1) RELATIVE PREVIOUS

COMPONENT Guide_Position = Arm( )
  AT (0, 0, 0.1) RELATIVE PREVIOUS

COMPONENT Guide1 = Guide(
    w1 = 0.05, h1 = 0.05, l = 1.0, R0 = 0.99, m=1)
  WHEN (Guide == 1)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideC = Guide_channeled(
    w1 = 0.05, h1 = 0.05, l = 1.0, R0 = 0.99, d=0, m=1)
  WHEN (Guide == 2)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideG = Guide_gravity(
    w1 = 0.05, h1 = 0.05, l = 1.0, R0 = 0.99, d=0, m=1)
  WHEN (Guide == 3)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideW = Guide_wavy(
    w1 = 0.05, h1 = 0.05, l = 1.0, R0 = 0.99, m=1)
  WHEN (Guide == 4)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideR = Guide_curved(
    w1 = 0.05, h1 = 0.05, l = 1.0, R0 = 0.99, m=1, curvature=10000)
  WHEN (Guide == 5)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideEl = Elliptic_guide_gravity(
  xwidth = 0.05, yheight=0.05, l = 1.0, R0 = 0.99, m=1,
  linxw=1e6, linyh=1e6, loutxw=1e6, loutyh=1e6, dimensionsAt="mid")
  WHEN (Guide == 6)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}

COMPONENT GuideA_prefix = Slit(xwidth=0.05, yheight=0.05)
  WHEN (Guide == 7)
AT (0,0,-0.0001) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_scatter++;
%}

COMPONENT GuideA = Guide_anyshape(geometry="Guide.off", R0=0.99, m=1)
  WHEN (Guide == 7)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_scatter++;
%}

COMPONENT GuideA_sufffix = Slit(xwidth=0.05, yheight=0.05)
  WHEN (Guide == 7)
AT (0,0,1+0.0001) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_scatter++;
  if (_Guide == 7 && !Flag_anyshape_scatter) ABSORB;
%}
  
COMPONENT GuideH = Guide_honeycomb(
    w1 = 0.05, l = 1.0, R0 = 0.99, m=1, d=0)
  WHEN (Guide == 8)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (!SCATTERED) ABSORB;
%}


COMPONENT GuideAr_prefix = Slit(xwidth=0.05, yheight=0.05)
  WHEN (Guide == 9)
AT (0,0,-0.0001) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_r_scatter++;
%}

COMPONENT GuideAr = Guide_anyshape_r(geometry="Guide_r.off", R0=0.99, m=0)
  WHEN (Guide == 9)
  AT (0, 0, 0) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_r_scatter++;
%}

COMPONENT GuideAr_sufffix = Slit(xwidth=0.05, yheight=0.05)
  WHEN (Guide == 9)
AT (0,0,1+0.0001) RELATIVE Guide_Position
EXTEND %{
  if (SCATTERED) Flag_anyshape_r_scatter++;
  if (_Guide== 9 && !Flag_anyshape_r_scatter) ABSORB;
%}  

COMPONENT Monitor2_xy = Monitor_nD(
    options = "x y", xwidth = 0.07, yheight = 0.07)
  AT (0, 0, 2) RELATIVE Guide_Position

END
