cometspec.rates¶
Rate-matrix construction, level-population solver, g-factors, spectrum synthesis, and line-spread-function builders.
Radiative rate matrix, steady-state solver, g-factors, spectrum synthesis, and line-spread-function helpers.
Routines¶
build_rate_matrix_nbar()– Build the radiative rate matrix from normalized transitions.solve_with_normalization()– Solve the steady-state systemM @ n = 0withsum(n) = 1.solve_with_normalization_fast()– Fast buffer-reusing version ofsolve_with_normalization().g_factors()– Compute per-line and total photon/energy g-factors.g_factors_fast_from_cache()– Fast g-factor evaluation using precomputed arrays.synth_spectrum_from_lines()– Build a synthetic emission spectrum from line g-factors.make_lsf()– Create a line-spread function callable from parameter values.
- cometspec.rates.build_rate_matrix_nbar(lines, *, include_stim_emission=False, verbose=True, A_col='A_ul', upper_id_col='upper_id', lower_id_col='lower_id', g_upper_col='g_upper', g_lower_col='g_lower')[source]¶
Build the radiative rate matrix from normalized transitions.
- Parameters:
lines (
astropy.table.Table) – Transition table with solar incident irradiance quantities attached.include_stim_emission (
bool, optional, defaultFalse) – Include stimulated emission in downward rates.verbose (
bool, optional, defaultTrue) – Print diagnostic information.A_col (
str, optional, default"A_ul") – Einstein A column name.upper_id_col (
str, optional, default"upper_id") – Upper-level ID column name.lower_id_col (
str, optional, default"lower_id") – Lower-level ID column name.g_upper_col (
str, optional, default"g_upper") – Upper-level degeneracy column name.g_lower_col (
str, optional, default"g_lower") – Lower-level degeneracy column name.
- Returns:
tuple[numpy.ndarray,dict[int,str],astropy.table.Table]– Rate matrix, a dictionary that maps each row/column index ofMback to its physical level identifier (the string fromupper_id_col/lower_id_col), and the input line lists with new columns (__nu_Hz, __R_lu, __R_ul, __upper_idx, __lower_idx).
- cometspec.rates.solve_with_normalization(M, *, verbose=True)[source]¶
Solve the steady-state system
Mn = 0withsum(n) = 1.- Parameters:
M (
numpy.ndarray) – Rate matrix.verbose (
bool, optional, defaultTrue) – Print solver diagnostics.
- Returns:
numpy.ndarray– Normalized non-negative level populations.- Raises:
RuntimeError – If no positive normalized solution can be formed.
Notes
If
np.linalg.lstsqfails to converge (e.g. because M contains NaN or Inf from extreme parameter values), aLinAlgErroris caught and an array of NaN is returned. In the MCMC context this propagates to a non-finite model flux so the walker step is rejected (log-likelihood → -inf) rather than crashing. In the direct-modelling contextbest_modelwill contain NaN, signalling that the chosen parameters are unphysical.
- cometspec.rates.solve_with_normalization_fast(M, A_work, b_work)[source]¶
Fast buffer-reusing version of
solve_with_normalization().- Parameters:
M (
numpy.ndarray) – Rate matrix.A_work (
numpy.ndarray) – Reusable matrix buffer.b_work (
numpy.ndarray) – Reusable right-hand-side buffer.
- Returns:
numpy.ndarray– Normalized non-negative level populations.- Raises:
RuntimeError – If no positive normalized solution can be formed.
Notes
The first row of
Mis overwritten with ones (andb[0] = 1) to imposesum(n) = 1directly, so the resulting square system is non-singular for any well-posed rate matrix and is solved withscipy.linalg.solve()(LU, withoverwrite_a/overwrite_bandcheck_finite=Falseto skip the input scan and reuse the work buffers in-place — several times faster than the SVD-basednumpy.linalg.lstsq()used bysolve_with_normalization()). If the matrix is singular (e.g. NaN/Inf collision rates at extreme parameter values), the raisedLinAlgErroris caught and an array of NaN is returned. In the MCMC context this propagates to a non-finite model flux so the walker step is rejected (log-likelihood -> -inf) rather than crashing. In the direct-modelling contextbest_modelwill contain NaN, signalling that the chosen parameters are unphysical.
- cometspec.rates.g_factors(lines_with_rates, n, *, A_col='A_ul')[source]¶
Compute per-line and total photon/energy g-factors.
The g-factors are in units of photons/s/molecule for
g_photand erg/s/molecule forg_energy.- Parameters:
lines_with_rates (
astropy.table.Table) – Transition table with cached rate-matrix indices.n (
numpy.ndarray) – Level populations.A_col (
str, optional, default"A_ul") – Einstein A column name.
- Returns:
tuple[numpy.ndarray,numpy.ndarray,float,float]–(g_phot, g_energy, sum_g_phot, sum_g_energy).
- cometspec.rates.g_factors_fast_from_cache(*, ui, A_ul, hnu, n, out_g_ph=None, out_g_en=None)[source]¶
Fast g-factor evaluation using precomputed arrays.
- Parameters:
ui (
numpy.ndarray) – Upper-level indices per line.A_ul (
numpy.ndarray) – Einstein A coefficients per line.hnu (
numpy.ndarray) –h * nuper line in erg.n (
numpy.ndarray) – Level populations.out_g_ph (
numpy.ndarray, optional, defaultNone) – Optional output buffer for photon g-factors.out_g_en (
numpy.ndarray, optional, defaultNone) – Optional output buffer for energy g-factors.
- Returns:
tuple[numpy.ndarray,numpy.ndarray]–(g_ph, g_en)arrays.
- cometspec.rates.synth_spectrum_from_lines(df_lines, N_col_cm2, *, g_line_energy=None, g_line_phot=None, fwhm_A=0.02, dlam_A=0.05, lam_min=None, lam_max=None, lam_col='Wave_vac_AA', Omega_sr=None, grid=None, lsf=None, v_shift_kms=0.0, dlam_shift_A=0.0)[source]¶
Build a synthetic emission spectrum from line g-factors.
- Parameters:
df_lines (
astropy.table.Table) – Transition table. RequiredN_col_cm2 (
float) – Column density in cm^-2. Requiredg_line_energy (
numpy.ndarray, optional, defaultNone) – Per-line energy g-factors. If not providedg_line_photmust be used to compute line intensities.g_line_phot (
numpy.ndarray, optional, defaultNone) – Per-line photon g-factors. If not provided,g_line_energymust be used to compute line intensities.fwhm_A (
float, optional, default0.02) – Gaussian FWHM in Angstrom if no custom LSF is provided.dlam_A (
float, optional, default0.05) – Wavelength step for an auto-generated grid.lam_min (
float, optional, defaultNone) – Optional minimum wavelength (after wavelength shifts) for an auto-generated grid. The minimum grid value will be at least 5 FWHM below the minimum line wavelength.lam_max (
float, optional, defaultNone) – Optional maximum wavelength (after wavelength shifts) for an auto-generated grid. The maximum grid value will be at least 5 FWHM above the maximum line wavelength.lam_col (
str, optional, default"Wave_vac_AA"If None,it will be setto"Wave_vac_AA") – Wavelength column indf_lines.Omega_sr (
float, optional, defaultNone) – Optional solid angle scaling in sr.grid (
numpy.ndarray, optional, defaultNone) – Optional output wavelength grid.lsf (
Callable[[numpy.ndarray],numpy.ndarray], optional, defaultNone) – Optional custom line-spread function.v_shift_kms (
float, optional, default0.0) – Velocity shift in km/s for the synthetic spectrum lines.dlam_shift_A (
float, optional, default0.0) – Additive wavelength shift in Angstrom for the synthetic spectrum lines.
- Returns:
tuple[numpy.ndarray,numpy.ndarray]–(wavelength_grid, synthetic_flux).- Raises:
ValueError – If required inputs are missing.
- cometspec.rates.make_lsf(params, mode)[source]¶
Create a line-spread function callable from parameter values.
If LSF mode == “2Gauss”, the parameters are
sigma1,sigma2, andratio.If LSF mode == “Gauss”, the parameter is
sigma.If LSF mode == “Gauss_Lorentz”, the parameters are
sigma_G,fwhm_L, andratio.If LSF mode == “Lorentz”, the parameter is
fwhm_L.
- Parameters:
params (
dict[str,float]) – LSF parameter dictionary.mode (
str) – LSF mode:2Gauss,Gauss,Gauss_Lorentz, orLorentz.
- Returns:
Callable[[numpy.ndarray],numpy.ndarray]– LSF callable. Returned as a small picklable class instance (not a closure), so it can be sent tospawn-based multiprocessing workers when used as thelsfargument tocometspec.mcmc.mcmc_fitting().- Raises:
ValueError – If
modeis invalid.