! It is a good practise to use comments in a source code. A comment
! with Fortran can be easily written by adding a "!" on the first
! column and here we go: whatever comes after the "!" will be
! skipped by the compiler (don't know what is a compiler? give a look
! at https://www.giuseppeforte.me). Comments can be used to explain
! the goals of the program and thus making it understandable
! for any future reference. You might find useful something like
! the following lines.
!**************************
! This program computes the value of Greek Pi using a Monte Carlo
! Algorithm.
! (refere to [1] and https://www.giuseppeforte.me) for more
! details on the algorithm).
!
! Author: Giuseppe Forte
! First release: 25/05/2017
! e-mail: giuseppe.forte@giuseppeforte.me
! Website: https://www.giuseppeforte.me
! License: Creative Commons Attribution-ShareAlike 4.0
! International (CC BY-SA 4.0)
! (https://creativecommons.org/licenses/by-sa/4.0/)
! REFERENCES:
! [1] Heermann, Dieter W., and K. Binder. "Monte Carlo Simulation
! in Statistical Physics. Springer-Verlag Berlin Heidelberg, 2010.
!!!!!! THE PROGRAM STARTS HERE
!!!!!! THE PROGRAM STARTS HERE
! IN FORTRAN DO NOT WRITE THE INSTRUCTIONS STARTING FROM THE FIRST
! COLUMN. ALWAYS START FROM COLUMN 10
program PiMonteCarlo ! "PiMoteCarlo" is the program name
! (your own choice)
implicit none
integer i, i1 !Dummy variables. We will use them in the loops
integer Nb ! "Nballs" is an integer variable in single
! precision. It represents the number of balls
! we wanto to throw on our unitary square
integer Nc ! Fraction of balls fallen in the unitary
! circle
real*8 Pi,x,y ! "Pi", "x" and "y" are real variables in
! double precision:
! Pi = Greek Pi
! x = a variable to which we will associate a
! random number
! y = a variable to which we will
! associate a random number
real*8 Dist ! Dist = Square root of (x^2 + y^2)
! it is the distance of the point (x,y) from
! the origin
real*8 Pi_From_Wiki ! Pi_From_Wiki = The value of Pi taken
! from Wikipedia
print*, "*************************************************"
print*, "This program evaluates Greek Pi with a simple "
print*, "Monte Carlo method."
print*, "*************************************************"
print*, ""
print*, "Insert the number of balls you want to throw:"
read(*,*) Nb
open(unit=18, file="ReadPi_Wiki.dat",status="old") ! open
!the already existing file "ReadPi_Wiki.dat". N.B.= the
! file "ReadPi_Wiki.dat" MUST be placed in the same folder
! of the current source code
read(18,*) Pi_From_Wiki ! Read the value of Pi written in
! the file "ReadPi_Wiki.dat"
close(18) ! close the file "ReadPi_Wiki.dat". We no longer
! need it
Nc = 0 !Initialize Nc to zero
do 100 i=1, Nb ! loop over "i" (i --> dummy variable)
! "100" is a label (your own choice)
call random_number(x) ! Assign to "x" and "y" two random
call random_number(y) ! numbers, uniformly dostributed
! between 0 and 1
Dist = sqrt(x**2.0d0 + y**2.0d0) ! distance from the
! origin
if(Dist .le. 1.0d0)then ! condition. If we are inside the
! circle, increase Nc by one,
! otherwise do not execute any
! operation and throw another ball
Nc = Nc + 1
end if
100 end do ! here again we find the label "100". In this case
! the label is needless, however when we have a lot
! of loops, especially nested loops in a single
! program, labelling them may help reading the
! the source code
Pi = dble(Nc)/dble(Nb)
Pi = 4.0d0*Pi ! evaluate Pi
! Let's print the result on the screen
print*,"**************************************************"
print*,""
print*,"The estimated value of Pi. is:"
print*,"single precision:", real(Pi)
print*,"double precision:", Pi
print*,"The error with respect the value taken from "
print*,"Wikipedia is written in the file Error.dat "
print*,"**************************************************"
print*,""
print*,"END OF THE PROGRAM!"
! Let's print the result on an external file
open(unit=19,file="Error.dat",status="unknown")!open the new
! file "Error.dat"
write(19,*)"**********************************************"
write(19,*)"Error:"
write(19,*)"(Pi Calculated - Pi from wiki) / Pi from wiki:"
write(19,*)"single precision:",
& real(abs(Pi-Pi_From_Wiki)*100.0d0/Pi_From_Wiki), "%"
write(19,*)"double precision:",
& abs(Pi-Pi_From_Wiki)*100.0d0/Pi_From_Wiki, "%"
write(19,*)"END OF THE PROGRAM!"
close(19) !close file "Error.dat"
end program PiMonteCarlo ! end of the program ... cheers :-)