Fortran Fortran issues - Not getting expected output

AI Thread Summary
The discussion revolves around a Fortran code issue where the expected output of average displacement calculations is not being achieved. The user reports that, despite initial success in generating random points centered in a circle, subsequent modifications have led to unexpected short lengths, indicating a potential problem with point generation. Suggestions include ensuring accurate initialization of constants like pi and e, as well as verifying the correct usage of the random number generator function, which may be affecting the randomness of generated points. Additionally, it is advised to simplify the code by using built-in functions for mathematical calculations. The user seeks guidance on identifying the source of the problem without direct code alterations.
karenmarie3
Messages
9
Reaction score
0
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.

Thanks!

Code:
	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

		 	exit
		 
! 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  	

	   close(3)
	   close(4)
	   close(6)
	   close(8)
	   close(11)	


	
!
!                                                                  !
!  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   
	
	stop

	close(7)
	close(12)

	end program
 
Technology news on Phys.org
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:
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.
 
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.
 
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.
 
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.
 
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...
What percentage of programmers have learned to touch type? Have you? Do you think it's important, not just for programming, but for more-than-casual computer users generally? ChatGPT didn't have much on it ("Research indicates that less than 20% of people can touch type fluently, with many relying on the hunt-and-peck method for typing ."). 'Hunt-and-peck method' made me smile. It added, "For programmers, touch typing is a valuable skill that can enhance speed, accuracy, and focus. While...
I had a Microsoft Technical interview this past Friday, the question I was asked was this : How do you find the middle value for a dataset that is too big to fit in RAM? I was not able to figure this out during the interview, but I have been look in this all weekend and I read something online that said it can be done at O(N) using something called the counting sort histogram algorithm ( I did not learn that in my advanced data structures and algorithms class). I have watched some youtube...

Similar threads

Replies
5
Views
3K
Replies
8
Views
2K
Replies
22
Views
5K
Replies
3
Views
3K
Replies
5
Views
5K
Replies
2
Views
2K
Replies
12
Views
3K
Replies
4
Views
2K
Replies
6
Views
3K
Back
Top