graph.dolfinx_helpers

DOLFINx mesh lookup and recorded derivative value helpers.

dolfinx_graph_ad.graph.dolfinx_helpers.add(total: Any, value: Any) → Any

Sum the adjoint values a node receives, without modifying either of them.

This helper is necessary because the adjoint values can be scalars, or distributed PETSc.Vec or nothing at all, and a simple summation would corrupt the data.

Parameters:
  • total (Any) – The values summed so far, a scalar or PETSc.Vec, or None if there are none yet.

  • value (Any) – The next value, of the same kind as total, or None if it vanishes.

Returns:

The sum, a new vector if both values are vectors, or the other value if one of them is None.

Return type:

Any

Raises:

TypeError – If the values cannot be summed.

dolfinx_graph_ad.graph.dolfinx_helpers.constant_mesh(constant) → Mesh

The DOLFINx mesh of a Constant.

An overloaded Constant keeps the mesh it was created on. A Constant of DOLFINx, e.g. the one dolfinx.fem.dirichletbc() creates for an array, reaches it through its UFL domain only, whose cargo is the C++ mesh, which is wrapped without copying or communicating.

Parameters:

constant (dolfinx.fem.Constant) – The Constant.

Returns:

The mesh.

Return type:

dolfinx.mesh.Mesh

dolfinx_graph_ad.graph.dolfinx_helpers.resolve_capture(entry: Any) → Any

Resolve a captured node reference to its snapshot, or return an untracked value.

Parameters:

entry – A weak reference returned by Graph.capture, or an untracked value.

Returns:

The recorded version’s saved value, or the untracked value unchanged.

Raises:
  • ReferenceError – If the node owning a captured value no longer exists.

  • RuntimeError – If the captured version has no saved value.

dolfinx_graph_ad.graph.dolfinx_helpers.scalar(constant, gradient: Vec)

The gradient of a Constant from the vector of its components.

The components are copied to a vector on the communicator of the mesh of the Constant, as the vector they are assembled into lives on the communicator of a temporary real space, which DOLFINx 0.11 frees with that space.

dolfinx_graph_ad.graph.dolfinx_helpers.zeros(like: Vec) → Vec

A zero vector in the layout of another.