.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/plot_ContributionFunction.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_plot_ContributionFunction.py: =================================================== Contribution function and formation height of 854.2 =================================================== .. GENERATED FROM PYTHON SOURCE LINES 8-12 After converging a simulation, the depth-dependent opacity and emissivity can be stored during a formal solution, and used to compute optical depths and contribution functions. Here we look at where the Ca II 854.2 nm line forms in the FAL C atmosphere. .. GENERATED FROM PYTHON SOURCE LINES 12-20 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import lightweaver as lw from lightweaver.fal import Falc82 from lightweaver.rh_atoms import CaII_atom, H_6_atom from lightweaver.utils import compute_contribution_fn, compute_tau, tau_isosurface .. GENERATED FROM PYTHON SOURCE LINES 21-22 Converge the NLTE problem with H and Ca II active. .. GENERATED FROM PYTHON SOURCE LINES 22-31 .. code-block:: Python atmos = Falc82() atmos.quadrature(5) rad_set = lw.RadiativeSet([H_6_atom(), CaII_atom()]) rad_set.set_active('H', 'Ca') spect = rad_set.compute_wavelength_grid() eq_pops = rad_set.compute_eq_pops(atmos) ctx = lw.Context(atmos, spect, eq_pops, Nthreads=2) lw.iterate_ctx_se(ctx, quiet=True) .. rst-class:: sphx-glr-script-out .. code-block:: none 240 .. GENERATED FROM PYTHON SOURCE LINES 32-34 Ask the formal solver to store the full depth-dependent opacity and emissivity (`ctx.depth_data`), then perform one more formal solution. .. GENERATED FROM PYTHON SOURCE LINES 34-37 .. code-block:: Python ctx.depth_data.fill = True ctx.formal_sol_gamma_matrices() .. rst-class:: sphx-glr-script-out .. code-block:: none IterationUpdate(ctx=, crsw=1.0, updated_J=True, dJ_max=0.004928092666125372, dJ_max_idx=308, updated_pops=False, dpops=[], dpops_max_idx=[], ng_accelerated=False, updated_ne=False, dne_max=0.0, dne_max_idx=0, updated_rho=False, Nprd_sub_iter=0, drho=[], drho_max_idx=[], updated_J_prd=False, dJ_prd_max=[], dJ_prd_max_idx=[]) .. GENERATED FROM PYTHON SOURCE LINES 38-42 Compute the contribution function and optical depth for the outgoing ray with the largest mu (the last ray of the quadrature), and the height at which tau = 1 along this ray. These are computed for every wavelength of the simulation's grid, so we select the points around 854.2. .. GENERATED FROM PYTHON SOURCE LINES 42-54 .. code-block:: Python mu = -1 cfn = compute_contribution_fn(ctx, mu=mu) tau = compute_tau(ctx, mu=mu) z = np.asarray(atmos.height) tau1 = tau_isosurface(tau, z) wavelength = np.asarray(ctx.spect.wavelength) ca_8542 = 854.444 mask = np.abs(wavelength - ca_8542) < 0.1 wave = wavelength[mask] I = np.asarray(ctx.spect.I)[mask, mu] .. GENERATED FROM PYTHON SOURCE LINES 55-57 Integrating the contribution function over height gives the emergent intensity (up to the accuracy of this simple quadrature). .. GENERATED FROM PYTHON SOURCE LINES 57-63 .. code-block:: Python integrated = -np.trapezoid(cfn[mask], z, axis=1) print( f'Max relative difference between integrated cfn and I: ' f'{np.max(np.abs(integrated - I) / I):.2e}' ) .. rst-class:: sphx-glr-script-out .. code-block:: none Max relative difference between integrated cfn and I: 5.48e-02 .. GENERATED FROM PYTHON SOURCE LINES 64-67 Plot the contribution function (normalised at each wavelength) with the tau = 1 height overlaid, and the emergent profile below. The line core forms in the chromosphere, around 1.2 Mm higher than the wings. .. GENERATED FROM PYTHON SOURCE LINES 67-81 .. code-block:: Python z_mm = z / 1e6 fig, ax = plt.subplots( 2, 1, figsize=(7, 7), sharex=True, height_ratios=[2, 1], constrained_layout=True ) cfn_norm = cfn[mask] / cfn[mask].max(axis=1, keepdims=True) ax[0].pcolormesh(wave, z_mm, cfn_norm.T, shading='auto', cmap='magma') ax[0].plot(wave, tau1[mask] / 1e6, c='C0', label=r'$\tau = 1$') ax[0].set_ylim(0, 2.2) ax[0].set_ylabel('Height [Mm]') ax[0].legend() ax[1].plot(wave, I) ax[1].set_xlabel('Wavelength [nm]') ax[1].set_ylabel(r'$I_\nu$ [J s$^{-1}$ m$^{-2}$ sr$^{-1}$ Hz$^{-1}$]') plt.show() .. image-sg:: /auto_examples/images/sphx_glr_plot_ContributionFunction_001.png :alt: plot ContributionFunction :srcset: /auto_examples/images/sphx_glr_plot_ContributionFunction_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 3.482 seconds) .. _sphx_glr_download_auto_examples_plot_ContributionFunction.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_ContributionFunction.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_ContributionFunction.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_ContributionFunction.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_