48
Spline interpolation for the magnetic confinement fusion particle-following code ASCOT Patrik Ollus February 7, 2021

Spline interpolation for the magnetic confinement fusion

Embed Size (px)

Citation preview

Spline interpolation for the magneticconfinement fusion particle-following

code ASCOT

Patrik Ollus

February 7, 2021

Abstract

A cubic spline interpolation code library was developed for the fifth ver-sion of the fusion research code ASCOT [1], which simulates test particlesin a magnetic confinement fusion device. This library is predominantly usedto interpolate magnetic field data in two or three dimensions. Other usesinclude interpolation of electric field data, plasma density and temperatureprofiles and reaction cross-sections.

This report reviews basic cubic spline theory, two different forms for splinefunctions, extension to multidimensional interpolation, and other theoreticalaspects. The source code is described on the levels of organization, func-tionality and interface. Testing was performed regarding convergence andcomputational performance. Convergence of an approximating spline to ananalytical test function was calculated and found to adhere to the theoreticalrate of h4, with the caveat of having to avoid edge effects near mismatchingboundary conditions. The difference in computational performance betweenthe algorithms of the two spline forms was measured, confirming a trade-offfor the available memory savings.

Contents

1 Introduction 1

2 Theory of cubic splines 22.1 Basics of cubic splines . . . . . . . . . . . . . . . . . . . . . . 22.2 Compact form . . . . . . . . . . . . . . . . . . . . . . . . . . . 72.3 Periodic and other boundary conditions . . . . . . . . . . . . . 102.4 Extension to multiple dimensions . . . . . . . . . . . . . . . . 13

2.4.1 Tensor product construct . . . . . . . . . . . . . . . . . 132.4.2 Bicubic spline . . . . . . . . . . . . . . . . . . . . . . . 162.4.3 Tricubic spline . . . . . . . . . . . . . . . . . . . . . . 19

2.5 Interpolated derivatives . . . . . . . . . . . . . . . . . . . . . . 212.6 Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3 ASCOT5 splines - the code 253.1 Source code . . . . . . . . . . . . . . . . . . . . . . . . . . . . 253.2 Interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 263.3 Further development . . . . . . . . . . . . . . . . . . . . . . . 27

4 Testing 294.1 Convergence . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29

4.1.1 Unit tests . . . . . . . . . . . . . . . . . . . . . . . . . 294.1.2 ASCOT test case . . . . . . . . . . . . . . . . . . . . . 34

4.2 Computational performance . . . . . . . . . . . . . . . . . . . 37

5 Summary and future 41

6 References 42

Appendix A Tricubic spline evaluation 43

1 Introduction

The particle-following code ASCOT models minority plasma particles intorus-shaped magnetic confinement fusion devices using the Monte Carlomethod [1]. Physical particles are in a simulation represented by test parti-cles. The behavior of these test particles is determined by background fieldquantities, most importantly the magnetic field. In fusion devices, the mag-netic fields are complicated and cannot be described by analytical functions.Instead, discrete value grids are calculated numerically. In simulations, con-tinuous field quantities are required, making interpolation of the grids neces-sary. ASCOT uses mostly cubic spline interpolation. Besides the magneticfield, also other quantities are given as discrete grids and interpolated. Thesecan include the electric field, density and temperature profiles of bulk plasmaparticles and reaction cross-sections.

A cubic spline is a piecewise polynomial function of third degree thatinterpolates a discrete grid of data under some boundary conditions. Themagnetic fields in fusion devices occupy three-dimensional real space, butcan be axisymmetric. They are typically represented by value grids with twoor three spatial coordinates. Spline theory extends to multiple variables, andcan be used to interpolate these multidimensional data grids. Furthermore,an ASCOT simulation requires not only the magnetic field value but alsoits gradients. Cubic spline functions are C2-smooth, thus facilitating thisrequirement as well.

ASCOT has been rewritten to utilize the parallelization capabilities ofmodern super-computer architectures. Earlier versions of the code were writ-ten in Fortran, and used Princeton splines, a Fortran-based spline library [2].The new version, ASCOT5 [3], was written in the C programming language,and needed its own spline library in the same language. This library waswritten from the ground up, but based on the Princeton splines code.

This report is a documentation of the newly developed cubic spline inter-polation library and its testing. Chapter 2 reviews the mathematical theoryof cubic splines, including discussion on boundary conditions, different formsof the spline function and extension to multiple dimensions. The interpola-tion library is described in chapter 3, in the context of the ASCOT5 code asa whole. Chapter 4 reports on the testing done regarding convergence andcomputational performance. Finally, a summary is given and future plansdiscussed in chapter 5.

1

2 Theory of cubic splines

Cubic splines are piecewise polynomial functions of third degree. In thiswork, they are used to interpolate discrete value grids. However, they haveapplications in other areas as well, such as finite element solutions to differ-ential equations [4, 5].

This chapter starts by reviewing basic theory behind cubic splines, andhow a spline interpolation problem is solved. As an alternative to the tra-ditional explicit form of splines, a compact form is introduced. For givenboundary conditions to a set of grid values, a spline is unique. However, thereis a range of possible boundary conditions, some of which will be discussedin more detail. After establishing the basics through consideration of theone-dimensional (1D) spline, extension of the theory to multiple dimensionswill be introduced. A brief discussion on differentiation of an interpolatingspline is also included. Finally, the perspective of splines as approximatingfunctions is adopted for a few words on convergence.

2.1 Basics of cubic splines

Consider the real axis interval [xmin, xmax] subdivided by a mesh of points xi,where i = 0, 1, 2, ..., N for some N , and x0 = xmin and xN = xmax. Supposea function f assigns a value fi = f(xi) to each grid point. The resultingpoints (xi, fi) are called knots. Now consider that a function s(x), which iscontinuous and has continuous first and second derivatives in [x0, xN ], is acubic polynomial in each subinterval [xi, xi+1], where i = 0, 1, ..., N − 1, andfor which s(xi) = fi for all i. Then s(x) is the interpolating cubic spline ofthe set of knots (xi, fi), and minimizes the integral [6]∫ xN

x0

(s′′(x)

)2dx. (1)

If the slope of the function is small, s′′(x) approximates the curvature. Inthis case, if we imagine the curve s to be an elastic physical strip, integral (1)measures the energy of the system. Thus, we can say that the cubic splinefunction minimizes energy.

As s(x) is a piecewise polynomial of third degree, it has four coefficientsin each subinterval [xi, xi+1]. There are N intervals, making a total of 4N un-knowns. The function s(x) is known to be continuous and twice continuously

2

differentiable at each of the N−1 internal knots. That gives us 3(N−1) con-ditions. Further, s(xi) = fi for all N+1 knots, giving N+1 more conditions.Thus far, there are 4N unknowns and a total of 4N − 2 conditions. We needtwo more conditions for closure of the system of equations that determiness(x).

A classic boundary condition is to make the second derivative disappearat the endpoints, i.e. s′′(x0) = s′′(xN) = 0. This defines the so-called naturalcubic spline, which minimizes the curvature for the entire function, making itthe smoothest interpolating function of the data set in question. Many otherpossible boundary conditions exist, some of which are discussed in chapter2.3. The choice of boundary condition slightly affects the interpolation prob-lem. In what follows, the natural boundary condition will be the default.

Let us define the exact form of the cubic spline function s(x) we aim tofind. We start by defining a normalized variable for the subinterval [xi, xi+1],

xi =x− xihi

∈ [0, 1],

where hi = xi+1−xi is the length of the subinterval. Since xi is independentof the index i of the interval, we can rewrite xi = x. The expression for thecubic spline function s(x) in the i:th subinterval takes the form

si(x) = ai + bix+ cix2 + dix

3. (2)

The interpolation problem consists in finding the polynomial coefficients ai,bi, ci and di, such that s(x) interpolates the knots (xi, fi). We need toconstruct a linear system of equations, and solve it for a set of given valuesfi.

The linear system can be constructed using the conditions listed earlier.To reiterate, these consist of relations between the pieces si(x) of the splinefunction and the data values fi, and between neighbouring spline pieces, aswell as the boundary conditions. Taking a similar approach as Bartels et al.[7], we start by recognizing that

si(0) = ai = fi, (3)

si(1) = ai + bi + ci + di = fi+1. (4)

The derivative with respect to the normalized variable is

Di(x) = s′i(x) = bi + 2cix+ 3dix2.

3

For notational ease, we denote Di = Di(0). We consider the derivative atthe subinterval endpoints and apply continuity of the first derivative, i.e.,Di(1) = Di+1, to give

Di = bi, (5)

Di+1 = bi + 2ci + 3di, (6)

for i = 0, ..., N − 1. Now we have a system of the four eqs. (3) - (6), withthe solution

ai = fi,

bi = Di,

ci = 3(fi+1 − fi)− 2Di −Di+1,

di = 2(fi − fi+1) +Di +Di+1.

(7)

Note that we have reduced our problem from 4N to N + 1 unknowns. Ifwe know the values of Di, we can find the polynomial coefficients using therelations (7), since the values fi are already known. To solve values Di, weneed to apply more conditions.

The second derivative is

s′′i (x) = 2ci + 6dix. (8)

We evaluate the second derivative at the subinterval endpoints and applycontinuity of the second derivative, i.e., s′′i−1(1) = s′′i (0), to get

s′′i (0) = 2ci−1 + 6di−1 = 2ci,

for i = 1, ..., N − 1. Inserting relations (7) yields

2(3(fi − fi−1)− 2Di−1 −Di

)+ 6(2(fi−1 − fi) +Di−1 +Di

)= 2(3(fi+1 − fi)− 2Di −Di+1

),

which simplifies to

Di−1 + 4Di +Di+1 = 3(fi+1 − fi−1). (9)

As established, we still need two conditions to close the system of equa-tions, namely boundary conditions. We choose the natural boundary con-ditions by requiring second derivatives at the endpoints to be zero, i.e.,s′′0(0) = s′′N(1) = 0. Recalling eqs. (8) and (7), the first subinterval yields

