/*******************************************************************************
*         McStas instrument definition URL=http://mcstas.risoe.dk
*
* Instrument: Test_Sources
*
* %Identification
* Written by: FARHI Emmanuel (farhi@ill.fr)
* Date: Aug 3, 2008
* Origin:ILL
* %INSTRUMENT_SITE: Tests_optics
*
* A test instrument to compare sources
*
* %Description
* A test instrument to compare sources and check they provide the valid
* sprectrum and intensity. It shows that the first 4 flat sources are equivalent,
* the 2 Maxwellian sources as well.
*
* WARNING: Result of test no. 1 for Source_adapt.comp is not correct if MPI is used, as
* that source component does not support MPI.
*
* Example: source=1 Detector: m1_I=9.97273e+11
* %Example: source=2 Detector: m1_I=9.48467e+11
* %Example: source=3 Detector: m1_I=9.96553e+11
* %Example: source=4 Detector: m1_I=9.966e+11
* %Example: source=5 Detector: m1_I=2.4279e+11
* %Example: source=6 Detector: m1_I=2.42284e+11
* %Example: source=7 Detector: m1_I=2.28139e+13
* %Example: source=8 Detector: m1_I=5.0787e+13
*
* %Parameters
* source: [1]  selection of the source to use in 1=Source_adapt, 2=Source_div, 3=Source_simple, 4=Source_gen (simple), 5=Source_gen, 6=Source_Maxwell_3, 7=ESS_butterfly, 8=Moderator
* Lmin: [AA] Minimum wavelength produced at source
* Lmax: [AA] Maximum wavelength produced at source
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Test_Sources(int source=0, Lmin=1, Lmax=11)

DECLARE
%{
  double Lambda0;
  double dLambda;
  char lamopts[128];
%}

INITIALIZE
%{
  printf("%s: ", NAME_INSTRUMENT);
  if (source==1) printf("Using Source_adapt\n");
  else if (source==2) printf("Using Source_div\n");
  else if (source==3) printf("Using Source_simple\n");
  else if (source==4) printf("Using Source_gen (flat spectrum)\n");
  else if (source==5) printf("Using Source_gen\n");
  else if (source==6) printf("Using Source_Maxwell_3\n");
  else if (source==7) printf("Using ESS_butterfly\n");
  else if (source==8) printf("Using Moderator\n");
  else { 
    printf("Selected moderator undefined, exiting!\n");
    exit(-1);
  }
  printf("Studied wavelength range is [%g %g] AA\n", Lmin, Lmax);
  dLambda=(Lmax-Lmin)/2;
  Lambda0=(Lmax+Lmin)/2;
  sprintf(lamopts,"lambda limits=[%g %g]",Lmin,Lmax);
%}

TRACE

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

/* the sources ============================================================== */

COMPONENT sa2 = Source_div(  /* flux in n/s/cm2/st/AA */
  xwidth=0.01, yheight=0.01, focus_aw=2*RAD2DEG*atan(0.1/1.0/2), focus_ah=2*RAD2DEG*atan(0.1/1.0/2), lambda0=Lambda0, dlambda=dLambda, gauss=0, flux=1e13
) WHEN (source == 2)
AT (0,0,0) ABSOLUTE

COMPONENT sa3 = Source_simple(   /* flux in n/s/cm2/st/AA */
  xwidth=0.01, yheight=0.01,
  dist=1, focus_xw=0.1, focus_yh=0.1,
  lambda0=Lambda0, dlambda=dLambda, flux=1e13
) WHEN (source == 3)
AT (0,0,0) ABSOLUTE

COMPONENT sa4 = Source_gen(  /* flux in n/s/cm2/st/AA */
  Lmin=Lmin, Lmax=Lmax, dist=1, focus_xw=0.1, focus_yh=0.1,
  xwidth=0.01, yheight=0.01,
  I1=1e+13
) WHEN (source == 4)
AT (0,0,0) ABSOLUTE

COMPONENT sa5 = Source_gen(  /* flux in n/s/cm2/st/AA */
  Lmin=Lmin, Lmax=Lmax, dist=1, focus_xw=0.1, focus_yh=0.1,
  xwidth=0.01, yheight=0.01, verbose=1,
  T1=216.8,I1=1.24e+13,T2=33.9,I2=1.02e+13, T3=16.7 ,I3=3.0423e+12
) WHEN (source == 5)
AT (0,0,0) ABSOLUTE

COMPONENT sa6 = Source_Maxwell_3( /* flux in n/s/cm2/st/AA */
  Lmin=Lmin, Lmax=Lmax, dist=1, focus_xw=0.1, focus_yh=0.1,
  xwidth=0.01, yheight=0.01,
  T1=216.8,I1=1.24e+13,T2=33.9,I2=1.02e+13, T3=16.7 ,I3=3.0423e+12
) WHEN (source == 6)
AT (0,0,0) ABSOLUTE

COMPONENT sa7 = ESS_butterfly(sector="W",beamline=8,
  Lmin=Lmin, Lmax=Lmax, dist=1, focus_xw=0.1, focus_yh=0.1
) WHEN (source == 7)
AT (0,0,0) ABSOLUTE

COMPONENT sa8 = Moderator( /* flux in n/s/cm2/st */
  radius = 0.01*sqrt(2), dist=1, focus_xw=0.1, focus_yh=0.1,
  Emin =81.81/(Lmax*Lmax), Emax = 81.81/(Lmin*Lmin), Ec = 9.0, t0 = 37.15, gamma = 39.1, flux=1e13
) WHEN (source == 8)
AT (0,0,0) ABSOLUTE


COMPONENT Events = Event_monitor_simple(nevents=1e3)
  AT (0,0,0) ABSOLUTE


/* the monitor ============================================================== */
COMPONENT m1 = Monitor_nD(
  xwidth=0.1, yheight=0.1,
  options=lamopts, bins=40
) AT (0,0,1) ABSOLUTE

COMPONENT m2 = Monitor_nD(
  xwidth=0.2, yheight=0.2,
  options="x, y, parallel", bins=40
) AT (0,0,1) ABSOLUTE

COMPONENT m3 = Monitor_nD(
  xwidth=0.2, yheight=0.2,
  options="dx limits=[-3.5 3.5] dy limits=[-3.5 3.5], parallel", bins=40
) AT (0,0,1) ABSOLUTE

END
