Arithmetic overflow in Fortran 95

In summary, the conversation discusses a problem with a code written in Fortran 95 to solve an advection-dispersion equation. The code works with smaller spatial intervals but results in an arithmetic overflow error with larger spatial intervals. The conversation also mentions using an upwind scheme and setting boundary conditions. The cause of the error is determined to be the k term and the solution is to print out the values of various variables to identify the issue. It is also suggested to refine the grid to avoid instabilities.
  • #1
edge333
16
0

Homework Statement



Wrote a code using Fortran 95 to solve for an advection-dispersion equation but at the spatial steps specified at dx = 20 m over a total length, L of 20000 m, I keep getting an arithmetic overflow error.

I have run this same program at smaller spatial intervals (dx = 200 m and dx = 2000 m) and they worked perfectly. There must be something about the total number of iterations that Fortran is trying to perform that makes it incapable of computing it (I'm not actually sure).

I'm a novice at using Fortran but curious if this has anything to do with the size of the concentration values that are being computed being too small (i.e. smaller than 10^-38). Does this have anything to do with double precision?

Homework Equations



The scheme used to solve the numerical solution to the ADE is an finite differenece upwind scheme.

The first equation in the code under the two "do" statements defines this upwind scheme within the internal bounds of the system.

The second equation in the code outside of the two "do" statements defines the boundary condition at X = L.

The Attempt at a Solution



module global ! Module is used to define arrays and use in main program blocks

implicit none
real len, dx, k, Dl
real, allocatable, dimension (:, :) :: conc ! I used allocatable arrays so that the size of
! the array can be changed, not necessary for this
! simple of a problem
real, allocatable, dimension (:) :: tim
integer ndx, i, n

end module global

program main
use global

real dt , tottime, u

This is where you write out what you're going to be doing. You can just link to where a
! source file is located, but make sure you do it.

dt = 30. ! unit is seconds
len = 20020. ! unit is meters
tottime = 44000. ! unit is seconds (43200 seconds in half a day)
ndx = 1001 ! number of grid points - we want 10 cells so we start at 1.
u = 0.25 ! unit is m/s
Dl = 50. ! unit is m^2/sec
k = 0.1 / 86400. ! unit is sec^-1
nts = (tottime) / dt ! number of time steps
dx = len / real(ndx) ! grid spacing

! Allocate the arrays, conc(entration), and tim(e).
allocate (conc ( ndx , nts ), tim ( nts ))

conc = 0.0 ! set initial concentration elements to 0 mg/L
tim = 0.0 ! initial time is zero
conc (1, :) = 100. ! set BC on LHS to 100 mg/l for all time

! Run the internal finite difference equation and populate the array conc ()

do n = 1, nts - 1
tim (n) = dt * (n - 1)
do i = 2, ndx - 1
conc (i, n + 1) = conc (i, n) - u * (dt / dx) * (conc (i, n) - conc (i - 1, n)) &

+ Dl * (dt / (dx**2)) * (conc (i + 1, n) - 2 * conc (i, n) + conc (i - 1, n)) - k * dt * conc (i, n)
end do
conc (ndx, n + 1) = conc (ndx, n) - u * (dt / dx) * (conc (ndx, n) - conc (ndx - 1, n)) &
+ Dl * (dt / dx**2) * (2 * conc (ndx - 1, n) - 2 * conc (ndx, n)) - k * dt * conc (ndx, n)
end do

! Set BC on RHS where c (ndx + 1, nts) = c (ndx - 1, nts)
! Write the array to a file

open (10, file = 'C:\g95\fortran\CEE 573\Problem Set 5\upwind20.csv', status = 'unknown')

! Write the column headings
write (10, 100) (tim (n) / 86400., n = 1, nts, 10)
100 format ('X, ',2x, 10000 ('t=', f8.4, ','))

! Write the columns
do i = 1, ndx
x = dx * (i - 1)
write (10, 110) x, (conc (i, n), n = 1, nts, 10)
110 format (f10.1, ',', 10000 (f8.4, ','))
end do

end program main
 
Physics news on Phys.org
  • #2
does the computer say what line is getting the overflow error?
 
  • #3
Yeah, it said the error was on line 47 which is the second line of this equation (internal finite difference equation):

conc (i, n + 1) = conc (i, n) - u * (dt / dx) * (conc (i, n) - conc (i - 1, n)) &
+ Dl * (dt / (dx**2)) * (conc (i + 1, n) - 2 * conc (i, n) + conc (i - 1, n)) - k * dt * conc (i, n)
 
  • #4
To debug this, break up the expression on the right side, so that you do the computation in at least three assignment statements. That should help you determine which term is causing problems.
 
  • #5
Okay, I did that. And so I put the k term on line 48 and the error occurs on line 48. So it must be something with the k term. Why would it not do this if I set the spatial steps higher at dx = 200 m and dx = 2000 m instead of dx = 20 m?
 
  • #6
so now you print out the value of k, dt,i,n and conc(i,n) separately
and see which var is causing the issue.
 
Last edited:
  • #7
If you have downstream boundary conditions instabilites may exist unless the Peclet number is kept small by making the spatial discritization small. The grid spacing must be sufficiently small so the Laplacian dominates the equation. This can be accomplished by grid refinement. For additional information google 'grid peclet number'.
 

What is arithmetic overflow in Fortran 95?

Arithmetic overflow in Fortran 95 occurs when the result of a mathematical operation exceeds the maximum value that can be stored in the data type being used. This can lead to inaccurate or unexpected results in the program.

What are the common causes of arithmetic overflow in Fortran 95?

The most common cause of arithmetic overflow in Fortran 95 is performing mathematical operations on data types that do not have enough storage capacity to hold the result. Another cause is using constants or literals that are too large for the data type being used.

How can I prevent arithmetic overflow in my Fortran 95 program?

To prevent arithmetic overflow, it is important to carefully select the appropriate data types for the variables and constants used in mathematical operations. Using larger data types or converting the data to a larger type before performing the operation can also help prevent overflow.

What happens when an arithmetic overflow occurs in a Fortran 95 program?

When an arithmetic overflow occurs, the result of the operation may be incorrect or unexpected. In some cases, the program may crash or produce an error message. It is important to handle arithmetic overflow errors in your program to avoid these issues.

Are there any built-in functions in Fortran 95 to handle arithmetic overflow?

Yes, Fortran 95 has several built-in functions that can be used to handle arithmetic overflow, such as INT, REAL, and MAXVAL. These functions can be used to convert data types or check for overflow before performing mathematical operations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
Back
Top