APL to MAPLE code of RKTest70

$$\def\quad{\;\;\;\;}$$

A MAPLE implementation of the APL code "Test" from 01/01/1970

Our first B-series program, contains a copy of the original APL program written by John Butcher and Jim Verner on Jan. 1, 1970 to determine the order of a Runge--Kutta method. This present code, RKTest70.maple was written to accurately implement the code RKTest70.apl in a linecode MAPLE form, and to allow some options for the format of the output. Execution in the attached form yields approximately the values of the local truncation errors of each of 200 order conditions that must be satisfied by a Runge--Kutta method of order eight. The method tested is a Cooper-Verner method derived in 1968. Because all these error approximations are sufficiently close to zero (and by options available can be shown to be exactly zero), this method is of order 8.

restart:
with(LinearAlgebra):
#            MAIN PROCEDURE
Test := proc(N)
local Ti, CTi, Lt, Rt, ORt, i, j, k, LB, UB, SumNu, ORTBN, T2err:
global ORTB, S, A, Rho, IRt, G, MU, NU:
if N=1 then
   print(`------------------------------------------------------------`):
   printf(MethSt):
fi:
if not(AccumulatedErrorCoefficients) then
   if N=1 then
      print(`------------------------------------------------------------`):
      printf(`VALUES OF LOCAL TRUNCATION ERROR EXPRESSIONS AND FUNCTIONS         \n`):
   fi:
   lprint(` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR    `):
else
   if N=1 then
      print(`------------------------------------------------------------`):
      print(`  TABLE OF 2-NORMS OF ERROR COEFFICIENTS`):
      print(`------------------------------------------------------------`):
      print(`2-Norms of  `,'chi(tau)'):
   fi:
   T2err:=0:
fi:
Ti := ORTB[N]:
CTi := Ti:
Lt := 0:
if N = 1 then
   Lt := 1:
   Rt := 0:
   if not(AccumulatedErrorCoefficients) then
      lprint(``, N, `  `, CTi, `   `, Lt, Rt, `   `, G[Ti], `   `, G[Ti]*NU[Ti,S+1] - 1):
   else
      T2err:=T2err+(evalf(G[Ti]*NU[Ti,S+1] - 1))^2:
   fi:
fi:
while Lt + 1 < ORTB[N] do
   Lt := Lt + 1:
   ORt := N - Rho[Lt]:
   Rt := max(ORTB[ORt], IRt[Lt]):
   UB := ORTB[ORt + 1] - 1:
   while Rt <= UB do
      IRt[Ti] := Rt:
      G[Ti] := N*G[Lt]*G[Rt]/Rho[Lt]:
      Rho[Ti] := N:
      for j from 1 to S + 1 do
         MU[Ti, j] := MU[Lt, j]*NU[Rt, j]:
      od:
      for j from 1 to S + 1 do
         SumNu := 0:
         for k from 1 to j - 1 do
            SumNu := SumNu + A[j, k]*MU[Ti, k]:
         od:
         NU[Ti, j] := simplify(expand(SumNu)):
      od:
      if not(AccumulatedErrorCoefficients) then
         lprint(``, N, `  `, CTi, `   `, Lt, Rt, `   `, G[Ti], `   `, G[Ti]*NU[Ti,S+1] - 1):
      else
         T2err:=T2err+(evalf(G[Ti]*NU[Ti,S+1] - 1))^2:
      fi:
      if N < p + 1 then
         Ti := Ti + 1:
      fi:
      Rt := Rt + 1:
      CTi := CTi + 1:
   od:   # END while Rt
od:      # END while Lt
if AccumulatedErrorCoefficients then
   print(`Order = `,N,` is `,evalf(sqrt(T2err),4)):
fi:
end proc:

Subprograms to read in coefficients of a method and to initiate the code and execute it sequentially for each order p = 1,2,... are required. In APL, indices identifying non-zero coeffients of a matrix A were entered followed by a triple for each value. For MAPLE, coefficients are stored sequentially as rows of triples.

