The discrete layer#

Binding symbolic components to meshes, as their discrete counterparts.

  • MeshData: one mesh with its mesh tags, and names for its markers. It knows nothing of symbolic domains.

  • DomainMaps: pairs each symbolic domain with a MeshData, checks that they match, and creates the submeshes of subdomains.

  • discretize(): the Discretization of symbolic components on domain maps, holding one DOLFINx function per terminal, shared by all of them. disc[component] gives a component’s discrete counterpart.

  • System: the residual, Jacobian and preconditioner of a symbolic system, compiled and bound to the meshes and functions.

  • DirichletBC: a symbolic boundary condition on the meshes.

  • counterpart(): the discrete counterpart of a symbolic object, with options.

Solving the counterparts is left to sumfem.solvers.

A discrete counterpart has the name of its symbolic one, and is made by indexing a discretization, by counterpart(), or by its own constructor from the discretization and the symbolic object. sumfem exports the symbolic ones.

Meshes#

class sumfem.discrete.MeshData(mesh, tags=mappingproxy({}), markers=mappingproxy({}))[source]#

One mesh with its mesh tags, and names for its markers.

It knows nothing of symbolic domains: DomainMaps pairs domains with meshes. For each entity dimension, a marker name maps to tag values. Create it from a locator function for each dimension and name with from_locators(), from existing mesh tags and the integer tag values of each name with from_meshtags(), e.g. MeshData.from_meshtags(mesh, {facet_tags: {"Inlet": (1,), "Walls": (2, 3)}}), or from the physical groups of a mesh read with dolfinx.io.gmsh with from_gmsh().

mesh#

The mesh.

Type:

dolfinx.mesh.Mesh

tags#

Mesh tags, by entity dimension.

Type:

collections.abc.Mapping[int, dolfinx.mesh.MeshTags]

markers#

The tag values of each named marker, by entity dimension.

Type:

collections.abc.Mapping[int, collections.abc.Mapping[str, tuple[int, …]]]

Raises:

ValueError – If mesh tags are of another mesh, or given for another dimension than their own, or markers for a dimension without mesh tags.

Parameters:
classmethod from_gmsh(data)[source]#

Use the physical groups of a mesh read with dolfinx.io.gmsh.

Each physical group becomes a marker with the group’s name.

Parameters:

data (MeshData) – The mesh, its mesh tags and its physical groups, e.g. from dolfinx.io.gmsh.read_from_msh().

Return type:

MeshData

classmethod from_meshtags(mesh, markers)[source]#

Name the tag values of existing mesh tags.

Each mesh tags is given with the tag values of each of its marker names, e.g. {facet_tags: {"Inlet": (1,), "Walls": (2, 3)}}. Their entity dimension is their own, dim of the mesh tags.

Parameters:
Raises:

ValueError – If two of the mesh tags have the same dimension, or any are of another mesh.

Return type:

MeshData

classmethod from_locators(mesh, locators, boundary=False)[source]#

Mark entities geometrically, as dolfinx.mesh.locate_entities().

Parameters:
  • mesh (Mesh) – The mesh.

  • locators (Mapping[int, Mapping[str, Callable[[ndarray[tuple[Any, ...], dtype[floating]]], ndarray[tuple[Any, ...], dtype[bool]]]]]) – For each entity dimension, a locator per marker name. Each entity may be marked by one of them only, as mesh tags hold one value per entity.

  • boundary (bool) – Mark only entities on the boundary of the mesh, for dimensions below the cell dimension. Use this for boundary markers, so that interior facets touching the boundary are not marked.

Raises:

ValueError – If the locators of two markers of a dimension mark the same entity, on any process. Mark disjoint parts instead, and name unions of them with from_meshtags().

Return type:

MeshData

entities(dim, marker)[source]#

Local indices of the entities of dimension dim with a marker.

Raises:

KeyError – If the mesh has no such marker.

Parameters:
Return type:

ndarray[tuple[Any, …], dtype[int32]]

unmarked_boundary_facets(markers)[source]#

Local indices of the boundary facets that none of the markers covers.

A hyperbolic system needs a boundary flux on every boundary facet, so the markers of its boundary models should leave none.

Parameters:

markers (Iterable[str])

Return type:

ndarray[tuple[Any, …], dtype[int32]]

submesh(dim, marker)[source]#

The submesh of the entities of a dimension with a marker.

The submesh keeps the markers of its entities: the parent’s markers of dimension dim and lower become those of the submesh, e.g. a surface of a 3D mesh has the parent’s facet markers as cell markers and its ridge markers as facet markers.

Returns:

The submesh, and the entity map from its cells to the parent’s entities.

