emout.utils package¶
Submodules¶
emout.utils.eflux module¶
Energy-flux computation and pitch-angle classification library. Energies are handled in eV.
- emout.utils.eflux.compute_energy_flux_histogram(velocities, probs, mass, energy_bins)[source]¶
Compute an energy-flux histogram.
- Parameters:
velocities (np.ndarray) – Particle velocity array.
probs (np.ndarray) – Weight (probability / contribution) array for each particle.
mass (float) – Particle mass [kg].
energy_bins (Union[int, np.ndarray]) – Histogram bin specification. An integer sets the number of bins; an array sets the bin edges explicitly.
- Returns:
Tuple of
(hist, bin_edges).- Return type:
- emout.utils.eflux.compute_energy_flux_histograms(velocities, probs, B, mass, energy_bins, pitch_ranges=None)[source]¶
Compute energy x energy-flux histograms for user-specified pitch-angle ranges and directions, from velocity vectors and a probability array. Energies are computed in eV.
- Parameters:
velocities (np.ndarray) – Velocity vectors for each particle (m/s), shaped
(N, 3). N is the number of samples.probs (np.ndarray) – Probability or weight corresponding to each velocity vector, shaped
(N,).B (np.ndarray) – Magnetic field vector (T) or direction vector, shaped
(3,).mass (float) – Particle mass (kg).
energy_bins (int or np.ndarray) –
int: let numpy.histogram auto-generate bin edges.
np.ndarray: use these bin edges directly, shaped
(M+1,).
pitch_ranges (list of (a_deg, b_deg, direction) | None) – List specifying pitch-angle ranges and directions. Each tuple has the form
(a_deg, b_deg, direction)where direction is one of'both','pos','neg'. IfNone, the default 6-class decomposition is used.
- Returns:
histograms – Keys have the format
f"{a_deg:02d}-{b_deg:02d}_{direction}". Values are(hist, bin_edges)tuples where: - hist: total energy flux (eV x v x prob) per bin, shaped(M,)- bin_edges: bin boundaries in eV, shaped(M+1,)- Return type:
- emout.utils.eflux.get_indices_in_pitch_range(velocities, B, a_deg, b_deg, direction='both')[source]¶
Return the indices of particles whose pitch angle falls within [a_deg, b_deg].
- Parameters:
velocities (np.ndarray) – Velocity vectors for each particle (m/s), shaped
(N, 3). N is the number of samples.B (np.ndarray) – Magnetic field vector (T) or direction vector, shaped
(3,). Must be non-zero.a_deg (float) – Lower bound of the pitch angle (degrees). 0 <= a_deg < b_deg <= 180.
b_deg (float) – Upper bound of the pitch angle (degrees).
direction (str, default='both') –
- Sign direction of the pitch angle:
’both’: all particles regardless of the sign of v dot B
’pos’ : only particles with v dot B > 0 (parallel to B)
’neg’ : only particles with v dot B < 0 (anti-parallel to B)
- Returns:
idx – Index array of particles satisfying the angle range and direction condition.
- Return type:
np.ndarray
- emout.utils.eflux.plot_energy_flux(velocities, probs, B, mass, energy_bins, pitch_ranges=None, cmap='plasma')[source]¶
Plot the energy x energy-flux distribution for all particles and for each specified pitch-angle range, overlaid on a single axis. Energies are computed in eV.
- Parameters:
velocities (np.ndarray) – Velocity vectors for each particle (m/s), shaped
(N, 3). N is the number of samples.probs (np.ndarray) – Probability or weight corresponding to each velocity vector, shaped
(N,).B (np.ndarray) – Magnetic field vector (T) or direction vector, shaped
(3,).mass (float) – Particle mass (kg).
energy_bins (int or np.ndarray) –
int: let numpy.histogram auto-generate bin edges.
np.ndarray: use these bin edges directly, shaped
(M+1,).
pitch_ranges (list of (a_deg, b_deg, direction) | None) – Pitch-angle ranges and direction specifier list. None uses the default 6-class decomposition.
cmap (str, default='plasma') – Matplotlib colourmap name.
- Returns:
fig, ax – The created Figure and Axes.
- Return type:
- emout.utils.eflux.plot_energy_fluxes(velocities_list, x, mass, energy_bins, use_probs=False, probs_list=None, cmap='viridis')[source]¶
Plot a 2-D heatmap (x vs Energy, colour scale = energy flux) from velocity vector lists across multiple series. The energy flux for each series is histogrammed as the sum of eV x v x (prob).
- Parameters:
velocities_list (list[np.ndarray]) – List of length T, each element a velocity array of shape (NT, 3).
x (np.ndarray) – x-axis values corresponding to each velocity list, shaped
(T,).mass (float) – Particle mass (kg).
energy_bins (int or np.ndarray) –
int: let numpy.histogram auto-generate bin edges across all series.
np.ndarray: use these bin edges directly, shaped
(M+1,).
use_probs (bool, default=False) – If True, read per-series probabilities from probs_list and include them in the energy-flux weights. If False, assume probs = np.ones(NT) (weight = eV x v only).
probs_list (list[np.ndarray] | None, default=None) – List of length T, each element a probability array of shape (NT,). Required when use_probs=True.
cmap (str, default='viridis') – Matplotlib colourmap name.
- Returns:
fig, ax – The created Figure and Axes.
- Return type:
emout.utils.emsesinp module¶
Parser for EMSES plasma.inp namelist files.
InpFile reads Fortran namelist parameters via f90nml and
exposes them as attributes, handling sparse-array indexing and the
!!key unit-conversion header.
- class emout.utils.emsesinp.AttrDict(*args, **kwargs)[source]¶
Bases:
dictDictionary subclass that supports attribute-style access.
- class emout.utils.emsesinp.InpFile(filename=None, convkey=None)[source]¶
Bases:
objectManage an EMSES parameter file.
- nmlf90nml.Namelist
Namelist object.
- property dx: float¶
Return the conversion key
dx.- Returns:
Grid width from the conversion key.
- Return type:
- save(filename, convkey=None)[source]¶
Save the parameters to a file.
- Parameters:
filename (str or Path) – Output file path.
convkey (UnitConversionKey, optional) – Unit conversion key to write, by default None.
- class emout.utils.emsesinp.UnitConversionKey(dx, to_c)[source]¶
Bases:
objectUnit conversion key.
- property keytext¶
Return the string representation of the conversion key.
- Returns:
String representation of the conversion key.
- Return type:
- classmethod load(filename)[source]¶
Load a unit conversion key from a file.
Read
dxandto_cfrom the first line of the file if it matches the format!!key dx=[1.0],to_c=[10000.0].- Parameters:
filename (str or Path) – File containing the unit conversion key.
- Returns:
Unit conversion key, or
Noneif the header is absent.- Return type:
UnitConversionKey or None
emout.utils.group module¶
Generic grouping container with element-wise attribute delegation.
- class emout.utils.group.Group(objs, attrs=None)[source]¶
Bases:
objectContainer that applies operations element-wise to a list of objects.
emout.utils.poisson module¶
Poisson-equation solver with configurable boundary conditions.
Provides the poisson() entry point and boundary-condition classes
(PeriodicPoissonBoundary, DirichletPoissonBoundary,
NeumannPoissonBoundary) for 1-D / 2-D / 3-D grids.
- class emout.utils.poisson.DirichletPoissonBoundary(axis, boundary_values=(0.0, 0.0))[source]¶
Bases:
PoissonBoundaryFixed-value (Dirichlet) boundary condition for the Poisson solver.
- correct_boundary_values(phi)[source]¶
Correct boundary values by assigning the fixed Dirichlet values.
- Parameters:
phi (np.ndarray) – Potential array.
- Returns:
No return value.
- Return type:
None
- property fft_backward: Callable[[ndarray], ndarray]¶
Return the backward FFT function for Dirichlet boundaries.
- Returns:
Backward IDST callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- property fft_forward: Callable[[ndarray], ndarray]¶
Return the forward FFT function for Dirichlet boundaries.
- Returns:
Forward DST callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- get_target_slice()[source]¶
Return the target slice for Dirichlet boundaries.
- Returns:
Slice excluding both boundary points.
- Return type:
- class emout.utils.poisson.FFT3d(forwards, backwards)[source]¶
Bases:
objectComposite 3-D FFT that applies per-axis forward/backward transforms.
- class emout.utils.poisson.NeumannPoissonBoundary(axis, boundary_values=(0.0, 0.0))[source]¶
Bases:
PoissonBoundaryZero-gradient (Neumann) boundary condition for the Poisson solver.
- correct_boundary_values(phi)[source]¶
Correct boundary values for Neumann conditions (no-op).
- Parameters:
phi (np.ndarray) – Potential array.
- Returns:
No return value.
- Return type:
None
- property fft_backward: Callable[[ndarray], ndarray]¶
Return the backward FFT function for Neumann boundaries.
- Returns:
Backward IDCT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- property fft_forward: Callable[[ndarray], ndarray]¶
Return the forward FFT function for Neumann boundaries.
- Returns:
Forward DCT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- get_target_slice()[source]¶
Return the target slice for Neumann boundaries.
- Returns:
Slice including all points.
- Return type:
- class emout.utils.poisson.PeriodicPoissonBoundary(axis, boundary_values=(0.0, 0.0))[source]¶
Bases:
PoissonBoundaryPeriodic boundary condition for the Poisson solver.
- correct_boundary_values(phi)[source]¶
Correct boundary values by copying the periodic duplicate.
- Parameters:
phi (np.ndarray) – Potential array.
- Returns:
No return value.
- Return type:
None
- property fft_backward: Callable[[ndarray], ndarray]¶
Return the backward FFT function for periodic boundaries.
- Returns:
Backward FFT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- property fft_forward: Callable[[ndarray], ndarray]¶
Return the forward FFT function for periodic boundaries.
- Returns:
Forward FFT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- get_target_slice()[source]¶
Return the target slice for periodic boundaries.
- Returns:
Slice excluding the last (periodic duplicate) point.
- Return type:
- class emout.utils.poisson.PoissonBoundary(axis, boundary_values=(0.0, 0.0))[source]¶
Bases:
objectAbstract base for Poisson boundary conditions.
- property boundary_values: Tuple[float]¶
Return the boundary values
(lower, upper).- Returns:
Boundary value pair.
- Return type:
Tuple[float]
- abstractmethod correct_boundary_values(phi)[source]¶
Correct boundary values in the potential array.
- Parameters:
phi (np.ndarray) – Potential array.
- Returns:
No return value.
- Return type:
None
- abstract property fft_backward: Callable[[ndarray], ndarray]¶
Return the backward FFT function for this boundary condition.
- Returns:
Backward FFT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- abstract property fft_forward: Callable[[ndarray], ndarray]¶
Return the forward FFT function for this boundary condition.
- Returns:
Forward FFT callable.
- Return type:
Callable[[np.ndarray], np.ndarray]
- abstractmethod get_target_slice()[source]¶
Return the slice selecting the interior grid points for this axis.
- Returns:
Slice corresponding to the target axis.
- Return type:
- emout.utils.poisson.poisson(rho, dx, boundary_types=None, boundary_values=None, btypes=None, epsilon_0=8.8541878188e-12)[source]¶
Solve Poisson’s equation with FFT.
- Parameters:
rho (np.ndarray) – 3-dimentional array of the charge density [C/m^3]. The shape is (nz+1, ny+1, nx+1).
boundary_types (List[str] of {'periodic', 'dirichlet', 'neumann'},) – the boundary condition types, by default [‘periodic’, ‘periodic’, ‘periodic’]
boundary_values (List[Tuple[float]]) – the boundary values [(x-lower, x-upper), (y-lower, y-upper), (z-lower, z-upper)], by default [(0., 0.), (0., 0.), (0., 0.)]
btypes (str) – string consisting of prefixes of boundary conditions, by default None. If this is set, it takes precedence over boundary_types.
dx (float, optional) – the grid width [m], by default 1.0
epsilon_0 (_type_, optional) – the electric constant (vacuum permittivity) [F/m], by default cn.epsilon_0
- Returns:
3-dimentional of the potential [V].
- Return type:
np.ndarray
emout.utils.units module¶
Physical unit translators and conversion system for EMSES quantities.
UnitTranslator converts between EMSES grid units and SI, while
Units bundles translators for all standard field quantities
derived from the simulation parameters.
- class emout.utils.units.UnitTranslator(from_unit, to_unit, name=None, unit=None)[source]¶
Bases:
objectUnit converter between two unit systems.
- set_name(name, unit=None)[source]¶
Set the display name and unit symbol.
- Parameters:
- Returns:
self
- Return type:
- class emout.utils.units.Units(dx, to_c)[source]¶
Bases:
objectManage unit converters for EMSES quantities.
Convert between SI and EMSES unit systems.
- B¶
Unit translator for Magnetic flux density [T]
- C¶
Unit translator for Capacitance [F]
- E¶
Unit translator for Electric field [V/m]
- EC¶
Unit translator for Electric conductivity [S/m]
- F¶
Unit translator for Force [N]
- G¶
Unit translator for Conductance [S]
- H¶
Unit translator for Magnetic field [A/m]
- J¶
Unit translator for Current density [A/m^2]
- L¶
Unit translator for Inductance [H]
- N¶
Unit translator for Flux [/m^2s]
- P¶
Unit translator for Power [W]
- R¶
Unit translator for Resistance [Ω]
- T¶
Unit translator for Temperature [K]
- W¶
Unit translator for Energy [J]
- a¶
Unit translator for Acceleration [m/s^2]
- c¶
Unit translator for Light Speed [m/s]
- dx¶
Grid length [m]
- e¶
Unit translator for Napiers constant []
- e0¶
Unit translator for FS-Permttivity [F/m]
- eps¶
Unit translator for Permittivity [F/m]
- f¶
Unit translator for Frequency [Hz]
- i¶
Unit translator for Current [A]
- kB¶
Unit translator for Boltzmann constant [J/K]
- length¶
Unit translator for Sim-to-Real length ratio [m]
- m¶
Unit translator for Mass [kg]
- m0¶
Unit translator for FS-Permeablity [N/A^2]
- me¶
Unit translator for Electron mass [kg]
- mi¶
Unit translator for Proton mass [kg]
- mu¶
Unit translator for Permiability [H/m]
- n¶
Unit translator for Number density [/m^3]
- phi¶
Unit translator for Potential [V]
- pi¶
Unit translator for Circular constant []
- q¶
Unit translator for Charge [C]
- q_m¶
Unit translator for Charge-to-mass ratio [C/kg]
- qe¶
Unit translator for Elementary charge [C]
- qe_me¶
Unit translator for Electron charge-to-mass ratio [C/kg]
- rho¶
Unit translator for Charge density [C/m^3]
- t¶
Unit translator for Time [s]
- to_c¶
Light speed in EMSES
- v¶
Unit translator for Velocity [m/s]
- w¶
Unit translator for Energy density [J/m^3]
emout.utils.util module¶
Miscellaneous helpers: regex dict, file-info parsing, interpolation, and slicing.
- class emout.utils.util.DataFileInfo(filename)[source]¶
Bases:
objectManage metadata about a data file.
- property abspath¶
Return the absolute path of the file.
- Returns:
Absolute file path, or
Noneif no filename was set.- Return type:
Path or None
- property directory¶
Return the absolute path of the parent directory.
- Returns:
Absolute directory path.
- Return type:
Path
- property filename¶
Return the file name.
- Returns:
File name.
- Return type:
Path
- class emout.utils.util.QuantizedPillowWriter(fps=5, metadata=None, codec=None, bitrate=None)[source]¶
Bases:
PillowWriterPillowWriter wrapper that quantises each frame to 256 colours.
- class emout.utils.util.RegexDict[source]¶
Bases:
dictDictionary that supports regular expression keys.
- emout.utils.util.apply_offset(line, offset)[source]¶
Apply a positional offset to a coordinate array.
- Parameters:
line (numpy.ndarray) – Coordinate values to shift.
offset (float or str) –
"left"sets the first element to 0,"center"centres on the middle element,"right"sets the last element to 0. A numeric value is added directly.
- Returns:
Shifted array (modified in-place when possible).
- Return type:
- emout.utils.util.hole_mask(inp, reverse=False)[source]¶
Generate a boolean mask for a rectangular hole region.
- emout.utils.util.interp2d(mesh, n, **kwargs)[source]¶
Perform bilinear interpolation on a 2-D array.
Module contents¶
Shared utilities: input-file parsing, unit conversion, and helpers.