2c0 = 0

2(3(f1 − f0)− 2D0 −D1

)= 0,

4

which rearranges to2D0 +D1 = 3(f1 − f0). (10)

Similarly, the last subinterval yields

2cN−1 + 6dN−1 = 0

2(3(fN − fN−1)− 2DN−1 −DN

)+6(2(fN−1 − fN) +DN−1 +DN

)= 0,

which simplifies to

DN−1 + 2DN = 3(fN − fN−1). (11)

Now we have N + 1 equations for our N + 1 unknowns. The equationsare contained in relations (9), (10) and (11). We can write the linear systemin matrix form as

2 11 4 1

1 4 1...1 4 1

1 2

D0

D1

D2

...DN−1DN

=

3(f1 − f0)3(f2 − f0)3(f3 − f1)

...3(fN − fN−2)3(fN − fN−1)

. (12)

The matrix is tridiagonal and diagonally dominant. It can be solved usingthe Thomas algorithm, a simplified and faster version of Gauss eliminationfor tridiagonal systems [6, 7].

The Thomas algorithm for solving the linear system (12) is presented as itis implemented in the spline interpolation library of the ASCOT5 code, usingthe C programming language. We denote the superdiagonal elements by pi(p[i] in C), where i = 0, ..., N−1, and the right-hand side (RHS) elements byFi (F[i] in C), where i = 0, ..., N . First, we do a forward elimination sweep.The algorithm in C code reads

p [ 0 ] = 1 . 0 / 2 ;F [ 0 ] = F [ 0 ] / 2 ;f o r ( i = 1 ; i < N; i++) {

p [ i ] = 1/(4 − p [ i −1 ] ) ;F [ i ] = (F [ i ] − F[ i −1])/(4 − p [ i −1 ] ) ;

}F[N] = (F [N] − F[N−1])/(2 − p [N−1 ] ) ;

5

After that, the linear system takes the form1 p00 1 p1

0 1 p2...0 1 pN−1

0 1

D0

D1

D2

...DN−1DN

=

F0

F1

F2

...FN−1FN

.

This gives us DN directly. We get the rest of the values Di (D[i] in C)iteratively by backward substitution. The code reads as follows,

D[N] = F[N ] ;f o r ( i = N−1; i > −1; i−−) {

D[ i ] = F [ i ] − p [ i ]∗D[ i +1] ;}

Now that we have the derivatives Di at the knots, we can calculate thepolynomial coefficients ai, bi, ci and di for each piece of the spline functionusing eqs. (7). That concludes the construction of the interpolating cubicspline. With values for the polynomial coefficients, the spline function s(x)is known and we can evaluate it to interpolate the data set fi at arbitrarypoints in the domain [x0, xN ].

Evaluation of the interpolating spline function at a point x works asfollows. First, we determine the subinterval [xi, xi+1] such that xi ≤ x < xi+1.Then we calculate the normalized variable x in the subinterval. In the generalcase of non-uniform grid spacing, the formula reads

x =x−

(x0 +

∑i−1j=0 hj

)hi

. (13)

If we use a uniformly spaced grid, with grid spacing hi = h = x1 − x0, theneq. (13) simplifies to

x =x− (x0 + ih)

h.

The current version of the ASCOT5 spline library only supports uniform gridspacing. Finally, the value of the interpolating spline function s(x) at thenormalized variable point x in subinterval [xi, xi+1] is evaluated using eq. (2).

6

Above, we have derived the 1D cubic spline in so-called explicit form,characterized by the use of polynomial coefficients. There exists an alter-native representation, the so-called compact form, which is presented in thenext chapter.

2.2 Compact form

The compact form, an alternative but equivalent representation for the cubicspline function, is based on that a cubic polynomial in an interval is uniquelydefined by the function values and second derivatives at the endpoints. Thisreduces the number of needed coefficients per subinterval from four to twocompared to the explicit form featured in chapter 2.1.

Suppose again we have a domain [xmin, xmax] subdivided by a grid ofpoints xi, where i = 0, ..., N for some N , and x0 = xmin and xN = xmax. Afunction f assigns values fi = f(xi) to each point, and there exists a cubicspline s(x) that interpolates f . Consider the subinterval [xi, xi+1], with thelength hi = xi+1−xi. We define a normalized variable x = (x−xi)/hi, whichruns from 0 up to 1, and a reversed normalized variable xr = (xi+1 − x)/hi,which runs from 1 down to 0. The compact form for the expression of thecubic spline function in this interval reads [8]

si(x) = xrfi + xfi+1 +h2i6

((x3r − xr)f ′′i + (x3 − x)f ′′i+1

), (14)

where f ′′i is the second derivative of the spline function with respect to thetrue variable x at the knot xi.

Note that the compact evaluation formula (14) uses coefficients (functionvalue and second derivative) corresponding to the knots at both ends of thesubinterval. This means that the compact form requires 2 · 2 = 4 coefficientsfor evaluation, same as the explicit one, despite needing fewer coefficientsin total for spline definition. It also means that the compact form requiresthe coefficients for the last knot, at xN , unlike the explicit form, which onlyuses the coefficients for the starting knot of a subinterval in its evaluationformula, eq. (2). In other words, the compact form requires a total of 2N+2coefficients to be stored in memory, while the explicit form requires 4N .

Before continuing, let us prove that expression (14) is indeed equivalentto the corresponding expression (2) of the explicit form. We can rewriteeq. (14) in terms of only the variable x, noting that xr = 1 − x, and that

7

a variable change and application of the chain rule for the second derivativeyields

d2

dx2si(x) =

d2x

dx2d

dxsi(x) +

(dx

dx

)2d2

dx2si(x) =

1

h2is′′i (x). (15)

Equation (14) then becomes

si(x) = (1− x)fi + xfi+1

+1

6

(((1− x)3 − (1− x)

)s′′i (x)|x=0 + (x3 − x)s′′i (x)|x=1

).

(16)

Recalling eqs. (3), (4) and (8), we can substitute the coefficients ai, bi, ci anddi into expression (16) and do a straight-forward calculation,

si(x) = (1− x)ai + x(ai + bi + ci + di)

+1

6

(((1− x)3 − (1− x))2ci + (x3 − x)(2ci + 6di)

)= ai + bix+ cix+ dix

+1

6

(−6cix+ 6cix

2 − 6dix+ 6dix3)

= ai + bix+ cix2 + dix

3.

This is exactly the expression (2), proving that the explicit and compactevaluation formulas for the cubic spline are equivalent. �

We proceed to construct a linear system for solving the values of thesecond derivative at the knots, as calculated with respect to the normalizedvariable. These unknowns will be denoted by Ei, and have the definitionEi = s′′i (x)|x=0. By continuity of the second derivative, we have the relationEi+1 = s′′i (x)|x=1. Since there is no spline function beyond xN , the lastunknown has the definition EN = s′′N−1(x)|x=1. We evaluate eq. (8) at theendpoints, rearrange and combine to get

ci =1

2Ei (17)

di =1

6(Ei+1 − Ei) (18)

Inserting eqs. (3), (17) and (18) into eq. (4) gives

bi = fi+1 − fi −1

2Ei −

1

6(Ei+1 − Ei). (19)

8

Rewriting eqs. (5) and (6), we can state the continuity of the first derivativeas

bi = bi−1 + 2ci−1 + 3di−1.

Inserting eqs. (17), (18) and (19), and performing algebra yields the relation

Ei−1 + 4Ei + Ei+1 = 6(fi−1 − 2fi + fi+1).

As boundary conditions, we once again choose to make the second derivativezero at the endpoints, i.e., construct the natural spline. Since E0 = EN =0, we can drop the top and bottom rows, reducing the system matrix todimensions (N − 1)× (N − 1). The system then reads

4 11 4 1

1 4 1...1 4 1

1 4

E1

E2

E3

...EN−2EN−1

=

6(f0 − 2f1 + f2)6(f1 − 2f2 + f3)6(f2 − 2f3 + f4)

...6(fN−3 − 2fN−2 + fN−1)6(fN−2 − 2fN−1 + fN)

. (20)

Solving linear system (20) is similar to what we did in the explicit case inchapter 2.1. However, since we dropped the top and bottom rows, the indicesfor the non-zero superdiagonal elements pi and RHS elements Fi run as i =1, ..., N − 2 and i = 1, ..., N − 1, respectively (actually, the forward sweepleaves pN−1 6= 0, but it has no effect in the backward substitution because itis multiplied by En = 0). The algorithm for the forward elimination sweepin C code reads

p [ 0 ] = 0 . 0 ;F [ 0 ] = 0 . 0 ;f o r ( i = 1 ; i < N; i++) {

p [ i ] = 1/(4 − p [ i −1 ] ) ;F [ i ] = (F [ i ] − F[ i −1])/(4 − p [ i −1 ] ) ;

}F[N] = 0 . 0 ;

Now we apply the same backward substitution as in the explicit case. How-ever, this time we are inserting zeros at the endpoints using variable valuesfrom the previous algorithm. The algorithm for backward substitution reads

9

E[N] = F[N ] ;f o r ( i = N−1; i > −1; i−−) {

E[ i ] = F [ i ] − p [ i ]∗E[ i +1] ;}

With the Ei:s solved and the function values fi at the knots known fromthe start, we are almost ready to use the evaluation formula. However,one important step remains. Recall that the Ei:s we solved are the secondderivatives with respect to the normalized variable x, while the f ′′i :s in eq.(14) are the second derivatives with respect to the true variable x. As shownin eq. (15), we get values f ′′i from values Ei by scaling with the square of thesubinterval length,

f ′′i =Eih2i. (21)

Now that we have both the function values fi and the values f ′′i of the secondderivative at the knots, we can evaluate the spline function at arbitrary pointsx using eq. (14).

2.3 Periodic and other boundary conditions

The natural spline boundary condition is a common choice, but there aremany other possibilities. Which is most suitable ultimately depends on theproblem at hand.

