Skip to content

Commit 7994074

Browse files
depateleios
authored andcommitted
Monte Carlo integration Fortran 90 (#363)
* first lines * added further code * Figured the use of functions out * Figured the use of functions out * Running program. 1 Warning left. Pi estimate cruel" * 1M samples * formating style * Applied the notes to the PR. Added correct precisions to constants. * Added INTERFACE section to resolve raise of IMPLICIT error * Change code according to review comments * white spaces and spacing * indexed?
1 parent 0f16f25 commit 7994074

File tree

4 files changed

+64
-1
lines changed

4 files changed

+64
-1
lines changed

CONTRIBUTORS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,4 +62,8 @@ mukundan314
6262
<br>
6363
Trashtalk
6464
<br>
65-
Cyrus Burt
65+
Cyrus Burt
66+
<br>
67+
Patrik Tesarik
68+
<br>
69+

book.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@
135135
{
136136
"lang": "nim",
137137
"name": "Nim"
138+
},
139+
{
140+
"lang": "f90",
141+
"name": "Fortran"
138142
}
139143
]
140144
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
FUNCTION in_circle(pos_x, pos_y, r)
2+
IMPLICIT NONE
3+
REAL(16), INTENT(IN) :: pos_x, pos_y, r
4+
LOGICAL :: in_circle
5+
6+
in_circle = (pos_x ** 2 + pos_y ** 2) < r ** 2
7+
8+
END FUNCTION in_circle
9+
10+
PROGRAM monte_carlo
11+
12+
IMPLICIT NONE
13+
14+
INTERFACE
15+
FUNCTION in_circle(pos_x, pos_y, r)
16+
IMPLICIT NONE
17+
REAL(16), INTENT(IN) :: pos_x, pos_y, r
18+
LOGICAL :: in_circle
19+
END FUNCTION in_circle
20+
END INTERFACE
21+
22+
INTEGER :: i,n
23+
REAL(16) :: pos_x,pos_y, r, pi_est, pi_count, pi_error, pi
24+
25+
! Calculate Pi from trigonometric functions as reference
26+
pi = DACOS(-1.d0)
27+
n = 1000000
28+
r = 1d0
29+
pos_x = 0d0
30+
pos_y = 0d0
31+
pi_count = 0d0
32+
33+
DO i=0,n
34+
35+
CALL RANDOM_NUMBER(pos_x)
36+
CALL RANDOM_NUMBER(pos_y)
37+
38+
IF (in_circle(pos_x, pos_y, r) .EQV. .TRUE.) THEN
39+
40+
pi_count = pi_count + 1d0
41+
42+
END IF
43+
END DO
44+
45+
pi_est = 4d0 * pi_count / n
46+
pi_error = 100d0 * (abs(pi_est - pi)/pi)
47+
48+
WRITE(*,'(A, F12.4)') 'The pi estimate is: ', pi_est
49+
WRITE(*,'(A, F12.4, A)') 'Percent error is: ', pi_error, ' %'
50+
51+
END PROGRAM monte_carlo

contents/monte_carlo_integration/monte_carlo_integration.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ each point is tested to see whether it's in the circle or not:
6969
[import:6-7, lang:"nim"](code/nim/monte_carlo.nim)
7070
{% sample lang="ruby" %}
7171
[import:1-4, lang:"ruby"](code/ruby/monte_carlo.rb)
72+
{% sample lang="f90" %}
73+
[import:1-8, lang:"fortran"](code/fortran/monte_carlo.f90)
7274
{% endmethod %}
7375

7476
If it's in the circle, we increase an internal count by one, and in the end,
@@ -137,6 +139,8 @@ Feel free to submit your version via pull request, and thanks for reading!
137139
[import, lang:"nim"](code/nim/monte_carlo.nim)
138140
{% sample lang="ruby" %}
139141
[import, lang:"ruby"](code/ruby/monte_carlo.rb)
142+
{% sample lang="f90" %}
143+
[import, lang:"fortran"](code/fortran/monte_carlo.f90)
140144
{% endmethod %}
141145

142146

0 commit comments

Comments
 (0)