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

AI Thread Summary
The Fortran code is intended to output x- and y-values to a file based on a Monte Carlo simulation using a Gaussian distribution. The issue arises because the condition in the if-statement incorrectly compares a variable that is always zero (z_min) with a random value (z), causing the write operation to never execute. Suggestions include ensuring the file is opened correctly with appropriate access and format options, and simplifying the if-statement to avoid logical errors. After modifying the condition, the code successfully writes to the output file. Proper indentation and formatting of the code are recommended for better readability and debugging.
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.
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
4
Views
2K
Replies
59
Views
11K
Replies
8
Views
2K
Replies
3
Views
3K
Replies
8
Views
2K
Replies
3
Views
2K
Replies
8
Views
4K
Replies
6
Views
3K
Back
Top