~ubuntu-branches/ubuntu/trusty/ffc/trusty

« back to all changes in this revision

Viewing changes to ffc/fem/createelement.py

  • Committer: Bazaar Package Importer
  • Author(s): Johannes Ring
  • Date: 2009-09-25 09:41:39 UTC
  • Revision ID: james.westby@ubuntu.com-20090925094139-nb845p3ffs8j5ike
Tags: upstream-0.7.0
ImportĀ upstreamĀ versionĀ 0.7.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
__author__ = "Kristian B. Oelgaard (k.b.oelgaard@tudelft.nl) and Anders Logg (logg@simula.no)"
 
2
__date__ = "2009-03-06 -- 2009-08-26"
 
3
__copyright__ = "Copyright (C) 2009 Kristian B. Oelgaard and Anders Logg"
 
4
__license__  = "GNU GPL version 3 or any later version"
 
5
 
 
6
# UFL modules
 
7
from ufl import FiniteElement as UFLFiniteElement
 
8
from ufl import MixedElement as UFLMixedElement
 
9
from ufl import ElementRestriction as UFLElementRestriction
 
10
 
 
11
# FFC common modules
 
12
from ffc.common.log import debug, error
 
13
 
 
14
# FFC fem modules
 
15
from finiteelement import FiniteElement as FFCFiniteElement
 
16
from mixedelement import MixedElement as FFCMixedElement
 
17
from quadratureelement import QuadratureElement as FFCQuadratureElement
 
18
 
 
19
# Cache for computed elements
 
20
_cache = {}
 
21
 
 
22
def create_element(ufl_element, domain=None):
 
23
    "Create FFC element (wrapper for FIAT element) from UFL element."
 
24
 
 
25
    # Check cache
 
26
    if ufl_element in _cache:
 
27
        debug("Found element in element cache: " + str(ufl_element))
 
28
        return _cache[ufl_element]
 
29
 
 
30
    # Save the element that we'll use as hash
 
31
    ufl_element_hash = ufl_element
 
32
    if isinstance(ufl_element, UFLElementRestriction):
 
33
        # If we already have a domain, make sure that it is equal to the domain
 
34
        # of the restricted element
 
35
        if domain and domain != ufl_element.domain():
 
36
            error("Domain of restriction is not equal to the domain of the already restricted element. %s %s"\
 
37
                   %(str(domain), str(ufl_element.domain())))
 
38
        # Get element and domain
 
39
        domain = ufl_element.domain()
 
40
        ufl_element = ufl_element.element()
 
41
 
 
42
    # Create equivalent FFC element
 
43
    if isinstance(ufl_element, UFLFiniteElement):
 
44
        # Special handling for quadrature elements
 
45
        if ufl_element.family() == "Quadrature":
 
46
            ffc_element = FFCQuadratureElement(ufl_element.cell().domain(), ufl_element.degree(), domain)
 
47
        else:
 
48
            ffc_element = FFCFiniteElement(ufl_element.family(), ufl_element.cell().domain(), ufl_element.degree(), domain)
 
49
    elif isinstance(ufl_element, UFLMixedElement):
 
50
        sub_elements = [create_element(e, domain) for e in ufl_element.sub_elements()]
 
51
        ffc_element = FFCMixedElement(sub_elements, domain)
 
52
    else:
 
53
        error("Unable to create equivalent FIAT element: %s" % str(ufl_element))
 
54
 
 
55
    # Add element to cache
 
56
    _cache[ufl_element_hash] = ffc_element
 
57
 
 
58
    return ffc_element
 
59