pnps.kernels#

Per-element scalar bodies shared by the input former and the save-pass reduction.

The two host kernels this package still owns – forming the delayed three-beamlet input state (pnps.form_input) and reducing a saved plane to the measured spectra (pnps.reduce_numba) – both evaluate the chromatic hole model one element at a time. They need the same mathematics, and need it once, so the bodies live here and are inlined into both callers. The propagation itself is solprop’s (pnps.bridge); nothing here steps a field.

The transverse-momentum plane is the mask plane: a sample (kx, ky) at frequency omega maps to the mask-plane position (kx, ky) * zmask * c / omega, so one physical hole is a different k aperture at every frequency (see pnps.beams). The analytic profiles are transcribed term for term from pnps.beams.chromatic_mask(), which is the NumPy reference they are tested against.

Notes#

The functions are @njit(inline="always") with no signatures, so they specialise on first use and inline into their caller. They take and return scalars only – no arrays, no allocation. inline="always" matters: they are called once per element, so a call that is not inlined would cost more than the arithmetic inside it.

Module Attributes

APOD_CODES

The integer code of each edge profile name.

Functions

admittance(k2, kperp2, omega)

Return the dimensionless axial admittance c k_z / omega of one element.

apod_code(apod)

Return the integer code of an edge profile, for branching inside a kernel.

area_weight(kx_value, ky_value, factor, ...)

Fraction of one k pixel lying inside a hard hole.

hole_weight(kx_value, ky_value, factor, ...)

Transmission of one hole at one element, whichever rendering it uses.

launch_weight(k2, kperp2, omega)

Return the factor that carries one air component into the slab at constant flux.

mask_plane_radius(kx_value, ky_value, ...)

Mask-plane distance of one element from a hole centre, in m.

profile_weight(radius, holediam, apod_code, ...)

Transmission of a radial edge profile at one mask-plane radius.

release_main_thread()

Give the calling thread back the CPUs an OpenMP binding took from it.

size_thread_pool()

Hold numba's thread pool within the process's thread budget.

pnps.kernels.APOD_CODES: dict[str, int] = {'area': 4, 'hard': 0, 'planck': 5, 'rcos': 3, 'supergauss': 1, 'tanh': 2}#

The integer code of each edge profile name.

pnps.kernels.admittance(k2, kperp2, omega)[source]#

Return the dimensionless axial admittance c k_z / omega of one element.

The scalar body of pnps.flux.axial_admittance(): the principal root of k^2 - k_perp^2 times c / omega, zero where the column does not propagate or the frequency is not positive.

Parameters:
k2complex

(n(omega) omega / c)^2 in rad^2/m^2.

kperp2float

kx^2 + ky^2 in rad^2/m^2.

omegafloat

Absolute angular frequency, rad/s.

Returns:
complex

The admittance; its real part weights |E|^2 in every measured energy.

pnps.kernels.apod_code(apod)[source]#

Return the integer code of an edge profile, for branching inside a kernel.

Parameters:
apodstr

One of the names in APOD_CODES.

Returns:
int

The profile’s code.

Raises:
InstrumentError

For an unknown profile name.

Examples

>>> apod_code("rcos")
3
pnps.kernels.area_weight(kx_value, ky_value, factor, delta_kx, delta_ky, holex, holey, holediam, sub)[source]#

Fraction of one k pixel lying inside a hard hole.

The exact hard aperture, antialiased by sub x sub supersampling of the pixel – the unbiased, parameter-free rendering, and what the production input masks use.

Most elements need none of that arithmetic: the pixel maps to a mask-plane patch whose half-diagonal bounds how far any subsample’s radius can lie from the pixel centre’s, so a pixel wholly inside the hole is 1 and a pixel wholly outside is 0. Only pixels straddling the edge are supersampled. The bound is widened by a relative slack of 1e-12 – thousands of ulps more than the rounding of a hypot – so the shortcut is exact: anything that could round either way falls through to the full loop.

Parameters:
kx_value, ky_valuefloat

Pixel-centre momenta, rad/m.

factorfloat

Angle-to-position mapping zmask * c / omega.

delta_kx, delta_kyfloat

Momentum pixel pitch, rad/m.

holex, holeyfloat

Hole centre in the mask plane, m.

holediamfloat

Hole diameter, m.

subint

Supersampling factor per axis (4 is well converged).

Returns:
float

Covered fraction in [0, 1].

pnps.kernels.hole_weight(kx_value, ky_value, factor, delta_kx, delta_ky, holex, holey, holediam, apod_code, apod_param, sub)[source]#

