Skip to content

Converting the to Fortran array order

Tim Spain edited this page May 2, 2023 · 1 revision

Following the discussion on issue 275, the decision has been made to move to Fortran array ordering for the thermodynamics, specifically the () operator in ModelArray and ModelArrayRef.

Background

ModelArray

The ModelArray class was designed to implement multi-dimensional indexing similarly to a C/C++ n-dimensional array. In these arrays, the final index varies fastest as successive elements are accessed and the first index varies slowest. To mimic this, when a ModelArrayis indexed as array(i, j, k), increasing k by 1 will advance to the next element in memory. Alternatively, if array[n] and array(i, j, k) refer the same one element, then array[n+1] and array(i, j, k+1) will the same element, consecutive to the former. Plotting the elements of the array starting in the bottom left corner, y will vary fastest.

DGVector

For the most part dgVector acts as a 1D array, with known connectivity to the other elements in the 1D array to calculate fluxes, gradients &c. The primary place that it acts as a 2D array is upon construction from the source .smesh file. This initializes the coordinates of the points within the dgVector. The coordinate data is presented as a n x 2 array, with a pair of coordinates on each line. By default, the coordinates are ordered such that the first (x) coordinate varies fastest, incrementing the y coordinate after each row is filled.

Fortran versus C ordering

The terms row-major and column major are confusing. Hereunder I will refer to the two possibilities as last-fastest for C array ordering and first-fastest for Fortran array ordering. If all elements of a 2-dimensional array are to be accessed sequentially as array(i, j) then in C/C++ the nested for loops are written as

for (auto i = 0; i < n; ++i) {
    for (auto j = 0; j < m; ++j) {
        array(i, j);
    }
}

and in Fortran the nested DO loops are written as

DO j = 0, m-1
    DO i = 0, n-1
        array(i, j)
    ENDDO
ENDDO

Justification

Having a consistent array order throughout nextSIM-DG is imperative. The decision to switch the order in the ModelArray class, as opposed to dgVectorwas based on several grounds:

  • The dynamics are much more dependent on spatial information and on that information being correct. It is therefore probably a less invasive and less risky choice to change the ModelArray code.