How to convert Ansys mechanical result file (.RST) to VTU/VTP format (typically used in paraview)

Member, Employee Posts: 222
100 Comments 100 Likes Second Anniversary Name Dropper
✭✭✭✭
edited June 2024 in Structures

This code:

  1. Extracts multiple fields (displacement and stress results) from an Ansys result file (.rst), and exports them to VTU format in a specified directory using DPF module

  2. It then reads the VTU file using pyvista, extracts the surface mesh, and saves it as a VTP file.

These files can be used by SimAI

Tagged:

Welcome!

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

Comments

  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited November 2024
    1. from ansys.dpf import core as dpf
    2. import pyvista as pv
    3. import os
    4.  
    5. # Inputs
    6. rstpath = r"your path\file.rst"
    7. vtu_directory = r"D:/"
    8. vtu_file_name = "MyFile" # your file name
    9.  
    10. # Initialize data source and model
    11. my_data_sources = dpf.DataSources(rstpath)
    12. my_model = dpf.Model(rstpath)
    13.  
    14. # Get all available result names
    15. available_results = my_model.metadata.result_info.available_results
    16. result_names = [r.name for r in available_results if hasattr(dpf.operators.result, r.name)]
    17. #result_names = ["displacement_Y","stress_X","stress_Y"] #Either specify this or specify all results by commenting this line
    18.  
    19. # Get mesh and time frequencies
    20. my_mesh = my_model.metadata.meshed_region
    21. my_time_scoping = (
    22. dpf.operators.metadata.time_freq_provider(data_sources=my_data_sources)
    23. .outputs.time_freq_support.get_data()
    24. .time_frequencies.data_as_list
    25. )
    26.  
    27. # Define scoping (all nodes)
    28. my_scoping = dpf.Scoping()
    29. my_scoping.location = dpf.locations.nodal
    30. my_scoping.ids = my_mesh.nodes.scoping.ids
    31.  
    32. # Extract result fields
    33. result_fields = []
    34. for result_name in result_names:
    35. result_operator = dpf.operators.result.__getattribute__(result_name)
    36. result_field = result_operator(
    37. time_scoping=my_time_scoping, data_sources=my_data_sources, mesh=my_mesh, mesh_scoping=my_scoping
    38. ).outputs.fields_container.get_data()[0]
    39. result_fields.append(result_field)
    40.  
    41. # Generate VTU file
    42. vtu_operator = dpf.operators.serialization.vtu_export(
    43. directory=vtu_directory, mesh=my_mesh, base_name=vtu_file_name
    44. )
    45. for count, result_field in enumerate(result_fields):
    46. vtu_operator.connect(count + 3, result_field)
    47.  
    48. vtu_operator.run()
    49.  
    50. # Generate VTP file from VTU
    51. vtu_file_path = os.path.join(vtu_directory, vtu_file_name + "_T0000000-1.vtu")
    52. vtp_file_path = os.path.join(vtu_directory, vtu_file_name + "_surface.vtp")
    53. vtu = pv.read(vtu_file_path)
    54. vtp = vtu.extract_surface()
    55. vtp.save(vtp_file_path)
    56.  

    See below:
    all results available in .RST is transferred in VTU and VTP

    In case you have your own custom result file , you can create your own fields using them before writing a VTU/VTP file . Please refer below link to see how you can create your own custom field:
    https://dpf.docs.pyansys.com/version/stable/examples/00-basic/03-create_entities.html#sphx-glr-examples-00-basic-03-create-entities-py

    Also if you want to just have solid elements and their results you can filter them out like below:
    (This could be very useful for SimAI workflows, which allows only solid elements on VTU, ignoring CONTACT, SURF like elements and corresponding fields )

    1. # Filter to include only solid elements (using 'solid' as the shape type)
    2. solid_element_ids = [
    3. elem.id for elem in my_mesh.elements#assuming you already have my_mesh from previously executed codes
    4. if elem.shape == "solid"
    5. ]
    6.  
    7. # Define scoping for element ids of solid elements
    8. my_scoping = dpf.Scoping()
    9. my_scoping.location = dpf.locations.elemental
    10. my_scoping.ids = solid_element_ids
    11.  
    12. #Creating mesh from solid element scopings
    13. my_mesh = dpf.operators.mesh.from_scoping(scoping=my_scoping,mesh=my_mesh).outputs.mesh.get_data()
    14.  
    15. # Creating nodal scoping (all nodes of the solid elements only)
    16. my_scoping = dpf.Scoping()
    17. my_scoping.location = dpf.locations.nodal
    18. my_scoping.ids = my_mesh.nodes.scoping.ids
  • Member Posts: 6
    Name Dropper First Comment First Anniversary
    **

    Hello Vishnu,

    Thank you for this script, Can you provide same script for extracting modal analysis results to see all frequency?

  • Member Posts: 6
    Name Dropper First Comment First Anniversary
    **

    @Vishnu said:
    This code:

    1. Extracts multiple fields (displacement and stress results) from an Ansys result file (.rst), and exports them to VTU format in a specified directory using DPF module

    2. It then reads the VTU file using pyvista, extracts the surface mesh, and saves it as a VTP file.

    These files can be used by SimAI

    Hello Vishnu,

    Thank you for this script, Can you provide same script for extracting modal analysis results to see all frequency?

  • Member, Employee Posts: 222
    100 Comments 100 Likes Second Anniversary Name Dropper
    ✭✭✭✭
    edited October 2024

    @Harikrushna are you interested in creating a VTU/VTP for your modal analysis?
    what information would you need , just the modal frequencies? Typically the frequencies can be embedded by creating a new field

    look at the below for how you can extract natural freq using DPF:

    https://discuss.ansys.com/discussion/1416/how-do-i-retrieve-natural-frequencies-from-a-modal-analysis-using-dpf-in-mechanical

  • Member Posts: 6
    Name Dropper First Comment First Anniversary
    **

    Yes, I want to create VTU/VTP file. I want to see them in paraview.

Welcome!

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