shadow4.sources.wiggler package

Submodules

shadow4.sources.wiggler.s4_wiggler module

S4 Wiggler magnetic structure.

class shadow4.sources.wiggler.s4_wiggler.S4Wiggler(magnetic_field_periodic=1, file_with_magnetic_field='', K_vertical=10.0, period_length=0.1, number_of_periods=10, emin=1000.0, emax=2000.0, ng_e=11, ng_j=20, psi_interval_number_of_points=101, flag_interpolation=0, flag_emittance=0, shift_x_flag=0, shift_x_value=0.0, shift_betax_flag=0, shift_betax_value=0.0)[source]

Bases: Wiggler

Defines a shadow4 wiggler magnetic structure.

Parameters:
  • magnetic_field_periodic (int, optional) – Flag: 0=use external magnetic field, 1=periodic magnetic field.

  • file_with_magnetic_field (str, optional) – for magnetic_field_periodic=0, the file name with the magnetic field (two columns, Y in m and B in T).

  • K_vertical (float, optional) – for magnetic_field_periodic=1, the K value.

  • period_length (float, optional) – for magnetic_field_periodic=1, the period in m.

  • number_of_periods (float, optional) – for magnetic_field_periodic=1, the number of periods.

  • emin (float, optional) – minimum photon energy in eV.

  • emax (float, optional) – maximum photon energy in eV.

  • ng_e (int, optional) – Number of points in energy. This is used for the calculation of the spectrum, and also for sampling rays if flag_interpolation=1.

  • ng_j (int, optional) – Number of points in calculating the electron trajectory.

  • psi_interval_number_of_points (int, optional) – The number of psi (vertical angle) points for internal calculation only.

  • flag_interpolation (int, optional) – Use interpolation mechanism for sampling vertical divergence of rays. 0=No, 1=Yes, 2=Yes and create debugging plots for each ray. Typically the interpolation is faster, and works very well for monochromatic or quasi-monochromatic cases. Here the ng_e and psi_interval_number_of_points can be small (e.g. ~50). In the case of creating polychromatic sources with a very large energy interval, the interpolation method tends to overestimate the tails of the vertical angle. In this case, the number of points should be increased. However, with large number of points, the calculation time is very large, similar to the more accurate option flag_interpolaton=0.

  • flag_emittance (int, optional) –

    Flag:
    • 0=Zero emittance (filament beam)

    • 1=Use emittance

    • 2=Use emittance, but do not consider natural emission (for testing the electron phase space).

  • shift_x_flag (int, optional) – Flag to deplace the X of the electron trajectory trajectory.

  • shift_x_value (float, optional) – A displacement in X to center or deplace the electron trajectory trajectory.

  • shift_betax_flag (float, optional) – Flag to deplace the X’ of the electron trajectory trajectory.

  • shift_betax_value (float, optional) – A displacement in X’ to center or deplace the electron trajectory trajectory.

get_energy_box()[source]

Returns the limits of photon energy distribution for the source and the number of points.

Returns:

(emin, emax, npoints)

Return type:

tuple

get_flag_emittance()[source]

Returns the flag for considering electron beam emittance.

Returns:

0=No, 1=Yes.

Return type:

int

get_info()[source]

Returns the specific information of the wiggler magnetic structure.

Return type:

str

is_monochromatic()[source]

Returns a flag indicating if the source is monochromatic (True) or polychromatic (False).

Return type:

boolean

set_electron_initial_conditions(shift_x_flag=0, shift_x_value=0.0, shift_betax_flag=0, shift_betax_value=0.0)[source]

Defines the shifts in position and velocity of the electron trajectory.

Parameters:
  • shift_x_flag (int, optional) – flag values are 0:’no_shift’, 1:’half_excursion’, 2:’minimum’, 3:’maximum’, 4:’value_at_zero’, 5:’user_value’.

  • shift_betax_flag (int, optional) – flag values are 0:’no_shift’, 1:’half_excursion’, 2:’minimum’, 3:’maximum’, 4:’value_at_zero’, 5:’user_value’.

  • shift_x_value (float, optional) – For shift_x_flag=5, the numeric value to consider.

  • shift_betax_value (float, optional) – For shift_betax_flag=5, the numeric value to consider.

