How to export displacement results (deformed shape) in CSV/TXT from nodes in Named Selection

Member Posts: 2
First Comment
**

How to export initial position and final displacement results (deformed shape) from my selected nodes in Named Selection and store them in text file?

Similar topics were already discussed:
https://ansyshelp.ansys.com/public/account/secured?returnurl=////////Views/Secured/corp/v242/en/act_script/act_script_examples_python_Export.html

https://discuss.ansys.com/discussion/4418/how-to-get-automatically-x-y-z-node-locations-and-corresponding-x-y-z-displacements

Best Answer

  • Member Posts: 2
    First Comment
    **
    Answer ✓

    Besides using the APDL commands, one possible way would be to add Python Code in Solution branch:

    1. def after_solve(this, analysis):
    2. # Do not edit this line
    3. import mech_dpf
    4. import Ans.DataProcessing as dpf
    5. import os
    6. mech_dpf.setExtAPI(ExtAPI)
    7.  
    8. dataSource = dpf.DataSources(analysis.ResultFileName)
    9.  
    10. ScopingOp = dpf.operators.scoping.on_named_selection()
    11. ScopingOp.inputs.named_selection_name.Connect("MyNodeSelection".upper())
    12. ScopingOp.inputs.data_sources.Connect(dataSource)
    13. MeshScoping = ScopingOp.outputs.mesh_scoping.GetData()
    14.  
    15. # Get displacements for the scoped nodes
    16. displacements = dpf.operators.result.displacement()
    17. displacements.inputs.data_sources.Connect(dataSource)
    18. displacements.inputs.mesh_scoping.Connect(MeshScoping)
    19. disp = displacements.outputs.fields_container.GetData()[0]
    20.  
    21. # Get Mesh information to retrieve node coordinates
    22. meshData = ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
    23. nodeIds = disp.ScopingIds # Get node list from results
    24.  
    25. # Create a list to store node data
    26. node_data = []
    27.  
    28. # Loop over nodes and collect data
    29. for nid in nodeIds:
    30. node = meshData.NodeById(nid) # Node data for nid
    31. nodeDisp = disp.GetEntityDataById(nid) # displacement from node nid
    32. node_data.append((nid, node.X, node.Y, node.Z, nodeDisp[0], nodeDisp[1], nodeDisp[2], node.X+nodeDisp[0], node.Y+nodeDisp[1], node.Z+nodeDisp[2]))
    33.  
    34. # Sort the node data by ascending X coordinate
    35. node_data.sort(key=lambda x: x[1])
    36.  
    37. # Write sorted data to txt file in user_files directory from project
    38. outFileName = os.path.join(analysis.WorkingDir, 'exported_disp.txt')
    39.  
    40. with open(outFileName, 'w') as f_out:
    41. f_out.write('# Node \t X\tY\tZ\tUX\tUY\tUZ\tdefX\tdefY\tdefZ\n')
    42. for data in node_data:
    43. f_out.write('\t'.join(map(str, data)) + '\n')
    44.  
    45. pass

Welcome!

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