Possible boundary conditions include given endpoint derivatives, s′(x0)and s′(xN), or second derivatives s′′(x0) and s′′(xN). Note that the naturalspline is a special case of the latter. One possible boundary condition isto calculate third derivatives from the unique cubic polynomials that passthrough the first four and last four data points [6]. The so-called not-a-knot boundary condition forces the third derivative, which generally is astep function, to be continuous at the points x1 and xN−1 [4]. A periodicboundary condition can also be imposed, where the function values and thefirst and second derivatives are all made periodic.

In magnetic confinement fusion, the magnetic field has the shape of atorus. ASCOT uses coordinates (r, φ, z), where r is the radius from the centerof the torus, φ the toroidal angle and z the height. The field is periodic inthe φ coordinate. Therefore, the ASCOT5 splines include interpolation usingthe periodic boundary condition. Below, we present the construction of theperiodic spline in the compact form.

10

Suppose again a set of data points (xi, fi), where i = 0, ..., N . In theperiodic boundary condition case, we imagine an extra knot (xN+1, fN+1) atthe end, such that s(xN+1) = fN+1 = f0 = s(x0), s

′(xN+1) = s′(x0) ands′′(xN+1) = s′′(x0). This way of thinking makes is easier to first set up theproblem, but, eventually, we will substitute fN+1 = f0. Proceeding as we didin chapter 2.2, to solve the problem in the compact form, the only differenceis at the endpoints. There we have

EN + 4E0 + E1 = 6(fN − 2f0 + f1)

EN−1 + 4EN + E0 = 6(fN−1 − 2fN + f0).

Hence, the linear system in the periodic case reads4 1 11 4 1

1 4 1...1 4 1

1 1 4

E0

E1

E2

...EN−1EN

=

6(fN − 2f0 + f1)6(f0 − 2f1 + f2)6(f1 − 2f2 + f3)

...6(fN−2 − 2fN−1 + fN)

6(fN−1 − 2fN + f0)

. (22)

It is worth mentioning that, if we were instead using the explicit form, i.e.,solving for a vector of first derivatives Di, the system matrix would be exactlythe same. Only the RHS elements would be different. Hence, the belowpresented algorithm for solving the linear system would also be the same.

Because of the non-zero elements in the lower left and upper right cornerof the matrix in eq. (22), it is not strictly tridiagonal, which makes the systemmore cumbersome to solve than in the case of the natural spline in chapters2.1 and 2.2. It is necessary to eliminate also the lower left element in theforward sweep of the simplified Gauss elimination algorithm, and keep trackof the right column of values generated during the sweep by the original upperright corner value. We introduce some new helper variables for the algorithm.The value that starts from the lower left corner and moves rightward duringthe sweep, we denote by L. The last diagonal and subdiagonal values, wedenote by dlast and blast, respectively. These will change during the sweepbecause of the elimination of L. Finally, the right column vector of thematrix, omitting the last two elements (last superdiagonal and last diagonal),we denote by r. The algorithm in C code reads

11

L = 1 . 0 ;d l a s t = 4 . 0 ;p [ 0 ] = 1 . 0 / 4 ;r [ 0 ] = 1 . 0 / 4 ;F [ 0 ] = F [ 0 ] / 4 ;f o r ( i = 1 ; i < N−1; i++) {

d l a s t = d l a s t − L∗ r [ i −1] ;F [N] = F[N] − L∗F[ i −1] ;L = −L∗p [ i −1] ;p [ i ] = 1/(4 − p [ i −1 ] ) ;r [ i ] = −r [ i −1]/(4 − p [ i −1 ] ) ;F [ i ] = (F [ i ] − F[ i −1])/(4 − p [ i −1 ] ) ;

}b l a s t = 1 .0 − L∗p [N−2] ;d l a s t = d l a s t − L∗ r [N−2] ;F [N] = F[N] − L∗F[N−2] ;p [N−1] = (1 − r [N−2])/(4 − p [N−2 ] ) ;F [N−1] = (F [N−1] − F[N−2])/(4 − p [N−2 ] ) ;F [N] = (F [N] − b l a s t ∗F[N−1])/( d l a s t − b l a s t ∗p [N−1 ] ) ;

The matrix equation now takes the form1 p0 r00 1 p1 r1

...1 pN−2 rN−20 1 pN−1

0 1

E0

E1

...EN−2EN−1EN

=

F0

F1

...FN−2FN−1FN

.

At this point, the difference to the case of the natural spline is the rightcolumn vector r of the matrix.

The vector r will make the back substitution algorithm slightly moreinvolved than for the natural spline. It reads

E[N] = F[N ] ;E [N−1] = F [N−1] − p [N−1]∗E[N ] ;f o r ( i = N−2; i > −1; i−−) {

E[ i ] = F [ i ] − p [ i ]∗E[ i +1] − r [ i ]∗E[N ] ;}

12

This completes the process of solving the linear system of the periodic spline,and we obtain the second derivatives at the knots. As in chapter 2.2, oncewe have scaled the second derivatives according to eq. (21), we can insertthem in eq. (14) to interpolate at arbitrary points.

2.4 Extension to multiple dimensions

Thus far, only 1D splines have been considered. It is possible to constructmultidimensional cubic spline functions that interpolate multidimensionaldiscrete value grids. Note that, in this context, the word dimension is usedas synonymous to the word variable. Multidimensional function means mul-tivariate function. The motivation behind the use of the word dimensionis physical intuition. A multivariate spline can model a magnetic or otherphysical field in multiple physical dimensions.

Spline theory offers a convenient method of constructing multidimensionalsplines by iteratively calculating several 1D splines, the tensor product con-struct, which is explained in more detail below in chapter 2.4.1. For thepurposes of this work, we are interested in two-dimensional (2D) interpolat-ing spline surfaces and three-dimensional (3D) interpolating spline volumes,called bicubic and tricubic splines, respectively. The bicubic spline construc-tion is presented first, in chapter 2.4.2. When we are familiar with that, itis easy to continue the same logic to the tricubic case, which is presentedconcisely in chapter 2.4.3.

2.4.1 Tensor product construct

This chapter delves somewhat into the theory regarding tensor products oflinear function spaces, linear functionals and linear interpolation schemes.The aim is to show how the tensor product nature of 2D basis functions canbe utilized to construct a 2D interpolating function as a serial constructionof 1D interpolating functions. The full version of this explanation can befound in [4]. For the interested reader, other references with good materialon this subject, and spline theory in general, are [5] and [9].

Let U and V be two linear spaces of functions defined on sets X and Y ,respectively, and giving real values. For each function u in U and function vin V , a function w on the Cartesian product space X × Y is defined as

w(x, y) = u(x)v(y),

13

for all (x, y) ∈ X × Y . This is called the tensor product of u with v, whichwe denote u⊗ v. The tensor product of the two linear function spaces U andV is the set of all finite linear combinations of functions of the from u ⊗ v.The definition reads

U ⊗ V =N∑i=0

αi(ui ⊗ vi),

where αi ∈ R, ui ∈ U and vi ∈ V for some N .Let λ and µ be linear functionals on U and V , respectively. According to

[4], a linear functional λ⊗ µ on U ⊗ V is defined as follows,

λ⊗ µ

(∑i

ui ⊗ vi

)=∑i

(λui)(µvi), (23)

for all∑

i ui ⊗ vi. Suppose

w =∑i

ui ⊗ vi,

for some (ui, vi) ∈ U × V . Using the definition of ui ⊗ vi, we can define theunivariate function

wy : X → R, wy(x) = w(x, y) =∑i

ui(x)vi(y)

for a particular y ∈ Y and all x ∈ X. In other words, y is fixed, while x isa variable. When not calling it explicitly for a variable value, this functiontakes the form

wy =∑i

vi(y)ui.

Since wy ∈ U , we can apply to it the functional λ. This yields a number as

λwy =∑i

vi(y)(λui),

where we have used the linearity of λ. Now we can define this as the sectionof w defined on Y , i.e.,

wλ : Y → R, wλ(y) = λwy,

14

for all y ∈ Y . When not explicitly applying it to a variable value, thisfunction takes the form

wλ =∑i

(λui)vi.

Since wλ ∈ V , we can apply the functional µ as

µwλ = µ

(∑i

(λui)vi

)=∑i

(λui)(µvi).

Note that∑

i(λui)(µvi) depends only on the functionals λ and µ and on thefunction w =

∑i ui ⊗ vi, not on the constituent functions ui and vi. Similar

to the construction of wλ above, we can apply the functional µ to derive afunction

wµ =∑i

(µvi)ui ∈ U,

for which

λwµ = λ

(∑i

(µvi)ui

)=∑i

(λui)(µvi).

We conclude that eq. (23) indeed defines a linear functional on U ⊗ V ,and that this linear functional satisfies

(λ⊗ µ)w = λwµ = µwλ, (24)

for all w ∈ U ⊗ V . For example, wµ(x) is the result of applying µ to w(x, y)as a function of y for each fixed x ∈ X.

We are now ready to look at the tensor product of two linear interpolationschemes. Suppose we have a sequence of linear functionals {λi}Nx

i=0 on U and

a sequence of linear functionals {µi}Ny

i=0 on V . These functionals pick out thefunction value of what they operate on. Combine these with correspondingsequences of basis functions {ui}Nx

i=0 and {vi}Ny

i=0, respectively, and we havetwo 1D linear interpolation problems with Gramian matrices

A = (λiuj),

B = (µivj),

which are invertible. Suppose further that we have a matrix νij of linearfunctionals on some linear function space W , which contains U ⊗ V , suchthat

νij(u⊗ v) = (λiu)(µjv),

15

for all i, j and all (u, v) ∈ U × V . Note that this follows the rule in eq. (23).For a given function w ∈ W , we can evaluate the interpolant r with theformula

r =∑i,j

Cijui ⊗ vj.

Utilizing the result described by eq. (24), the coefficients Cij can be foundas the elements of the matrix product

C = A−1(νijw)(BT )−1. (25)

This result shows that a 2D linear interpolation problem can be solvedas two consecutive 1D linear interpolation problems, if the basis functions ofthe 2D interpolant are the tensor product of the basis functions of the 1Dinterpolants. The theory presented here for 2D problems extends readily tohigher dimensions [5, 9].

