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
|
# Copyright (C) 2023-2024 Matthew Scroggs and Garth N. Wells
#
# This file is part of Basix (https://www.fenicsproject.org)
#
# SPDX-License-Identifier: MIT
"""Functions for working with polynomials."""
import numpy as np
import numpy.typing as npt
from basix._basixcpp import PolynomialType, PolysetType
from basix._basixcpp import polynomials_dim as _pd
from basix._basixcpp import restriction as _restriction
from basix._basixcpp import superset as _superset
from basix._basixcpp import tabulate_polynomial_set as _tps
from basix._basixcpp import tabulate_polynomials as _tabulate_polynomials
from basix.cell import CellType
from basix.utils import index
__all__ = ["reshape_coefficients", "dim", "tabulate_polynomial_set"]
def reshape_coefficients(
poly_type: PolynomialType,
cell_type: CellType,
coefficients: npt.NDArray[np.float64],
value_size: int,
input_degree: int,
output_degree: int,
) -> npt.NDArray[np.float64]:
"""Reshape the coefficients.
Args:
poly_type: The polynomial type.
cell_type: The cell type
coefficients: The coefficients
value_size: The value size of the polynomials associated with
the coefficients.
input_degree: The maximum degree of polynomials associated with
the input coefficients.
output_degree: The maximum degree of polynomials associated with
the output coefficients.
Returns:
Coefficients representing the same coefficients as the input in
the set of polynomials of the output degree.
"""
if poly_type != PolynomialType.legendre:
raise NotImplementedError()
if output_degree < input_degree:
raise ValueError("Output degree must be greater than or equal to input degree")
if output_degree == input_degree:
return coefficients
pdim = dim(poly_type, cell_type, output_degree)
out = np.zeros((coefficients.shape[0], pdim * value_size))
indices: list[tuple[int, ...]] = []
if cell_type == CellType.interval:
indices = [(i,) for i in range(input_degree + 1)]
def idx(d, i):
return index(i[0])
elif cell_type == CellType.triangle:
indices = [(i, j) for i in range(input_degree + 1) for j in range(input_degree + 1 - i)]
def idx(d, i):
return index(i[1], i[0])
elif cell_type == CellType.tetrahedron:
indices = [
(i, j, k)
for i in range(input_degree + 1)
for j in range(input_degree + 1 - i)
for k in range(input_degree + 1 - i - j)
]
def idx(d, i):
return index(i[2], i[1], i[0])
elif cell_type == CellType.quadrilateral:
indices = [(i, j) for i in range(input_degree + 1) for j in range(input_degree + 1)]
def idx(d, i):
return (d + 1) * i[0] + i[1]
elif cell_type == CellType.hexahedron:
indices = [
(i, j, k)
for i in range(input_degree + 1)
for j in range(input_degree + 1)
for k in range(input_degree + 1)
]
def idx(d, i):
return (d + 1) ** 2 * i[0] + (d + 1) * i[1] + i[2]
elif cell_type == CellType.pyramid:
indices = [
(i, j, k)
for k in range(input_degree + 1)
for i in range(input_degree + 1 - k)
for j in range(input_degree + 1 - k)
]
def idx(d, i):
rv = d - i[2] + 1
r0 = i[2] * (d + 1) * (d - i[2] + 2) + (2 * i[2] - 1) * (i[2] - 1) * i[2] // 6
return r0 + i[0] * rv + i[1]
elif cell_type == CellType.prism:
indices = [
(i, j, k)
for i in range(input_degree + 1)
for j in range(input_degree + 1 - i)
for k in range(input_degree + 1)
]
def idx(d, i):
return (d + 1) * index(i[1], i[0]) + i[2]
else:
raise ValueError("Unsupported cell type")
pdim_in = dim(poly_type, cell_type, input_degree)
for v in range(value_size):
for i in indices:
out[:, v * pdim + idx(output_degree, i)] = coefficients[
:, v * pdim_in + idx(input_degree, i)
]
return out
def dim(ptype: PolynomialType, celltype: CellType, degree: int) -> int:
"""Dimension of a polynomial space.
Args:
ptype: The polynomial type
celltype: The cell type
degree: The polynomial degree
Returns:
The dimension of the polynomial space
"""
return _pd(ptype, celltype, degree)
def tabulate_polynomials(
ptype: PolynomialType, celltype: CellType, degree: int, pts: npt.NDArray
) -> npt.ArrayLike:
"""Tabulate a set of polynomials on a reference cell.
Args:
ptype: The polynomial type
celltype: The cell type
degree: The polynomial degree
pts: The points
Returns:
Tabulated polynomials
"""
return _tabulate_polynomials(ptype, celltype, degree, pts)
def restriction(ptype: PolysetType, cell: CellType, restriction_cell: CellType) -> PolysetType:
"""Get the polyset type that represents the restrictions of a type on a subentity.
Args:
ptype: The polynomial type
cell: The cell type
restriction_cell: The cell type if the subentity
Returns:
The restricted polyset type
"""
return _restriction(ptype, cell, restriction_cell)
def superset(cell: CellType, type1: PolysetType, type2: PolysetType) -> PolysetType:
"""Get the polyset type that is a superset of two types on the given cell.
Args:
cell: The cell type
type1: The first polyset type
type2: The second polyset type
Returns:
The superset type
"""
return _superset(cell, type1, type2)
def tabulate_polynomial_set(
celltype: CellType, ptype: PolysetType, degree: int, nderiv: int, pts: npt.NDArray
) -> npt.ArrayLike:
"""Tabulate a polynomial set.
Args:
celltype: The cell type
ptype: The polyset type
degree: The polynomial degree
nderiv: The number of derivatives
pts: The points to tabulat at
Returns:
Tabulated polynomial set
"""
return _tps(celltype, ptype, degree, nderiv, pts)
|