SUBROUTINE FillUpCache() ! Disperse the (pre-calculated) <ik|u|jk> integrals throughout the cache. ! The cache consists of an unordered set (in the standard UMatCache ! sense) of labels and elements. ! We must order this, and then distribute the elements throughout each set of SLOTS. INTEGER I, J, K, N, nK DO I = 1, nPairs ! Find the last value in the cache CALL BinarySearch(nPairs + 1, UMatLabels(1:nSlots, I), 1, nSlots, N, J, K) N = J - 1 ! N is now the last element and thus number of elements. ! Sort according to label call sort(UMatLabels(1:N, i), UMatCacheData(:, 1:N, i)) K = nSlots ! Now copy element among the whole array for this, from the end. ! (Multiple copies of the same integral in an unfilled cache make adding ! elements ! substantially faster.) DO J = N, 1, -1 nK = (nSlots * (J - 1)) / N + 1 UMatLabels(nK:K, I) = UMatLabels(J, I) DO K = K, nK, -1 UMatCacheData(:, K, I) = UMatCacheData(:, J, I) end do K = nK end do end do END SUBROUTINE FillUpCache