Fortran issues - Not getting expected output

In summary: M read(3,*) zlength zlength_sum = zlength_sum + zlength avg_length = zlength_sum / zcount end do write(7,*) avg_length In summary, this conversation is about a project that calculates the average displacement of an object from a randomly chosen point. The code was working fine with just the random points and a comparison to a Gaussian calculated by the original two points, but after adding extra steps, the lengths are now shorter instead of longer. The person is seeking guidance on where the problem may be, as this is their first time writing their own code. The conversation also includes code and
  • #1
karenmarie3
10
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
  • #2
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.
 
  • #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.
 
  • #4
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.
 
  • #5
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.
 

What is Fortran and why is it used?

Fortran is a high-level programming language commonly used for scientific and engineering applications. It is known for its reliable numerical computing capabilities and is often used for creating simulations and mathematical models.

Why am I not getting the expected output in Fortran?

There could be several reasons for this issue. It could be due to a coding error, incorrect use of variables or functions, or an error in the input data. Debugging your code and checking for any mistakes can help identify the cause of the unexpected output.

How can I improve the performance of my Fortran program?

There are several ways to optimize the performance of a Fortran program. This includes using efficient algorithms and data structures, minimizing the use of costly operations like I/O, and taking advantage of parallel processing techniques. Profiling tools can also help identify bottlenecks in your code.

What are common errors in Fortran programming?

Some common errors in Fortran programming include syntax errors, logical errors, and runtime errors. Syntax errors occur when the code does not follow the proper grammar and structure of the language. Logical errors result in incorrect output due to flawed logic in the code. Runtime errors occur when the code encounters an unexpected problem during execution, such as division by zero or out of bounds array access.

Is Fortran still relevant in modern programming?

Yes, Fortran continues to be widely used in scientific and engineering fields, especially in high-performance computing. It has evolved over the years and remains a popular choice for developing numerical applications due to its efficient and reliable performance. Additionally, newer versions of Fortran have added features that make it more compatible with modern programming practices.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
498
  • Programming and Computer Science
Replies
22
Views
4K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top