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

In summary: 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
  • #1
karenmarie3
10
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
  • #2
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.
 
  • #3
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:
  • #4
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.
 
  • #5
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.
 
  • #6
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.
 

1. Why is my Fortran code not producing the expected output file?

There could be several reasons for this issue. Some common reasons include errors in the code, incorrect input parameters, or issues with the file writing process. It is important to carefully check your code for any mistakes and ensure that the input parameters are correct. You may also want to use debugging tools to identify any errors in your code.

2. How can I troubleshoot my Fortran code to fix the output file issue?

One way to troubleshoot your Fortran code is to use a systematic approach. Start by checking for any errors in the code and correcting them. Then, carefully examine the file writing process to ensure that the output file is being created and written correctly. You may also want to try running the code with different input parameters to see if that affects the output file.

3. What can I do if my Fortran code is still not producing the desired output file?

If your Fortran code is still not giving you the desired output file, you may need to seek help from more experienced Fortran programmers or consult online forums and communities for troubleshooting advice. It may also be helpful to review the documentation for the Fortran libraries or tools you are using to see if there are any specific guidelines or best practices for creating output files.

4. Can the operating system or compiler affect the output file of my Fortran code?

Yes, the operating system and compiler used can have an impact on the output file of your Fortran code. It is important to ensure that the code is compatible with the chosen operating system and compiler. Additionally, some operating systems may have limitations on file sizes or names, so it is important to check for any restrictions that may affect the creation of your output file.

5. How can I improve the performance of my Fortran code when creating an output file?

To improve the performance of your Fortran code when creating an output file, you can consider optimizing your code by using efficient algorithms and data structures. Additionally, you may want to use parallel programming techniques to leverage the processing power of multiple cores or processors. It can also be beneficial to regularly monitor and optimize your code's memory usage to improve overall performance.

Similar threads

  • Programming and Computer Science
Replies
4
Views
627
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
6
Views
8K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
1
Views
758
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top