Comparison of Fortran and Python side by side:
https://fortran-lang.org/learn/rosetta_stone/
Fortran examples:
Here i am giving simple fortran90 examples in the beginning. These examples mainly deal with how to control
the input and output formatting and more.
INPUT/OUTPUT statements in FORTRAN:
OPEN - open a file.
READ - read from opened file or from command line.
CLOSE - once a file is read, it is always good to close it at the end of the program or may be in the middle.
WRITE - print the output.
PRINT - similar to write statement.
INQUIRE - inquires about file status.
REWIND - rewinds or moves the file position indicator to the beginning of file.
BACKSPACE - rewinds a file one record back - should be avoided.
ENDFILE - marks the end of file.
FORMAT - used to output/input the data in specific manner or format.
Derived types or user defined data types in fortran90
============================================
So far we know that fortran provides us intrinsic data types, and using it we can define data of different
types such as integer, real, array, complex or character type. But the problem comes when we have a variable
which can be of different types. Then there we need to define derived data types (similar to structures in C language).
In Fortran90 every element of derived type variable is accessed by % sign (just as . in C language).
-------------------------------------------------------------
How to define derived data type:
Say, we want to define "date" as derived data type so that it can contain date, month and year in it.
TYPE date
INTEGER :: day=4
INTEGER :: month=4
INTEGER :: year=2013
END TYPE date
------------------------------------------------------------
How to use derived data type i.e. date
TYPE (date) :: d
WRITE( *,*) d%day, d%month, d%year
-----------------------------------------------------------
Above i have defined d as derived type of type date. Derived types should be declared in modules, if you
want to use them in multiple subroutines.
example 1:
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
! ==========================================
! Example using derived types and modules.
! ==========================================
module struct_def
type struct ! Define new type 'struct', which
real :: atom_no ! is made up of two reals, an
real :: atom_tau ! integer and a character.
integer :: mesh
character(len=10) :: atom_label
end type struct
end module struct_def
program main
use struct_def ! Associate module struct_def with main.
type(struct) system1, system2 ! Declare two variables of type 'struct'.
system1 = struct(2.,44.8,1200,"H") ! Assign value to system1.
system2%atom_no = 10. ! Assign value to parts
system2%atom_tau = 91.284 ! of system2.
system2%mesh = 2550
system2%atom_label = 'Ho'
Write(*,*) "WELCOME: to my home page at google site."
Write(*,*) "https://sites.google.com/site/1009ukumar/"
write(*,*)
write (*,*)"========================================"
write(*,*) "Example using derived types and module."
write (*,*)"========================================"
write(*,*)
write(*,*) "Printing the values of two derived type variables."
write(*,*) "format : values with separation using 1x or 2x and so on."
write(*,"(2f5.1,x,i5,1x,a5)") system1%atom_no,system1%atom_tau,system1%mesh,system1%atom_label
write(*,"(2f5.1,x,i5,1x,a5)") system2%atom_no,system2%atom_tau,system2%mesh,system2%atom_label
write(*,*) "with different format : values in new line using /"
write(*,"(f5.1,/,f5.1,/,i5,/,a5)") system1%atom_no,system1%atom_tau,system1%mesh,system1%atom_label
write(*,"(f5.1,/,f5.1,/,i5,/,a5)") system2%atom_no,system2%atom_tau,system2%mesh,system2%atom_label
end program main
Output of example 1:
WELCOME: to my home page at google site.
https://sites.google.com/site/1009ukumar/
========================================
Example using derived types and module.
========================================
Printing the values of two derived type variables.
format : values with separation using 1x or 2x and so on.
2.0 44.8 1200 H
10.0 91.3 2550 Ho
with different format : values in new line using /
2.0
44.8
1200
H
10.0
91.3
2550
Ho
======================
POINTERS in fortran90:
======================
Pointers in fortran90 are different than the way they are used in other programming languages such as C. They don't
correspond to memory addresses like in other languages rather are used as aliases.
EXAMPLE 2 :
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
!Program to find the angle between two vectors in degrees.
!==================================================
! Excercises done by S. Kumar, IIT Delhi 2011.
! formula used :
! cos_theta=(dotproduct/(mod_vecA*mod_vecB))
!==================================================
program angle
implicit none
!real, save, dimension(10) :: cpu_times,wall_times
real,dimension(3) :: vecA, vecB
real :: dotproduct,mod_vecA,mod_vecB,cos_theta,theta
integer :: i
double precision,Parameter :: pi=3.141592653589793
!---------
write(*,*) " Enter vector A "
! read this way
!Do i=1,3
! read(*,*) vecA(i)
!Enddo
! OR this way ......note sonu
read(*,*) (vecA(i),i=1,3)
write(*,*) " Enter vector B "
! Do i=1,3
! read(*,*) vecB(i)
! Enddo
read(*,*) (vecB(i),i=1,3)
write(*, *) " Vector A is"
! Do i=1,3
! write(*, "(f9.5)") vecA(i)
! Enddo
write(*, "(6x,f9.5)") (vecA(i),i=1,3)
write(*, *) " Vector B is"
! Do i=1,3
! write(*,"(f9.5)") vecB(i)
! Enddo
write(*,"(6x,f9.5)") (vecB(i),i=1,3)
write(*,*)
write(*,"(6x,a)") " NOTE : both loops write these vectors A and B columwise &
& unless you use format statement 3F9.5, so using this statement, &
& we have:"
write(*, *)
write(*, *) " Vector A is"
write(*,"(6x,3f9.5)") (vecA(i),i=1,3)
write(*, *) " Vector B is"
write(*,"(6x,3f9.5)") (vecB(i),i=1,3)
write(*,*)
dotproduct=(vecA(1)*vecB(1)+vecA(2)*vecB(2)+vecA(3)*vecB(3))
write(*,*) " dot product is", dotproduct
mod_vecA=sqrt(vecA(1)*vecA(1)+vecA(2)*vecA(2)+vecA(3)*vecA(3))
write(*,*) " modulus of vector A is ", mod_vecA
mod_vecB=sqrt(vecB(1)*vecB(1)+vecB(2)*vecB(2)+vecB(3)*vecB(3))
write(*,*) " modulus of vector B is ", mod_vecB
cos_theta=(dotproduct/(mod_vecA*mod_vecB))
write(*,*)
write(*,"(a,f5.1)") " Cosine(AB) is", cos_theta
theta=((180.0*acos(cos_theta))/pi)
write(*,"(a,f5.1,a)") " Angle between vector A and B =", theta,"(degree)"
end program angle
Output of example 2:
Enter vector A
1 2 3
Enter vector B
1 2 3
Vector A is
1.00000
2.00000
3.00000
Vector B is
1.00000
2.00000
3.00000
NOTE : both loops write these vectors A and B columwise unless you use format statement 3F9.5, so using this statement, we have:
Vector A is
1.00000 2.00000 3.00000
Vector B is
1.00000 2.00000 3.00000
dot product is 14.00000
modulus of vector A is 3.741657
modulus of vector B is 3.741657
Cosine(AB) is 1.0
Angle between vector A and B = 0.0(degree)
NOTE: Here you get angle not exactly to zero (if you consider more significant digits), better work with double precision digits.
Example 3: