Integration of interaction tensor

Viewed 158

I need to calculate the following integral several thousand times per time step:

enter image description here

where enter image description here and:

enter image description here , and enter image description here

So far I have implemented in Julia as:

using StaticArrays

function interactiontensor(C, a1, a2, a3, ϕ, θ)

n1,n2   = 100,50
T       = fill(0.0,3,3,3,3)
Av      = zeros(4,4)
invAv   = similar(Av)
xi      = Vector{Float64}(undef, 3)


@inbounds  for p ∈ 1:n1        

    sinθp = sind(θ[p])
    cosθp = cosd(θ[p])
    for q ∈ 1:n2

        sinϕq  = sind(ϕ[q])
        cosϕq  = cosd(ϕ[q])            
        
        # -- Director cosines
        xi[1]  = sinθp*cosϕq/a1
        xi[2]  = sinθp*sinϕq/a2
        xi[3]  = cosθp/a3

        Christoffel!(Av,C,xi)
        fillAv!(Av, xi)

        invAv = inv(SMatrix{4,4}(Av))

        tensorT!(T,invAv,xi,sinθp)

        surface += sinθp
    end
end

return T ./= surface
end

@inline function Christoffel!(Av,C,xi)
@inbounds for t ∈ 1:3, r ∈ 1:3
    aux = zero(eltype(C))
    for u ∈ 1:3, s ∈ 1:3
        aux += C[r, s, t, u] * xi[s] * xi[u]        
    end
    Av[r, t] = aux
end
end

@inline function tensorT!(T,invAv,xi,sinθp)
@inbounds for k ∈ 1:3, i ∈ 1:3
    aux = invAv[i, k]
    for l ∈ 1:3, j ∈ 1:3
        T[i, j, k, l] += aux * xi[j] * xi[l] * sinθp
    end
end
end

@inline function fillAv!(Av, xi)
@inbounds for i ∈ 1:3
    xi0      = xi[i]
    Av[i, 4] = xi0
    Av[4, i] = xi0
end
end

with

n1,n2 = 100,100
step    = π/n1
dθ,dϕ   = π/n1, 2π/n2
θ       = rad2deg.(range(dθ, stop = pi,  length = n1))
ϕ       = rad2deg.(range(dϕ, stop = 2pi, length = n2))
C = @SArray rand(3,3,3,3)
@btime interactiontensor($C, $10.0, $5.0, $1.0, $ϕ, $θ);
# 544.795 μs  (4 allocations: 1.08 KiB)

Given the number of times I ideally need to compute this integral, is there any optimization to my implementation, or an alternative approach, to considerably reduce the computational cost?

1 Answers

Here are some suggestions:

  • sinθp, cosθp = sincosd(θ[p]), i.e. computing sine and cosine in one step.
  • Initializing xi = @SVector zeros(3) as a static vector and then use Setfield.jl to assign the values in each iteration, i.e. @set x[1] = sinθp*cosϕq/a1.
  • Load the package LoopVectorization.jl and use the @avx macro (very roughly speaking similar to @simd) to speed up the loops in Christoffel!, tensorT! and fillAv!.

On my machine I find that these changes reduce the computation time by more than a factor of 5 (relative to the original function in the OP). The biggest chunk is due to @avx, the second point above amounts to about ~30%.

julia> @btime interactiontensor_original($C, $10.0, $5.0, $1.0, $ϕ, $θ);
  661.655 μs (5 allocations: 1.28 KiB)

julia> @btime interactiontensor_optimized($C, $10.0, $5.0, $1.0, $ϕ, $θ);
  125.352 μs (4 allocations: 1.17 KiB)

Here the full modified code (note that I commented out the lines involving surface which isn't specified in the OP):

using StaticArrays, Setfield, LoopVectorization

function interactiontensor_optimized(C, a1, a2, a3, ϕ, θ)

    n1,n2   = 100,50
    T       = fill(0.0,3,3,3,3)
    Av      = zeros(4,4)
    invAv   = similar(Av)
    xi      = @SVector zeros(3)


    @inbounds for p ∈ 1:n1

        sinθp, cosθp = sincosd(θ[p])
        for q ∈ 1:n2

            sinϕq, cosϕq  = sincosd(ϕ[q])

            # -- Director cosines
            @set xi[1]  = sinθp*cosϕq/a1
            @set xi[2]  = sinθp*sinϕq/a2
            @set xi[3]  = cosθp/a3

            Christoffel!(Av,C,xi)
            fillAv!(Av, xi)

            invAv = inv(SMatrix{4,4}(Av))

            tensorT!(T,invAv,xi,sinθp)

            # surface += sinθp
        end
    end

    return T #./= surface
end

@inline function Christoffel!(Av,C,xi)
    @avx for t ∈ 1:3, r ∈ 1:3
        aux = zero(eltype(C))
        for u ∈ 1:3, s ∈ 1:3
            aux += C[r, s, t, u] * xi[s] * xi[u]
        end
        Av[r, t] = aux
    end
end

@inline function tensorT!(T,invAv,xi,sinθp)
    @avx for k ∈ 1:3, i ∈ 1:3
        aux = invAv[i, k]
        for l ∈ 1:3, j ∈ 1:3
            T[i, j, k, l] += aux * xi[j] * xi[l] * sinθp
        end
    end
end

@inline function fillAv!(Av, xi)
    @avx for i ∈ 1:3
        xi0      = xi[i]
        Av[i, 4] = xi0
        Av[4, i] = xi0
    end
end
Related