set_electron_initial_conditions_by_label(position_label='no_shift', velocity_label='no_shift', position_value=0.0, velocity_value=0.0)[source]

Defines the shifts in position and velocity of the electron trajectory.

Parameters:
  • position_label (str, optional) – keyworrd values are ‘no_shift’, ‘half_excursion’, ‘minimum’, ‘maximum’, ‘value_at_zero’, ‘user_value’.

  • velocity_label (str, optional) – keyworrd values are ‘no_shift’, ‘half_excursion’, ‘minimum’, ‘maximum’, ‘value_at_zero’, ‘user_value’.

  • position_value (float, optional) – For position_label equal to user_value, the numeric value to consider.

  • velocity_value (float, optional) – For velocity_label equal to user_value, the numeric value to consider.

set_energy_box(emin, emax, npoints=None)[source]

Sets a box for photon energy distribution for the source.

Parameters:
  • emin (float) – minimum photon energy in eV.

  • emax (float) – maximum photon energy in eV.

  • npoints (int, optional) – Number of points in energy.

set_energy_monochromatic(emin)[source]

Sets a single energy line for the source (monochromatic).

Parameters:

emin (float) – photon energy in eV.

to_python_code()[source]

Returns the python code for defining the wiggler magnetic structure.

Returns:

The python code.

Return type:

str

shadow4.sources.wiggler.s4_wiggler_light_source module

Wiggler light source.

class shadow4.sources.wiggler.s4_wiggler_light_source.S4WigglerLightSource(name='Undefined', electron_beam=None, magnetic_structure=None, nrays=5000, seed=12345)[source]

Bases: S4LightSource

Defines a wiggler light source and implements the mechanism of sampling rays.

Parameters:
  • name (str, optional) – The name of the light source.

  • electron_beam (instance of ElectronBeam) – The electron beam parameters.

  • magnetic_structure (instance of S4BendingMagnet) – The shadow4 bending magnet magnetic structure.

  • nrays (int, optional) – The number of rays.

  • seed (int, optional) – The Monte Carlo seed.

calculate_spectrum(output_file='')[source]

Calculates the spectrum.

Parameters:

output_file (str, optional) – Name of the file to write the spectrom (use “” for not writing file).

Returns:

(e, f, w) numpy arrays with photon energy (e), photon flux (f) and spectral power (w).

Return type:

tuple

get_beam(F_COHER=0, psi_interval_in_units_one_over_gamma=None)[source]

Creates the beam as emitted by the wiggler.

Parameters:
  • F_COHER (int, optional) – A flag to indicate that the phase for the s-component is set to zero (coherent_beam=1) or is random for incoherent.

  • psi_interval_in_units_one_over_gamma (None or float, optional) – The interval of psi*gamma for sampling rays.

Return type:

instance of S4Beam

get_info()[source]

Returns the specific information for the wiggler light source.

Return type:

str

get_trajectory()[source]

Returns the electron trajectory.

Returns:

(trajectory, parameters) with trajectory a numpy array (8, npoints) and parameters a str.

Return type:

tuple

to_python_code(**kwargs)[source]

Returns the python code for calculating the wiggler source.

Returns:

The python code.

Return type:

str

class shadow4.sources.wiggler.s4_wiggler_light_source.Sampler1Dcdf(cdf, pdf_x=None)[source]

Bases: object

Constructor.

Parameters:
  • pdf (numpy array) – 1D input probability distrubution function.

  • pdf_x (numpy array) – the abscissas of the odf.

  • cdf_interpolation_factor (float, optional) – interpolation factor for calculating the cdf (1 makes no interpolation)/

abscissas()[source]

Gets the abscissas array.

Returns:

The abscissas array (referenced, not copied).

Return type:

numpy array

cdf()[source]

Gets the cumulative distribution function (cdf).

Returns:

The cdf (referenced, not copied).

Return type:

numpy array

cdf_abscissas()[source]

Gets the abscissas of the cumulative distribution function (cdf).

