.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_how_to/example_sample_plane.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_how_to_example_sample_plane.py: Sample a field on a cutting plane ================================= Interpolating a volume field onto a plane is the 2-D analogue of :doc:`example_sample_line`. You get back a ``SurfaceDataSet`` — face centres, face areas, and the sampled field. From there the usual reducers apply (``Mean``, ``Sum``, ``SurfIntegrate``). .. GENERATED FROM PYTHON SOURCE LINES 12-14 Open the case ------------- .. GENERATED FROM PYTHON SOURCE LINES 14-37 .. code-block:: Python import os import subprocess import pyOFTools.patch_pybfoam # noqa: F401 from pyOFTools import clone_example CASE = clone_example("damBreak") subprocess.run( ["./Allrun"], cwd=CASE, check=True, env={**os.environ}, capture_output=True, text=True, ) from pybFoam import Time, fvMesh, volScalarField time = Time(str(CASE.parent), CASE.name) mesh = fvMesh(time) volScalarField.read_field(mesh, "p") .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 38-42 Sample p on a mid-plane ----------------------- ``plane`` gives the geometry; ``sample(mesh, "p")`` interpolates ``p`` onto it. .. GENERATED FROM PYTHON SOURCE LINES 42-56 .. code-block:: Python from pyOFTools.aggregators import Max, Mean from pyOFTools.builders import plane, sample mid_plane_mean = ( plane(mesh, point=(0.0, 0.292, 0.0), normal=(0, 1, 0)) | sample(mesh, "p") | Mean() ).compute() print(f"mean p on y=0.292 plane = {mid_plane_mean.values[0].value:.4g} Pa") mid_plane_max = ( plane(mesh, point=(0.0, 0.292, 0.0), normal=(0, 1, 0)) | sample(mesh, "p") | Max() ).compute() print(f"max p on y=0.292 plane = {mid_plane_max.values[0].value:.4g} Pa") .. rst-class:: sphx-glr-script-out .. code-block:: none mean p on y=0.292 plane = 1e+05 Pa max p on y=0.292 plane = 1e+05 Pa .. GENERATED FROM PYTHON SOURCE LINES 57-62 Scatter plot of sampled values ------------------------------ To inspect the field itself (not a reduction), ``compute()`` the plane workflow after ``sample`` but without a reducer — the returned object is the ``SurfaceDataSet`` with geometry and field populated. .. GENERATED FROM PYTHON SOURCE LINES 62-83 .. code-block:: Python from pyOFTools.workflow import WorkFlow plane_wf = plane(mesh, point=(0.0, 0.292, 0.0), normal=(0, 1, 0)) | sample(mesh, "p") assert isinstance(plane_wf, WorkFlow) surface_ds = plane_wf.compute() import matplotlib.pyplot as plt import numpy as np positions = np.asarray(surface_ds.geometry.positions) p_values = np.asarray(surface_ds.field) fig, ax = plt.subplots(figsize=(6, 4)) sc = ax.scatter(positions[:, 0], positions[:, 2], c=p_values, s=6) ax.set_xlabel("x [m]") ax.set_ylabel("z [m]") ax.set_title("p on y=0.292 plane") fig.colorbar(sc, ax=ax, label="p [Pa]") fig.tight_layout() plt.show() .. image-sg:: /auto_how_to/images/sphx_glr_example_sample_plane_001.png :alt: p on y=0.292 plane :srcset: /auto_how_to/images/sphx_glr_example_sample_plane_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.599 seconds) .. _sphx_glr_download_auto_how_to_example_sample_plane.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_sample_plane.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_sample_plane.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_sample_plane.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_