PyAnsys Tutorial – Exporting Ansys Results in 3D File Formats, Part 1

Categories:

It is finally time to learn PyAnsys. Getting simulation results out of Ansys Mechanical and Ansys MAPDL has been an obsession of mine for many more years than I care to admit. It started when we got the first color printer on my first job and continues today when I want to 3D Print my stress results. My last effort was a series of blog posts updating how to output all sorts of 2D and 3D formats. But that 3D format part was a kludge. We needed something better for 3D file formats.

PyAnsys Tutorial, 3D Result Files, Part 1, 3D Printed Impeller

I also wanted to learn PyAnsys. Well, the two seemed like a great combination. So, with the help of PADT’s Alex Grishin, who has shared his PyAnsys adventures here as well, I got going. And, because I really don’t have time to do fun things often, after a lot of starts and stops, I have the tool I was looking for. This post will look at getting a very simple Python script going that reads a result file, plots the results to the screen, then saves a VTK file. In later posts, we will add distortion, output to other file formats, then add a graphical user interface.

Before we Can Export to 3D File Formats, We Have to Get Started with PyAnsys

The great thing about Python is that there are hundreds of libraries that do almost anything you need. The worst thing about Python is that there are hundreds of libraries that all act a bit differently. And most are written by volunteers and are free, which is also a two-edged sword. So to use one of those libraries, PyAnsys, we need to not just load Python on your machine but also a bunch of other libraries.

If you don’t have Python installed, go to python.org, then in the top menu, choose Downloads > Windows. Look for the latest release, click on that and scroll to the bottom and download the 64-bit Windows installer. It should be marked as “Recommended”

PyAnsys Tutorial, 3D Result Files, Part 1, Figure 1

Go ahead and install it. The basic package comes with a development tool called IDLE, and you can use that. Or, if you know another platform that supports Python, use what you like. PyAnsys doesn’t care.

If you are new to Python or rusty like I was, now is a good time to search the web for some simple tutorials to get you up to speed. The documentation page on python.org is a good place to start. But there are thousands of resources out there. Once you feel comfortable with Python, come back and get going with pyAnsys.

Next, we need to start installing libraries and to do that, we need a tool called “pip.” If it is not already installed, you need to add it. To do so, open a Windows Command Prompt window and type:

py -m ensurepip --upgrade

We are almost ready to start using PyAnsys. But first, visit the Ansys documentation for PyAnsys. Everything you will need from the Ansys side of things is here. Bookmark this site. Before you do anything, go through the “Getting Started” page and use pip to install the libraries you need. Here is what you will need for this tutorial:

C:\py -m pip install ansys-dpf-core
C:\py -m pip install ansys-dpf-post
C:\py -m pip install pyvista

The other thing you will need is an RST file with some displacement and stress results in it. I recommend you build a simple tower or flat-plate-with-a-hole-in-it model in Ansys Mechanical and then grab the RST file and put it in a directory where you will write your code. I’ve included the RST file from my “Tower of Test” in the zip file at the bottom of this post.

Time to Use PyAnsys to Extract a VTK File of Equivalent Stress

For this first PyAnsys script, we are going to hard code in a lot of options that will be variables in the next version. Our goal here is:

  1. Setup the process
  2. Open the file, get the mesh and stresses
  3. Plot the stress values on the mesh
  4. Output the contoured mesh as a VTK file

We will step through each bit of code for each of these steps. The zip file at the bottom of this post will have it all in one file, but you can also copy and paste as we go.

1: Setup the Process

In this section, we set stuff up and echo back the information. It’s a good idea to start with this chunk, run it, and make sure you don’t have any errors.

#### SECTION 1 ####
#1.1: Get all modules loaded
from ansys.dpf import post
from ansys.dpf import core as dpf
from ansys.dpf.core import operators as coreops
from ansys.dpf.core import fields_container_factory
from ansys.dpf.core.plotter import DpfPlotter
import pyvista as pv
import os

#1.2: Specify variables for filenames and directory
#     Make sure you change this to your directory and RST file
#     Note that Python converst / to \ on windows so you don't have to. 
rstFile = "twrtest1-str.rst"
rstDir = "C:/Users/eric.miller/OneDrive - PADT/Development/pyAnsys/twrtest/"
outroot= "twrtest1"

#1.3: Build the output file name
outfname = outroot + "-seqv-1.vtk"

