Mercurial > repos > public > sbplib_julia
view diffOp.jl @ 116:cfe7d091aca2 cell_based_test
Close head
author | Jonatan Werpers <jonatan@werpers.com> |
---|---|
date | Fri, 08 Feb 2019 23:57:27 +0100 |
parents | 17b971a0b852 |
children |
line wrap: on
line source
abstract type DiffOp end function apply(D::DiffOp, v::AbstractVector, i::Int) 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 # DiffOp must have a grid!!! function apply!(D::DiffOp, u::AbstractVector, v::AbstractVector) li = LinearIndices(D.grid.numberOfPointsPerDim) Nx, Ny = D.grid.numberOfPointsPerDim h = Grid.spacings(D.grid) li = LinearIndices(D.grid.numberOfPointsPerDim) ci = CartesianIndices(D.grid.numberOfPointsPerDim) is = D.op.innerStencil for i ∈ view(li, 5:Nx-4, 5:Ny-4) I = ci[i] u[i] = zero(eltype(v)) for j ∈ is.range[1]:is.range[2] u[i] += is[j]*v[(I[2]-1)*Nx - 1 + I[1]+j]/h[1]^2 end for j ∈ is.range[1]:is.range[2] u[i] += is[j]*v[(I[2]-1)*Nx - 1 + I[1]+ j*Nx]/h[2]^2 end end return nothing end function apply(D::DiffOp, v::AbstractVector)::AbstractVector u = zeros(eltype(v), size(v)) apply!(D,v,u) return u end # Differential operator for a*d^2/dx^2 struct Laplace1D <: DiffOp grid::Grid.EquidistantGrid a::Real op::D2{Float64} end # u = L*v function apply(L::Laplace1D, v::AbstractVector, i::Int) h = Grid.spacings(L.grid)[1] uᵢ = L.a * apply(L.op, h, v, i) return uᵢ end # Differential operator for a*d^2/dx^2 + a*d^2/dy^2 struct Laplace2D <: DiffOp grid::Grid.EquidistantGrid a::Real op::D2{Float64} end # u = L*v function apply(L::Laplace2D, v::AbstractVector, i::Int) h = Grid.spacings(L.grid) li = LinearIndices(L.grid.numberOfPointsPerDim) ci = CartesianIndices(L.grid.numberOfPointsPerDim) I = ci[i] uᵢ = apply(L.op.innerStencil, view(v, li[:,I[2]]), I[1])/h[1]^2 uᵢ += apply(L.op.innerStencil, view(v, li[I[1],:]), I[2])/h[2]^2 return uᵢ end