Parameters:
Return type:

tuple[MeshData, EntityMap]

ghost_by_vertex()[source]#

The mesh with every cell ghosted on each process sharing its vertices.

See ghost_by_vertex(). The markers carry over, onto the new ghost entities too. On one process the mesh is returned.

Return type:

MeshData

sumfem.discrete.ghost_by_vertex(mesh)[source]#

The mesh, with every cell ghosted on each process sharing its vertices.

Interior facet integrals on a submesh of facets need both cells of each of its facets on the process, but cells that share only a ridge are not ghosted by default. With this ghost layer, all cells around each owned vertex are on the process. The cells keep their owners. The new mesh has the global node numbers of mesh as its input node indices, see mesh.geometry.input_global_indices.

After J. S. Dokken, https://gist.github.com/jorgensd/1e3d89b8c46a05fce36eb2fe88cfe9a6.

Parameters:

mesh (Mesh)

Return type:

Mesh

Discretizations#

class sumfem.discrete.DomainMaps(meshes, entity_maps=())[source]#

Pairs each symbolic domain with a MeshData, and checks that they match.

The maps also hold the submesh of each subdomain, created with its markers and its entity map from the parent’s mesh when a discretization first needs it, unless a mesh is given for the subdomain, and shared by every discretization on the maps. Integration entities of the markers are computed on first use.

Parameters:
  • meshes (Mapping[Domain | str, MeshLike]) – The mesh of each domain, by the domain or its name. A mesh given for a domain is checked against it here, one given by name when the domain is first used.

  • entity_maps (Iterable[dolfinx.mesh.EntityMap]) – Entity maps between meshes, for forms that couple fields on different meshes.

Raises:

ValueError – If there are no meshes, a domain is given twice, or a mesh does not match its domain.

property entity_maps: list[EntityMap]#

Entity maps between meshes, passed to every form.

property comm: Intracomm#

The MPI communicator of the meshes.

ensure_mesh(domain)[source]#

Make sure a domain has a mesh, creating the submesh of a subdomain if needed.

Collective: a submesh is created on every process together. Use mesh_data() or mesh() to get the mesh, which never create one, and so can be called on any process.

Raises:
  • KeyError – If there is no mesh for a domain that is no subdomain.

  • ValueError – If the mesh does not match the domain.

Parameters:

domain (Domain)

Return type:

None

mesh_data(domain)[source]#

The mesh of a domain, with its markers.

The submesh of a subdomain must have been created by ensure_mesh(), which a discretization does for the domains of what it holds.

Raises:
  • KeyError – If there is no mesh for the domain.

  • ValueError – If the mesh does not match the domain’s cell type, coordinate degree or geometric dimension.

Parameters:

domain (Domain)

Return type:

MeshData

mesh(domain)[source]#

The mesh of a domain.

Parameters:

domain (Domain)

Return type:

Mesh

integration_entities(domain, integral_type, marker)[source]#

Integration entities of a named marker, for a UFL integral type.

The entities are in the flattened layout DOLFINx assembles over, e.g. cells for a cell integral, (cell, local facet) pairs for an exterior facet integral, and (cell, local facet, cell, local facet) for an interior facet integral. The marker is one of:

  • a marker of the domain’s mesh, see MeshData.markers;

  • a one-sided marker "<subdomain>.<marker>" of an exterior facet integral moved from a subdomain to its parent, see one_sided();

  • an interface marker of an interior facet integral between two subdomains, see interface().

They are computed on first use and cached.

Raises:
  • KeyError – If there is no mesh for the domain.

  • ComponentError – If the facets of the marker cannot be integrated over with the integral type, e.g. an exterior facet marker with facets inside the mesh.

Parameters:
Return type:

ndarray[tuple[Any, …], dtype[int32]]

check_interior_facets(domain, marker)[source]#

Raise unless the interior facets of a domain can be integrated over.

Each needs exactly two cells, and both on the process. A submesh of facets can have more, where three or more of its cells meet, which an interior facet integral does not cover. In parallel, the cells of a submesh of codimension one or more that meet at a facet on a process boundary share only a ridge of the parent, so the parent must be ghosted by vertex, see MeshData.ghost_by_vertex().

Parameters:
  • domain (Domain) – The domain.

  • marker (str | None) – A facet marker of the domain, or None for all facets.

Raises:

ComponentError – On every process, if a process has such a facet.

Return type:

None

class sumfem.discrete.Discretization(maps, max_quadrature_degree=None, form_compiler_options=None, jit_options=None)[source]#

The discrete state of symbolic components on domain maps.

