/*******************************************************************************
*
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: Chopper_simple
*
* %Identification
*
* Written by; Erik Knudsen 
* Date: August 2011
* Release: McXtrace 1.0
* Origin: Risoe
*
* Ideal chopper
*
* %Description
* Ideal model of a chopper situated at Z=0. If a photon arrives at the chopper plane
* at a time of t = [t0 +-n*T: t0 +-n*T +tau], where T is the period of the chopper, t0 the initial delay
* and tau the opening time of the chopper, it is left untouched - otherwise it is ABSORBed. 
* If on a continous source the isfirst parameter may be used. In this case the photon time is _defined_
* by the chopper. In other words no photons are absorbed, the photon time is merely sampled within the chopper window.
* t_rise is the rise-time of the chopper opening giving a trapezoidal shape.
* Limitations: this component does not take chopper geometry into account. If isfirst only samples in the first chopper opening window.
*
* Example: Chopper_simple(
* t0 = -0.5/M_C, T = 20e-6, tau = 100e-12, xwidth = 1e-4,
* yheight = 1e-4, isfirst = 1)
* 
*
* %Parameters
* Input parameters:
* t0:       [s]   Initial delay of the opening time
* T:        [s]   Period of the chopper
* tau:      [s]   Opening time of the chopper 
* t_rise:   [s]   Rise time of the chopper pulse.
* xwidth:   [m]   Height of the chopper opening
* yheight:  [m]   Width of the chopeper opening
* isfirst:  [0/1] Is the chopper the first chopper on a continous source.
* tjit:     [1]   Timing jitter in terms of the opening time tau. For each ray the opening window will be shifted by a random amount within t=[-tjit*.5*tau,tjit*.5*tau]  
*
* %End
*******************************************************************************/

DEFINE COMPONENT Chopper_simple
SETTING PARAMETERS (t0=0,T=1,tau=0.1,xwidth=0.1,yheight=0.1,isfirst=0,t_rise=0, tjit=0)

INITIALIZE
%{
  if (T<=0 || tau<=0){
    fprintf(stderr,"Error: (%s): Periodm T=%g <=0 or opening time, tau=%g <=0\n",NAME_CURRENT_COMP,T,tau);
    exit(-1);
  }
  if(xwidth<=0 || yheight<=0){
    fprintf(stderr,"Error: (%s): Opening area (xwidth x yheight) = (%g x %g) <=0\n",NAME_CURRENT_COMP,xwidth,yheight);
    exit(-1);
  }
%}

TRACE
%{
  double tjitter=0;
  PROP_Z0;
  if(tjit) tjitter = (rand01()-0.5)*tau*tjit;
  if ( fabs(x)<xwidth/2.0 && fabs(y)<yheight/2.0){
    if (isfirst){
      if (t_rise==0){
        /*this is the first time defining element*/
        t=t0+rand01()*tau+tjitter;
        p*=tau/T;/*correct for only sampling part of the time line*/
      }else{
        t=rand01()*(tau+2*t_rise);
        if (t<t_rise){
          p*=t/t_rise;
        }else if( t<tau+t_rise && t>tau ){
          p*=(1-t_rise)/t_rise;
        }
        t+=t0;
      }
    }else{
      int n=(int) floor((t-t0)/T);
      if(t-(t0+n*T+tjitter)>tau){
        ABSORB;
      }
    }
  }else{
    ABSORB;
  }
%}

MCDISPLAY
%{
  /*chopper is symbolically shown as a disc chopper, although it is not*/ 
  
  rectangle("xy",0,0,0,xwidth,yheight);
  double delta,radius;
  delta=2*yheight;
  radius=sqrt(xwidth*xwidth/4+delta*delta);
  circle("xy",0,-(delta-yheight/2.0),0,radius);
%}

END
