-
Notifications
You must be signed in to change notification settings - Fork 0
/
quads.py
executable file
·318 lines (260 loc) · 10.1 KB
/
quads.py
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#!/usr/bin/env python3
# This generates level-symmetric quadratures for an S_n solver
# that match those obtained by Lathrop and Carlson.
#
# Note:
# How do we know that the solution to these nonlinear equations is unique?
import matplotlib.pyplot as plt
import numpy as np
import numpy.linalg as lin
import scipy.optimize
from mpl_toolkits.mplot3d import Axes3D
import itertools
from textwrap import wrap
np.seterr(all='raise')
# helper to wrap in C++ initializer syntax
def wrapCurly(ll):
lls = [str(s) for s in ll]
return '\n '.join(wrap('{' + ', '.join(lls) + '}'))
class LevelSymmetricQuadrature:
'''
This class defines symmetry relations required for pointwise weights
on a quadrature set, and provides a plotting utility.
'''
def generate_symmetry_dict(self):
'''
Generates a dictionary that maps i,j,k indices of the
quadrature to its enumerated group that tells which
rays must have the same weight in order to maintain
symmetry about all 90 deg rotations
'''
if self.ptweight_dict:
return
symm_group = 0
for i in range(self.n_2):
for j in range(self.n_2-i):
k = self.n_2-i-j-1
# Update dictionary of point weights
# Each different point weight corresponds to a group
# of permuations of the indices. This comes from the
# symmetry condition on point weights.
if (i, j, k) not in self.ptweight_dict:
newkeys = itertools.permutations((i,j,k))
for newkey in newkeys:
self.ptweight_dict[newkey] = symm_group
self.max_symm_group = symm_group
symm_group += 1
def gen_sym(self):
'''
Generates a singular matrix, which, if the RHS satisfied certain conditions,
yields a system of equations where level weights are the RHS, and
the solution of this system gives a set of weights for each ray in
a symmetric group.
e.g.
1
1 2 2
2 2 3 4 3
2 3 2 2 4 4 2
1 2 2 1 1 2 3 2 1
are each equivalent to a certain matrix
'''
self.generate_symmetry_dict()
ncols = max(self.ptweight_dict.values()) + 1
nrows = self.n_2
# Allocate matrix:
mat = np.zeros((nrows, ncols))
self.generate_symmetry_dict()
for i in range(self.n_2):
for j in range(self.n_2-i):
k = self.n_2-i-j-1
group = self.ptweight_dict[(i,j,k)]
mat[i, group] += 1.0
return mat
def plot(self, circles=False):
# Make sure symmetry dictionary is created:
self.generate_symmetry_dict()
# Create a 3D plot of the quadrature
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# Loop over all points on the sphere (in positive quadrant)
n = self.n_2 * 2
npts = int(n*(n+2)/8) # this is guaranteed to be an integer
xvals = np.zeros(npts)
yvals = np.zeros(npts)
zvals = np.zeros(npts)
weights = np.zeros(npts)
pt = 0
for i in range(self.n_2):
for j in range(self.n_2-i):
k = self.n_2-i-j-1
xvals[pt] = self.mus[i]
yvals[pt] = self.mus[j]
zvals[pt] = self.mus[k]
weights[pt] = self.point_weights[self.ptweight_dict[(i,j,k)]]
pt += 1
# permute scatter plot over all octants
# The below for loops can be commented and the next line uncommented
# to get a single octant.
# xs = 1; ys = 1; zs = 1
sgns = [-1.0, 1.0]
for xs in sgns:
for ys in sgns:
for zs in sgns:
sc = ax.scatter(xs*xvals, ys*yvals, zs=zs*zvals, s=20, c=weights)
plt.colorbar(sc)
# Plot some circles that intersect the points in order to exhibit the
# unique nature of the level symmetric quadratures:
if circles:
n_circ = 50 # points on the circle
phis = np.linspace(0.0, 2.0 * np.pi, n_circ)
for i in range(self.n_2):
xvals = np.zeros(n_circ)
yvals = np.zeros(n_circ)
zvals = np.zeros(n_circ)
j = 0
k = self.n_2-i-j-1
circ_radius = np.sqrt(self.mus[j]**2 + self.mus[k]**2)
for l in range(n_circ):
xvals[l] = np.abs(self.mus[i])
yvals[l] = circ_radius * np.cos(phis[l])
zvals[l] = circ_radius * np.sin(phis[l])
for sign in [-1, 1]:
ax.plot(sign * xvals, yvals, zvals, c='b', alpha=0.25)
for sign in [-1, 1]:
ax.plot(yvals, sign*xvals, zvals, c='b', alpha=0.25)
for sign in [-1, 1]:
ax.plot(yvals, zvals, sign*xvals, c='b', alpha=0.25)
plt.show()
def calcPointWeights(self, sum4pi=False):
# assumes that self.mus and self.weights have already been calculated
n = self.n_2 * 2
# Division by two here matches the normalization of the paper
mat_sym = self.gen_sym() / 2.0
self.point_weights = np.matmul(lin.pinv(mat_sym),self.weights)
if sum4pi:
# Get count of
tot_weight = np.sum(self.point_weights)
self.point_weights *= 4.0 * np.pi / tot_weight / (n * (n+2))
def toCpp(self):
# Prints out a template specialization of my LevelSymmetricQuadrature class
# that gives a nice, clean syntax.
retstr = """
else if (na == %i and ma == %s)
{
mu = %s;
weights = %s;
point_weights = %s;
}
""" % ( self.n_2 * 2,
self.type,
wrapCurly(self.mus),
wrapCurly(self.weights),
wrapCurly(self.point_weights))
return retstr
class EvenQuadrature(LevelSymmetricQuadrature):
# Generates a level-symmetric S_n quadrature, given n
# A nonlinear set of equations is created, with both
# mu_1 and the weights of the quadrature being the unknowns
# This quadrature set exactly integrates even power monomials
# of the polar angle.
#
# This then feeds into a linear system for the weights,
# which arises from symmetry considerations on levels.
def __init__(self, n, sum4pi=False):
self.type = 'EVEN'
self.n_2 = int(n/2)
# avoid writing "self" a bunch
n_2 = self.n_2
# empty point weight group dictionary
self.ptweight_dict = {}
# solve for quadrature
guess = np.zeros(n_2 + 1)
guess[1:] = np.ones(n_2) / n_2 # equal weights
while True:
try:
guess[0] = 0.5 * np.random.rand(1)[0]
soln = scipy.optimize.root(self.residual, guess, method='anderson')
if not soln.success:
continue
break
except:
pass
if not soln.success:
raise Exception('Failed to find quadrature in nonlinear solve')
self.mus = np.zeros(self.n_2)
self.mus[0] = np.abs(soln.x[0])
delta = 2.0 * (1.0 - 3.0 * self.mus[0]**2) / (2.0 * self.n_2 - 2.0)
for i in range(1, self.n_2):
self.mus[i] = np.sqrt(self.mus[i-1]**2 + delta)
self.weights = soln.x[1:]
self.calcPointWeights(sum4pi=sum4pi)
def residual(self, unknowns):
# Unknowns are mu1, followed by the weights.
mu1 = unknowns[0]
weights = unknowns[1:]
# Create residual
residual = np.zeros(self.n_2+1)
# Calculate mu values
delta = 2.0 * (1.0 - 3.0 * mu1**2) / (2.0 * self.n_2 - 2.0)
mus = np.zeros(self.n_2)
mus[0] = mu1
for i in range(1, self.n_2):
mus[i] = np.sqrt(mus[i-1]**2 + delta)
for i in range(self.n_2+1):
residual[i] = 2.0 * np.dot(weights, mus**(2*i)) - 1.0/(2.0 * i + 1)
return residual
class OddQuadrature(LevelSymmetricQuadrature):
# Generates a level-symmetric S_n quadrature, given n
# a quadrature which matches odd moments.
def __init__(self, n, sum4pi=False):
self.type = 'ODD'
self.n_2 = int(n/2)
# avoid writing "self" a bunch
n_2 = self.n_2
# empty point weight group dictionary
self.ptweight_dict = {}
# solve for quadrature
guess = np.zeros(n_2 + 1)
guess[1:] = np.ones(n_2) / n_2 # equal weights
while True:
try:
guess[0] = 0.5 * np.random.rand(1)[0]
soln = scipy.optimize.root(self.residual, guess, method='anderson')
break
except:
pass
if not soln.success:
raise Exception('Failed to find quadrature in nonlinear solve')
self.mus = np.zeros(self.n_2)
self.mus[0] = np.abs(soln.x[0])
delta = 2.0 * (1.0 - 3.0 * self.mus[0]**2) / (2.0 * self.n_2 - 2.0)
for i in range(1, self.n_2):
self.mus[i] = np.sqrt(self.mus[i-1]**2 + delta)
self.weights = soln.x[1:]
self.calcPointWeights(sum4pi=sum4pi)
def residual(self, unknowns):
# Unknowns are mu1, followed by the weights.
mu1 = unknowns[0]
weights = unknowns[1:]
# Create residual
residual = np.zeros(self.n_2+1)
# Calculate mu values
delta = 2.0 * (1.0 - 3.0 * mu1**2) / (2.0 * self.n_2 - 2.0)
mus = np.zeros(self.n_2)
mus[0] = mu1
for i in range(1, self.n_2):
mus[i] = np.sqrt(mus[i-1]**2 + delta)
for i in range(self.n_2+1):
residual[i] = 2.0 * np.dot(weights, mus**i) - 1.0/(i + 1)
return residual
# for i in range(4, 30, 2):
# even = EvenQuadrature(i)#, sum4pi=True)
# odd = OddQuadrature(i)#, sum4pi=True)
# print(even.toCpp())
# print(odd.toCpp())
# print(q.gen_sym())
# print(q.gen_sym())
q = EvenQuadrature(4, sum4pi=True)
# print(q.toCpp())
# print(q.toCpp())
q.plot()