Getting Started with Fortran FYI

Home Documentation

Getting Started with Fortran

Welcome to Fortran FYI! This guide will help you get started with Fortran programming, from installation to writing your first scientific computing programs.

What is Fortran?

Fortran (derived from “Formula Translation”) is a programming language originally developed by IBM in the 1950s for scientific and engineering applications. It remains one of the most important languages for high-performance numerical computing, weather prediction, computational fluid dynamics, and other computationally intensive applications.

Key Features

  • High Performance: Optimized for numerical and scientific computing
  • Array Processing: Built-in support for multi-dimensional arrays
  • Mathematical Functions: Extensive intrinsic mathematical functions
  • Parallel Computing: Native support for coarrays and OpenMP
  • Legacy and Modern: Supports both legacy code and modern programming paradigms
  • Standards: Well-defined standards (Fortran 90, 95, 2003, 2008, 2018, 2023)

Fortran Standards Evolution

Fortran 77

Classic Fortran with fixed-form source format.

Fortran 90/95

Major modernization with free-form source, modules, and dynamic arrays.

Fortran 2003

Object-oriented features, C interoperability.

Fortran 2008

Coarrays for parallel programming, submodules.

Fortran 2018

Teams, events, and collective subroutines for parallel computing.

Fortran 2023

Latest standard with enhanced parallel features and generic programming.

Installation

macOS:

# Using Homebrew
brew install gcc

# Using MacPorts
sudo port install gcc12 +gfortran

# Verify installation
gfortran --version

Linux:

# Ubuntu/Debian
sudo apt update && sudo apt install gfortran

# CentOS/RHEL
sudo yum install gcc-gfortran

# Fedora
sudo dnf install gcc-gfortran

Windows:

Intel Fortran Compiler

Intel oneAPI (Free):

# Download from Intel oneAPI website
# Includes Intel Fortran Compiler (ifx/ifort)

Other Compilers

  • NAG Fortran: Commercial compiler with excellent diagnostics
  • PGI/NVIDIA HPC SDK: Optimized for GPU computing
  • IBM XL Fortran: For IBM systems

Verify Installation

gfortran --version

Your First Fortran Program

Hello World

Create a file called hello.f90:

program hello_world
    implicit none
    write(*,*) 'Hello, Fortran FYI!'
end program hello_world

Compile and run:

gfortran hello.f90 -o hello
./hello

Simple Calculation

program simple_math
    implicit none
    real :: x, y, result
    
    x = 3.14159
    y = 2.71828
    result = x + y
    
    write(*,*) 'Sum of pi and e:', result
end program simple_math

Basic Concepts

Variables and Data Types

program data_types
    implicit none
    
    ! Integer types
    integer :: count = 10
    integer(kind=8) :: big_number = 1234567890123_8
    
    ! Real types
    real :: temperature = 98.6
    real(kind=8) :: precise_value = 3.141592653589793_8
    
    ! Complex numbers
    complex :: impedance = (3.0, 4.0)
    
    ! Character strings
    character(len=20) :: name = "Fortran"
    character(len=:), allocatable :: dynamic_string
    
    ! Logical
    logical :: is_converged = .false.
    
    write(*,*) 'Count:', count
    write(*,*) 'Temperature:', temperature
    write(*,*) 'Name:', trim(name)
end program data_types

Arrays

program arrays_example
    implicit none
    integer, parameter :: n = 5
    real :: vector(n)
    real :: matrix(3,3)
    real, allocatable :: dynamic_array(:)
    integer :: i
    
    ! Initialize vector
    vector = [1.0, 2.0, 3.0, 4.0, 5.0]
    
    ! Initialize matrix
    matrix = reshape([1,2,3,4,5,6,7,8,9], [3,3])
    
    ! Dynamic array
    allocate(dynamic_array(10))
    dynamic_array = [(real(i), i=1,10)]
    
    write(*,*) 'Vector:', vector
    write(*,*) 'Matrix diagonal:', [(matrix(i,i), i=1,3)]
    
    deallocate(dynamic_array)
end program arrays_example

Procedures (Subroutines and Functions)

program procedures_example
    implicit none
    real :: a = 5.0, b = 3.0, result
    
    ! Call subroutine
    call add_numbers(a, b, result)
    write(*,*) 'Sum:', result
    
    ! Call function
    result = multiply_numbers(a, b)
    write(*,*) 'Product:', result
end program procedures_example

subroutine add_numbers(x, y, sum)
    implicit none
    real, intent(in) :: x, y
    real, intent(out) :: sum
    
    sum = x + y
end subroutine add_numbers

function multiply_numbers(x, y) result(product)
    implicit none
    real, intent(in) :: x, y
    real :: product
    
    product = x * y
end function multiply_numbers

Modules

module math_operations
    implicit none
    private
    public :: factorial, fibonacci
    
