| 1 |
""" |
| 2 |
.. _streamlines_2D_example: |
| 3 |
|
| 4 |
2D Streamlines |
| 5 |
~~~~~~~~~~~~~~ |
| 6 |
|
| 7 |
Integrate a vector field to generate streamlines on a 2D surface. |
| 8 |
""" |
| 9 |
|
| 10 |
# sphinx_gallery_thumbnail_number = 3 |
| 11 |
|
| 12 |
# %% |
| 13 |
# This example generates streamlines of flow around a cylinder in cross flow. |
| 14 |
import pyvista as pv |
| 15 |
from pyvista import examples |
| 16 |
|
| 17 |
# %% |
| 18 |
# The data is multiblock with the fluid data as the first block. |
| 19 |
# The data lies in the `xy` plane, i.e. `z=0`, with no `z` velocity. |
| 20 |
|
| 21 |
mesh = examples.download_cylinder_crossflow() |
| 22 |
fluid_mesh = mesh[0] |
| 23 |
print(fluid_mesh) |
| 24 |
|
| 25 |
# %% |
| 26 |
# The default behavior of the :func:`streamlines() |
| 27 |
# <pyvista.DataSetFilters.streamlines>` filter is to use a 3D sphere source as |
| 28 |
# the seed points. This often will not generate any seed points on the 2D |
| 29 |
# plane of interest. Instead, a single streamline can be generated using the |
| 30 |
# ``start_position`` argument. The ``surface_streamlines=True`` argument is |
| 31 |
# also needed if the dataset has nonzero normal velocity component. This is |
| 32 |
# not the case in this dataset. |
| 33 |
|
| 34 |
one_streamline = fluid_mesh.streamlines( |
| 35 |
start_position=(0.0, 0.4, 0.0), |
| 36 |
max_length=100.0, |
| 37 |
compute_vorticity=False, # vorticity already exists in dataset |
| 38 |
) |
| 39 |
|
| 40 |
clim = [0, 20] |
| 41 |
camera_position = pv.CameraPosition( |
| 42 |
position=(7, 0, 20.0), focal_point=(7, 0.0, 0.0), viewup=(0.0, 1.0, 0.0) |
| 43 |
) |
| 44 |
|
| 45 |
pl = pv.Plotter() |
| 46 |
for i in range(1, len(mesh)): |
| 47 |
pl.add_mesh(mesh[i], color='k') |
| 48 |
pl.add_mesh(one_streamline.tube(radius=0.05), scalars='vorticity_mag', clim=clim) |
| 49 |
pl.view_xy() |
| 50 |
pl.show(cpos=camera_position) |
| 51 |
|
| 52 |
# %% |
| 53 |
# To generate multiple streamlines, a line source can be used with the ``pointa`` |
| 54 |
# and ``pointb`` parameters. |
| 55 |
|
| 56 |
line_streamlines = fluid_mesh.streamlines( |
| 57 |
pointa=(0, -5, 0), |
| 58 |
pointb=(0, 5, 0), |
| 59 |
n_points=25, |
| 60 |
max_length=100.0, |
| 61 |
compute_vorticity=False, # vorticity already exists in dataset |
| 62 |
) |
| 63 |
|
| 64 |
pl = pv.Plotter() |
| 65 |
for i in range(1, len(mesh)): |
| 66 |
pl.add_mesh(mesh[i], color='k') |
| 67 |
pl.add_mesh(line_streamlines.tube(radius=0.05), scalars='vorticity_mag', clim=clim) |
| 68 |
pl.view_xy() |
| 69 |
pl.show(cpos=camera_position) |
| 70 |
|
| 71 |
# %% |
| 72 |
# The behavior immediately downstream of the cylinder is still not apparent |
| 73 |
# using streamlines at the inlet. |
| 74 |
# |
| 75 |
# Another method is to use :func:`streamlines_evenly_spaced_2D() |
| 76 |
# <pyvista.DataSetFilters.streamlines_evenly_spaced_2D>`. |
| 77 |
# This filter only works with 2D data that lies on the xy plane. This method |
| 78 |
# can quickly run of memory, so particular attention must be paid to the input |
| 79 |
# parameters. The defaults are in cell length units. |
| 80 |
|
| 81 |
line_streamlines = fluid_mesh.streamlines_evenly_spaced_2D( |
| 82 |
start_position=(4, 0.1, 0.0), |
| 83 |
separating_distance=3, |
| 84 |
separating_distance_ratio=0.2, |
| 85 |
compute_vorticity=False, # vorticity already exists in dataset |
| 86 |
) |
| 87 |
|
| 88 |
pl = pv.Plotter() |
| 89 |
for i in range(1, len(mesh)): |
| 90 |
pl.add_mesh(mesh[i], color='k') |
| 91 |
pl.add_mesh(line_streamlines.tube(radius=0.02), scalars='vorticity_mag', clim=clim) |
| 92 |
pl.view_xy() |
| 93 |
pl.show(cpos=camera_position) |
| 94 |
|
| 95 |
# %% |
| 96 |
# The streamlines are only approximately evenly spaced and capture the |
| 97 |
# vortex pair downstream of the cylinder with appropriate choice of |
| 98 |
# ``start_position``. |
| 99 |
# |
| 100 |
# .. tags:: filter |