changeset 85:8d505e9bc715 cell_based_test

Merge with default
author Jonatan Werpers <jonatan@werpers.com>
date Fri, 25 Jan 2019 15:26:47 +0100
parents b795ec7f9ca0 (diff) 48079bd39969 (current diff)
children c0f33eccd527 9d53ecca34f7
files EquidistantGrid.jl diffOp.jl plotDerivative.jl plotDerivative2d.jl sbpD2.jl stencil.jl
diffstat 5 files changed, 51 insertions(+), 57 deletions(-) [+]
line wrap: on
line diff
--- a/diffOp.jl	Fri Jan 25 15:20:40 2019 +0100
+++ b/diffOp.jl	Fri Jan 25 15:26:47 2019 +0100
@@ -1,6 +1,6 @@
 abstract type DiffOp end
 
-function apply!(D::DiffOp, u::AbstractVector, v::AbstractVector)
+function apply(D::DiffOp, v::AbstractVector, i::Int)
     error("not implemented")
 end
 
@@ -32,7 +32,21 @@
     error("not implemented")
 end
 
-# Differential operator for a*d^2/dx^2
+# DiffOp must have a grid!!!
+function apply!(D::DiffOp, u::AbstractVector, v::AbstractVector)
+    for i ∈ 1:Grid.numberOfPoints(D.grid)
+        u[i] = apply(D, v, i)
+    end
+
+    return nothing
+end
+
+function apply(D::DiffOp, v::AbstractVector)::AbstractVector
+    u = zeros(eltype(v), size(v))
+    apply!(D,v,u)
+    return u
+end
+
 struct Laplace{Dim,T<:Real,N,M,K} <: DiffOp
     grid::Grid.EquidistantGrid{Dim,T}
     a::T
@@ -40,42 +54,28 @@
 end
 
 # u = L*v
-function apply!(L::Laplace{1}, u::AbstractVector, v::AbstractVector)
+function apply(L::Laplace{1}, v::AbstractVector, i::Int)
     h = Grid.spacings(L.grid)[1]
-    apply!(L.op, u, v, h)
-    u .= L.a * u
-    return nothing
+    uᵢ = L.a * apply(L.op, h, v, i)
+    return uᵢ
 end
 
+using UnsafeArrays
+
 # u = L*v
-function apply!(L::Laplace{2}, u::AbstractVector, v::AbstractVector)
-    u .= 0*u
+function apply(L::Laplace{2}, v::AbstractVector, i::Int)
     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
+    ci = CartesianIndices(L.grid.numberOfPointsPerDim)
+    I = ci[i]
 
-    # 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])
+    # 2nd x-derivative
+    @inbounds vx = uview(v, uview(li,:,I[2]))
+    uᵢ  = apply(L.op, h[1], vx , I[1])
+    # 2nd y-derivative
+    @inbounds vy = uview(v, uview(li,I[1],:))
+    uᵢ += apply(L.op, h[2], vy, I[2])
 
-        u[li[:,i]] += temp
-    end
-
-    u .= L.a*u
-
-    return nothing
+    return uᵢ
 end
--- a/plotDerivative.jl	Fri Jan 25 15:20:40 2019 +0100
+++ b/plotDerivative.jl	Fri Jan 25 15:26:47 2019 +0100
@@ -2,7 +2,7 @@
 op =sbp.readOperator("d2_4th.txt","h_4th.txt")
 Laplace = sbp.Laplace(g,1.0,op)
 
-init(x) = sin(x)
+init(x) = cos(x)
 v = sbp.Grid.evalOn(g,init)
 u = zeros(length(v))
 
--- a/plotDerivative2d.jl	Fri Jan 25 15:20:40 2019 +0100
+++ b/plotDerivative2d.jl	Fri Jan 25 15:26:47 2019 +0100
@@ -9,8 +9,8 @@
 
 sbp.apply!(Laplace,u,v)
 
-@show u
-@show u'*u
+#@show u
+#@show u'*u
 
 sbp.Grid.plotgridfunction(g,u)
 
--- a/sbpD2.jl	Fri Jan 25 15:20:40 2019 +0100
+++ b/sbpD2.jl	Fri Jan 25 15:26:47 2019 +0100
@@ -1,24 +1,18 @@
 abstract type ConstantStencilOperator end
 
-function apply!(op::ConstantStencilOperator, u::AbstractVector, v::AbstractVector, h::Real)
-    N = length(v)
+@inline function apply(op::ConstantStencilOperator, h::Real, v::AbstractVector, i::Int)
     cSize = closureSize(op)
+    N = length(v)
 
-    for i ∈ range(1; length=cSize)
-        u[i] = apply(op.closureStencils[i], v, i)/h^2
+    if i ∈ range(1; length=cSize)
+        @inbounds uᵢ = apply(op.closureStencils[i], v, i)/h^2
+    elseif i ∈ range(N - cSize+1, length=cSize)
+        @inbounds uᵢ = Int(op.parity)*apply(flip(op.closureStencils[N-i+1]), v, i)/h^2
+    else
+        @inbounds uᵢ = apply(op.innerStencil, v, i)/h^2
     end
 
-    innerStart = 1 + cSize
-    innerEnd = N - cSize
-    for i ∈ range(innerStart, stop=innerEnd)
-        u[i] = apply(op.innerStencil, v, i)/h^2
-    end
-
-    for i ∈ range(innerEnd+1, length=cSize)
-        u[i] = Int(op.parity)*apply(flip(op.closureStencils[N-i+1]), v, i)/h^2
-    end
-
-    return nothing
+    return uᵢ
 end
 
 @enum Parity begin
@@ -52,7 +46,7 @@
 
     # Create boundary stencils
     boundarySize = length(d["boundary_stencils"])
-    closureStencils = Vector{Stencil}()
+    closureStencils = Vector{typeof(innerStencil)}() # TBD: is the the right way to get the correct type?
 
     for i ∈ 1:boundarySize
         stencilWeights = stringToVector(Float64, d["boundary_stencils"][i])
--- a/stencil.jl	Fri Jan 25 15:20:40 2019 +0100
+++ b/stencil.jl	Fri Jan 25 15:26:47 2019 +0100
@@ -10,18 +10,18 @@
 
 # Provides index into the Stencil based on offset for the root element
 function Base.getindex(s::Stencil, i::Int)
-    # TBD: Rearrange to mark with @boundscheck?
-    if s.range[1] <= i <= s.range[2]
-        return s.weights[1 + i - s.range[1]]
-    else
-        return 0
+    @boundscheck if i < s.range[1] || s.range[2] < i
+        return eltype(s.weights)(0)
     end
+
+    return s.weights[1 + i - s.range[1]]
 end
 
-function apply(s::Stencil, v::AbstractVector, i::Int)
+Base.@propagate_inbounds function apply(s::Stencil, v::AbstractVector, i::Int)
     w = zero(eltype(v))
     for j ∈ s.range[1]:s.range[2]
-        w += s[j]*v[i+j] # TBD: Make this without boundschecks?
+        @inbounds weight = s[j]
+        w += weight*v[i+j]
     end
     return w
 end