



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




The following is a compilation of common, trivial but āinvisibleā errors that may have catastrophic results. The erroneous action is marked with red.
Common Errors: Precision, Expressions
a = 4. b = 1/2 + a**
Here b will be equal to 16.0 after execution. The correct expression is: b = 1.0/2.0 + a**
Suppose we are using Newtonās method to solve numerically an equation. If we write:
DO WHILE (Err1 /= 0.D0)
we will never converge because the error cannot be exactly equal to zero. The right way to do this is:
DO WHILE (dabs(Err1) >= Tol)
were Tol is a predefined small number indicating Tolerance, e.g. Tol = 1.D-8.
We know the position of a fly in 3D space x, y, z at time t and we have an algorithm that evaluates its position at the next timestep t + dt. The algorithm says:
x (^) next = 1.08xcurrent + 0.02ycurrent ynext = y (^) current + 0.14xcurrent z (^) next = 1.01zcurrent + 0.01* xcurrent*2 + 0.04y (^) current
This will be manifested as follows:
DO xnext = 1.08xcurrent + 0.02ycurrent ynext = y (^) current + 0.14xcurrent z (^) next = 1.01zcurrent + 0.01* xcurrent*2 + 0.04y (^) current xcurrent = x (^) next ycurrent = y (^) next z (^) current = z (^) next ENDDO
The following structure is incorrect and will yield erroneous results because x and y are updated prematurely:
DO xcurrent = 1.08xcurrent + 0.02ycurrent ycurrent = y (^) current + 0.14xcurrent z (^) current = 1.01z (^) current + 0.01* xcurrent *2 + 0.04ycurrent ENDDO
Common Errors: Loops, Branches
DO i = 1, x(j) = y(j)** ENDDO
We should have used I instead of j inside the loop.
DO i = 1, DO i = 1, x(i,i) = y(i)** ENDDO ENDDO
Suppose we want to calculate the sum
10 2 i 1
. This is automatically a DO loop:
DO i = 1, Sum = Sum + (1/i**2) ENNDO
Now if this is the first time variable Sum is used we may not have problem, because Fortran give the value Sum = 0.D0 upon initialization of the variable. But if Sum has previously been used and has a value, say, Sum = -198.9381D0, then the final value calculated in our loop will be erroreous. To be sure that you get correct results always initialize your variables:
Sum = 0.D
SUBROUTINE tempcalc(x,y,z) IMPLICIT NONE REAL temperatures, distance, x, y, z
! Commands follow ā¦
temperatures = ā¦
! Commands follow ā¦
RETURN END
Even if we have the command that calculates the temperatures, the results are not returned to the main program unless the subroutine definition is as follows:
SUBROUTINE tempcalc(temperatures,x,y,z)
Always remember that the variables used in a subroutine or function are ālocalā, dummy variables (unless COMMON blocks are used of course).
CALL updateaux(indx(1),indx,prqu,n,n0)
ā¦for subroutine:
SUBROUTINE updateaux(i,indx,prqu,n,n0) ! Definitionsā¦
indx(1) = 51
! Commands follow ā¦
Now suppose that at some point in the execution of the subroutine, the value indx(1) changes, as in our example indx(1)=51. This change will inevitably affect variable i of the subroutine as well, because of the way the call statement was constructed. That is immediately after execution of the assignement command: indx(1)=51, variable i will have the value 51 even though we did not have any command such as i = 51. The way to avoid this is to rephrase the CALL command as follows:
k=indx(1) call updateaux(k,indx,prqu,n,n0)
Common Errors: Arrays
PROGRAM main IMPLICIT NONE REAL temperatures(10), height
height = 10. CALL tempcalc(temperatures, height)
! Commands follow ā¦
END
SUBROUTINE tempcalc(temperatures, height) IMPLICIT NONE REAL temperatures(20), height
! Commands calculating temperatures follow ⦠! height is not assigned any new value (we have no āheight = ā¦ā commands)
RETURN END
This may have very strange effects in your program, depending on the compiler, for example variable height may have different values before and after execution of tempcalc, even though the subroutine did not assign any new value to height.