parallelize moments across all triangles
This commit is contained in:
parent
5caf0fc916
commit
604fb74439
1 changed files with 31 additions and 31 deletions
|
|
@ -82,43 +82,43 @@ def get_moments(poly: poly_t, order: int) -> numpy.ndarray:
|
||||||
Iteratively calculate higher-order moments, as per Koehl (2012).
|
Iteratively calculate higher-order moments, as per Koehl (2012).
|
||||||
'''
|
'''
|
||||||
prevlayer, curlayer = 0, 1
|
prevlayer, curlayer = 0, 1
|
||||||
S = numpy.zeros((3, order + 1, order + 1, 2), dtype=float)
|
|
||||||
inds = numpy.arange(order + 1)
|
inds = numpy.arange(order + 1)
|
||||||
mx, my = numpy.meshgrid(inds, inds, indexing='ij')
|
mx, my = numpy.meshgrid(inds, inds, indexing='ij')
|
||||||
valid = numpy.zeros((2, order + 1, order + 1), dtype=bool)
|
valid = numpy.zeros((2, order + 1, order + 1), dtype=bool)
|
||||||
cropped = numpy.zeros((order + 1, order + 1), dtype=bool)
|
cropped = numpy.zeros((order + 1, order + 1), dtype=bool)
|
||||||
|
|
||||||
for i, vecs in enumerate(triangles):
|
Q = numpy.zeros((len(triangles), 3, order + 1, order + 1, 2), dtype=float)
|
||||||
S[:, 0, 0, prevlayer] = 1
|
Q[:, :, 0, 0, prevlayer] = 1
|
||||||
valid[prevlayer, :, :] = False
|
valid[prevlayer, :, :] = False
|
||||||
valid[prevlayer, 0, 0] = True
|
valid[prevlayer, 0, 0] = True
|
||||||
m = 1
|
m = 1
|
||||||
for c in range(1, order + 1):
|
for c in range(1, order + 1):
|
||||||
valid[curlayer] = c >= mx + my
|
valid[curlayer] = c >= mx + my
|
||||||
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
||||||
|
|
||||||
S_prev = S[:, valid[prevlayer], prevlayer]
|
Q_prev = Q[:, :, valid[prevlayer], prevlayer]
|
||||||
S[:, 0, :, curlayer] = 0
|
Q[:, :, 0, :, curlayer] = 0
|
||||||
|
|
||||||
cropped.fill(0)
|
cropped.fill(0)
|
||||||
cropped[1:, :] = valid[curlayer, 1:, :]
|
cropped[1:, :] = valid[curlayer, 1:, :]
|
||||||
S[:, cropped, curlayer] = vecs[:, 0][:, None] * S_prev
|
Q[:, :, cropped, curlayer] = triangles[:, :, 0:1] * Q_prev
|
||||||
|
|
||||||
cropped = numpy.moveaxis(cropped, 0, 1)
|
cropped = numpy.moveaxis(cropped, 0, 1)
|
||||||
S[:, cropped, curlayer] += vecs[:, 1][:, None] * S_prev
|
Q[:, :, cropped, curlayer] += triangles[:, :, 1:2] * Q_prev
|
||||||
|
|
||||||
S[:, valid[prevlayer], curlayer] += vecs[:, 2][:, None] * S_prev
|
Q[:, :, valid[prevlayer], curlayer] += triangles[:, :, 2:3] * Q_prev
|
||||||
|
|
||||||
# Actually only need to cumsum S[:, valid[prevlayer], curlayer], but it's faster
|
# Actually only need to cumsum S[:, valid[prevlayer], curlayer], but it's faster
|
||||||
# when done in-place for the whole array
|
# when done in-place for the whole array
|
||||||
numpy.cumsum(S, axis=0, out=S)
|
numpy.cumsum(Q, axis=1, out=Q)
|
||||||
|
|
||||||
moments[m:m + num_valid] += volumes[i] * S[2, valid[curlayer], curlayer].ravel()
|
moments[m:m + num_valid] += (volumes[:, None] * Q[:, 2, valid[curlayer], curlayer]).sum(axis=0).ravel()
|
||||||
m += num_valid
|
m += num_valid
|
||||||
prevlayer, curlayer = curlayer, prevlayer
|
prevlayer, curlayer = curlayer, prevlayer
|
||||||
|
|
||||||
# reuse S[0] to compute the moments' coefficients
|
# Compute moment coefficients
|
||||||
S[0, 0, 0, prevlayer] = 1
|
S = numpy.zeros((order + 1, order + 1, 2), dtype=float)
|
||||||
|
S[0, 0, prevlayer] = 1
|
||||||
m = 1
|
m = 1
|
||||||
valid[prevlayer, :, :] = False
|
valid[prevlayer, :, :] = False
|
||||||
valid[prevlayer, 0, 0] = True
|
valid[prevlayer, 0, 0] = True
|
||||||
|
|
@ -126,19 +126,19 @@ def get_moments(poly: poly_t, order: int) -> numpy.ndarray:
|
||||||
valid[curlayer] = c >= mx + my
|
valid[curlayer] = c >= mx + my
|
||||||
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
num_valid = (c + 1) * (c + 2) // 2 # always an int, see number_of_moments()
|
||||||
|
|
||||||
S_prev = S[0, valid[prevlayer], prevlayer]
|
S_prev = S[valid[prevlayer], prevlayer]
|
||||||
S[0, 0, :, curlayer] = 0
|
S[0, :, curlayer] = 0
|
||||||
|
|
||||||
cropped.fill(0)
|
cropped.fill(0)
|
||||||
cropped[1:, :] = valid[curlayer, 1:, :]
|
cropped[1:, :] = valid[curlayer, 1:, :]
|
||||||
S[0, cropped, curlayer] = S_prev
|
S[cropped, curlayer] = S_prev
|
||||||
|
|
||||||
cropped = numpy.moveaxis(cropped, 0, 1)
|
cropped = numpy.moveaxis(cropped, 0, 1)
|
||||||
S[0, cropped, curlayer] += S_prev
|
S[cropped, curlayer] += S_prev
|
||||||
|
|
||||||
S[0, valid[prevlayer], curlayer] += S_prev
|
S[valid[prevlayer], curlayer] += S_prev
|
||||||
|
|
||||||
moments[m:m + num_valid] /= S[0, valid[curlayer], curlayer].ravel() * (c + 1) * (c + 2) * (c + 3)
|
moments[m:m + num_valid] /= S[valid[curlayer], curlayer].ravel() * (c + 1) * (c + 2) * (c + 3)
|
||||||
m += num_valid
|
m += num_valid
|
||||||
prevlayer, curlayer = curlayer, prevlayer
|
prevlayer, curlayer = curlayer, prevlayer
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue