|
| 1 | +# Copyright (C) 2019 Chris Eldred (Inria Grenoble Rhone-Alpes) and Werner Bauer (Inria Rennes) |
| 2 | +# |
| 3 | +# This file is part of FIAT. |
| 4 | +# |
| 5 | +# FIAT is free software: you can redistribute it and/or modify |
| 6 | +# it under the terms of the GNU Lesser General Public License as published by |
| 7 | +# the Free Software Foundation, either version 3 of the License, or |
| 8 | +# (at your option) any later version. |
| 9 | +# |
| 10 | +# FIAT is distributed in the hope that it will be useful, |
| 11 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | +# GNU Lesser General Public License for more details. |
| 14 | +# |
| 15 | +# You should have received a copy of the GNU Lesser General Public License |
| 16 | +# along with FIAT. If not, see <http://www.gnu.org/licenses/>. |
| 17 | +# |
| 18 | + |
| 19 | +import sympy |
| 20 | +import numpy as np |
| 21 | +from FIAT.finite_element import FiniteElement |
| 22 | +from FIAT.dual_set import make_entity_closure_ids |
| 23 | +from FIAT.polynomial_set import mis |
| 24 | +from FIAT import quadrature |
| 25 | + |
| 26 | +x = sympy.symbols('x') |
| 27 | + |
| 28 | + |
| 29 | +def _lagrange_poly(i, xi): |
| 30 | + '''Returns the Langrange polynomial P_i(x) of pts xi |
| 31 | + which interpolates the values (0,0,..1,..,0) where 1 is at the ith support point |
| 32 | + :param i: non-zero location |
| 33 | + :param xi: set of N support points |
| 34 | + ''' |
| 35 | + index = list(range(len(xi))) |
| 36 | + index.pop(i) |
| 37 | + return sympy.prod([(x-xi[j][0])/(xi[i][0]-xi[j][0]) for j in index]) |
| 38 | + |
| 39 | + |
| 40 | +def _lagrange_basis(spts): |
| 41 | + symbas = [] |
| 42 | + for i in range(len(spts)): |
| 43 | + symbas.append(_lagrange_poly(i, spts)) |
| 44 | + return symbas |
| 45 | + |
| 46 | + |
| 47 | +def _create_compatible_l2_basis(cg_symbas): |
| 48 | + ncg_basis = len(cg_symbas) |
| 49 | + symbas = [] |
| 50 | + for i in range(1, ncg_basis): |
| 51 | + basis = 0 |
| 52 | + for j in range(i): |
| 53 | + basis = basis + sympy.diff(-cg_symbas[j]) |
| 54 | + symbas.append(basis) |
| 55 | + return symbas |
| 56 | + |
| 57 | + |
| 58 | +class _EdgeElement(FiniteElement): |
| 59 | + |
| 60 | + def __new__(cls, ref_el, degree): |
| 61 | + dim = ref_el.get_spatial_dimension() |
| 62 | + if dim == 1: |
| 63 | + self = super().__new__(cls) |
| 64 | + return self |
| 65 | + else: |
| 66 | + raise IndexError("only intervals supported for _IntervalElement") |
| 67 | + |
| 68 | + def tabulate(self, order, points, entity=None): |
| 69 | + |
| 70 | + if entity is None: |
| 71 | + entity = (self.ref_el.get_spatial_dimension(), 0) |
| 72 | + entity_dim, entity_id = entity |
| 73 | + dim = self.ref_el.get_spatial_dimension() |
| 74 | + |
| 75 | + transform = self.ref_el.get_entity_transform(entity_dim, entity_id) |
| 76 | + points = list(map(transform, points)) |
| 77 | + |
| 78 | + phivals = {} |
| 79 | + for o in range(order + 1): |
| 80 | + alphas = mis(dim, o) |
| 81 | + for alpha in alphas: |
| 82 | + try: |
| 83 | + basis = self.basis[alpha] |
| 84 | + except KeyError: |
| 85 | + basis = sympy.diff(self.basis[(0,)], x) |
| 86 | + self.basis[alpha] = basis |
| 87 | + T = np.zeros((len(basis), len(points))) |
| 88 | + for i in range(len(points)): |
| 89 | + subs = {x: points[i][0]} |
| 90 | + for j, f in enumerate(basis): |
| 91 | + T[j, i] = f.evalf(subs=subs) |
| 92 | + phivals[alpha] = T |
| 93 | + |
| 94 | + return phivals |
| 95 | + |
| 96 | + def degree(self): |
| 97 | + return self._degree |
| 98 | + |
| 99 | + def get_nodal_basis(self): |
| 100 | + raise NotImplementedError("get_nodal_basis not implemented for _IntervalElement") |
| 101 | + |
| 102 | + def get_dual_set(self): |
| 103 | + raise NotImplementedError("get_dual_set is not implemented for _IntervalElement") |
| 104 | + |
| 105 | + def get_coeffs(self): |
| 106 | + raise NotImplementedError("get_coeffs not implemented for _IntervalElement") |
| 107 | + |
| 108 | + def entity_dofs(self): |
| 109 | + """Return the map of topological entities to degrees of |
| 110 | + freedom for the finite element.""" |
| 111 | + return self.entity_ids |
| 112 | + |
| 113 | + def entity_closure_dofs(self): |
| 114 | + """Return the map of topological entities to degrees of |
| 115 | + freedom on the closure of those entities for the finite element.""" |
| 116 | + return self.entity_closure_ids |
| 117 | + |
| 118 | + def value_shape(self): |
| 119 | + return () |
| 120 | + |
| 121 | + def dmats(self): |
| 122 | + raise NotImplementedError |
| 123 | + |
| 124 | + def get_num_members(self, arg): |
| 125 | + raise NotImplementedError |
| 126 | + |
| 127 | + def space_dimension(self): |
| 128 | + return len(self.basis[(0,)]) |
| 129 | + |
| 130 | + def __init__(self, ref_el, degree): |
| 131 | + |
| 132 | + dim = ref_el.get_spatial_dimension() |
| 133 | + topology = ref_el.get_topology() |
| 134 | + |
| 135 | + if not dim == 1: |
| 136 | + raise IndexError("only intervals supported for DMSE") |
| 137 | + |
| 138 | + formdegree = 1 |
| 139 | + |
| 140 | + # This is general code to build empty entity_ids |
| 141 | + entity_ids = {} |
| 142 | + for dim in range(dim+1): |
| 143 | + entity_ids[dim] = {} |
| 144 | + for entity in sorted(topology[dim]): |
| 145 | + entity_ids[dim][entity] = [] |
| 146 | + |
| 147 | + # The only dofs for DMSE are with the interval! |
| 148 | + entity_ids[dim][0] = list(range(degree + 1)) |
| 149 | + |
| 150 | + # Build the basis |
| 151 | + # This is a dictionary basis[o] that gives the "basis" functions for derivative order o, where o=0 is just the basis itself |
| 152 | + # This is filled as needed for o > 0 by tabulate |
| 153 | + |
| 154 | + self.entity_ids = entity_ids |
| 155 | + self.entity_closure_ids = make_entity_closure_ids(ref_el, entity_ids) |
| 156 | + self._degree = degree |
| 157 | + |
| 158 | + super(_EdgeElement, self).__init__(ref_el=ref_el, dual=None, order=degree, formdegree=formdegree) |
| 159 | + |
| 160 | + |
| 161 | +class EdgeGaussLobattoLegendre(_EdgeElement): |
| 162 | + def __init__(self, ref_el, degree): |
| 163 | + super(EdgeGaussLobattoLegendre, self).__init__(ref_el, degree) |
| 164 | + cont_pts = quadrature.GaussLobattoLegendreQuadratureLineRule(ref_el, degree+2).pts |
| 165 | + cont_basis = _lagrange_basis(cont_pts) |
| 166 | + basis = _create_compatible_l2_basis(cont_basis) |
| 167 | + self.basis = {(0,): sympy.Array(basis)} |
| 168 | + |
| 169 | + |
| 170 | +class EdgeExtendedGaussLegendre(_EdgeElement): |
| 171 | + def __init__(self, ref_el, degree): |
| 172 | + super(EdgeExtendedGaussLegendre, self).__init__(ref_el, degree) |
| 173 | + cont_pts = quadrature.ExtendedGaussLegendreQuadratureLineRule(ref_el, degree+2).pts |
| 174 | + cont_basis = _lagrange_basis(cont_pts) |
| 175 | + basis = _create_compatible_l2_basis(cont_basis) |
| 176 | + self.basis = {(0,): sympy.Array(basis)} |
0 commit comments