contains
    
    function factorial(n) result(fact)
        integer, intent(in) :: n
        integer :: fact, i
        
        fact = 1
        do i = 1, n
            fact = fact * i
        end do
    end function factorial
    
    function fibonacci(n) result(fib)
        integer, intent(in) :: n
        integer :: fib, prev, curr, i
        
        if (n <= 2) then
            fib = 1
        else
            prev = 1
            curr = 1
            do i = 3, n
                fib = prev + curr
                prev = curr
                curr = fib
            end do
        end if
    end function fibonacci
    
end module math_operations

program use_module
    use math_operations
    implicit none
    
    write(*,*) 'Factorial of 5:', factorial(5)
    write(*,*) 'Fibonacci of 10:', fibonacci(10)
end program use_module

Scientific Computing Examples

Numerical Integration

program integration
    implicit none
    real(kind=8) :: a = 0.0_8, b = 1.0_8
    integer :: n = 1000000
    real(kind=8) :: result
    
    result = trapezoidal_rule(a, b, n)
    write(*,*) 'Integral of x^2 from 0 to 1:', result
    write(*,*) 'Analytical result: 1/3 =', 1.0_8/3.0_8
    
contains
    
    function f(x) result(y)
        real(kind=8), intent(in) :: x
        real(kind=8) :: y
        y = x**2
    end function f
    
    function trapezoidal_rule(a, b, n) result(integral)
        real(kind=8), intent(in) :: a, b
        integer, intent(in) :: n
        real(kind=8) :: integral, h, x
        integer :: i
        
        h = (b - a) / real(n, kind=8)
        integral = 0.5_8 * (f(a) + f(b))
        
        do i = 1, n-1
            x = a + i * h
            integral = integral + f(x)
        end do
        
        integral = integral * h
    end function trapezoidal_rule
    
end program integration

Linear Algebra

program linear_algebra
    implicit none
    integer, parameter :: n = 3
    real(kind=8) :: A(n,n), b(n), x(n)
    integer :: i, j
    
    ! Initialize matrix A and vector b for Ax = b
    A = reshape([2.0_8, 1.0_8, 1.0_8, &
                 1.0_8, 3.0_8, 2.0_8, &
                 1.0_8, 2.0_8, 4.0_8], [n,n])
    b = [4.0_8, 5.0_8, 6.0_8]
    
    ! Solve using Gaussian elimination (simplified)
    call gaussian_elimination(A, b, x, n)
    
    write(*,*) 'Solution:'
    do i = 1, n
        write(*,*) 'x(', i, ') =', x(i)
    end do
end program linear_algebra

subroutine gaussian_elimination(A, b, x, n)
    implicit none
    integer, intent(in) :: n
    real(kind=8), intent(inout) :: A(n,n), b(n)
    real(kind=8), intent(out) :: x(n)
    real(kind=8) :: factor
    integer :: i, j, k
    
    ! Forward elimination
    do k = 1, n-1
        do i = k+1, n
            factor = A(i,k) / A(k,k)
            do j = k, n
                A(i,j) = A(i,j) - factor * A(k,j)
            end do
            b(i) = b(i) - factor * b(k)
        end do
    end do
    
    ! Back substitution
    do i = n, 1, -1
        x(i) = b(i)
        do j = i+1, n
            x(i) = x(i) - A(i,j) * x(j)
        end do
        x(i) = x(i) / A(i,i)
    end do
end subroutine gaussian_elimination

Build Systems

Simple Compilation

gfortran -O3 -o program program.f90

Makefile Example

FC = gfortran
FFLAGS = -O3 -Wall
SOURCES = main.f90 math_module.f90
OBJECTS = $(SOURCES:.f90=.o)
TARGET = simulation

$(TARGET): $(OBJECTS)
	$(FC) $(FFLAGS) -o $@ $^

%.o: %.f90
	$(FC) $(FFLAGS) -c $<

clean:
	rm -f *.o *.mod $(TARGET)

CMake Example

cmake_minimum_required(VERSION 3.12)
project(FortranProject Fortran)

set(CMAKE_Fortran_STANDARD 2018)

add_executable(simulation
    main.f90
    math_module.f90
)

# Link external libraries
find_package(BLAS REQUIRED)
find_package(LAPACK REQUIRED)
target_link_libraries(simulation ${BLAS_LIBRARIES} ${LAPACK_LIBRARIES})

Next Steps

Explore these advanced topics:

  1. Modern Fortran - Fortran 2008+ features
  2. Parallel Programming - OpenMP, Coarrays, MPI
  3. Numerical Libraries - BLAS, LAPACK, FFTW
  4. HPC Techniques - Optimization and performance
  5. Interoperability - C/Python integration

Mathematical Libraries

  • BLAS: Basic Linear Algebra Subprograms
  • LAPACK: Linear Algebra PACKage
  • FFTW: Fast Fourier Transform library
  • GSL: GNU Scientific Library

Parallel Computing

  • OpenMP: Shared memory parallelization
  • MPI: Message Passing Interface
  • Coarrays: Native Fortran parallel arrays

Specialized Libraries

  • NetCDF: Scientific data formats
  • HDF5: Hierarchical data format
  • PETSc: Scalable linear algebra

Resources

Ready to dive into scientific computing? Check out our comprehensive documentation for detailed guides on numerical methods, parallel programming, and high-performance computing techniques!