|
| 1 | +# Copyright (C) 2023 - 2024 ANSYS, Inc. and/or its affiliates. |
| 2 | +# SPDX-License-Identifier: MIT |
| 3 | +# |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +""" |
| 24 | +.. _ref_postprocess_using_meshobjects: |
| 25 | +
|
| 26 | +==================================================================== |
| 27 | +Postprocessing simulation results using the ``MeshObjectPlot`` class |
| 28 | +==================================================================== |
| 29 | +
|
| 30 | +The Visualization Interface Tool provides the ``MeshObject`` helper class to relate a custom object. |
| 31 | +With a custom object, you can take advantage of the full potential of the Visualization Interface Tool. |
| 32 | +
|
| 33 | +This example shows how to use the ``MeshObjectPlot`` class to plot your custom objects with scalar data on mesh. |
| 34 | +
|
| 35 | +""" |
| 36 | + |
| 37 | +################################################### |
| 38 | +# Necessary imports |
| 39 | +# ================= |
| 40 | + |
| 41 | + |
| 42 | +from ansys.fluent.core import examples |
| 43 | +import pyvista as pv |
| 44 | + |
| 45 | +from ansys.tools.visualization_interface.backends.pyvista import PyVistaBackend |
| 46 | +from ansys.tools.visualization_interface import MeshObjectPlot, Plotter |
| 47 | + |
| 48 | +################################################### |
| 49 | +# Download the VTK file |
| 50 | +# ===================== |
| 51 | +# |
| 52 | +# A VTK dataset can be produced utilizing `PyDPF <https://dpf.docs.pyansys.com/version/stable/>`_ |
| 53 | +# for Ansys Flagship products simulations results file format. |
| 54 | + |
| 55 | +mixing_elbow_file_src = examples.download_file("mixing_elbow.vtk", "result_files/fluent-mixing_elbow_steady-state") |
| 56 | + |
| 57 | +################################################### |
| 58 | +# Define a custom object class |
| 59 | +# ============================ |
| 60 | +# |
| 61 | +# Note that the ``CustomObject`` class must have a way to get the mesh |
| 62 | +# and a name or ID. |
| 63 | + |
| 64 | + |
| 65 | +class CustomObject: |
| 66 | + def __init__(self): |
| 67 | + self.name = "CustomObject" |
| 68 | + self.mesh = pv.read(mixing_elbow_file_src) |
| 69 | + |
| 70 | + def get_mesh(self): |
| 71 | + return self.mesh |
| 72 | + |
| 73 | + def get_field_array_info(self): |
| 74 | + return self.mesh.array_names |
| 75 | + |
| 76 | + def name(self): |
| 77 | + return self.name |
| 78 | + |
| 79 | + |
| 80 | +# Create a custom object |
| 81 | +custom_vtk = CustomObject() |
| 82 | + |
| 83 | +###################################### |
| 84 | +# Create a ``MeshObjectPlot`` instance |
| 85 | +# ==================================== |
| 86 | + |
| 87 | +mesh_object = MeshObjectPlot(custom_vtk, custom_vtk.get_mesh()) |
| 88 | + |
| 89 | +# Define the camera position |
| 90 | +cpos = ( |
| 91 | + (-0.3331763564757694, 0.08802797061044923, -1.055269197114142), |
| 92 | + (0.08813476356878325, -0.03975174212669032, -0.012819952697089087), |
| 93 | + (0.045604530283921085, 0.9935979348314435, 0.10336039239608838), |
| 94 | +) |
| 95 | + |
| 96 | +###################################### |
| 97 | +# Get the available field data arrays |
| 98 | +# ==================================== |
| 99 | + |
| 100 | +field_data_arrays = custom_vtk.get_field_array_info() |
| 101 | +print(f"Field data arrays: {field_data_arrays}") |
| 102 | + |
| 103 | +######################################################################## |
| 104 | +# Plot the ``MeshObjectPlot`` instance with mesh object & field data (0) |
| 105 | +# ====================================================================== |
| 106 | + |
| 107 | +pv_backend = PyVistaBackend() |
| 108 | +pl = Plotter(backend=pv_backend) |
| 109 | +pl.plot( |
| 110 | + mesh_object, |
| 111 | + scalars=field_data_arrays[0], |
| 112 | + show_edges=True, |
| 113 | + show_scalar_bar=True, |
| 114 | +) |
| 115 | +pl._backend.pv_interface.scene.camera_position = cpos |
| 116 | +pl.show() |
| 117 | + |
| 118 | + |
| 119 | +############################################################################## |
| 120 | +# Plot the ``MeshObjectPlot`` instance with mesh object & other field data (1) |
| 121 | +# ============================================================================ |
| 122 | + |
| 123 | +pv_backend = PyVistaBackend() |
| 124 | +pl = Plotter(backend=pv_backend) |
| 125 | +pl.plot( |
| 126 | + mesh_object, |
| 127 | + scalars=field_data_arrays[1], |
| 128 | + show_edges=True, |
| 129 | + show_scalar_bar=True, |
| 130 | +) |
| 131 | +pl._backend.pv_interface.scene.camera_position = cpos |
| 132 | +pl.show() |
0 commit comments