Export results to VTK format

Sven Lohmann
Sven Lohmann Member Posts: 3
Name Dropper First Comment
**
edited June 2023 in Structures

I am trying to export each time step of my analysis (available as rst file) into the VTK format using a python script and ansys.dpf.core. I don't want to save the whole model, but only a selection of elements, which I have as named_selection. This seems to work in general with my script (see below).

The problem I have is that the stress tensor is not written for some reason. It doesn't matter if I write it into fields1 or fields2. I would also like to export other results (e.g. total strain) but the operator only has two variables for fields. Is there a way to save more than two results?

Finally, I would like to assign a name to the results. At the moment the result of the displacement is called e.g. "3_displacement_1.s_()" in the first time step, "3_displacement_2.s_()" in the second time step and "3_displacement_3.s_() in the third time step. Can I rename the result so that the displacement in each time step is called "displacement" or "displ"?

Here is my code so far:

from ansys.dpf import core
from ansys.dpf.core import operators as ops

rst = "model.rst"
model = core.Model(rst)


mesh_scoping = core.mesh_scoping_factory.named_selection_scoping("myElements", model=model)
mesh_op = core.operators.mesh.from_scoping()
mesh_op.inputs.mesh.connect(model.metadata.meshed_region)
mesh_op.inputs.scoping.connect(mesh_scoping)


for time in range(1, model.metadata.time_freq_support.n_sets+1):
    results = model.results
    displacement = results.displacement(time_scoping = time)
    displ_vector = displacement.outputs.fields_container()
    stress = results.stress(time_scoping = time)
    stress_tensor = stress.outputs.fields_container()   # This is somehow not exported
    
    op_export = core.Operator("vtk_export")
    op_export.inputs.file_path.connect(f"export_{time}.vtk")
    op_export.inputs.mesh.connect(mesh_op)
    op_export.inputs.fields1.connect(displ_vector)
    op_export.inputs.fields2.connect(stress_tensor)
    op_export.run()


Speaking of which: Can i format the code in the post so that it looks pretty?

Tagged:

Best Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    Hi again @Sven Lohmann , to answer your other questions:

    Can you please check that your result file indeed contains stress results ? You can print this to the console through print(model). On my side I see that the stress is saved to the .rst file:

    and I can check the stress data through:

    stress = model.results.stress()
    data = stress.outputs.fields_container.get_data()
    print(data)
    

    Here is an example of exporting to vtk:

    from ansys.dpf import core as dpf 
    my_file_path = r'D:/test/export/file.rst'
    my_data_sources = dpf.DataSources(my_file_path)
    my_model = dpf.Model(my_data_sources)
    my_mesh = my_model.metadata.meshed_region 
    u_op = my_model.results.displacement()
    my_export = dpf.operators.serialization.vtk_export()
    my_export.inputs.file_path.connect(r'D:/test/export/fileNew.vtk')
    my_export.inputs.fields1.connect(u_op.outputs.fields_container)
    my_export.inputs.mesh.connect(my_mesh)
    my_export.run()
    

    The vtk_export operator only allows two fields (https://dpf.docs.pyansys.com/api/ansys.dpf.core.operators.serialization.vtk_export.html?highlight=vtk#module-ansys.dpf.core.operators.serialization.vtk_export). The way to go here will be to export one vtk file per result that you want to export.

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    Answer ✓

    Hi @Sven Lohmann , after checking with the dev team they confirmed that vtk_export() does not support ElementalNodal results (as you have experienced). However you can look into vtu_export that should be able to do that.

Answers

  • Pernelle Marone-Hitz
    Pernelle Marone-Hitz Member, Moderator, Employee Posts: 871
    100 Answers 500 Comments 250 Likes First Anniversary
    ✭✭✭✭
    edited January 2023

    Hi @Sven Lohmann , this post should help you for the last part of your question: https://discuss.ansys.com/discussion/1887/how-do-i-use-the-text-editor#latest

    Please give us a little bit more time to address the technical question, we'll get back to you shortly!

  • James Derrick
    James Derrick Administrator, Employee Posts: 269
    Ancient Membership 100 Comments 100 Likes 25 Answers
    admin

    Hi @Sven Lohmann, welcome to the forum! @Pernelle Marone-Hitz is correct concerning the code formatting. I've edited your post for now, to change the code to a code block, although I think you still need to fix the indentation to get the code highlighting to work properly.


    Additionally, you should have a look at the posts in the "Miscellaneous" category as there is a lot of guidance there around how to get the most out of the forum. 😊

  • Sven Lohmann
    Sven Lohmann Member Posts: 3
    Name Dropper First Comment
    **
    edited January 2023

    Hi @James Derrick

    Thanks for adjusting the code. I have tweaked the code block further, however, it seems that it is not formatted properly until the comments are removed completely. After a #, all lines that come after that seem to be interpreted as comments.

    I will have a look in the misc section.

    Hi @Pernelle Marone-Hitz

    Thanks for the help. My rst file contains stress as ElementalNodal Stress:

    DPF Model
    ------------------------------
    Static analysis
    Unit system: SI: m, kg, N, s, V, A, K
    Physics Type: Mecanic
    Available results:
         -  displacement: Nodal Displacement
         -  reaction_force: Nodal Force   
         -  element_nodal_forces: ElementalNodal Element nodal Forces
         -  stress: ElementalNodal Stress 
         -  elemental_volume: Elemental Volume
         -  stiffness_matrix_energy: Elemental Energy-stiffness matrix
         -  artificial_hourglass_energy: Elemental Hourglass Energy
         -  thermal_dissipation_energy: Elemental thermal dissipation energy
         -  kinetic_energy: Elemental Kinetic Energy
         -  co_energy: Elemental co-energy
         -  incremental_energy: Elemental incremental energy
         -  elastic_strain: ElementalNodal Strain
         -  thermal_strain: ElementalNodal Thermal Strains
         -  thermal_strains_eqv: ElementalNodal Thermal Strains eqv
         -  swelling_strains: ElementalNodal Swelling Strains
         -  structural_temperature: ElementalNodal Temperature
    ------------------------------
    DPF  Meshed Region: 
      3111730 nodes 
      868247 elements 
      Unit:  
      With solid (3D) elements, shell (2D) elements, shell (3D) elements, beam (1D) elements
    ------------------------------
    DPF  Time/Freq Support: 
      Number of sets: 3 
    Cumulative     Time (s)       LoadStep       Substep         
    1              1.000000       1              1               
    2              2.000000       2              1               
    3              3.000000       3              1               
    

    And the output also says that data would be available:

    >>> print(stress_tensor)
    DPF stress(s)Fields Container
      with 1 field(s)
      defined on labels: time 
    
    
      with:
      - field 0 {time:  1} with ElementalNodal location, 6 components and 794853 entities.
    

    The output differs only between that of displacement in the location (ElementalNodal instead of Nodal):

    >>> print(displ_vector)
    DPF displacement(s)Fields Container
      with 1 field(s)
      defined on labels: time 
    
    
      with:
      - field 0 {time:  1} with Nodal location, 3 components and 3111730 entities.
    

    Is it possible that VTK cannot store ElementalNodal information? Do they have to be converted to Nodal first?

    EDIT:

    Indeed, it seems that ElementalNodal results are not written to VTK. I have converted the result to Nodal with:

    stress = results.stress(time_scoping = time)
    stress_tensor = stress.outputs.fields_container()
    stress_tensor_nodal = core.operators.averaging.elemental_nodal_to_nodal(
        field = stress_tensor,
    ).eval()
    op_export.inputs.fields2.connect(stress_tensor_nodal)
    

    and now it works.

  • Sven Lohmann
    Sven Lohmann Member Posts: 3
    Name Dropper First Comment
    **

    Thank you very much, i will have a closer look.