#1.4: Let the user know what you have
print
("==========================================================================")
print ("Starting the translation of your Ansys results to a different file format")
print ("  Input file: ", rstFile)
print ("  Directory: ", rstDir)
print ("  Output file: ", outfname)
print 
("-------------------------------------------------------------------------")

2. Open the file, get the mesh and stresses

Opening the RST file and getting at the mesh and stress is a bit anticlimactic. You stick the solution into a solution object, then pull the stress and mesh from there. For this first case, we are hardcoding the extraction of equivalent stress from the first solution set. In the next version, we will use variables to make it more usable.

But for now, go ahead and add this bit and make sure you can grab the stresses and mesh without an error.

#### SECTION 2 ####
#2.1: Move into the directory where the file is, grab the path for later
os.chdir(rstDir)
path = os.getcwd()

#2.2: Load the solution from the rst file into "mysol"
mysol = post.load_solution(rstFile)

#2.3: Load the mesh from the solution into "mymesh"
mymesh = mysol.mesh

#2.4: Frab the stresses from first solution step, then get equivilent stress
print ("++ Getting result information from file")
str1 = mysol.stress(set=1)
rstval = str1.von_mises.result_fields_container

#2.5: Make a copy of the mesh 
      Note: we don't need a copy now, but we will in the 
            next version of this script
theMesh = mymesh.deep_copy()

If you really want to understand what is going on when you do a mysol.mesh, you should look at the PyAnsys documentation. I’ll be honest… the documentation is a bit confusing and sometimes it takes a while to figure out what is going on. To give a small example, let’s look at this getting the mesh from a solution.

  1. First look at the “load_solution”
  2. But if you look at the result object, it doesn’t tell you that .mesh is a property
  3. It is found in the DpfSolution class. This is where you can find all the things you can pull from the solution object you get when you do an = post.load_solution(rstFile)
  4. If you poke around in there, you will find how to point to almost everything in your result file.

For an “experienced” engineer like me who didn’t grow up with object-oriented programming, this can be a bit bewildering. But after poking around for a while, it starts to make sense. If not, ask someone under 40.

3. Plot the stress values on the mesh

This is even simpler. PyAnsys has done all the heavy lifting for you under the hood. You basically create a PyAnsys plot object, add the mesh and result values to that object, and plot it.

Add this to your script and run it. It should bring up a gray square window with your results right in the middle. Use the left-mouse-button to spin the model, the middle to move it around, and the right-mouse-button to zoom in and out. When you are done, close the window, and your script will exit.

#### SECTION 3 ####
#3.1: create a Pyansys plotter object first
print ("++ Making plot")
plt = DpfPlotter()

#3.2: Add the mesh and the results you pulled into the plot
plt.add_field(rstval[0],theMesh,show_edges=False)

#3.3: show the plot. There are a ton of options, for now use the basics. 
plt.show_figure(
    show_axes=True,
    parallel_projection=False,
    background="#aaaaaa"
)
PyAnsys Tutorial, 3D Result Files, Part 1, Figure 2

4. Output the contoured mesh as a VTK file

Now it is time to write out your VTK file. Again, all the hard work is done by the modules. Just like plotting, you create an object, add the mesh, results, and other options, then run it. Paste this bit in and rerun. You should get a VTK file in the same directory as you have our RST file in.

I use ParaView to look at the files and convert it to other formats. More below on that.

#### SECTION 4 ####

print ("++ Making output file")

#4.1: Create a VTK operator for exporting to VTK
vtkop = coreops.serialization.vtk_export() 

#4.2: Add the mesh to the operator
vtkop.inputs.mesh.connect(theMesh)

#4.3: Specify the output file name
vtkop.inputs.file_path.connect(outfname)

#4.4: Attach the results to the operatior
vtkop.inputs.fields1.connect(rstval)

#4.5: Run the operator, which creates the VTK file. 
#     Print the message created by the operator, just in case you got an error 
runMsg = vtkop.run()
print("Message from Output Operation: ", runMsg)

#4.6: Close out
print ("---------------------------------------------------------------------------")
print ("Done")

Here is what the VTK file looks like in ParaView:

PyAnsys Tutorial, 3D Result Files, Part 1, Figure e

Above is what it looks like in ParaView. What is cool is that once you get the results and the colors you want in ParaView, you can export to a variety of formats. Go to File > Export View and pick one of the support formats:

padt pyansys 3d result files part 1 f05

