Skip to main content

Script Tip Friday- Node Connectivity using DPF

| 05.06.2022

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)