view diffOp.jl @ 88:170e5447bc19 patch_based_test

Reduce allocations
author Vidar Stiernström <vidar.stiernstrom@it.uu.se>
date Fri, 25 Jan 2019 15:10:41 +0100
parents 38733e84ef1a
children c0729ade65da
line wrap: on
line source

abstract type DiffOp end

function apply!(D::DiffOp, u::AbstractVector, v::AbstractVector)
    error("not implemented")
end

function innerProduct(D::DiffOp, u::AbstractVector, v::AbstractVector)::Real
    error("not implemented")
end

function matrixRepresentation(D::DiffOp)
    error("not implemented")
end

function boundaryCondition(D::DiffOp,b::Grid.BoundaryId,type)::(Closure, Penalty)
    error("not implemented")
end

function interface(Du::DiffOp, Dv::DiffOp, b::Grid.BoundaryId; type)
    error("not implemented")
end

abstract type Closure end

function apply(c::Closure, v::AbstractVector, i::Int)
    error("not implemented")
end

abstract type Penalty end

function apply(c::Penalty, g, i::Int)
    error("not implemented")
end

# Differential operator for a*d^2/dx^2
struct Laplace{Dim, T<:Real} <: DiffOp
    grid::Grid.EquidistantGrid{Dim,T}
    a::T
    op::D2{Float64}
end

# u = L*v
function apply!(L::Laplace{1}, u::AbstractVector, v::AbstractVector)
    h = Grid.spacings(L.grid)[1]
    apply!(L.op, u, v, h)
    u .= L.a * u
    return nothing
end

# u = L*v
using UnsafeArrays
@inline function apply!(L::Laplace{2}, u::AbstractVector, v::AbstractVector)
    fill!(u,0)
    h = Grid.spacings(L.grid)

    li = LinearIndices(L.grid.numberOfPointsPerDim)
    n_x, n_y = L.grid.numberOfPointsPerDim


    # For each x
    temp = zeros(eltype(u), n_y)
    for i ∈ 1:n_x
        @inbounds indices = uview(li,i,:)
        @inbounds apply!(L.op, temp, uview(v, indices), h[2])
        for i ∈ eachindex(indices)
            @inbounds u[indices[i]] += temp[i]
        end
    end

    # For each y
    temp = zeros(eltype(u), n_x)
    for i ∈ 1:n_y
        @inbounds indices = uview(li,:,i)
        @inbounds apply!(L.op, temp, uview(v, indices), h[1])
        for i ∈ eachindex(indices)
            @inbounds u[indices[i]] += temp[i]
        end
    end

    for i ∈ eachindex(u)
        @inbounds u[i] = L.a*u[i]
    end

    return nothing
end