Two of these formats we will add in the next post, and one you can use Paraview to get

  • EPS, PDF, PS, SVG: Don’t get too excited. It saves a raster and not a 3D Vector version of your results. Bummer
  • GLTF: This is the most popular format for virtual reality. So if you want to view your results using the latest VR goggles, save to GLTF and drop it into your scene. We will add direct creation of this format in the next post.
  • VRML: This is an old format, but if you change the extension to .wrl, you can 3D Print in full color with the Stratasys Polyjet printers using that file through GrabCAD. We will add direct creation of this format in the next post.
  • X3D: This is the modern upgrade to VRML and is designed for viewing 3D objects on the web. I’ll update how to do this in the next post, but you can see how to do it in a post I did on 3D output a few years ago here.

Congrats, You Can Now Write Your Own Ansys Post Processor

OK, that may be an exaggeration. But the basics are there. You can grab what you need from a results file and either display it or save to a different format.

Here is a zip file with my RST, the VTK, and this first, simple script:

In the next part, we will add parameters, expose more result types, distort the mesh, and save to WRL/VRML… a format that most color 3D Printers can read.

When you are ready to move on, please continue with Part 2.

padt pyansys 3d result files part 1 f04
Categories

Get Your Ansys Products & Support from the Engineers who Contribute to this Blog.

Technical Expertise to Enable your Additive Manufacturing Success.

PADT’s Pulse Newsletter

Keep up to date on what is going on at PADT by subscribing to our newsletter.


By submitting this form, you are consenting to receive marketing emails from: . You can revoke your consent to receive emails at any time by using the SafeUnsubscribe® link, found at the bottom of every email. Emails are serviced by Constant Contact

Share this post:

Upcoming Events

09/22/2023

AIAA Rocky Mountain Section Technical Symposium 2023

09/22/2023

Experience Stratasys Truck Tour - Dallas, TX

09/21/2023

Accelerating the Energy Transition through Simulation

09/20/2023

3D Printing vs. CNC Machining - Webinar

09/13/2023

Maxwell Updates in Ansys 2023 R2 - Webinar

09/12/2023

Sandia Science & Technology Park 25th Anniversary

09/12/2023

Experience Stratasys Truck Tour - Tempe, AZ

09/08/2023

26th Annual New Mexico Flying 40 Awards

09/08/2023

New Mexico Tech Summit

09/07/2023

New Mexico Tech Summit

08/30/2023

Structures Updates in Ansys 2023 R2 (1) - Mechanical, Post & Graphics

08/23/2023

Improved Injection Molding with Additive - Webinar

08/22/2023

SPIE Optics & Photonics Exhibition 2023

08/16/2023

Fluids Updates in Ansys 2023 R2 - Webinar

08/04/2023

Experience Stratasys Truck Tour - Salt Lake City, Utah

08/01/2023

Experience Stratasys Truck Tour - Denver Colorado

07/26/2023

Solving Supply Chain Issues with Additive - Webinar

07/25/2023

Arizona Tech Leadership Golf Tournament

07/24/2023

Arizona Tech CEO Leadership Retreat

07/19/2023

System Automation & Optimization Updates in Ansys 2023 R1 - Webinar

07/13/2023

2023 AEROSPACE, AVIATION, DEFENSE AND MANUFACTURING CONFERENCE

07/12/2023

Materials Updates in Ansys Granta 2023 R1 - Webinar

06/30/2023

Turbo Expo 2023

06/29/2023

Turbo Expo 2023

06/28/2023

Turbo Expo 2023

06/28/2023

Revolutionize Packaging Design with Additive - Webinar

06/27/2023

Turbo Expo 2023

06/27/2023

2023 E-MOBILITY AND CLEAN ENERGY SUMMIT

06/26/2023

Turbo Expo 2023

06/21/2023

Optics Updates in Ansys 2023 R1 - Webinar

06/07/2023

LS-DYNA Updates in Ansys 2023 R1 - Webinar

05/31/2023

Driving Automotive Innovation with Additive - Webinar

05/24/2023

Hill Air Force Base Tech Expo

05/24/2023

Structural Updates in Ansys 2023 R1 (3) – Structural Optimization & Ex

05/23/2023

CROSSTALK 2023: Emerging Opportunities for Advanced Manufacturing Smal

05/10/2023

Signal & Power Integrity Updates in Ansys 2023 R1 - Webinar

04/26/2023

Additive Manufacturing Updates in Ansys 2023 R1 - Webinar

04/20/2023

38th Space Symposium Arizona Space Industry

More Info

04/19/2023

