Skip to content
Extraits de code Groupes Projets
Valider 3fb222d0 rédigé par Pierre-Yves Barriat's avatar Pierre-Yves Barriat
Parcourir les fichiers

Final release

parent 55947267
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -191,3 +191,5 @@ sympy-plots-for-*.tex/ ...@@ -191,3 +191,5 @@ sympy-plots-for-*.tex/
# Notebooks # Notebooks
notebooks/*.ipynb_checkpoints notebooks/*.ipynb_checkpoints
notebooks/*dataxy
notebooks/*gnuxy
%% Cell type:markdown id:10d0e3c5 tags:
## Introduction to structured programming with Fortran
### Why to learn Fortran ?
* Because of the execution speed of a program
* Well suited for numerical computations : more than **45% of scientific applications** are in Fortran
## Getting started
### Hello World
%% Cell type:code id:00ec1776 tags:
``` Fortran
program hello_world
implicit none ! important
print *, "Hello World!"
end program hello_world
```
%% Cell type:markdown id:c4b788f0 tags:
### Data Type Declarations and Assignments
%% Cell type:code id:ffd3f0c3 tags:
``` Fortran
program data_type
implicit none
real x, y
integer i, j
logical flag
integer matrix(2,2)
character(80) month
character(len=80) months(12)
character family*16
real, dimension(12) :: small_array
character(len=80), dimension(24) :: screen
integer, parameter :: it = 100
i = 1
j = i+2
x = 85.8
y = 3.5*cos(x)
month="december"
months(:)="empty"
months(12)=month
flag = .TRUE.
family = "GEORGE P. BURDELL"
print*,family(:6)
print*,family(8:9)
print*,family(11:)
print*,family(:6)//FAMILY(10:)
end
```
%% Cell type:markdown id:133046ed tags:
### Arithmetic Assignments
The result of any integer divide is truncated to the integer value less than the correct decimal answer for the division. The result of this is that changing the order of operations can make a big difference in the answers. Notice how parentheses force more expected results.
%% Cell type:code id:13343800 tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ians1=i2*i3/i5
ians2=i3/i5*i2
ians3=i2*(i3/i5)
ians4=(i3/i5)*i2
print *, '2*3/5 =', ians1, ', 3/5*2 =',ians2,', 2*(3/5) =',ians3 ,', (3/5)*2 =',ians4
end program arith
```
%% Cell type:markdown id:cabea667 tags:
Real arithmetic behaves more uniformly:
%% Cell type:code id:e1de5730 tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ans1=r2*r3/r5
ans2=r3/r5*r2
ans3=(r3/r5)*r2
print *, '2.0*3.0/5.0 =', ans1, ', 3.0/5.0*2.0 =',ans2,', (3.0/5.0)*2.0 =',ans3
end program arith
```
%% Cell type:markdown id:f18b85ec tags:
Watch how precedence of operations effects the following:
%% Cell type:code id:9a362858 tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ians1=i2+i5*i3**i2
ians2=i5*i3**i2+i2
ians3=i3**i2*i5+i2
print *, '2+5*3**2 =',ians1,', 5*3**2+2 =',ians2, ', 3**2*5+2 =',ians3
end program arith
```
%% Cell type:markdown id:5297b017 tags:
You can mix real and integers, but watch what happens
%% Cell type:code id:7d50669e tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ans1=r5+i3/i2
ans2=5.0+3/2
print *, '5.0+3/2 =',ans1
print *, '5.0+3/2 =',ans2
end program arith
```
%% Cell type:markdown id:222cabbc tags:
Look at what happens when I put a real in either the numerator or denominator of the division term
%% Cell type:code id:78929c6e tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ans1=r5+i3/r2
ans2=r5+r3/i2
print *, '5.0+3/2.0 =',ans1, ', 5.0+3.0/2 =', ans2
end program arith
```
%% Cell type:markdown id:a7d37bc2 tags:
Although Fortran normally works from left to right at a given level of precedence (does all multiply and divide from left to right before moving on to adds and subtracts). It works exponentiation from right to left when it hits 2 or more sequential exponentiation operations
%% Cell type:code id:3c2da1a9 tags:
``` Fortran
program arith
implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/
ians1= i5**i3**i2
ians2= (i5**i3)**i2
ians3= i5**(i3**i2)
print *, '5**3**2 =',ians1, ', (5**3)**2 =',ians2, ', 5**(3**2) =',ians3
end program arith
```
%% Cell type:markdown id:8387be7a tags:
When in doubt use parentheses to get the answer that you really want.
### Assignments exercise
%% Cell type:code id:14f6e7e2 tags:
``` Fortran
program sphere
implicit none
real pi,radius,volume,area
radius = 1.0
pi = 0.0
write(*,*) 'The value of pi is ', pi
write(*,*)
area = 0.0
volume = 0.0
write(*,*) 'For a radius ', radius
write(*,*) 'the area of a sphere is ', area
write(*,*) 'and the volume is ', volume
end
```
%% Cell type:markdown id:9a1702f7 tags:
### Execution Control
%% Cell type:code id:41f9a8f0 tags:
``` Fortran
PROGRAM gcd
! Computes the greatest common divisor, Euclidean algorithm
IMPLICIT NONE
INTEGER :: m, n, t
WRITE(*,*) "Give positive integers m and n :"
m=5464
n=484682
WRITE(*,*) 'm:', m,' n:', n
positive_check: IF (m > 0 .AND. n > 0) THEN
main_algorithm: DO WHILE (n /= 0)
t = MOD(m,n)
m = n
n = t
END DO main_algorithm
WRITE(*,*) "Greatest common divisor: ",m
ELSE
WRITE(*,*) 'Negative value entered'
END IF positive_check
END PROGRAM gcd
```
%% Cell type:markdown id:cef2ad42 tags:
### File-Directed Input and Output
%% Cell type:code id:8fd042c4 tags:
``` Fortran
program plot
! Program to provide plots of Sin(x)
implicit none
character label*150
real x
integer i
character xlabel*32,ylabel*32,title*32
real fx
!
! label - Character string
! xlabel - Contains a label for the x-axis
! ylabel - Contains a label for the y-axis
! title - Contains a title for the plot
!
! Drive a separate true graphics program (gnuplot)
!
! First set up the command file for gnuplot
!
xlabel="'x'"
ylabel="'y'"
title="'sin(x)'"
open (112,file='03_gnuxy')
!
label='set xlabel '//xlabel
write(112,*)label
write(112,*)'set xrange [0:6]'
label='set ylabel '//ylabel
write(112,*)label
write(112,*)'set yrange [-1.2:1.2]'
label='plot "03_dataxy" using 1:2 title '//title
label=trim(label)//' with lines lt rgb "red"'
write(112,*) label
write (112,*) 'pause -1'
close(112)
!
! Generate x-y pairs for the graph
!
open (112,file='03_dataxy')
do i=0,60
x=.1*i
fx=sin(x)
write(112,*) x,fx
enddo
close(112)
!
end program
```
%% Cell type:markdown id:059d5e2a tags:
This code is going to create 2 files: "03_dataxy" and "03_gnuxy".
The idea is to use a linux plotting tool called "GNUplot" to make a graph: the first file is the data for the graph, the second one is the gnuplot script using these data.
```bash
gnuplot 03_gnuxy
```
<img src="https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/raw/master/assets/sin.png">
#### Format controlled IO
%% Cell type:code id:a26b3cc5 tags:
``` Fortran
program format
implicit none
real(4) A
real(8) B
integer I, KEY
A= 1500.*acos(0.)
B= 4e3*dcos(16.5d0)
KEY= 884
write(*,*) A,B,KEY
write(*,*)
write(*,1000) A,B,KEY
write(*,1001) ("-",I=1,38)
1000 FORMAT(F18.10,E14.5,I6)
1001 FORMAT(38A1)
end program format
```
%% Cell type:markdown id:31f27416 tags:
### Hands on learning: numerical analysis
Apply the Newton's method to find the root(s) of a polynomial function.
<img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/6929060731e351c465426e37567abe5ee13d65d9">
%% Cell type:code id:df362a1a tags:
``` Fortran
program newton
implicit none
! Use a Newton iteration to solve a polynomial equation
integer :: i
write(*,*) 'Try to solve "x**3+x-10=0"'
write(*,*) 'What is your initial guess for the solution?'
do while( .TRUE. )
write(*,*)
exit
end do
end program
! ******************************************************************************************
subroutine derivate()
! Evaluate the function f(x)=x**3+x-10
! also return the derivative of the function
end subroutine
```
%% Cell type:markdown id:919d1d4f tags:
### COMMON Statement (F77)
%% Cell type:code id:4b517d6b tags:
``` Fortran
PROGRAM test_arg
implicit none
integer a,b,c
common /arg/ a,b,c
a = 2
c = 1
print *, 'Before the call:'
print *, 'a = ',a,', b = ',b,', c = ',c
call sub
print *, 'After the call:'
print *, 'a = ',a,', b = ',b,', c = ',c
END PROGRAM
SUBROUTINE sub
implicit none
integer a,b,c
common /arg/ a,b,c
b = a + c
c = c + 1
END SUBROUTINE
```
%% Cell type:markdown id:1e457015 tags:
### MODULE Statement (F90)
%% Cell type:code id:71f26006 tags:
``` Fortran
MODULE arg
implicit none
integer :: a,b,c
real(8) :: x
END MODULE arg
! * * * * * * *
PROGRAM test_arg
USE arg
implicit none
a = 2
c = 1
write(*,*) 'Before the call:'
write(*,'(3(A5,I3))') ' a = ',a,', b = ',b,', c = ',c
call sub
write(*,*) 'After the call:'
write(*,'(3(A5,I3))') 'a = ',a,', b = ',b,', c = ',c
END PROGRAM test_arg
! * * * * * * *
SUBROUTINE sub
USE arg, only : a,b,c ! seuls a b et c sont utiles
implicit none
b = a + c
c = c + 1
END SUBROUTINE sub
```
%% Cell type:markdown id:a0900a54 tags:
### NAMELIST (F90)
%% Cell type:code id:e3791d4f tags:
``` Fortran
PROGRAM test_namelist
implicit none
real(8) lon_min, lon_max, lat_min, lat_max
NAMELIST/namlon/ lon_min, lon_max
NAMELIST/namlat/ lat_min, lat_max
write(*,*) 'Before:'
call print_res(lon_min, lon_max, lat_min, lat_max)
open(161,file='../src/07_namelist.def',status='old',form='formatted')
read(161,NML=namlon)
write(*,*) 'Between:'
call print_res(lon_min, lon_max, lat_min, lat_max)
read(161,NML=namlat)
close (161)
write(*,*) 'After:'
call print_res(lon_min, lon_max, lat_min, lat_max)
END
SUBROUTINE print_res(a,b,c,d)
implicit none
real(8), intent(in) :: a,b,c,d
write(*,'(4(A12,F6.2))') ' lon_min = ',a,', lon_max = ',b, &
', lat_min = ',c,', lat_max = ',d
RETURN
END
```
%% Cell type:markdown id:c0b0a801 tags:
### Hands on learning: structure
The aim of this exercise is to work with loops (for or while) in order to draw in a terminal a Christmas tree with its balls.
The program must be carried out in Fortran 90. It will take as argument the height of the tree which is a variable of the problem:
Height=7
```
#
###
#o###
##o####
#o#####o#
####o#####o
#####o#####o#
```
This parameter must be supplied at the command line when the program (for example './tree 10').
Balls must be positioned all 6 sharps.
%% Cell type:code id:930eedf9 tags:
``` Fortran
program ChristmasTree
implicit none
end program
```
%% Cell type:code id:6362d74e tags:
``` Fortran
```
%% Cell type:markdown id:10d0e3c5 tags: %% Cell type:markdown id:10d0e3c5 tags:
## Introduction to structured programming with Fortran ## Introduction to structured programming with Fortran
### Why to learn Fortran ? ### Why to learn Fortran ?
* Because of the execution speed of a program * Because of the execution speed of a program
* Well suited for numerical computations : more than **45% of scientific applications** are in Fortran * Well suited for numerical computations : more than **45% of scientific applications** are in Fortran
### Getting started ## Getting started
#### Hello World ### Hello World
%% Cell type:code id:00ec1776 tags: %% Cell type:code id:00ec1776 tags:
``` Fortran ``` Fortran
program hello_world program hello_world
implicit none ! important implicit none ! important
print *, "Hello World!" print *, "Hello World!"
end program hello_world end program hello_world
``` ```
%% Cell type:markdown id:c4b788f0 tags: %% Cell type:markdown id:c4b788f0 tags:
#### Data Type Declarations and Assignments ### Data Type Declarations and Assignments
%% Cell type:code id:ffd3f0c3 tags: %% Cell type:code id:ffd3f0c3 tags:
``` Fortran ``` Fortran
program data_type program data_type
implicit none implicit none
real x, y real x, y
integer i, j integer i, j
logical flag logical flag
integer matrix(2,2) integer matrix(2,2)
character(80) month character(80) month
character(len=80) months(12) character(len=80) months(12)
character family*16 character family*16
real, dimension(12) :: small_array real, dimension(12) :: small_array
character(len=80), dimension(24) :: screen character(len=80), dimension(24) :: screen
integer, parameter :: it = 100 integer, parameter :: it = 100
i = 1 i = 1
j = i+2 j = i+2
x = 85.8 x = 85.8
y = 3.5*cos(x) y = 3.5*cos(x)
month="december" month="december"
months(:)="empty" months(:)="empty"
months(12)=month months(12)=month
flag = .TRUE. flag = .TRUE.
family = "GEORGE P. BURDELL" family = "GEORGE P. BURDELL"
print*,family(:6) print*,family(:6)
print*,family(8:9) print*,family(8:9)
print*,family(11:) print*,family(11:)
print*,family(:6)//FAMILY(10:) print*,family(:6)//FAMILY(10:)
end end
``` ```
%% Cell type:markdown id:133046ed tags: %% Cell type:markdown id:133046ed tags:
#### Arithmetic Assignments ### Arithmetic Assignments
The result of any integer divide is truncated to the integer value less than the correct decimal answer for the division. The result of this is that changing the order of operations can make a big difference in the answers. Notice how parentheses force more expected results. The result of any integer divide is truncated to the integer value less than the correct decimal answer for the division. The result of this is that changing the order of operations can make a big difference in the answers. Notice how parentheses force more expected results.
%% Cell type:code id:13343800 tags: %% Cell type:code id:13343800 tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ians1=i2*i3/i5 ians1=i2*i3/i5
ians2=i3/i5*i2 ians2=i3/i5*i2
ians3=i2*(i3/i5) ians3=i2*(i3/i5)
ians4=(i3/i5)*i2 ians4=(i3/i5)*i2
print *, '2*3/5 =', ians1, ', 3/5*2 =',ians2,', 2*(3/5) =',ians3 ,', (3/5)*2 =',ians4 print *, '2*3/5 =', ians1, ', 3/5*2 =',ians2,', 2*(3/5) =',ians3 ,', (3/5)*2 =',ians4
end program arith end program arith
``` ```
%% Cell type:markdown id:cabea667 tags: %% Cell type:markdown id:cabea667 tags:
Real arithmetic behaves more uniformly: Real arithmetic behaves more uniformly:
%% Cell type:code id:e1de5730 tags: %% Cell type:code id:e1de5730 tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ans1=r2*r3/r5 ans1=r2*r3/r5
ans2=r3/r5*r2 ans2=r3/r5*r2
ans3=(r3/r5)*r2 ans3=(r3/r5)*r2
print *, '2.0*3.0/5.0 =', ans1, ', 3.0/5.0*2.0 =',ans2,', (3.0/5.0)*2.0 =',ans3 print *, '2.0*3.0/5.0 =', ans1, ', 3.0/5.0*2.0 =',ans2,', (3.0/5.0)*2.0 =',ans3
end program arith end program arith
``` ```
%% Cell type:markdown id:f18b85ec tags: %% Cell type:markdown id:f18b85ec tags:
Watch how precedence of operations effects the following: Watch how precedence of operations effects the following:
%% Cell type:code id:9a362858 tags: %% Cell type:code id:9a362858 tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ians1=i2+i5*i3**i2 ians1=i2+i5*i3**i2
ians2=i5*i3**i2+i2 ians2=i5*i3**i2+i2
ians3=i3**i2*i5+i2 ians3=i3**i2*i5+i2
print *, '2+5*3**2 =',ians1,', 5*3**2+2 =',ians2, ', 3**2*5+2 =',ians3 print *, '2+5*3**2 =',ians1,', 5*3**2+2 =',ians2, ', 3**2*5+2 =',ians3
end program arith end program arith
``` ```
%% Cell type:markdown id:5297b017 tags: %% Cell type:markdown id:5297b017 tags:
You can mix real and integers, but watch what happens You can mix real and integers, but watch what happens
%% Cell type:code id:7d50669e tags: %% Cell type:code id:7d50669e tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ans1=r5+i3/i2 ans1=r5+i3/i2
ans2=5.0+3/2 ans2=5.0+3/2
print *, '5.0+3/2 =',ans1 print *, '5.0+3/2 =',ans1
print *, '5.0+3/2 =',ans2 print *, '5.0+3/2 =',ans2
end program arith end program arith
``` ```
%% Cell type:markdown id:222cabbc tags: %% Cell type:markdown id:222cabbc tags:
Look at what happens when I put a real in either the numerator or denominator of the division term Look at what happens when I put a real in either the numerator or denominator of the division term
%% Cell type:code id:78929c6e tags: %% Cell type:code id:78929c6e tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ans1=r5+i3/r2 ans1=r5+i3/r2
ans2=r5+r3/i2 ans2=r5+r3/i2
print *, '5.0+3/2.0 =',ans1, ', 5.0+3.0/2 =', ans2 print *, '5.0+3/2.0 =',ans1, ', 5.0+3.0/2 =', ans2
end program arith end program arith
``` ```
%% Cell type:markdown id:a7d37bc2 tags: %% Cell type:markdown id:a7d37bc2 tags:
Although Fortran normally works from left to right at a given level of precedence (does all multiply and divide from left to right before moving on to adds and subtracts). It works exponentiation from right to left when it hits 2 or more sequential exponentiation operations Although Fortran normally works from left to right at a given level of precedence (does all multiply and divide from left to right before moving on to adds and subtracts). It works exponentiation from right to left when it hits 2 or more sequential exponentiation operations
%% Cell type:code id:3c2da1a9 tags: %% Cell type:code id:3c2da1a9 tags:
``` Fortran ``` Fortran
program arith program arith
implicit none implicit none
real r2,r3,r4,r5,r6,ans1,ans2,ans3 real r2,r3,r4,r5,r6,ans1,ans2,ans3
integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4 integer i2,i3,i4,i5,i6,ians1,ians2,ians3,ians4
data r2/2./,r3/3./,r4/4.0/,r5/5.0/ data r2/2./,r3/3./,r4/4.0/,r5/5.0/
data i2,i3,i4,i5/2,3,4,5/ data i2,i3,i4,i5/2,3,4,5/
ians1= i5**i3**i2 ians1= i5**i3**i2
ians2= (i5**i3)**i2 ians2= (i5**i3)**i2
ians3= i5**(i3**i2) ians3= i5**(i3**i2)
print *, '5**3**2 =',ians1, ', (5**3)**2 =',ians2, ', 5**(3**2) =',ians3 print *, '5**3**2 =',ians1, ', (5**3)**2 =',ians2, ', 5**(3**2) =',ians3
end program arith end program arith
``` ```
%% Cell type:markdown id:8387be7a tags: %% Cell type:markdown id:8387be7a tags:
When in doubt use parentheses to get the answer that you really want. When in doubt use parentheses to get the answer that you really want.
#### Assignments exercise ### Assignments exercise
%% Cell type:code id:14f6e7e2 tags: %% Cell type:code id:14f6e7e2 tags:
``` Fortran ``` Fortran
program sphere program sphere
implicit none implicit none
real pi,radius,volume,area real pi,radius,volume,area
radius = 1.0 radius = 1.0
pi = 0.0 pi = 0.0
write(*,*) 'The value of pi is ', pi write(*,*) 'The value of pi is ', pi
write(*,*) write(*,*)
area = 0.0 area = 0.0
volume = 0.0 volume = 0.0
write(*,*) 'For a radius ', radius write(*,*) 'For a radius ', radius
write(*,*) 'the area of a sphere is ', area write(*,*) 'the area of a sphere is ', area
write(*,*) 'and the volume is ', volume write(*,*) 'and the volume is ', volume
end end
``` ```
%% Cell type:markdown id:9a1702f7 tags: %% Cell type:markdown id:9a1702f7 tags:
#### Execution Control ### Execution Control
%% Cell type:code id:41f9a8f0 tags: %% Cell type:code id:41f9a8f0 tags:
``` Fortran ``` Fortran
PROGRAM gcd PROGRAM gcd
! Computes the greatest common divisor, Euclidean algorithm ! Computes the greatest common divisor, Euclidean algorithm
IMPLICIT NONE IMPLICIT NONE
INTEGER :: m, n, t INTEGER :: m, n, t
WRITE(*,*) "Give positive integers m and n :" WRITE(*,*) "Give positive integers m and n :"
m=5464 m=5464
n=484682 n=484682
WRITE(*,*) 'm:', m,' n:', n WRITE(*,*) 'm:', m,' n:', n
positive_check: IF (m > 0 .AND. n > 0) THEN positive_check: IF (m > 0 .AND. n > 0) THEN
main_algorithm: DO WHILE (n /= 0) main_algorithm: DO WHILE (n /= 0)
t = MOD(m,n) t = MOD(m,n)
m = n m = n
n = t n = t
END DO main_algorithm END DO main_algorithm
WRITE(*,*) "Greatest common divisor: ",m WRITE(*,*) "Greatest common divisor: ",m
ELSE ELSE
WRITE(*,*) 'Negative value entered' WRITE(*,*) 'Negative value entered'
END IF positive_check END IF positive_check
END PROGRAM gcd END PROGRAM gcd
``` ```
%% Cell type:markdown id:cef2ad42 tags: %% Cell type:markdown id:cef2ad42 tags:
#### File-Directed Input and Output ### File-Directed Input and Output
%% Cell type:code id:8fd042c4 tags: %% Cell type:code id:8fd042c4 tags:
``` Fortran ``` Fortran
program plot program plot
! Program to provide plots of Sin(x) ! Program to provide plots of Sin(x)
implicit none implicit none
character label*150 character label*150
real x real x
integer i integer i
character xlabel*32,ylabel*32,title*32 character xlabel*32,ylabel*32,title*32
real fx real fx
! !
! label - Character string ! label - Character string
! xlabel - Contains a label for the x-axis ! xlabel - Contains a label for the x-axis
! ylabel - Contains a label for the y-axis ! ylabel - Contains a label for the y-axis
! title - Contains a title for the plot ! title - Contains a title for the plot
! !
! Drive a separate true graphics program (gnuplot) ! Drive a separate true graphics program (gnuplot)
! !
! First set up the command file for gnuplot ! First set up the command file for gnuplot
! !
xlabel="'x'" xlabel="'x'"
ylabel="'y'" ylabel="'y'"
title="'sin(x)'" title="'sin(x)'"
open (112,file='03_gnuxy') open (112,file='03_gnuxy')
! !
label='set xlabel '//xlabel label='set xlabel '//xlabel
write(112,*)label write(112,*)label
write(112,*)'set xrange [0:6]' write(112,*)'set xrange [0:6]'
label='set ylabel '//ylabel label='set ylabel '//ylabel
write(112,*)label write(112,*)label
write(112,*)'set yrange [-1.2:1.2]' write(112,*)'set yrange [-1.2:1.2]'
label='plot "03_dataxy" using 1:2 title '//title label='plot "03_dataxy" using 1:2 title '//title
label=trim(label)//' with lines lt rgb "red"' label=trim(label)//' with lines lt rgb "red"'
write(112,*) label write(112,*) label
write (112,*) 'pause -1' write (112,*) 'pause -1'
close(112) close(112)
! !
! Generate x-y pairs for the graph ! Generate x-y pairs for the graph
! !
open (112,file='03_dataxy') open (112,file='03_dataxy')
do i=0,60 do i=0,60
x=.1*i x=.1*i
fx=sin(x) fx=sin(x)
write(112,*) x,fx write(112,*) x,fx
enddo enddo
close(112) close(112)
! !
end program end program
``` ```
%% Cell type:markdown id:31f27416 tags: %% Cell type:markdown id:059d5e2a tags:
This code is going to create 2 files: "03_dataxy" and "03_gnuxy". This code is going to create 2 files: "03_dataxy" and "03_gnuxy".
The idea is to use a linux plotting tool called "GNUplot" to make a graph: the first file is the data for the graph, the second one is the gnuplot script using these data. The idea is to use a linux plotting tool called "GNUplot" to make a graph: the first file is the data for the graph, the second one is the gnuplot script using these data.
```bash ```bash
gnuplot 03_gnuxy gnuplot 03_gnuxy
``` ```
<img src="https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/raw/master/assets/sin.png"> <img src="https://gogs.elic.ucl.ac.be/pbarriat/learning-fortran/raw/master/assets/sin.png">
#### Format controlled IO
%% Cell type:code id:a26b3cc5 tags:
``` Fortran
program format
implicit none
real(4) A
real(8) B
integer I, KEY
A= 1500.*acos(0.)
B= 4e3*dcos(16.5d0)
KEY= 884
write(*,*) A,B,KEY
write(*,*)
write(*,1000) A,B,KEY
write(*,1001) ("-",I=1,38)
1000 FORMAT(F18.10,E14.5,I6)
1001 FORMAT(38A1)
end program format
```
%% Cell type:markdown id:31f27416 tags:
### Hands on learning: numerical analysis
Apply the Newton's method to find the root(s) of a polynomial function.
<img src="https://wikimedia.org/api/rest_v1/media/math/render/svg/6929060731e351c465426e37567abe5ee13d65d9">
%% Cell type:code id:df362a1a tags: %% Cell type:code id:df362a1a tags:
``` Fortran ``` Fortran
program newton program newton
implicit none implicit none
! Use a Newton iteration to solve a polynomial equation ! Use a Newton iteration to solve a polynomial equation
integer :: i integer :: i
write(*,*) 'Try to solve "x**3+x-10=0"' write(*,*) 'Try to solve "x**3+x-10=0"'
write(*,*) 'What is your initial guess for the solution?' write(*,*) 'What is your initial guess for the solution?'
do while( .TRUE. ) do while( .TRUE. )
write(*,*) write(*,*)
exit exit
end do end do
end program end program
! ****************************************************************************************** ! ******************************************************************************************
subroutine derivate() subroutine derivate()
! Evaluate the function f(x)=x**3+x-10 ! Evaluate the function f(x)=x**3+x-10
! also return the derivative of the function ! also return the derivative of the function
end subroutine end subroutine
``` ```
%% Output %% Cell type:markdown id:919d1d4f tags:
Try to solve "x**3+x-10=0" ### COMMON Statement (F77)
What is your initial guess for the solution?
%% Cell type:code id:4b517d6b tags: %% Cell type:code id:4b517d6b tags:
``` Fortran ``` Fortran
PROGRAM test_arg PROGRAM test_arg
implicit none implicit none
integer a,b,c integer a,b,c
common /arg/ a,b,c common /arg/ a,b,c
a = 2 a = 2
c = 1 c = 1
print *, 'Before the call:' print *, 'Before the call:'
print *, 'a = ',a,', b = ',b,', c = ',c print *, 'a = ',a,', b = ',b,', c = ',c
call sub call sub
print *, 'After the call:' print *, 'After the call:'
print *, 'a = ',a,', b = ',b,', c = ',c print *, 'a = ',a,', b = ',b,', c = ',c
END PROGRAM END PROGRAM
SUBROUTINE sub SUBROUTINE sub
implicit none implicit none
integer a,b,c integer a,b,c
common /arg/ a,b,c common /arg/ a,b,c
b = a + c b = a + c
c = c + 1 c = c + 1
END SUBROUTINE END SUBROUTINE
``` ```
%% Output %% Cell type:markdown id:1e457015 tags:
Before the call: ### MODULE Statement (F90)
a = 2 , b = 0 , c = 1
After the call:
a = 2 , b = 3 , c = 2
%% Cell type:code id:71f26006 tags: %% Cell type:code id:71f26006 tags:
``` Fortran ``` Fortran
MODULE arg MODULE arg
implicit none implicit none
integer :: a,b,c integer :: a,b,c
real(8) :: x real(8) :: x
END MODULE arg END MODULE arg
! * * * * * * * ! * * * * * * *
PROGRAM test_arg PROGRAM test_arg
USE arg USE arg
implicit none implicit none
a = 2 a = 2
c = 1 c = 1
write(*,*) 'Before the call:' write(*,*) 'Before the call:'
write(*,'(3(A5,I3))') ' a = ',a,', b = ',b,', c = ',c write(*,'(3(A5,I3))') ' a = ',a,', b = ',b,', c = ',c
call sub call sub
write(*,*) 'After the call:' write(*,*) 'After the call:'
write(*,'(3(A5,I3))') 'a = ',a,', b = ',b,', c = ',c write(*,'(3(A5,I3))') 'a = ',a,', b = ',b,', c = ',c
END PROGRAM test_arg END PROGRAM test_arg
! * * * * * * * ! * * * * * * *
SUBROUTINE sub SUBROUTINE sub
USE arg, only : a,b,c ! seuls a b et c sont utiles USE arg, only : a,b,c ! seuls a b et c sont utiles
implicit none implicit none
b = a + c b = a + c
c = c + 1 c = c + 1
END SUBROUTINE sub END SUBROUTINE sub
``` ```
%% Output %% Cell type:markdown id:a0900a54 tags:
Before the call: ### NAMELIST (F90)
a = 2, b = 0, c = 1
After the call:
a = 2, b = 3, c = 2
%% Cell type:code id:e3791d4f tags: %% Cell type:code id:e3791d4f tags:
``` Fortran ``` Fortran
PROGRAM test_namelist PROGRAM test_namelist
implicit none implicit none
real(8) lon_min, lon_max, lat_min, lat_max real(8) lon_min, lon_max, lat_min, lat_max
NAMELIST/namlon/ lon_min, lon_max NAMELIST/namlon/ lon_min, lon_max
NAMELIST/namlat/ lat_min, lat_max NAMELIST/namlat/ lat_min, lat_max
write(*,*) 'Before:' write(*,*) 'Before:'
call print_res(lon_min, lon_max, lat_min, lat_max) call print_res(lon_min, lon_max, lat_min, lat_max)
open(161,file='../src/07_namelist.def',status='old',form='formatted') open(161,file='../src/07_namelist.def',status='old',form='formatted')
read(161,NML=namlon) read(161,NML=namlon)
write(*,*) 'Between:' write(*,*) 'Between:'
call print_res(lon_min, lon_max, lat_min, lat_max) call print_res(lon_min, lon_max, lat_min, lat_max)
read(161,NML=namlat) read(161,NML=namlat)
close (161) close (161)
write(*,*) 'After:' write(*,*) 'After:'
call print_res(lon_min, lon_max, lat_min, lat_max) call print_res(lon_min, lon_max, lat_min, lat_max)
END END
SUBROUTINE print_res(a,b,c,d) SUBROUTINE print_res(a,b,c,d)
implicit none implicit none
real(8), intent(in) :: a,b,c,d real(8), intent(in) :: a,b,c,d
write(*,'(4(A12,F6.2))') ' lon_min = ',a,', lon_max = ',b, & write(*,'(4(A12,F6.2))') ' lon_min = ',a,', lon_max = ',b, &
', lat_min = ',c,', lat_max = ',d ', lat_min = ',c,', lat_max = ',d
RETURN RETURN
END END
``` ```
%% Output %% Cell type:markdown id:c0b0a801 tags:
### Hands on learning: structure
The aim of this exercise is to work with loops (for or while) in order to draw in a terminal a Christmas tree with its balls.
The program must be carried out in Fortran 90. It will take as argument the height of the tree which is a variable of the problem:
Height=7
```
#
###
#o###
##o####
#o#####o#
####o#####o
#####o#####o#
```
This parameter must be supplied at the command line when the program (for example './tree 10').
Balls must be positioned all 6 sharps.
%% Cell type:code id:930eedf9 tags:
Before: ``` Fortran
lon_min = 0.00, lon_max = -0.00, lat_min = 0.00, lat_max = -0.00 program ChristmasTree
Between:
lon_min = 0.00, lon_max = 360.00, lat_min = 0.00, lat_max = -0.00 implicit none
After:
lon_min = 0.00, lon_max = 360.00, lat_min = -90.00, lat_max = 90.00
end program
```
%% Cell type:code id:27748335 tags: %% Cell type:code id:6362d74e tags:
``` Fortran ``` Fortran
``` ```
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter