More Fortran
More Fortran
Fortran is a compiled language where you write the program in one or more source files that are compiled into objects that are linked together to produce an executable.
Intrinsic Data Types:
Defining precisions
use iso_c_binding integer, parameter:: sp = c_float integer, parameter:: dp = c_double integer, parameter:: li = c_long
Arrays are very flexible in Fortran.
The first dimension is contiguous in memory.
Dynamic allocation by allocate
real, dimension(-1:3) :: a
real(-2:2) :: b
real, dimension(3,0:2) :: c
integer, parameter :: n = 10
integer, dimension(n,n,n,n) :: i4
real, allocatable, dimension(:,:) :: a
allocate(a(10,0:33))
deallocate(a)
if (i .ge. 3) then p = 1.0 else if (i .eq. 1) then p = 2.0 else p = 1.0 end ifn = 10 ; stride = 2 sum = 0.0 do i = 1,n,stride sum = sum + 1.0 end do
There are three program units in Fortran
PROGRAM
MODULE
SUBROUTINE
/ FUNCTION
A basic example: quaternions \(q = a+bi+cj+dk\)
module quat_module use type_defs implicit none ! type quat real(kind=dp) :: q(4) end type quad contains subroutine get_real(qt,qt_real) use type_defs type(quat), intent(in) :: qt real(dp), intent(out) :: qt_real qt_real = qt%q(1) end subroutine end module quat_module
module quat_module use type_defs implicit none ! Alternative type quat real(kind=dp) :: qim(3) real(kind=dp) :: qre end type quad contains subroutine get_inv(qt,qt_inv) use type_defs type(quat), intent(in) :: qt type(quat), intent(out) :: qt_inv real(kind=dp) :: di di = 1.0_dp / (sum((qt%qim)**2) + (qt%qim)**2) qt_inv%qre = di*qt%qre qt_inv%qim = -di*qt%qim end subroutine end module quat_module
program apa use type_defs use quat_module implicit none type(quat) :: q1,q2,q3 q1%qre = 1.0_dp q1%qim = 1.0e3_dp call get_inv(q1,q2) call get_inv(q2,q3) end program apa
$ gfortran -c type_defs.f90 $ gfortran -c quat_module.f90 $ gfortran -c apa.f90 $ gfortran -o apa.x apa.o quad_module.o type_defs.o
Once the number of files become moderately large this method does not work and we will use make instead.