/usr/lib/python2.7/dist-packages/ffc/fiatinterface.py is in python-ffc 2016.2.0-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 | # -*- coding: utf-8 -*-
# Copyright (C) 2009-2016 Kristian B. Oelgaard and Anders Logg
#
# This file is part of FFC.
#
# FFC is free software: you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# FFC is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with FFC. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Garth N. Wells, 2009.
# Modified by Marie Rognes, 2009-2013.
# Modified by Martin Sandve Alnæs, 2013
# Modified by Lizao Li, 2015, 2016
# Python modules
import six
import numpy
from numpy import array
# UFL and FIAT modules
import ufl
from ufl.utils.sorting import sorted_by_key
import FIAT
from FIAT.hdiv_trace import HDivTrace
# FFC modules
from ffc.log import debug, error
from ffc.mixedelement import MixedElement
from ffc.restrictedelement import RestrictedElement
from ffc.enrichedelement import EnrichedElement, SpaceOfReals
# Dictionary mapping from cellname to dimension
from ufl.cell import cellname2dim
# Element families supported by FFC
supported_families = ("Brezzi-Douglas-Marini",
"Brezzi-Douglas-Fortin-Marini",
"Crouzeix-Raviart",
"Discontinuous Lagrange",
"Discontinuous Raviart-Thomas",
"HDiv Trace",
"Lagrange",
"Lobatto",
"Nedelec 1st kind H(curl)",
"Nedelec 2nd kind H(curl)",
"Radau",
"Raviart-Thomas",
"Real",
"Bubble",
"Quadrature",
"Regge",
"Hellan-Herrmann-Johnson")
# Cache for computed elements
_cache = {}
def reference_cell(dim):
if isinstance(dim, int):
return FIAT.ufc_simplex(dim)
else:
return FIAT.ufc_simplex(cellname2dim[dim])
def reference_cell_vertices(dim):
"Return dict of coordinates of reference cell vertices for this 'dim'."
cell = reference_cell(dim)
return cell.get_vertices()
def create_element(ufl_element):
# Create element signature for caching (just use UFL element)
element_signature = ufl_element
# Check cache
if element_signature in _cache:
debug("Reusing element from cache")
return _cache[element_signature]
# Create regular FIAT finite element
if isinstance(ufl_element, ufl.FiniteElement):
element = _create_fiat_element(ufl_element)
# Create mixed element (implemented by FFC)
elif isinstance(ufl_element, ufl.MixedElement):
elements = _extract_elements(ufl_element)
element = MixedElement(elements)
# Create element union (implemented by FFC)
elif isinstance(ufl_element, ufl.EnrichedElement):
elements = [create_element(e) for e in ufl_element._elements]
element = EnrichedElement(elements)
# Create restricted element(implemented by FFC)
elif isinstance(ufl_element, ufl.RestrictedElement):
element = _create_restricted_element(ufl_element)
else:
error("Cannot handle this element type: %s" % str(ufl_element))
# Store in cache
_cache[element_signature] = element
return element
def _create_fiat_element(ufl_element):
"Create FIAT element corresponding to given finite element."
# Get element data
family = ufl_element.family()
cell = ufl_element.cell()
cellname = cell.cellname()
degree = ufl_element.degree()
# Check that FFC supports this element
if family not in supported_families:
error("This element family (%s) is not supported by FFC." % family)
# Handle the space of the constant
if family == "Real":
dg0_element = ufl.FiniteElement("DG", cell, 0)
constant = _create_fiat_element(dg0_element)
element = SpaceOfReals(constant)
# FIXME: AL: Should this really be here?
# Handle QuadratureElement
elif family == "Quadrature":
element = QuadratureElement(ufl_element)
else:
# Create FIAT cell
fiat_cell = reference_cell(cellname)
# Handle Bubble element as RestrictedElement of P_{k} to interior
if family == "Bubble":
V = FIAT.supported_elements["Lagrange"](fiat_cell, degree)
tdim = cell.topological_dimension()
return RestrictedElement(V, _indices(V, "interior", tdim), None)
# Check if finite element family is supported by FIAT
if family not in FIAT.supported_elements:
error("Sorry, finite element of type \"%s\" are not supported by FIAT.", family)
# Create FIAT finite element
ElementClass = FIAT.supported_elements[family]
if degree is None:
element = ElementClass(fiat_cell)
else:
element = ElementClass(fiat_cell, degree)
# Consistency check between UFL and FIAT elements.
if element.value_shape() != ufl_element.reference_value_shape():
error("Something went wrong in the construction of FIAT element from UFL element." +
"Shapes are %s and %s." % (element.value_shape(), ufl_element.reference_value_shape()))
return element
def create_quadrature(shape, degree, scheme="default"):
"""
Generate quadrature rule (points, weights) for given shape
that will integrate an polynomial of order 'degree' exactly.
"""
if isinstance(shape, int) and shape == 0:
return (numpy.zeros((1, 0)), numpy.ones((1,)))
if shape in cellname2dim and cellname2dim[shape] == 0:
return (numpy.zeros((1, 0)), numpy.ones((1,)))
if scheme == "vertex":
# The vertex scheme, i.e., averaging the function value in the vertices
# and multiplying with the simplex volume, is only of order 1 and
# inferior to other generic schemes in terms of error reduction.
# Equation systems generated with the vertex scheme have some
# properties that other schemes lack, e.g., the mass matrix is
# a simple diagonal matrix. This may be prescribed in certain cases.
if degree > 1:
from warnings import warn
warn(("Explicitly selected vertex quadrature (degree 1), "
+ "but requested degree is %d.") % degree)
if shape == "tetrahedron":
return (array([[0.0, 0.0, 0.0],
[1.0, 0.0, 0.0],
[0.0, 1.0, 0.0],
[0.0, 0.0, 1.0]]),
array([1.0 / 24.0, 1.0 / 24.0, 1.0 / 24.0, 1.0 / 24.0])
)
elif shape == "triangle":
return (array([[0.0, 0.0],
[1.0, 0.0],
[0.0, 1.0]]),
array([1.0 / 6.0, 1.0 / 6.0, 1.0 / 6.0])
)
elif shape == "interval":
# Trapezoidal rule.
return (array([[0.0],
[1.0]]),
array([1.0 / 2.0, 1.0 / 2.0])
)
quad_rule = FIAT.create_quadrature(reference_cell(shape), degree, scheme)
points = numpy.asarray(quad_rule.get_points())
weights = numpy.asarray(quad_rule.get_weights())
return points, weights
def map_facet_points(points, facet):
"""
Map points from the e (UFC) reference simplex of dimension d - 1
to a given facet on the (UFC) reference simplex of dimension d.
This may be used to transform points tabulated for example on the
2D reference triangle to points on a given facet of the reference
tetrahedron.
"""
# Extract the geometric dimension of the points we want to map
dim = len(points[0]) + 1
# Special case, don't need to map coordinates on vertices
if dim == 1:
return [[(0.0,), (1.0,)][facet]]
# Get the FIAT reference cell for this dimension
fiat_cell = reference_cell(dim)
# Extract vertex coordinates from cell and map of facet index to
# indicent vertex indices
coordinate_dofs = fiat_cell.get_vertices()
facet_vertices = fiat_cell.get_topology()[dim - 1]
# coordinate_dofs = \
# {1: ((0.,), (1.,)),
# 2: ((0., 0.), (1., 0.), (0., 1.)),
# 3: ((0., 0., 0.), (1., 0., 0.),(0., 1., 0.), (0., 0., 1))}
# Facet vertices
# facet_vertices = \
# {2: ((1, 2), (0, 2), (0, 1)),
# 3: ((1, 2, 3), (0, 2, 3), (0, 1, 3), (0, 1, 2))}
# Compute coordinates and map the points
coordinates = [coordinate_dofs[v] for v in facet_vertices[facet]]
new_points = []
for point in points:
w = (1.0 - sum(point),) + tuple(point)
x = tuple(sum([w[i] * array(coordinates[i]) for i in range(len(w))]))
new_points += [x]
return new_points
def _extract_elements(ufl_element, restriction_domain=None):
"Recursively extract un-nested list of (component) elements."
elements = []
if isinstance(ufl_element, ufl.MixedElement):
for sub_element in ufl_element.sub_elements():
elements += _extract_elements(sub_element, restriction_domain)
return elements
# Handle restricted elements since they might be mixed elements too.
if isinstance(ufl_element, ufl.RestrictedElement):
base_element = ufl_element.sub_element()
restriction_domain = ufl_element.restriction_domain()
return _extract_elements(base_element, restriction_domain)
if restriction_domain:
ufl_element = ufl.RestrictedElement(ufl_element, restriction_domain)
elements += [create_element(ufl_element)]
return elements
def _create_restricted_element(ufl_element):
"Create an FFC representation for an UFL RestrictedElement."
if not isinstance(ufl_element, ufl.RestrictedElement):
error("create_restricted_element expects an ufl.RestrictedElement")
base_element = ufl_element.sub_element()
restriction_domain = ufl_element.restriction_domain()
# If simple element -> create RestrictedElement from fiat_element
if isinstance(base_element, ufl.FiniteElement):
element = _create_fiat_element(base_element)
tdim = ufl_element.cell().topological_dimension()
return RestrictedElement(element, _indices(element, restriction_domain, tdim), restriction_domain)
# If restricted mixed element -> convert to mixed restricted element
if isinstance(base_element, ufl.MixedElement):
elements = _extract_elements(base_element, restriction_domain)
return MixedElement(elements)
error("Cannot create restricted element from %s" % str(ufl_element))
def _indices(element, restriction_domain, tdim):
"Extract basis functions indices that correspond to restriction_domain."
# FIXME: The restriction_domain argument in FFC/UFL needs to be re-thought and
# cleaned-up.
# If restriction_domain is "interior", pick basis functions associated with
# cell.
if restriction_domain == "interior":
return element.entity_dofs()[tdim][0]
# Pick basis functions associated with
# the topological degree of the restriction_domain and of all lower
# dimensions.
if restriction_domain == "facet":
rdim = tdim - 1
elif restriction_domain == "face":
rdim = 2
elif restriction_domain == "edge":
rdim = 1
elif restriction_domain == "vertex":
rdim = 0
else:
error("Restriction to domain: %s, is not supported." % repr(restriction_domain))
entity_dofs = element.entity_dofs()
indices = []
for dim in range(rdim + 1):
entities = entity_dofs[dim]
for (entity, index) in sorted_by_key(entities):
indices += index
return indices
# Import FFC module with circular dependency
from ffc.quadratureelement import QuadratureElement
|