/*******************************************************************************
* Instrument: SOLEIL_ANATOMIX
*
* %Identification
* Written by: J. Perrin and E. Farhi
* Date: 2022-03-23
* Origin: Synchrotron SOLEIL
* %INSTRUMENT_SITE: SOLEIL
*
* The ANATOMIX tomography beam-line at Synchrotron SOLEIL.
*
* %Description
* The beamline ANATOMIX (Advanced Nanotomography and Imaging with coherent X rays)
* works at photon energies between 5 and 50 keV. It is dedicated to full-field
* radiography and tomography in absorption and phase contrast, with pixel sizes
* from 20 nm to 20 µm.
*
* ANATOMIX is a beamline for X-ray tomography on the micro- and nanoscale, in
* absorption and phase contrast. It operates in the energy range from 5 keV upward
* and allows its users to obtain two- and three-dimensional radiographic images of
* bulk volume samples of macroscopic size (up to several cm thickness). For
* smaller samples, a spatial resolution down to 50 nm (20 nm pixel size) can be
* achieved. Real-time studies are possible at speeds of currently up to one
* microtomography scan per second; higher speeds up to 20 volume scans per second
* (50 ms per scan) have been demonstrated.
*
* A flexible sample interface enables in situ and/or operando studies under
* conditions similar to the natural or working environment of the samples
* (temperature, humidity, mechanical load, transport processes). Biological
* samples can be measured without dehydration and, in many cases, without chemical
* fixation. With suitable sample preparation, cellular imaging without cryogenic
* environment is possible.
*
* This is a simplified model with an Undulator, a double channel-cut monochromator,
* a sample stage and a detector. It models a parallel beam full field tomograph.
* The monochromator is an Si(111), the sample is made of Ge. The sample is 2 cm
* wide.
*
* List of important beamline elements. Distances are given from the center of the undulator.
*
* Element | Distance (m) | Description  |Hutch
* --------|--------------|--------------|-------------------
* Diaphragm       | 22.7 | Rectangular aperture | 2.5 × 2 mm2 (h×v). | OH1
* Vertical slit   | 23.2 | Horizontal aperture 100. . .300 μm, removable. Increases horizontal transverse coherence where needed. |
* Primary slits   | 25.9 | Define footprint on mirror and other optics. |
* Attenuators     | 34.6 | Protect transfocator and other downstream elements. | OH3
* Mirror M1       | 35.3 | Horiz. deflection. Useful length 400 mm. Bender for horiz. focusing (focal length 3.5 m). Coatings: B4C, Rh, Pt. Grazing angle ≈ 3 mrad. |
* Mirror M2       | 36.3 | Horiz. deflection. Plane mirror, steers beam back into direction parallel to direct beam, offset 5 mm. Same length, angle and coatings as M1. |
* Transfocator    | 37.7 | Beryllium lenses. |
* Vertical slit   | 38.8 | Defines horizontal secondary source when mirror is used. |
* Secondary slits | 48.4 | Define footprint on monochromators. | OH4
* Attenuators     | 48.8 | Filter out low energies, especially when using DMM. |
* DMM             | 49.8 | Under design. Vertical deflection, beam offset 20 mm. Tentative specs: two strips Ru/B4C, Ir/B4C, d = 2.4 nm. 400-mm substrates. |
* DCM             | 52.7 | Vertical deflection, beam offset 20 mm. Si(111), LN2-cooled. |
* EH3             | 165.7-174.1 | Guard slits; TXM sample stage and zone-plate table;  microtomography sample stage; detector table | EH3
* EH4             | 197.2-208.5 | Guard slits; microtomography sample stage; detector table; X-ray grating interferometer | EH4
*
* %Example: -n 1e5 E0=17 Detector: psd_monitor_after_cc_I=8.7e+17
*
* %Parameters
* E0:    [keV] Energy selected at the Undulator.
* Emono: [keV] Energy selected at the monochromator. When 0, it is set to E0.
* dE:    [keV] Energy spread at the Undulator.
* ANGLE: [deg] Rotation angle of the sample stage.
* sample:[str] Sample geometry file, OFF/PLY format.
* 
* %Link
* https://www.synchrotron-soleil.fr/en/beamlines/anatomix
* %Link
* T Weitkamp et al 2017 J. Phys.: Conf. Ser. 849 012037 DOI: 10.1088/1742-6596/849/1/012037
*
* %E
*******************************************************************************/
DEFINE INSTRUMENT SOLEIL_ANATOMIX(E0=11, Emono=0, dE=1, ANGLE=0, string sample="wire.ply")

