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

Conversation

depate
Copy link
Contributor

@depate depate commented Sep 4, 2018

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

@june128 june128 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Sep 4, 2018
@leios
Copy link
Member

leios commented Sep 4, 2018

Thanks a bunch and welcome to the AAA community!

A few notes:

  1. Please remove the binary from the PR
  2. I don't think the .gitignore is necessary here either
  3. Please put your name in the CONTRIBUTORS.md file in the main directory
  4. This is the first time we have fortran90 in the AAA, so please put the language in the book.json file (following the example of the other languages). Note here that the "lang" value will be the filename extension (here .f90) and the "name" will be the language name (Here Fortran90).
  5. Please also update the monte_carlo_integration.md file with the fortran entry, using the tag you created in the book.json file and following the example from the other languages. Basically, you just need to grab the line numbers for the in-text snippets and then put them in the appropriate spot in the method. It will probably look like this:
{% sample lang="f90" %}
[import:3-10, lang:"fortran"](code/f90/monte_carlo.f90)

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!

Copy link
Member

@leios leios left a 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
Copy link
Member

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

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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
Copy link
Member

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
Copy link
Member

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?

@leios
Copy link
Member

leios commented Sep 21, 2018

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
Copy link
Member

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.

Copy link
Contributor Author

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
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.

Copy link
Member

@leios leios left a 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!

@leios leios merged commit 7994074 into algorithm-archivists:master Sep 23, 2018
@depate depate deleted the monte_carlo_fortran branch October 7, 2019 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants