/
quadrature.jl
293 lines (231 loc) · 6.77 KB
/
quadrature.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# ============================================================
# Quadrature Methods
# ============================================================
"""
$(SIGNATURES)
Maxwell quadrature
## Arguments
* `N`: quadrature order (MUST less than 33)
"""
function maxwell_quadrature(N2, C = 1.0)
N = N2 ÷ 2
a = zeros(N)
b = zeros(N)
a[1] = 1.0 / sqrt(pi)
a[2] = 2.0 / sqrt(pi) / (pi - 2.0)
b[2] = a[1] / (a[1] + a[2]) / 2.0
for i in 3:N
b[i] = (i - 2) + 1.0 / 2.0 - b[i - 1] - a[i - 1]^2
a[i] = ((i-1)^2 / (4.0 * b[i]) - b[i - 1] - 1.0 / 2) / a[i - 1] - a[i - 1]
end
J = Diagonal(a) + diagm(1=>sqrt.(b[2:N])) + diagm(-1=>sqrt.(b[2:N]))
v, V = eigen(J)
w = V[1, :] .^ 2 .* sqrt(pi) / 2.0
vw = [v w]
vw = sortslices(vw, dims=1, by=x->x[1])
v = vw[:, 1]
w = vw[:, 2]
Xis = vcat(-reverse(v), v)
weights = vcat(reverse(w), w)
weights .*= exp.(Xis .^ 2) .* C
Xis .*= C
return Xis, weights
end
"""
$(SIGNATURES)
Gauss-Legendre quadrature
## Arguments
* `n`: quadrature order (MUST be even)
## Outputs
* `points`: quadrature points in 3D coordinate
* `weights`: quadrature weights
"""
function legendre_quadrature(n::Integer)
pointsxyz = zeros(n * n, 3)
weights = zeros(n * n)
# construct Gauss quadrature
mu, gaussweights = gausslegendre(n)
# transform between (mu,phi) and (x,y,z)
phi = [(k + 0.5) * pi / n for k = 0:2*n-1] # equidistance in z axis
range = 1:n÷2 # only use upper half of the sphere as quadrature point due to pseudo 3D
x = sqrt.(1.0 .- mu[range] .^ 2) .* cos.(phi)'
y = sqrt.(1.0 .- mu[range] .^ 2) .* sin.(phi)'
z = mu[range] .* ones(size(phi))'
weights = 2.0 * pi / n * repeat(gaussweights[range], 1, 2 * n)
# assign
pointsxyz[:, 1] .= x[:]
pointsxyz[:, 2] .= y[:]
pointsxyz[:, 3] .= z[:]
weights = weights[:]
return pointsxyz, weights
end
"""
$(SIGNATURES)
Octaeder quadrature
## Arguments
* `n`: quadrature order
* `slerpflag`: flag of spherical linear interpolation
## Outputs
* points and triangulation
"""
function octa_quadrature(n::Integer, slerpflag = true::Bool)
points, triangulation = octa_triangle(n, slerpflag)
weights = triangle_weights(points, triangulation)
return points, weights
end
function octa_triangle(n::Integer, slerpflag = true::Bool)
# integral range
pt0 = [0.0, 0.0, 1.0]
pt1 = [0.0, 1.0, 0.0]
pt2 = [1.0, 0.0, 0.0]
# slerp / linspace
if slerpflag
pts01 = slerp(pt0, pt1, n)
pts02 = slerp(pt0, pt2, n)
else
pts01 = linspace(pt0, pt1, n)
pts02 = linspace(pt0, pt2, n)
end
nptsoctant = Int64(n * (n + 1) / 2)
pts = zeros(3, nptsoctant)
# generate points in planar geometry
counter = 0
for i = 1:n
if slerpflag
if i == 1
tmp = pts01[:, 1]
else
tmp = slerp(pts01[:, i], pts02[:, i], i)
end
else
tmp = linspace(pts01[i], pts02[i], i)
end
for j = 1:i
counter += 1
if slerpflag
pts[:, counter] = tmp[:, j]
else
pts[:, counter] = tmp[j]
end
end
end
# project points onto sphere
for i = 1:nptsoctant
pts[:, i] = pts[:, i] / norm(pts[:, i])
end
# enumerate over points and write their connectivity
ids = zeros(Int64, n, n) # matrix that assigns an ID to the points
nTrianglesOctant = Int64(n * n - 2 * n + 1)
triangles = zeros(Int64, 3, nTrianglesOctant) # matrix that contains all triangles
counter = 0
for i = 1:n
for j = 1:i
counter += 1
ids[i, j] = counter
end
end
# create triangles
counter = 0
tmp = zeros(Int64, 1)
for i = 1:n
for j = 1:i-1
tmp = [ids[i, j], ids[i, j+1], ids[i-1, j]]
counter += 1
triangles[:, counter] = tmp
end
if i < n
for j = 1:i-1
tmp = [ids[i, j], ids[i, j+1], ids[i+1, j+1]]
counter += 1
triangles[:, counter] = tmp
end
end
end
# now we have the quadrature points and triangles for a single octant
ptsAll = deepcopy(pts)
tmp = deepcopy(pts)
tmp[1, :] *= -1.0
ptsAll = deepcopy(hcat(ptsAll, tmp))
tmp = deepcopy(pts)
tmp[2, :] *= -1.0
ptsAll = deepcopy(hcat(ptsAll, tmp))
tmp = deepcopy(pts)
tmp[1, :] *= -1.0
tmp[2, :] *= -1.0
ptsAll = deepcopy(hcat(ptsAll, tmp))
tmp = deepcopy(ptsAll)
tmp[3, :] *= -1.0
ptsAll = deepcopy(hcat(ptsAll, tmp))
trianglesAll = deepcopy(triangles)
for i = 1:7
trianglesAll = (hcat(trianglesAll, triangles .+ i .* nptsoctant))
end
ptsAll, triangulation = unique(ptsAll, trianglesAll)
points = permutedims(ptsAll)
triangulation = permutedims(triangulation)
return points, triangulation
end
"""
$(SIGNATURES)
Create quadrature weights from points and triangulation
## Arguments
* `xyz`: quadrature points
* `triangles`: triangulation
## Outputs
* quadrature weights
"""
function triangle_weights(xyz::AM, triangles::AM)
weights = zeros(axes(xyz, 1))
nTriangles = size(triangles, 1)
xy = zeros(3)
yz = zeros(3)
zx = zeros(3)
mid = zeros(3)
for n = 1:nTriangles
# get three nodes of a triangle
i, j, k = triangles[n, :]
# get the corresponding points
x = xyz[i, :]
y = xyz[j, :]
z = xyz[k, :]
# Now get the midpoint of the triangle and the midpoints along the lines
mid = (x + y + z) / 3.0
xy = (x + y) / 2.0
yz = (y + z) / 2.0
zx = (z + x) / 2.0
# These points still have to be projected onto the sphere
mid = mid / norm(mid, 2)
xy = xy / norm(xy, 2)
yz = yz / norm(yz, 2)
zx = zx / norm(zx, 2)
# By these four points, plus x,y,z we can span 6 triangles
# Each of these triangles is assigned to one of the three nodes of the triangle x,y,z.
# the area = the weight
# i
weights[i] += area(x, mid, xy, :sphere)
weights[i] += area(x, mid, zx, :sphere)
# j
weights[j] += area(y, mid, xy, :sphere)
weights[j] += area(y, mid, yz, :sphere)
# k
weights[k] += area(z, mid, yz, :sphere)
weights[k] += area(z, mid, zx, :sphere)
end
return weights
end
"""
$(SIGNATURES)
Evaluate quadrature weight from Newton-Cotes rule
"""
function newton_cotes(idx::T, num::T) where {T<:Integer}
if idx == 1 || idx == num
nc_coeff = 14.0 / 45.0
elseif (idx - 5) % 4 == 0
nc_coeff = 28.0 / 45.0
elseif (idx - 3) % 4 == 0
nc_coeff = 24.0 / 45.0
else
nc_coeff = 64.0 / 45.0
end
return nc_coeff
end