|
| 1 | +""" |
| 2 | +.. _plot_3d_streamlines: |
| 3 | +
|
| 4 | +Compute and plot 3D streamlines |
| 5 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 6 | +This example shows you how to compute and |
| 7 | +plot streamlines of fluid simulation results, |
| 8 | +for 3D models. |
| 9 | +
|
| 10 | +""" |
| 11 | + |
| 12 | +############################################################################### |
| 13 | +# Compute and plot streamlines from single source |
| 14 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 15 | + |
| 16 | +############################################################################### |
| 17 | +# Import modules, create the data sources and the model |
| 18 | +# ----------------------------------------------------- |
| 19 | +# Import modules: |
| 20 | + |
| 21 | +from ansys.dpf import core as dpf |
| 22 | +from ansys.dpf.core import examples |
| 23 | +from ansys.dpf.core.helpers.streamlines import compute_streamlines |
| 24 | +from ansys.dpf.core.plotter import DpfPlotter |
| 25 | + |
| 26 | +############################################################################### |
| 27 | +# Create data sources for fluids simulation result: |
| 28 | +fluent_files = examples.download_fluent_mixing_elbow_steady_state() |
| 29 | +ds_fluent = dpf.DataSources() |
| 30 | +ds_fluent.set_result_file_path(fluent_files["cas"][0], "cas") |
| 31 | +ds_fluent.add_file_path(fluent_files["dat"][1], "dat") |
| 32 | + |
| 33 | +############################################################################### |
| 34 | +# Create model from fluid simulation result data sources: |
| 35 | +m_fluent = dpf.Model(ds_fluent) |
| 36 | + |
| 37 | +############################################################################### |
| 38 | +# Get meshed region and velocity data |
| 39 | +# ----------------------------------- |
| 40 | +# Meshed region is used as geometric base to compute the streamlines. |
| 41 | +# Velocity data is used to compute the streamlines. The velocity data must be nodal. |
| 42 | + |
| 43 | +############################################################################### |
| 44 | +# Get the meshed region: |
| 45 | +meshed_region = m_fluent.metadata.meshed_region |
| 46 | + |
| 47 | +############################################################################### |
| 48 | +# Get the velocity result at nodes: |
| 49 | +velocity_op = m_fluent.results.velocity() |
| 50 | +fc = velocity_op.outputs.fields_container() |
| 51 | +field = dpf.operators.averaging.to_nodal_fc(fields_container=fc).outputs.fields_container()[0] |
| 52 | + |
| 53 | +############################################################################### |
| 54 | +# Compute and plot the streamlines adjusting the request |
| 55 | +# ------------------------------------------------------ |
| 56 | +# The following steps show you how to create streamlines using DpfPlotter, with several sets |
| 57 | +# of parameters. It demonstrates the issues that can happen and the adjustments that you can make. |
| 58 | + |
| 59 | +############################################################################### |
| 60 | +# First, Streamlines and StreamlinesSource objects are created. The |
| 61 | +# StreamlinesSource is available using the 'return_source' argument. |
| 62 | +# Then, you can correctly set the source coordinates using the |
| 63 | +# "source_center" argument that moves the source center, and |
| 64 | +# "permissive" option that allows you to display the source even, if the computed |
| 65 | +# streamline size is zero. Default value for "permissive" argument is True. If permissive |
| 66 | +# is set to False, the "add_streamlines" method throws. |
| 67 | +streamline_obj, source_obj = compute_streamlines( |
| 68 | + meshed_region=meshed_region, |
| 69 | + field=field, |
| 70 | + return_source=True, |
| 71 | + source_center=(0.1, 0.1, 0.2), |
| 72 | +) |
| 73 | +pl1 = DpfPlotter() |
| 74 | +pl1.add_mesh(meshed_region=meshed_region, opacity=0.3) |
| 75 | +pl1.add_streamlines( |
| 76 | + streamlines=streamline_obj, |
| 77 | + source=source_obj, |
| 78 | + permissive=True, |
| 79 | +) |
| 80 | +pl1.show_figure(show_axes=True) |
| 81 | + |
| 82 | +############################################################################### |
| 83 | +# After the adjustment, the correct values for the "source_center" argument are set. |
| 84 | +# You can remove the "permissive" option. |
| 85 | +# You can display velocity data with a small opacity value to avoid hiding the streamlines. |
| 86 | +# More settings are added to adapt the streamlines creation to the geometry and |
| 87 | +# the data of the model: |
| 88 | +# - radius: streamlines radius |
| 89 | +# - n_points: source number of points |
| 90 | +# - source_radius |
| 91 | +# - max_time: maximum integration time of the streamline. It controls |
| 92 | +# the streamline length. |
| 93 | +streamline_obj, source_obj = compute_streamlines( |
| 94 | + meshed_region=meshed_region, |
| 95 | + field=field, |
| 96 | + return_source=True, |
| 97 | + source_center=(0.56, 0.48, 0.0), |
| 98 | + n_points=10, |
| 99 | + source_radius=0.075, |
| 100 | + max_time=10.0, |
| 101 | +) |
| 102 | +pl2 = DpfPlotter() |
| 103 | +pl2.add_field(field, meshed_region, opacity=0.2) |
| 104 | +pl2.add_streamlines( |
| 105 | + streamlines=streamline_obj, |
| 106 | + source=source_obj, |
| 107 | + radius=0.001, |
| 108 | +) |
| 109 | +pl2.show_figure(show_axes=True) |
| 110 | + |
| 111 | +############################################################################### |
| 112 | +# Compute and plot streamlines from several sources |
| 113 | +# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 114 | + |
| 115 | +############################################################################### |
| 116 | +# Get data to plot |
| 117 | +# ---------------- |
| 118 | +# Create data sources for fluid simulation result: |
| 119 | + |
| 120 | +files_cfx = examples.download_cfx_heating_coil() |
| 121 | +ds_cfx = dpf.DataSources() |
| 122 | +ds_cfx.set_result_file_path(files_cfx["cas"], "cas") |
| 123 | +ds_cfx.add_file_path(files_cfx["dat"], "dat") |
| 124 | + |
| 125 | +############################################################################### |
| 126 | +# Create model from fluid simulation result data sources: |
| 127 | +m_cfx = dpf.Model(ds_cfx) |
| 128 | + |
| 129 | +############################################################################### |
| 130 | +# Get meshed region and velocity data |
| 131 | +meshed_region = m_cfx.metadata.meshed_region |
| 132 | +velocity_op = m_cfx.results.velocity() |
| 133 | +field = velocity_op.outputs.fields_container()[0] |
| 134 | + |
| 135 | +############################################################################### |
| 136 | +# Compute streamlines from different sources |
| 137 | +# ------------------------------------------ |
| 138 | + |
| 139 | +############################################################################### |
| 140 | +# Compute streamlines from different sources: |
| 141 | +streamline_1, source_1 = compute_streamlines( |
| 142 | + meshed_region=meshed_region, |
| 143 | + field=field, |
| 144 | + return_source=True, |
| 145 | + source_radius=0.25, |
| 146 | + source_center=(0.75, 0.0, 0.0), |
| 147 | +) |
| 148 | +streamline_2, source_2 = compute_streamlines( |
| 149 | + meshed_region=meshed_region, |
| 150 | + field=field, |
| 151 | + return_source=True, |
| 152 | + source_radius=0.25, |
| 153 | + source_center=(0.0, 0.75, 0.0), |
| 154 | +) |
| 155 | +streamline_3, source_3 = compute_streamlines( |
| 156 | + meshed_region=meshed_region, |
| 157 | + field=field, |
| 158 | + return_source=True, |
| 159 | + source_radius=0.25, |
| 160 | + source_center=(-0.75, 0.0, 0.0), |
| 161 | +) |
| 162 | +streamline_4, source_4 = compute_streamlines( |
| 163 | + meshed_region=meshed_region, |
| 164 | + field=field, |
| 165 | + return_source=True, |
| 166 | + source_radius=0.25, |
| 167 | + source_center=(0.0, -0.75, 0.0), |
| 168 | +) |
| 169 | + |
| 170 | +############################################################################### |
| 171 | +# Plot streamlines from different sources |
| 172 | +# --------------------------------------- |
| 173 | + |
| 174 | +pl = DpfPlotter() |
| 175 | +pl.add_field(field, meshed_region, opacity=0.2) |
| 176 | +pl.add_streamlines( |
| 177 | + streamlines=streamline_1, |
| 178 | + source=source_1, |
| 179 | + radius=0.007, |
| 180 | +) |
| 181 | +pl.add_streamlines( |
| 182 | + streamlines=streamline_2, |
| 183 | + source=source_2, |
| 184 | + radius=0.007, |
| 185 | +) |
| 186 | +pl.add_streamlines( |
| 187 | + streamlines=streamline_3, |
| 188 | + source=source_3, |
| 189 | + radius=0.007, |
| 190 | +) |
| 191 | +pl.add_streamlines( |
| 192 | + streamlines=streamline_4, |
| 193 | + source=source_4, |
| 194 | + radius=0.007, |
| 195 | +) |
| 196 | +pl.show_figure(show_axes=True) |
0 commit comments