view diffOp.jl @ 55:c62ea0112d4d

Add abstract types for Closure and Penalty
author Ylva Rydin <ylva.rydin@telia.com>
date Tue, 15 Jan 2019 10:25:56 +0100
parents 4300a3fbd818
children 27a8d3021a1c 8a7a537f54e5 d04569696918
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 Laplace1D <: DiffOp
    grid
    a
    op
end

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


# Differential operator for a*d^2/dx^2 + a*d^2/dy^2
struct Laplace2D <: DiffOp
    grid
    a
    op
end

# u = L*v
function apply!(L::Laplace2D, u::AbstractVector, v::AbstractVector)
    u .= 0*u
    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

        v_i = view(v, li[i,:])
        apply!(L.op, temp, v_i, h[2])

        u[li[i,:]] += temp
    end

    # For each y
    temp = zeros(eltype(u), n_x)
    for i ∈ 1:n_y
        v_i = view(v, li[:,i])
        apply!(L.op, temp, v_i, h[1])

        u[li[:,i]] += temp
    end

    u .= L.a*u

    return nothing
end