2.4.2 Bicubic spline

In this chapter, we introduce the bicubic interpolating spline and presentits tensor product construction. Consider the Cartesian product domain[xmin, xmax] × [ymin, ymax] that is partitioned by a 2D rectilinear grid intoNx · Ny cells [xi, xi+1] × [yj, yj+1], where i = 0, ..., Nx and j = 0, ..., Ny forsome Nx and Ny, and x0 = xmin, xNx = xmax, y0 = ymin and yNy = ymax.The grid is rectilinear because that is a requirement of the tensor productconstruct for multidimensional splines [2]. Suppose a function f assignsvalues fij = f(xi, yj) to the grid points. The bicubic spline surface s(x, y)that interpolates this value grid consists of piecewise bivariate polynomialfunctions of third degree in each variable, and is continuous up to the secondorder derivative in each variable on the grid lines. Each grid cell has its ownfunction sij, a piece of the total spline function. It takes the form

sij(x, y) = C00ij + xC10

ij + x2C20ij + x3C30

ij

+y(C01ij + xC11

ij + x2C21ij + x3C31

ij )

+y2(C02ij + xC12

ij + x2C22ij + x3C32

ij )

+y3(C03ij + xC13

ij + x2C23ij + x3C33

ij ),

(26)

where x and y are the normalized grid cell variables

x =x− xihx,i

, y =y − yjhy,j

, (27)

16

with grid cell side lengths hx,i = xi+1−xi and hy,j = yj+1−yj. The compoundterms of the normalized variables, i.e., xlym, where l,m = 0, ..., 3, act asbasis functions in the function space of sij. Each basis function is scaled byits corresponding coefficient C lm

ij , where l and m reflect the degree, in eachvariable, of the basis function. The interpolation problem consists in findingthe coefficients C lm

ij , such that s(x, y) = {sij(x, y)}Nx,Ny

i=0,j=0 interpolates the 2Dgrid of values fij.

The basis functions {xlym}3,3l=0,m=0 are the tensor product of the corre-

sponding 1D basis function sequences {xl}3l=0 and {ym}3m=0. Hence, theresults of chapter 2.4.1 can be utilized to separate the 2D cubic spline in-terpolation problem into consecutive 1D cubic spline interpolation problems,similarly as in eq. (25). However, since our 1D cubic splines have four basisfunctions in each cell, the calculation is a bit more involved than two matrixoperations. Further separation and restructuring of the problem is requiredafter the first 1D spline solutions are found.

The tensor product construction of the bicubic spline for the above de-fined 2D interpolation problem goes as follows. At every fixed value yj ofthe variable y, we calculate the 1D cubic spline along the variable x. Thiscalculation works exactly as in chapter 2.1. The function values fij are thevalues used in the RHS vector of the linear system (12). These 1D splinescoincide with s(x, y) when y = 0, i.e., when y = yj. Hence, the coefficientswe have found are the coefficients C l0

ij , where l = 0, ..., 3.The coefficients found are four new sets of grid values. They will serve as

the values to be interpolated in the next step. At every fixed value xi of thevariable x, we calculate four separate 1D cubic splines along the variable y.When the coefficients C00

ij (i.e., the function values fij) are used in the RHSvector of the linear system (12), the calculation yields the coefficients C0m

ij ,where m = 0, ..., 3. Similarly, coefficients C10

ij yield coefficients C1mij , coeffi-

cients C20ij yield coefficients C2m

ij , and coefficients C30ij yield coefficients C3m

ij .This completes the construction of the bicubic spline. With all coefficientsC lmij found, the expression (26) for s(x, y) interpolates the discrete function

f(xi, yj), and we can evaluate the spline interpolant at arbitrary points inthe full domain [x0, xNx ]× [y0, yNy ].

Note that, due to the nature of the tensor product construct, the ordercould have been reversed, as indicated by eq. (24). We could have firstcalculated 1D splines along y and then along x. That would have yielded thecoefficients in a different order, but they would still have been the same in the

17

end. This will hold also in the alternative forms and higher dimensionalitiespresented below.

Let us now briefly present the compact form of the bicubic spline. Onceagain, we write down the expression for the spline function first. As in the1D case, presented in chapter 2.2, polynomial coefficients, featured in theexplicit form, are in the compact form replaced by the function values of theinterpolated function and by second partial derivatives of the spline functionat the knots. We will adopt a notation using the symbol f of the interpolatedfunction. The function values f(xi, yj) will be denoted by fi,j. The secondpartial derivatives will be identified with superscripts. For example, thesecond partial derivative of the spline function s with respect to the variablex at (xi, yj) will be denoted by fxxi,j . Commas are used between indices forthe compact form because some indices are followed by +1. To present theformula of the spline function concisely, we will define a few helper variables,namely ’reversed’ variables, expressions containing the ’cube’ of the variable,and combinations of the two:

xr = 1− x,yr = 1− y,xc = x3 − x,yc = y3 − y,xrc = (1− x)3 − (1− x),

yrc = (1− y)3 − (1− y).

(28)

Also the grid cell side lengths hx,i and hy,j are relevant. These enter into theexpression because the second derivatives are taken with regard to the truevariables x and y, and not the normalized variables x and y. The compactform expression for the bicubic spline function in a particular grid cell reads

18

sij(x, y) = xr(yrfi,j + yfi,j+1)+

x(yrfi+1,j + yfi+1,j+1)

+h2x,i6

(xrc(yrf

xxi,j + yfxxi,j+1)+

xc(yrfxxi+1,j + yfxxi+1,j+1)

)+h2y,j6

(xr(yrcf

yyi,j + ycf

yyi,j+1)+

x(yrcfyyi+1,j + ycf

yyi+1,j+1)

)+h2x,ih

2y,j

36

(xrc(yrcf

xxyyi,j + ycf

xxyyi,j+1)+

xc(yrcfxxyyi+1,j + ycf

xxyyi+1,j+1)

).

(29)

Note that function values and second partial derivatives, of which there area total of four for each knot, from all corners of the grid cell are used. Hence,the total number of coefficients used for evaluation in the compact form is4 · 4 = 16, same as in the explicit form.

In the compact form, the idea of the construction of the 2D spline isthe same as in the explicit scheme, calculating consecutively 1D splines.The difference is that now we use the compact form algorithm presented inchapter 2.2. First, 1D splines are calculated along x at each yj, using thevalues fi,j of the function to be interpolated as the knot values in the RHSvector of the linear system (20). This yields second partial derivatives fxxi,j .Then two separate 1D splines are calculated along y at each xi, one using fi,jas knot values to get the second partial derivatives f yyi,j , and another usingfxxi,j as knot values to get the second partial derivatives fxxyyi,j . This completesthe construction of expression (29) for the compact form of the interpolatingbicubic spline.

2.4.3 Tricubic spline

This chapter introduces the tricubic interpolating spline and briefly presentsits tensor product construction. Consider the Cartesian product domain[xmin, xmax]× [ymin, ymax]× [zmin, zmax] that is partitioned by a 3D rectilineargrid into Nx ·Ny ·Nz cells [xi, xi+1]× [yj, yj+1]× [zk, zk+1], where i = 0, ..., Nx,j = 0, ..., Ny and k = 0, ..., Nz for some Nx, Ny and Nz, and x0 = xmin,xNx = xmax, y0 = ymin, yNy = ymax, z0 = zmin and zNz = zmax. Suppose afunction f assigns values fi,j,k = f(xi, yj, zk) to the grid points. The tricubic

19

spline volume s(x, y, z) that interpolates this value grid consists of piecewisetrivariate polynomial functions of third degree in each variable, and is con-tinuous up to the second order derivative in each variable on the grid lines.Each grid cell has its own function sijk, a piece of the total spline function.The expressions are very lengthy for both the explicit and the compact formand have been placed in appendix A. The explicit form expression featuresthe normalized grid cell variables

x =x− xihx,i

, y =y − yjhy,j

, z =z − zkhz,k

, (30)

where hx,i = xi+1 − xi, hy,j = yj+1 − yj and hz,k = zk+1 − zk, normalizedvariable compound terms xlymzn, where l,m, n = 0, ..., 3, that act as basisfunctions, and the coefficients C lmn

ijk corresponding to each basis function.There are a total of 43 = 64 polynomial coefficients. The compact formexpression, as written in appendix A, also features the normalized variablesand their compound terms, as well as helper variables made from the nor-malized variables, and the compound terms of these helpers. As coefficients,it has the function values and all possible partial derivatives of second orderin one or several variables. The number of such coefficients correspondingto one knot is 23 = 8. However, the evaluation formula uses the coefficientsfrom all corners of the relevant grid cell, i.e., a total of 8 · 8 = 64 coefficients,same as the explicit form. The interpolation problem consists in finding thecoefficients C lmn

ijk of the explicit form, or the partial second derivatives of thecompact form, such that s(x, y, z) interpolates the function in question.

The extension further from 2D to 3D adds one more variable to the tensorproduct construct. This happens quite intuitively compared to the extensionfrom 1D to 2D, presented in the preceding chapters. Hence, we will onlybriefly discuss the main steps involved in the tensor product construction ofa tricubic interpolating spline.

Suppose we wish to find the tricubic interpolating spline s(x, y, z) to theabove defined problem. First, we calculate for each zk the interpolatingbicubic spline s(x, y, zk). Each of these works exactly as in the 2D casedescribed above, and they will provide us with a number of new sets of gridvalues. Using the explicit form, we get 16 polynomial coefficients C lm0

ijk , oneof which is the original function value. Using the compact form, we retainthe original function values and get three partial second derivatives, yieldingfour coefficients in total. To extend the interpolating spline to include thez direction, we need to solve for each pair (xi, yj) as many 1D cubic spline

20

