-
-
Notifications
You must be signed in to change notification settings - Fork 360
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
leios
merged 13 commits into
algorithm-archivists:master
from
depate:monte_carlo_fortran
Sep 23, 2018
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
42e7698
first lines
depate fb31d58
added further code
depate 3b175e4
Figured the use of functions out
depate 906cc0b
Figured the use of functions out
depate 0d56e0f
Running program. 1 Warning left. Pi estimate cruel"
depate d0232d6
1M samples
depate 611fda8
formating style
depate 3896b3f
Applied the notes to the PR. Added correct precisions to constants.
depate 3e5b1e8
Added INTERFACE section to resolve raise of IMPLICIT error
depate 4d7acdc
Merge branch 'master' into monte_carlo_fortran
leios 2312537
Change code according to review comments
depate ba7a990
white spaces and spacing
depate 269fcc4
indexed?
depate File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,4 +62,8 @@ mukundan314 | |
<br> | ||
Trashtalk | ||
<br> | ||
Cyrus Burt | ||
Cyrus Burt | ||
<br> | ||
Patrik Tesarik | ||
<br> | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -135,6 +135,10 @@ | |
{ | ||
"lang": "nim", | ||
"name": "Nim" | ||
}, | ||
{ | ||
"lang": "f90", | ||
"name": "Fortran" | ||
} | ||
] | ||
} | ||
|
51 changes: 51 additions & 0 deletions
51
contents/monte_carlo_integration/code/fortran/monte_carlo.f90
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.