DECLARE
%{
    double calculated_angle;
%}

INITIALIZE
%{
    if (Emono==0) Emono=E0;
    
    if (E0-dE < Emono && Emono < E0+dE) {
    
      double arg=12398.42*sqrt(3)/(2*5.4309*Emono*1e3);
      if (fabs(arg) > 1)
        exit(printf("%s: ERROR: Monochromator can not reach this energy.\n", NAME_INSTRUMENT));
      calculated_angle = RAD2DEG*asin(arg);
    } else {
      exit(printf("%s: ERROR: Monochromator energy is outside the Undulator bandwidth.\n", NAME_INSTRUMENT));
    }
%}

TRACE

COMPONENT origin = Progress_bar()
AT (0, 0, 0) RELATIVE ABSOLUTE

// Undulator U18
COMPONENT source=Undulator(
 E0=E0, dE=dE, Ee=2.75, dEe=0.001, Ie=0.5, 
 K=1.03118, Nper=140, lu=3.2e-2, 
 sigex=267.9e-6, sigey=8.4e-6, sigepx=30.1e-6, sigepy=3.9e-6, 
 dist=50, E1st=12.400)
AT (0, 0, 0) RELATIVE PREVIOUS

COMPONENT e_repartition_before_cc = Monitor_nD(
    xwidth=0.01, 
    yheight=0.01, 
    restore_xray=1, 
    options="auto energy, y", bins=100,
    filename="e_repartition_before_cc")
AT (0, 0, 50) RELATIVE PREVIOUS

COMPONENT psd_monitor_before_cc = PSD_monitor(
    filename="psd_monitor_before_cc.dat", 
    xwidth=0.01, 
    yheight=0.01)
AT (0, 0, 0) RELATIVE PREVIOUS

// double channel-cut monochromator at about 50 m from undulator
COMPONENT bragg_crystal = Bragg_crystal(
    crystal_type=2)
AT (0, 0, 0.1) RELATIVE PREVIOUS
ROTATED (-calculated_angle, 0, 0) RELATIVE PREVIOUS

COMPONENT arm = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (-calculated_angle, 0, 0) RELATIVE PREVIOUS

COMPONENT bragg_crystal_two = Bragg_crystal(
    crystal_type=2)
AT (0, 0.01, calculated_angle ? 0.01/tan(calculated_angle*DEG2RAD) : 0) RELATIVE bragg_crystal
ROTATED (calculated_angle, 0, 0) RELATIVE PREVIOUS

COMPONENT arm_two = Arm()
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (calculated_angle, 0, 0) RELATIVE PREVIOUS


COMPONENT psd_after_cc = PSD_monitor(
  xwidth =0.1 , yheight=0.1 ,
  filename="psd_after_cc2",restore_xray = 1) 
AT (0,0,1) RELATIVE PREVIOUS

COMPONENT e_monitor_before_sample = E_monitor(
    filename="e_monitor_before_sample.dat", 
    xwidth=1, 
    yheight=1, 
    Emin=E0-dE, 
    Emax=E0+dE, 
    nE=500,
    restore_xray=1)
AT (0, 0, 1) RELATIVE PREVIOUS

COMPONENT psd_repartition_after_cc = PSD_monitor(
  xwidth =.1 , yheight=.1 ,
  filename="psd_repartition_after_cc",restore_xray = 1) 
AT (0,0,0) RELATIVE PREVIOUS

COMPONENT sample_rotation_stage = Arm()
AT (0, 0, 150) RELATIVE PREVIOUS

// sample at about 200 m from Undulator
COMPONENT sample = Filter(material_datafile="Ge.txt",
    geometry=sample,xwidth=0.02,yheight=0,zdepth=0)
AT (0, 0, 0) RELATIVE PREVIOUS
ROTATED (0,ANGLE,0) RELATIVE PREVIOUS

COMPONENT e_monitor_after_sample = E_monitor(
    filename="e_monitor_after_sample.dat", 
    xwidth=1, 
    yheight=1, 
    Emin=E0-E0*0.1, 
    Emax=E0+E0*0.1, 
    nE=500,
    restore_xray=1)
AT (0, 0, 0.5) RELATIVE sample_rotation_stage

COMPONENT psd_monitor_after_cc = PSD_monitor(
    filename="psd_monitor_after_sample.dat", nx=512, ny=512,
    xwidth=.02, 
    yheight=.02)
AT (0, 0, 0) RELATIVE PREVIOUS


END