Transmission of one hole at one element, whichever rendering it uses.

Dispatches between the pixel-area rendering and the radial profiles so a kernel can form its input state without knowing which the configuration asked for. The branch is on a loop-invariant integer.

Parameters:
kx_value, ky_valuefloat

Element momenta, rad/m.

factorfloat

Angle-to-position mapping zmask * c / omega.

delta_kx, delta_kyfloat

Momentum pixel pitch, rad/m (used only by the area rendering).

holex, holeyfloat

Hole centre in the mask plane, m.

holediamfloat

Hole diameter, m.

apod_codeint

One of the APOD_* codes.

apod_paramfloat

Profile parameter (ignored by APOD_AREA and APOD_HARD).

subint

Supersampling factor for APOD_AREA (ignored otherwise).

Returns:
float

Transmission in [0, 1].

pnps.kernels.launch_weight(k2, kperp2, omega)[source]#

Return the factor that carries one air component into the slab at constant flux.

The scalar body of pnps.flux.launch_factor(), sqrt(eta_air / eta).

Parameters:
k2complex

The slab’s (n(omega) omega / c)^2 in rad^2/m^2.

kperp2float

kx^2 + ky^2 in rad^2/m^2.

omegafloat

Absolute angular frequency, rad/s.

Returns:
float

The real, non-negative factor; zero where either medium does not carry the component.

pnps.kernels.mask_plane_radius(kx_value, ky_value, factor, holex, holey)[source]#

Mask-plane distance of one element from a hole centre, in m.

Parameters:
kx_value, ky_valuefloat

Transverse momenta of this element, rad/m.

factorfloat

The angle-to-position mapping zmask * c / omega, m/(rad/m). The caller hoists it out of the transverse loops, where it is constant.

holex, holeyfloat

Hole centre in the mask plane, m.

Returns:
float

hypot(kx * factor - holex, ky * factor - holey) in m.

pnps.kernels.profile_weight(radius, holediam, apod_code, apod_param)[source]#

Transmission of a radial edge profile at one mask-plane radius.

The analytic profiles of pnps.beams.chromatic_mask(), transcribed term for term. "rcos" gets the one shortcut worth taking: it has compact support, so outside the transition band the answer is exactly 1 or exactly 0 and the sine need not be evaluated at all – which is most elements.

Parameters:
radiusfloat

Mask-plane distance from the hole centre, m.

holediamfloat

Hole diameter, m.

apod_codeint

One of APOD_HARD, APOD_SUPERGAUSS, APOD_TANH, APOD_RCOS, APOD_PLANCK. APOD_AREA is not a radial profile – see area_weight().

apod_paramfloat

Supergaussian order, or the tanh/raised-cosine/Planck width in m.

Returns:
float

Transmission in [0, 1].

pnps.kernels.release_main_thread()[source]#

Give the calling thread back the CPUs an OpenMP binding took from it.

Importing solprop’s native backend starts its OpenMP runtime, which under OMP_PROC_BIND (with OMP_PLACES) binds the main thread to its first place: one CPU. Every thread the main thread starts afterwards inherits that CPU, numba’s OpenMP pool among them: a run then uses about one core of its allocation, and steps five times slower at the production shape on 16 cores (LupoLab/pnps#7). The native backend records the process’s CPUs before it binds anything (solprop_native.PROCESS_CPUS); when the calling thread now holds a strict subset of them, this restores them and emits a ThreadBindingWarning. On 16 cores of an AMD EPYC 7513, a bound run restored this way took 11.5 s per step, against 10.5 s unbound.

Returns:
bool

Whether the thread’s CPUs were restored. False where there is nothing to undo: the native backend is not loaded, the platform has no CPU affinity, or the thread already holds every CPU the process had.

pnps.kernels.size_thread_pool()[source]#

Hold numba’s thread pool within the process’s thread budget.

The former and the reducer are numba’s parallel kernels, and numba starts as many threads as the processor count it sees, which in a container is commonly every core of the host. solprop’s solprop.backends.thread_budget() is the true budget: the smallest of the CPU affinity, the physical cores, the control groups’ CPU quota and SOLPROP_THREADS. The pool takes the smaller of that and numba’s own ceiling (NUMBA_NUM_THREADS), for the calling thread. It first undoes an OpenMP binding of the calling thread (release_main_thread()), so the pool runs on the process’s CPUs, not on the one CPU the binding left.

Returns:
int

The pool’s size now.

Examples

>>> import numba
>>> size_thread_pool() == numba.get_num_threads()
True