This repository has been archived by the owner on Feb 21, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merged in miklos1/ffc-elements (pull request #36)
Bring *all* finite elements to FIAT! Approved-by: Jan Blechta <blechta@karlin.mff.cuni.cz>
- Loading branch information
Showing
7 changed files
with
345 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Copyright (C) 2005-2010 Anders Logg | ||
# | ||
# This file is part of FIAT. | ||
# | ||
# FIAT 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. | ||
# | ||
# FIAT 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 FIAT. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
from __future__ import absolute_import, print_function, division | ||
from six.moves import map | ||
|
||
import numpy | ||
|
||
from collections import defaultdict | ||
from operator import add | ||
from functools import partial | ||
|
||
from FIAT.dual_set import DualSet | ||
from FIAT.finite_element import FiniteElement | ||
|
||
|
||
class MixedElement(FiniteElement): | ||
"""A FIAT-like representation of a mixed element. | ||
:arg elements: An iterable of FIAT elements. | ||
:arg ref_el: The reference element (optional). | ||
This object offers tabulation of the concatenated basis function | ||
tables along with an entity_dofs dict.""" | ||
def __init__(self, elements, ref_el=None): | ||
elements = tuple(elements) | ||
|
||
cells = set(e.get_reference_element() for e in elements) | ||
if ref_el is not None: | ||
cells.add(ref_el) | ||
ref_el, = cells | ||
|
||
# These functionals are absolutely wrong, they all map from | ||
# functions of the wrong shape, and potentially of different | ||
# shapes. However, they are wrong precisely as FFC hacks | ||
# expect them to be. :( | ||
nodes = [L for e in elements for L in e.dual_basis()] | ||
|
||
entity_dofs = concatenate_entity_dofs(elements) | ||
|
||
dual = DualSet(nodes, ref_el, entity_dofs) | ||
super(MixedElement, self).__init__(ref_el, dual, None, mapping=None) | ||
self._elements = elements | ||
|
||
def elements(self): | ||
return self._elements | ||
|
||
def num_sub_elements(self): | ||
return len(self._elements) | ||
|
||
def value_shape(self): | ||
return (sum(numpy.prod(e.value_shape(), dtype=int) for e in self.elements()), ) | ||
|
||
def mapping(self): | ||
return [m for e in self._elements for m in e.mapping()] | ||
|
||
def get_nodal_basis(self): | ||
raise NotImplementedError("get_nodal_basis not implemented") | ||
|
||
def tabulate(self, order, points, entity=None): | ||
"""Tabulate a mixed element by appropriately splatting | ||
together the tabulation of the individual elements. | ||
""" | ||
shape = (self.space_dimension(),) + self.value_shape() + (len(points),) | ||
|
||
output = {} | ||
|
||
sub_dims = [0] + list(e.space_dimension() for e in self.elements()) | ||
sub_cmps = [0] + list(numpy.prod(e.value_shape(), dtype=int) | ||
for e in self.elements()) | ||
irange = numpy.cumsum(sub_dims) | ||
crange = numpy.cumsum(sub_cmps) | ||
|
||
for i, e in enumerate(self.elements()): | ||
table = e.tabulate(order, points, entity) | ||
|
||
for d, tab in table.items(): | ||
try: | ||
arr = output[d] | ||
except KeyError: | ||
arr = numpy.zeros(shape, dtype=tab.dtype) | ||
output[d] = arr | ||
|
||
ir = irange[i:i+2] | ||
cr = crange[i:i+2] | ||
tab = tab.reshape(ir[1] - ir[0], cr[1] - cr[0], -1) | ||
arr[slice(*ir), slice(*cr)] = tab | ||
|
||
return output | ||
|
||
def is_nodal(self): | ||
"""True if primal and dual bases are orthogonal.""" | ||
return all(e.is_nodal() for e in self._elements) | ||
|
||
|
||
def concatenate_entity_dofs(elements): | ||
"""Combine the entity_dofs from a list of elements into a combined | ||
entity_dof containing the information for the concatenated DoFs of | ||
all the elements.""" | ||
entity_dofs = defaultdict(partial(defaultdict, list)) | ||
offsets = numpy.cumsum([0] + list(e.space_dimension() | ||
for e in elements), dtype=int) | ||
for i, d in enumerate(e.entity_dofs() for e in elements): | ||
for dim, dofs in d.items(): | ||
for ent, off in dofs.items(): | ||
entity_dofs[dim][ent] += list(map(partial(add, offsets[i]), off)) | ||
return entity_dofs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
# -*- coding: utf-8 -*- | ||
|
||
# Copyright (C) 2007-2016 Kristian B. Oelgaard | ||
# Copyright (C) 2017 Miklós Homolya | ||
# | ||
# This file is part of FIAT. | ||
# | ||
# FIAT 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. | ||
# | ||
# FIAT 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 FIAT. If not, see <http://www.gnu.org/licenses/>. | ||
# | ||
# Modified by Garth N. Wells 2006-2009 | ||
|
||
from __future__ import absolute_import, print_function, division | ||
from six.moves import range | ||
|
||
# Python modules. | ||
import numpy | ||
import six | ||
|
||
# FIAT modules. | ||
from FIAT.dual_set import DualSet | ||
from FIAT.finite_element import FiniteElement | ||
from FIAT.functional import PointEvaluation | ||
|
||
|
||
class QuadratureElement(FiniteElement): | ||
"""A set of quadrature points pretending to be a finite element.""" | ||
|
||
def __init__(self, ref_el, points): | ||
# Create entity dofs. | ||
entity_dofs = {dim: {entity: [] for entity in entities} | ||
for dim, entities in six.iteritems(ref_el.get_topology())} | ||
entity_dofs[ref_el.get_dimension()] = {0: list(range(len(points)))} | ||
|
||
# The dual nodes are PointEvaluations at the quadrature points. | ||
# FIXME: KBO: Check if this gives expected results for code like evaluate_dof. | ||
nodes = [PointEvaluation(ref_el, tuple(point)) for point in points] | ||
|
||
# Construct the dual set | ||
dual = DualSet(nodes, ref_el, entity_dofs) | ||
|
||
super(QuadratureElement, self).__init__(ref_el, dual, order=None) | ||
self._points = points # save the quadrature points | ||
|
||
def value_shape(self): | ||
"The QuadratureElement is scalar valued" | ||
return () | ||
|
||
def tabulate(self, order, points, entity=None): | ||
"""Return the identity matrix of size (num_quad_points, num_quad_points), | ||
in a format that monomialintegration and monomialtabulation understands.""" | ||
|
||
if entity is not None and entity != (self.ref_el.get_dimension(), 0): | ||
raise ValueError('QuadratureElement does not "tabulate" on subentities.') | ||
|
||
# Derivatives are not defined on a QuadratureElement | ||
if order: | ||
raise ValueError("Derivatives are not defined on a QuadratureElement.") | ||
|
||
# Check that incoming points are equal to the quadrature points. | ||
if len(points) != len(self._points) or abs(numpy.array(points) - self._points).max() > 1e-12: | ||
raise AssertionError("Mismatch of quadrature points!") | ||
|
||
# Return the identity matrix of size len(self._points). | ||
values = numpy.eye(len(self._points)) | ||
dim = self.ref_el.get_spatial_dimension() | ||
return {(0,) * dim: values} | ||
|
||
@staticmethod | ||
def is_nodal(): | ||
# No polynomial basis, but still nodal. | ||
return True |
Oops, something went wrong.