Вы находитесь на странице: 1из 5

Workshop 2

Running Python

Goals
When you complete this workshop, you will be able to:
● Run Python interactively; from the command line and from Abaqus/CAE.
● Run Python by using scripts.

Introduction
Accessing Abaqus Python
A version of Python is delivered with Abaqus. This version contains many extensions
that have been developed to work only with the Abaqus databases. You may access this
version at the system shell command line by typing the command abaqus python like
this:

Here, the word abaqus should be the name of the launcher (execution procedure) that is
associated with a particular release of the Abaqus software on your system. Each site may
use a slightly different name for each maintenance release. Typically, the launcher name
adheres to a convention that relates to the level of the release (for example, abq2020 is
usually used for release Abaqus 2020). If the appropriate launcher name is not recognized
by the system, you will need to fix this. At the end of this workshop is an Appendix to be
used for troubleshooting the access of Abaqus Python.

Other Python Versions


Note your Python experience does not have to be limited to the version that is shipped
with Abaqus. Users are free to access other versions of Python that are freely available
from a variety of sources. There are some limitations, however.

The first limitation is that SIMULIA does not provide support for other versions of the
Python language. Also, the Abaqus extensions (the things you will need to work with the
Abaqus database) are only supported for the version of Python that is delivered with the
software. For other versions of Python, you may also need to add the path of the Python
executable to your PATH environment variable in windows. The benefit of using other

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W2.2

Python sources is that you may be able to find later versions of the language and may
find that Python is something that you want to use for other applications besides Abaqus.

Tasks
1. Using the Command-line Interactive Mode. Start Abaqus Python at the command-
line. Type any expression such as: "Hello World!" (include the double quote
characters).

The string should be echoed when you hit the return key. Try executing other
commands such as 2+2 (no quotes here). Try the expression sin(pi). Why does the
latter not work? Try it again after typing the command from math import *.
Exit the Python session by typing CTRL-Z on UNIX or CTRL-Z, RETURN on
Windows.
Type abaqus python -h for more information.

2. Using the Abaqus/CAE Interactive Mode. Create a text file called run_cae.bat in
your present working directory. Include the following line in the new file:
<your-Abaqus-launcher-name> cae
Save the file and start Abaqus/CAE by double-clicking the file icon. This is a
convenient means of quickly launching Abaqus interactive products while assuring
that the associated files are written to your present working directory. You access the
Python interactive mode from the command line interface as shown below.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W2.3

Now repeat some of the procedures from task 1. What version of Python are you
using? Hint: try looking at the sys.version object. What version of Abaqus are
you using? Hint: try calling uti.getVersion(). In both cases, you will need to
“import” the appropriate module like we did in task 1 for the math module.

3. Using Programs from the Command-line. With a text editor or a Python IDE, write a
simple script file. The file should contain a single print statement such as: print
'Hello World!' (this time include single quote characters). Store this statement in a
file named file1.py. Now, run this file by passing it to the Python interpreter
program on the system shell’s command line (hint: type something like
abaqus python file1.py).
Why does a single quote character work for the string? Is the .py extension
required for the file name?

4. Using Programs from Abaqus/CAE. Repeat task 3 from Abaqus/CAE. You may run
scripts by using the File→Run Script pull-down menu, or by using the execfile()
method from the command line interface. Where does the text get displayed? When
Python programs are executed as we have in steps 3 and 4, we say that we are running
them as “top level scripts.”

5. Using Script Files.


Unix/Linux platforms:
Give the file file1.py executable privileges, and “run it” directly as an executable.
Does the first line need to contain anything special for your platform? For UNIX
platforms, add the #! magic line to the top of file1.py.

Windows platforms:
Depending on your installation, you may be able to run your script by
(i) typing the file1.py at the command line,
(ii) typing file1 at the command line (i.e. no file extension required)
(iii) double-clicking the file icon in Windows Explorer.
If any of these do not work, we will address remedies in a later lesson.

If you have trouble completing this workshop, the file ws_scr_hello_answer.py


contains the answer to the workshop. It is provided for your convenience.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W2.4

Appendix: Troubleshooting

Windows Platforms
Windows comes with a command prompt allowing users to run programs from a
command-line interface. The Windows command prompt originated from early versions
of MS-DOS and uses the same basic syntax.
Check to see if the launcher name appears in the directory called:
<your-abaqus-directory>\Commands\
If the launcher name does not exist in this directory as a Windows batch file (.bat
extension), then the name you are using is not appropriate for your installation. If you do
not know the appropriate name for the launcher, speak to the instructor or your systems
administrator.
If the name does exist, but it is not recognized by your system, then it is likely that the
commands directory is not defined in the PATH environment variable for your system. If
so, set the PATH environment variable.

The simplest method of setting environment variables is through the System applet in
Control Panel. Depending on your version of Windows, one of the screen shots below
should show the lists of user and system environment variables. To change the value of
an existing user variable, select the variable from the corresponding list and modify the
value in the Value field. You use the following steps to create a new variable:

1. Click the User Variables list to select the type of variable you want to create.
2. Replace the Variable field contents with the new variable name that you want to
create.
3. Replace the Value field contents with the value that you want to assign to the new
variable.
4. Click Set or OK.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting


W2.5

The variable list that you selected will list your new variable.

UNIX Platforms
The easiest way to set a user path environment variable on UNIX is to issue a command
like the one below:

set path = ($path /usr/local/bin)

This may be added to your shell configuration file (such as the file called .cshrc for c-
shell users or .kshrc for korn-shell users) to make the launcher name permanently
available.

© Dassault Systèmes, 2019 Introduction to Abaqus Scripting

Вам также может понравиться