.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/validation/absorbing_sphere_balance.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_validation_absorbing_sphere_balance.py: Absorbing material and an optical-theorem check =============================================== Assign absorption to a reusable Material before voxelization. Compare forward extinction from the optical theorem against scattering plus dissipation. Finite-order Born approximations need not satisfy this balance exactly. .. GENERATED FROM PYTHON SOURCE LINES 9-75 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np from bornsim import Directions, Grid, Material, Solver, Source, Sphere, StructuredMedium from bornsim.units import ureg material = Material( refractive_index=1.335, extinction_coefficient=0.0002, ) sphere = Sphere(radius=36 * ureg.nanometer, material=material) medium = StructuredMedium() medium.add_background(refractive_index=1.33) medium.add_structures(sphere) grid = Grid(shape=(16, 16, 16), spacing=6 * ureg.nanometer) volume = medium.to_volume(grid=grid) source = Source(wavelength=633 * ureg.nanometer) solver = Solver(source=source, order=4) result = solver.solve(target=volume) forward = Directions(vectors=[[0, 0, 1]]) cut = solver.solve(target=volume, sampling=forward) amplitudes = np.cumsum(cut.amplitudes.to("meter").magnitude, axis=0) forward_component = (amplitudes[:, 0, 0, 0] + amplitudes[:, 0, 1, 1]) / 2 host_wavenumber = 2 * np.pi * 1.33 / source.wavelength.to("meter").magnitude extinction = 4 * np.pi / host_wavenumber * forward_component.imag scattering = (result.mu_s * result.sample_volume).to("meter**2").magnitude absorption = result.absorption_cross_section.to("meter**2").magnitude orders = np.arange(1, 5) figure, axes = plt.subplots(1, 2, figsize=(10, 4), layout="constrained") axes[0].plot(orders, extinction * 1e18, "o-", label="Forward optical theorem") axes[0].plot(orders, (scattering + absorption) * 1e18, "s--", label="Scattering + absorption") axes[0].set(xlabel="Highest Born order", ylabel="Cross section (nm²)", xticks=orders) axes[0].legend() relative_defect = abs(extinction - scattering - absorption) / (scattering + absorption) axes[1].semilogy(orders, relative_defect, "o-") axes[1].set(xlabel="Highest Born order", ylabel="Relative energy-balance defect", xticks=orders) plt.show() .. image-sg:: /auto_examples/validation/images/sphx_glr_absorbing_sphere_balance_001.png :alt: absorbing sphere balance :srcset: /auto_examples/validation/images/sphx_glr_absorbing_sphere_balance_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 76-84 Validation scope ----------------- The automated Mie-reference tests additionally check a weak-contrast lossless sphere and an absorbing sphere against independently evaluated Mie-series cross sections and angular scattering. Those tests match occupied voxel volume to a sphere to reduce staircase-volume bias. They are not a universal accuracy bound. This example checks internal energy consistency; mesh and constitutive errors can remain even if the balance defect is small. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.707 seconds) .. _sphx_glr_download_auto_examples_validation_absorbing_sphere_balance.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: absorbing_sphere_balance.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: absorbing_sphere_balance.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: absorbing_sphere_balance.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_