Examples of Python Results for Mechanical

Ansys Developer
Ansys Developer
Published in
3 min readNov 11, 2022

Today’s script tip comes from Pernelle Marone-Hitz, Lead Applications Engineer 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.

In this post, we will cover 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.

Example 1: Maximum over time of the total deformation.

Please note that this result is already available natively in Mechanical: Insert a Total Deformation result and change the “Definition/By” section to “Maximum Over Time”:

In this example, we will “recode” this functionality and show that DPF can be used to create the same result. First, insert a Python Result object:

The chain of operators will be relatively simple:

The Data Source will be the result file: dataSource = dpf.DataSources(analysis.ResultFileName)

It is important to define the Time Scoping appropriately: by default, DPF will operate on the last time step only, and here we want to make sure all the time steps are considered so that for each node we get the maximum value on all the time steps. This can be achieved by checking the number of results sets and selecting them all.

For some operators such as the max_over_time_by_entity that is used here, the result will be a field container without any label. That would cause the Python Result to raise an error. To overcome this, a ‘time’ label should be added to the fields container.

Finally, to plot the results back on the model, the forward_field() method should be used to send the field to an operator to be able to plot it.

The complete script is as follows:

And we can check that the plot obtained through this method is the same as the one obtained in the standard Mechanical result:

Example 2: Average total deformation on all time steps.

Again, the chain of operators will be quite simple:

The complete script is as follows:

And the values are plotted back on the model:

--

--

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.