/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Test_Sqq_w
*
* %Identification
* Written by: P. Willendrup
* Date: June, 2018
* Origin: DTU
*
* %INSTRUMENT_SITE: Tests_samples
*
* Test instrument for the Sqq_w_monitor and Magnon_bcc components, derived from template_Laue 
*
* %Description
*
* %Example: inelastic=1 Detector: det_I=4.98e11
*
* %Parameters
* lambda:     [AA]      Central wavelength of wavelength distribution
* dlambda:    [AA]      Width of wavelength distribution
* Rotation:   [deg]     Sample orientation
* inelastic:  [in 0:1]  Fraction of statistics to scatter inelastically
* aa:         [AA]      BCC lattice constant
* sample_J:   [meV]     Magnitude of sample nearest-neighbour interaction
* TT:         [K]       Sample temperature
* FerroMagnet:[boolean] Flag to choose if sample is FM or AFM
* verbose:    [boolean] Flag to allow verbose information from Magnon comp
* imultiplier:[1]       Parameter to rescale intensity-output from Magnon comp
*
* %End
*******************************************************************************/

/* Change name of instrument and input parameters with default values */
DEFINE INSTRUMENT Test_Magnon_bcc_2D(lambda=10, dlambda=9.9, Rotation=0, inelastic=1, aa=6.283185307179586,
				     sample_J=2, TT=300, FerroMagnet=0, Verbose=0, imultiplier=1)
DEPENDENCY " -O0 "

DECLARE %{
%}

USERVARS %{
  double Samplechoice;
  double vix;
  double viy;
  double viz;
%}

INITIALIZE %{
%}

TRACE

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

COMPONENT source = Source_simple(
  radius=0.02, focus_xw=0.01, focus_yh=0.01, target_index=1,
  lambda0=lambda, dlambda=dlambda, flux=1e12)
AT (0,0,0) ABSOLUTE
EXTEND %{
  Samplechoice=rand01();
  vix=vx;
  viy=vy;
  viz=vz;
%}

COMPONENT In = Event_monitor_simple(nevents=1e2)
  AT (0, 0, 5.1) RELATIVE source

COMPONENT Magnon = Magnon_bcc(
    radius=0.02,
    yheight=0.03,
    sigma_abs=0, 
    sigma_inc=1, 
    a=aa,  
    J1=sample_J, 
    J2=0,
    D=0.0,
    s=1,
    DW=1,
    verbose=Verbose, 
    T=TT, 
    FM = FerroMagnet,
    target_index=0,
    focus_xw=0, 
    focus_yh=0,
    focus_aw=180, /* For statistics considerations, only a small angular */
    focus_ah=0.1)   /* band arround sample is illuminated */
  WHEN (inelastic>0 && Samplechoice<inelastic)  AT (0, 0, 5.1) RELATIVE source
  ROTATED (0, Rotation, 0) RELATIVE source
EXTEND %{
    p/=INSTRUMENT_GETPAR(inelastic);
    p*=INSTRUMENT_GETPAR(imultiplier);
%}


COMPONENT Lattice = Single_crystal(order=1, 
     reflections = "BCC_fake.lau", mosaic=30, radius = 0.02, yheight = 0.03,   delta_d_d=1e-3,
     ax =-aa , ay = 0, az =0 , bx =0 , by =0, bz = aa , cx =0 , cy = aa , cz =0, barns=1)
  WHEN (inelastic<1 && Samplechoice>=inelastic) AT ( 0, 0, 5.1) RELATIVE source
  ROTATED (0, Rotation, 0) RELATIVE source
EXTEND %{
  p/=(1-INSTRUMENT_GETPAR(inelastic));
   // Remove direct beam
  if(!SCATTERED) ABSORB;
%}

COMPONENT Out = Event_monitor_simple(nevents=1e2)
  AT (0, 0, 5.1) RELATIVE source

  COMPONENT Sqqw = Sqq_w_monitor(filename="qa_vs_qb",nE=33,nqa=401,nqb=401,qamin=0.5,qamax=4.5,qamin=0.5,qbmax=4.5,Emin=-16, Emax=16, yheight=0.1, vix="vix", viy="viy", viz="viz")
AT (0,0,0) RELATIVE Magnon

COMPONENT det= PSD_monitor_4PI(radius=2.1, nx=360,ny=180,filename="psd")
AT (0,0,0) RELATIVE Magnon

END

