Fortran ‘Must Knows’ for Writing Subroutines in Abaqus (PART II)

Fortran for Abaqus Subroutine

In the last blog post, Fortran ‘Must Knows’ for Writing Subroutines in Abaqus (PART I), we review some of the Fortran basics vital for us to write an Abaqus subroutine. We have also talked about different Fortran Data Types, including Numerical, Non-numerical Variables, and Arrays. Here, 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.

3. Operators

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.

Operators in Fortran


  • 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

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


      IF (X.LT.0) X = -X

II. More than one executable statement

      IF (logical expression) THEN 
      END IF

III. General Form

      IF (logical expression1) THEN
      ELSE IF (logical expression2) THEN
      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.


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)

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):

      SUM = 0
      DO 10 I = 1, N
         SUM = SUM + I
         WRITE(*,*) 'I =', I
         WRITE(*,*) 'SUM =', SUM

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 first part of this post: Fortran ‘Must Knows’ for Writing Subroutines in Abaqus (PART I))

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


  • 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:

Fortran Built-in 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


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, it 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 the quotes, it is interpreted as a variable.


  • 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.

Related Posts

Leave a Reply