/*******************************************************************************
*
* McStas, neutron ray-tracing package
*         Copyright (C) 1997-2006, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: Chopper
*
* %I
* Written by: Philipp Bernhardt
* Date: Januar 22 1999
* Version: $Revision: 1.3 $
* Origin: Risoe
* Release: McStas 1.9.1
* Modified by: P. Willendrup, renamed into DiskChopper with upgrade
*
* Disk chopper.
*
* %D
* Models a disc chopper with n identical slits, which are symmetrically disposed
* on the disc.
* If the chopper is the 1st chopper of the instrument, it sets t time with phase
* OBSOLETE: rather use optics/DiskChopper
*
* Example: Chopper(R=0.2, w=0.05, f=2500.0, n=3, pha=0, IsFirst=1) First chopper
*          Chopper(R=0.2, w=0.05, f=2500.0, n=3, pha=0, IsFirst=0)
*
* %P
* INPUT PARAMETERS:
*
* w:       (m)      Width of the slits at the bottom side
* R:       (m)      Radius of the disc
* f:       (rad/s)  angular frequency of the Chopper
*                   (algebraic sign defines the direction of rotation)
* n:       (1)      Number of slits
* pha:     (s)      Phase
*
* Optional parameters:
* IsFirst: (0/1)    Set it to 1 for the first chopper position in a cw source
*                   (it then spreads the neutron time distribution)
* n_pulse: (1)      Number of pulses
*
* %D
* Example values: w=0.05 R=0.5 f=2500 n=3 pha=0 n_pulse=1
*
* %E
*******************************************************************************/

DEFINE COMPONENT Chopper
DEFINITION PARAMETERS ()
SETTING PARAMETERS (R, w=0.05, f=2500.0, n=3, pha=0, IsFirst=0, n_pulse=1)
OUTPUT PARAMETERS (Tg, T, To)
STATE PARAMETERS (x, y, z, vx, vy, vz, t, s1, s2, p)

DECLARE
 %{
      double Tg,To;
 %}

INITIALIZE
 %{
      if (n<=0 || w < 0 || R <=0)
      { fprintf(stderr,"Chopper: %s: n, w and R must be > 0\n", NAME_CURRENT_COMP);
        exit(-1); }
      if (IsFirst && n_pulse <=0)
      { fprintf(stderr,"Chopper: %s: wrong First chopper pulse number (n_pulse)\n", NAME_CURRENT_COMP);
        exit(-1); }

      /* time between two pulses */
      Tg=2*PI/fabs(f)/n;

      /* how long can neutrons pass the Chopper at a single point */
      To=2*atan(w/R/2.0)/fabs(f);
 %}

TRACE
 %{
    double toff;

    PROP_Z0;

    if (IsFirst)
    {
      t=atan2(x,y+R)/f+To*(rand01()-0.5)+pha+floor(n_pulse*rand01())*Tg;
      p*=n*atan2(w,R)/2/PI;
    }
    else
    {
      toff=fabs(t-atan2(x,y+R)/f-pha)+To/2.0;

      /* does neutron hit the slit? */
      if (fmod(toff,Tg)>To)
         ABSORB;
    }
    SCATTER;
 %}

MCDISPLAY
%{
  magnify("xy");
  circle("xy", 0, 0, 0, R);
%}

END
