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

Fortran issues - Not getting expected output

  1. Apr 11, 2014 #1
    Hey everyone,

    I've been working on this project for awhile (some of you may recall having seen a very early version), and I am so close to being finished, but I have a problem somewhere, and I can't figure out why.

    So what is happening here is that this code is supposed to calculate the average displacement of an object from some point chosen randomly. In the first steps of writing this code, I started with simply the choice of random points, and then a third random point was chosen and compared to the Gaussian calculated by the original 2 points. (First "do" loop.) When I did this part, everything worked fine. My points all had a higher probability of being created in the center of the circle I have created in the program.

    The problem is, that now when I run this with all my extra steps, I am getting a large number of lengths that are short, instead of long... suggesting most of my creation points are no longer near the center, which is not the case.

    I have been looking at this for way too long, and I cannot find the problem. This is the first code I have written completely on my own, in any language, so I may be missing subtleties of the language or something related to syntax. I realize there are some possibly more efficient ways of doing things than the way I am, but could someone please give me some direction as to what is going wrong, or where my problem is? I don't necessarily want to be told what to change as to where to look right now.


    Code (Text):

        program avglength

        implicit none

        integer N, L, M, i, j, k
        double precision gauss_dist, a, a2, z_min, e, total
        double precision z_max, time, tmprt, zlength, y_length
        double precision avg_length, zcount, rad, x_length
        double precision x, y, z, x_path, y_path, beta, pi
        double precision delta_t, zlength_sum  
        open(unit=2, file="x-y-acc.dat", status="unknown")
        open(unit=3, file="lengths.dat", status="unknown")
        open(unit=4, file="x-y-z.dat", status="unknown")
        open(unit=6, file="x-and-y_path_beta.dat", status="unknown")   
        open(unit=7, file="avg_length.dat", status="unknown")
        open(unit=8, file="x_and_y_path.dat", status="unknown")
        open(unit=9, file="x_length-y_length.dat", status="unknown")
        open(unit=11, file="x-y-rad.dat", status="unknown")
        open(unit=12, file="count.dat", status="unknown")

    !!!!!!  value assignment for counters & constants  !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        N = 10000
        M = 100
        L = 135
        a = 7
        a2 = a*a
        e = 2.718
        pi = 3.1415
        time = 0.0d0
            delta_t = 0.2d0
        zcount = 0.0d0

    !!!!!!                                             !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!      creation and motion evolution          !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!                                             !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    !!!!!!  selection of seed points (seed loop)   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!    

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

           y = (rand() - 0.5) * 26.0

           z = rand()

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

           write(4,*) x, y, z, gauss_dist

           if (z .le. gauss_dist) then
           write(2,*) x, y
           end if

           if (z .le. gauss_dist) then
    !!!!!!   assignment of M angles to each jet seed (angle loop)   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!  
           do j = 1, M
              time = 0.0d0

              beta = 2.0*pi*rand()    !angle of expulsion
              x_path = x

              y_path = y

              write(6,*) x_path, y_path, beta

    !!!!!!   time evolution of movement (time loop)   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!        

              do k = 1, L

             rad = dsqrt(x_path*x_path + y_path*y_path)

             write(11,*) x_path, y_path, rad

             if ( rad .le. 10.0d0  ) then

                tmprt = 1.0d0

             else if (rad .gt. 10.0d0 ) then

                tmprt = 0.0d0

             end if

             if ( tmprt .gt. 0.16) then

                time = time + delta_t

                    x_path = x_path + delta_t*cos(beta)

                    y_path = y_path + delta_t*sin(beta)

                write(8,*) x_path, y_path, time

             else if (tmprt .le. 0.16) then

    ! preceding two lines should cause computer to skip out of this loop to calculate jet length once temp drops below 0.16GeV

             end if

                  end do

              x_length = x_path - x

              y_length = y_path - y

              write(9,*) x_length, y_length

              zlength = dsqrt (x_length**2 + y_length**2)

              zcount = zcount + 1.00

              write(3,*) zlength, zcount

           end do

           end if

        end do     


    !!!!!!                                                                  !!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!  summation of lengths followed by averaging (counting loop)  !!!!!!!!!!!!!!!!!!!!!!!!!!
    !!!!!!                                                                  !!!!!!!!!!!!!!!!!!!!!!!!!!  

    !!!!!!  initilaization of values in counting loop   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

        zlength_sum = 0.0d0
        write(12,*) zcount

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

        N = 1000

        do i = 1, N

            read(3,*) zlength

           zlength_sum = zlength_sum + zlength
        end do

        avg_length = zlength_sum/zcount

        write(7,*) avg_length  


        end program
  2. jcsd
  3. Apr 11, 2014 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    To be consistent, when you initialize 'a', you should probably use 'a = 7.0d0' instead of 'a = 7'. Similarly, you are using truncated values for the constants 'e' and 'pi', which are declared DOUBLE PRECISION. You can initialize as follows:

    Code (Text):

    e = EXP (1.0d0)
    pi = ATAN2(0.0d0, -1.0d0)
    Alternatively, you can look up extended precision values of these constants in most scientific handbooks.
  4. Apr 11, 2014 #3
    I don't think that is going to solve my problem. The places that pi and e come into play have nothing to do with why I am not getting the appropriate distribution of lengths when I am starting with the appropriate distribution of initial points. I appreciate the comment, but this project is going into a larger project, and I am simply doing things the way the other part was started.
  5. Apr 11, 2014 #4


    Staff: Mentor

    Since you're getting values that are too small, these could well be due to the imprecise values you're using for e and ##\pi##. Your value for ##\pi## is incorrect in the 4th decimal place, and its imprecision will propagate into your calculations for beta, which is used in the calculation for the x and y values. Also, your rouigh value for e will affect the calculations for gauss_dist.
  6. Apr 12, 2014 #5


    User Avatar
    Science Advisor
    Homework Helper

    This may depend on what version of Fortran you are using, but are your calls to rand() correct?

    For example http://gcc.gnu.org/onlinedocs/gfortran/RAND.html implies it should have a parameter. You probably want to use 0. Since you left off the parameter, it may be picking up a non-zero value of the parameter and repeatedly resetting the sequence of random numbers. If it picks up the SAME non-zero value as before, it will repeat the same "random" sequence every time it is reset.

    Look at the random number you are writing on file 4 and check they really are random - e.g. sort them and plot the cumulative distribution function.

    The advice about accurate values for e and pi is good, but since your values are correct to 4 decimal places, you would normally expect the answers to only be "wrong" by a similar amount, and that might not be obvious. From your posts I'm guessing the errors ARE obvious, so the cause is likely to be something else.

    Note, you probably don't need the value of e. Instead of
    gauss_dist = e**(-(x*x + y*y)/a2)
    you can write use the built-in function (as you did for SQRT) and write
    gauss_dist = exp(-(x*x + y*y)/a2)

    You don't need to explicitly call dexp or dsqrt. If the argument of exp or sqrt is double precision, the compiler is smart enough to know you want the answer to be double precision. Except when you really need to, use the "generic" function names like exp, sqrt, sin, cos, max, min, etc rather than the specific ones like dexp etc.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Similar Discussions: Fortran issues - Not getting expected output