1
# Copyright (C) 2011 Marie E. Rognes
3
# This file is part of DOLFIN.
5
# DOLFIN 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.
10
# DOLFIN 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.
15
# You should have received a copy of the GNU Lesser General Public License
16
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
18
# Based on original implementation by Martin Alnes and Anders Logg
20
# Last changed: 2012-11-14
22
from includes import snippets
24
__all__ = ["apply_function_space_template", "extract_coefficient_spaces",
27
#-------------------------------------------------------------------------------
28
def extract_coefficient_spaces(forms):
29
"""Extract a list of tuples
31
(classname, finite_element_classname, dofmap_classname)
33
for the coefficient spaces in the set of given forms. This can
34
then be used for input to the function space template."""
36
# Extract data for each coefficient space
39
for (i, name) in enumerate(form.coefficient_names):
40
# Skip if already considered
44
# Map element name, dof map name etc to this coefficient
45
spaces[name] = ("CoefficientSpace_%s" % name,
46
form.ufc_finite_element_classnames[form.rank + i],
47
form.ufc_dofmap_classnames[form.rank + i])
49
# Return coefficient spaces sorted alphabetically by coefficient
53
return [spaces[name] for name in names]
54
#-------------------------------------------------------------------------------
55
def generate_typedefs(form, classname):
56
"""Generate typedefs for test, trial and coefficient spaces
57
relative to a function space."""
59
# Generate typedef data for test/trial spaces
60
pairs = [("%s_FunctionSpace_%d" % (classname, i),
61
snippets["functionspace"][i]) for i in range(form.rank)]
63
# Generate typedefs for coefficient spaces
64
pairs += [("%s_FunctionSpace_%d" % (classname, form.rank + i),
65
"CoefficientSpace_%s" % form.coefficient_names[i])
66
for i in range(form.num_coefficients)]
68
# Combine data to typedef code
69
code = "\n".join(" typedef %s %s;" % (to, fro) for (to, fro) in pairs)
71
#-------------------------------------------------------------------------------
72
function_space_template = """\
73
class %(classname)s: public dolfin::FunctionSpace
77
//--- Constructors for standard function space, 2 different versions ---
79
// Create standard function space (reference version)
80
%(classname)s(const dolfin::Mesh& mesh):
81
dolfin::FunctionSpace(dolfin::reference_to_no_delete_pointer(mesh),
82
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
83
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()), mesh)))
88
// Create standard function space (shared pointer version)
89
%(classname)s(std::shared_ptr<const dolfin::Mesh> mesh):
90
dolfin::FunctionSpace(mesh,
91
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
92
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()), *mesh)))
97
//--- Constructors for constrained function space, 2 different versions ---
99
// Create standard function space (reference version)
100
%(classname)s(const dolfin::Mesh& mesh, const dolfin::SubDomain& constrained_domain):
101
dolfin::FunctionSpace(dolfin::reference_to_no_delete_pointer(mesh),
102
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
103
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()), mesh,
104
dolfin::reference_to_no_delete_pointer(constrained_domain))))
109
// Create standard function space (shared pointer version)
110
%(classname)s(std::shared_ptr<const dolfin::Mesh> mesh, std::shared_ptr<const dolfin::SubDomain> constrained_domain):
111
dolfin::FunctionSpace(mesh,
112
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
113
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()), *mesh, constrained_domain)))
118
//--- Constructors for restricted function space, 2 different versions ---
120
// Create restricted function space (reference version)
121
%(classname)s(const dolfin::Restriction& restriction):
122
dolfin::FunctionSpace(dolfin::reference_to_no_delete_pointer(restriction.mesh()),
123
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
124
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()),
125
reference_to_no_delete_pointer(restriction))))
130
// Create restricted function space (shared pointer version)
131
%(classname)s(std::shared_ptr<const dolfin::Restriction> restriction):
132
dolfin::FunctionSpace(dolfin::reference_to_no_delete_pointer(restriction->mesh()),
133
std::shared_ptr<const dolfin::FiniteElement>(new dolfin::FiniteElement(std::shared_ptr<ufc::finite_element>(new %(ufc_finite_element_classname)s()))),
134
std::shared_ptr<const dolfin::DofMap>(new dolfin::DofMap(std::shared_ptr<ufc::dofmap>(new %(ufc_dofmap_classname)s()),
147
#-------------------------------------------------------------------------------
148
def apply_function_space_template(name, element_name, dofmap_name):
149
args = {"classname": name,
150
"ufc_finite_element_classname": element_name,
151
"ufc_dofmap_classname": dofmap_name }
152
return function_space_template % args