interpolation problems along z as there are coefficients in the bicubic splineexpression, i.e., 16 in the explicit form and four in the compact form. Afterthat we have found all the needed coefficients, i.e., 16 · 4 = 64 in the explicitform and 4 · 2 = 8 in the compact form for each grid cell (and, in thecase of the compact form, for the knots at the ends of the domain). Thiscompletes the construction of the tricubic interpolating spline, and we canuse the expressions of the cell-specific functions sijk(x, y, z) to interpolate atarbitrary points (x, y, z) in the domain.

This time, we did not specify at which point in the algorithm the differentcoefficients are found, nor which basis functions they correspond to. However,this organization follows the same intuitive logic as in the 2D case, only withone more variable in the fold. It should be straight-forward to deduce.

Note that, as in the 2D case, the order in which the different variablesare included in the interpolation problem can be chosen arbitrarily. If theorder is chosen differently than above, the various coefficients are found in adifferent order, but have the same values in the end.

2.5 Interpolated derivatives

The expressions for the interpolating cubic spline function in varying formsand dimensionalities, i.e., eqs. (2), (14), (26), (29) and those in appendix A,are, per definition of the cubic spline, continuously differentiable up to secondorder in each variable, within each grid cell and in the boundaries betweengrid cells. This ensures well-behaved derivatives for physical applications, animportant property for ASCOT.

Splines consist of polynomial functions, meaning that familiar polynomialderivation rules apply. However, we must keep in mind that our function ex-pressions use normalized variables, such as x = x(x). Derivation is typicallyperformed with respect to a true variable, such as x. Recalling the defini-tions of the normalized variables in eqs. (30) and applying the chain rule,this gives rise to factors such as 1/hx,i in the derivative expressions (recalleq.(15)).

Let us take the 2D case as an example, and calculate the partial derivativewith respect to x, i.e.,

∂xsij(x, y). (31)

21

When using the explicit form, we take the derivative of expression (26) andget that

∂xsij(x, y) =

1

hx,i

(C10ij + 2xC20

ij + 3x2C30ij

+y(C11ij + 2xC21

ij + 3x2C31ij )

+y2(C12ij + 2xC22

ij + 3x2C32ij )

+y3(C13ij + 2xC23

ij + 3x2C33ij )).

For the compact form, we first define shorthand notations for derivatives ofhelper variables in eqs. (28) multiplied by the grid spacing:

xxc = hx,i∂

∂x(xc) = 3x2 − 1,

xxrc = hx,i∂

∂x(xrc) = −3x2r + 1.

Then we take the derivative of expression (29), which yields

∂xsij(x, y) =

1

hx,i

(− (yrfi,j + yfi,j+1)

+(yrfi+1,j + yfi+1,j+1))

+hx,i6

(xxrc(yrf

xxi,j + yfxxi,j+1)

+xxc (yrfxxi+1,j + yfxxi+1,j+1)

)+h2y,j6hx,i

(− (yrcf

yyi,j + ycf

yyi,j+1)

+(yrcfyyi+1,j + ycf

yyi+1,j+1)

)+hx,ih

2y,j

36

(xxrc(yrcf

xxyyi,j + ycf

xxyyi,j+1)

+xxc (yrcfxxyyi+1,j + ycf

xxyyi+1,j+1)

).

The ASCOT code simulates physical phenomena and needs to calculategradients, divergences and curls of multidimensional magnetic and other fieldquantities. These are needed for example in the equations of motion for theguiding-center approach, an approximative particle-following method used toimprove computational performance [1]. Vector derivatives consist of partialderivative components, such as eq. (31). For example, the 3D gradient is

22

a linear combination of unit vectors scaled by the partial derivatives takenwith respect to each variable. It reads

∇sijk(x, y, z) =

(ex∂

∂x+ ey

∂y+ ez

∂z

)sijk(x, y, z), (32)

where ex, ey and ez are the unit vectors along x, y and z, respectively.

2.6 Convergence

Splines interpolate discrete girds of data. If this data is generated by anunderlying unknown continuous function, then the spline can be viewed as anapproximation of this original function. It then becomes interesting to knowhow well the approximation converges to the exact function as we increase theresolution of sampling, i.e., decrease grid spacing. The convergence propertiesof splines can be investigated by applying them to discretizations of knownanalytical functions.

Consider an analytical function fanl, which is infinitely smooth on thedomain [xmin, xmax]. The domain is discretized by a grid of points xi, wherei = 0, ..., N , and x0 = xmin and xN = xmax (except with the periodic bound-ary condition, in which case xN < xmax). The cubic spline approxima-tion fspl of the function fanl is constructed by interpolating the grid values{fanl(xi)}Ni=0. The convergence rate will be related to the rate of tendency tozero of the maximum grid interval length hmax = maxi(hi) = maxi(xi+1−xi),where i = 0, ..., N − 1 (or N with the periodic boundary condition, in whichcase xN+1 = xmax) [5, chapter 2]. We assume that the boundary conditionsused for the spline construction match the analytical function exactly. With-out this match at the domain edges, so-called edge effects would prevent theachievement of optimal convergence close to the edges. This is discussed inchapter 7 of [5] and in [10]. In ASCOT, the discrete input field is generally notgenerated by an underlying function that agrees exactly with the boundaryconditions. However, the field is usually evaluated only far from the domainedges, where the edge effects might have been damped. We investigate thisthrough testing in chapter 4.1.2.

23

Under the above assumptions, the error |fanl − fspl| scales as O(h4max).When we take derivatives of the function, the convergence rate reduces byone order per order of derivative. For the cubic spline, which is continuouslydifferentiable up to the second derivative, the relevant results read

|fanl − fspl| ∝ h4max,

|f ′anl − f ′spl| ∝ h3max,

|f ′′anl − f ′′spl| ∝ h2max.

(33)

These theoretical convergence rates are proved for example in chapter 2 of[5] and chapter 5 of [4].

The same convergence rates persist as we extend to multiple dimensions,only they are restricted by the grid cell side lengths for all variables. Forexample, if we set hmax = hx,max = hy,max, then eqs. (33) hold for 2Dfunctions fanl and fspl, as shown in chapter 7 of [5]. The convergence rates ofthe developed spline interpolation code are numerically tested in chapter 4.1to verify them against the theoretical results.

24

3 ASCOT5 splines - the code

A cubic spline interpolation code was written as an internal library for theASCOT5 code, which simulates test particles in magnetic confinement fusiondevices. Like the rest of ASCOT5, the spline library is written in the Cprogramming language.

This chapter documents the spline library as a part of the ASCOT5 code,without going into details. The organization and division of labour in thesource code is first presented. Then the interface between ASCOT5 and itsinterpolation library is explained. Finally, possible further development isdiscussed.

3.1 Source code

The files that make up the cubic spline interpolation library are contained ina directory by the name spline. The files splineexpl.c and splinecomp.c con-tain the code for solving 1D interpolation problems in the explicit and com-pact forms, respectively. They share the common header file spline.h. Thesix files interp1Dexpl.c, interp1Dcomp.c, interp2Dexpl.c, interp2Dcomp.c,interp3Dexpl.c and interp3Dcomp.c contain the code for constructing andevaluating interpolating splines in different dimensionalities and forms. Itis apparent from the filenames which file handles which dimensionality andwhich form. These files share the header interp.h.

The files splineexpl.c and splinecomp.c both consist of single functions,with the obvious names splineexpl and splinecomp, respectively. These func-tions solve the linear systems presented in chapter 2, namely eqs. (12), (20)and (22), to produce the coefficients that define the interpolating 1D splinefunction. In other words, this is where the math is hidden. The functionsfeature separate clauses for different boundary conditions of the interpolationproblem. Currently, the functions for both forms support two boundary con-ditions: natural, presented in chapter 2.1, and periodic, presented in chapter2.3.

The .c-files with filenames starting with ”interp”, listed above, all havethe same functions, only adapted to the relevant dimensionality and form.The function names have as prefixes the filenames but are otherwise the same.For example, the function ∗eval f (∗ indicates the existence of a prefix) hasin the file interp2Dcomp.c the full name interp2Dcomp eval f . Functions∗init coeff and ∗init spline initialize the spline, in accordance with AS-

25

COT5 input handling. First, ∗init coeff calls the function in splineexpl.cor splinecomp.c to calculate the defining coefficients and places them in tem-porary storage. In the case of a multidimensional spline, the process con-sists in solving iteratively a number of 1D splines, recall chapters 2.4.2 and2.4.3. Once ∗init coeff has produced the necessary coefficients, ∗init splinestores the coefficients and other spline-defining data in a dedicated spline datastruct, which has a custom data type defined in interp.h, e.g., interp2D data.The functions ∗eval f and ∗eval df find the grid cell containing the point ofinterpolation and evaluate the spline function at the normalized coordinates.The former evaluates only the function value, while the latter evaluates alsoderivatives.

3.2 Interface

The interface between the ASCOT5 code and its spline interpolation libraryare calls to the functions described in chapter 3.1. An ASCOT5 executioncan be roughly divided into two stages, initialization and simulation. Thisis reflected also in its use of the spline library. During initialization, all thenecessary splines are pre-calculated for later use in the simulation stage.

As the first step of initialization, ASCOT5 calls ∗init coeff for all ofthe input data sets that it will need to interpolate. For example, for amagnetic field component that depends on two variables, when the compactspline form is used, ASCOT5 would call interp2Dcomp init coeff . Due toits significant memory savings, the compact form spline is the default inASCOT5. The function ∗init coeff takes as parameters the discrete inputdata, i.e., domain edges, number of grid points in each variable, and the fieldvalues. Furthermore, the wanted boundary conditions are communicatedwith flag parameters, and a pointer parameter points to a pre-allocated arraywith room for all the defining coefficients to be solved. The domain edges,number of grid points in each variable, and the boundary conditions uniquelydefine a uniform rectilinear grid, to which the current version of the code islimited. The result of an ∗init coeff function call is a pre-allocated arrayfilled with the defining coefficients for a spline. As the second step of theinitialization stage, for each of the input data sets, ASCOT5 initializes thespline data struct and calls ∗init spline, which fills the struct with the spline-defining data.

