| 1 |
""" |
| 2 |
.. _streamlines_example: |
| 3 |
|
| 4 |
Streamlines |
| 5 |
~~~~~~~~~~~ |
| 6 |
|
| 7 |
Integrate a vector field to generate streamlines. |
| 8 |
""" |
| 9 |
|
| 10 |
# %% |
| 11 |
# This example generates streamlines of blood velocity. An isosurface of speed |
| 12 |
# provides context. The starting positions for the streamtubes were determined |
| 13 |
# by experimenting with the data. |
| 14 |
|
| 15 |
# sphinx_gallery_thumbnail_number = 3 |
| 16 |
import numpy as np |
| 17 |
import pyvista as pv |
| 18 |
from pyvista import examples |
| 19 |
|
| 20 |
# %% |
| 21 |
# Carotid |
| 22 |
# +++++++ |
| 23 |
# Download a sample dataset containing a vector field that can be integrated. |
| 24 |
|
| 25 |
mesh = examples.download_carotid() |
| 26 |
|
| 27 |
# %% |
| 28 |
# Run the stream line filtering algorithm using random seed points inside a |
| 29 |
# sphere with radius of 2.0. |
| 30 |
|
| 31 |
streamlines, src = mesh.streamlines( |
| 32 |
return_source=True, |
| 33 |
max_length=100.0, |
| 34 |
initial_step_length=2.0, |
| 35 |
terminal_speed=0.1, |
| 36 |
n_points=25, |
| 37 |
source_radius=2.0, |
| 38 |
source_center=(133.1, 116.3, 5.0), |
| 39 |
) |
| 40 |
|
| 41 |
# %% |
| 42 |
# Display the results. Please note that because this dataset's velocity field |
| 43 |
# was measured with low resolution, many streamlines travel outside the artery. |
| 44 |
|
| 45 |
pl = pv.Plotter() |
| 46 |
pl.add_mesh(mesh.outline(), color='k') |
| 47 |
pl.add_mesh(streamlines.tube(radius=0.15)) |
| 48 |
pl.add_mesh(src) |
| 49 |
pl.add_mesh(mesh.contour([160]).extract_all_edges(), color='grey', opacity=0.25) |
| 50 |
pl.camera_position = pv.CameraPosition( |
| 51 |
position=(182.0, 177.0, 50), focal_point=(139, 105, 19), viewup=(-0.2, -0.2, 1) |
| 52 |
) |
| 53 |
pl.show() |
| 54 |
|
| 55 |
|
| 56 |
# %% |
| 57 |
# Blood Vessels |
| 58 |
# +++++++++++++ |
| 59 |
# Here is another example of blood flow: |
| 60 |
|
| 61 |
mesh = examples.download_blood_vessels().cell_data_to_point_data() |
| 62 |
mesh.set_active_scalars('velocity') |
| 63 |
streamlines, src = mesh.streamlines( |
| 64 |
return_source=True, |
| 65 |
source_radius=10, |
| 66 |
source_center=(92.46, 74.37, 135.5), |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
# %% |
| 71 |
boundary = mesh.decimate_boundary().extract_all_edges() |
| 72 |
|
| 73 |
sargs = dict(vertical=True, title_font_size=16) |
| 74 |
pl = pv.Plotter() |
| 75 |
pl.add_mesh(streamlines.tube(radius=0.2), lighting=False, scalar_bar_args=sargs) |
| 76 |
pl.add_mesh(src) |
| 77 |
pl.add_mesh(boundary, color='grey', opacity=0.25) |
| 78 |
pl.camera_position = pv.CameraPosition( |
| 79 |
position=(10, 9.5, -43), focal_point=(87.0, 73.5, 123.0), viewup=(-0.5, -0.7, 0.5) |
| 80 |
) |
| 81 |
pl.show() |
| 82 |
|
| 83 |
|
| 84 |
# %% |
| 85 |
# A source mesh can also be provided using the |
| 86 |
# :func:`pyvista.DataSetFilters.streamlines_from_source` |
| 87 |
# filter, for example if an inlet surface is available. In this example, the |
| 88 |
# inlet surface is extracted just inside the domain for use as the seed for |
| 89 |
# the streamlines. |
| 90 |
|
| 91 |
source_mesh = mesh.slice('z', origin=(0, 0, 182)) # inlet surface |
| 92 |
# thin out ~40% points to get a nice density of streamlines |
| 93 |
seed_mesh = source_mesh.decimate_boundary(0.4) |
| 94 |
streamlines = mesh.streamlines_from_source(seed_mesh, integration_direction='forward') |
| 95 |
# print *only* added arrays from streamlines filter |
| 96 |
print('Added arrays from streamlines filter:') |
| 97 |
print( |
| 98 |
[ |
| 99 |
array_name |
| 100 |
for array_name in streamlines.array_names |
| 101 |
if array_name not in mesh.array_names |
| 102 |
] |
| 103 |
) |
| 104 |
|
| 105 |
# %% |
| 106 |
# Plot streamlines colored by the time along the streamlines. |
| 107 |
|
| 108 |
sargs = dict(vertical=True, title_font_size=16) |
| 109 |
pl = pv.Plotter() |
| 110 |
pl.add_mesh( |
| 111 |
streamlines.tube(radius=0.2), |
| 112 |
scalars='IntegrationTime', |
| 113 |
clim=[0, 1000], |
| 114 |
lighting=False, |
| 115 |
scalar_bar_args=sargs, |
| 116 |
) |
| 117 |
pl.add_mesh(boundary, color='grey', opacity=0.25) |
| 118 |
pl.add_mesh(source_mesh, color='red') |
| 119 |
pl.camera_position = pv.CameraPosition( |
| 120 |
position=(10, 9.5, -43), focal_point=(87.0, 73.5, 123.0), viewup=(-0.5, -0.7, 0.5) |
| 121 |
) |
| 122 |
pl.show() |
| 123 |
|
| 124 |
|
| 125 |
# %% |
| 126 |
# Kitchen |
| 127 |
# +++++++ |
| 128 |
# |
| 129 |
kpos = pv.CameraPosition( |
| 130 |
position=(-6.68, 11.9, 11.6), focal_point=(3.5, 2.5, 1.26), viewup=(0.45, -0.4, 0.8) |
| 131 |
) |
| 132 |
|
| 133 |
mesh = examples.download_kitchen() |
| 134 |
kitchen = examples.download_kitchen(split=True) |
| 135 |
|
| 136 |
# %% |
| 137 |
streamlines = mesh.streamlines(n_points=40, source_center=(0.08, 3, 0.71), max_length=200) |
| 138 |
|
| 139 |
# %% |
| 140 |
pl = pv.Plotter() |
| 141 |
pl.add_mesh(mesh.outline(), color='k') |
| 142 |
pl.add_mesh(kitchen, color=True) |
| 143 |
pl.add_mesh(streamlines.tube(radius=0.01), scalars='velocity', lighting=False) |
| 144 |
pl.camera_position = kpos |
| 145 |
pl.show() |
| 146 |
|
| 147 |
|
| 148 |
# %% |
| 149 |
# Custom 3D Vector Field |
| 150 |
# ++++++++++++++++++++++ |
| 151 |
# |
| 152 |
|
| 153 |
nx = 20 |
| 154 |
ny = 15 |
| 155 |
nz = 5 |
| 156 |
|
| 157 |
origin = (-(nx - 1) * 0.1 / 2, -(ny - 1) * 0.1 / 2, -(nz - 1) * 0.1 / 2) |
| 158 |
mesh = pv.ImageData(dimensions=(nx, ny, nz), spacing=(0.1, 0.1, 0.1), origin=origin) |
| 159 |
x = mesh.points[:, 0] |
| 160 |
y = mesh.points[:, 1] |
| 161 |
z = mesh.points[:, 2] |
| 162 |
vectors = np.empty((mesh.n_points, 3)) |
| 163 |
vectors[:, 0] = np.sin(np.pi * x) * np.cos(np.pi * y) * np.cos(np.pi * z) |
| 164 |
vectors[:, 1] = -np.cos(np.pi * x) * np.sin(np.pi * y) * np.cos(np.pi * z) |
| 165 |
vectors[:, 2] = ( |
| 166 |
np.sqrt(3.0 / 3.0) * np.cos(np.pi * x) * np.cos(np.pi * y) * np.sin(np.pi * z) |
| 167 |
) |
| 168 |
|
| 169 |
mesh['vectors'] = vectors |
| 170 |
# %% |
| 171 |
stream, src = mesh.streamlines( |
| 172 |
'vectors', |
| 173 |
return_source=True, |
| 174 |
terminal_speed=0.0, |
| 175 |
n_points=200, |
| 176 |
source_radius=0.1, |
| 177 |
) |
| 178 |
# %% |
| 179 |
cpos = pv.CameraPosition( |
| 180 |
position=(1.2, 1.2, 1.2), focal_point=(-0.0, -0.0, -0.0), viewup=(0.0, 0.0, 1.0) |
| 181 |
) |
| 182 |
stream.tube(radius=0.0015).plot(cpos=cpos) |
| 183 |
# %% |
| 184 |
# .. tags:: filter |