| 1 |
""" |
| 2 |
.. _openfoam_example: |
| 3 |
|
| 4 |
Plot OpenFOAM data |
| 5 |
~~~~~~~~~~~~~~~~~~ |
| 6 |
""" |
| 7 |
|
| 8 |
import pyvista as pv |
| 9 |
from pyvista import examples |
| 10 |
|
| 11 |
# %% |
| 12 |
# This example uses data from a lid-driven cavity flow. It is recommended to |
| 13 |
# use :class:`pyvista.POpenFOAMReader` for reading OpenFOAM files for more |
| 14 |
# control over reading data. |
| 15 |
# |
| 16 |
# This example will only run correctly in versions of vtk>=9.1.0. The names |
| 17 |
# of the patch arrays and resulting keys in the read mesh will be different |
| 18 |
# in prior versions. |
| 19 |
|
| 20 |
filename = examples.download_cavity(load=False) |
| 21 |
reader = pv.POpenFOAMReader(filename) |
| 22 |
|
| 23 |
# %% |
| 24 |
# OpenFOAM datasets include multiple sub-datasets including the internal mesh |
| 25 |
# and patches, typically boundaries. This can be inspected before reading the data. |
| 26 |
|
| 27 |
print(f'All patch names: {reader.patch_array_names}') |
| 28 |
print(f'All patch status: {reader.all_patch_arrays_status}') |
| 29 |
|
| 30 |
# %% |
| 31 |
# This data is represented as a :class:`pyvista.MultiBlock` object. |
| 32 |
# The internal mesh will be located in the top-level MultiBlock mesh. |
| 33 |
|
| 34 |
mesh = reader.read() |
| 35 |
print(f'Mesh patches: {mesh.keys()}') |
| 36 |
internal_mesh = mesh['internalMesh'] # or internal_mesh = mesh[0] |
| 37 |
|
| 38 |
# %% |
| 39 |
# In this case the internal mesh is a :class:`pyvista.UnstructuredGrid`. |
| 40 |
|
| 41 |
print(internal_mesh) |
| 42 |
|
| 43 |
# %% |
| 44 |
# Additional Patch meshes are nested inside another MultiBlock mesh. The name |
| 45 |
# of the sub-level MultiBlock mesh depends on the vtk version. |
| 46 |
|
| 47 |
boundaries = mesh['boundary'] |
| 48 |
print(boundaries) |
| 49 |
print(f'Boundaries patches: {boundaries.keys()}') |
| 50 |
print(boundaries['movingWall']) |
| 51 |
|
| 52 |
# %% |
| 53 |
# The default in OpenFOAMReader is to translate the existing cell data to point |
| 54 |
# data. Therefore, the cell data arrays are duplicated in point data. |
| 55 |
|
| 56 |
print('Cell Data:') |
| 57 |
print(internal_mesh.cell_data) |
| 58 |
print('\nPoint Data:') |
| 59 |
print(internal_mesh.point_data) |
| 60 |
|
| 61 |
# %% |
| 62 |
# This behavior can be turned off if only cell data is required. |
| 63 |
|
| 64 |
reader.cell_to_point_creation = False |
| 65 |
internal_mesh = reader.read()['internalMesh'] |
| 66 |
print('Cell Data:') |
| 67 |
print(internal_mesh.cell_data) |
| 68 |
print('\nPoint Data:') |
| 69 |
print(internal_mesh.point_data) |
| 70 |
|
| 71 |
# %% |
| 72 |
# Now we will read in all the data at the last time point. |
| 73 |
|
| 74 |
print(f'Available Time Values: {reader.time_values}') |
| 75 |
reader.set_active_time_value(2.5) |
| 76 |
reader.cell_to_point_creation = True # Need point data for streamlines |
| 77 |
mesh = reader.read() |
| 78 |
internal_mesh = mesh['internalMesh'] |
| 79 |
boundaries = mesh['boundary'] |
| 80 |
|
| 81 |
# %% |
| 82 |
# This OpenFOAM simulation is in 3D with |
| 83 |
# only 1 cell in the z-direction. First, the solution is sliced in the center |
| 84 |
# of the z-direction. |
| 85 |
# :func:`pyvista.DataSetFilters.streamlines_evenly_spaced_2D` requires the data |
| 86 |
# to lie in the z=0 plane. So, after the domain sliced, it is translated to |
| 87 |
# ``z=0``. |
| 88 |
|
| 89 |
|
| 90 |
def slice_z_center(mesh): |
| 91 |
"""Slice mesh through center in z normal direction, move to z=0.""" |
| 92 |
slice_mesh = mesh.slice(normal='z') |
| 93 |
slice_mesh.translate((0, 0, -slice_mesh.center[-1]), inplace=True) |
| 94 |
return slice_mesh |
| 95 |
|
| 96 |
|
| 97 |
slice_internal_mesh = slice_z_center(internal_mesh) |
| 98 |
slice_boundaries = pv.MultiBlock( |
| 99 |
{key: slice_z_center(boundaries[key]) for key in boundaries.keys()}, |
| 100 |
) |
| 101 |
|
| 102 |
# %% |
| 103 |
# Streamlines are generated using the point data "U". |
| 104 |
|
| 105 |
streamlines = slice_internal_mesh.streamlines_evenly_spaced_2D( |
| 106 |
vectors='U', |
| 107 |
start_position=(0.05, 0.05, 0), |
| 108 |
separating_distance=1, |
| 109 |
separating_distance_ratio=0.1, |
| 110 |
) |
| 111 |
|
| 112 |
# %% |
| 113 |
# Plot streamlines colored by velocity magnitude. Additionally, the moving |
| 114 |
# and fixed wall boundaries are plotted. |
| 115 |
|
| 116 |
pl = pv.Plotter() |
| 117 |
pl.add_mesh(slice_boundaries['movingWall'], color='red', line_width=3) |
| 118 |
pl.add_mesh(slice_boundaries['fixedWalls'], color='black', line_width=3) |
| 119 |
pl.add_mesh(streamlines.tube(radius=0.0005), scalars='U') |
| 120 |
pl.view_xy() |
| 121 |
pl.enable_parallel_projection() |
| 122 |
pl.show() |