/*******************************************************************************
*
* McXtrace, x-ray tracing package
*         Copyright, All rights reserved
*         DTU Physics, Kgs. Lyngby, Denmark
*         Synchrotron SOLEIL, Saint-Aubin, France
*
* Component: Lens_CRL_RTM
*
* %Identification
*
* Written by: Erik Knudsen 
* Date: Jan '21 
* Version: 1.1
* Origin: DTU Physics
*
* 1D CRL stack based on RTM formalism
*
* %Description
* A CRL stack component based on the formalism presented by Simons et.al. J. Synch. Rad.
* 2017, vol. 24.
* We model a 1D lens stack focusing in the y-direction. I.e. invariant along x.
* Photons outside the aperture (|x|<xwidth/2, |y|<yheight/2) are absorbed.
*
* Example: Lens_CRL_RTM(
*   r=0.5e-3, N=10, fast=1, yheight=0, d=0.1e-3,xwidth=1e-4, zdepth=4e-4)
*   
* %Parameters
* Input parameters:
*
* r:        [m]   Radius of curvature at the lens apex.
* N:        [1]   Number of lenslets in the stack.
* xwidth:   [m]   Width of the lenslets (invariant direction).
* yheight:  [m]   Full physical height of the aperture. If zero the aperture is set by the lenslet thickness: Y=sqrt(r*zdepth).
* zdepth:   [m]   Length (pitch) of a single lenslet along z.
* d:        [m]   Thickness of the web between the two parabolic surfaces of a single lenslet.
* material: [str] Datafile containing f1 constants
* fast:     [1]   Use fast (analytical) absorption calculation - set to 0 for better display with mxdisplay
*
* %End
*******************************************************************************/

DEFINE COMPONENT Lens_CRL_RTM
SETTING PARAMETERS (r=0.5e-3,d=0.1e-3,string material="Be.txt",int N=1,
    zdepth=2e-3,yheight=0, xwidth=1.2e-3, int fast=1)

SHARE
%{
  %include "read_table-lib"
%}

DECLARE
%{
  double Y;
  t_Table matT;
  double rho;
  double Ar;
  int Z;
%}

INITIALIZE
%{
  int status;
  if (r <= 0 || zdepth <= 0 || N < 1) {
    fprintf (stderr, "Error (%s): r, zdepth must be positive and N>=1.\n", NAME_CURRENT_COMP);
    exit (-1);
  }
  /* half-height of the aperture */
  if (yheight > 0) {
    Y = yheight / 2.0;
    if (Y > sqrt (r * zdepth) * (1.0 + 1e-9))
      fprintf (stderr, "Warning (%s): yheight/2 exceeds sqrt(r*zdepth); the parabolic profile is deeper than zdepth.\n", NAME_CURRENT_COMP);
  } else {
    Y = sqrt (r * zdepth);
  }
  if ((status = Table_Read (&(matT), material, 0)) == -1) {
    fprintf (stderr, "Error (%s): Could not parse file \"%s\"\n", NAME_CURRENT_COMP, material);
    exit (-1);
  }
  char** header_parsed;
  header_parsed = Table_ParseHeader (matT.header, "Z", "A[r]", "rho", NULL);
  if (!Z)
    Z = strtol (header_parsed[0], NULL, 10);
  if (!Ar)
    Ar = strtod (header_parsed[1], NULL);
  if (!rho)
    rho = strtod (header_parsed[2], NULL);
%}

TRACE
%{
  PROP_Z0;
  if (fabs (x) >= xwidth / 2.0 || fabs (y) >= Y) {
    /* outside the lens aperture */
    ABSORB;
  }
  SCATTER;

  double k, k_yz;
  double alpha0, y0, x0;
  double f1, mu, delta, rhoel;
  double f, psi, fpsi;
  double yN, alphaN;

  k_yz = sqrt (ky * ky + kz * kz);
  k = sqrt (kx * kx + ky * ky + kz * kz);

  /*get material data from tables*/
  mu = Table_Value (matT, k * K2E, 5) * rho * 1e2; /*mu is now in SI, [m^-1]*/
  f1 = Table_Value (matT, k * K2E, 1);

  rhoel = f1 * NA * (rho * 1e-24) / Ar; /*Material's Number Density of Electrons [e/A^3] incl f' scattering length correction*/
  delta = 2.0 * M_PI * RE * rhoel / (k * k);

  /*focal length of one lenslet (two surfaces) and RTM angle per lenslet*/
  f = r / (2 * delta);
  psi = sqrt (zdepth / f);
  fpsi = sqrt (f * zdepth); /* = f*psi */
  alpha0 = atan2 (ky, kz);  /* paraxial angle in the yz-plane */
  x0 = x;
  y0 = y;

  /*RTM matrix for N lenslets*/
  yN = cos (N * psi) * y0 + fpsi * sin (N * psi) * alpha0;
  alphaN = -sin (N * psi) / fpsi * y0 + cos (N * psi) * alpha0;

  /*absorption: each lenslet has material thickness d + y_n^2/r at height y_n*/
  double yn2sum = 0;
  double pmul;
  if (!fast) {
    int n;
    for (n = 1; n <= N; n++) {
      double th = (n - 0.5) * psi;
      double yn = y0 * cos (th) + fpsi * alpha0 * sin (th);
      /* set scatter points along the way */
      y = yn;
      z = zdepth * (n - 0.5);
      x = x0 + kx * z / kz;
      SCATTER;
      yn2sum += yn * yn;
    }
    pmul = exp (-N * mu * d) * exp (-mu / r * yn2sum);
  } else {
    /* Closed form of mu/r * sum_{n=1}^N y_n^2 with y_n = y0 cos(th_n) + f*psi*alpha0 sin(th_n), th_n=(n-1/2)psi:
       sum cos^2 = (N + sN)/2, sum sin^2 = (N - sN)/2, sum sin*cos = (1-cos(2N psi))/(4 sin psi)
       with sN = sin(2N psi)/(2 sin psi). */
    double s = sin (psi);
    double sN = sin (2 * N * psi) / (2 * s);
    double AN = mu / r * (f * zdepth) * (N - sN) / 2.0;
    double BN = mu / r * fpsi * (1.0 - cos (2 * N * psi)) / (2 * s);
    double CN = mu / r * (N + sN) / 2.0;
    yn2sum = AN * alpha0 * alpha0 + BN * alpha0 * y0 + CN * y0 * y0;
    pmul = exp (-N * mu * d) * exp (-yn2sum);
  }

  p *= pmul;
  /*figure out deviation along x (invariant direction)*/
  x = x0 + kx * (zdepth * N) / kz;
  if (fabs (x) > xwidth / 2.0) {
    ABSORB;
  }
  y = yN;
  z = zdepth * N;
  SCATTER;
  ky = sin (alphaN) * k_yz;
  kz = cos (alphaN) * k_yz;
%}

FINALLY
%{
  Table_Free (&(matT));
%}

MCDISPLAY
%{
  /* Draw a box around the lenslets*/
  box (0, 0, zdepth * N / 2.0, xwidth, 2 * Y, zdepth * N, 0, 0, 1, 0);
%}

END
