.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_how_to/example_blockmesh.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_blockmesh.py: Run blockMesh and checkMesh from Python ======================================= Build a mesh from ``system/blockMeshDict`` with :func:`pybFoam.meshing.generate_blockmesh` and inspect it with :func:`pybFoam.meshing.checkMesh`. Both are direct bindings of the OpenFOAM utilities — the returned ``fvMesh`` is a regular mesh that can be used immediately for field access, sampling, or further checks. Prerequisite: OpenFOAM v2312+ sourced; pybFoam installed. .. GENERATED FROM PYTHON SOURCE LINES 15-19 Prepare a working case ---------------------- :func:`pybFoam.clone_example` copies the named example into a tmp directory so the baseline under ``examples/`` stays pristine. .. GENERATED FROM PYTHON SOURCE LINES 19-26 .. code-block:: Python from pybFoam import Time, argList, clone_example, dictionary from pybFoam.meshing import checkMesh, generate_blockmesh case = clone_example("case") print(f"working case: {case}") .. rst-class:: sphx-glr-script-out .. code-block:: none working case: /tmp/pybfoam_q4adtry1/case .. GENERATED FROM PYTHON SOURCE LINES 27-29 Generate the mesh ----------------- .. GENERATED FROM PYTHON SOURCE LINES 29-37 .. code-block:: Python time = Time(argList([str(case), "-case", str(case)])) mesh = generate_blockmesh(time, dictionary.read(str(case / "system" / "blockMeshDict"))) print(f"nCells = {mesh.nCells()}") print(f"nFaces = {mesh.nFaces()}") print(f"nPoints = {mesh.nPoints()}") .. rst-class:: sphx-glr-script-out .. code-block:: none nCells = 2268 nFaces = 9176 nPoints = 4746 .. GENERATED FROM PYTHON SOURCE LINES 38-44 Inspect mesh quality -------------------- ``checkMesh`` returns a structured dict with ``mesh_stats``, ``cell_types``, ``topology``, ``geometry``, ``quality``, ``passed``, ``failed``, and ``total_errors``. Select the flags you want — here we run the topology + geometry checks. .. GENERATED FROM PYTHON SOURCE LINES 44-58 .. code-block:: Python result = checkMesh( mesh, check_topology=True, all_topology=True, all_geometry=True, check_quality=False, ) print(f"passed = {result['passed']}") print(f"total_errors = {result['total_errors']}") print(f"hex cells = {result['cell_types'].get('hexahedra', 0)}") print(f"max non-orth = {result['geometry']['max_non_orthogonality']:.3f}") .. rst-class:: sphx-glr-script-out .. code-block:: none passed = True total_errors = 0 hex cells = 2268 max non-orth = 0.000 .. GENERATED FROM PYTHON SOURCE LINES 59-64 Patch breakdown --------------- Iterate ``mesh.boundary()`` (the ``fvBoundaryMesh``) to see what patches the mesh actually has — useful when verifying that the boundary section of ``blockMeshDict`` came through as expected. .. GENERATED FROM PYTHON SOURCE LINES 64-74 .. code-block:: Python boundary = mesh.boundary() patches = [] for i in range(len(boundary)): patch = boundary[i] name = str(patch.name()) size = patch.size() patches.append((name, size)) print(f"{name:<16s} faces={size}") .. rst-class:: sphx-glr-script-out .. code-block:: none leftWall faces=50 rightWall faces=50 lowerWall faces=62 atmosphere faces=46 defaultFaces faces=0 .. GENERATED FROM PYTHON SOURCE LINES 75-79 Plot the patch face counts -------------------------- A simple matplotlib bar chart confirms the mesh has the patches the user expects. A missing or zero-face patch shows up immediately. .. GENERATED FROM PYTHON SOURCE LINES 79-94 .. code-block:: Python import matplotlib.pyplot as plt names = [p[0] for p in patches] sizes = [p[1] for p in patches] fig, ax = plt.subplots(figsize=(6, 3)) ax.bar(names, sizes, color="steelblue") ax.set_ylabel("number of faces") ax.set_title(f"Boundary patches ({mesh.nCells()} internal cells)") for i, n in enumerate(sizes): ax.text(i, n, f"{n}", ha="center", va="bottom", fontsize=9) fig.tight_layout() plt.show() .. image-sg:: /auto_how_to/images/sphx_glr_example_blockmesh_001.png :alt: Boundary patches (2268 internal cells) :srcset: /auto_how_to/images/sphx_glr_example_blockmesh_001.png :class: sphx-glr-single-img .. GENERATED FROM PYTHON SOURCE LINES 95-100 See also -------- - :doc:`example_read_dictionaries` — extract typed field entries from any dictionary, including ``blockMeshDict``. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.148 seconds) .. _sphx_glr_download_auto_how_to_example_blockmesh.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: example_blockmesh.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: example_blockmesh.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: example_blockmesh.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_