comparison diffOp.jl @ 95:9d53ecca34f7 cell_based_test

Switch to using cartesian indicies
author Jonatan Werpers <jonatan@werpers.com>
date Mon, 04 Feb 2019 17:51:36 +0100
parents 8d505e9bc715
children 6b6d680f2e25
comparison
equal deleted inserted replaced
85:8d505e9bc715 95:9d53ecca34f7
1 abstract type DiffOp end 1 abstract type DiffOp end
2 2
3 # TBD: The "error("not implemented")" thing seems to be hiding good error information. How to fix that? Different way of saying that these should be implemented?
3 function apply(D::DiffOp, v::AbstractVector, i::Int) 4 function apply(D::DiffOp, v::AbstractVector, i::Int)
4 error("not implemented") 5 error("not implemented")
5 end 6 end
6 7
7 function innerProduct(D::DiffOp, u::AbstractVector, v::AbstractVector)::Real 8 function innerProduct(D::DiffOp, u::AbstractVector, v::AbstractVector)::Real
30 31
31 function apply(c::Penalty, g, i::Int) 32 function apply(c::Penalty, g, i::Int)
32 error("not implemented") 33 error("not implemented")
33 end 34 end
34 35
35 # DiffOp must have a grid!!! 36 abstract type DiffOpCartesian{Dim} <: DiffOp end
36 function apply!(D::DiffOp, u::AbstractVector, v::AbstractVector) 37
37 for i ∈ 1:Grid.numberOfPoints(D.grid) 38 # DiffOp must have a grid of dimension Dim!!!
38 u[i] = apply(D, v, i) 39 function apply!(D::DiffOpCartesian{Dim}, u::AbstractArray{T,Dim}, v::AbstractArray{T,Dim}) where {T,Dim}
40 for I ∈ eachindex(D.grid)
41 u[I] = apply(D, v, I)
39 end 42 end
40 43
41 return nothing 44 return nothing
42 end 45 end
43 46
45 u = zeros(eltype(v), size(v)) 48 u = zeros(eltype(v), size(v))
46 apply!(D,v,u) 49 apply!(D,v,u)
47 return u 50 return u
48 end 51 end
49 52
50 struct Laplace{Dim,T<:Real,N,M,K} <: DiffOp 53 struct Laplace{Dim,T<:Real,N,M,K} <: DiffOpCartesian{Dim}
51 grid::Grid.EquidistantGrid{Dim,T} 54 grid::Grid.EquidistantGrid{Dim,T}
52 a::T 55 a::T
53 op::D2{Float64,N,M,K} 56 op::D2{Float64,N,M,K}
57 end
58
59 function apply(L::Laplace{Dim}, v::AbstractArray{T,Dim} where T, I::CartesianIndex{Dim}) where Dim
60 error("not implemented")
54 end 61 end
55 62
56 # u = L*v 63 # u = L*v
57 function apply(L::Laplace{1}, v::AbstractVector, i::Int) 64 function apply(L::Laplace{1}, v::AbstractVector, i::Int)
58 h = Grid.spacings(L.grid)[1] 65 h = Grid.spacings(L.grid)[1]
61 end 68 end
62 69
63 using UnsafeArrays 70 using UnsafeArrays
64 71
65 # u = L*v 72 # u = L*v
66 function apply(L::Laplace{2}, v::AbstractVector, i::Int) 73 function apply(L::Laplace{2}, v::AbstractArray{T,2} where T, I::CartesianIndex{2})
67 h = Grid.spacings(L.grid) 74 h = Grid.spacings(L.grid)
68 75
69 li = LinearIndices(L.grid.numberOfPointsPerDim)
70 ci = CartesianIndices(L.grid.numberOfPointsPerDim)
71 I = ci[i]
72
73 # 2nd x-derivative 76 # 2nd x-derivative
74 @inbounds vx = uview(v, uview(li,:,I[2])) 77 @inbounds vx = uview(v, :, I[2])
75 uᵢ = apply(L.op, h[1], vx , I[1]) 78 @inbounds uᵢ = apply(L.op, h[1], vx , I[1])
76 # 2nd y-derivative 79 # 2nd y-derivative
77 @inbounds vy = uview(v, uview(li,I[1],:)) 80 @inbounds vy = uview(v, I[1], :)
78 uᵢ += apply(L.op, h[2], vy, I[2]) 81 @inbounds uᵢ += apply(L.op, h[2], vy, I[2])
79 82
80 return uᵢ 83 return uᵢ
81 end 84 end