After initialization, ASCOT5 proceeds to simulate test particles by solv-ing their time-evolution numerically, one time step at a time. The values of

26

field quantities in the environment of the test particle must be evaluated atevery time step. For this, ASCOT5 calls the function ∗eval f , or ∗eval dfif derivatives are also wanted, to interpolate field values at the current testparticle location. The parameters are: a pre-allocated pointer to where theevaluated value or values will be placed, the spline data struct, which con-tains all the spline-defining data, and the coordinates of the test particlelocation. As the result of such a function call, the evaluated spline value,and in the case of ∗eval df also derivatives, can be found behind the pre-allocated pointer.

3.3 Further development

The spline library is functional and can be used for interpolation in ASCOT5and, possibly, other computer codes. However, it has a number of limitations,and there is room to improve and add new capabilities. Areas that canbe developed are: non-uniform grid spacing, available boundary conditions,possible dimensionality and computational performance.

The current version of the code only supports data sets with uniform gridspacing in each dimension. An obvious improvement would be the possibilityof non-uniform grid spacing. It would allow more flexibility in the choice ofASCOT5 inputs, and enable localized optimization of grid resolutions.

The boundary conditions currently available are: setting second deriva-tives to zero at the edges, which yields the so-called natural spline, and theperiodic boundary condition. There are many more possible boundary con-ditions that could be added. Some are mentioned in chapter 2.3. A largerarsenal of boundary conditions means a better chance to find ones that fit theinput data. This could help in avoiding edge effects, which might interferewith convergence and are investigated in chapter 4.1.2.

A possible subject of interest is time-dependent ASCOT5 simulation.This would mean that some field quantities in the virtual environment of thetest particles change in time. For example, there might be a time-dependentelectric field. This adds one more dimension to the interpolation problem. Ifthe field is already 3D in real space, this would make the time-dependent field4D. Therefore, 4D interpolation might become necessary. Theoretically, cu-bic splines extend readily to higher dimensions [5, 9], making this a possibleextension of the capabilities of the code.

Finally, while the interpolation library was written with computationalefficiency in mind, the developers lack professional skills in code optimiza-

27

tion. There are likely ways in which the code could be rewritten to reducecomputation time. By utilizing the compact form of cubic splines, memoryefficiency has been optimized to some extent. However, further improvementmight be possible also in that regard.

28

4 Testing

The interpolation code was tested on the convergence of an approximationand the difference in computational performance between the explicit andcompact forms. The convergence tests aim to verify the code by showingtheoretical convergence rates. The comparison between forms aims to givesome idea of how much, if any, computational performance is sacrificed whenexploiting the memory savings of the compact form.

4.1 Convergence

As established in chapter 2.6 for an analytical function generating discretedata grids, the approximating cubic spline should converge as O(h4), recalleqs. (33). In this chapter, the convergence properties of the written cubicspline code are put to the test, first under simple and optimal conditions,then in a scenario more realistic for the ASCOT5 physics code.

4.1.1 Unit tests

A unit test program, test spline.c, was written that calls the spline interpo-lation library to test convergence. It generates from an analytical functiona uniform grid of discrete values. The interpolation library is applied tothis test data to construct an interpolating spline, which approximates theoriginal analytical function. A large number Nrnd of random points in thedomain are drawn. In these points, the approximative interpolant and itsderivatives are evaluated using the spline. The exact reference values arecalculated using the analytical expressions of the original function and itsderivative functions. The error of the spline approximation is defined as themean error

err =

∑Nrnd

i=1 |fanl − fspl|Nrnd

, (34)

where fanl and fspl are the analytical and spline function values, respectively,and the index i = 1, ..., Nrnd, i.e., runs over all the random points. This pro-cess is in the test program performed for dimensionalities 1–3D, using trigono-metric functions and the natural and periodic boundary conditions. Further-more, the process is repeated for grids of differing resolution, i.e., differentconstant grid spacing h = (xmax − xmin)/(N − 1) (or h = (xmax − xmin)/Nwith the periodic boundary condition), with example interval endpoints xmin

29

and xmax, and the number of grid points N . Note that with this definitionN is one larger in this chapter than it was in chapter 2. To avoid edge ef-fects, briefly explained in chapter 2.6, the analytical functions and boundaryconditions are chosen to match.

The test cases reported here were prepared as follows. In the 1D case, theanalytical function sin(x) was used in the domain [0, 2π]. In the 2D case, theanalytical function sin(x)sin(y) was used in the domain [0, 2π]× [0, 2π]. Inthe 3D case, the analytical function sin(x)sin(y)sin(z) was used, in the do-main [0, 2π]×[0, 2π]×[0, 2π]. These trigonometric functions are well suited forthis test, since their second partial derivatives with respect to each variableare zero and they are periodic at the domain edges, thus perfectly match-ing both the natural and periodic boundary conditions used. This ensuresthat convergence-spoiling edge effects do not occur. The periodic boundarycondition was used in the y-direction in the 3D case. The natural boundarycondition was used everywhere else. The number of grid points and gridspacing was set to be the same in all variables, i.e., N = Nx = Ny = Nz

and h = hx = hy = hz, to simplify studying the error scaling. However,there is one exception to this, hy in the 3D case. Since the y-coordinatethere has the periodic boundary condition, it has one more subinterval thanthe other coordinates, which have the natural boundary condition. Hence,hy > h = hx = hz in the 3D case. The relative significance of this decreasesquickly with increasing N , and it should not distort the results much. Finally,the number of error sample points Nrnd was set to one million.

Figures 1 – 3 show the decay of the mean error (eq. (34)) with the de-crease of the grid spacing h, for the cases described above for dimensionalities1–3D, respectively. Values used for the number of grid points along each co-ordinate were N = 8, 16, 32, 64 and 128. The numerical convergence rates ofthe cubic spline approximations agree well with the theoretical expectations(eqs. (33)): O(h4) for the function value, O(h3) for the first order deriva-tives, and O(h2) for the second order derivatives. However, as the observantreader might notice in the 3D case, shown in Fig. 3, the convergences of thefunctions that are derivatives, to some order, with respect to y differ from theconvergences of the other functions of corresponding order of differentiation.This is likely explained by the above mentioned slight distortion hy > h,which is caused by the periodic boundary condition, and is indeed relativelymore significant for high h, or low N . It is expected that the errors of thederivatives with respect to y start out as looking smaller than those of theothers, since the true resolution of the y-coordinate is slightly finer than the

30

reference resolution, i.e., hy > h.Note that the order of the derivative here refers to how many times

the function has been differentiated with respect to each variable. Cross-derivatives that have been differentiated once per variable, for example, oncewith respect to x and once with respect to y, count as first order deriva-tives, and converge accordingly. In other words, taking partial derivatives offirst order with respect to other variables after the first one does not furtherreduce the exponent of h in the tendency of the error to zero. However,the error might change by a factor. Looking closely at the multidimensionalcases, Figs. 2 and 3, this can be observed for all cross-derivatives. Theirerrors are systematically larger than those of the first order derivatives takenwith respect to only one variable in the same case. This indicates that takingthe partial derivative with respect to a second variable increases the error bysome factor.

10-1

h

10-8

10-6

10-4

10-2

err

or

f

fx

fxx

~ h4

~ h3

~ h2

Figure 1: Mean errors of the cubic spline approximation of sin(x) (f), in thedomain [0, 2π], and its first (fx) and second (fxx) derivatives as functions ofthe grid spacing h = hx. The natural spline was used. Suitably normalizedreferences for h-scaling are included in grey to improve readability.

31

10 -1

h

10 -8

10 -6

10 -4

10 -2err

or f

fx

fy

fxx

fyy

fxy

~ h4

~ h3

~ h2

Figure 2: Mean errors of the bicubic spline approximation of sin(x)sin(y),in the domain [0, 2π] × [0, 2π], and a number of its derivatives as functionsof the grid spacing h = hx = hy. The natural boundary condition was usedeverywhere. Suitably normalized references for h-scaling are included in greyto improve readability.

32

10 -1

h

10 -8

10 -7

10 -6

10 -5

10 -4

10 -3

err

or

f

fx

fy

fz

fxx

fyy

fzz

fxy

fxz

fyz

~ h4

~ h3

~ h2

Figure 3: Mean errors of the tricubic spline approximation ofsin(x)sin(y)sin(z), in the domain [0, 2π] × [0, 2π] × [0, 2π], and a numberof its derivatives as functions of the grid spacing h = hx = hz ? hy. Thenatural boundary condition was used for x and z, and the periodic for y.Suitably normalized references for h-scaling are included in grey to improvereadability.

The results shown in Figs. 1 - 3 are from tests run using the default,compact form. The same tests were run also using the explicit form, andwith different combinations of the two boundary conditions, always yieldingsimilar convergence rates that conform to theory. This was expected sincethe explicit and compact forms are equivalent, as was proved for the 1D casein chapter 2.2.

The results presented here verify that the developed cubic spline inter-polation code achieves theoretical convergence rates. We conclude that thecode library has been correctly implemented. However, these convergence re-sults are limited to the optimal case, where boundary conditions are knownto match the underlying analytical function being approximated.

33

4.1.2 ASCOT test case

In research work done with ASCOT, it is generally not the case that theinput data matches perfectly available boundary conditions, as it did inchapter 4.1.1. This chapter investigates the convergence in a more realisticcase, where the geometry is non-trivial and the boundary conditions mightonly approximate the true nature of the data at the edge. The focus is onconvergence-ruining edge effects caused by mismatch between the boundaryconditions used and the input data.

The ASCOT5 code includes a feature that gathers internal functions intoa library, usable through a python interface. There is also a built-in an-alytical model for torus-shaped magnetic fields, comparable to those in anexperimental fusion device. The model consists of a number of componentscalar fields that together make up the magnetic field. The component fieldsare given in cylindrical coordinates (R, φ, z), on a domain shaped as a toruswith a rectangular cross-section. The library functions were used to constructspline approximations of component fields of the analytical model, and theirconvergence was studied. A more detailed explanation follows.

