Script Tip Friday- Node Connectivity using DPF
Welcome back to Script Tip Friday! Pernelle Marone-Hitz is a Lead Application Engineer at Ansys with incredible knowledge of scripting. Her Script Tip today answers the following questions:
Using DPF in Mechanical how can I get:
1) The ids and position of the nodes of a named selection
2)Know which elements this node is attached to (node connectivity)
"Last week, I struggled for a few hours on this topic, so maybe it would help avoid others getting a headache if they have the same question!?
First, let's use DPF to get the ids of the nodes in the named selection. The NS is a face NS in Mechanical, called "my_NS*".* When written to the .rst file the named selection names are converted to capital letters so we need to search for the NS called "MY_NS":
# Import DPF
import mech_dpf
import Ans.DataProcessing as dpf
mech_dpf.setExtAPI(ExtAPI)
#Get the data source (i.e. result file)
analysis = ExtAPI.DataModel.Project.Model.Analyses[0]
dataSource = dpf.DataSources(analysis.ResultFileName)
#Create Named Selection Operator
ns_op = dpf.operators.scoping.on_named_selection()
ns_op.inputs.data_sources.Connect(dataSource)
ns_op.inputs.requested_location.Connect('Nodal')
#Name should be in all caps
ns_op.inputs.named_selection_name.Connect('MY_NS')
# Get node numbers for nodes in named selection
mesh_data = ns_op.outputs.mesh_scoping.GetData()
mesh_data.Ids # node Ids
Then, let's grab the complete mesh through:
领英推è
# Get complete mesh
model=dpf.Model(dataSource)
mesh=model.Mesh
Now we can use the info on the mesh and on the node Ids to get information on the nodes of this named selection:
# Get information for first node in named selection
node1=mesh.NodeById(mesh_data.Ids[0])
# Get node position
print(node1.X,node1.Y,node1.Z)
Finally, for the node connectivity, we can use the NodalConnectivityPropertyField property of the mesh. One thing that is important to notice is that?the connectivity will return the index of the element in the element list, which is not equal to the element Id.?Once the index is known, the element Id can be obtained from the element list:
# Get elements attached to node
connectivities=mesh.NodalConnectivityPropertyField
elem_conn = connectivities.GetEntityDataById(node1.Id)
elem_id = mesh.Elements[elem_conn[0]].Id
print(elem_id)
Hope you enjoyed this week's Script Tip! If you are looking for past Script Tips, head over to our Ansys Structures LinkedIn page and look under "Articles".
Do you have a script tip to share? We'd love to hear from you! Comment on this post and we'll reach out to you to hear more!
team lead, leadership, FEA simulation and its automation/programming
2 å¹´Wouldn't it be more convenient and easier to get node IDs from ACT rather than dpf? Benefits: the NS doesn't need to be in the result file and the code would be shorter and simpler. Potential caveat? Thanks for these regular scripting tips!
Mechanical Engineer at Thales Netherlands B.V.
2 年Is it possible to achieve the same goal using a command snippet with the appropriate CMSEL ESLN commands? If so why wasn’t that route followed?
Mechanical Engineer
2 å¹´Nice tip! Thanks for sharing!
Digital Engineering: The Key to Asymmetric Advantage
2 å¹´Thanks, Pernelle Marone-Hitz!! General building blocks like this one really helps when writing full blown scripts.