| 1 |
""" |
| 2 |
.. _volumetric_analysis_example: |
| 3 |
|
| 4 |
Volumetric Analysis |
| 5 |
~~~~~~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
|
| 8 |
Calculate mass properties such as the volume or area of datasets |
| 9 |
""" |
| 10 |
|
| 11 |
# sphinx_gallery_thumbnail_number = 4 |
| 12 |
import numpy as np |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# %% |
| 16 |
# Computing mass properties such as the volume or area of datasets in PyVista |
| 17 |
# is quite easy using the :func:`pyvista.DataObjectFilters.compute_cell_sizes` |
| 18 |
# filter and the :attr:`pyvista.DataSet.volume` property on all PyVista meshes. |
| 19 |
# |
| 20 |
# Let's get started with a simple gridded mesh: |
| 21 |
|
| 22 |
# Load a simple example mesh |
| 23 |
dataset = examples.load_uniform() |
| 24 |
dataset.set_active_scalars('Spatial Cell Data') |
| 25 |
|
| 26 |
# %% |
| 27 |
# We can then calculate the volume of every cell in the array using the |
| 28 |
# ``.compute_cell_sizes`` filter which will add arrays to the cell data of the |
| 29 |
# mesh core the volume and area by default. |
| 30 |
|
| 31 |
# Compute volumes and areas |
| 32 |
sized = dataset.compute_cell_sizes() |
| 33 |
|
| 34 |
# Grab volumes for all cells in the mesh |
| 35 |
cell_volumes = sized.cell_data['Volume'] |
| 36 |
|
| 37 |
# %% |
| 38 |
# We can also compute the total volume of the mesh using the ``.volume`` |
| 39 |
# property: |
| 40 |
|
| 41 |
# Compute the total volume of the mesh |
| 42 |
volume = dataset.volume |
| 43 |
|
| 44 |
# %% |
| 45 |
# But what if we have a dataset that we threshold with two |
| 46 |
# volumetric bodies left over in one dataset? Take this for example: |
| 47 |
|
| 48 |
|
| 49 |
threshed = dataset.threshold_percent([0.15, 0.50], invert=True) |
| 50 |
threshed.plot(show_grid=True, cpos=[-2, 5, 3]) |
| 51 |
|
| 52 |
# %% |
| 53 |
# We could then assign a classification array for the two bodies, compute the |
| 54 |
# cell sizes, then extract the volumes of each body. Note that there is a |
| 55 |
# simpler implementation of this below in :ref:`split_vol`. |
| 56 |
|
| 57 |
# Create a classifying array to ID each body |
| 58 |
rng = dataset.get_data_range() |
| 59 |
cval = ((rng[1] - rng[0]) * 0.20) + rng[0] |
| 60 |
classifier = threshed.cell_data['Spatial Cell Data'] > cval |
| 61 |
|
| 62 |
# Compute cell volumes |
| 63 |
sizes = threshed.compute_cell_sizes() |
| 64 |
volumes = sizes.cell_data['Volume'] |
| 65 |
|
| 66 |
# Split volumes based on classifier and get the volumes |
| 67 |
idx = np.argwhere(classifier) |
| 68 |
hvol = np.sum(volumes[idx]) |
| 69 |
idx = np.argwhere(~classifier) |
| 70 |
lvol = np.sum(volumes[idx]) |
| 71 |
|
| 72 |
print(f'Low grade volume: {lvol}') |
| 73 |
print(f'High grade volume: {hvol}') |
| 74 |
print(f'Original volume: {dataset.volume}') |
| 75 |
|
| 76 |
# %% |
| 77 |
# Or better yet, you could simply extract the largest volume from your |
| 78 |
# dataset directly by passing ``'largest'`` to the ``connectivity`` and |
| 79 |
# specifying the scalar range of interest. |
| 80 |
|
| 81 |
# Grab the largest connected volume within a scalar range |
| 82 |
scalar_range = [0, 77] # Range corresponding to bottom 15% of values |
| 83 |
largest = threshed.connectivity('largest', scalar_range=scalar_range) |
| 84 |
|
| 85 |
# Get volume as numeric value |
| 86 |
large_volume = largest.volume |
| 87 |
|
| 88 |
# Display it |
| 89 |
largest.plot(show_grid=True, cpos=[-2, 5, 3], color=True) |
| 90 |
|
| 91 |
|
| 92 |
# %% |
| 93 |
# ----- |
| 94 |
# |
| 95 |
# .. _split_vol: |
| 96 |
# |
| 97 |
# Splitting Volumes |
| 98 |
# +++++++++++++++++ |
| 99 |
# |
| 100 |
# What if instead, we wanted to split all the different connected bodies / |
| 101 |
# volumes in a dataset like the one above? We could use the |
| 102 |
# :func:`pyvista.DataSetFilters.split_bodies` filter to extract all the |
| 103 |
# different connected volumes in a dataset into blocks in a |
| 104 |
# :class:`pyvista.MultiBlock` dataset. For example, lets split the thresholded |
| 105 |
# volume in the example above: |
| 106 |
|
| 107 |
# Load a simple example mesh |
| 108 |
dataset = examples.load_uniform() |
| 109 |
dataset.set_active_scalars('Spatial Cell Data') |
| 110 |
threshed = dataset.threshold_percent([0.15, 0.50], invert=True) |
| 111 |
|
| 112 |
bodies = threshed.split_bodies() |
| 113 |
|
| 114 |
for i, body in enumerate(bodies): |
| 115 |
print(f'Body {i} volume: {body.volume:.3f}') |
| 116 |
|
| 117 |
# %% |
| 118 |
|
| 119 |
|
| 120 |
bodies.plot(show_grid=True, multi_colors=True, cpos=[-2, 5, 3]) |
| 121 |
|
| 122 |
|
| 123 |
# %% |
| 124 |
# ----- |
| 125 |
# |
| 126 |
# A Real Dataset |
| 127 |
# ++++++++++++++ |
| 128 |
# |
| 129 |
# Here is a realistic training dataset of fluvial channels in the subsurface. |
| 130 |
# This will threshold the channels from the dataset then separate each |
| 131 |
# significantly large body and compute the volumes for each. |
| 132 |
# |
| 133 |
# Load up the data and threshold the channels: |
| 134 |
|
| 135 |
data = examples.load_channels() |
| 136 |
channels = data.threshold([0.9, 1.1]) |
| 137 |
|
| 138 |
# %% |
| 139 |
# Now extract all the different bodies and compute their volumes: |
| 140 |
|
| 141 |
bodies = channels.split_bodies() |
| 142 |
# Now remove all bodies with a small volume |
| 143 |
for key in bodies.keys(): |
| 144 |
b = bodies[key] |
| 145 |
vol = b.volume |
| 146 |
if vol < 1000.0: |
| 147 |
del bodies[key] |
| 148 |
continue |
| 149 |
# Now lets add a volume array to all blocks |
| 150 |
b.cell_data['TOTAL VOLUME'] = np.full(b.n_cells, vol) |
| 151 |
|
| 152 |
# %% |
| 153 |
# Print out the volumes for each body: |
| 154 |
|
| 155 |
for i, body in enumerate(bodies): |
| 156 |
print(f'Body {i:02d} volume: {body.volume:.3f}') |
| 157 |
|
| 158 |
# %% |
| 159 |
# And visualize all the different volumes: |
| 160 |
|
| 161 |
bodies.plot(scalars='TOTAL VOLUME', cmap='viridis', show_grid=True) |
| 162 |
# %% |
| 163 |
# .. tags:: filter |