Python scripts, which call the ASCOT5 library functions, were written toautomate the testing scheme employed here. First, a number of parametersare chosen to define the domain and characteristics of the analytical model.Then an ASCOT5 library function is called upon to generate the spline ap-proximation of the model. The library does this internally, but follows thesame recipe as the unit test program in chapter 4.1.1. Discretized versionsof the analytical component fields are interpolated, yielding splines that ap-proximate the original function. Since the field has the shape of a torus, theperiodic boundary condition is used along φ, while the other two variablesget the natural boundary condition.

Ten thousand random points in the domain are drawn for error estima-tion. However, an extra maneuver was necessary in this part of the test.The subdomain within one fourth of the minor radius from the axis insidethe torus was excluded from error sampling, because the analytical modelbehaves poorly close to the axis. With the error points drawn, the error ofthe approximation is calculated, in accordance with eq. 34, as the averagedifference between the spline and analytical functions in these points.

The testing scheme repeats the described process for a range of differentgrid spacings to find the error scaling. Unlike in chapter 4.1.1, this case doesnot feature a simple Cartesian coordinate system, where all the axes have

34

the same length. The domain dimensions were chosen to reflect a real fusiondevice, and generally hR 6= hφ 6= hz. However, to retain the ability to trackerror-scaling as a function of a single grid interval length, hR was chosen asthe standard, and hφ and hz were scaled to as close to this as possible, whilestill having integer numbers of grid points. Simply put, hR ≈ hφ ≈ hz.

Of the several scalar functions that make up the magnetic field model,two were tested here, the component Bφ in the direction along the axis insidethe torus, and the component BR in the direction of the major radius of thetorus. The Bφ component has its own original analytical function. The BR

component, on the other hand, is calculated from the spline interpolation ofthe magnetic flux ψ through differentiation, according to the relation [11]

BR = − 1

R

∂ψ

∂z.

This supplies us with a convenient way to also look at derivative convergence.The analytical function for Bφ is exactly periodic, making the boundary

condition along φ perfectly matching. Further, ψ, and by extension BR,is constant along φ, making its function 2D. Hence, optimal convergence isexpected with regard to the φ-coordinate. However, the natural boundarycondition used for the other coordinates has no reason to match the functionsexactly, and edge effects on the convergence are expected from the edgeregions of R and z, i.e., the outer edge of the rectangular torus.

The same test was run twice for each component field, once evaluatingthe error at random points from the whole domain (still omitting the centralregion as explained above), and once only from a sub-domain reaching nocloser than one fourth of the minor radius from all edges of the domain. Thelatter is dubbed the far-from-edge (ffe) case. The goal was to find out ifconvergence-ruining edge effects can be avoided by staying away from edgeswith boundary condition mismatch.

Figure 4 shows the mean errors (eq. (34)) from the different componentsand cases as functions of hR. When evaluating in the whole domain, Bφ

and BR exhibit errors that do not tend to zero with the theoretical ratesh4 and h3, respectively. However, when we evaluate only far-from-edge val-ues, the convergences reach the theoretical rates. For relatively large hR,the convergences even exceed the theoretical rates, particularly much in thecase of the derivative function BR. This could be explained by the originalfunction being simple or exceptionally suitable for cubic spline interpolation.Regardless, for small hR, the convergences settle at the theoretical rates. The

35

important result is that for far-from-edge evaluation the convergence rate isnever worse than what is promised by theory in the absence of edge-effects.

10-1

hR

10-8

10-6

10-4

10-2

err

or

B

ffe B

Br

ffe Br

~ hR

4

~ hR

3

Figure 4: Mean error of the cubic spline approximation of realistic magneticfield components Bφ and BR. The latter is calculated as the first orderderivative of the magnetic flux ψ. Compared are the errors from the fulldomain and far-from-edge (ffe) cases. Suitably normalized references for h-scaling are included in grey to improve readability.

In summary, optimal theoretical convergence is reached only far from theedges of the rectangular torus. We can conclude that the interpolating splineindeed suffers edge-effects close to edges where the boundary conditions donot match the data, which prevents optimal convergence of the approxima-tion. Such risk of inaccuracy can be avoided by not evaluating the functionclose to the edge. This is often possible in ASCOT, since the input fielddata can and typically does extend beyond the space that will actually bereachable by the test particles.

36

4.2 Computational performance

The two different forms for cubic splines are mathematically equivalent, butthe defining coefficients, their calculation and the expressions for functionevaluation all differ, as thoroughly explained in chapter 2. Therefore, alsothe computational performance can be different. The memory savings of thecompact form, in comparison to the explicit form, is straight-forward. In1D, the compact form requires two coefficients per knot, instead of four inthe explicit form. In 2D, the amounts are four and 16, and in 3D, eight and64, respectively. This means that the memory savings are two-, four- andeight-fold, respectively in dimensionalities one, two and three (actually, thisis generally only approximately true, since the compact form requires thecoefficients for the knots at the ending edges for any boundary conditions).The compact form clearly offers a substantial advantage in memory usage,especially in higher dimensions. Is there a trade-off? This question brings usto the other main computational resource, CPU time. Unlike memory usage,calculation of CPU time usage is not straight-forward for this code.

The CPU time usage of the spline interpolation code was tested compu-tationally to investigate differences between the explicit and compact forms.Library functions of the C programming language were used to measure theCPU time taken for different operations. This gauge was inserted in theconvergence-testing program described in chapter 4.1.1. A single run of thetest program measures separately the CPU time for initialization and evalu-ation of splines for dimensionalities 1–3D, for five different grid resolutions.The separate treatment of initialization and evaluation of the splines cor-responds to the two different stages of an ASCOT5 run, initialization andsimulation, which were explained in chapter 3.2. To make the scaling of CPUtime with grid resolution straight-forward, the number of grid points waskept equal between variables, as in chapter 4.1.1, i.e., Nx = Ny = Nz = N .For each resolution and each dimensionality, the spline was initialized andevaluated in one million pseudo random points, and the CPU times weremeasured. The evaluation was done both using the function ∗eval f , whichonly evaluates the spline value, and using the function ∗eval df , which eval-uates the spline value and its derivatives. The CPU time for evaluation wascumulated across functions and points. The same test was run for both theexplicit and compact forms.

The linear systems solved in spline construction are similar for the explicitand compact forms (recall chapters 2.1 and 2.2, especially eqs. (12) and (20)).

37

However, fewer coefficients are needed and their derivation from the solutionof the linear system is simpler in the compact form than in the explicit.Therefore, the compact splines are expected to initialize faster.

Recall next the explicit and compact evaluation formulas, eqs. (2) and(14) for 1D, eqs. (26) and (29) for 2D, and appendix A for 3D. The compactformulas use coefficients from all corners of the relevant grid cell. Therefore,the number of coefficients to fetch from memory for evaluation is the same asfor the explicit form, despite the total number of spline-defining coefficientsbeing only a fraction. Since all the coefficients needed in explicit evaluationcorrespond to a single knot, whereas compact evaluation requires coefficientsfrom several knots, one could imagine that the process of fetching the coeffi-cients from memory is faster for the explicit form. However, without intimateknowledge of how memory works in the used computer, we cannot say forcertain. Something we can say for certain is that the explicit formulas involvefewer mathematical operations. Hence, the explicit splines are expected toevaluate faster.

The performance test was run on one core of a standard desktop com-puter. The data reported here is from a single test program run. However,several runs were performed, and the results differed only by statistical fluc-tuations.

In Fig. 5, the measured CPU times for explicit and compact spline initial-ization are plotted as a function of N , the number of grid points along onecoordinate. Higher dimensions mean more grid points and spline-definingcoefficients, which requires more computation time. Beyond helping confirmthat trivial result, the 1D data is useless. We believe that the CPU time forthe 1D operations tested here is too short to get a good measurement. Wefocus on the 2D and 3D cases. Regarding the different forms separately first,we see that the CPU time scales quite accurately as O(N2) in the 2D case andas O(N3) in the 3D case. This is expected, since the number of coefficientsto initialize grows approximately as Nx ·Ny = N2 and Nx ·Ny ·Nz = N3, re-spectively. (Only approximately because the explicit form in the case of thenatural boundary condition does not require the polynomial coefficients tobe solved for the domain-ending knot.) Comparing the two forms, the com-pact initialization is faster, as was predicted. For this data, the N -averagedspeed-up factors, i.e., ratios between the longer (explicit) and shorter (com-pact) CPU times averaged over all values of N , are approximately 2.8 in 2Dand 5.1 in 3D. These ratios are not as high as the ratios between the num-

38

bers of needed coefficients, which are approximately 16/4 = 4 and 64/8 = 8,respectively.

10 1 10 2

N

10 -5

10 -4

10 -3

10 -2

10 -1

10 0

CP

U tim

e

expl 1D

comp 1D

expl 2D

comp 2D

expl 3D

comp 3D

~ N2

~ N3

Figure 5: CPU time for spline initialization for the explicit and compactforms as a function of N , the number of grid points along one coordinate.Suitably normalized references for N -scaling are included in grey to improvereadability.

Figure 6 shows the measured CPU times for spline evaluation in onemillion points as a function of N . Time usage again increases with dimen-sionality, explained by the increasing number of coordinates and operationsinvolved in the evaluation formulas. Little N -dependence is observed. Forthis data, the 3D cases show a slight increase in CPU time with increasingN . This might suggest that fetching coefficients from memory becomes moretime-consuming as the number of grid points grows. However, this data isinsufficient for conclusions about that. Comparing the two spline forms, thecompact evaluations are faithfully slower, as was predicted. For this data,the N -averaged slow-down factors are approximately 1.05 in 1D, 1.15 in 2Dand 1.28 in 3D. The last factor is in the same ballpark as the slow-downfactor reported for tricubic Princeton splines, a factor of 1.4 [2].

39

0 20 40 60 80 100 120 140

N

0.3

0.4

0.5

0.6

0.7

0.8

0.9

1

1.1

CP

U tim

e

expl 1D

comp 1D

expl 2D

comp 2D

expl 3D

comp 3D

