Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Jul 31, 2013 #1
    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
     
  2. jcsd
  3. Jul 31, 2013 #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.
     
  4. Jul 31, 2013 #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: Aug 1, 2013
  5. Aug 1, 2013 #4

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  6. Aug 6, 2013 #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.
     
  7. Aug 6, 2013 #6

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Code (Text):
    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 (Text):
     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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Can't get Fortran code to give me desired output file
  1. Fortran output file (Replies: 1)

Loading...