For each method, the coefficients are entered from a file of (S+1)(S+1) rows with three entries per row. For each index i, i=1,..,S, the i-th set of S+1 rows represents S elements of row i of the coefficient matrix plus one dummy element. The final S+1 rows represent the S weights plus one dummy element. (For example, each zero value appears by the row: 0 0 1 .) (Observe that this procedure allows for either explicit or implicit Runge--Kutta methods to be tested.)

#          ENTERING THE COEFFICIENT MATRIX
Input := proc(S, Methodname)
local X, i,j,k:
global A, C, SQ:
#     S = number of stages in method
#     Use  S+1  to accommodate an extra line for the weights.
#     A[S+1,S+1] = matrix of coefficients and weights of the method
#     C[S] = vector of nodes of the method
#     SQ contains a surd  (sqrt(21.)) required  by Lobatto quadrature

#     Each row will yield a coefficient or weight in the form

#              (X[1] + SQ*X[2])/X[3]

C := Vector(S):                            # Define nodes of the RK method
X := readdata(Methodname, integer, 3):     # Define the array of parameters

#    Now read the triple in each row to give one parameter at a time.

   for k from 1 to S + 1 do
      for j from 1 to S do
         i := j + (S + 1)*(k - 1):
         A[k, j] := (X[i, 1] + SQ*X[i, 2])/X[i, 3]:
      od:   # END j
   od:      # END k
   for k from 1 to S do
      C[k]:=add(A[k,j],j=1..k-1):
   od:      # END k
   lprint(`Nodes of the method are `):
   print(Vector(C)):
lprint(`     Echo print to verify coefficient matrix `):
print(Matrix(A)):
end proc:

#               CHOOSING A TEST METHOD AND OUTPUT

      Digits:=17:
      S:=11:    p:=8:    SQ:=sqrt(21.):
      Methodname:="CVRK118method.txt":
      SpaceSt:="                                    ":
      MethSt:=cat(SpaceSt,"The method tested is a Cooper-Verner 11-8 method.\n\n"):

#               OPTIONS FOR ALTERNATIVE OUTPUT

   AccumulatedErrorCoefficients:=false:    # Display all error coefficients.
  # AccumulatedErrorCoefficients:=true:    # Remove  #  to display only 2-norms
                                           # of error coefficients of each order.
   pextra:=0:                              # Test to order  p.
  # pextra:=1:                             # Remove  #  to test to order  p+1

#                      EXECUTION

A := Matrix(S + 1, S + 1):                 # Matrix of coefficient with weights in last row.

Input(S, Methodname):

#                   INITIATING  TEST

ORTB := Vector(11, [1, 2, 3, 5, 9, 18, 38, 86, 201, 487, 1206]):
N :=8:
MU := Matrix(ORTB[N + 1], S + 1):
NU := Matrix(ORTB[N + 1], S + 1):
Rho := Vector(ORTB[N + 1]):
G := Vector(ORTB[N + 1]):
IRt := Vector(ORTB[N + 1]):
for i from 1 to ORTB[N + 1] do
    Rho[i] := 1:
    G[i] := 1:
    IRt[i] := 0:
od:
for j from 1 to S + 1 do
    MU[1, j] := 1:
od:
lprint(` `):
for j from 1 to S + 1 do
    SumNu := 0:
    for k from 1 to j - 1 do
        SumNu := A[j, k]*MU[1, k] + SumNu:
    od:
    NU[1, j] := SumNu:
od:

For a given S-stage method with coefficients and weights in matrix A, the procedure Test is executed sequentially for each order N, N = 1,2,..,p, and possibly p+1. The order of the method is determined as N equal to one less than the order of the first term that was not approximately zero.

#                 EXECUTING ORDERTEST

for i from 1 to p+pextra do
   Test(i):
od:

Simulation of computation from Jan. 1, 1970 to Test the order of a Lobatto (11-8) Runge--Kutta method:

To execute this code, copy and paste all lines in the final box to a data file with the name, CVRK118method.txt; copy and paste all other boxed lines in this file to a text file with the name RKTest70.maple; each text non-executable line in this file is preceded with #. Both files should be in a folder convenient for working. Now, enter a TERMINAL and adopt this folder as the default folder; execute this RKTest70.maple by the command

maple RKTest70.maple