Figure 6: CPU time for spline evaluation in one million points as a functionof N , the number of grid points along one coordinate. Compared are theexplicit and compact forms, each in three dimensionalities.

More accurate estimates of the CPU time usage could likely be measuredwith a more carefully performed test, where the dedication of the computerresources to the relevant task is ensured. Furthermore, the algorithms inthe spline library have not been optimized by professionals. The resultsof this test, which is meant to measure the efficiencies of the alternativemathematical forms of the cubic spline, are likely affected by imperfectionsin the code.

To summarize the results, compact splines initialize faster but evaluateslower. Since initialization occurs only once at the beginning of an ASCOT5run, whereas evaluation is done repeatedly for the entire duration of simula-tion, the slow-down in evaluation is the more significant result. We concludethat the trade-off for the substantial memory savings of the compact formis a modest increase in the CPU time needed for evaluation. The crude testperformed here estimates an increase of 5− 28% in CPU time usage, wherethe increase grows with dimensionality.

40

5 Summary and future

The particle-following code ASCOT, which models minority particles in mag-netic confinement fusion devices, has been rewritten in the C programminglanguage to take advantage of parallelization capabilities of modern super-computer architectures. The simulation code needs to interpolate magneticfield data and other quantities. For this purpose, a cubic spline interpolationlibrary was written in C for the new ASCOT version, ASCOT5. Multidimen-sional interpolation is supported, there are two boundary condition options,and two alternative spline forms are implemented. The so-called compactform offers substantial memory savings.

Testing confirmed theoretical convergence rates of cubic spline approxi-mations, verifying the code. Mismatching boundary conditions were foundto prevent optimal convergence. However, these edge effects can be avoidedby not evaluating the field close to the domain edge. The CPU time usagewas measured to identify the trade-off for the memory savings of the com-pact form. A modest increase of 5− 28% in the computation time for splineevaluation was found.

The interpolation library has room for further development. Support fornon-uniform data grids would make the choice of input less limited and allowlocally optimized grid resolutions. Implementing new boundary conditionscould help in avoiding edge effects that impair convergence, because it wouldincrease the likelihood of finding boundary conditions that match the inputdata. The number of supported dimensions could be further increased, forexample to 4D to allow the simulation of time-dependent problems in 3D realspace. Finally, the code could likely be optimized to use computer resourcesmore efficiently.

41

6 References

[1] E. Hirvijoki, O. Asunta, T. Koskela, T. Kurki-Suonio, J. Miettunen, S.Sipila, A. Snicker, S. Akaslompolo. ASCOT: Solving the kinetic equa-tion of minority particle species in tokamak plasmas. Computer PhysicsCommunications, 1;185(4):1310-21, 2014.

[2] Princeton Plasma Physics Laboratory. PSPLINE Module: PrincetonSpline and Hermite Cubic Interpolation Routines. Retrieved January 11,2019, from https://w3.pppl.gov/ntcc/PSPLINE/.

[3] J. Varje, K. Sarkimaki, J. Kontula, P. Ollus, T. Kurki-Suonio, A. Snicker,E. Hirvijoki, S. Akaslompolo. High-performance orbit-following code AS-COT5 for Monte Carlo simulations in fusion plasmas. arXiv preprintarXiv:1908.02482, 2019.

[4] C. de Boor. A Practical Guide to Splines. New York: Springer-Verlag,1978.

[5] J. Ahlberg, E. Nilson, J. Walsh. The Theory of Splines and Their Appli-cations. Academic Press, 1967.

[6] G. Forsythe, M. Malcolm, C. Moler. Computer Methods for MathematicalComputations. Prentice-Hall Series in Automatic Computation, 1977.

[7] R. Bartels, J. Beatty, B. Barsky. An Introduction to Splines for use inComputer Graphics and Geometric Modeling. Morgan Kaufmann, 1987.

[8] W. Press, S. Teukolsky, W. Vetterling, B. Flannery. Numerical Recipesin Fortran 77: The Art of Scientific Computing, 2nd ed. Cambridge Uni-versity Press, 1993.

[9] L.L. Schumaker. Spline Functions: Computational Methods. Society forIndustrial and Applied Mathematics, 2015.

[10] M.J.D. Powell. The uniform convergence of thin plate spline interpola-tion in two dimensions. Numerische Mathematik, 1;68(1):107-28, 1994.

[11] J. Wesson. Tokamaks, 3rd ed. Oxford University Press, 2004.

42

Appendix A Tricubic spline evaluation

This appendix presents the explicit and compact form evaluation formulas forthe tricubic spline function in the grid cell corresponding to the coordinateindices i, j and k. Both expressions use the normalized variables defined ineqs. (30).

The explicit expression features besides normalized variables also thepolynomial coefficients C lmn

ijk . It reads

sijk(x, y, z) = C000ijk + xC100

ijk + x2C200ijk + x3C300

ijk

+y(C010ijk + xC110

ijk + x2C210ijk + x3C310

ijk )

+y2(C020ijk + xC120

ijk + x2C220ijk + x3C320

ijk )

+y3(C030ijk + xC130

ijk + x2C230ijk + x3C330

ijk )

+z(C001ijk + xC101

ijk + x2C201ijk + x3C301

ijk

+y(C011ijk + xC111

ijk + x2C211ijk + x3C311

ijk )

+y2(C021ijk + xC121

ijk + x2C221ijk + x3C321

ijk )

+y3(C031ijk + xC131

ijk + x2C231ijk + x3C331

ijk ))

+z2(C002ijk + xC102

ijk + x2C202ijk + x3C302

ijk

+y(C012ijk + xC112

ijk + x2C212ijk + x3C312

ijk )

+y2(C022ijk + xC122

ijk + x2C222ijk + x3C322

ijk )

+y3(C032ijk + xC132

ijk + x2C232ijk + x3C332

ijk ))

+z3(C003ijk + xC103

ijk + x2C203ijk + x3C303

ijk

+y(C013ijk + xC113

ijk + x2C213ijk + x3C313

ijk )

+y2(C023ijk + xC123

ijk + x2C223ijk + x3C323

ijk )

+y3(C033ijk + xC133

ijk + x2C233ijk + x3C333

ijk )).

The compact expression also features helper variables based on the nor-malized variables. Their definitions are as in eqs. (28), plus the followingsimilar ones for the z-coordinate:

zr = 1− z,zc = z3 − z,zrc = (1− z)3 − (1− z).

43

The coefficients in the compact expression are the function values and allpossible partial derivatives of second order in one or several variables, fromall corners of the grid cell. The notation for these coefficients follows thesame logic as in eq. (29). For example, the second partial derivative of thespline function s with regard to the variable x at (xi, yj, zk) is denoted byfxxi,j,k. The compact spline function expression reads (notice continuation onnext page)

sijk(x, y, z) = zr(xr(yrfi,j,k + yfi,j+1,k)

+x(yrfi+1,j,k + yfi+1,j+1,k)

+z(xr(yrfi,j,k+1 + yfi,j+1,k+1)

+x(yrfi+1,j,k+1 + yfi+1,j+1,k+1))

+h2x,i6

(zr(xrc(yrf

xxi,j,k + yfxxi,j+1,k)

+xc(yrfxxi+1,j,k + yfxxi+1,j+1,k)

)+z(xrc(yrf

xxi,j,k+1 + yfxxi,j+1,k+1)

+xc(yrfxxi+1,j,k+1 + yfxxi+1,j+1,k+1)

))+h2y,j6

(zr(xr(yrcf

yyi,j,k + ycf

yyi,j+1,k)

+x(yrcfyyi+1,j,k + ycf

yyi+1,j+1,k)

)+z(xr(yrcf

yyi,j,k+1 + ycf

yyi,j+1,k+1)

+x(yrcfyyi+1,j,k+1 + ycf

yyi+1,j+1,k+1)

))+h2z,k6

(zrc(xr(yrf

zzi,j,k + yf zzi,j+1,k)

+x(yrfzzi+1,j,k + yf zzi+1,j+1,k)

)+zc(xr(yrf

zzi,j,k+1 + yf zzi,j+1,k+1)

+x(yrfzzi+1,j,k+1 + yf zzi+1,j+1,k+1)

))...

44

...

+h2x,ih

2y,j

36

(zr(xrc(yrcf

xxyyi,j,k + ycf

xxyyi,j+1,k)

+xc(yrcfxxyyi+1,j,k + ycf

xxyyi+1,j+1,k)

)+z(xrc(yrcf

xxyyi,j,k+1 + ycf

xxyyi,j+1,k+1)

+xc(yrcfxxyyi+1,j,k+1 + ycf

xxyyi+1,j+1,k+1)

))+h2x,ih

2z,k

36

(zrc(xrc(yrf

xxzzi,j,k + yfxxzzi,j+1,k)

+xc(yrfxxzzi+1,j,k + yfxxzzi+1,j+1,k)

)+zc(xrc(yrf

xxzzi,j,k+1 + yfxxzzi,j+1,k+1)

+xc(yrfxxzzi+1,j,k+1 + yfxxzzi+1,j+1,k+1)

))+h2y,jh

2z,k

36

(zrc(xr(yrcf

yyzzi,j,k + ycf

yyzzi,j+1,k)

+x(yrcfyyzzi+1,j,k + ycf

yyzzi+1,j+1,k)

)+zc(xr(yrcf

yyzzi,j,k+1 + ycf

yyzzi,j+1,k+1)

+x(yrcfyyzzi+1,j,k+1 + ycf

yyzzi+1,j+1,k+1)

))+h2x,ih

2y,jh

2z,k

216

(zrc(xrc(yrcf

xxyyzzi,j,k + ycf

xxyyzzi,j+1,k )

+xc(yrcfxxyyzzi+1,j,k + ycf

xxyyzzi+1,j+1,k)

)+zc(xrc(yrcf

xxyyzzi,j,k+1 + ycf

xxyyzzi,j+1,k+1)

+xc(yrcfxxyyzzi+1,j,k+1 + ycf

xxyyzzi+1,j+1,k+1)

)).

45