-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyquad.py
323 lines (252 loc) · 9.81 KB
/
pyquad.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
319
320
321
322
323
#/bin/python3
"""
The below code calculates the Gauss-Legendre quadrature points to arbitrary degree.
No limit is imposed on the number of dimensions.
Disclaimer: If you rely on the accuracy of this for real world problems then do your own testing.
This is without warranty. The only check I did was to make sure the determinants add to the
n-volume of the integration domain. You can easily do this too.
Copyright 2020, Marc Graham
License: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy of this software
and associated documentation files (the "Software"), to deal in the Software without restriction,
including without limitation the rights to use, copy, modify, merge, publish, distribute,
sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or
substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
import math
from itertools import combinations, product
import numpy as np
def binom(n, k):
return math.factorial(n) // math.factorial(k) // math.factorial(n - k)
def legendre_poly(n:int,x:float):
# calculate Legendre Polynomial using the Rodrigues derivation
p = 1.
if n<0 :
raise ValueError("negative polynomial order")
if -1.>x or 1.<x:
raise ValueError("Only for range x in [-1, 1]")
if n>0:
p = 0.
for k in range(0,n+1):
p += binom(n,k)*binom(n+k,k)*((x-1.)/2)**k
return p
def legendre_poly_diff(n:int, x:float):
# calculate the derivative using the differential rule
if n<0 :
raise ValueError("negative polynomial order")
if n==0:
return 0.
if -1.>=x or 1.<=x:
raise ValueError("Only for range x in (-1, 1)")
dpdx = (x*legendre_poly(n,x)-legendre_poly(n-1,x))*n/(x**2-1)
return dpdx
def calculate_legendre_roots(n:int):
"""Solves the roots of the legendre polynomial up to
order n by determining eigenvalues of the jacobi matrix.
Weights are calculated using the common rule for the
standard interval.
"""
if n<0 :
raise ValueError("negative polynomial order")
if n==0:
return None
A = np.zeros(shape=(n,n))
for i in range(1,n):
A[i-1,i] = i/np.sqrt(4*i**2-1)
A += A.transpose()
roots, vecs = np.linalg.eigh(A)
weights = np.zeros_like(roots)
for i in range(roots.shape[0]):
x = roots[i]
weights[i] = 2./(legendre_poly_diff(n,x)**2*(1-x**2))
return roots, weights
def vec_to_grid(vec:np.array, weight:np.array, ndim:int):
""" Does the cartesian product of points to get grid of integration points
"""
l = vec.shape[0]
if len(vec.shape)!=1:
raise IndexError("This vec should be a vector")
# prepare a vector with the mapped point and the associated weight
out = np.zeros(shape=(l**ndim,ndim+1))
comb = product(list(range(l)),repeat=ndim) #itertools.product
i = 0
for p in comb:
out[i,ndim] = 1.
for d in range(ndim):
x = vec[p[d]]
out[i,d] = x
out[i,ndim] *= weight[p[d]]
i += 1
return out
"""
***********************************************************************
*
* Primary function here!
*
***********************************************************************
"""
def gauss_points_box(npoly:int, ndim:int):
"""Produces the gaussian integration points and weights (last column)
corresponding to the Legendre-Polynomial of degree npoly, in ndim.
Note that the ndim works for ndim>3 but didn't really think about
how right this is. """
r,w = calculate_legendre_roots(npoly)
out = vec_to_grid(r,w,ndim)
return out
"""
***********************************************************************
*
* The remainder is for mapping to the d-simplex
*
***********************************************************************
"""
def build_mapping(ndim:int):
"""Builds the mapping from the line/square/cube [-1,1]^d to the reference triangle
which has vertices at {e_i} for 1<= i <= d
"""
if ndim<=0 :
raise ValueError("minimum dimension is obviously one")
quad_vert = np.zeros(shape=(2**ndim,ndim))
for i in range(2**(ndim-1)):
quad_vert[0+2*i,0] = -1.*(-1)**i
quad_vert[1+2*i,0] = 1.*(-1)**i
for col in range(1, ndim):
vec = -np.ones(2**(col))
vec = np.hstack((vec,-vec))
l = vec.shape[0]
for i in range(2**(ndim-col-1)):
quad_vert[l*i:l*i+l,col] = vec
# simplex vertices
vertices = np.zeros(shape=(ndim+1,ndim))
for i in range(1,ndim+1):
vertices[i,i-1] = 1.
#mapping
# get every combination of first order variables
pterms = [(0,)]
directions = list(range(1,ndim+1))
for i in range(1, ndim+1):
pterms += list(combinations(directions,i))
l = len(pterms)
# calculate the factor matrix and invert
M = np.ones(shape=(l,l))
for i in range(quad_vert.shape[0]):
M[i,0] = 1.
point = quad_vert[i,:]
for j in range(1,l):
v = pterms[j]
for k in v:
M[i,j] *= point[k-1]
# now we can build the shape functions
Minv = np.linalg.inv(M)
mapto = np.zeros_like(quad_vert)
# first move all the points in the (1,1,1,1) direction
# then shrink by two
for i in range(quad_vert.shape[0]):
mapto[i,:] = (quad_vert[i,:]+1.)/2
# if any point coincides with a simplex vertex
# then it does not move
# for the others, if the point is equidistant
# from two or more other points, then it goes to an average
# of those points
eps = 1e-6
for i in range(quad_vert.shape[0]):
point = mapto[i,:]
dist = np.zeros(ndim+1)
for j in range(ndim+1):
dist[j] = np.linalg.norm(point-vertices[j])
mindist = dist.min()
lowdistgroup = [vertices[k] for k in range(ndim+1)
if np.linalg.norm(point-vertices[k]) <= mindist+eps]
mapto[i,:] = sum(lowdistgroup)/len(lowdistgroup)
return pterms, mapto, Minv
def pterms_diff(pterm, diffdir):
"""
returns the differential of a term above in the direction diffdir"""
if diffdir in pterm:
still_there = [a for a in pterm if a!=diffdir]
if len(still_there)==0:
still_there = 1.
else:
still_there = tuple(still_there)
return (still_there)
else:
return 0.
def apply_map(coord:np.array, pterms:list, mapto:np.array, Minv:np.array):
"""Applies the built map to a point
"""
if len(coord.shape)!=1:
raise IndexError("This coord should be a vector")
ndim = coord.shape[0]
if len(pterms)!=2**ndim:
raise IndexError("not enough poly terms in pterms")
if mapto.shape[0]!=2**ndim:
raise IndexError("Not enough mapping points to determine system")
if Minv.shape[0]!=Minv.shape[1] or Minv.shape[0]!=2**ndim:
raise IndexError("Minv has incompatible dimensions")
ptvals = np.ones(2**ndim)
for i in range(1,len(pterms)):
for d in pterms[i]:
ptvals[i] *= coord[d-1]
shapes = ptvals.dot(Minv).transpose()
res = np.zeros(ndim)
for i in range(ndim):
res[i] = sum([mapto[j,i]*shapes[j] for j in range(2**ndim)])
return res
def jacobian(coord:np.array, pterms:list, mapto:np.array, Minv:np.array):
if len(coord.shape)!=1:
raise IndexError("This coord should be a vector")
ndim = coord.shape[0]
if len(pterms)!=2**ndim:
raise IndexError("not enough poly terms in pterms")
if mapto.shape[0]!=2**ndim:
raise IndexError("Not enough mapping points to determine system")
if Minv.shape[0]!=Minv.shape[1] or Minv.shape[0]!=2**ndim:
raise IndexError("Minv has incompatible dimensions")
# calculate already the derivatives for the jacobian
diffs = []
l = len(pterms)
for d in range(1,ndim+1):
ptd = []
for i in range(l):
ptd.append(pterms_diff(pterms[i], d))
diffs.append(ptd)
jac = np.zeros(shape=(ndim,ndim))
for j in range(ndim):
dif = diffs[j]
difvec = np.ones(2**ndim)
for k in range(2**ndim):
if type(dif[k]) is float:
difvec[k] = dif[k]
elif type(dif[k]) is tuple:
for d in dif[k]:
difvec[k] *= coord[d-1]
else:
raise TypeError("only floats and tuples allowed here")
dshapes = difvec.dot(Minv).transpose()
for i in range(ndim):
jac[i,j] = sum([mapto[k,i]*dshapes[k] for k in range(2**ndim)])
return jac
"""
***********************************************************************
*
* Primary function here!
*
***********************************************************************
"""
def gauss_map_to_simplex(g):
ndim = g.shape[1]-1
ps = g.shape[0]
pterms, mapto, Minv = build_mapping(ndim)
g_mapped = g.copy()
for i in range(ps):
p = g[i,:ndim]
g_mapped[i,:ndim] = apply_map(p, pterms, mapto, Minv)
g_mapped[i,ndim] *= np.linalg.det(jacobian(p, pterms, mapto, Minv))
return g_mapped