Made by discretize(). It holds what its components need: the submesh of each subdomain they use, in the maps, and a function for each field, parameter, known coefficient and previous value in them. These are created when the components are declared, by discretize() or add(). A terminal not held raises, so nothing is created behind a lookup.

Indexing gives discrete counterparts, compiled on first access and kept: see __getitem__(). Every system of a discretization shares its functions and compiler options.

Declaring is collective: every process must declare the same components in the same order, since creating a submesh is.

Parameters:
  • maps (DomainMaps) – The meshes of the domains.

  • max_quadrature_degree (int | None) – Upper bound on the quadrature degree UFL estimates for each integral without an explicit degree, through FFCx’s max_quadrature_degree metadata. Use it to keep nonlinear integrands cheap. None for no bound.

  • form_compiler_options (dict | None) – Options for FFCx.

  • jit_options (dict | None) – Options for the JIT compilation of the generated code.

Raises:

ValueError – If the bound is negative.

add(*components)[source]#

Declare components, creating the submeshes and functions they need.

A component is a residual, functional or Jacobian, a symbolic system, a Dirichlet condition, a transfer, a sequence, a clock, a form, an expression, a single terminal, or a domain, e.g. a subdomain whose submesh is needed. A system holds the terminals a time-stepping scheme introduced, such as previous values and weights, which its residual does not. A parameter starts at its default value. Terminals and submeshes held already are kept.

Raises:
  • KeyError – If there is no mesh for a domain they use.

  • TypeError – For something that is not a component.

Parameters:

components (Domain | Residual | Functional | Jacobian | System | DirichletBC | Transfer | Sequential | Clock | Form | Expr)

Return type:

None

property maps: DomainMaps#

The meshes of the domains.

property max_quadrature_degree: int | None#

Upper bound on estimated quadrature degrees, or None.

property form_compiler_options: dict#

Options for FFCx.

property jit_options: dict#

Options for the JIT compilation of the generated code.

property comm: Intracomm#

The MPI communicator of the meshes.

function_space(field)[source]#

The function space of a field, distinct from that of any other field.

Parameters:

field (Field)

Return type:

FunctionSpace

function(terminal)[source]#

The function of a field, parameter, known coefficient or history value.

Raises:
  • SystemBuildError – For the time-derivative placeholder field.dot, which a time-stepping scheme must replace.

  • KeyError – If the discretization does not hold the terminal.

Parameters:

terminal (Coefficient)

Return type:

Function

property fields: Mapping[str, Function]#

The function of each field held, by field name.

property parameters: Mapping[str, Function]#

The function of each parameter held, by parameter name.

property known_coefficients: Mapping[str, Function]#

The function of each known coefficient held, by name.

terminal(key)[source]#

The symbolic terminal with a name, among those held.

A terminal given as an object is returned as it is.

Raises:

KeyError – If no terminal held has the name.

Parameters:

key (str | Coefficient)

Return type:

Coefficient

set(values)[source]#

Set the values of fields, parameters or known coefficients.

Values are given in the unit of the terminal and stored in SI base units. A sympy quantity with its own units, e.g. 5 * sympy.physics.units.centimeter, is converted from those.

A symbolic UFL expression, of fields, parameters, known coefficients and spatial coordinates, sets a field or known coefficient by interpolation, e.g. an initial condition. It evaluates in SI base units, as every symbolic expression does.

Parameters:

values (Mapping[str | Coefficient, float | Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str] | Function | Expression | Callable[[ndarray[tuple[Any, ...], dtype[floating]]], Buffer | _SupportsArray[dtype[Any]] | _NestedSequence[_SupportsArray[dtype[Any]]] | complex | bytes | str | _NestedSequence[complex | bytes | str]] | Expr | Expr]) – Values by terminal, given as the object or by name. See Value for the accepted values. Parameters take numbers or arrays of their shape.

Raises:
  • ValueError – If a sympy quantity has the wrong dimension, or an expression the wrong shape.

  • TypeError – For an expression given to a parameter.

Return type:

None

compile_expression(expr, space)[source]#

Compile a symbolic expression for interpolation into a space.

The expression is discretized with discretize() and compiled at the interpolation points of the space, with this discretization’s compiler options and entity maps. On a domain whose coordinate system writes vectors in 3D, see coordinates, the expression is evaluated in 3D, and a space of the mesh’s dimension receives its components in the mesh’s coordinates, e.g. those of a stress but its hoop component.

Raises:

ValueError – If the expression’s shape is not the space’s.

Parameters:
Return type:

Expression

value(parameter)[source]#

The current value of a parameter, in the parameter’s unit.

Parameters:

parameter (Parameter | str)

