Skip to main content

Script Tip Friday - Efficiently Combining MAPDL and Mechanical Scripting | 09.30.2022

This Script Tip Friday is brought to you by Pernelle Marone-Hitz, Lead Application Engineer at Ansys.

Pernelle's tip today will cover how to efficiently combine MAPDL and Mechanical scripting.

Many users have developed post-processing macros based on MAPDL command snippets in Mechanical.

The usual workflow is to have the command snippet use MAPDL to extract result data and then write the results to an output text file. These scripts work fine but it is often hard to link the Mechanical model with the content of the exported file.

In the example we’ll consider today, a model has several joints defined:

and an APDL script has been used to extract the reaction forces and moments on these joints. (Note – other methods could have been used for extracting these results, but this is not the point of this script tip).

It is not easy to relate the joint’s name in Mechanical to the element number used by the MAPDL solver, which is what the MAPDL post-processing script is usually based on.

To alleviate this problem, Mechanical automation scripting and MAPDL scripting can be combined.

The following Mechanical Automation script will browse through the connections’ folder and insert an APDL command snippet under each joint, provided that it is not in suppressed stat

connections_folder = ExtAPI.DataModel.Project.Model.Connections
joints = connections_folder.GetChildren(DataModelObjectCategory.Joint,True)
nb_joints = 0
for j in joints:       
    if j.Suppressed is False:
        nb_joints += 1
        with Transaction():
            new_snippet = j.AddCommandSnippet()
            string1 = 'joint_'+str(nb_joints) + ' = _jid \n'
            string2 = '*DIM,joint_name_'+str(nb_joints)+',string,248 \n'
            string3 = 'joint_name_'+str(nb_joints) + '(1) = \'' + j.Name + '\'\n'

new_snippet.AppendText("nb_joints = " + str(nb_joints))

Each command snippet is automatically populated with a few lines of MAPDL code to store the joint element number and the joint’s name.

joint_1 = _jid ! Retrieve element type number that is used by MAPDL solver
*DIM,joint_name_1,string,248 ! Create an MAPDL array to store joint name
joint_name_1(1) = 'Spherical - SYS-11\Surface To SYS-11\Surface' ! Retrieve joint name from Mechanical and store it in the MAPDL array

Then, for the MAPDL post-processing macro,

it is easy to loop over all the joints as their total number has been evaluated in the Mechanical automation script and stored in variable nb_joints. The Mechanical name of each joint is known and can be used in the MAPDL macro, and more specifically can be written in the exported file.

As an example, the MAPDL script below will extract reaction forces and moments on the “I” joint node for each joint in the model:

/POST1 ! Enter general post-processor

/out,outputfile,txt ! Redirect output to outputfile.txt

SET,LAST ! Consider last time step
ALLS ! Select all model
RSYS,solu ! Use solution coordinate system

*do,i,1,nb_joints !Loop over all joints in model

ESEL,S,TYPE,,joint_%i% ! Select element accounting for joint n°i
NSLE ! Select nodes attached to element
*GET,Nnumero1,NODE,,num,min ! Get “I” node of element (first node)
*GET,loc_X%i%,NODE,Nnumero1,LOC,X ! Get X location of node
*GET,loc_Y%i%,NODE,Nnumero1,LOC,Y ! Get Y location of node
*GET,loc_Z%i%,NODE,Nnumero1,LOC,Z ! Get Z location of node
*GET,staticFX%i%,elem,elnext(0),SMISC,1 ! Get FX on node
*GET,staticFY%i%,elem,elnext(0),SMISC,2 ! Get FY on node
*GET,staticFZ%i%,elem,elnext(0),SMISC,3 ! Get FZ on node
*GET,staticMX%i%,elem,elnext(0),SMISC,4 ! Get MX on node
*GET,staticMY%i%,elem,elnext(0),SMISC,5 ! Get MY on node
*GET,staticMZ%i%,elem,elnext(0),SMISC,6 ! Get MZ on node

/out,outputfile,txt,,append ! Append outputfile.txt and write a table with Joint Name in Mechanical and FX, FY, FZ, MX, MY, MZ reaction forces.



The outputfile.txt file is written in the solver’s file directory:

and can be opened in any text editor: