.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/getting_started/infinite_diffusion.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_getting_started_infinite_diffusion.py: Infinite-medium diffusion ========================= All physical values are illustrative. The plotted box is an observation window, not a tissue boundary. Optical properties apply at 970 nm. .. GENERATED FROM PYTHON SOURCE LINES 8-25 .. code-block:: Python import os import matplotlib.pyplot as plt import numpy as np import MomentRT from MomentRT import ( CartesianMesh, HomogeneousMedium, InfiniteP1, Material, PointSource, Simulation, ureg, ) show_figures = os.environ.get("MOMENTRT_SPHINX_GALLERY") != "1" .. GENERATED FROM PYTHON SOURCE LINES 27-30 Optical properties ------------------ Define illustrative absorption and reduced scattering at 970 nm. .. GENERATED FROM PYTHON SOURCE LINES 30-42 .. code-block:: Python material = Material( phase_function=MomentRT.phase_function.HenyeyGreenstein(), absorption=0.02 / ureg.millimeter, reduced_scattering=1 / ureg.millimeter, refractive_index=1.4, ) medium = HomogeneousMedium( material=material, ) .. GENERATED FROM PYTHON SOURCE LINES 43-47 Observation window ------------------ This grid samples an infinite medium; its edges are not physical boundaries. The even node counts avoid sampling the singular point source. .. GENERATED FROM PYTHON SOURCE LINES 47-54 .. code-block:: Python mesh = CartesianMesh( shape=(50, 50, 50), spacing=np.array([0.4, 0.4, 0.4]) * ureg.millimeter, origin=(-9.8, -9.8, -9.8) * ureg.millimeter, ) .. GENERATED FROM PYTHON SOURCE LINES 55-58 Embedded illumination --------------------- Place an isotropic point source at the center of the observation window. .. GENERATED FROM PYTHON SOURCE LINES 58-65 .. code-block:: Python source = PointSource( position=np.zeros(3) * ureg.millimeter, wavelength=970 * ureg.nanometer, optical_power=1 * ureg.milliwatt, ) .. GENERATED FROM PYTHON SOURCE LINES 66-69 Configure transport ------------------- Keep the numerical model separate from the physical configuration. .. GENERATED FROM PYTHON SOURCE LINES 69-72 .. code-block:: Python solver = InfiniteP1() .. GENERATED FROM PYTHON SOURCE LINES 73-76 Compose the experiment ---------------------- Combine the medium, illumination, observation mesh, and solver. .. GENERATED FROM PYTHON SOURCE LINES 76-84 .. code-block:: Python simulation = Simulation( medium=medium, source=source, mesh=mesh, solver=solver, ) .. GENERATED FROM PYTHON SOURCE LINES 85-88 Inspect the configuration ------------------------- Rotate the three-dimensional observation window and locate the source. .. GENERATED FROM PYTHON SOURCE LINES 88-93 .. code-block:: Python geometry = simulation.plot_system( show=show_figures, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 94-97 Calculate fluence ----------------- Solve the analytical infinite-medium diffusion model. .. GENERATED FROM PYTHON SOURCE LINES 97-100 .. code-block:: Python result = simulation.run() .. GENERATED FROM PYTHON SOURCE LINES 101-105 Inspect the fluence field ------------------------- The volume uses a logarithmic color scale with physical values retained on hover. Values close to the source remain diffusion approximations. .. GENERATED FROM PYTHON SOURCE LINES 105-112 .. code-block:: Python figure = result.fluence.plot( view="volume", scale="log", show=show_figures, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 113-117 Make a static central-plane overview ------------------------------------- The gallery uses this Matplotlib image as a compact thumbnail. The live three-dimensional Plotly volume remains available in the example above. .. GENERATED FROM PYTHON SOURCE LINES 117-155 .. code-block:: Python fluence_values = result.fluence.values.to("watt / meter**2").magnitude plane_index = mesh.shape[2] // 2 x_axis = ( (mesh.origin[0] + np.arange(mesh.shape[0]) * mesh.spacing[0]) .to("millimeter") .magnitude ) y_axis = ( (mesh.origin[1] + np.arange(mesh.shape[1]) * mesh.spacing[1]) .to("millimeter") .magnitude ) figure_static, axes = plt.subplots(figsize=(6, 5)) image = axes.pcolormesh( x_axis, y_axis, np.log10(fluence_values[:, :, plane_index].T), shading="auto", cmap="magma", ) axes.set_aspect("equal") axes.set_xlabel("x (mm)") axes.set_ylabel("y (mm)") axes.set_title("log10 fluence on a central plane") figure_static.colorbar(image, ax=axes, label="log10 fluence rate (W m$^{-2}$)") figure_static.tight_layout() if show_figures: plt.show() .. image-sg:: /gallery/getting_started/images/sphx_glr_infinite_diffusion_001.png :alt: log10 fluence on a central plane :srcset: /gallery/getting_started/images/sphx_glr_infinite_diffusion_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 156-159 Inspect the modeled geometry ---------------------------- This view shows the configured geometry and instrument footprints. .. GENERATED FROM PYTHON SOURCE LINES 159-161 .. 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.638 seconds) .. _sphx_glr_download_gallery_getting_started_infinite_diffusion.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: infinite_diffusion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: infinite_diffusion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: infinite_diffusion.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_