I have a simulation that is already solved and takes a significant amount of time to run. I would like to select a Named Selection that contains a body and convert it to mesh nodes.
I am currently running the code in a Python Result block within ANSYS Mechanical.
Is there a way to extract mesh nodes from the Named Selection?
Also, if there is a more efficient method or better environment for running this code, please share your suggestions!
Philipp
def post_started(sender, analysis):# Do not edit this line
define_dpf_workflow(analysis)
import mech_dpf
import Ans.DataProcessing as dpf
import os
mech_dpf.setExtAPI(ExtAPI)
myLoadStep = 1 # 1-based index in DPF
myNamedSelection = "N_Test"
dataSource = dpf.DataSources(analysis.ResultFileName)
ScopingOp = dpf.operators.scoping.on_named_selection()
ScopingOp.inputs.named_selection_name.Connect(myNamedSelection.upper())
ScopingOp.inputs.data_sources.Connect(dataSource)
MeshScoping = ScopingOp.outputs.mesh_scoping.GetData()
# Get displacements for the scoped nodes
displacements = dpf.operators.result.displacement()
displacements.inputs.data_sources.Connect(dataSource)
displacements.inputs.mesh_scoping.Connect(MeshScoping)
displacements.inputs.time_scoping.Connect(dpf.Scoping([myLoadStep]))
disp = displacements.outputs.fields_container.GetData()[0]
# Get Mesh information to retrieve node coordinates
meshData = ExtAPI.DataModel.Project.Model.Analyses[0].MeshData
nodeIds = disp.ScopingIds # Get node list from results
# Create a list to store node data
node_data = []
# Loop over nodes and collect data
for nid in nodeIds:
node = meshData.NodeById(nid) # Node data for nid
nodeDisp = disp.GetEntityDataById(nid) # displacement from node nid
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]))
# Sort the node data by ascending X coordinate
node_data.sort(key=lambda x: x[1])
# Write sorted data to txt file in user_files directory from project
outFileName = os.path.join(analysis.WorkingDir, 'exported_disp.txt')
with open(outFileName, 'w') as f_out:
f_out.write('# Node \t X\tY\tZ\tUX\tUY\tUZ\tdefX\tdefY\tdefZ\n')
for data in node_data:
f_out.write('\t'.join(map(str, data)) + '\n')