Return type:

ndarray[tuple[Any, …], dtype[Any]]

has_value(parameter)[source]#

Whether a parameter has a value, from its default or set explicitly.

Parameters:

parameter (Parameter | str)

Return type:

bool

check_values(system)[source]#

Raise if a parameter of a system has no value.

Raises:

ValueError – Naming the parameters without a value.

Parameters:

system (System)

Return type:

None

history(field)[source]#

The functions of the field’s previous values held, by k.

Parameters:

field (Field)

Return type:

dict[int, Function]

initialize_history()[source]#

Set every previous value of every field to the field’s current value.

Return type:

None

advance_history()[source]#

Shift the previous values of every field one step back in time.

Afterwards previous(1) holds the current value of the field.

Return type:

None

assemble_scalar(form)[source]#

Assemble a symbolic functional, summed over all processes.

The functional is compiled on first use and kept, so assembling it again, e.g. at every time step, uses the current values of its terminals without compiling it again.

Parameters:

form (Form | Functional) – A form without test or trial functions over one domain, e.g. c * domain.ds("Membrane"), or a functional.

Return type:

float

sumfem.discrete.discretize(components, maps, max_quadrature_degree=None, form_compiler_options=None, jit_options=None)[source]#

The discretization of components on domain maps.

Components that are discretized together share their functions, so declare every component that couples to another here, e.g. a flow and the transport it carries. Discretization.add() declares later additions.

Parameters:
Return type:

Discretization

Example

A flow, and the half-cell it feeds:

disc = discretize([flow, cell.model, *inlet], maps)
Solver(disc[flow], [outlet]).solve()
Simulation(disc, cell.model, inlet, clock=clock, scheme=BDF(1)).run()

Counterparts#

sumfem.discrete.counterpart(symbolic, discretization, **options)[source]#
sumfem.discrete.counterpart(condition, discretization, **options)
sumfem.discrete.counterpart(expr, discretization, **options)
sumfem.discrete.counterpart(system, discretization, **options)
sumfem.discrete.counterpart(residual, discretization, **options)
sumfem.discrete.counterpart(form, discretization)
sumfem.discrete.counterpart(functional, discretization, **options)

The discrete counterpart of a symbolic object on a discretization.

Dispatches on the type of the symbolic object. A discrete counterpart has the name of its symbolic one in sumfem.discrete, e.g. a symbolic System has a System, and a symbolic Interpolation a Interpolation. Expressions give expressions in the discrete functions, and functionals dolfinx.fem.Form.

Indexing a discretization, disc[symbolic], gives the counterpart with default options and keeps it. Call this function for other options, e.g. counterpart(interpolation, disc, padding=1e-10). Register the discrete counterpart of a new symbolic type, e.g. a transfer by projection, with @counterpart.register:

@counterpart.register
def _(projection: Projection, discretization, **options):
    return DiscreteProjection(discretization, projection, **options)
Parameters:
  • symbolic (Any) – The symbolic object.

  • discretization (Discretization) – The discretization holding its terminals.

  • options (Any) – Discrete options of the counterpart, e.g. the point search of an interpolation.

Raises:

NotImplementedError – If no discrete counterpart is registered for the type.

Return type:

Any

class sumfem.discrete.System(discretization, system)[source]#

The residual, Jacobian and preconditioner of a symbolic system, compiled and bound to the meshes and functions of a discretization.

The forms are evaluated at the current values of the functions, so a Solver assembles them again at every Newton iteration. A linear system is the case where one iteration suffices. The forms are compiled with the discretization’s compiler options, and cached per symbolic system and options.

Parameters:
property symbolic: System#

The symbolic system.

property discretization: Discretization#

The discretization owning the meshes and functions.

property residual: Form | list[Form]#

Residual form, or one form per unknown.

property jacobian: Form | list[list[Form | None]]#

Jacobian form, or blocks with None for zero blocks.

property preconditioner: Form | list[list[Form | None]] | None#

Preconditioner forms, or None.

property solution: tuple[Function, ...]#

The function of each unknown, in block order.

class sumfem.discrete.DirichletBC(discretization, condition)[source]#

A DirichletBC discretized on its field’s mesh.

A UFL value is compiled into an expression, evaluated by pack(). Any other value is assigned once.

Parameters:
property symbolic: DirichletBC#

The symbolic condition.

property function: Function#

The function holding the prescribed values.

property dolfinx_bc: DirichletBC#

The DOLFINx boundary condition.

property expression: Expression | None#

The compiled expression of a symbolic value, or None.

pack()[source]#

Evaluate a symbolic value with the current values of its inputs.

Return type:

None