.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "gallery/getting_started/surface_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_surface_diffusion.py: Surface illumination and FEM diffusion ====================================== Illustrative single-wavelength properties. Effective reflectance is supplied explicitly, not inferred from refractive index. All internal refractive indices are equal: refracting internal interfaces are outside this solver's scope. .. GENERATED FROM PYTHON SOURCE LINES 9-24 .. code-block:: Python import matplotlib.pyplot as plt import numpy as np import MomentRT from MomentRT import ( P1, Material, RobinBoundary, Simulation, StructuredMedium, SurfaceSource, TetrahedralMesh, ureg, ) .. GENERATED FROM PYTHON SOURCE LINES 26-30 Single-wavelength materials --------------------------- Use equal internal refractive indices: this continuous-fluence FEM model does not support refracting internal interfaces. .. GENERATED FROM PYTHON SOURCE LINES 30-45 .. code-block:: Python background = Material( phase_function=MomentRT.phase_function.HenyeyGreenstein(), absorption=0.02 / ureg.millimeter, reduced_scattering=1 / ureg.millimeter, refractive_index=1.4, ) upper_material = Material( phase_function=MomentRT.phase_function.HenyeyGreenstein(), absorption=0.04 / ureg.millimeter, reduced_scattering=1.5 / ureg.millimeter, refractive_index=1.4, ) .. GENERATED FROM PYTHON SOURCE LINES 46-49 Build the layered sample ------------------------ Create a finite rectangular sample and add a one-millimeter upper layer. .. GENERATED FROM PYTHON SOURCE LINES 49-65 .. code-block:: Python sample = StructuredMedium( origin=(-4, -4, 0) * ureg.millimeter, size=(8, 8, 4) * ureg.millimeter, ) sample.add_background( material=background, ) sample.add_layer( name="Upper layer", thickness=1 * ureg.millimeter, material=upper_material, ) .. GENERATED FROM PYTHON SOURCE LINES 66-70 Discretize the sample --------------------- Generate tetrahedra fitted to the layer interface. Resolution is a target element size; refine the mesh to assess spatial and source-footprint accuracy. .. GENERATED FROM PYTHON SOURCE LINES 70-76 .. code-block:: Python mesh = TetrahedralMesh.from_sample( sample=sample, resolution=0.5 * ureg.millimeter, ) .. GENERATED FROM PYTHON SOURCE LINES 77-81 Illuminate the surface ---------------------- Define a finite circular diffuse spot on the upper face. Optical power is already coupled into the sample, excluding specular entry losses. .. GENERATED FROM PYTHON SOURCE LINES 81-90 .. code-block:: Python source = SurfaceSource( face="z_min", position=np.array([0, 0, 0]) * ureg.millimeter, radius=1 * ureg.millimeter, wavelength=970 * ureg.nanometer, optical_power=1 * ureg.milliwatt, ) .. GENERATED FROM PYTHON SOURCE LINES 91-95 Configure boundary conditions ----------------------------- Supply the effective diffuse reflectance explicitly on every face. It is not the normal-incidence Fresnel reflectance. .. GENERATED FROM PYTHON SOURCE LINES 95-104 .. code-block:: Python boundary = RobinBoundary( effective_reflectance=0.5, ) solver = P1( boundaries=MomentRT.Boundaries.uniform(condition=boundary), ) .. GENERATED FROM PYTHON SOURCE LINES 105-108 Compose the simulation ---------------------- Keep sample, mesh, illumination, and solver configuration distinct. .. GENERATED FROM PYTHON SOURCE LINES 108-116 .. code-block:: Python simulation = Simulation( medium=sample, source=source, mesh=mesh, solver=solver, ) .. GENERATED FROM PYTHON SOURCE LINES 117-120 Inspect the illuminated sample ------------------------------ The red circle shows the illumination footprint on the sample surface. .. GENERATED FROM PYTHON SOURCE LINES 120-126 .. code-block:: Python geometry = simulation.plot_system( show=False, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 127-131 Solve and check energy balance ------------------------------ Compare incoming, absorbed, and escaping power. Conservation verifies numerical accounting, not the accuracy of diffusion against radiative transfer. .. GENERATED FROM PYTHON SOURCE LINES 131-142 .. code-block:: Python result = simulation.run() print(f"Incoming power: {result.energy_balance['incoming_power']}") print(f"Absorbed power: {result.energy_balance['absorbed_power'].to('milliwatt')}") print(f"Escaping power: {result.energy_balance['escaping_power'].to('milliwatt')}") print(f"Energy residual: {result.energy_balance['residual_power'].to('watt')}") .. rst-class:: sphx-glr-script-out .. code-block:: none Incoming power: 1 milliwatt Absorbed power: 0.24344504993650523 milliwatt Escaping power: 0.7565549500634947 milliwatt Energy residual: 1.0842021724855044e-19 watt .. GENERATED FROM PYTHON SOURCE LINES 143-147 Explore fluence in 3D --------------------- Inspect the fluence volume. The logarithmic colors retain physical hover values; neither negative values nor zeros are silently clipped. .. GENERATED FROM PYTHON SOURCE LINES 147-155 .. code-block:: Python figure = result.fluence.plot( view="volume", scale="log", display_resolution=0.4 * ureg.millimeter, show=False, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 156-160 Save a representative cross-section ------------------------------------ This static plane complements the interactive volume above; use the Plotly view to rotate the sample and inspect physical hover values. .. GENERATED FROM PYTHON SOURCE LINES 160-205 .. code-block:: Python display_field = result.fluence.resample(resolution=0.4 * ureg.millimeter) display_mesh = display_field.mesh fluence_values = display_field.values.to("watt / meter**2").magnitude center_y = display_mesh.shape[1] // 2 x_axis = ( ( display_mesh.origin[0] + np.arange(display_mesh.shape[0]) * display_mesh.spacing[0] ) .to("millimeter") .magnitude ) z_axis = ( ( display_mesh.origin[2] + np.arange(display_mesh.shape[2]) * display_mesh.spacing[2] ) .to("millimeter") .magnitude ) figure_static, axes = plt.subplots(figsize=(6, 4)) image = axes.pcolormesh( x_axis, z_axis, np.log10(fluence_values[:, center_y, :].T), shading="auto", cmap="magma", ) axes.set_aspect("equal") axes.set_xlabel("x (mm)") axes.set_ylabel("z (mm)") axes.set_title("log10 fluence through the illuminated source") figure_static.colorbar(image, ax=axes, label="log10 fluence rate (W m$^{-2}$)") figure_static.tight_layout() .. image-sg:: /gallery/getting_started/images/sphx_glr_surface_diffusion_001.png :alt: log10 fluence through the illuminated source :srcset: /gallery/getting_started/images/sphx_glr_surface_diffusion_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 206-209 Inspect diffusive flux ---------------------- Arrows show element-centered flux without smoothing across interfaces. .. GENERATED FROM PYTHON SOURCE LINES 209-215 .. code-block:: Python flux_figure = result.flux.plot( show=False, ) .. raw:: html .. GENERATED FROM PYTHON SOURCE LINES 216-219 Inspect the modeled geometry ---------------------------- This view shows the configured geometry and instrument footprints. .. GENERATED FROM PYTHON SOURCE LINES 219-221 .. 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.279 seconds) .. _sphx_glr_download_gallery_getting_started_surface_diffusion.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: surface_diffusion.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: surface_diffusion.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: surface_diffusion.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_