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.

  1. 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
    
    PS > conda create --name waves-tutorial-env --channel conda-forge waves scons matplotlib pandas pyyaml xarray seaborn numpy salib pytest
    
  2. Activate the environment

    $ conda activate waves-tutorial-env
    
    PS > 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.

  1. You can find the documentation version in the upper-left corner of the webpage.

  2. 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#

  1. 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 [42]. 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

15# Create the job-ready beam example CAE file
16cae_prep.extend(
17    env.Command(
18        target=["beamExample.py"],
19        source=["SConscript"],
20        action=[
21            "cd ${TARGET.dir.abspath} && ${abaqus} fetch job=beamExample",
22            "echo \"mdb.saveAs(pathName='beam.cae')\" >> ${TARGET.abspath}",
23        ],
24        abaqus=env["ABAQUS_PROGRAM"],
25    )
26)
27
28cae_prep.extend(
29    env.AbaqusJournal(
30        target=["beam.cae"],
31        source=["beamExample.py"],
32    )
33)

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

37# Run the tutorial CAE job submission solution
38workflow.extend(
39    env.AbaqusJournal(
40        target=["beam.odb"],
41        source=["submit_cae.py", "beam.cae"],
42        subcommand_options="--input-file ${SOURCES[1].abspath} --job-name beam --model-name Beam",
43    )
44)

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

  1"""Open an Abaqus CAE model file and submit the job."""
  2
  3import argparse
  4import inspect
  5import json
  6import os
  7import shutil
  8import sys
  9import tempfile
 10
 11import abaqus
 12import abaqusConstants
 13import job
 14
 15default_model_name = "Model-1"
 16default_cpus = None
 17
 18if sys.version_info[0] > 2:
 19    unicode = str
 20
 21
 22def main(input_file, job_name, model_name=default_model_name, cpus=default_cpus, write_inp=False, **kwargs):
 23    """Open an Abaqus CAE model file and submit the job.
 24
 25    If the job already exists, ignore the model name and update the job options. If the job does not exist, create it
 26    using the job attributes passed in from the API/CLI, e.g. ``cpus`` and ``kwargs``.
 27
 28    Because Abaqus modifies CAE files on open, a temporary copy of the file is created to avoid constant job rebuilds in
 29    build tools like SCons or Make.
 30
 31    See ``mdb.Job`` in the "Abaqus Scripting Reference Guide" for job behavior and keyword arguments.
 32
 33    :param str input_file: CAE file to open by absolute or relative path. Must include the extension.
 34    :param str job_name: The name of the Abaqus job
 35    :param str model_name: The name of the Abaqus model
 36    :param int cpus: The number of CPUs for the Abaqus solver
 37    :param bool write_inp: write an Abaqus ``job.inp`` file and exit without submitting the job
 38    :param kwargs: The ``abaqus.mdb.Job`` keyword arguments. If the job exists, these overwrite existing job
 39        attributes. If provided, the ``cpus`` argument overrides both existing job attributes _and_ the kwargs.
 40    """
 41    if cpus is not None:
 42        kwargs.update({"numCpus": cpus})
 43
 44    with AbaqusNamedTemporaryFile(input_file=input_file, suffix=".cae", dir="."):
 45        if job in abaqus.mdb.jobs:
 46            script_job = abaqus.mdb.jobs[job]
 47            script_job.setValues(**kwargs)
 48            model_name = script_job.model
 49
 50        if model_name in abaqus.mdb.models:
 51            script_job = abaqus.mdb.Job(name=job_name, model=model_name, **kwargs)
 52        else:
 53            raise RuntimeError("Could not find model name '{}' in file '{}'\n".format(model_name, input_file))
 54
 55        if write_inp:
 56            script_job.writeInput(consistencyChecking=abaqusConstants.OFF)
 57        else:
 58            script_job.submit()
 59            script_job.waitForCompletion()
 60
 61
 62def get_parser():
 63    """Return parser for CLI options.
 64
 65    All options should use the double-hyphen ``--option VALUE`` syntax to avoid clashes with the Abaqus option syntax,
 66    including flag style arguments ``--flag``. Single hyphen ``-f`` flag syntax often clashes with the Abaqus command
 67    line options and should be avoided.
 68
 69    :returns: parser
 70    :rtype: argparse.ArgumentParser
 71    """
 72    filename = inspect.getfile(lambda: None)
 73    basename = os.path.basename(filename)
 74
 75    default_json_file = None
 76
 77    prog = "abaqus cae -noGui {} --".format(basename)
 78    cli_description = (
 79        "Open an Abaqus CAE model file and submit the job."
 80        "If the job already exists, ignore the model name and update the job options. If the job does not exist, "
 81        "create it using the job attributes passed in from the API/CLI, e.g. ``cpus`` and ``kwargs``. "
 82        "Because Abaqus modifies CAE files on open, a temporary copy of the file is created to avoid constant job "
 83        "rebuilds in build tools like SCons or Make."
 84    )
 85    parser = argparse.ArgumentParser(description=cli_description, prog=prog)
 86    parser.add_argument(
 87        "--input-file",
 88        type=str,
 89        required=True,
 90        help="The Abaqus CAE model file with extension, e.g. ``input_file.cae``",
 91    )
 92    parser.add_argument(
 93        "--job-name",
 94        type=str,
 95        required=True,
 96        help="The name of the Abaqus job",
 97    )
 98    parser.add_argument(
 99        "--model-name",
100        type=str,
101        default=default_model_name,
102        help="The name of the Abaqus model (default %(default)s)",
103    )
104    parser.add_argument(
105        "--cpus",
106        type=int,
107        default=default_cpus,
108        help="The number of cpus for the Abaqus simulation (default %(default)s)",
109    )
110    parser.add_argument(
111        "--json-file",
112        type=str,
113        default=default_json_file,
114        help="A JSON file containing a dictionary of keyword arguments for ``abaqus.mdb.Job`` (default %(default)s)",
115    )
116    parser.add_argument(
117        "--write-inp",
118        "--write-input",
119        action="store_true",
120        help="Write an Abaqus ``job.inp`` file and exit without submitting the job (default %(default)s)",
121    )
122    return parser
123
124
125class AbaqusNamedTemporaryFile:
126    """Open an Abaqus CAE ``input_file`` as a temporary file. Close and delete on exit of context manager.
127
128    Provides Windows compatible temporary file handling. Required until Python 3.12 ``delete_on_close=False`` option is
129    available in Abaqus Python.
130
131    :param str input_file: The input file to copy before open
132    """
133
134    def __init__(self, input_file, *args, **kwargs):
135        """Initialize the Abaqus temporary file class."""
136        self.temporary_file = tempfile.NamedTemporaryFile(*args, delete=False, **kwargs)
137        shutil.copyfile(input_file, self.temporary_file.name)
138        abaqus.openMdb(pathName=self.temporary_file.name)
139
140    def __enter__(self):
141        """Define the context manager construction method."""
142        return self.temporary_file
143
144    def __exit__(self, exc_type, exc_val, exc_tb):
145        """Define the context manager cleanup method."""
146        abaqus.mdb.close()
147        self.temporary_file.close()
148        os.remove(self.temporary_file.name)
149
150
151def return_json_dictionary(json_file):
152    """Open a JSON file and return a dictionary compatible with Abaqus keyword arguments.
153
154    If the JSON file is ``None``, return an empty dictionary. Convert unicode strings to str. If a value is found in
155    ``abaqusConstants`` convert the value.
156
157    :param str json_file: path to a JSON file
158
159    :returns: Abaqus compatible keyword argument dictionary
160    :rtype: dict
161    """
162    kwargs = {}
163    if json_file is not None:
164        with open(json_file) as json_open:
165            dictionary = json.load(json_open)
166        for key, value in dictionary.items():
167            if isinstance(key, unicode):
168                return_key = str(key)
169            else:
170                return_key = key
171            if isinstance(value, unicode):
172                return_value = str(value)
173            else:
174                return_value = value
175            if hasattr(abaqusConstants, return_value):
176                return_value = getattr(abaqusConstants, return_value)
177            kwargs[return_key] = return_value
178    return kwargs
179
180
181if __name__ == "__main__":
182    parser = get_parser()
183    try:
184        args, unknown = parser.parse_known_args()
185    except SystemExit as err:
186        sys.exit(err.code)
187    possible_typos = [argument for argument in unknown if argument.startswith("--")]
188    if len(possible_typos) > 0:
189        raise RuntimeError("Found possible typos in CLI option(s) {}".format(possible_typos))
190
191    kwargs = return_json_dictionary(args.json_file)
192
193    sys.exit(
194        main(
195            input_file=args.input_file,
196            job_name=args.job_name,
197            model_name=args.model_name,
198            cpus=args.cpus,
199            write_inp=args.write_inp,
200            **kwargs,
201        )
202    )

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