How Do I Convert Nodal Coordinates Field to Local Coordinate System Using PyDPF?
Today’s Script Tip comes from Ayush Kumar, Senior Applications Engineer at Ansys.
GCS: Global Coordinate System
LCS: Local Coordinate System
In 3D simulations, local coordinate systems play a crucial role. Therefore, conversion of nodal coordinates from GCS to LCS is often needed.
The operator
:class: `rotate <ansys.dpf.core.operators.geo.rotate.rotate>`
rotates the input field in GCS as per the input rotation matrix. If the LCS is at the same origin as the GCS, only one operation using the `rotate` operator gives the desired output. But for an LCS with a transformed origin, after rotation, a transformation along the rotated position vector of the LCS’s origin is also needed to get the correct coordinates in LCS. Below is a PyDPF example to achieve that.
# Import necessary modules:
from ansys.dpf import core as dpf
from ansys.dpf.core import examples
###############################################################################
# Create a model object to establish a connection with an example result file:
model = dpf.Model(r"\Path\to\my\file.rst")
###############################################################################
# Get the property `coordinates_field` from :class: nodes ansys.dpf.core.nodes`
ncoord_f = model.metadata.meshed_region.nodes.coordinates_field
###############################################################################
# Get the rotation matrix of the LCS ID 12
# The first 9 values in the `cs` output is the rotation matrix
cs = model.operator(r"mapdl::rst::CS")
cs.inputs.cs_id.connect(12)
cs_rot_mat = cs.outputs.field.get_data().data.T[0:9]
###############################################################################
# Create a 3x3 rotation matrix field - `rot_mat_f`
rot_mat_f = dpf.fields_factory.create_scalar_field(1)
rot_mat_f.data = cs_rot_mat
###############################################################################
# Create a 3D vector field for the position vector of the LCS's origin and
# rotate the origin as per the rotation matrix of the LCS.
# The last 3 entries of `cs` output is the LCS's origin in GCS.
pos_vec = dpf.fields_factory.create_3d_vector_field(1)
pos_vec.data = cs.outputs.field.get_data().data.T[-3:]
pos_vec_rot = dpf.operators.geo.rotate(field=pos_vec,
field_rotation_matrix=rot_mat_f)
###############################################################################
# Get rotated nodal coordinates field.
ncoord_rot_f = dpf.operators.geo.rotate(field=ncoord_f,
field_rotation_matrix=rot_mat_f)
###############################################################################
# Transform rotated nodal coordinates field along rotated position vector -
# `pos_vec_rot`,
pos_vec_rot_neg_f = dpf.operators.math.scale(field=pos_vec_rot,
ponderation=-1.0)
pos_vec_rot_neg = pos_vec_rot_neg_f.outputs.field.get_data().data_as_list
nccord_translated = dpf.operators.math.add_constant(field=ncoord_rot_f,
ponderation=pos_vec_rot_neg)
###############################################################################
# Get the nodal coordinates field `ncoord_lcs_f` in LCS
ncoord_lcs_f = nccord_translated.outputs.field.get_data()
###############################################################################
# Coordinates of NID 1 in GCS
print(ncoord_f.get_entity_data_by_id(1))
###############################################################################
# Coordinates of NID 1 in LCS
print(ncoord_lcs_f.get_entity_data_by_id(1))