Lately, I’ve received a few questions related to python scripting. It is a nice way to add to the default Abaqus functionality and automate repetitive work. Here I want to give a few tips that may help when you are creating your first Abaqus python scripts.
1. Make use of the .rpy file.Abaqus automatically stores what is done in Abaqus/CAE as python commands in a .rpy file. This file is a good starting point to base your own script on.
2. Read our previous blog on scriptingThis blog post gives an example of automating postprocessing. This example is based on the .rpy file, and it includes a loop to do the same things for multiple inputs, in this case .odb’s. A similar approach can be used to automate model creation or job submission.
3. Use the command line interface
To quickly test Abaqus commands, the command line interface can be used. This is in the CAE, on the same place as the message area. Clicking on the icon with >>>
You can directly type in code and press enter. It also allows automatic completion using the TAB-key: if you start typing a command and then press the TAB-key, an option for continuing the command will be given. Pressing TAB again will give another option, cycling through all options. With SHIFT+TAB, you can cycle in the backwards direction, going back to the previous option.
4. Learn to find the structure of the Abaqus Scripting Interface
The Abaqus Scripting Interface (ASI) is the python code that allows us to interact with Abaqus models and data. It includes many data types in a structure with methods to modify the data. In order to do things with Abaqus, we need to use this structure. If we want to use a certain output, for example, we need to know where to find it. There are several things that can help us with this.
- The .rpy file: a reference to something in the .rpy file shows the path to access it. For example, the line
a = mdb.models[‘Model-1’].rootAssembly
in the .rpy file shows the path to access the assembly.
- The manual: in the Abaqus Scripting Reference Guide, an overview of the ASI is given. This includes information on access, a list of methods and members.
The access section describes how to reach the object, such as mdb.models[name].rootAssembly. Sometimes, more than one option is available.
Methods are the functions available on the object. For the rootAssembly, this is quite a long list including deleteFeatures, getDistance, makeDependent, regenerate, translate, lock, ….
Members are the properties of the object. For the rootAssembly, this includes isOutOfDate, instances, surfaces, …
- Using the command line interface: automatic completion in the command line interface can help to determine the access path to objects. For Abaqus objects, __members__ and __methods__ can be used to find the members and methods of the object respectively. Typing in
will give the list of members, like the one that is found in the manual
Use job.waitForCompletion()When a job is submitted from a script, the script will continue while the job is running. If the job is part of a loop, often a second job is submitted while the first is still running, which takes up additional tokens and system resources. This is usually unintended. The .rpy file will not tell you how to solve this.
Luckily, the job has a method specifically for this purpose: waitForCompletion. Example usage:
Read error messages
If a script is giving an error message, read it carefully. It usually provides information on the line number in which the error is occurring, as well as the type of error.
In this case, for example, the problem occurs in line 10 and is a KeyError. Apparently, the XYPlot-1 is not defined. We can then go through the script to determine why we though it could be referred to, while it is not available. It may be a typo (identifiers are case-sensitive); it may be defined inside a function and not available outside it; the script could be based on a macro, where it was already defined beforehand; or we could simply have forgotten something.
7. Use print statements and commentsTo determine what is going on, print statements can be of great use. By printing the value of an object or the fact that a certain part of the code is reached, a lot more clarity can be obtained on what is going on. A simple print statement can be defined as:
print “string with what is being printed”, identifier
Comments are also valuable. Apart from them helping to make sense out of machine code, they can be used to effectively remove part of the script, without deleting it entirely. (Similar to suppressing instances in a model for example). This helps to determine where an error occurs among other things.