{"id":42320,"date":"2023-07-06T12:29:07","date_gmt":"2023-07-06T19:29:07","guid":{"rendered":"https:\/\/www.padtinc.com\/?p=42320"},"modified":"2023-10-03T16:54:36","modified_gmt":"2023-10-03T23:54:36","slug":"pyansys-tutorial-3d-files-part-1","status":"publish","type":"post","link":"https:\/\/www.padtinc.com\/2023\/07\/06\/pyansys-tutorial-3d-files-part-1\/","title":{"rendered":"PyAnsys Tutorial – Exporting Ansys Results in 3D File Formats, Part 1"},"content":{"rendered":"\n

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<\/a> 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.<\/p>\n\n\n

\n
\"PyAnsys <\/picture><\/figure><\/div>\n\n\n

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<\/a> 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<\/a> file. In later posts, we will add distortion, output to other file formats, then add a graphical user interface.<\/p>\n\n\n\n

Before we Can Export to 3D File Formats, We Have to Get Started with PyAnsys<\/h2>\n\n\n\n

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. <\/p>\n\n\n\n

If you don’t have Python installed, go to python.org<\/a>, 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” <\/p>\n\n\n

\n
\"PyAnsys <\/picture><\/figure><\/div>\n\n\n

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. <\/p>\n\n\n\n

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<\/a> 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. <\/em><\/p>\n\n\n\n

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

py -m ensurepip --upgrade<\/code><\/pre>\n\n\n\n

We are almost ready to start using PyAnsys. But first, visit the Ansys documentation for PyAnsys<\/a>. 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<\/a> and use pip to install the libraries you need. Here is what you will need for this tutorial:<\/p>\n\n\n\n

py -m pip install ansys-dpf-core\npy -m pip install ansys-dpf-post\npy -m pip install pyvista<\/code><\/pre>\n\n\n\n

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. <\/p>\n\n\n\n

Time to Use PyAnsys to Extract a VTK File of Equivalent Stress<\/h2>\n\n\n\n

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:<\/p>\n\n\n\n

    \n
  1. Setup the process<\/li>\n\n\n\n
  2. Open the file, get the mesh and stresses<\/li>\n\n\n\n
  3. Plot the stress values on the mesh<\/li>\n\n\n\n
  4. Output the contoured mesh as a VTK file<\/li>\n<\/ol>\n\n\n\n

    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. <\/p>\n\n\n\n

    1: Setup the Process<\/h3>\n\n\n\n

    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.<\/p>\n\n\n\n

    #### SECTION 1 ####<\/strong>\n#1.1: Get all modules loaded<\/strong>\nfrom ansys.dpf import post\nfrom ansys.dpf import core as dpf\nfrom ansys.dpf.core import operators as coreops\nfrom ansys.dpf.core import fields_container_factory\nfrom ansys.dpf.core.plotter import DpfPlotter\nimport pyvista as pv\nimport os\n\n#1.2: Specify variables for filenames and directory<\/strong>\n#     Make sure you change this to your directory and RST file\n#     Note that Python converst \/ to \\ on windows so you don't have to. <\/strong>\nrstFile = \"twrtest1-str.rst\"\nrstDir = \"C:\/Users\/eric.miller\/OneDrive - PADT\/Development\/pyAnsys\/twrtest\/\"\noutroot= \"twrtest1\"\n\n#1.3: Build the output file name<\/strong>\noutfname = outroot + \"-seqv-1.vtk\"\n\n#1.4: Let the user know what you have<\/strong>\nprint\n(\"==========================================================================\")\nprint (\"Starting the translation of your Ansys results to a different file format\")\nprint (\"  Input file: \", rstFile)\nprint (\"  Directory: \", rstDir)\nprint (\"  Output file: \", outfname)\nprint \n(\"-------------------------------------------------------------------------\")\n<\/code><\/pre>\n\n\n\n

    2. Open the file, get the mesh and stresses<\/h3>\n\n\n\n

    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. <\/p>\n\n\n\n

    But for now, go ahead and add this bit and make sure you can grab the stresses and mesh without an error. <\/p>\n\n\n\n

    #### SECTION 2 ####\n#2.1: Move into the directory where the file is, grab the path for later<\/strong>\nos.chdir(rstDir)\npath = os.getcwd()\n\n#2.2: Load the solution from the rst file into \"mysol\"<\/strong>\nmysol = post.load_solution(rstFile)\n\n#2.3: Load the mesh from the solution into \"mymesh\"<\/strong>\nmymesh = mysol.mesh\n\n#2.4: Frab the stresses from first solution step, then get equivilent stress<\/strong>\nprint (\"++ Getting result information from file\")\nstr1 = mysol.stress(set=1)\nrstval = str1.von_mises.result_fields_container\n\n#2.5: Make a copy of the mesh <\/strong>\n      Note: we don't need a copy now, but we will in the <\/strong>\n            next version of this script<\/strong>\ntheMesh = mymesh.deep_copy()<\/code><\/pre>\n\n\n\n

    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. <\/p>\n\n\n\n

      \n
    1. First look at the “load_solution”\n