|Next page: Numerical derivative>
In this section, we will learn the basics of programming with Fortran. Specifically, we will learn about basic code writing, compilation methods, and how to execute programs, in addition to the basics of numerical computation such as arithmetic operations, DO loops, and IF statements.
First, let's look at the basic steps to execute a Fortran program on a computer. A Fortran program can be executed in the following steps:
.f90
.To learn this process of executing a Fortran program, we will write and execute a famous program known as the "Hello World" program. The "Hello World" program is a typical program used when learning programming for the first time and is one of the most famous programs. (Reference: Wikipedia)
.f90
in Fortran language.First, use any editor you like (Emacs, vim, Notepad, Sakura Editor, etc.) to write the following Fortran code, and save it with the file name lesson0_1.f90
program main
write(*,*)"Hello world!!"
end program main
The first line program main
indicates the start of the program and it runs in order from top to bottom until the corresponding end program main
. Here, main
is the name of the program, and you can give it any name you like. For simplicity, we are using the name main
here. The line write(*,*)"Hello world!!"
is a command to output the string "Hello world!!". The write
command used here is often employed for outputting various results.
Once the above Fortran code (lesson0_1.f90
) is ready, next, compile this code to create an executable file. Enter the following command in the terminal to compile and create the executable file from the code:
gfortran lesson0_1.f90 -o hello
Here, the gfortran
compiler is used, but you can use any compiler you like. The above command compiles the Fortran source file (lesson0_1.f90
) and creates an executable file (hello
). You can specify the name of the executable file using the compile option (-o
), and you can use any name other than hello
.
Once the above executable file (hello
) is ready, execute it by entering the following command:
./hello
As a result, the following string will be output on the terminal:
Hello World!!
This output is produced by the second line of the code lesson0_1.f90, the statement write(*,*)"Hello world!!"
.
To recap what we learned about Fortran programming here, you can execute a Fortran program in the following three steps: (i) Write code with the extension .f90
in Fortran language. (ii) Compile the Fortran source code and generate an executable file. (iii) Execute the executable file and obtain the results. This is the general process for Fortran programming, and we will continue to look at similar processes for other programming examples.
Almost all numerical calculation methods and codes are realized by a combination of basic mathematical operations such as arithmetic operations (addition, subtraction, multiplication, division). Here, we will learn about such basic mathematical operations (arithmetic operations, exponentiation). First, let's look at the following sample program.
program main
implicit none
real(8) :: a,b,c,d,e,f,g
a = 2.0d0
b = 3.0d0
write(*,*)"a=",a
write(*,*)"b=",b
c = a + b
d = a - b
e = a * b
f = a / b
g = a**b
write(*,*)"a+b =",c
write(*,*)"a-b =",d
write(*,*)"a*b =",e
write(*,*)"a/b =",f
write(*,*)"a**b=",g
end program main
In this program, arithmetic operations (addition, subtraction, multiplication, division), as well as exponentiation, are performed on variables a
and b
. As seen in the previous Lesson 0-1, this program also starts with the program
statement and ends with the end program
statement, and the instructions within this range are executed in order from the top.
After the program
statement, the implicit none
statement is declared. By declaring this implicit none
statement, you can disable the implicit type declaration described next. Implicit type declaration is a feature of Fortran language's initial setting for variable types, where variables starting with i, j, k, l
are treated as integer type, and all other variables are treated as real type. Using this implicit type declaration increases the risk of unintended bugs in the program, so always declare the implicit none
statement to disable this feature when writing new programs.
Next, under the implicit none
statement, variables (a-g
) are defined as follows:
real(8) :: a,b,c,d,e,f,g
Here, the type of the variables is defined as real(8)
. This real(8)
is a type called double precision real, treated as a real number with approximately 16 digits of precision. In the declaration statement of the above program, seven variables (a,b,c,d,e,f,g
) are declared as double precision real variables (real(8)
).
Besides double precision real, other types such as integer type integer
and double precision complex complex(8)
can also be used.
After the declaration of variable types, you can see the following code:
a = 2.0d0
b = 3.0d0
Here, 2.0d0
represents a double precision real number value, \(2.0 \times 10^0\). For example, to use a double precision real number value of \(3.9 \times 10^2\), it can be expressed as 3.9d2
. In the above code, the value \(2.0 \times 10^0\) is assigned to variable a
, and \(3.0 \times 10^0\) to variable b
. In Fortran language, the equal sign (=
) is used not for equivalence but as an assignment operator.
In lines 8 and 9 of the program, the values assigned to variables a, b
are output to the screen:
write(*,*)"a=",a
write(*,*)"b=",b
Here, the write
statement is used to output variables. This write statement was also used in the above Lesson 0-1. Unlike the Hello World program example above, this example involves output of both string variables and real variables. In Fortran, strings can be expressed by enclosing them in double quotes ("). For example, in the code (lesson0_2.f90) example above, the string a=
is represented in the code as "a="
. This method of expression was also used in the above Hello World program. In this example, in addition to string variables, the double precision real variables a
and b
are also output.
Next, let's look at the part of the code where the actual calculations are performed. In lines 11 to 15 of the above program, arithmetic operations and exponentiation calculations are performed using variables a, b
.
c = a + b
d = a - b
e = a * b
f = a / b
g = a**b
Here, basic mathematical operations are performed on double precision real variables, and the results are assigned to double precision real variables c, d, e, f, g
. In Fortran language, the +
symbol represents addition, the -
symbol represents subtraction, the *
symbol represents multiplication, the /
symbol represents division, and the **
symbol represents exponentiation. After performing these operations, the results are output using the write
statement in this example.
Next, let's compile and run the program. To compile, execute the following command in the terminal::
gfortran lesson0_2.f90 -o lesson0_2
After the compilation is complete, execute the executable file as follows.
./lesson0_2
Then, the following calculation results should be output on the screen:
a= 2.0000000000000000
b= 3.0000000000000000
a+b = 5.0000000000000000
a-b = -1.0000000000000000
a*b = 6.0000000000000000
a/b = 0.66666666666666663
a**b= 8.0000000000000000
Do these results match what you predicted? Also, try changing the numbers assigned to the double precision real variables a
and b
in the above program, and see how the results change.
In many numerical calculation methods, it is often necessary to repeatedly iterate a set procedure. In the Fortran language, a feature called a do
loop is implemented for such iterative processes. In this section, we will learn how to use the do
loop.
As an example for learning the do
loop, let's consider a program that calculates the sum of integers from \(1\) to \(10\). The answer to this problem can be found using the following simple formula:
\[ S=1+2+3+\cdots+n = \frac{1}{2}n(n+1). \tag{1} \]
Let's consider the following program to numerically calculate this sum of integers using a do
loop.
program main
implicit none
integer :: i, n, sum
n = 10
sum = 0
do i = 1, n
sum = sum + i
end do
write(*,*)"calculated sum =",sum
write(*,*)"expected sum =",n*(n+1)/2
end program main
In the above code, first in line 3, integer type variables i, n, sum
are defined as follows:
integer :: i, n, sum
Next, in line 5 of the code, an integer n
, which represents up to which integer the sum will be calculated, is given. Here, n
is set to 10
.
Furthermore, in line 6, the variable sum
, which stores the calculated sum, is initialized by inserting a zero value with sum=0
. Variables can hold random values with no reproducibility if they are just declared in the declaration statement. Therefore, it is necessary to initialize variables by explicitly assigning values like zero as needed.
Next, from line 7, we move to the do
loop block. The do
loop block starts with the do
statement and ends with the end do
statement. The do
loop repeats the procedure inside the do
block. As shown in the code, the do
statement is used together with an integer variable:
do i = 1, n
Here, i
is an integer variable and is called a loop counter, used to control the number of iterations of the do
loop. In this line, the loop counter, the integer i
, is initially set to 1
. The procedure inside the do
block enclosed by the do
and end do
statements is repeated, and with each iteration, the loop counter i
is incremented by 1
(i=i+1
). This process repeats until the loop counter i
exceeds n
, and the do
loop finishes when i
exceeds n
. In the above program, as the loop counter i
increases from 1
to n
, the value of the loop counter i
is added to the integer variable sum
. Here, sum = sum + i
represents assigning the new value of sum
after adding i
to the original value of sum
.
Now, let's compile and execute the above program (lesson0_3.f90). The following results should be obtained.
calculated sum = 55
expected sum = 55
Here, the calculated sum
shows the value obtained by adding numbers from 1
to 10
in the above program, while the expected sum
shows the value calculated using the formula (\(1\)). Therefore, it can be confirmed that the sum calculated by the program correctly reproduces the value derived mathematically from the formula.
When performing numerical calculations, different processes may be required depending on given conditions. In such cases, you can use the if
statement to perform conditional branching. To understand the function of this if
statement, let's look at the following Fortran code.
program main
implicit none
real(8) :: a, b
a = 2d0
b = 3d0
write(*,*)"a=",a
write(*,*)"b=",b
if(a == b)then
write(*,*)"a==b"
else if(a > b)then
write(*,*)"a>b"
else
write(*,*)"a<b"
end if
end program main
In the above program, the if(a == b)then
statement in line 11 makes a determination if a
is equal to b
, and if a
and b
are equal, the statement below it, write(*,*)"a==b"
, displays the string "a==b" on the screen. In this case, no other actions of the if
statement are performed, and it moves to the end if
statement to finish the conditional branching.
If the condition provided in the first conditional statement if(a == b)then
is not met, it moves to the next condition. In the code above, else if(a > b)then
determines whether a
is greater than b
, and if a
is greater than b
, the statement write(*,*)"a>b"
outputs the string "a>b" and moves to the end if
statement. In this way, multiple layers of conditional determinations can be made using else if (condition) then
.
Up to now, we have explained what happens when a condition is met, but handling cases when conditions are not met (exception handling) is also important in programming. In the program above, after the if(a == b)then
and else if(a > b)then
statements, an else
statement follows. This else
statement describes what to do if none of the previous conditions are met. In the example of the above program, since it is already known that a
and b
are not equal and a
is not greater than b
, it outputs "a<b"
, indicating that a
is less than b
, and finishes the if
statement.
if
statements can use various conditions, but in many cases, knowing the following conditions is sufficient for writing a program.
a==b
: a
is equal to b
.a>b
: a
is greater than b
.a<b
: a
is less than b
.a>=b
: a
is greater than or equal to b
.a<=b
: a
is less than or equal to b
.a/=b
: a
is not equal to b
.In this page, we learned about the basics of Fortran programming. In the next page, we will learn about numerical differentiation using Fortran.
Numerical Differentiation in Fortran
|Next page: Numerical derivative>