-
-
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
Monte Carlo integration Fortran 90 #363
Conversation
Thanks a bunch and welcome to the AAA community! A few notes:
Sorry there is so much bookkeeping to do! I'll take a look at the actual code soon. I have a lot of PR's to go through, but this one is Fortran, so it's on my list too. Thanks again! |
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.
So building the book, it says include-codeblock: unknown language
fortran, use default
Which would mean that fortran is not indexed by include-codeblock and shouldn't have syntax highlighting, but for some reason the syntax highlighting works anyway, so I guess we can just leave it.
Also: My fortran is a bit rusty, so I asked some stupid questions
@@ -0,0 +1,45 @@ | |||
FUNCTION in_circle(pos_x, pos_y, r) | |||
IMPLICIT NONE |
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.
I realize that fortran77 doesn't usually have indenting for functions, but I thought everything about F90 did? I don't know if this is standard or not, but it might help with readability
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.
TL;DR: You're right. For the sake of code readability indentation is optionally used. I think I did indent in the other code contributions. I will use a Python-like indentation format, four white spaces respectively. But we can discuss another convention. I am open minded about this.
Some Literature:
As I looked around in the documentation and "standards" there is no definition about how many or if one should use indents at all.
- Here there is only indentation for INTERFACE and DO routines with four white spaces, but 2-3 are also legit: https://www.fortran90.org/src/best-practices.html
- https://en.wikipedia.org/wiki/Fortran_95_language_features and https://www.csee.umbc.edu/~squire/fortranclass/summary.shtml#Intr suggest that after F90 indentation got used more seriously e.g. in functions and modules as well.
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.
Yeah, it seems inconsistent. I don't care the number of spaced used here, but I think indentation is nice. I used to use 6 spaces because that was what Fortran77 required and I needed backward compatibility for my old boss.
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.
I used to use 6 spaces because that was what Fortran77 required
Ah, that's interesting. I was already wondering, why my programming helper addon in VIM always put 6 spaces in.
END FUNCTION in_circle | ||
|
||
PROGRAM monte_carlo | ||
IMPLICIT NONE |
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.
Again, a bit of spacing would help readability.
pi_est = 4d0 * pi_count / n | ||
pi_error = 100d0 * (abs(pi_est - pi)/pi) | ||
|
||
WRITE(*,*) 'The pi estimate is: ', pi_est |
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.
Could we do some formatting here to truncate the number of significant digits output for this result?
Thanks again! Sorry it took me so long to get to this PR. I'll tackle the other Fortran PR's after this one. It's ultimately good, but maybe add some formatting to the output and some spacing to the functions. The spacing is your call. I am not sure what "standard fortran" looks like nowadays. |
END FUNCTION in_circle | ||
|
||
PROGRAM monte_carlo | ||
IMPLICIT NONE |
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.
I think spacing here for the program
block might be nice too. Again, this is inconsistent. If you are adamantly against putting spaces here, I totally understand. It just helps a bit with readability. I am trying to think of what people will be submitting in future fortran methods.
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.
Will be done. I did this in the following contributions as well.
|
||
END FUNCTION in_circle | ||
|
||
PROGRAM monte_carlo |
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.
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.
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.
Looks good to me. Thanks for the submission!
Hi all!
The code was compiled with
gfortran
(gcc-8.2.0) with the following command:gfortran -Og -Wall -o montecarlo monte_carlo_pi.f90
.// The warning or error is concerning the implicit call of the
in_circle
function.// Thanks in advance for any suggestion or idea solving this issue.
Compiling the code on an other machine with
gfortran -Og -Wall -Werror -o montecarlo monte_carlo.f90
does not raise the error.Best regards,
Patrik