| 1 |
""" |
| 2 |
.. _integrate_data_example: |
| 3 |
|
| 4 |
Integrate Data |
| 5 |
~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Integrate data over a surface using the |
| 8 |
:func:`pyvista.DataSetFilters.integrate_data` filter. |
| 9 |
|
| 10 |
""" |
| 11 |
|
| 12 |
import pyvista as pv |
| 13 |
from pyvista import examples |
| 14 |
|
| 15 |
# %% |
| 16 |
# This example calculates the total flow rate and average velocity inside a |
| 17 |
# blood vessel. The boundary object is only used for plotting the shape of |
| 18 |
# the dataset geometry. The inlet surface is generated by slicing the domain. |
| 19 |
# Fluid flowing into the domain is in the negative z-direction, so |
| 20 |
# a new array, ``normal_velocity``, is created. |
| 21 |
|
| 22 |
dataset = examples.download_blood_vessels() |
| 23 |
boundary = dataset.decimate_boundary().extract_all_edges() |
| 24 |
inlet_surface = dataset.slice('z', origin=(0, 0, 182)) |
| 25 |
inlet_surface['normal_velocity'] = -1 * inlet_surface['velocity'][:, 2] |
| 26 |
|
| 27 |
# %% |
| 28 |
# The velocity in the inlet is shown. |
| 29 |
|
| 30 |
pl = pv.Plotter() |
| 31 |
pl.add_mesh(boundary, color='grey', opacity=0.25) |
| 32 |
pl.add_mesh( |
| 33 |
inlet_surface, |
| 34 |
scalars='normal_velocity', |
| 35 |
component=2, |
| 36 |
scalar_bar_args=dict(vertical=True, title_font_size=16), |
| 37 |
lighting=False, |
| 38 |
) |
| 39 |
pl.add_axes() |
| 40 |
pl.camera_position = pv.CameraPosition( |
| 41 |
position=(10, 9.5, -43), focal_point=(87.0, 73.5, 123.0), viewup=(-0.5, -0.7, 0.5) |
| 42 |
) |
| 43 |
pl.show() |
| 44 |
|
| 45 |
# %% |
| 46 |
# The total flow rate is calculated using the |
| 47 |
# :func:`pyvista.DataSetFilters.integrate_data` filter. Note that the data |
| 48 |
# is a :class:`pyvista.UnstructuredGrid` object with only 1 point and 1 cell. |
| 49 |
integrated_data = inlet_surface.integrate_data() |
| 50 |
integrated_data |
| 51 |
|
| 52 |
# %% |
| 53 |
# Each array in ``integrated_data`` stores the integrated data. |
| 54 |
integrated_data['normal_velocity'] |
| 55 |
|
| 56 |
# %% |
| 57 |
# An additional ``Area`` or ``Volume`` array is added. |
| 58 |
print(f'Original arrays: {inlet_surface.array_names}') |
| 59 |
new_arrays = [ |
| 60 |
name for name in integrated_data.array_names if name not in inlet_surface.array_names |
| 61 |
] |
| 62 |
print(f'New arrays : {new_arrays}') |
| 63 |
|
| 64 |
# %% |
| 65 |
# Display the total flow rate, area of inlet surface, and average velocity. |
| 66 |
total_flow_rate = integrated_data['normal_velocity'][0] |
| 67 |
area = integrated_data['Area'][0] |
| 68 |
average_velocity = total_flow_rate / area |
| 69 |
print(f'Total flow rate : {total_flow_rate:.1f}') |
| 70 |
print(f'Area : {area:.0f}') |
| 71 |
print(f'Average velocity: {average_velocity:.3f}') |
| 72 |
|
| 73 |
|
| 74 |
# %% |
| 75 |
# Volume Integration |
| 76 |
# ~~~~~~~~~~~~~~~~~~ |
| 77 |
# You can also integrate over a volume. Here, we effectively sum the cell and |
| 78 |
# point data across the entire volume. You can use this to compute mean values |
| 79 |
# by dividing by the volume of the dataset. |
| 80 |
# |
| 81 |
# Note that the calculated volume is the same as :attr:`pyvista.DataSet.volume`. |
| 82 |
# |
| 83 |
# Also note that the center of the dataset is the "point" of the integrated |
| 84 |
# volume. |
| 85 |
|
| 86 |
integrated_volume = dataset.integrate_data() |
| 87 |
center = integrated_volume.points[0] |
| 88 |
volume = integrated_volume['Volume'][0] |
| 89 |
mean_density = integrated_volume['density'][0] / volume |
| 90 |
mean_velocity = integrated_volume['velocity'][0] / volume |
| 91 |
|
| 92 |
print(f'Center : {center}') |
| 93 |
print(f'Volume : {volume:.0f}') |
| 94 |
print(f'Mean density : {mean_density:.4f}') |
| 95 |
print(f'Mean velocity : {mean_velocity}') |
| 96 |
# %% |
| 97 |
# .. tags:: filter |