fbpx

How to use special characters in Abaqus/Scripting

Written by Miguel

22 Feb, 2021

Out of the anglosaxon world it is very common using the so called special characters. For example, some of the most common special characters in latin-based languages are accented vowels (á, à, ä, â…) and others like ñ, ç, ß…

In general, when working with international software, best practices recommend avoid using special characters and stick to the first 128 characters of the ASCII table (unextended). Otherwise, the program may not recognize those characters and may behave unexpectedly.

Special characters in Abaqus

Abaqus does not support directly using special characters, that is, we cannot call our model “máquina” or “sphère“, because it will not recognize the characters “á” and “è“. Similarly, special characters cannot be used when naming any sort of object like: parts, sets, surfaces, materials, sections, instances, jobs… For more details about naming restrictions check the documentation out.

However, we can use directories or paths that contain special characters. Let’s say that we have a Python script in which we create a folder that will be used as working directory:

Python

# Usual imports to run Abaqus
from abaqus import *
from abaqusConstants import *
from caeModules import *
from driverUtils import executeOnCaeStartup
import os
executeOnCaeStartup()

# Create new directory and use it as working directory
wdir = 'Análisis_máquina_año2020'

# Create folder if it does not exist yet
if not os.path.exists(wdir):
    os.makedirs(wdir)

# Set working directory in Abaqus
os.chdir(wdir)

In this script, we create a new folder (if it doesn’t exist yet). Then, we set that folder as the wroking directory in Abaqus. But this script will not work because the string wdir contains special characters (á and ñ). To solve it we must add two features:

Python

# -*- coding: utf-8 -*-

# Usual imports to run Abaqus
from abaqus import *
from abaqusConstants import *
from caeModules import *
from driverUtils import executeOnCaeStartup
import os
executeOnCaeStartup()

# Create new directory and use it as working directory
wdir = u'Análisis_máquina_año2020'

# Create folder if it does not exist yet
if not os.path.exists(wdir):
os.makedirs(wdir)

# Set working directory in Abaqus
os.chdir(wdir)

First, we specify the utf-8 encoding to the Python interpreter (on line 1), that is, we can include Unicode-type characters in the script (which includes these special characters).

Secondly, when creating the string wdir, we indicate that Unicode characters are used by typing u right before the first single quote of the string. The use of single quotes or double quotes does not make any difference.

With these modifications, the script will work as expected.

Defining directories in Windows

When working with Windows directories in Python scripts, not only for Abaqus. we need to use the backslash character ‘\’. Nevertheless, the backslash character in Python is reserved to represent ‘escape characters‘ in strings, which are characters with some special functionality in Python. Escape characters start with a backslash followed by another character, for example, the newline (\n), double quotes (\”), single quotes (\’) or even the backslash itself (\\).

Therefore, to represent a Windows directory in Python we can do the following:

Python

wdir = u'D:\\ABAQUS\\Análisis_máquina_año2020'

Wherever there should be the \ symbol, we have to type \\.

We could also indicate that it is a ‘raw string’ by typing r before the string (as we did with the u). In that case, escape characters are ignored and considered as single characters. Then we could write:

 

Python

# Raw string
wdir = ru'D:\ABAQUS\Análisis_máquina_2020'
# Equivalent regular string
wdir2 = u'D:\\ABAQUS\\Análisis_máquina_2020'

I hope that you find these tips useful!

Download the FREE GUIDE

“5 KEYS TO MASTER ABAQUS USING PYTHON”

¡DOWNLOAD GUIDE NOW!

FOLLOW US IN SOCIAL MEDIA

You might be interested as well…

0 Comments

Submit a Comment

Personal details provided through this form will be managed by TecnoDigital School responsible for this website. The aim of collecting and treating the details required is the management of the comments made in this blog.

Legitimation: Consent of the interested. Requests cannot be atended if personal details are not introduced in the fields marked as mandatory.

You will be able to exercise your access, rectification, limitation rights and to suppress your personal details through media@tecnodigitalschool.com, as well as the right to make a compliant through a control authority.

You can read additional information about Personal Details Protection in the website: https://tecnodigitalschool.com, as well as the privacy policy.