The purpose of this post is not only showing how to use Python 3 with Abaqus, but to go much further and make you aware that you can use any library, program, or external application to interact with Abaqus.
The first question that comes to our mind reading this is: Why?
Why do I need Python 3 or other external applications to work with Abaqus?
Abaqus is a great piece of software for numerical simulations, however it is not intended to do everything. It is not the best CAD software, it is not the best either to make high-quality plots or visualize data, and we could find many more examples like these ones.
Even though Abaqus provides a very robust, powerful and well documented Python API, with which we can enhance Abaqus functionalities, it also has limitations.
1. Real scenarios
“How can I use Pandas in my Python scripts in Abaqus?”
(FYI: Pandas is a Python library for data manipulation)
“I need to read some Excel files from my Python script in Abaqus, how can I do it?”
(FYI: we can read Excel files using the Python library openpyxl)
“I am working on a machine learning model with scikit-learn and I want to run Abaqus simulations along the training process.”
These scenarios would be no problem if we could install Python modules into the built-in Python API of Abaqus. Unfortunately, this is not possible, we cannot customize the Python API environment. It is fixed for safety and stability reasons. So, we cannot install pandas, nor openpyxl, nor scikit, nor any other library.
On the other hand, the Python version of the API is 2.7, therefore, many features won’t be fully compatible, or some libraries may not be available at all.
And that’s not all!
What if we want to interact with other programming languages or applications:
“I have worked in a topology optimization algorithm for FEM, but it is in Matlab!
How can I run it in Abaqus?
Do I need to ‘translate’ it into Python 2.7???”
That’s even more tricky: how can we run Matlab code into our Python scripts???
These are just a few real situations in which the built-in Python API is not enough on its own.
2. What can we do then?
What do all these scenarios have in common?
They require additional functionalities that the Python API of Abaqus doesn’t provide.
What can we do then?
We can use external applications to ‘complement’ these missing functionalities.
However, in order to achieve this purpose, we need to establish an effective communication between both sides: the external application (grey block in the figure below) and Abaqus (purple block below).
Abaqus is one of the best FEA packages in the world, but it cannot do everything. That’s why we can extend its functionalities by means of external applications. Let’s see how to do it!
3. The key is the command line!
The most versatile solution for all these issues is to call and control Abaqus from the command line (cmd) using a Python script. This script will tell Abaqus what to do, e.g. create an FE model, solve it and post-process the results. This workflow is illustrated in the figure below:
- An external application is executed by the user (or another process).
- This external application accesses the cmd.
- Abaqus is invoked from the cmd.
- A Python script controls the operations carried out in Abaqus.
Abaqus can be controlled from an external application through the command line (cmd) thanks to the Python API. The red frame represents Abaqus infrastructure, which cannot be customized by the user (e.g. installing external libraries…)
>> abaqus cae
The abaqus command may vary if you have multiple versions installed on the same PC (e.g. abq2022, abq2021se…). Executing this command in the cmd will open Abaqus/CAE as usual.
However, we can also run the following command:
>> abaqus cae script=abq.py
Which will open Abaqus/CAE and immediately run the script abq.py.
So, as long as our external application can execute commands in the cmd, it will be able to control Abaqus!
3.1 Practical case: Python 3 application with Abaqus
Let’s see how this is implemented if our external application is a Python 3 program.
In the schematic below, we show the “architecture” to use a Python 3 environment that communicates with Abaqus through the built-in Python API. The main key of this communication is invoking the ‘abq.py’ script through the command line from an external app (in this case a Python 3 script: app.py).
Schematic to use Python 3 with Abaqus. Notice that the Python 3 environment is free to be customized by the user with any external libraries (e.g. pandas, scikit-learn, latest versions of numpy, scipy…)
Of course, the Python 3 application (app.py) has access to the command line. In general, Python 2 or 3, the command line is invoked using the system method from the os module (os.module) as shown below:
# Script: app.py # Import any libraries that we wish # Notice these modules are not available within Abaqus import pandas, openpyxl, sklearn, bokeh, torch # To execute statements in the command line we need to import os import os # The function to execute in the command line is os.system(command) os.system('abaqus cae script=abq.py') # This line will open Abaqus/CAE and execute the script 'abq.py'
The app.py script is executed from the external interpreter (Python 3) and it is able to run Abaqus through the “bridging” script abq.py, which makes use of Abaqus libraries, like abaqus, abaqusConstants and caeModules.
# Script: abq.py from abaqus import * from abaqusConstants import * from caeModules import * # Parameters length = 15. height = 20. width = 10. name = 'Box' myview = session.Viewport(name='Viewport: 1', origin=(0.0, 0.0), width=50., height=50.) myview.makeCurrent() myview.maximize() # Create new model Mdb() mymodel = mdb.models['Model-1'] # Create sketch s = mymodel.ConstrainedSketch(name='__profile__', sheetSize=200.0) s.rectangle(point1=(0.0, 0.0), point2=(length, height)) # Create part p = mymodel.Part(name=name, dimensionality=THREE_D, type=DEFORMABLE_BODY) p.BaseSolidExtrude(sketch=s, depth=width) # Save cae model mdb.saveAs(pathName=name + '.cae') # Display part on the viewport myview.setValues(displayedObject=p)
As we see, the actions performed through the abq.py script are:
- [Lines 2 to 4] To import Abaqus libraries. Remember that it is already running within Abaqus!
- [Lines 12 to 14] To create a new viewport where the part will be displayed.
- [Lines 17 & 18] To create a new model database with an empty model (‘Model-1’).
- [Lines 21 to 26] To draw a rectangle on a sketch and to extrude it to obtain a box-like part.
- [Lines 29 to 32] Finally, the cae file is saved and the part is displayed on the viewport.
In conclusion, from an external Python script, that can import and execute any other library/module available in the world, we can run Abaqus!
If you have problems or you are not very sure of how to set up a Python environment. I explain how to do it step-by-step in this video.
Why not running Abaqus from a Jupyter notebook?
You can use any library that you want in your external Python environment: pandas, tensorflow, jupyter notebooks, Qt, numpy, scipy, matplotlib…
And this is applicable to any other external application.
4. This is just the beginning
What we have covered in this post is just the tip of a huge iceberg!
We can come up with many ideas by combining the simulation capabilities of Abaqus with any other program. This concept can be used in the development of Machine Learning models, design of user interfaces (GUI) for specialized applications, and a lot more.
In my case, I exploited (and discovered) this capability during my Phd. I had developed some algorithms to generate 2D microstructures to model carbon fiber-reinforced composites. These algorithms were written in Python, but they employed some libraries that were not available in Abaqus, therefore, I couldn’t run them within the Python API of Abaqus directly.
Viper (on the left) is a user interface to generate 2D microstructures with high fiber volume fractions of arbitrary size and shape fibers. Viper is also able to build the corresponding FE model of the microstructures in Abaqus (on the right).
As we have seen, we can communicate with Abaqus from a Python 3 application, thus exploiting all the capabilities of any Python library! We don’t need to stick to the limited capabilities of the Python API of Abaqus.
But that’s not all, the most striking is that we can interact with Abaqus from any external application to append any type of functionalities.
In this way, we will be able to include Abaqus into any automated workflow where FE analyses are required.
Thanks to the Python API accessible from the command line, we can control Abaqus from any application and even from the Web!
The only requirement is that the governing application must have access to the command line. Therefore, any programming language is eligible for this purpose, CAD or design applications, and even web-based applications!
Did you already know this feature?
How would you take advantage of it in your projects?
And if you want to go deeper into the details of interacting with Abaqus from external applications, check this out.
I hope that you find these tips useful!