.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/unstructured/plot_GetCellVolume.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_examples_unstructured_plot_GetCellVolume.py: Get the cell centroids and cell volumes of a given box ============================================ This example shows how to extract the cell volumes inside a given box .. GENERATED FROM PYTHON SOURCE LINES 10-16 First import the getVolumes function and other relevant libraries -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 16-28 .. code-block:: Python #import the class MeshVisu, numpy library and getVolumes function from fluidfoam import MeshVisu from fluidfoam.readof import getVolumes import numpy as np # path to the simulation to load path = '../../output_samples/pipeline' # Load mesh and create an object called myMesh # The box by default is equal to the mesh dimension myMesh = MeshVisu( path = path) .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../../output_samples/pipeline/constant/polyMesh/faces Reading file ../../output_samples/pipeline/constant/polyMesh/points Box set to mesh size: (minx, miny, minz) = (np.float64(-0.75), np.float64(-0.1), np.float64(-0.001)) (maxx, maxy, maxz) = (np.float64(1.0), np.float64(0.205), np.float64(0.0)) .. GENERATED FROM PYTHON SOURCE LINES 29-32 We are going to extract the cell volumes and cell centroids of two given boxes -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 32-48 .. code-block:: Python #tuple of box's dimension: ((xmin, ymin, zmin), (xmax, ymax, zmax)) mybox_A = ((0, 0, -1), (0.03, 0.03, 1)) mybox_B = ((0.022, 0.0221, -1), (0.0274, 0.0275, 1)) #getVolumes function returns arrays containing the centroids and volume of the #cells inside boxes A and B centroidList_box_A,vol_box_A = getVolumes( path = path, box = mybox_A) centroidList_box_B,vol_box_B = getVolumes( path = path, box = mybox_B) vol_box_A_total = sum(vol_box_A) vol_box_B_total = sum(vol_box_B) print("Total cell volume inside the box A:", vol_box_A_total) print("Total cell volume inside the box B:", vol_box_B_total) .. rst-class:: sphx-glr-script-out .. code-block:: none Reading file ../../output_samples/pipeline/constant/polyMesh/owner Reading file ../../output_samples/pipeline/constant/polyMesh/faces Reading file ../../output_samples/pipeline/constant/polyMesh/points Reading file ../../output_samples/pipeline/constant/polyMesh/neighbour Reading file ../../output_samples/pipeline/constant/polyMesh/owner Reading file ../../output_samples/pipeline/constant/polyMesh/faces Reading file ../../output_samples/pipeline/constant/polyMesh/points Reading file ../../output_samples/pipeline/constant/polyMesh/neighbour Total cell volume inside the box A: 4.0746335288354696e-07 Total cell volume inside the box B: 2.966701578686209e-08 .. GENERATED FROM PYTHON SOURCE LINES 49-51 Visualisation of the two boxes -------------------------------------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 51-86 .. code-block:: Python myMesh.update_box(mybox_A) import matplotlib.pyplot as plt from matplotlib.collections import LineCollection from matplotlib.patches import Rectangle fig, ax = plt.subplots( figsize = (8,8)) # create a collection with edges and print it ln_coll = LineCollection(myMesh.get_all_edgesInBox(), linewidths = 0.20, colors = 'black') ax.add_collection(ln_coll, autolim=True) # Set box dimensions as the figures's limits ax.set_xlim(myMesh.get_xlim()) ax.set_ylim(myMesh.get_ylim()) # Add rectangle to plot, which corresponds to box B ax.add_patch(Rectangle((mybox_B[0][0], mybox_B[0][1]), mybox_B[1][0]-mybox_B[0][0], mybox_B[1][1]-mybox_B[0][1], edgecolor = 'pink', facecolor = 'red', alpha=0.3, fill=True, lw=3)) # to avoid distorting the mesh: ax.set_aspect('equal') # to don't print axis: ax.axis('off') # to save the figure in pdf or svg format, uncomment one of the following two lines: # plt.savefig('./myCylinderCellVolumes.pdf', dpi=fig.dpi, transparent = True, bbox_inches = 'tight') # plt.savefig('./myCylinderZomm.svg', dpi=fig.dpi, transparent = True, bbox_inches = 'tight') .. image-sg:: /auto_examples/unstructured/images/sphx_glr_plot_GetCellVolume_001.png :alt: plot GetCellVolume :srcset: /auto_examples/unstructured/images/sphx_glr_plot_GetCellVolume_001.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-script-out .. code-block:: none (np.float64(0.0), np.float64(0.03), np.float64(0.0), np.float64(0.03)) .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 26.845 seconds) .. _sphx_glr_download_auto_examples_unstructured_plot_GetCellVolume.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: plot_GetCellVolume.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: plot_GetCellVolume.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: plot_GetCellVolume.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_