| 1 |
# Copyright (C) 2008 Robert C. Kirby (Texas Tech University) |
| 2 |
# |
| 3 |
# This file is part of FIAT (https://www.fenicsproject.org) |
| 4 |
# |
| 5 |
# SPDX-License-Identifier: LGPL-3.0-or-later |
| 6 |
"""Principal orthogonal expansion functions as defined by Karniadakis |
| 7 |
and Sherwin. These are parametrized over a reference element so as |
| 8 |
to allow users to get coordinates that they want.""" |
| 9 |
|
| 10 |
import numpy |
| 11 |
import math |
| 12 |
import sympy |
| 13 |
from FIAT import reference_element |
| 14 |
from FIAT import jacobi |
| 15 |
|
| 16 |
|
| 17 |
def jrc(a, b, n): |
| 18 |
an = (2*n+1+a+b)*(2*n+2+a+b) / (2*(n+1)*(n+1+a+b)) |
| 19 |
bn = (a*a-b*b) * (2*n+1+a+b) / (2*(n+1)*(2*n+a+b)*(n+1+a+b)) |
| 20 |
cn = (n+a)*(n+b)*(2*n+2+a+b) / ((n+1)*(n+1+a+b)*(2*n+a+b)) |
| 21 |
return an, bn, cn |
| 22 |
|
| 23 |
|
| 24 |
def _tabulate_dpts(tabulator, D, n, order, pts): |
| 25 |
X = sympy.DeferredVector('x') |
| 26 |
|
| 27 |
def form_derivative(F): |
| 28 |
'''Forms the derivative recursively, i.e., |
| 29 |
F -> [F_x, F_y, F_z], |
| 30 |
[F_x, F_y, F_z] -> [[F_xx, F_xy, F_xz], |
| 31 |
[F_yx, F_yy, F_yz], |
| 32 |
[F_zx, F_zy, F_zz]] |
| 33 |
and so forth. |
| 34 |
''' |
| 35 |
out = [] |
| 36 |
try: |
| 37 |
out = [sympy.diff(F, X[j]) for j in range(D)] |
| 38 |
except (AttributeError, ValueError): |
| 39 |
# Intercept errors like |
| 40 |
# AttributeError: 'list' object has no attribute |
| 41 |
# 'free_symbols' |
| 42 |
for f in F: |
| 43 |
out.append(form_derivative(f)) |
| 44 |
return out |
| 45 |
|
| 46 |
def numpy_lambdify(X, F): |
| 47 |
'''Unfortunately, SymPy's own lambdify() doesn't work well with |
| 48 |
NumPy in that simple functions like |
| 49 |
lambda x: 1.0, |
| 50 |
when evaluated with NumPy arrays, return just "1.0" instead of |
| 51 |
an array of 1s with the same shape as x. This function does that. |
| 52 |
''' |
| 53 |
try: |
| 54 |
lambda_x = [numpy_lambdify(X, f) for f in F] |
| 55 |
except TypeError: # 'function' object is not iterable |
| 56 |
# SymPy's lambdify also works on functions that return arrays. |
| 57 |
# However, use it componentwise here so we can add 0*x to each |
| 58 |
# component individually. This is necessary to maintain shapes |
| 59 |
# if evaluated with NumPy arrays. |
| 60 |
lmbd_tmp = sympy.lambdify(X, F) |
| 61 |
lambda_x = lambda x: lmbd_tmp(x) + 0 * x[0] |
| 62 |
return lambda_x |
| 63 |
|
| 64 |
def evaluate_lambda(lmbd, x): |
| 65 |
'''Properly evaluate lambda expressions recursively for iterables. |
| 66 |
''' |
| 67 |
try: |
| 68 |
values = [evaluate_lambda(l, x) for l in lmbd] |
| 69 |
except TypeError: # 'function' object is not iterable |
| 70 |
values = lmbd(x) |
| 71 |
return values |
| 72 |
|
| 73 |
# Tabulate symbolically |
| 74 |
symbolic_tab = tabulator(n, X) |
| 75 |
# Make sure that the entries of symbolic_tab are lists so we can |
| 76 |
# append derivatives |
| 77 |
symbolic_tab = [[phi] for phi in symbolic_tab] |
| 78 |
# |
| 79 |
data = (order + 1) * [None] |
| 80 |
for r in range(order + 1): |
| 81 |
shape = [len(symbolic_tab), len(pts)] + r * [D] |
| 82 |
data[r] = numpy.empty(shape) |
| 83 |
for i, phi in enumerate(symbolic_tab): |
| 84 |
# Evaluate the function numerically using lambda expressions |
| 85 |
deriv_lambda = numpy_lambdify(X, phi[r]) |
| 86 |
data[r][i] = \ |
| 87 |
numpy.array(evaluate_lambda(deriv_lambda, pts.T)).T |
| 88 |
# Symbolically compute the next derivative. |
| 89 |
# This actually happens once too many here; never mind for |
| 90 |
# now. |
| 91 |
phi.append(form_derivative(phi[-1])) |
| 92 |
return data |
| 93 |
|
| 94 |
|
| 95 |
def xi_triangle(eta): |
| 96 |
"""Maps from [-1,1]^2 to the (-1,1) reference triangle.""" |
| 97 |
eta1, eta2 = eta |
| 98 |
xi1 = 0.5 * (1.0 + eta1) * (1.0 - eta2) - 1.0 |
| 99 |
xi2 = eta2 |
| 100 |
return (xi1, xi2) |
| 101 |
|
| 102 |
|
| 103 |
def xi_tetrahedron(eta): |
| 104 |
"""Maps from [-1,1]^3 to the -1/1 reference tetrahedron.""" |
| 105 |
eta1, eta2, eta3 = eta |
| 106 |
xi1 = 0.25 * (1. + eta1) * (1. - eta2) * (1. - eta3) - 1. |
| 107 |
xi2 = 0.5 * (1. + eta2) * (1. - eta3) - 1. |
| 108 |
xi3 = eta3 |
| 109 |
return xi1, xi2, xi3 |
| 110 |
|
| 111 |
|
| 112 |
class PointExpansionSet(object): |
| 113 |
"""Evaluates the point basis on a point reference element.""" |
| 114 |
|
| 115 |
def __init__(self, ref_el): |
| 116 |
if ref_el.get_spatial_dimension() != 0: |
| 117 |
raise ValueError("Must have a point") |
| 118 |
self.ref_el = ref_el |
| 119 |
self.base_ref_el = reference_element.Point() |
| 120 |
|
| 121 |
def get_num_members(self, n): |
| 122 |
return 1 |
| 123 |
|
| 124 |
def tabulate(self, n, pts): |
| 125 |
"""Returns a numpy array A[i,j] = phi_i(pts[j]) = 1.0.""" |
| 126 |
assert n == 0 |
| 127 |
return numpy.ones((1, len(pts))) |
| 128 |
|
| 129 |
def tabulate_derivatives(self, n, pts): |
| 130 |
"""Returns a numpy array of size A where A[i,j] = phi_i(pts[j]) |
| 131 |
but where each element is an empty tuple (). This maintains |
| 132 |
compatibility with the interfaces of the interval, triangle and |
| 133 |
tetrahedron expansions.""" |
| 134 |
deriv_vals = numpy.empty_like(self.tabulate(n, pts), dtype=tuple) |
| 135 |
deriv_vals.fill(()) |
| 136 |
|
| 137 |
return deriv_vals |
| 138 |
|
| 139 |
|
| 140 |
class LineExpansionSet(object): |
| 141 |
"""Evaluates the Legendre basis on a line reference element.""" |
| 142 |
|
| 143 |
def __init__(self, ref_el): |
| 144 |
if ref_el.get_spatial_dimension() != 1: |
| 145 |
raise Exception("Must have a line") |
| 146 |
self.ref_el = ref_el |
| 147 |
self.base_ref_el = reference_element.DefaultLine() |
| 148 |
v1 = ref_el.get_vertices() |
| 149 |
v2 = self.base_ref_el.get_vertices() |
| 150 |
self.A, self.b = reference_element.make_affine_mapping(v1, v2) |
| 151 |
self.mapping = lambda x: numpy.dot(self.A, x) + self.b |
| 152 |
self.scale = numpy.sqrt(numpy.linalg.det(self.A)) |
| 153 |
|
| 154 |
def get_num_members(self, n): |
| 155 |
return n + 1 |
| 156 |
|
| 157 |
def tabulate(self, n, pts): |
| 158 |
"""Returns a numpy array A[i,j] = phi_i(pts[j])""" |
| 159 |
if len(pts) > 0: |
| 160 |
ref_pts = numpy.array([self.mapping(pt) for pt in pts]) |
| 161 |
psitilde_as = jacobi.eval_jacobi_batch(0, 0, n, ref_pts) |
| 162 |
|
| 163 |
results = numpy.zeros((n + 1, len(pts)), type(pts[0][0])) |
| 164 |
for k in range(n + 1): |
| 165 |
results[k, :] = psitilde_as[k, :] * math.sqrt(k + 0.5) |
| 166 |
|
| 167 |
return results |
| 168 |
else: |
| 169 |
return [] |
| 170 |
|
| 171 |
def tabulate_derivatives(self, n, pts): |
| 172 |
"""Returns a tuple of length one (A,) such that |
| 173 |
A[i,j] = D phi_i(pts[j]). The tuple is returned for |
| 174 |
compatibility with the interfaces of the triangle and |
| 175 |
tetrahedron expansions.""" |
| 176 |
ref_pts = numpy.array([self.mapping(pt) for pt in pts]) |
| 177 |
psitilde_as_derivs = jacobi.eval_jacobi_deriv_batch(0, 0, n, ref_pts) |
| 178 |
|
| 179 |
# Jacobi polynomials defined on [-1, 1], first derivatives need scaling |
| 180 |
psitilde_as_derivs *= 2.0 / self.ref_el.volume() |
| 181 |
|
| 182 |
results = numpy.zeros((n + 1, len(pts)), "d") |
| 183 |
for k in range(0, n + 1): |
| 184 |
results[k, :] = psitilde_as_derivs[k, :] * numpy.sqrt(k + 0.5) |
| 185 |
|
| 186 |
vals = self.tabulate(n, pts) |
| 187 |
deriv_vals = (results,) |
| 188 |
|
| 189 |
# Create the ordinary data structure. |
| 190 |
dv = [] |
| 191 |
for i in range(vals.shape[0]): |
| 192 |
dv.append([]) |
| 193 |
for j in range(vals.shape[1]): |
| 194 |
dv[-1].append((vals[i][j], [deriv_vals[0][i][j]])) |
| 195 |
|
| 196 |
return dv |
| 197 |
|
| 198 |
|
| 199 |
class TriangleExpansionSet(object): |
| 200 |
"""Evaluates the orthonormal Dubiner basis on a triangular |
| 201 |
reference element.""" |
| 202 |
|
| 203 |
def __init__(self, ref_el): |
| 204 |
if ref_el.get_spatial_dimension() != 2: |
| 205 |
raise Exception("Must have a triangle") |
| 206 |
self.ref_el = ref_el |
| 207 |
self.base_ref_el = reference_element.DefaultTriangle() |
| 208 |
v1 = ref_el.get_vertices() |
| 209 |
v2 = self.base_ref_el.get_vertices() |
| 210 |
self.A, self.b = reference_element.make_affine_mapping(v1, v2) |
| 211 |
self.mapping = lambda x: numpy.dot(self.A, x) + self.b |
| 212 |
# self.scale = numpy.sqrt(numpy.linalg.det(self.A)) |
| 213 |
|
| 214 |
def get_num_members(self, n): |
| 215 |
return (n + 1) * (n + 2) // 2 |
| 216 |
|
| 217 |
def tabulate(self, n, pts): |
| 218 |
if len(pts) == 0: |
| 219 |
return numpy.array([]) |
| 220 |
else: |
| 221 |
return numpy.array(self._tabulate(n, numpy.array(pts).T)) |
| 222 |
|
| 223 |
def _tabulate(self, n, pts): |
| 224 |
'''A version of tabulate() that also works for a single point. |
| 225 |
''' |
| 226 |
m1, m2 = self.A.shape |
| 227 |
ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i] |
| 228 |
for i in range(m1)] |
| 229 |
|
| 230 |
def idx(p, q): |
| 231 |
return (p + q) * (p + q + 1) // 2 + q |
| 232 |
|
| 233 |
results = ((n + 1) * (n + 2) // 2) * [None] |
| 234 |
|
| 235 |
results[0] = 1.0 \ |
| 236 |
+ pts[0] - pts[0] \ |
| 237 |
+ pts[1] - pts[1] |
| 238 |
|
| 239 |
if n == 0: |
| 240 |
return results |
| 241 |
|
| 242 |
x = ref_pts[0] |
| 243 |
y = ref_pts[1] |
| 244 |
|
| 245 |
f1 = (1.0 + 2 * x + y) / 2.0 |
| 246 |
f2 = (1.0 - y) / 2.0 |
| 247 |
f3 = f2**2 |
| 248 |
|
| 249 |
results[idx(1, 0)] = f1 |
| 250 |
|
| 251 |
for p in range(1, n): |
| 252 |
a = (2.0 * p + 1) / (1.0 + p) |
| 253 |
# b = p / (p+1.0) |
| 254 |
results[idx(p+1, 0)] = a * f1 * results[idx(p, 0)] \ |
| 255 |
- p/(1.0+p) * f3 * results[idx(p-1, 0)] |
| 256 |
|
| 257 |
for p in range(n): |
| 258 |
results[idx(p, 1)] = 0.5 * (1+2.0*p+(3.0+2.0*p)*y) \ |
| 259 |
* results[idx(p, 0)] |
| 260 |
|
| 261 |
for p in range(n - 1): |
| 262 |
for q in range(1, n - p): |
| 263 |
(a1, a2, a3) = jrc(2 * p + 1, 0, q) |
| 264 |
results[idx(p, q+1)] = \ |
| 265 |
(a1 * y + a2) * results[idx(p, q)] \ |
| 266 |
- a3 * results[idx(p, q-1)] |
| 267 |
|
| 268 |
for p in range(n + 1): |
| 269 |
for q in range(n - p + 1): |
| 270 |
results[idx(p, q)] *= math.sqrt((p + 0.5) * (p + q + 1.0)) |
| 271 |
|
| 272 |
return results |
| 273 |
# return self.scale * results |
| 274 |
|
| 275 |
def tabulate_derivatives(self, n, pts): |
| 276 |
order = 1 |
| 277 |
data = _tabulate_dpts(self._tabulate, 2, n, order, numpy.array(pts)) |
| 278 |
# Put data in the required data structure, i.e., |
| 279 |
# k-tuples which contain the value, and the k-1 derivatives |
| 280 |
# (gradient, Hessian, ...) |
| 281 |
m = data[0].shape[0] |
| 282 |
n = data[0].shape[1] |
| 283 |
data2 = [[tuple([data[r][i][j] for r in range(order+1)]) |
| 284 |
for j in range(n)] |
| 285 |
for i in range(m)] |
| 286 |
return data2 |
| 287 |
|
| 288 |
def tabulate_jet(self, n, pts, order=1): |
| 289 |
return _tabulate_dpts(self._tabulate, 2, n, order, numpy.array(pts)) |
| 290 |
|
| 291 |
|
| 292 |
class TetrahedronExpansionSet(object): |
| 293 |
"""Collapsed orthonormal polynomial expanion on a tetrahedron.""" |
| 294 |
|
| 295 |
def __init__(self, ref_el): |
| 296 |
if ref_el.get_spatial_dimension() != 3: |
| 297 |
raise Exception("Must be a tetrahedron") |
| 298 |
self.ref_el = ref_el |
| 299 |
self.base_ref_el = reference_element.DefaultTetrahedron() |
| 300 |
v1 = ref_el.get_vertices() |
| 301 |
v2 = self.base_ref_el.get_vertices() |
| 302 |
self.A, self.b = reference_element.make_affine_mapping(v1, v2) |
| 303 |
self.mapping = lambda x: numpy.dot(self.A, x) + self.b |
| 304 |
self.scale = numpy.sqrt(numpy.linalg.det(self.A)) |
| 305 |
|
| 306 |
def get_num_members(self, n): |
| 307 |
return (n + 1) * (n + 2) * (n + 3) // 6 |
| 308 |
|
| 309 |
def tabulate(self, n, pts): |
| 310 |
if len(pts) == 0: |
| 311 |
return numpy.array([]) |
| 312 |
else: |
| 313 |
return numpy.array(self._tabulate(n, numpy.array(pts).T)) |
| 314 |
|
| 315 |
def _tabulate(self, n, pts): |
| 316 |
'''A version of tabulate() that also works for a single point. |
| 317 |
''' |
| 318 |
m1, m2 = self.A.shape |
| 319 |
ref_pts = [sum(self.A[i][j] * pts[j] for j in range(m2)) + self.b[i] |
| 320 |
for i in range(m1)] |
| 321 |
|
| 322 |
def idx(p, q, r): |
| 323 |
return (p + q + r)*(p + q + r + 1)*(p + q + r + 2)//6 + (q + r)*(q + r + 1)//2 + r |
| 324 |
|
| 325 |
results = ((n + 1) * (n + 2) * (n + 3) // 6) * [None] |
| 326 |
results[0] = 1.0 \ |
| 327 |
+ pts[0] - pts[0] \ |
| 328 |
+ pts[1] - pts[1] \ |
| 329 |
+ pts[2] - pts[2] |
| 330 |
|
| 331 |
if n == 0: |
| 332 |
return results |
| 333 |
|
| 334 |
x = ref_pts[0] |
| 335 |
y = ref_pts[1] |
| 336 |
z = ref_pts[2] |
| 337 |
|
| 338 |
factor1 = 0.5 * (2.0 + 2.0 * x + y + z) |
| 339 |
factor2 = (0.5 * (y + z))**2 |
| 340 |
factor3 = 0.5 * (1 + 2.0 * y + z) |
| 341 |
factor4 = 0.5 * (1 - z) |
| 342 |
factor5 = factor4**2 |
| 343 |
|
| 344 |
results[idx(1, 0, 0)] = factor1 |
| 345 |
for p in range(1, n): |
| 346 |
a1 = (2.0 * p + 1.0) / (p + 1.0) |
| 347 |
a2 = p / (p + 1.0) |
| 348 |
results[idx(p+1, 0, 0)] = a1 * factor1 * results[idx(p, 0, 0)] \ |
| 349 |
- a2 * factor2 * results[idx(p-1, 0, 0)] |
| 350 |
|
| 351 |
# q = 1 |
| 352 |
for p in range(0, n): |
| 353 |
results[idx(p, 1, 0)] = results[idx(p, 0, 0)] \ |
| 354 |
* (p * (1.0 + y) + (2.0 + 3.0 * y + z) / 2) |
| 355 |
|
| 356 |
for p in range(0, n - 1): |
| 357 |
for q in range(1, n - p): |
| 358 |
(aq, bq, cq) = jrc(2 * p + 1, 0, q) |
| 359 |
qmcoeff = aq * factor3 + bq * factor4 |
| 360 |
qm1coeff = cq * factor5 |
| 361 |
results[idx(p, q+1, 0)] = qmcoeff * results[idx(p, q, 0)] \ |
| 362 |
- qm1coeff * results[idx(p, q-1, 0)] |
| 363 |
|
| 364 |
# now handle r=1 |
| 365 |
for p in range(n): |
| 366 |
for q in range(n - p): |
| 367 |
results[idx(p, q, 1)] = results[idx(p, q, 0)] \ |
| 368 |
* (1.0 + p + q + (2.0 + q + p) * z) |
| 369 |
|
| 370 |
# general r by recurrence |
| 371 |
for p in range(n - 1): |
| 372 |
for q in range(0, n - p - 1): |
| 373 |
for r in range(1, n - p - q): |
| 374 |
ar, br, cr = jrc(2 * p + 2 * q + 2, 0, r) |
| 375 |
results[idx(p, q, r+1)] = \ |
| 376 |
(ar * z + br) * results[idx(p, q, r)] \ |
| 377 |
- cr * results[idx(p, q, r-1)] |
| 378 |
|
| 379 |
for p in range(n + 1): |
| 380 |
for q in range(n - p + 1): |
| 381 |
for r in range(n - p - q + 1): |
| 382 |
results[idx(p, q, r)] *= \ |
| 383 |
math.sqrt((p+0.5)*(p+q+1.0)*(p+q+r+1.5)) |
| 384 |
|
| 385 |
return results |
| 386 |
|
| 387 |
def tabulate_derivatives(self, n, pts): |
| 388 |
order = 1 |
| 389 |
D = 3 |
| 390 |
data = _tabulate_dpts(self._tabulate, D, n, order, numpy.array(pts)) |
| 391 |
# Put data in the required data structure, i.e., |
| 392 |
# k-tuples which contain the value, and the k-1 derivatives |
| 393 |
# (gradient, Hessian, ...) |
| 394 |
m = data[0].shape[0] |
| 395 |
n = data[0].shape[1] |
| 396 |
data2 = [[tuple([data[r][i][j] for r in range(order + 1)]) |
| 397 |
for j in range(n)] |
| 398 |
for i in range(m)] |
| 399 |
return data2 |
| 400 |
|
| 401 |
def tabulate_jet(self, n, pts, order=1): |
| 402 |
return _tabulate_dpts(self._tabulate, 3, n, order, numpy.array(pts)) |
| 403 |
|
| 404 |
|
| 405 |
def get_expansion_set(ref_el): |
| 406 |
"""Returns an ExpansionSet instance appopriate for the given |
| 407 |
reference element.""" |
| 408 |
if ref_el.get_shape() == reference_element.POINT: |
| 409 |
return PointExpansionSet(ref_el) |
| 410 |
elif ref_el.get_shape() == reference_element.LINE: |
| 411 |
return LineExpansionSet(ref_el) |
| 412 |
elif ref_el.get_shape() == reference_element.TRIANGLE: |
| 413 |
return TriangleExpansionSet(ref_el) |
| 414 |
elif ref_el.get_shape() == reference_element.TETRAHEDRON: |
| 415 |
return TetrahedronExpansionSet(ref_el) |
| 416 |
else: |
| 417 |
raise Exception("Unknown reference element type.") |
| 418 |
|
| 419 |
|
| 420 |
def polynomial_dimension(ref_el, degree): |
| 421 |
"""Returns the dimension of the space of polynomials of degree no |
| 422 |
greater than degree on the reference element.""" |
| 423 |
if ref_el.get_shape() == reference_element.POINT: |
| 424 |
if degree > 0: |
| 425 |
raise ValueError("Only degree zero polynomials supported on point elements.") |
| 426 |
return 1 |
| 427 |
elif ref_el.get_shape() == reference_element.LINE: |
| 428 |
return max(0, degree + 1) |
| 429 |
elif ref_el.get_shape() == reference_element.TRIANGLE: |
| 430 |
return max((degree + 1) * (degree + 2) // 2, 0) |
| 431 |
elif ref_el.get_shape() == reference_element.TETRAHEDRON: |
| 432 |
return max(0, (degree + 1) * (degree + 2) * (degree + 3) // 6) |
| 433 |
else: |
| 434 |
raise ValueError("Unknown reference element type.") |