.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/validation/convergence_reports.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_convergence_reports.py: Explicit refinement studies and equation residuals ================================================== Change one numerical control at a time. Grid studies below retain the same physical sphere and box; extent studies retain voxel spacing and sphere size. Statistical sample-size studies require independently assessing representative volume effects and must not be confused with padding a deterministic sphere. .. GENERATED FROM PYTHON SOURCE LINES 10-32 .. code-block:: Python import matplotlib.pyplot as plt from bornsim import AngularSampling, ConvergenceReport, Grid, Material, Solver, Source, Sphere, StructuredMedium from bornsim.units import ureg material = Material(refractive_index=1.335, extinction_coefficient=0.0005) sphere = Sphere(radius=40 * ureg.nanometer, material=material) medium = StructuredMedium() medium.add_background(refractive_index=1.33) medium.add_structures(sphere) source = Source(wavelength=633 * ureg.nanometer) base_grid = Grid(shape=(12, 12, 12), spacing=10 * ureg.nanometer) base_volume = medium.to_volume(grid=base_grid) .. GENERATED FROM PYTHON SOURCE LINES 33-38 Born order: residual of the cumulative internal field ------------------------------------------------------ residual_norms[M-1] is ||E_inc + K chi E_[M] - E_[M]|| / ||E_inc||. This checks the discretized equation, not the omitted quadratic contrast, staircase geometry or missing physical effects. .. GENERATED FROM PYTHON SOURCE LINES 38-57 .. code-block:: Python order_results = [] for order in (1, 2, 3, 4): solver = Solver(source=source, order=order) order_results.append(solver.solve(target=base_volume)) report = ConvergenceReport(results=tuple(order_results), labels=("Order 1", "Order 2", "Order 3", "Order 4")) print(report.summary()) figure, axis = plt.subplots(layout="constrained") axis.semilogy([1, 2, 3, 4], order_results[-1].residual_norms.magnitude, "o-") axis.set(xlabel="Internal-field Born order", ylabel="Relative equation residual", xticks=[1, 2, 3, 4]) plt.show() .. image-sg:: /auto_examples/validation/images/sphx_glr_convergence_reports_001.png :alt: convergence reports :srcset: /auto_examples/validation/images/sphx_glr_convergence_reports_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none Observable stability (not a physical-validity certificate): Order 1: mu_s=3.72872 m^-1, mu_a=1608.3866197946693, residual=3.604774718193159e-06, stable=False Order 2: mu_s=3.71372 m^-1, mu_a=1602.0219596880988, residual=1.1563284751113575e-08, stable=True Changes: {'mu_s': 0.004022791563443326, 'mu_a': 0.003957170513755569, 'g': 4.3278253315841286e-05, 'phase_l1': 6.759171247849403e-05} Order 3: mu_s=3.71376 m^-1, mu_a=1602.0363469373933, residual=5.3320129528529805e-11, stable=True Changes: {'mu_s': 9.03701050255426e-06, 'mu_a': 8.98060104690545e-06, 'g': 6.889289475703331e-08, 'phase_l1': 1.1635563432826566e-07} Order 4: mu_s=3.71376 m^-1, mu_a=1602.0363104122728, residual=3.067321596781078e-13, stable=True Changes: {'mu_s': 2.268242453929161e-08, 'mu_a': 2.279918343877665e-08, 'g': 9.268708717202756e-11, 'phase_l1': 2.0384946816435508e-10} .. GENERATED FROM PYTHON SOURCE LINES 58-60 Grid refinement at fixed box size ----------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 60-75 .. code-block:: Python solver = Solver(source=source, order=4) grid_results = [] for count in (8, 12, 16): grid = Grid(shape=(count, count, count), spacing=120 / count * ureg.nanometer) volume = medium.to_volume(grid=grid) grid_results.append(solver.solve(target=volume)) grid_report = ConvergenceReport(results=tuple(grid_results), labels=("8 cubed", "12 cubed", "16 cubed")) print(grid_report.summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Observable stability (not a physical-validity certificate): 8 cubed: mu_s=4.15801 m^-1, mu_a=1699.357783767117, residual=3.592210578621519e-13, stable=False 12 cubed: mu_s=3.71376 m^-1, mu_a=1602.0363104122728, residual=3.067321596781078e-13, stable=False Changes: {'mu_s': 0.1068422017815386, 'mu_a': 0.05726956046836893, 'g': 0.0020231708661912276, 'phase_l1': 0.0028749802176079443} 16 cubed: mu_s=3.29636 m^-1, mu_a=1506.1453668356592, residual=2.5253191594829603e-13, stable=False Changes: {'mu_s': 0.11239269212642004, 'mu_a': 0.05985566179329403, 'g': 0.0017421543475186485, 'phase_l1': 0.0024629458375026946} .. GENERATED FROM PYTHON SOURCE LINES 76-78 Angular quadrature refinement on a fixed grid ----------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 78-89 .. code-block:: Python angular_results = [] for samples in (16, 24, 32): sampling = AngularSampling(polar_samples=samples, azimuth_samples=samples) angular_results.append(solver.solve(target=base_volume, sampling=sampling)) angular_report = ConvergenceReport(results=tuple(angular_results), labels=("16 x 16", "24 x 24", "32 x 32")) print(angular_report.summary()) .. rst-class:: sphx-glr-script-out .. code-block:: none Observable stability (not a physical-validity certificate): 16 x 16: mu_s=3.71376 m^-1, mu_a=1602.0363104122728, residual=3.067321596781078e-13, stable=False 24 x 24: mu_s=3.71376 m^-1, mu_a=1602.0363104122728, residual=3.067321596781078e-13, stable=True Changes: {'mu_s': 7.174771485410794e-16, 'mu_a': 0.0, 'g': 3.469446951953614e-17, 'phase_l1': 7.414988375534874e-16} 32 x 32: mu_s=3.71376 m^-1, mu_a=1602.0363104122728, residual=3.067321596781078e-13, stable=True Changes: {'mu_s': 0.0, 'mu_a': 0.0, 'g': 9.71445146547012e-17, 'phase_l1': 3.011906932774793e-16} .. GENERATED FROM PYTHON SOURCE LINES 90-95 Finite box extent: compare cross sections, not box-normalized coefficients -------------------------------------------------------------------------- For a fixed sphere, padding adds empty host voxels. mu_s and mu_a change because they are divided by box volume. Physical cross sections should be stable instead; this illustrates why a generic stability flag needs context. .. GENERATED FROM PYTHON SOURCE LINES 95-107 .. code-block:: Python for count in (12, 16): padded_grid = Grid(shape=(count, count, count), spacing=10 * ureg.nanometer) padded_volume = medium.to_volume(grid=padded_grid) result = solver.solve(target=padded_volume) scattering_area = (result.mu_s * result.sample_volume).to("nanometer**2") absorption_area = result.absorption_cross_section.to("nanometer**2") print(f"Box {count} cubed: scattering area = {scattering_area[-1]}, absorption area = {absorption_area[-1]}") .. rst-class:: sphx-glr-script-out .. code-block:: none Box 12 cubed: scattering area = 0.0064173708348157195 nanometer ** 2, absorption area = 2.7683187443924075 nanometer ** 2 Box 16 cubed: scattering area = 0.0064173708348157195 nanometer ** 2, absorption area = 2.768318744392408 nanometer ** 2 .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 8.566 seconds) .. _sphx_glr_download_auto_examples_validation_convergence_reports.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: convergence_reports.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: convergence_reports.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: convergence_reports.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_