Friday, May 12, 2017

Intro to Fortran


Due to a technical interview at Intel, I have to pick up this legacy language.
Fortran, derived from Formula Translation, is a general-purpose, imperative programming language that is especially suited to numeric computation and scientific computing. It was originally developed by IBM in the 1950s. It has still been used in computationally intensive areas due to its fast speed and existing software/packages.
.f file is used in early Fortran program written in a fixed-column format to reflect the 80-column punched-card practice. So it has very weird grammar:
  • in each line, first 1-5 are label fields, it can be c (comment) or number (notation for the code block)
  • 6th column. If it is something other than 0, it means the code continues from the previous line
  • 7~72. Real, independent codes
  • 73-80 are ignored because the IBM 704 card reader only had 72 columns
Smiley face
After Fortran 90, the Free Format is used and the file extension is .f90 In this format, the comment is signaled by ! and each line can be 132 symbols without the need of first 5 empty columns. The between line continuation is signaled by & at the end of the previous line as well as the head of next line.
Most commonly used versions today are: Fortran 77, Fortran 90, and Fortran 95. Newer versions such as Fortran 2008 only adds minor revision.

setup

There are several ways to install Fortran compiler/IDE.

1. GNU compiler

brew install gcc
🍺 /usr/local/Cellar/gcc/7.1.0: 1,485 files, 289.6MB
This GNU version of compiler bundles fortran and c together.

2. intel compiler

Intel® Parallel Studio XE Composer Edition for Fortran macOS*
Serial number : 26BK-MCT25TSK
expire 2018-5-12
But it turns out to be a compiler, which must be used along with Microsoft visual studio or Max Xcode.

3. eclipse Photran

run compiler

gfortran xx.f      # default output file is named a.out
gfortran xx.f -o xx  # customerize file name
./a.out   # execute file

tutorial

stanford

From time to time, so-called experts predict that Fortran will rapidly fade in popularity and soon become extinct. These predictions have always failed. Fortran is the most enduring computer programming language in history. One of the main reasons Fortran has survived and will survive is software inertia. Once a company has spent many man-years and perhaps millions of dollars on a software product, it is unlikely to try to translate the software to a different language. Reliable software translation is a very difficult task.
Use Fortrain 77 compiler on a Unix workstation.
Install libraries? Libraries have file names starting with lib and ending in .a. Some libraries have already been installed by your system administrator, usually in the directories /usr/lib and /usr/local/lib. For example, the BLAS library MAY be stored in the file /usr/local/lib/libblas.a. You use the -l option to link it together with your main program, e.g.
      f77 main.f -lblas

tutorials point

This website provides an online fortran 95 environment.
program title
implicit none  ! let compiler check all variables
real :: a, b, result ! declare variable type
a = 12.0
b = 15.0
result = a + b
print *, "the total is", result  ! * means format
write(*,*) reult ! similar to print, more variety
end program title  ! finish program
fortran is case insensitive.
variable type:
integer a
a = 1
real b
b = 1.0
real(kind=8) c ! declare bite size
c = 1e9
double precision cc ! double precision
cc = 1.578d10
complex d 
d = (3.2,2.5)  ! set complex value
character e ! declare one lette
character(len=10) f ! declear string size
f = "Hello"
logical h
h = .true.  ! note the weird dots
real, parameter :: pi = 3.14159 ! declare tyes and set initial value, using two colons
integer i, j
equivalence (i,j)  ! using the same meomory
mod (b,c)  ! equivalent to % in python
customized type
type :: person  ! begin to define a type person
    character(len=30) :: name
    interger :: age
end type person  ! finish defining the type
type(person) :: a  ! declare a person type variable
write(*,*) "name:" ! prompt user to input name
read(*,*) a%name  ! read user input into name
logical control
if (a>b) then
    print *, "a is larger than b"
else if (a== b) then
    print *, "a is equal to b"
else
    print *, "a is not larger than b"
end if
I happen to have a book “Fortran 95 程序设计” (彭国伦) which I bought 5 years ago but never get a chance to read it until now. It turns out to be extremely good. It not only introduces FORTRAN 95, but also mentions between its improvement over Fortran 77, and how some old-fashion styles such as goto should be discarded. It really helps me to understand some legacy codes within a couple of hours. I remembered how the old-fashion formatted Fortran codes scared me off when I first encounter Fortran. I master it now.