Abaqus Fortran “must knows”
In Abaqus, user subroutines are primarily written in Fortran, although developing your code in C or C++ is also possible. If you are newer to Abaqus subroutine, you should start from here:
Start Writing an Abaqus Subroutine : Basics & Recommendations
Fortran is one of the easiest programming languages to learn because it can do virtually nothing other than basic low-level operations common to all languages. Its syntax is also very similar to MATLAB. Furthermore, you need to know only an even smaller subset of FORTRAN to write a subroutine code in Abaqus. Here is a handy package of almost all the things you will need… Enjoy this article to know Abaqus Fortran basics.
1. Basics
A Fortran program is just a sequence of lines of text. It has to follow a specific structure. Take a look at this simple piece of code:
PROGRAM CIRCLE REAL R, AREA C THIS PROGRAM READS A REAL NUMBER R AND PRINTS C THE AREA OF A CIRCLE WITH RADIUS R. WRITE (*,*) 'GIVE RADIUS R:' READ (*,*) R !THIS IS RADIUS OF THE CIRCLE AREA = 3.14159*R*R WRITE (*,*) 'AREA = ', AREA STOP END
Originally, all Fortran programs had to be written in all uppercase letters. However, Fortran is not case-sensitive, so “X” and “x” are the same variable.
Column Position Rules
Fortran has very few rules for how to format the source code. The most important rules are the column position rules:
» Col. 1 Blank, or a “C” for comments
» Col. 1-5 Statement label (optional)
» Col. 6 Continuation of the previous line (optional)
» Col. 7-72 Statements
Comments
The lines that begin with a “C” here are comments and have no purpose other than to make the program more readable. You may also use the exclamation mark (!) for comments. This type of comment can be any place among your piece of code (look at ‘!THIS IS RADIUS OF THE CIRCLE’).
Statement label
Statement labels are used to mark positions in the program. Typically, many loops and other statements in a single program require a statement label. The programmer is responsible for assigning a unique number to each label in each program (or subprogram). Look at 4.1. Loops Section (Do Loop) in the second part of this article (Under Construction) to see a practical use of statement labels.
Continuation
Sometimes, a statement does not fit into the 66 available columns (7-72) of a single line. One can then break the statement into two or more lines and use the continuation mark in position 6 (I mean, you put 5 spaces and then type +). Example:
C THE NEXT STATEMENT GOES OVER TWO PHYSICAL LINES AREA = 3.14159265358979 + * R * R
Any character can be used instead of the plus sign (+) as a continuation character. Also, we may use digits (using 2 for the second line, 3 for the third, and so on).
Keep reading this post to know the main points of Abaqus Fortran subroutine writing.
2. Data Types in Fortran
Variable names
Variable names in Fortran consist of the letters A-Z, the digits 0-9 and the underscore (_). The first character must be a letter.
Like many other languages, the words which make up the Fortran language are called reserved words and cannot be used as names of variables. Some examples are PROGRAM, REAL, STOP, END, etc.
Variable Types
Fortran supports several different data types to make data manipulation easier:
The most frequently used data types are integer and real (floating point).
Integers
An integer is a number that can be written without a fractional component. For example, 21, 4, 0, and −2048 are integers. Fortran has only one type for integer variables. They are usually stored as 4 bytes variables.
Floating-point variables
When a number is not an integer, it is considered a decimal. Fortran has two different types of decimal (floating point) variables, called REAL and DOUBLE PRECISION. Numerical calculations in subroutines usually need very high precision, and DOUBLE PRECISION should be used. Usually, a real is a 4-byte variable, and the double-precision is 8 bytes. You may use the syntax REAL*8 to denote 8-byte floating-point variables in Abaqus subroutines.
Declarations
Every variable should be defined in a declaration. This establishes the type of the variable. The most common declarations are:
INTEGER Variable1, Variable2,… REAL Variable1, Variable2,… DOUBLE PRECISION Variable1, Variable2,…
Each variable should be declared exactly once. If a variable is undeclared, Fortran uses a set of implicit rules to establish the type. This means all variables starting with the letters I, J, K, L, M and N are integers and all others are real. You have to be very careful with variable names.
However, you can make a habit of putting a simple piece of code at the top of each of your subroutines:
IMPLICIT NONE
Then, you must explicitly declare every variable that your subroutine uses or needs.
Parameters
Some constants appear many times in a program. Therefore, it is often desirable to define them only once, at the beginning of the program. This is called PARAMETER in Fortran:
- To reduce the number of typos (i.e., typing errors).
- To facilitate changing a constant that appears many times in a program.
- To increase the readability of the code.
For example, we can rewrite the example code above (circle area) as:
PROGRAM CIRCLE REAL R, AREA, PI PARAMETER (PI = 3.14159) C THIS PROGRAM READS A REAL NUMBER R AND... . . . END
The syntax of the parameter statement is
PARAMETER (name1 = value1, name2 = value2, …)
The PARAMETER statement(s) must come before the first executable statement.
Arrays
Many computations in Abaqus Fortran subroutines use vectors and matrices. The data type Fortran uses for representing such objects is the array. A one-dimensional array corresponds to a vector, while a two-dimensional array corresponds to a matrix.
1D arrays
It is just a linear sequence of elements stored consecutively in memory. For example,
REAL A(6)
declares A as a real array of length 6. By convention, Fortran arrays are indexed from 1 and up. Each element of an array can be thought of as a separate variable. You reference the ith element of array A by A(i).
2D arrays
Matrices are very important in linear algebra. They are usually represented by two-dimensional arrays. For example, the declaration
REAL A(3,5)
defines a two-dimensional array of 3 × 5 = 15 real numbers. It is useful to think of the first index as the row index and the second as the column index.
In the following, we will discuss the remaining topics such as Operators and Expressions (for Variables and Matrices), Conditional (IF) and Loop (DO) Statements, some Build-in Functions in Fortran, and finally, a brief talk about How to Write to files from inside Fortran. Follow this post to know more about Fortran for Abaqus subroutines.
3. Operators in Fortran for Abaqus
The simplest non-constant expressions are of the form:
operand operator operand
The result of an expression is itself an operand; hence we can nest expressions together.
ℜemember:
- The order of precedence is essential: arithmetic expressions are evaluated first, then relational operators, and finally logical operators (Take a look at Quiz Time question 3).
- Logical expressions are frequently used in conditional statements like the IF statement.
4. Controls
Fortran for Abaqus subroutine has some control features presented here briefly.
4.1. Conditional Statements
The most common conditional statement in Fortran is the IF statement, which has several forms:
I. One-line (one executable statement)
IF (logical expression) executable statement
Example:
IF (X.LT.0) X = -X
II. More than one executable statement
IF (logical expression) THEN Statement1 Statement2 . . . END IF
III. General Form
IF (logical expression1) THEN statements1 ELSE IF (logical expression2) THEN statements2 . . . ELSE statementsn END IF
The conditional expressions are evaluated in a row until one is found to be true. Then the associated statements are executed, and the control resumes after the endif.
4.2. Loops
For repeated execution of similar things, loops are used. Here we only take a look at DO-loop in Fortran.
Do-loop
This loop repeats a series of one or more Fortran statements a set number of times. The general format is:
DO count_variable = start, stop, step . fortran statement(s) . END DO
count_variable is the loop variable (often called the ‘loop index’ or ‘counter’).
start specifies the initial value of count_variable,
stop is the terminating bound,
step is the increment (step).
Where the count_variable must be an integer variable. start, stop, and step are integer variables or integer expressions. The step value is optional. If it is omitted, the default value is 1.
Also, another general form of the DO loop that you may see is as follows:
DO label count_variable = start, stop, step . fortran statement(s) . label CONTINUE
Here is a simple example that prints the cumulative sums of the integers from 1 through N (assume N has been assigned a value elsewhere):
INTEGER I, N, SUM SUM = 0 DO 10 I = 1, N SUM = SUM + I WRITE(*,*) 'I =', I WRITE(*,*) 'SUM =', SUM 10 CONTINUE
The number 10 is a statement label. Recall that column positions 1-5 are reserved for statement labels and the numerical value of statement labels has no significance (see the explanation of Statement Label and Column Position Rules in the above parts.
The variable defined in the DO-statement is incremented by 1 by default. However, you can define the step to be any number but zero. This program segment prints the even numbers between 1 and 10 in decreasing order:
INTEGER I DO I = 10, 1, -2 WRITE(*,*) 'i =', I END DO
ℜemember:
- Other statements within the loop must never change the DO-loop variable! Doing so may cause great confusion.
- The loop index can be of type REAL, but due to round-off errors, may not take on precisely the expected sequence of values.
5. Built-in Functions
Some useful functions have been included in Fortran. For example, most of the practical mathematical functions:
6. Array and Matrix Operations
Fortran operates on vectors and matrices much like Matlab:
6.1. Vectorial Operations:
In the statements like
U = SIN (V)
U and V can be vectors, so you do not have to calculate such statements member by member (in a loop).
6.2. Matrix Multiplication
MATMUL (A,B)
Which is of great importance in FEA. Note that:
C = A * B
is NOT a matrix multiplication. However, it will multiply the elements of A and B to create C.
6.3. Inner Product
DOT_PRODUCT (U,V)
Compute a general dot product that is particularly useful in Abaqus subroutines.
6.4. Dimension of Vectors and Matrices
LENGTH = SIZE (vector_array)
Returns LENGTH as a single integer (2, 3, 6, …) to determine the length of a vector_array.
DIMS = SHAPE (matrix_array)
Returns DIMS as an integer vector ( (2,2), (3,1), (3,3), …) to determine the dimensions of a matrix. For a 2D matrix, simply shows the number of rows and number of columns, respectively.
7. Writing data to files
In Fortran, the general syntax to write a file is:
WRITE (unit number, format string) list of variables
The unit number is an integer-valued variable that is defined when opening the file.
format string describes how the output is to be formatted (This is something like %s or %d in MATLAB or C).
For debugging purposes in your Abaqus subroutines, you can write to the default (unit number=6), which is the Printed Output file:
WRITE (6,*) "Number 1 = ", num1, "Number 2 = ", num2
In Fortran, * means default. So, this will simply use standard defaults to print integers, double-precision numbers, etc.
With the above statement, the values held by num1 and num2 variables can be displayed. The information inside the quotes is shown as it is, including capitalization and any spelling errors. When you do not use quotes, it is interpreted as a variable.
Notes:
- Also, you can use:
Write (*,*)
Since the default output is unit number=6 (Printed Output file) for Abaqus Subroutine Interface.
- For Abaqus/Explicit, printed output is a .log file (Log tab in Job Monitor window displays the contents of this file during the analysis). The counterpart for Abaqus/Standard is a .dat file (and Data tab). If you are more curious, take a look at Fortran unit numbers that Abaqus uses.
Default formatting is OK for printing a few variables but not much good for printing an array, which will be largely incomprehensible. The format string is a character string that has a special meaning for Fortran, for example:
‘I4’ an integer length 4
‘1X’ a single blank space
‘E12.4’ a floating-point number in Exponent format with total length 12 and 4 decimal places
A10 a character string with 10 characters
For a handy reference of these format codes, you can see the Format Statements in Stanford Fortran Tutorial.
8. How can we learn to write an Abaqus subroutine correctly?
Here below, I have listed some main Abaqus subroutines that you may encounter when using Abaqus at an advanced level as a graduate student or researcher. Good news for you! You can learn each subroutine you want by clicking on that.
In the below video, you can learn more about Fortran language as a beginner and start programming in Fortran:
Get this post as a PDF file: caeassistant.com Fortran for Abaqus subroutines-i
References:
- FORTRAN 77 for Engineers and Scientists with an Introduction to Fortran 90, L. Nyhoff and S. Leestma, Prentice Hall
- Fortran 77 Tutorial, Stanford University