Docsity

Common Debugging Errors: Precision, Expressions, Loops, Branches, Subroutines, Functions, Exams of Chemistry

Toggle sidebar
bg1
1
Debugging Guide
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
• Mixed expressions that produce non desired results are used. Example (both a, b are
real):
a = 4.0
b = 1/2 + a**2
Here b will be equal to 16.0 after execution. The correct expression is:
b = 1.0/2.0 + a**2
• Tolerance criteria cannot be met because of finite precision. Example:
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.
• A series of expressions containing many variables is evaluated. The variables are
incorrectly updated. Example:
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:
xnext = 1.08*xcurrent + 0.02*ycurrent
ynext = ycurrent + 0.14*xcurrent
znext = 1.01*zcurrent + 0.01* xcurrent**2 + 0.04*ycurrent
This will be manifested as follows:
DO
xnext = 1.08*xcurrent + 0.02*ycurrent
ynext = ycurrent + 0.14*xcurrent
znext = 1.01*zcurrent + 0.01* xcurrent**2 + 0.04*ycurrent
xcurrent = xnext
ycurrent = ynext
zcurrent = znext
ENDDO
pf3
pf4
pf5

Partial preview of the text

Download Common Debugging Errors: Precision, Expressions, Loops, Branches, Subroutines, Functions and more Exams Chemistry in PDF only on Docsity!

Debugging Guide

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

  • Mixed expressions that produce non desired results are used. Example (both a, b are real):

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**

  • Tolerance criteria cannot be met because of finite precision. Example:

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.

  • A series of expressions containing many variables is evaluated. The variables are incorrectly updated. Example:

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

  • Signs are incorrect. In many cases the whole code is correct except from a single sign and after many hours of debugging we realize how simple the problem was.

Common Errors: Loops, Branches

  • A DO loop is initialized with a counter, but inside the loop another counter is used. Example:

DO i = 1, x(j) = y(j)** ENDDO

We should have used I instead of j inside the loop.

  • The same counter is used in nested loops. Nested loops can never have the same counters, e.g. the following structure is invalid:

DO i = 1, DO i = 1, x(i,i) = y(i)** ENDDO ENDDO

  • A quantity is calculated through a recursive expression, but the variable used is not initialized first. Example:

Suppose we want to calculate the sum

10 2 i 1

āˆ‘= i

. 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

  • A subroutine updates a variable but the result is not returned to the calling program. Be sure to include all the desired input and output variables in the definition of a subroutine. Example: we want to calculate temperatures and return them in the main program but we forget to include variable temperatures in the definition:

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).

  • Arguments in a subroutine or function create memory site conflicts upon execution of the subroutine. Consider the following call statement…

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

  • An array is defined with fixed subscript ranges and in the program there is reference to an element ā€œoutsideā€ of the array bounds, e.g. we define x(20,20) and we have a command x(21,21) = 3.2. This produces ā€œArray bounds exceededā€ errors upon execution; however some compilers may not detect the error.
  • Fixed size array is defined with different subscript ranges in a program and a subroutine. Example:

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.