Can't get Fortran code to give me desired output file

Click For Summary

Discussion Overview

The discussion revolves around a Fortran code issue where a participant is unable to output data to a file as intended. The code is part of a Monte-Carlo simulation that calculates values based on a Gaussian distribution. Participants are exploring potential reasons for the failure to write to the output file and suggesting modifications to the code.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant describes their Fortran code and the specific issue of not being able to write x- and y-values to a file after performing calculations involving a Gaussian distribution.
  • Another participant questions the use of the "status" argument in the "open" procedure, suggesting that it may affect the file's ability to be written to.
  • Suggestions are made to add "ACCESS='SEQUENTIAL'" and "FORM='FORMATTED'" to the OPEN statement for better file handling.
  • A participant notes that the unit number used for the file should not be reserved for default use, implying that it could affect file operations.
  • One participant reports success after removing lower bounds from an if-statement, speculating that the condition may have been problematic due to the nature of the random number generation.
  • Another participant emphasizes the importance of code formatting for readability and understanding, particularly in relation to logical statements.
  • It is pointed out that if the first condition in a logical statement fails, the entire statement will evaluate to false, which could explain why the write operation was not executed.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the issue, with some suggesting changes to the code while others provide explanations for the behavior observed. No consensus is reached on a definitive solution, and multiple hypotheses are presented.

Contextual Notes

There are unresolved assumptions regarding the behavior of the random number generator and the implications of the logical conditions in the code. The discussion also highlights potential limitations in the understanding of file handling in Fortran.

karenmarie3
Messages
9
Reaction score
0
Hey everyone,

I have written a Fortran code for a project I am working on, but am having an issue: I can't get the code to output my data to the file I have opened. Here is what I am trying to do.

I am setting up a Monte-Carlo code that selects a random x- and y-value. These values are then used to calculate the value of a Gaussian distribution (gauu_dist) at that point. Then another random value (z) is found, and compared as 0 is greater than or equal to z, and z is less than equal to gauss_dist at that point. If so, then the x- and y- values are to be written to my file jet_dist.dat (I do not care what z is after this, nor do I care what the gauss_dist value is anymore. ) For some reason I cannot get the resulting data to be put into my .dat file. If I ask the code to wirte my x- and y-values as soon as they are found, and comment out the lines of my if/then statement and my gauss_dist line, I can get the code to write my x- and y-values. I really can't figure out what I am missing!

Hopefully this is clear enough for someone to assist me. The following is my code, if you need further clarification, please let me know. Oh, and I am compiling using gfortran on an Ubuntu (most recent distro) platform.




program jetdist

implicit none

integer N, i
real x, y, z, gauss_dist, a, a2, z_min, e
real x_acc, y_acc, z_max

real, dimension(1100, 1100, 1100, 1100) :: jet_dist

open(unit=3, file="jet_dist.dat", status="unknown")

N = 1000
a = 3
a2 = a*a
e = 2.718
! need to insert print/write command so that N/-values can be changed for different runs of code
a2 = a*a

x_acc = 0

y_acc = 0

z_min = 0

z_max = 1.25

do i = 1, N

x = (rand()-0.5) * 26

y = (rand()-0.5) * 26

z = (rand())

gauss_dist = e**(-(x*x + y*y)/a2)

if (z_min .ge. z .and. z .le. gauss_dist) then

write(3,*) x, y

end if

end do

close(3)

return

stop

end
 
Technology news on Phys.org
Note: I have insterted write(*,*) statements after the x- & y- value selection, the z selection, and the gauss_dist calculation. All 4 values are being printed to my screen, but my data file is not being written to.
 
Does the "open" procedure's "status" argument tell the computer whether to open the file for reading, for writing or for both? For now, the value of status is "unknown." So maybe the computer doesn't know that you want it to write to the file.

Are you sure you need x_acc and y_acc? After you assign zero to each, the values of those variables stay the same. Maybe you can delete those lines or change them into comments?
 
Last edited:
Since you are writing formatted data to the output file, I would also add ACCESS='SEQUENTIAL' and FORM='FORMATTED' to your OPEN statement.

Note: STATUS='UNKNOWN' will create a new file if none exists, or open an old file if the named file does exist.

Make sure that unit 3 is not reserved for some default use. Outside of certain reserved units, the unit number can be any valid positive integer.
 
Thanks for the suggestions. I ended up deleting the lower bounds in my 'if' statement. I'm not entirely certain why it worked, but it seems like the compiler may have gotten angry with me for the statement "z_min .ge. z" when rand() automatically gives a number greater than 0, always (and, of course, less than 1). That's all I could come up with.
 
Code:
program jetdist

      implicit none

      integer N, i
      real x, y, z, gauss_dist, a, a2, z_min, e
      real x_acc, y_acc, z_max

      real, dimension(1100, 1100, 1100, 1100) :: jet_dist

      open(unit=3, file="jet_dist.dat", status="unknown")

      N = 1000
      a = 3
      a2 = a*a
      e = 2.718
! need to insert print/write command so that N/-values can be changed for different runs of code
      a2 = a*a

      x_acc = 0
      y_acc = 0

      z_min = 0
      z_max = 1.25

      do i = 1, N
         x = (rand()-0.5) * 26

         y = (rand()-0.5) * 26

         z = (rand())

         gauss_dist = e**(-(x*x + y*y)/a2)

         if (z_min .ge. z .and. z .le. gauss_dist) then
           write(3,*) x, y
         end if
      end do

      close(3)

      return

      stop

      end

It helps for reading and understanding you code is you use indenting and grouping of your statements, especially for loops and IF statements.

For PF use, always include your source files within
Code:
 tags to preserve any formatting of the text.

In the last IF block, the variable 'z_min' has already been set to zero and you are comparing its value to 'z'.  If z is always greater than 0, then the first condition on this IF statement will fail, and because it fails, then the following WRITE statement will not get executed, which was why the program wasn't writing any output files.

In evaluating logical statements like A .AND. B, if A is FALSE, A .AND. B will always be FALSE, regardless of the logical value of B.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
4
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K