/usr/share/pyshared/dolfin/functions/functionspace.py is in python-dolfin 1.0.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 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | """
This module contains functionality for function spaces in particular
discrete function spaces defined over meshes in terms of finite
elements.
"""
# Copyright (C) 2008 Johan Hake
#
# This file is part of DOLFIN.
#
# DOLFIN 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.
#
# DOLFIN 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 DOLFIN. If not, see <http://www.gnu.org/licenses/>.
#
# Modified by Anders Logg 2008
# Modified by Martin Alnes 2008
# Modified by Kent-Andre Mardal 2009
#
# First added: 2008-11-03
# Last changed: 2011-11-15
__all__ = ["FunctionSpace", "MixedFunctionSpace",
"VectorFunctionSpace", "TensorFunctionSpace",
"FunctionSpaceBase","FunctionSpaceFromCpp",
"create_ufc_function_spaces"]
import copy
# Import UFL and SWIG-generated extension module (DOLFIN C++)
import ufl
import dolfin.cpp as cpp
from dolfin.compilemodules.jit import jit
# Mapping from dimension to domain
dim2domain = {1: "interval", 2: "triangle", 3: "tetrahedron"}
class FunctionSpaceBase(cpp.FunctionSpace):
"Base class for all function spaces."
def __init__(self, mesh, element):
"""Create function space on given mesh for given finite element.
*Arguments*
mesh
A :py:class:`Mesh <dolfin.cpp.Mesh>`
element
A :py:class:`(UFL) FiniteElement <ufl.FiniteElementBase>`
"""
# Check arguments
if not isinstance(mesh, cpp.Mesh):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument, not a mesh: " + str(mesh))
if not isinstance(element, (ufl.FiniteElementBase)):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument, not a finite element: " + str(element))
# Store element
# Note: self._element cannot be a private attribute as we want to be able to
# set the element from a derived class.
self._ufl_element = element
# JIT-compile element to get ufc_element and ufc_dofmap
ufc_element, ufc_dofmap = jit(self._ufl_element)
# Instantiate DOLFIN FiniteElement and DofMap
self._dolfin_element = cpp.FiniteElement(ufc_element)
dolfin_dofmap = cpp.DofMap(ufc_dofmap, mesh)
# Initialize the cpp_FunctionSpace
cpp.FunctionSpace.__init__(self, mesh,
self._dolfin_element,
dolfin_dofmap)
# FIXME: Sort out consistent interface for access to DOLFIN, UFL and UFC objects...
def cell(self):
"Return the ufl cell."
return self._ufl_element.cell()
def ufl_element(self):
"Return the UFL element."
return self._ufl_element
def dolfin_element(self):
"Return the DOLFIN element."
return self._dolfin_element
def __add__(self, other):
"Create enriched function space."
return EnrichedFunctionSpace((self, other))
def __mul__(self, other):
"Create mixed function space."
return MixedFunctionSpace((self, other))
def __str__(self):
"Pretty-print."
return "<Function space of dimension %d (%s)>" % \
(self.dofmap().global_dimension(), str(self._ufl_element))
def num_sub_spaces(self):
"Return the number of sub spaces"
return self._dolfin_element.num_sub_elements()
def sub(self, i):
"Return the i:th cpp.SubSpace"
# Fixme: Should we have a more extensive check other than whats included in
# the cpp code?
#if i not in self._cpp_sub_spaces.keys():
# Store the created subspace to prevent swig garbage collection
# Should not be needed as SubSpace is shared_ptr stored
# self._cpp_sub_spaces[i] = FunctionSpaceFromCpp(cpp.SubSpace(self,i))
if not isinstance(i, int):
raise TypeError, "expected an int for 'i'"
if self.num_sub_spaces() == 1:
raise ValueError, "no SubSpaces to extract"
if i >= self.num_sub_spaces():
raise ValueError, "Can only extract SubSpaces with i = 0 ... %d" % \
(self.num_sub_spaces() - 1)
assert(hasattr(self._ufl_element,"sub_elements"))
element = self._ufl_element.sub_elements()[i]
return FunctionSpaceFromCpp(cpp.SubSpace(self,i), element)
def split(self):
"""
Split a mixed functionspace into its sub spaces
"""
S = []
for i in range(self.num_sub_spaces()):
S.append(self.sub(i))
return S
def collapse(self, collapsed_dofs=False):
"""
Collapse a subspace and return a new function space and a map
from new to old dofs
*Arguments*
collapsed_dofs (bool)
Return the map from new to old dofs
*Returns*
_FunctionSpace_
The new function space.
dict
The map from new to old dofs (optional)
"""
cpp_space, dofs = cpp.FunctionSpace.collapse(self)
if collapsed_dofs:
return FunctionSpaceFromCpp(cpp_space), dofs
return FunctionSpaceFromCpp(cpp_space)
class FunctionSpaceFromCpp(FunctionSpaceBase):
" FunctionSpace represents a finite element function space."
def __init__(self, cppV, element=None):
""" Initialize a FunctionSpace from an already excisting
cpp.FunctionSpace.
"""
if not isinstance(cppV, (cpp.FunctionSpace)):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument, not a cpp.FunctionSpace: " + str(cppV))
if element is None:
# Get the ufl.FiniteElement from the compiled element sigature
self._ufl_element = eval(cppV.element().signature(), ufl.__dict__)
else:
if not isinstance(element, ufl.FiniteElementBase):
raise TypeError, "expected a ufl.FiniteElementBase"
self._ufl_element = element
# Init the cpp.FunctionSpace with the provided cppV
cpp.FunctionSpace.__init__(self, cppV)
# Assign the dolfin element
self._dolfin_element = cpp.FunctionSpace.element(self)
def create_ufc_function_spaces(mesh, ufc_form, cache=None):
"""
Instantiate cpp.FunctionSpaces from compiled ufc form.
*Arguments*
mesh
a :py:class:`Mesh <dolfin.cpp.Mesh>`.
ufc_form
compiled ufc form
cache
a 'dict' with already instantiated
:py:class:`cpp.FunctionSpaces <dolfin.cpp.FunctionSpace>`.
*Examples of usage*
.. code-block:: python
fs0, c = create_ufc_function_spaces(mesh, ufc_form0)
fs1, c = create_ufc_function_spaces(mesh, ufc_form1, c)
"""
# Initialize return arguments
functionspaces = []
if cache is None:
cache = {}
# Iterate over all known ufc_finite_elements
for i in range(ufc_form.rank() + ufc_form.num_coefficients()):
# Create a ufc_finite_elements
fe = ufc_form.create_finite_element(i)
# Use the signature of the element as key in the cache dict
fesig = fe.signature()
# Try to access the cache
V = cache.get(fesig)
# If the cpp.FunctionSpace does not excists in the cache
if V is None:
# Instantiate a dofmap
dm = ufc_form.create_dofmap(i)
# Instantiate the UFCFunctionSpace
V = UFCFunctionSpace(mesh, fe, dm)
cache[fesig] = V
functionspaces.append(V)
return functionspaces, cache
class UFCFunctionSpace(cpp.FunctionSpace):
"FunctionSpace represents a finite element function space."
def __init__(self, mesh, ufc_finite_element, ufc_dofmap):
" Initialize a FunctionSpace from ufc data "
self._mesh = mesh
self._finite_element = cpp.FiniteElement(ufc_finite_element)
self._dofmap = cpp.DofMap(ufc_dofmap, mesh)
self._ufc_finite_element = ufc_finite_element
self._ufc_dofmap = ufc_dofmap
cpp.FunctionSpace.__init__(self, self._mesh, self._finite_element, \
self._dofmap)
class FunctionSpace(FunctionSpaceBase):
"FunctionSpace represents a finite element function space."
def __init__(self, mesh, family, degree, form_degree=None,
restriction=None):
"""
Create finite element function space.
*Arguments*
mesh (:py:class:`Mesh <dolfin.cpp.Mesh>`)
the mesh
family (string)
specification of the element family, see below for
alternatives.
degree (int)
the degree of the element.
form_degree (int)
the form degree (FEEC notation, used when field is
viewed as k-form)
restriction
restriction of the element (e.g. to cell facets).
Which families and degrees that are supported is determined by the
form compiler used to generate the element, but typical families
include
================================= =========
Name Usage
================================= =========
Argyris* "ARG"
Arnold-Winther* "AW"
Brezzi-Douglas-Fortin-Marini* "BDFM"
Brezzi-Douglas-Marini "BDM"
Bubble "B"
Crouzeix-Raviart "CR"
Discontinuous Lagrange "DG"
Hermite* "HER"
Lagrange "CG"
Mardal-Tai-Winther* "MTW"
Morley* "MOR"
Nedelec 1st kind H(curl) "N1curl"
Nedelec 2nd kind H(curl) "N2curl"
Quadrature "Q"
Raviart-Thomas "RT"
================================= =========
*only partly supported.
*Examples of usage*
To define a discrete function space over e.g. the unit square:
.. code-block:: python
mesh = UnitSquare(32,32)
V = FunctionSpace(mesh, "CG", 1)
Here, ``"CG"`` stands for Continuous Galerkin, implying the
standard Lagrange family of elements. Instead of ``"CG"``, we
could have written ``"Lagrange"``. With degree 1, we get the
linear Lagrange element. Other examples include:
.. code-block:: python
# Discontinuous element of degree 0
V = FunctionSpace(mesh, "DG", 0)
# Brezzi-Douglas-Marini element of degree 2
W = FunctionSpace(mesh, "BDM", 2)
"""
# Check arguments
if not isinstance(mesh, cpp.Mesh):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument, not a mesh: " + str(mesh))
if not isinstance(family, str):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument for finite element family, not a string: " + str(family))
if not isinstance(degree, int):
cpp.dolfin_error("functionspace.py",
"create function space",
"Illegal argument for degree, not an integer: " + str(degree))
dim = mesh.topology().dim()
if not dim in dim2domain:
cpp.dolfin_error("functionspace.py",
"create function space",
"Invalid mesh dimension (%s)" % str(dim))
# Create element
domain = dim2domain[mesh.topology().dim()]
element = ufl.FiniteElement(family, domain, degree,
form_degree=form_degree)
if restriction is not None:
element = element[restriction]
# Initialize base class
FunctionSpaceBase.__init__(self, mesh, element)
self.___degree = degree
self.___family = family
self.___mesh = mesh
self.___form_degree = form_degree
def restriction(self, meshfunction):
space = FunctionSpace(self.___mesh, self.___family, self.___degree,
form_degree=self.___form_degree)
space.attach(meshfunction)
return space
class EnrichedFunctionSpace(FunctionSpaceBase):
"EnrichedFunctionSpace represents an enriched finite element function space."
def __init__(self, spaces):
"""
Create enriched finite element function space.
*Arguments*
spaces
a list (or tuple) of :py:class:`FunctionSpaces
<dolfin.functions.functionspace.FunctionSpace>`.
*Usage*
The function space may be created by
.. code-block:: python
V = EnrichedFunctionSpace(spaces)
"""
# Check arguments
if not len(spaces) > 0:
cpp.dolfin_error("functionspace.py",
"create enriched function space",
"Need at least one subspace")
if not all(isinstance(V, FunctionSpaceBase) for V in spaces):
cpp.dolfin_error("functionspace.py",
"create enriched function space",
"Invalid subspaces: " + str(spaces))
#if not all(V.mesh() == spaces[0].mesh() for V in spaces):
# cpp.dolfin_error("functionspace.py",
# "Nonmatching meshes for mixed function space: " + str([V.mesh() for V in spaces]))
# Create element
element = ufl.EnrichedElement(*[V.ufl_element() for V in spaces])
# Initialize base class
FunctionSpaceBase.__init__(self, spaces[0].mesh(), element)
class MixedFunctionSpace(FunctionSpaceBase):
"MixedFunctionSpace represents a mixed finite element function space."
def __init__(self, spaces):
"""
Create mixed finite element function space.
*Arguments*
spaces
a list (or tuple) of :py:class:`FunctionSpaces
<dolfin.functions.functionspace.FunctionSpace>`.
*Examples of usage*
The function space may be created by
.. code-block:: python
V = MixedFunctionSpace(spaces)
``spaces`` may consist of multiple occurances of the same space:
.. code-block:: python
P1 = FunctionSpace(mesh, "CG", 1)
P2v = VectorFunctionSpace(mesh, "Lagrange", 2)
ME = MixedFunctionSpace([P2v, P1, P1, P1])
"""
# Check arguments
if not len(spaces) > 0:
cpp.dolfin_error("functionspace.py",
"create mixed function space",
"Need at least one subspace")
if not all(isinstance(V, FunctionSpaceBase) for V in spaces):
cpp.dolfin_error("functionspace.py",
"create mixed function space",
"Invalid subspaces: " + str(spaces))
#if not all(V.mesh() == spaces[0].mesh() for V in spaces):
# cpp.dolfin_error("functionspace.py", "Nonmatching meshes for mixed function space: " \
# + str([V.mesh() for V in spaces]))
# Create element
element = ufl.MixedElement(*[V.ufl_element() for V in spaces])
# Initialize base class
FunctionSpaceBase.__init__(self, spaces[0].mesh(), element)
class VectorFunctionSpace(MixedFunctionSpace):
"VectorFunctionSpace represents a vector-valued finite element function space."
def __init__(self, mesh, family, degree, dim=None, form_degree=None,
restriction=None):
"""
Create vector-valued finite element function space.
Use VectorFunctionSpace if the unknown is a vector field,
instead of a :py:class:`FunctionSpace
<dolfin.functions.functionspace.FunctionSpace>` object for
scalar fields.
*Arguments*
mesh (:py:class:`Mesh <dolfin.cpp.Mesh>`)
the mesh
family (string)
a string specifying the element family, see
:py:class:`FunctionSpace
<dolfin.functions.functionspace.FunctionSpace>` for
alternatives.
degree (int)
the (polynomial) degree of the element.
dim (int)
an optional argument specifying the number of components.
form_degree (int)
an optional argument specifying the degree of the
k-form (used for FEEC notation)
If the dim argument is not provided, the dimension will be deduced from
the dimension of the mesh.
*Example of usage*
.. code-block:: python
V = VectorFunctionSpace(mesh, "CG", 1)
"""
# Create element
dim = dim or mesh.geometry().dim()
cell = dim2domain[mesh.topology().dim()]
element = ufl.VectorElement(family, cell, degree, dim=dim,
form_degree=form_degree)
if restriction is not None:
element = element[restriction]
# Initialize base class
FunctionSpaceBase.__init__(self, mesh, element)
class TensorFunctionSpace(FunctionSpaceBase):
"TensorFunctionSpace represents a tensor-valued finite element function space."
def __init__(self, mesh, family, degree, shape=None, symmetry=None, \
restriction=None):
"""
Create tensor-valued finite element function space.
*Arguments*
mesh
a :py:class:`Mesh <dolfin.cpp.Mesh>`.
family
a string specifying the element family,
see :py:class:`FunctionSpace
<dolfin.functions.functionspace.FunctionSpace>`
for alternatives.
degree
the degree of the element.
shape
an optional argument specifying the shape of the tensor.
symmetry
optional argument specifying whether the tensor is symmetric.
If the shape argument is not provided, the dimension will be deduced from
the dimension of the mesh.
*Example of usage*
.. code-block:: python
V = TensorFunctionSpace(mesh, "CG", 1)
"""
# Create subspaces
if shape == None:
dim = mesh.topology().dim()
shape = (dim,dim)
cell = dim2domain[mesh.topology().dim()]
element = ufl.TensorElement(family, cell, degree, shape, symmetry)
if restriction is not None:
element = element[restriction]
# Initialize base class
FunctionSpaceBase.__init__(self, mesh, element)
|