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

Options
Vishnu
Vishnu Member, Employee Posts: 214
Name Dropper First Anniversary Solution Developer Community of Practice Member First Comment
edited June 13 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:

Comments

  • Vishnu
    Vishnu Member, Employee Posts: 214
    Name Dropper First Anniversary Solution Developer Community of Practice Member First Comment
    edited June 13
    Options
    from ansys.dpf import core as dpf
    import pyvista as pv
    import os
    
    # Inputs
    rstpath = r"your path\file.rst"
    vtu_directory = r"D:/"
    vtu_file_name = "MyFile" # your file name
    
    # Initialize data source and model
    my_data_sources = dpf.DataSources(rstpath)
    my_model = dpf.Model(rstpath)
    
    # Get all available result names
    available_results = my_model.metadata.result_info.available_results
    result_names = [r.name for r in available_results if hasattr(dpf.operators.result, r.name)]
    #result_names = ["displacement_Y","stress_X","stress_Y"] #Either specify this or specify all results by commenting this line
    
    # Get mesh and time frequencies
    my_mesh = my_model.metadata.meshed_region
    my_time_scoping = (
        dpf.operators.metadata.time_freq_provider(data_sources=my_data_sources)
        .outputs.time_freq_support.get_data()
        .time_frequencies.data_as_list
    )
    
    # Define scoping (all nodes)
    my_scoping = dpf.Scoping()
    my_scoping.location = dpf.locations.nodal
    my_scoping.ids = my_mesh.nodes.scoping.ids
    
    # Extract result fields
    result_fields = []
    for result_name in result_names:
        result_operator = dpf.operators.result.__getattribute__(result_name)
        result_field = result_operator(
            time_scoping=my_time_scoping, data_sources=my_data_sources, mesh=my_mesh, mesh_scoping=my_scoping
        ).outputs.fields_container.get_data()[0]
        result_fields.append(result_field)
    
    # Generate VTU file
    vtu_operator = dpf.operators.serialization.vtu_export(
        directory=vtu_directory, mesh=my_mesh, base_name=vtu_file_name
    )
    for count, result_field in enumerate(result_fields):
        vtu_operator.connect(count + 3, result_field)
    
    vtu_operator.run()
    
    # Generate VTP file from VTU
    vtu_file_path = os.path.join(vtu_directory, vtu_file_name + "_T0000000-1.vtu")
    vtp_file_path = os.path.join(vtu_directory, vtu_file_name + "_surface.vtp")
    vtu = pv.read(vtu_file_path)
    vtp = vtu.extract_surface()
    vtp.save(vtp_file_path)
    
    

    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