Abaqus provides a very powerful Python API with hundreds of dedicated methods and classes to implement any workflow through scripts. However, the number of external Python libraries included is limited, and we may need some more in our workflows. The question is how can we install Python packages in Abaqus.
From Abaqus 2024, with the introduction of Python 3.10, we can now intall the latest external packages like Pandas or even Tensorflow!
However, installing Python packages in Abaqus is not straight forward, that’s why I want to show you how to do it in this post.
1. Which Python packages are already included
The first question that we want to answer is:
“Which Python packages are already available by default in Abaqus?”
Let’s answer this question by asking Abaqus. Open the command line in Windows (‘cmd’) and execute the following:
- cmd
>> abaqus python -m pydoc modules > abqModules.txt
A text file (abqModules.txt) will be generated including all the Python modules available inside Abaqus. Among these modules you’ll find the most common ones like Numpy, Scipy, Matplotlib… And others like zipfile, tkinter or even turtle!
The table below summarizes the upgrades of the most popular packages in Engineering and Data Science between Abaqus versions:
Abaqus 2023 | Abaqus 2024 | |
---|---|---|
Python | 2.7 | 3.10 |
Numpy | 1.15 | 1.22 |
Scipy | 1.2 | 1.11 |
Matplotlib | 2.2 | 3.7 |
If the package/module that you need is in the list generated with the command shown before, then perfect! You can import it already from your scripts.
Otherwise, you can install it. Keep reading to discover how.
2. How to install Python packages in Abaqus
To install external Python packages in Abaqus, we will use the package manager called pip. Unfortunately, Pip is not installed in the Python 3 environment of Abaqus 2024, we will have to install it ourselves.
To install Pip, run the following command in the cmd:
- cmd
>> abaqus python -m ensurepip
After a few seconds, pip and the auxiliary package setuptools will be installed!!!
Pip commands
Now, we can run pip commands from the cmd. For example, we can get a reduced list of the Python packages and versions with:
- cmd
>> abaqus python -m pip list
When I ran it in Abaqus 2024 Learning Edition, I got the following:
- cmd
Package Version
--------------- -------
2to3 1.0
contourpy 1.1.0
cycler 0.11.0
fonttools 4.42.0
kiwisolver 1.4.4
matplotlib 3.7.2
numpy 1.22.4
packaging 23.1
Pillow 10.1.0
pip 22.0.4
pyparsing 3.0.9
python-dateutil 2.8.2
pywin32 304
scipy 1.11.1
setuptools 58.1.0
six 1.16.0
sympy 1.12
To print out the documentation of pip, with the options and arguments together with a brief description, run pip help:
- cmd
>> abaqus python -m pip help
The most important pip command that we want to use is pip install, to install new packages in the Python environment of Abaqus:
- cmd
>> abaqus python -m pip install
Installing Pandas with Pip
Reading Excel files from our Python scripts in Abaqus has become an important need nowadays. However, to simplify this process in Python, we can use the external package: Pandas.
Pandas is a Python package that provides data analysis tools and methods for manipulating numerical tables in different formats, like Excel sheets.
Using Pandas from Abaqus is now possible! We have to install it with the following commands in the command line:
- cmd
>> abaqus python -m pip install pandas openpyxl
Lovely! Pandas is installed. Let’s try it! (openpyxl is required)
Open Abaqus CAE and run the following Python script to export data into an Excel file using Pandas.
- Python
""" Testing Pandas
In Abaqus CAE go to: File -> Run script...
"""
# Import pandas package
import pandas as pd
# Create some synthetic data for our table
df = pd.DataFrame(
{
"A": 1.0,
"B": pd.date_range("20130101", periods=4),
"C": pd.Series(list(range(4)), dtype="float32"),
"D": [3] * 4,
"E": pd.Categorical(["test", "train", "test", "train"]),
"F": "foo",
}
)
# Print out the first rows of the table
print(df.head())
# Export table to Excel file
df.to_excel('output_from_abaqus.xlsx')
AMAZING! Pandas is running inside Abaqus!
I have made a few tests using dataframes and they work seamlessly even in combination with Matplotlib.
Installing Tensorflow with Pip
Integrating Machine Learning models into our FEA workflow in Abaqus is also a bit easier because we can run Tensorflow natively.
Tensorflow 2.10 is comp[atible with Abaqus, we can install with Pip like this:
- cmd
>> abaqus python -m pip install "tensorflow<2.11"
Let’s evaluate Tensorflow by modeling, training and testing the MNIST dataset (more details here):
- Python
""" Testing tensorflow
In Abaqus CAE go to: File -> Run script...
"""
import tensorflow as tf
# Print tensorflow version
print("TensorFlow version:", tf.__version__)
# Load dataset
mnist = tf.keras.datasets.mnist
# Split and normalize dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
# Build multilayer Neural Network model in tf
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
# Define loss function
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
# Compile NN model
model.compile(optimizer='adam', loss=loss_fn, metrics=['accuracy'])
# Train model
model.fit(x_train, y_train, epochs=5)
# Test model
model.evaluate(x_test, y_test, verbose=2)
probability_model = tf.keras.Sequential([model, tf.keras.layers.Softmax()])
probability_model(x_test[:5])
More Python packages
I have explored and installed succesfully a few more Python packages that can complement Abaqus functionalities from multiple perspectives:
Which Python package would you like to install in Abaqus?
Out of the records...
I was recently messaging with Ross McLendon (Simulia R&D Product Manager) and told me about a modified Pip version inside Abaqus 2024, abqPip. This lite-Pip version can install third-party packages. One of the main differences is that packages won’t be installed in the Abaqus installation, but in a ‘target directory’ specified by the user.
- cmd
>> abaqus python -m abqPip -h
I was recently messaging with Ross McLendon (Simulia R&D Product Manager) and told me about a modified Pip version inside Abaqus 2024, abqPip. This lite-Pip version can install third-party packages.
One of the main differences is that packages won’t be installed in the Abaqus installation, but in a ‘target directory’ specified by the user, so don’t forget to append the installation directory to the path into your script as shown below:
- Python
""" Testing Pandas installed with abqPip
In the cmd:
abaqus python -m abqPip install pandas -d "C:/my_folder"
In Abaqus CAE go to: File -> Run script...
"""
# Append my_folder to sys.path to import pandas
import sys
sys.path.append(r'C:/my_folder/')
# Import pandas package (from my_folder)
import pandas as pd
# ...
3. Conclusions
Abaqus 2024 has enabled the inclusion of modern Python packages in our scripts thanks to the long-awaited Python 3.10 update.
Once we are able to install Pip in the Python environment, installing third-party packages is trivial as we have seen in this post.
This update consolidates Python as the global programming language for Finite Element Analysis APIs. Python is the language that engineering software speaks now and will speak for many more years. Hurrah!
If you have any issues with Python 3.10 or installing other packages, please text me:
By the way, the “Abaqus Scripting with Python” course is already updated with scripts in Python 3.10 and we take advantage of external libraries like Sympy, check it out!
I hope that you find these tips useful!