.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_how_to/example_volume_integral.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_volume_integral.py: Compute a volume integral ========================= ``VolIntegrate`` is the canonical reducer for volume fields: it multiplies by cell volume and sums. Use it whenever "how much" is the question — total mass, total energy, integrated source term. We compute the total water volume in the ``damBreak`` case at t=0. .. GENERATED FROM PYTHON SOURCE LINES 13-15 Open the case ------------- .. GENERATED FROM PYTHON SOURCE LINES 15-38 .. 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, "alpha.water") .. rst-class:: sphx-glr-script-out .. code-block:: none .. GENERATED FROM PYTHON SOURCE LINES 39-43 Integrate --------- ``field | VolIntegrate`` — read as "wrap the field, integrate it by cell volumes". The result is an ``AggregatedDataSet`` with one value. .. GENERATED FROM PYTHON SOURCE LINES 43-51 .. code-block:: Python from pyOFTools.aggregators import VolIntegrate from pyOFTools.builders import field result = (field(mesh, "alpha.water") | VolIntegrate()).compute() total_water_volume = result.values[0].value print(f"total water volume at t=0: {total_water_volume:.6g} m^3") .. rst-class:: sphx-glr-script-out .. code-block:: none total water volume at t=0: 0.000840422 m^3 .. GENERATED FROM PYTHON SOURCE LINES 52-58 Write it to a CSV ----------------- For a standalone script, the raw value is usually enough. But if you are going to wire this into ``@postProcess.Table`` later (see :doc:`/how-to/configure_controlDict`), it is useful to know that the CSV row you get is exactly what ``AggregatedDataSet.grouped_values`` returns. .. GENERATED FROM PYTHON SOURCE LINES 58-61 .. code-block:: Python print("headers:", result.headers) print("rows: ", result.grouped_values) .. rst-class:: sphx-glr-script-out .. code-block:: none headers: ['alpha.water_volIntegrate'] rows: [[0.0008404216050726664]] .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 10.581 seconds) .. _sphx_glr_download_auto_how_to_example_volume_integral.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_volume_integral.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_volume_integral.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_volume_integral.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_