Note
Go to the end to download the full example code.
Computing a simple NLTE 8542 line profile in a FAL C atmosphere
First, we import everything we need. Lightweaver is typically imported as lw, but things like the library of model atoms and Fal atmospheres need to be imported separately.
from lightweaver.fal import Falc82
from lightweaver.rh_atoms import H_6_atom, C_atom, O_atom, Si_atom, Al_atom, \
CaII_atom, Fe_atom, He_9_atom, MgII_atom, N_atom, Na_atom, S_atom
import lightweaver as lw
import matplotlib.pyplot as plt
import time
import numpy as np
Now, we define the functions that will be used in our spectral synthesise. First synth_8542 which synthesises and returns the line given by an atmosphere.
def synth_8542(atmos, conserve, useNe, wave):
'''
Synthesise a spectral line for given atmosphere with different
conditions.
Parameters
----------
atmos : lw.Atmosphere
The atmospheric model in which to synthesise the line.
conserve : bool
Whether to start from LTE electron density and conserve charge, or
simply use from the electron density present in the atomic model.
useNe : bool
Whether to use the electron density present in the model as the
starting solution, or compute the LTE electron density.
wave : np.ndarray
Array of wavelengths over which to resynthesise the final line
profile for muz=1.
Returns
-------
ctx : lw.Context
The Context object that was used to compute the equilibrium
populations.
Iwave : np.ndarray
The intensity at muz=1 for each wavelength in `wave`.
'''
# Configure the atmospheric angular quadrature
atmos.quadrature(5)
# Configure the set of atomic models to use.
aSet = lw.RadiativeSet([H_6_atom(), C_atom(), O_atom(), Si_atom(),
Al_atom(), CaII_atom(), Fe_atom(), He_9_atom(),
MgII_atom(), N_atom(), Na_atom(), S_atom()
])
# Set H and Ca to "active" i.e. NLTE, everything else participates as an
# LTE background.
aSet.set_active('H', 'Ca')
# Compute the necessary wavelength dependent information (SpectrumConfiguration).
spect = aSet.compute_wavelength_grid()
# Either compute the equilibrium populations at the fixed electron density
# provided in the model, or iterate an LTE electron density and compute the
# corresponding equilibrium populations (SpeciesStateTable).
if useNe:
eqPops = aSet.compute_eq_pops(atmos)
else:
eqPops = aSet.iterate_lte_ne_eq_pops(atmos)
# Configure the Context which holds the state of the simulation for the
# backend, and provides the python interface to the backend.
# Feel free to increase Nthreads to increase the number of threads the
# program will use.
ctx = lw.Context(atmos, spect, eqPops, conserveCharge=conserve, Nthreads=1)
# Iterate the Context to convergence (using the iteration function now
# provided by Lightweaver)
lw.iterate_ctx_se(ctx)
# Update the background populations based on the converged solution and
# compute the final intensity for mu=1 on the provided wavelength grid.
eqPops.update_lte_atoms_Hmin_pops(atmos)
Iwave = ctx.compute_rays(wave, [atmos.muz[-1]], stokes=False)
return ctx, Iwave
The wavelength grid to output the final synthesised line on.
wave = np.linspace(853.9444, 854.9444, 1001)
Load an lw.Atmosphere object containing the FAL C atmosphere with 82 points in depth, before synthesising the Ca II 8542 AA line profile using:
The given electron density.
The electron density charge conserved from a starting LTE solution.
The LTE electron density.
These results are then plotted.
atmosRef = Falc82()
ctxRef, IwaveRef = synth_8542(atmosRef, conserve=False, useNe=True, wave=wave)
atmosCons = Falc82()
ctxCons, IwaveCons = synth_8542(atmosCons, conserve=True, useNe=False, wave=wave)
atmosLte = Falc82()
ctx, IwaveLte = synth_8542(atmosLte, conserve=False, useNe=False, wave=wave)
plt.plot(wave, IwaveRef, label='Reference FAL')
plt.plot(wave, IwaveCons, label='Reference Cons')
plt.plot(wave, IwaveLte, label='Reference LTE n_e')
plt.legend()
plt.show()
-- Iteration 0:
dJ = 1.00e+00
(Lambda iterating background)
-- Iteration 7:
dJ = 1.39e+00
H delta = 4.0869e-01
Ca delta = 2.6519e-01
-- Iteration 14:
dJ = 2.23e-01
H delta = 1.3472e-01
Ca delta = 1.1470e-01
-- Iteration 21:
dJ = 1.09e-01
H delta = 5.5900e-02
Ca delta = 5.1882e-02
-- Iteration 28:
dJ = 8.04e-02
H delta = 4.9052e-02
Ca delta = 3.2436e-02
-- Iteration 35:
dJ = 7.07e-02
H delta = 4.3949e-02
Ca delta = 2.3786e-02
-- Iteration 42:
dJ = 6.41e-02
H delta = 3.7163e-02
Ca delta = 1.8058e-02
-- Iteration 49:
dJ = 5.65e-02
H delta = 3.0134e-02
Ca delta = 1.3092e-02
-- Iteration 56:
dJ = 4.60e-02
H delta = 2.3072e-02
Ca delta = 8.7255e-03
-- Iteration 63:
dJ = 3.34e-02
H delta = 1.7782e-02
Ca delta = 5.3550e-03
-- Iteration 70:
dJ = 2.15e-02
H delta = 1.1876e-02
Ca delta = 3.0818e-03
-- Iteration 77:
dJ = 1.26e-02
H delta = 7.0785e-03
Ca delta = 1.6607e-03
-- Iteration 84:
dJ = 6.88e-03
H delta = 3.9289e-03
Ca delta = 8.7176e-04
-- Iteration 91:
dJ = 3.63e-03
H delta = 2.0879e-03
Ca delta = 4.4986e-04
-- Iteration 98:
dJ = 1.87e-03
H delta = 1.0813e-03
Ca delta = 2.3031e-04
--------------------------------------------------------------------------------
Final Iteration: 99
--------------------------------------------------------------------------------
dJ = 1.70e-03
H delta = 9.8324e-04
Ca delta = 2.0990e-04
--------------------------------------------------------------------------------
Context converged to statistical equilibrium in 99 iterations after 3.22 s.
--------------------------------------------------------------------------------
LTE Iterations 1 (-- slowest convergence)
-- Iteration 0:
dJ = 1.00e+00
(Lambda iterating background)
-- Iteration 7:
dJ = 9.63e-01
H delta = 4.7083e-01
Ca delta = 2.1479e-01
ne delta = 1.0980e-10
-- Iteration 13:
dJ = 2.87e-01
H delta = 2.1174e-01
Ca delta = 1.0680e-01
ne delta = 3.3406e-13
-- Iteration 19:
dJ = 1.28e-01
H delta = 7.8491e-02
Ca delta = 5.7767e-02
ne delta = 2.6401e-15
-- Iteration 25:
dJ = 9.10e-02
H delta = 5.5430e-02
Ca delta = 3.6327e-02
ne delta = 1.2321e-15
-- Iteration 31:
dJ = 7.61e-02
H delta = 4.8553e-02
Ca delta = 2.5084e-02
ne delta = 7.0403e-16
-- Iteration 37:
dJ = 6.82e-02
H delta = 4.3857e-02
Ca delta = 1.8210e-02
ne delta = 1.0207e-15
-- Iteration 43:
dJ = 6.29e-02
H delta = 3.7590e-02
Ca delta = 1.3229e-02
ne delta = 3.4023e-16
-- Iteration 49:
dJ = 5.74e-02
H delta = 3.4340e-02
Ca delta = 9.1206e-03
ne delta = 1.1854e-15
-- Iteration 55:
dJ = 5.08e-02
H delta = 3.1091e-02
Ca delta = 5.8793e-03
ne delta = 1.0669e-15
-- Iteration 61:
dJ = 4.30e-02
H delta = 2.6848e-02
Ca delta = 3.5450e-03
ne delta = 2.0055e-15
-- Iteration 67:
dJ = 3.37e-02
H delta = 2.1794e-02
Ca delta = 2.1398e-03
ne delta = 1.3040e-15
-- Iteration 73:
dJ = 2.54e-02
H delta = 1.6639e-02
Ca delta = 1.7595e-03
ne delta = 2.0414e-15
-- Iteration 79:
dJ = 1.81e-02
H delta = 1.2030e-02
Ca delta = 1.3881e-03
ne delta = -1.7601e-16
-- Iteration 85:
dJ = 1.24e-02
H delta = 8.2873e-03
Ca delta = 1.0006e-03
ne delta = 8.5057e-16
-- Iteration 91:
dJ = 8.20e-03
H delta = 5.4886e-03
Ca delta = 6.8131e-04
ne delta = 1.5411e-15
-- Iteration 97:
dJ = 5.21e-03
H delta = 3.5020e-03
Ca delta = 4.4508e-04
ne delta = 7.0403e-16
-- Iteration 103:
dJ = 3.26e-03
H delta = 2.1960e-03
Ca delta = 2.8318e-04
ne delta = 8.1270e-16
-- Iteration 109:
dJ = 2.02e-03
H delta = 1.3623e-03
Ca delta = 1.7718e-04
ne delta = 2.7576e-15
--------------------------------------------------------------------------------
Final Iteration: 113
--------------------------------------------------------------------------------
dJ = 1.46e-03
H delta = 9.8695e-04
Ca delta = 1.2885e-04
ne delta = -5.4180e-16
--------------------------------------------------------------------------------
Context converged to statistical equilibrium in 113 iterations after 3.89 s.
--------------------------------------------------------------------------------
LTE Iterations 1 (-- slowest convergence)
-- Iteration 0:
dJ = 1.00e+00
(Lambda iterating background)
-- Iteration 7:
dJ = 1.15e+00
H delta = 4.0475e-01
Ca delta = 2.6918e-01
-- Iteration 14:
dJ = 1.87e-01
H delta = 1.0140e-01
Ca delta = 7.3618e-02
-- Iteration 21:
dJ = 1.04e-01
H delta = 5.7490e-02
Ca delta = 3.6305e-02
-- Iteration 28:
dJ = 7.72e-02
H delta = 5.0382e-02
Ca delta = 2.4421e-02
-- Iteration 35:
dJ = 6.62e-02
H delta = 4.4336e-02
Ca delta = 1.8149e-02
-- Iteration 42:
dJ = 5.83e-02
H delta = 3.6137e-02
Ca delta = 1.3145e-02
-- Iteration 49:
dJ = 4.87e-02
H delta = 2.6396e-02
Ca delta = 8.8447e-03
-- Iteration 56:
dJ = 3.67e-02
H delta = 1.8626e-02
Ca delta = 5.4852e-03
-- Iteration 63:
dJ = 2.39e-02
H delta = 1.2764e-02
Ca delta = 3.0983e-03
-- Iteration 70:
dJ = 1.42e-02
H delta = 7.7443e-03
Ca delta = 1.6866e-03
-- Iteration 77:
dJ = 8.01e-03
H delta = 4.3965e-03
Ca delta = 8.9956e-04
-- Iteration 84:
dJ = 4.19e-03
H delta = 2.3351e-03
Ca delta = 4.5131e-04
-- Iteration 91:
dJ = 2.15e-03
H delta = 1.2012e-03
Ca delta = 2.2773e-04
--------------------------------------------------------------------------------
Final Iteration: 93
--------------------------------------------------------------------------------
dJ = 1.77e-03
H delta = 9.8999e-04
Ca delta = 1.8946e-04
--------------------------------------------------------------------------------
Context converged to statistical equilibrium in 93 iterations after 3.04 s.
--------------------------------------------------------------------------------
LTE Iterations 1 (-- slowest convergence)
Total running time of the script: (0 minutes 11.007 seconds)