Tutorial: Abaqus CAE#
While the core tutorials focus on script based, non-interactive workflows, it is possible to mix interactive and graphical elements into a workflow, too. Interactive tasks remove many of the benefits of a build system, such as large batch execution (parameter studies) and reproducibility. However, they are sometimes unavoidable.
An example of an unavoidable, non-graphical, interactive task is authentication against access controls like when a user is required to enter a password. Interactive tasks may also be necessary when the cost of scripting project files is prohibitively high. For example, a project may require editing binary files where the cost of scripting the edits is not justified, as when the files undergo whole-sale changes frequently, when the edits are difficult to program and require significant analyst judgement, or when large, legacy files are relatively static. In particular, proprietary binary files may be more convenient to edit with the tools provided by the proprietary graphical software, like geometric CAD files.
When adding interactive elements to a workflow, you can preserve some value of automation and reproducibility by limiting interactive tasks to the earliest portions of the workflow and putting interactively modified files under version control. The editing process may be interactive, but preserving the file with version control makes the downstream workflow reproducible for all project members. To regain the benefits of a build system, the downstream workflow must still be executable from a command line interface.
This tutorial will cover one possible solution to incorporate the graphical, interactive tasks of Abaqus model creation in Abaqus CAE with an automated, non-interactive job submission task. Similar solutions are limited by third-party interfaces, but should be possible in most cases where a scripting interface is available.
Environment#
SCons and WAVES can be installed in a Conda environment with the Conda package manager. See the Conda installation and Conda environment management documentation for more details about using Conda.
Note
The SALib and numpy versions may not need to be this strict for most tutorials. However, Tutorial: Sensitivity Study uncovered some undocumented SALib version sensitivity to numpy surrounding the numpy v2 rollout.
Create the tutorials environment if it doesn’t exist
$ conda create --name waves-tutorial-env --channel conda-forge waves 'scons>=4.6' matplotlib pandas pyyaml xarray seaborn 'numpy>=2' 'salib>=1.5.1' pytest
Activate the environment
$ conda activate waves-tutorial-env
Some tutorials require additional third-party software that is not available for the Conda package manager. This
software must be installed separately and either made available to SConstruct by modifying your system’s PATH
or by
modifying the SConstruct search paths provided to the waves.scons_extensions.add_program()
method.
Warning
STOP! Before continuing, check that the documentation version matches your installed package version.
You can find the documentation version in the upper-left corner of the webpage.
You can find the installed WAVES version with
waves --version
.
If they don’t match, you can launch identically matched documentation with the WAVES Command-Line Utility
docs subcommand as waves docs
.
Directory Structure#
Create the project directory structure and copy the tutorial files into the
~/waves-tutorials/tutorial_abaqus_cae
sub-directory with the WAVES Command-Line Utility fetch subcommand.
$ waves fetch tutorials/tutorial_abaqus_cae --destination ~/waves-tutorials/tutorial_abaqus_cae
WAVES fetch
Destination directory: '/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae'
$ cd ~/waves-tutorials/tutorial_abaqus_cae
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
SConscript File#
For this tutorial, we will not discuss the main SCons configuration file named SConstruct
, which contains project
setup boilerplate. Tutorial 00: SConstruct has a more complete discussion about the contents of the SConstruct
file.
The SConscript
file contains the workflow task definitions. This tutorial contains two sets of tasks. The first set
of tasks prepare the Abaqus CAE cantilever beam model from the tutorials provided by the Abaqus documentation and
retrieved with the fetch
command [37]. These tasks are not strictly part of the tutorial. Instead they
produce a job submission ready beam.cae
file. See the Abaqus manual for a tutorial on using Abaqus CAE.
tutorial_abaqus_cae/SConscript
16# Create the job-ready beam example CAE file
17cae_prep.extend(
18 env.Command(
19 target=["beamExample.py"],
20 source=["SConscript"],
21 action=[
22 "cd ${TARGET.dir.abspath} && ${abaqus} fetch job=beamExample",
23 "echo \"mdb.saveAs(pathName='beam.cae')\" >> ${TARGET.abspath}",
24 ],
25 abaqus=env["ABAQUS_PROGRAM"],
26 )
27)
28
29cae_prep.extend(
30 env.AbaqusJournal(
31 target=["beam.cae"],
32 source=["beamExample.py"],
33 )
34)
The tutorial task is a single Abaqus journal file execution. It should look similar to the journal file tasks introduced
in the core tutorials: Tutorial 01: Geometry and Tutorial 02: Partition and Mesh. You can read more about the behavior
of the AbaqusJournal
builder in the function API waves.scons_extensions.abaqus_journal_builder_factory()
and the core
tutorials.
tutorial_abaqus_cae/SConscript
38# Run the tutorial CAE job submission solution
39workflow.extend(
40 env.AbaqusJournal(
41 target=["beam.odb"],
42 source=["submit_cae.py", "beam.cae"],
43 subcommand_options="--input-file ${SOURCES[1].abspath} --job-name beam --model-name Beam",
44 )
45)
Abaqus Journal File#
The Abaqus journal file is a small command line utility designed to open job submission ready CAE model files and submit the simulation. You can read more about journal file design in the core tutorials: Tutorial 01: Geometry and Tutorial 02: Partition and Mesh. This journal file shares many of the same features, such as docstrings for the API, a command line interface, default execution values, input file handling to avoid accidental source modification during workflow execution, and function separation to smaller unit tasks.
tutorial_abaqus_cae/submit_cae.py
1import os
2import sys
3import shutil
4import inspect
5import tempfile
6import argparse
7
8import abaqus
9import abaqusConstants
10import job
11
12
13default_model_name = "Model-1"
14default_cpus = None
15
16
17def main(input_file, job_name, model_name=default_model_name, cpus=default_cpus, write_inp=False, **kwargs):
18 """Open an Abaqus CAE model file and submit the job.
19
20 If the job already exists, ignore the model name and update the job options. If the job does not exist, create it
21 using the job attributes passed in from the API/CLI, e.g. ``cpus`` and ``kwargs``.
22
23 Because Abaqus modifies CAE files on open, a temporary copy of the file is created to avoid constant job rebuilds in
24 build tools like SCons or Make.
25
26 See ``mdb.Job`` in the "Abaqus Scripting Reference Guide" for job behavior and keyword arguments.
27
28 :param str input_file: CAE file to open by absolute or relative path. Must include the extension.
29 :param str job_name: The name of the Abaqus job
30 :param str model_name: The name of the Abaqus model
31 :param int cpus: The number of CPUs for the Abaqus solver
32 :param bool write_inp: write an Abaqus ``job.inp`` file and exit without submitting the job
33 :param kwargs: The ``abaqus.mdb.Job`` keyword arguments. If the job exists, these overwrite existing job
34 attributes. If provided, the ``cpus`` argument overrides both existing job attributes _and_ the kwargs.
35 """
36 if cpus is not None:
37 kwargs.update({"numCpus": cpus})
38
39 with AbaqusNamedTemporaryFile(input_file=input_file, suffix=".cae", dir="."):
40
41 if job in abaqus.mdb.jobs.keys():
42 script_job = abaqus.mdb.jobs[job]
43 script_job.setValues(**kwargs)
44 model_name = script_job.model
45
46 if model_name in abaqus.mdb.models.keys():
47 script_job = abaqus.mdb.Job(name=job_name, model=model_name, **kwargs)
48 else:
49 raise RuntimeError("Could not find model name '{}' in file '{}'\n".format(model_name, input_file))
50
51 if write_inp:
52 script_job.writeInput(consistencyChecking=abaqusConstants.OFF)
53 else:
54 script_job.submit()
55 script_job.waitForCompletion()
56
57
58def get_parser():
59 """Return parser for CLI options
60
61 All options should use the double-hyphen ``--option VALUE`` syntax to avoid clashes with the Abaqus option syntax,
62 including flag style arguments ``--flag``. Single hyphen ``-f`` flag syntax often clashes with the Abaqus command
63 line options and should be avoided.
64
65 :returns: parser
66 :rtype: argparse.ArgumentParser
67 """
68 filename = inspect.getfile(lambda: None)
69 basename = os.path.basename(filename)
70
71 default_json_file = None
72
73 prog = "abaqus cae -noGui {} --".format(basename)
74 cli_description = (
75 "Open an Abaqus CAE model file and submit the job."
76 "If the job already exists, ignore the model name and update the job options. If the job does not exist, "
77 "create it using the job attributes passed in from the API/CLI, e.g. ``cpus`` and ``kwargs``. "
78 "Because Abaqus modifies CAE files on open, a temporary copy of the file is created to avoid constant job "
79 "rebuilds in build tools like SCons or Make."
80 )
81 parser = argparse.ArgumentParser(description=cli_description, prog=prog)
82 parser.add_argument(
83 "--input-file",
84 type=str,
85 required=True,
86 help="The Abaqus CAE model file with extension, e.g. ``input_file.cae``",
87 )
88 parser.add_argument(
89 "--job-name",
90 type=str,
91 required=True,
92 help="The name of the Abaqus job",
93 )
94 parser.add_argument(
95 "--model-name",
96 type=str,
97 default=default_model_name,
98 help="The name of the Abaqus model (default %(default)s)",
99 )
100 parser.add_argument(
101 "--cpus",
102 type=int,
103 default=default_cpus,
104 help="The number of cpus for the Abaqus simulation (default %(default)s)",
105 )
106 parser.add_argument(
107 "--json-file",
108 type=str,
109 default=default_json_file,
110 help="A JSON file containing a dictionary of keyword arguments for ``abaqus.mdb.Job`` " "(default %(default)s)",
111 )
112 parser.add_argument(
113 "--write-inp",
114 "--write-input",
115 action="store_true",
116 help="Write an Abaqus ``job.inp`` file and exit without submitting the job " "(default %(default)s)",
117 )
118 return parser
119
120
121class AbaqusNamedTemporaryFile:
122 """Open an Abaqus CAE ``input_file`` as a temporary file. Close and delete on exit of context manager.
123
124 Provides Windows compatible temporary file handling. Required until Python 3.12 ``delete_on_close=False`` option is
125 available in Abaqus Python.
126
127 :param str input_file: The input file to copy before open
128 """
129
130 def __init__(self, input_file, *args, **kwargs):
131 self.temporary_file = tempfile.NamedTemporaryFile(*args, delete=False, **kwargs)
132 shutil.copyfile(input_file, self.temporary_file.name)
133 abaqus.openMdb(pathName=self.temporary_file.name)
134
135 def __enter__(self):
136 return self.temporary_file
137
138 def __exit__(self, exc_type, exc_val, exc_tb):
139 abaqus.mdb.close()
140 self.temporary_file.close()
141 os.remove(self.temporary_file.name)
142
143
144def return_json_dictionary(json_file):
145 """Open a JSON file and return a dictionary compatible with Abaqus keyword arguments
146
147 If the JSON file is ``None``, return an empty dictionary. Convert unicode strings to str. If a value is found in
148 ``abaqusConstants`` convert the value.
149
150 :param str json_file: path to a JSON file
151
152 :returns: Abaqus compatible keyword argument dictionary
153 :rtype: dict
154 """
155 kwargs = {}
156 if json_file is not None:
157 with open(json_file) as json_open:
158 dictionary = json.load(json_open)
159 for key, value in dictionary.items():
160 if isinstance(key, unicode):
161 key = str(key)
162 if isinstance(value, unicode):
163 value = str(value)
164 if hasattr(abaqusConstants, value):
165 value = getattr(abaqusConstants, value)
166 kwargs[key] = value
167 return kwargs
168
169
170if __name__ == "__main__":
171 parser = get_parser()
172 try:
173 args, unknown = parser.parse_known_args()
174 except SystemExit as err:
175 sys.exit(err.code)
176 possible_typos = [argument for argument in unknown if argument.startswith("--")]
177 if len(possible_typos) > 0:
178 raise RuntimeError("Found possible typos in CLI option(s) {}".format(possible_typos))
179
180 kwargs = return_json_dictionary(args.json_file)
181
182 sys.exit(
183 main(
184 input_file=args.input_file,
185 job_name=args.job_name,
186 model_name=args.model_name,
187 cpus=args.cpus,
188 write_inp=args.write_inp,
189 **kwargs # fmt: skip
190 )
191 )
After command line utility design, Python coding practices, and the Python style guide, the most unique aspect of this
journal file is the return_json_dictionary
function. This is necessary to convert string based job arguments to
Abaqus scripting objects found in the abaqusConstants
module. In an active modsim project, this function would be
broken into yet smaller units of re-usable work and placed in a shared utilities module as introduced in
Tutorial 02: Partition and Mesh.
Here it is kept as a limited use function implemented directly in the calling script for the convenience of documenting this tutorial and maintaining the tutorial suite. See the ModSim Templates for examples of more re-usable utility functions and unit testing.
Building targets#
The tutorial may be executed by a single call of the submit_beam_cae
alias. However, to demonstrate the interactive
use of a CAE file, a second alias is provided, create_beam_cae
.
First, create the CAE file
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ scons create_beam_cae
This intermediate workflow will fetch the beamExample.py
file from the Abaqus tutorial repository with abaqus
fetch
, append a CAE model save command, and execute the journal file. The Abaqus tutorial includes job execution with
the name beam_tutorial
, so you will see job output in the build directory along with the desired beam.cae
file.
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ ls build/beam.cae
build/beam.cae
$ tree build/
build/
|-- SConscript
|-- abaqus.rpy
|-- beam.cae
|-- beam.cae.stdout
|-- beam.jnl
|-- beamExample.py
|-- beam_tutorial.com
|-- beam_tutorial.dat
|-- beam_tutorial.inp
|-- beam_tutorial.log
|-- beam_tutorial.msg
|-- beam_tutorial.odb
|-- beam_tutorial.prt
`-- beam_tutorial.sta
0 directories, 14 files
Take some time to open the CAE file and look for familiar CAE model and job data. Since this model is job ready, there is nothing to edit.
Finally, run the post-interactive workflow that is the subject of this tutorial
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ scons submit_beam_cae
This workflow calls the submit_cae.py
journal file to submit the job defined in beam.cae
. The new files should
look identical (except for timestamps and job name) to the output produced by the beamExample.py
job.
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ tree build -P "beam.*"
build
|-- beam.cae
|-- beam.cae.stdout
|-- beam.com
|-- beam.dat
|-- beam.inp
|-- beam.jnl
|-- beam.log
|-- beam.msg
|-- beam.odb
|-- beam.odb.stdout
|-- beam.prt
`-- beam.sta
0 directories, 13 files
To better understand the mixed workflow, open the build/beam.cae
file and edit it with Abaqus CAE. When you’ve
finished editing the file, save it, close Abaqus CAE, and re-run the scons submit_beam_cae
command. Observe the
SCons output during execution and the timestamp changes in the build directory. It should look similar to the example
output below.
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ scons submit_beam_cae --debug=explain
scons: Reading SConscript files ...
Checking whether /apps/abaqus/Commands/abq2024 program exists.../apps/abaqus/Commands/abq2024
Checking whether abq2024 program exists...no
scons: done reading SConscript files.
scons: Building targets ...
scons: rebuilding `build/beam.odb' because `build/beam.cae' changed
cd /home/roppenheimer/waves-tutorials/tutorial_abaqus_cae/build && /apps/abaqus/Commands/abq2024 cae -noGui /home/roppenheimer/waves-tutorials/tutorial_abaqus_cae/build/submit_cae.py -- --input-file /home/roppenheimer/waves-tutorials/tutorial_abaqus_cae/build/beam.cae --job-name beam --model-name Beam > /home/roppenheimer/waves-tutorials/tutorial_abaqus_cae/build/beam.odb.stdout 2>&1
scons: done building targets.
Output Files#
The full output directory should look like the following
$ pwd
/home/roppenheimer/waves-tutorials/tutorial_abaqus_cae
$ tree build
build
|-- SConscript
|-- abaqus.rpy
|-- abaqus.rpy.1
|-- beam.cae
|-- beam.cae.stdout
|-- beam.com
|-- beam.dat
|-- beam.inp
|-- beam.jnl
|-- beam.log
|-- beam.msg
|-- beam.odb
|-- beam.odb.stdout
|-- beam.prt
|-- beam.sta
|-- beamExample.py
|-- beam_tutorial.com
|-- beam_tutorial.dat
|-- beam_tutorial.inp
|-- beam_tutorial.log
|-- beam_tutorial.msg
|-- beam_tutorial.odb
|-- beam_tutorial.prt
|-- beam_tutorial.sta
`-- submit_cae.py
0 directories, 26 files