When this folder contains a copy of CVRK118.txt, the OUTPUT should include the following ten lines, and the whole file shows the order is 8:

            ------------------------------------------------------------

                    The method tested was a Cooper-Verner 11-8 method

            ------------------------------------------------------------
                        SOLUTION

VALUES OF ERROR EXPRESSIONS AND FUNCTIONS
` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR`
``, 1, `  `, 1, `   `, 1, 0, `   `, 1, `   `, 0.
` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR`
``, 2, `  `, 2, `   `, 1, 1, `   `, 2, `   `, -.8e-16
` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR`
``, 3, `  `, 3, `   `, 1, 2, `   `, 6, `   `, -.16e-15
``, 3, `  `, 4, `   `, 2, 1, `   `, 3, `   `, -.22e-15
` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR`
``, 4, `  `, 5, `   `, 1, 3, `   `, 24, `   `, -.11e-15
``, 4, `  `, 6, `   `, 1, 4, `   `, 12, `   `, -.26e-15
``, 4, `  `, 7, `   `, 2, 2, `   `, 8, `   `, -.32e-15
``, 4, `  `, 8, `   `, 4, 1, `   `, 4, `   `, -.36e-15
` ORDER   EQ NO     SUB-INDICES    GAMMA     ERROR`
``, 5, `  `, 9, `   `, 1, 5, `   `, 120, `   `, -.9e-16
``, 5, `  `, 10, `   `, 1, 6, `   `, 60, `   `, -.4e-16

The values of ERRORs in this simulation differ from those that occurred in the original computation occur because of the different processes used for rounding approximate computations then compared to those that occur on the Apple MacBook Pro used now. These errors can be changed using the parameter Digits in MAPLE, and can be eliminated entirely by using exact computation in MAPLE with rationalize and other tools.

Results from this code can be contrasted with those obtained on testing this method using the modern version RKTest21.maple

For a low order example, replace the procedure definition and the OUTPUT choices above by the shorter procedure and lines

#                         LOW ORDER EXAMPLE
Input:=proc(S, Methodname)
 global A,C;
 A:=Matrix(5,5,[[0],[1/2],[0,1/2],[0,0,1],[1/6,1/3,1/3,1/6,0]]);
 C:=Vector([0,1/2,1/2,1]);
 end proc:

 S:=4:  p:=4:
 SpaceSt:="                                   ":
 MethSt:=cat(SpaceSt,"The standard 4 stage RK method of order 4 is tested.\n\n"):

There has been an attempt to make this MAPLE coding faithful to the APL codes, but the use of 'go to' statements and matrix computations in APL preclude this possibility. Otherwise, the MAPLE statements utilized implement those used in the original APL code.

#              OPTIONS FOR ALTERNATIVE OUTPUT
1. Replace  sqrt(21.)  by  sqrt(21)  to show errors are identically zero
2. Change  Digits:=17:  to yield a desired accuracy of the coefficients.
3. Set pextra:=1: to add additional order tests by removing  #
4. Allow norms rather than individual errors by removing  #

$\to$ Our first B-series program
The data file for the Cooper-Verner method, CVRK118method.txt follows in the box below.

0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 2
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 4
1 0 4
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 7
-7 -3 98
21 5 49
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
11 1 84
0 0 1
18 4 63
21 -1 252
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
5 1 48
0 0 1
9 1 36
-231 14 360
63 -7 80
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
10 -1 42
0 0 1
-432 92 315
633 -145 90
-504 115 70
63 -13 35
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 14
0 0 1
0 0 1
0 0 1
14 -3 126
13 -3 63
1 0 9
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
1 0 32
0 0 1
0 0 1
0 0 1
91 -21 576
11 0 72
-385 -75 1152
63 13 128
0 0 1
0 0 1
0 0 1
0 0 1
1 0 14
0 0 1
0 0 1
0 0 1
1 0 9
-733 -147 2205
515 111 504
-51 -11 56
132 28 245
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
-42 7 18
-18 28 45
-273 -53 72
301 53 72
28 -28 45
49 -7 18
0 0 1
0 0 1
1 0 20
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
0 0 1
49 0 180
16 0 45
49 0 180
1 0 20
0 0 1