Returns:

The cdf abscissas (referenced, not copied).

Return type:

numpy array

get_n_sampled_points(npoints, seed=None)[source]

Returns a given number points sampled points sampled with the pdf.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

Returns:

The sampled points.

Return type:

numpy array

get_n_sampled_points_and_histogram(npoints, bins=51, range=None, seed=None)[source]

Returns a given number points sampled points sampled with the pdf and the histogram.

Parameters:
  • npoints (int) – The number of points.

  • seed (int, optional) – The seed (numpy generator is initialized with numpy.random.default_rng(seed))

  • bins (int, optional) – Number of bins

  • range (list or tuple) – [min, max] the histogram limits.

Returns:

(s1, h, bin_edges) s1: the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1, h: the array with the histogram values at the bin edges, bin_edges: the bin edges.

Return type:

tuple

get_sampled(random_in_0_1)[source]

Return an array with sampled points.

Parameters:

random_in_0_1 (float or numpy array) – Points sampled in a uniform interval.

Returns:

the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1.

Return type:

numpy array

get_sampled_and_histogram(random_in_0_1, bins=51, range=None)[source]

Return an array with sampled points and the histogram.

Parameters:
  • random_in_0_1 (float or numpy array) – Points sampled in a uniform interval.

  • bins (int, optional) – Number of bins

  • range (list or tuple) – [min, max] the histogram limits.

Returns:

(s1, h, bin_edges) s1: the points sampled with the current pdf. The number of points is equal to the dimension of random_in_0_1, h: the array with the histogram values at the bin edges, bin_edges: the bin edges.

Return type:

tuple

shadow4.sources.wiggler.s4_wiggler_optimized_light_source module

class shadow4.sources.wiggler.s4_wiggler_optimized_light_source.S4WigglerOptimizedLightSource(name='Undefined', electron_beam=None, magnetic_structure=None, nrays=5000, seed=12345, optim_method=0, optim_slit_d=1.0, optim_slit_center_x=0.0, optim_slit_center_z=0.0, optim_slit_gap_x=1.0, optim_slit_gap_z=1.0, optim_max_iterations=10, optim_beamline_element=None)[source]

Bases: S4WigglerLightSource

Defines an “optimized” wiggler light source. “Optimized” means that a reject mechanist is in place to avoid storing the rays that will not go through a slit.

Parameters:
  • name (str, optional) – The name of the light source.

  • electron_beam (instance of ElectronBeam) – The electron beam parameters.

  • magnetic_structure (instance of S4BendingMagnet) – The shadow4 bending magnet magnetic structure.

  • nrays (int, optional) – The number of rays.

  • seed (int, optional) – The Monte Carlo seed.

  • optim_method (int, optional) – 0=No optimization, 1=Optimization by defined slit, 2=Optimization by entered S4BeamlineElement.

  • optim_max_iterations (int, optional) – The maximum number of iterations (source creation).

  • optim_slit_distance (float, optional) – The position of the slit in m (for optim_method=1).

  • optim_slit_center_x (float, optional) – The X coordinate of the slit center in m (for optim_method=1).

  • optim_slit_center_z (float, optional) – The Z coordinate of the slit center in m (for optim_method=1).

  • optim_slit_gap_x (float, optional) – The slit gap in X direction, in m (for optim_method=1).

  • optim_slit_gap_z (float, optional) – The slit gap in Z direction, in m (for optim_method=1).

  • optim_beamline_element (None or instance of S4BeamlineElement, optional) – For optim_method=2, the beamline element.

get_beam(F_COHER=0, psi_interval_in_units_one_over_gamma=None)[source]

Creates the beam as emitted by the wiggler.

Parameters:
  • F_COHER (int, optional) – A flag to indicate that the phase for the s-component is set to zero (coherent_beam=1) or is random for incoherent.

  • psi_interval_in_units_one_over_gamma (None or float, optional) – The interval of psi*gamma for sampling rays.

Return type:

instance of S4Beam

to_python_code(**kwargs)[source]

Returns the python code for calculating the wiggler source.

Returns:

The python code.

Return type:

str

Module contents