.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/sources_and_detectors/detector_convergence.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_sources_and_detectors_detector_convergence.py: Choose a mesh for detector collection ===================================== Compare collected power, sampled detector area, and sampled illumination area over four explicit meshes. The study preserves the physical sample and instrument geometry. Its recommendation is a numerical resolution check for these observables, not validation of P1 against radiative transfer. .. GENERATED FROM PYTHON SOURCE LINES 10-28 .. code-block:: Python import os import numpy as np import MomentRT from MomentRT import ( P1, CartesianMesh, Material, RobinBoundary, Simulation, StructuredMedium, SurfaceDetector, SurfaceSource, ureg, ) show_figures = os.environ.get("MOMENTRT_SPHINX_GALLERY") != "1" .. GENERATED FROM PYTHON SOURCE LINES 30-34 Fix the sample and optical configuration ---------------------------------------- These coefficients describe an illustrative homogeneous phantom at 800 nm. The disk source and the separated reflection aperture lie on the top face. .. GENERATED FROM PYTHON SOURCE LINES 34-87 .. code-block:: Python material = Material( phase_function=MomentRT.phase_function.HenyeyGreenstein(), absorption=0.02 / ureg.mm, reduced_scattering=1.0 / ureg.mm, refractive_index=1.4, ) sample = StructuredMedium( origin=(-4, -3, 0) * ureg.mm, size=(8, 6, 4) * ureg.mm, ) sample.add_background(material=material) source = SurfaceSource( face="z_min", position=np.array([-1.5, 0, 0]) * ureg.mm, radius=0.8 * ureg.mm, wavelength=800 * ureg.nm, optical_power=1 * ureg.mW, ) detector = SurfaceDetector( face="z_min", position=np.array([1.5, 0, 0]) * ureg.mm, radius=0.75 * ureg.mm, collection_axis=np.array([0, 0, -1]), acceptance_angle=45 * ureg.degree, ) mesh = CartesianMesh.from_sample( sample=sample, shape=(17, 13, 13), ) boundary = RobinBoundary(effective_reflectance=0.4) solver = P1( boundaries={ face: boundary for face in ("x_min", "x_max", "y_min", "y_max", "z_min", "z_max") }, ) simulation = Simulation( medium=sample, source=source, mesh=mesh, solver=solver, detectors={"reflection": detector}, ) .. GENERATED FROM PYTHON SOURCE LINES 88-98 Compare explicit mesh levels ---------------------------- Power must change by at most 5 percent between successive levels. Both sampled footprint areas must be within 5 percent of their geometric areas. Two consecutive passing refinements are required at the fine end of the study; a recommended level must also agree with the finest supplied power. Each detector legend entry controls both its power and area curves. Node-count ticks identify the exact tested mesh levels. A missing recommendation means more refinement or different tolerances need investigation, rather than automatic acceptance of the last mesh. .. GENERATED FROM PYTHON SOURCE LINES 98-130 .. code-block:: Python study = simulation.mesh_convergence( mesh_shapes=[ (9, 7, 7), (13, 10, 10), (17, 13, 13), (25, 19, 19), ], power_rtol=0.05, area_rtol=0.05, required_consecutive=2, ) convergence_figure = study.plot( power_unit="mW", area_unit="mm^2", show=show_figures, ) for record in study.records: measurement = record["detectors"]["reflection"] print(f"Mesh shape: {record['mesh_shape']}") print(f"Reflection power: {measurement['collected_power'].to('mW'):~P}") print(f"Detector area error: {measurement['area_relative_error']:.2%}") print(f"Source area error: {record['source_area_relative_error']:.2%}") print(f"Recommended shape: {study.recommended_shape}") .. raw:: html .. rst-class:: sphx-glr-script-out .. code-block:: none Mesh shape: (9, 7, 7) Reflection power: 0.006115150694112388 mW Detector area error: 13.18% Source area error: 0.53% Mesh shape: (13, 10, 10) Reflection power: 0.0043455866554655215 mW Detector area error: 7.78% Source area error: 0.53% Mesh shape: (17, 13, 13) Reflection power: 0.004653302067557733 mW Detector area error: 3.75% Source area error: 0.53% Mesh shape: (25, 19, 19) Reflection power: 0.00432318018288807 mW Detector area error: 1.49% Source area error: 1.31% Recommended shape: None .. GENERATED FROM PYTHON SOURCE LINES 131-136 Inspect the finest field with the same instrument overlays ---------------------------------------------------------- The study retains each result. A source footprint with no quadrature points is recorded with result=None; this example's meshes resolve the source. The original simulation.mesh remains unchanged. .. GENERATED FROM PYTHON SOURCE LINES 136-147 .. code-block:: Python finest_result = study.results[-1] fluence_figure = finest_result.fluence.plot( view="volume", scale="log", overlays=[source, detector], show=show_figures, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 148-151 Inspect the modeled geometry ---------------------------- This view shows the configured geometry and instrument footprints. .. GENERATED FROM PYTHON SOURCE LINES 151-153 .. code-block:: Python gallery_system_figure = simulation.plot_system(show=False) .. raw:: html .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 1.060 seconds) .. _sphx_glr_download_gallery_sources_and_detectors_detector_convergence.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: detector_convergence.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: detector_convergence.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: detector_convergence.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_