-
Notifications
You must be signed in to change notification settings - Fork 2
/
kernel.jl
227 lines (180 loc) · 7.09 KB
/
kernel.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
include("tree.jl")
type KIFMM
tree::Tree
# surfaces variables
nSurface
stdUpEquivSurface
stdUpCheckSurface
stdDownEquivSurface
stdDownCheckSurface
stdNormal
# kernel variables
kernel
# charge variables
chargeTree
KIFMM() = new(Tree(), 0, [], [], [], [], Nil, [])
end
function initialize!(k::KIFMM, nSurface, source, normal, target, charge, nSource, nTarget, rank, maxLevel, kernel::Function)
k.tree = Tree()
k.nSurface = nSurface
# build tree
t = k.tree
populate!(t, source, normal, target, nSource, nTarget, rank, maxLevel)
k.chargeTree = charge
k.kernel = kernel
k.stdUpEquivSurface = []
k.stdUpCheckSurface = []
k.stdDownEquivSurface = []
k.stdDownCheckSurface = []
k.stdNormal = []
dr = 0.1
getStdSurfaces!(√2 + dr, nSurface, k.stdUpEquivSurface)
getStdSurfaces!(4.0 - √2 - 2 * dr, nSurface, k.stdUpCheckSurface)
getStdSurfaces!(√2 + dr, nSurface, k.stdDownCheckSurface)
getStdSurfaces!(4.0 - √2 - 2 * dr, nSurface, k.stdDownEquivSurface)
getStdSurfaces!(1.0, nSurface, k.stdNormal)
end
function FMM!(k::KIFMM)
upPass!(k, 1)
potential = zeros(k.tree.nTarget)
downPass!(k, 1, potential)
return potential
end
function getStdSurfaces!(r::Float64, nSurface::Int, surface)
θ = 2 * π/nSurface
for l = 0:nSurface-1
push!(surface, [r * cos(l*θ), r * sin(l*θ)])
end
end
function getLocalSurface!(origin, center, radius, remote)
nSurface = length(origin)
for l = 1:nSurface
push!(remote, [radius[1] * origin[l][1], radius[2] * origin[l][2]] + center)
end
end
function upPass!(k::KIFMM, rootId)
# not relevant to target, only related to source terms
# S2M, M2M
# bottom up, postorder traversal
t = k.tree
d = t.dict
node = d[rootId]
# at current node, initialize all surfaces
# if there is no target points in this node then
# this is no Down-* surfaces
getLocalSurface!(k.stdDownEquivSurface, node.center, node.radius, node.shiftedDownEquivalentSurface)
getLocalSurface!(k.stdDownCheckSurface, node.center, node.radius, node.shiftedDownCheckSurface)
node.downwardEquivalent = zeros(k.nSurface)
# if there is no source points in this node then
# there is no Up-* surfaces
getLocalSurface!(k.stdUpEquivSurface, node.center, node.radius, node.shiftedUpEquivalentSurface)
getLocalSurface!(k.stdUpCheckSurface, node.center, node.radius, node.shiftedUpCheckSurface)
node.upwardEquivalent = zeros(k.nSurface)
if (node.isLeaf)
# at leaf, S2M
getCharge!(k, rootId)
# upward transfer to equivalent surface (target)
contribution = getAccumulation(k, node.source, node.normal, node.shiftedUpCheckSurface, node.charge)
node.upwardEquivalent = getInversion(k, node.shiftedUpEquivalentSurface, k.stdNormal, node.shiftedUpCheckSurface, contribution)
else
# at non-leaf, M2M
contribution = zeros(k.nSurface)
for l = 1:4
upPass!(k, node.child[l])
if (!d[node.child[l]].isEmpty)
contribution += getAccumulation(k, d[node.child[l]].shiftedUpEquivalentSurface, k.stdNormal, node.shiftedUpCheckSurface, d[node.child[l]].upwardEquivalent)
end
end
node.upwardEquivalent = getInversion(k, node.shiftedUpEquivalentSurface, k.stdNormal, node.shiftedUpCheckSurface, contribution)
end
end
function downPass!(k::KIFMM, rootId, potential)
# preorder traversal, first root, then children
# M2L, L2L, L2T
t = k.tree
d = t.dict
node = d[rootId]
if (node.parent != -1)
# V List
contribution = zeros(k.nSurface)
for l = 1: node.nVList
if (!d[node.vList[l]].isEmpty)
contribution += getAccumulation(k, d[node.vList[l]].shiftedUpEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, d[node.vList[l]].upwardEquivalent)
end
end
node.downwardEquivalent += getInversion(k, node.shiftedDownEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, contribution)
# X list
contribution = zeros(k.nSurface)
for l = 1: node.nXList
if (!d[node.xList[l]].isEmpty)
contribution += getAccumulation(k, d[node.xList[l]].shiftedUpEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, d[node.xList[l]].upwardEquivalent)
end
end
node.downwardEquivalent += getInversion(k, node.shiftedDownEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, contribution)
# L2L
# from parent
parentNode = d[node.parent]
contribution = getAccumulation(k, parentNode.shiftedDownEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, parentNode.downwardEquivalent)
node.downwardEquivalent += getInversion(k, node.shiftedDownEquivalentSurface, k.stdNormal, node.shiftedDownCheckSurface, contribution)
end
if (node.isLeaf && node.nTarget != 0)
node.potential = zeros(node.nTarget)
# get U , W, Parent
for l = 1:node.nUList
# direct interactions
getCharge!(k, node.uList[l])
if (!d[node.uList[l]].isEmpty)
node.potential += getAccumulation(k, d[node.uList[l]].source, d[node.uList[l]].normal, node.target, d[node.uList[l]].charge)
end
end
for l = 1:node.nWList
if (!d[node.wList[l]].isEmpty)
node.potential += getAccumulation(k, d[node.wList[l]].shiftedUpEquivalentSurface, k.stdNormal, node.target, d[node.wList[l]].upwardEquivalent)
end
end
# from down-* surface
node.potential += getAccumulation(k, node.shiftedDownEquivalentSurface, k.stdNormal, node.target, node.downwardEquivalent)
for l = 1:node.nTarget
potential[node.targetIndex[l]] += node.potential[l]
end
end
if !node.isLeaf
for l = 1:4
downPass!(k, node.child[l], potential)
end
end
end
function getCharge!(k::KIFMM, id)
# lazy evaluation
node = k.tree.dict[id]
if (node.chargeComputed == true)
else
node.chargeComputed = true
node.charge = zeros(node.nSource)
for l = 1:node.nSource
node.charge[l] = k.chargeTree[node.sourceIndex[l]]
end
end
end
function kernelEval(source, normal, target, f)
sourceSize = length(source)
targetSize = length(target)
K = zeros(targetSize, sourceSize)
for s = 1:sourceSize
for t = 1: targetSize
K[t, s] = f(source[s], normal[s], target[t])
end
end
return K
end
function getAccumulation(k::KIFMM, source, normal, target, charge)
K = kernelEval(source, normal, target, k.kernel)
# K(target, source) * charge
# density of length(target)
# charge of lenth(source)
return K * charge
end
function getInversion(k::KIFMM, source, normal, target, density)
K = kernelEval(source, normal, target, k.kernel)
return pinv(K)*density
end