38th Space Symposium
Arizona Space Industry

04/19/2023

Additive Aids for Manufacturing - Webinar

04/18/2023

38th Space Symposium
Arizona Space Industry

04/17/2023

38th Space Symposium

04/13/2023

Venture Madness 2023

04/12/2023

Fluid Meshing & GPU-Solver Updates in Ansys 2023 R1 - Webinar

03/29/2023

8th Thermal and Fluids Engineering Conference

03/29/2023

Structural Updates in Ansys 2023 R1 - Composites, Fracture & MAPDL

03/28/2023

8th Thermal and Fluids Engineering Conference

03/27/2023

8th Thermal and Fluids Engineering Conference

03/26/2023

8TH Thermal and Fluids Engineering Conference

03/24/2023

Arizona BioPreneur Conference | Spring 2023

03/22/2023

2023 Arizona MedTech Conference

03/22/2023

Optimize Jigs & Fixtures with Additive - Webinar

03/15/2023

3D Design Updates in Ansys 2023 R1 - Webinar

03/08/2023

Competitive Advantages of 1D/3D Coupled Simulation - Webinar

03/01/2023

High Frequency Updates in Ansys 2023 R1 - Webinar

02/22/2023

Additive Advantages in Aerospace - Webinar

02/15/2023

Structural Updates in Ansys 2023 R1 (1) - Webinar

02/09/2023

IME 2023: MD&M | WestPack | ATX | D&M | Plastek

02/08/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

02/07/2023

IME 2023 MD&M | WestPack | ATX | D&M | Plastek

01/27/2023

Arizona Photonics Days, 2023

01/26/2023

Arizona Photonics Days, 2023

01/26/2023

TIPE 3D Printing | 2023

01/26/2023

Venture Cafe Phoenix Talent Night - Job Fari

01/26/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/25/2023

Arizona Photonics Days, 2023

01/25/2023

Building A.M.- Utah: Kickoff!

01/25/2023

TIPE 3D Printing | 2023

01/25/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

VFS 2023 Autonomous/Electric VTOL Symposium

01/24/2023

TIPE 3D Printing | 2023

01/18/2023

2023 AZ Tech Council Golf Tournament

12/21/2022

Simulation Best Practices for 5G Technology - Webinar

12/14/2022

Digital Twins Updates in Ansys 2022 R2 - Webinar

12/08/2022

Tech the Halls - AZ Tech Council Holiday Mixer

12/07/2022

Electric Vehicle and Other Infrastructure Update Panel

11/30/2022

SPEOS Updates in Ansys 2022 R2 - Webinar

11/23/2022

Simulation Best Practices for Electronics Reliability - Webinar

11/16/2022

Discovery Updates in Ansys 2022 R2

11/10/2022

VentureCafe Phoenix Panel: Venture Capital in AZ

11/08/2022

2022 GOVERNOR’S CELEBRATION OF INNOVATION AWARDS + TECH SHOWCASE

11/03/2022

VentureCafe Phoenix Panel: Angel Investment in AZ

11/02/2022

High & Low Frequency Electromagnetics Updates in Ansys 2022 R2

10/26/2022

Simulation Best Practices For Chip-Package-System Design & Development

10/20/2022

Nerdtoberfest 2022

10/19/2022

2022 Southern Arizona Tech + Business Expo

10/19/2022

LS-DYNA Updates in Ansys 2022 R2 - Webinar

10/17/2022

Experience Stratasys Truck Tour - Clearfield Utah

10/14/2022

ASU School of Manufacturing Systems and Networks - Formal Opening Cele

10/14/2022

Experience Stratasys Truck Tour - Midvale Utah

10/12/2022

Experience Stratasys Truck Tour - Littleton Colorado

10/06/2022

Fluids Updates in Ansys 2022 R2 - Webinar

10/05/2022

Experience Stratasys Truck Tour - Colorado Springs

09/29/2022

White Hat Life Science Investor Conference - 2022

09/28/2022

2022 AZBio Awards

09/28/2022

Simulation Best Practices for Rotating Machinery Design & Development

09/21/2022

ExperienceIT NM 2022

09/21/2022

Additive Updates in Ansys 2022 R2 - Webinar

09/14/2022

Rocky Mountain Life Sciences Investor & Partnering Conference

Search in PADT site

Contact Us

Most of our customers receive their support over the phone or via email. Customers who are close by can also set up a face-to-face appointment with one of our engineers.

For most locations, simply contact us: