Skip to content

Monte Carlo integration Fortran 90 #363

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Sep 23, 2018
6 changes: 5 additions & 1 deletion CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ mukundan314
<br>
Trashtalk
<br>
Cyrus Burt
Cyrus Burt
<br>
Patrik Tesarik
<br>

4 changes: 4 additions & 0 deletions book.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@
{
"lang": "nim",
"name": "Nim"
},
{
"lang": "f90",
"name": "Fortran"
}
]
}
Expand Down
51 changes: 51 additions & 0 deletions contents/monte_carlo_integration/code/fortran/monte_carlo.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
FUNCTION in_circle(pos_x, pos_y, r)
IMPLICIT NONE
REAL(16), INTENT(IN) :: pos_x, pos_y, r
LOGICAL :: in_circle

in_circle = (pos_x ** 2 + pos_y ** 2) < r ** 2

END FUNCTION in_circle

PROGRAM monte_carlo
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to be clear: When I said "spacing" before, I was actually asking to index this block. Sorry for the confusion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, got it. This feels a bit odd because it's unfamiliar to see Fortran Code this way, but I'll get along with it.

Copy link
Member

@leios leios Sep 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, typo on my part. I meant "indent."

If it is not standard to indent fortran code, you don't have to. I don't know the standard here. I used to indent my stuff, but my advisor didn't. It seems that fortran doesn't require indenting here: http://www.fortran90.org/src/best-practices.html#modules-and-programs

I am sorry for the misunderstanding.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I understand the reference one can use indenting optionally. But in this case consistent use of 2, 3 or 4 spaces should be followed.

The reference manual only uses indentation for code control structures like DO blocks etc. But I also saw references, e.g. from companies, that take the indentation further for all structures e.g. FUNCTION and SUBROUTINE procedures.

So we are pretty much free to set our own convention. I would suggest to stick to 'indent all the things' scheme. You, and I guess, many other programmers or reviewers are used to indented source codes. But should any other contributor insist on the more historical 'tightly block and pack all the things', I suggest we may also accept this. Because the reference is only strict about consistency.

It may be nice to look into the history of the different FORTRAN references and their style in the not-yet-done section of the book.


IMPLICIT NONE

INTERFACE
FUNCTION in_circle(pos_x, pos_y, r)
IMPLICIT NONE
REAL(16), INTENT(IN) :: pos_x, pos_y, r
LOGICAL :: in_circle
END FUNCTION in_circle
END INTERFACE

INTEGER :: i,n
REAL(16) :: pos_x,pos_y, r, pi_est, pi_count, pi_error, pi

! Calculate Pi from trigonometric functions as reference
pi = DACOS(-1.d0)
n = 1000000
r = 1d0
pos_x = 0d0
pos_y = 0d0
pi_count = 0d0

DO i=0,n

CALL RANDOM_NUMBER(pos_x)
CALL RANDOM_NUMBER(pos_y)

IF (in_circle(pos_x, pos_y, r) .EQV. .TRUE.) THEN

pi_count = pi_count + 1d0

END IF
END DO

pi_est = 4d0 * pi_count / n
pi_error = 100d0 * (abs(pi_est - pi)/pi)

WRITE(*,'(A, F12.4)') 'The pi estimate is: ', pi_est
WRITE(*,'(A, F12.4, A)') 'Percent error is: ', pi_error, ' %'

END PROGRAM monte_carlo
4 changes: 4 additions & 0 deletions contents/monte_carlo_integration/monte_carlo_integration.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ each point is tested to see whether it's in the circle or not:
[import:6-7, lang:"nim"](code/nim/monte_carlo.nim)
{% sample lang="ruby" %}
[import:1-4, lang:"ruby"](code/ruby/monte_carlo.rb)
{% sample lang="f90" %}
[import:1-8, lang:"fortran"](code/fortran/monte_carlo.f90)
{% endmethod %}

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


Expand Down