Debian Codegraph @

examples/99-advanced/openfoam_tubes.py

raw
1 """
2 .. _openfoam_tubes_example:
3
4 Plot CFD Data
5 -------------
6 Plot a CFD example from OpenFoam hosted on the public SimScale examples at
7 `SimScale Project Library <https://www.simscale.com/projects/>`_.
8
9 This example dataset was read using the :class:`pyvista.POpenFOAMReader`. See
10 :ref:`openfoam_example` for a full example using this reader.
11
12 """
13
14 import numpy as np
15 import pyvista as pv
16 from pyvista import examples
17
18 # %%
19 # Download and load the example dataset.
20
21 block = examples.download_openfoam_tubes()
22 block
23
24
25 # %%
26 # Plot Cross Section
27 # ~~~~~~~~~~~~~~~~~~
28 # Plot the outline of the dataset along with a cross section of the flow velocity.
29
30 # first, get the first block representing the air within the tube.
31 air = block[0]
32
33 # generate a slice in the XZ plane
34 y_slice = air.slice('y')
35
36 pl = pv.Plotter()
37 pl.add_mesh(
38 y_slice, scalars='U', lighting=False, scalar_bar_args={'title': 'Flow Velocity'}
39 )
40 pl.add_mesh(air, color='w', opacity=0.25)
41 pl.enable_anti_aliasing()
42 pl.show()
43
44
45 # %%
46 # Plot Streamlines - Flow Velocity
47 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
48 # Generate streamlines using :func:`streamlines_from_source()
49 # <pyvista.DataSetFilters.streamlines_from_source>`.
50
51 # Let's use the inlet as a source. First plot it.
52 inlet = block[1][2]
53 pl = pv.Plotter()
54 pl.add_mesh(inlet, color='b', label='inlet')
55 pl.add_mesh(air, opacity=0.2, color='w', label='air')
56 pl.enable_anti_aliasing()
57 pl.add_legend(face=None)
58 pl.show()
59
60
61 # %%
62 # Now, actually generate the streamlines. Since the original inlet contains
63 # 1000 points, let's reduce this to around 200 points by using every 5th point.
64 #
65 # .. note::
66 # If we wanted a uniform subsampling of the inlet, we could use
67 # `pyvista/pyacvd <https://github.com/pyvista/pyacvd>`_
68
69 pset = pv.PointSet(inlet.points[::5])
70 lines = air.streamlines_from_source(
71 pset,
72 vectors='U',
73 max_length=1.0,
74 )
75
76 pl = pv.Plotter()
77 pl.add_mesh(
78 lines,
79 render_lines_as_tubes=True,
80 line_width=3,
81 lighting=False,
82 scalar_bar_args={'title': 'Flow Velocity'},
83 scalars='U',
84 rng=(0, 212),
85 )
86 pl.add_mesh(air, color='w', opacity=0.25)
87 pl.enable_anti_aliasing()
88 pl.camera_position = 'xz'
89 pl.show()
90
91
92 # %%
93 # Volumetric Plot - Visualize Turbulent Kinematic Viscosity
94 # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
95 # The turbulent kinematic viscosity of a fluid is a derived quantity used in
96 # turbulence modeling to describe the effect of turbulent motion on the
97 # momentum transport within the fluid.
98 #
99 # For this example, we will first sample the results from the
100 # :class:`pyvista.UnstructuredGrid` onto a :class:`pyvista.ImageData` using
101 # :func:`sample() <pyvista.DataObjectFilters.sample>`. This is so we can visualize
102 # it using :func:`add_volume() <pyvista.Plotter.add_volume>`
103
104 # sphinx_gallery_start_ignore
105 # volume rendering does not work in interactive plots currently
106 PYVISTA_GALLERY_FORCE_STATIC = True
107 # sphinx_gallery_end_ignore
108
109 bounds = np.array(air.bounds) * 1.2
110 origin = (bounds[0], bounds[2], bounds[4])
111 spacing = (0.003, 0.003, 0.003)
112 dimensions = (
113 int((bounds[1] - bounds[0]) // spacing[0] + 2),
114 int((bounds[3] - bounds[2]) // spacing[1] + 2),
115 int((bounds[5] - bounds[4]) // spacing[2] + 2),
116 )
117 grid = pv.ImageData(dimensions=dimensions, spacing=spacing, origin=origin)
118 grid = grid.sample(air)
119
120 pl = pv.Plotter()
121 vol = pl.add_volume(
122 grid,
123 scalars='nut',
124 opacity='linear',
125 scalar_bar_args={'title': 'Turbulent Kinematic Viscosity'},
126 )
127 vol.prop.interpolation_type = 'linear'
128 pl.add_mesh(air, color='w', opacity=0.1)
129 pl.camera_position = 'xz'
130 pl.show()