Examples of Python Results for Mechanical — Part 2

Ansys Developer
Ansys Developer
Published in
5 min readDec 2, 2022

Today’s script tip comes from Pernelle Marone-Hitz, Lead Applications Engineer, Structural Mechanics — EMEA Field Engineering and Services at Ansys.

The Python Result object enables you to evaluate output quantities by executing an Iron-python script based on the Data Processing Framework (DPF) post-processing toolbox.

A previous post showed two examples of Python Results:

Example 1: Get the maximum over time of the total deformation.

Example 2: Get the average total deformation on all time steps.

In this new post, we will create four different Python Results of growing complexity.

Example 1: Equivalent Stress

The method here is quite simple, as we will only need to use the

dpf.operators.result.stress_von_mises() operator

We simply have to provide the following inputs:

The data sources: this is simply the result file.

The time scoping : here we will use the first result set.

The complete script is as follows:

Connect the result:

and then evaluate it. We can then verify that the plot :

matches the one obtained in a standard Mechanical Equivalent Stress Result:

Example 2: Equivalent Stress on Named Selection

Let’s imagine we have a named selection in the model to identify some specific elements. We’d like to plot the Equivalent Stress again, but only on this Named Selection:

This can be done natively in Mechanical by inserting an Equivalent Stress Result and changing the scoping to the Named Selection:

However, we’d like to do the same with a Python Result. The only thing we’ll have to modify from our code in Example 1 is limit the mesh scoping to the nodes in this named selection. This is easily done by adding these two lines of code:

zone1_mesh_region = model.GetNamedSelection('NS1')
s_eqv_op.inputs.mesh_scoping.Connect(zone1_mesh_region)

Note: all named selections defined in Mechanical will be sent to the solver with their names in capital letters. So if a named selection is called ‘my_ns’, the result file will know it as ‘MY_NS’.

The complete code is as follows:

And this is what we get:

Example 3: Scaled Equivalent Stress on Named Selection

Now we’d like to use the result created in Example 2 but also scale the values by a specified amount.

Again, this can be done natively in Mechanical through inserting a User-Defined Result scoped to the Named Selection and with the following expression: SEQV*Val where Val is the scale value.

To obtain a similar result through a Python Result, we’ll need two additional things compared to Python Result n°2:

  • Use the math.scale() operator to scale the results
  • Add a section in the detail’s view of the Python Result so that the user can define the scale value.

Let’s start with the second point. This is done by opening the Property Provider tab and by adding a property as follows:

The properties need to be reloaded:

and then we get the line where the scale value can be defined:

In the script of the Python Code, the value defined as Scale Value can be retrieved by:

this.GetCustomPropertyByPath("Group 1/Scale Value")

As for the scale operator, we just need to connect it both to the scale value and to the output field of the equivalent stress operator.

The script is as follows:

The Python Result can then be evaluated:

Example 4: Scaled Equivalent Stress on two Named Selections with two different scale values

Here we’d like to get on the same plot the following result :

  • For nodes belonging to Named Selection n°1, scale the equivalent stress result by value N1
  • For nodes belonging to Named Selection n°2, scale the equivalent stress result by value N2

This is where we get all the added value of the Python Result object as such a result cannot be obtained in Mechanical.

First, we’ll have to change the Property Provider so that the user can input two different scale values (one for NS1, one for NS2). This is easily done by adapting the code created in Example 3:

The code created for Example 3 is easily adapted so that we get two output fields:

  • Scaled stress for NS1
  • Scaled stress for NS2.

The only remaining task is to combine both fields into a unique field so that the result can be plotted on the model. The

dpf.operators.utility.merge_fields_containers()

operator is the operator we need for that.

The complete code is as follows:

The result then can be evaluated:

--

--

Ansys Developer
Ansys Developer

The Ansys Developer Team writes about a variety of topics related to physics, engineering simulation, Python, AI/ML, and other developer topics.