Plot field Vectors on a Named Selection in PyDPF

Member, Employee Posts: 12
10 Comments First Anniversary Name Dropper Photogenic
✭✭✭

How to plot field vectors on a named selection in PyDPF?

Tagged:

Comments

  • Member, Employee Posts: 12
    10 Comments First Anniversary Name Dropper Photogenic
    ✭✭✭
    edited July 2023

    For generating a vector plot on a named selection in PyDPF, some functions of the PyVista Python library are needed. The example presented below is a combination of the PyDPF and PyVista open-source codes. Here is an example of working with the Heat Flux field:

    1. from ansys.dpf import core as dpf
    2. from ansys.dpf.core.plotter import DpfPlotter
    3. from ansys.dpf.core.common import locations
    4.  
    5. import pyvista as pv
    6. import numpy as np
    7.  
    8. """
    9. Load result file and create model database
    10. """
    11.  
    12. path = "my_Path"
    13.  
    14. ds = dpf.DataSources(path)
    15. model = dpf.Model(ds)
    16.  
    17. mesh = model.metadata.meshed_region
    18.  
    19. """
    20. Select preferable Named Selection
    21. """
    22.  
    23. get_NSs = model.metadata.available_named_selections
    24. my_mesh_scoping = model.metadata.named_selection("my_Named_Selection")
    25.  
    26. """
    27. Scope Named Selection to corresponding Mesh
    28. """
    29.  
    30. scoping_op = dpf.operators.mesh.from_scoping()
    31. scoping_op.inputs.scoping.connect(my_mesh_scoping)
    32. scoping_op.inputs.mesh.connect(mesh)
    33.  
    34. my_mesh = scoping_op.outputs.mesh()
    35.  
    36. """
    37. Get Heat Flux results at last time step
    38. """
    39.  
    40. get_all_heatFlux = model.results.heat_flux.on_all_time_freqs
    41. get_fieldContents_heatFlux = get_all_heatFlux(mesh_scoping=my_mesh_scoping).eval()
    42.  
    43. get_field_heatFlux = get_fieldContents_heatFlux[-1]
    44.  
    45. """
    46. Recreate DpfPlotter() and Contour Plot based on PyDPF Source Code and PyVista library
    47. """
    48.  
    49. sargs = dict(
    50. title = "Heat Flux [W/m^2]",
    51. title_font_size = 30,
    52. label_font_size = 20,
    53. interactive = True
    54. )
    55.  
    56. margs = dict(
    57. show_edges = True,
    58. nan_color = None,
    59. )
    60.  
    61. field = get_field_heatFlux
    62. meshed_region = my_mesh
    63. location = field.location
    64.  
    65. show_max = False
    66. show_min = False
    67.  
    68.  
    69. mesh_location = meshed_region.nodes
    70. component_count = field.component_count
    71. overall_data = np.full((len(mesh_location), component_count), np.nan)
    72.  
    73. ind, mask = mesh_location.map_scoping(field.scoping)
    74. overall_data[ind] = field.data[mask]
    75.  
    76. deform_by = None
    77. scale_factor=1.0
    78. as_linear=True
    79.  
    80. grid = meshed_region.grid
    81. grid.set_active_scalars(None)
    82.  
    83. p = pv.Plotter()
    84. p.add_mesh(mesh=grid, scalars=overall_data, **margs, scalar_bar_args=sargs)
    85. p.add_axes()
    86. p.show()
    87.  
    88. """
    89. Vector Plot
    90. """
    91.  
    92. scaling = 1e-7
    93. grid["my_vectors"] = overall_data * scaling
    94. grid.set_active_vectors("my_vectors")
    95.  
    96. vtitle = "Vector Magnitude"
    97. vargs = dict(title = vtitle)
    98.  
    99. p = pv.Plotter()
    100. p.add_mesh(grid.arrows, lighting=False, scalar_bar_args=vargs)
    101. p.add_mesh(grid, scalars=overall_data, **margs, scalar_bar_args=sargs)
    102. p.remove_scalar_bar(title=vtitle)
    103. p.add_axes()
    104. p.show()

Welcome!

It looks like you're new here. Sign in or register to get started.