.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/validation/energy_conservation.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_gallery_validation_energy_conservation.py: Energy-conservation validation ============================== Check the identity ``Qext = Qsca + Qabs`` over a wavelength sweep for a lossy sphere. This is an internal consistency validation that is independent of plotting and can be reused as a compact regression example. .. GENERATED FROM PYTHON SOURCE LINES 9-49 .. rst-class:: sphx-glr-script-out .. code-block:: none Maximum absolute residual: 0.000e+00 | .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from PyMieSim import Gaussian, Measure, PolarizationState, Simulation, Sphere, ureg wavelengths = np.linspace(450, 750, 25) * ureg.nanometer residuals = [] for wavelength in wavelengths: source = Gaussian( wavelength=wavelength, polarization=PolarizationState(angle=0 * ureg.degree), optical_power=1e-3 * ureg.watt, numerical_aperture=0.2, ) simulation = Simulation( scatterer=Sphere( diameter=180 * ureg.nanometer, material=1.6 + 0.03j, medium=1.0, ), source=source, ) qext = simulation.run(Measure.QEXT).magnitude qsca = simulation.run(Measure.QSCA).magnitude qabs = simulation.run(Measure.QABS).magnitude residuals.append(float(qext - qsca - qabs)) residuals = np.asarray(residuals) print(f"Maximum absolute residual: {np.max(np.abs(residuals)):.3e}") assert np.max(np.abs(residuals)) < 1e-10 figure, axis = plt.subplots() axis.plot(wavelengths.to("nanometer").magnitude, residuals, marker="o") axis.set(xlabel="Wavelength [nm]", ylabel="Qext - Qsca - Qabs") axis.axhline(0, color="black", linewidth=0.8) figure.tight_layout() plt.close(figure) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.105 seconds) .. _sphx_glr_download_gallery_validation_energy_conservation.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: energy_conservation.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: energy_conservation.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: energy_conservation.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_