firedrake.adjoint_utils.blocks package

Submodules

firedrake.adjoint_utils.blocks.assembly module

class firedrake.adjoint_utils.blocks.assembly.AssembleBlock(form, ad_block_tag=None)[source]

Bases: Block

adj_state
block_helper
compute_action_adjoint(adj_input, arity_form, form=None, c_rep=None, space=None, dform=None)[source]

This computes the action of the adjoint of the derivative of \(form\) wrt \(c_rep\) on \(adj_input\). In other words, it returns: \(<(dform/dc_rep)*, adj_input>\)

  • If \(form\) has arity 0 => \(dform/dc_rep\) is a 1-form and \(adj_input\) a float, we can simply use the \(*\) operator.

  • If \(form\) has arity 1 => \(dform/dc_rep\) is a 2-form and we can symbolically take its adjoint and then apply the action on \(adj_input\), to finally assemble the result.

evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
prepare_evaluate_adj(inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_hessian(inputs, hessian_inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_tlm(inputs, tlm_inputs, relevant_outputs)[source]
prepare_recompute_component(inputs, relevant_outputs)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag

firedrake.adjoint_utils.blocks.block_utils module

firedrake.adjoint_utils.blocks.block_utils.isconstant(expr)[source]

Check whether expression is constant type. In firedrake this is a function in the real space Ie: \(firedrake.Function(FunctionSpace(mesh, "R"))\)

firedrake.adjoint_utils.blocks.constant module

class firedrake.adjoint_utils.blocks.constant.ConstantAssignBlock(other, ad_block_tag=None)[source]

Bases: Block

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
prepare_evaluate_adj(inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_hessian(inputs, hessian_inputs, adj_inputs, relevant_dependencies)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag
firedrake.adjoint_utils.blocks.constant.constant_from_values(constant, values=None)[source]

Returns a new Constant with \(constant.values()\).

If the optional argument \(values\) is provided, then \(values\) will be the values of the new Constant instead, while still preserving the ufl_shape of \(constant\).

Parameters:
  • constant – A constant with the ufl_shape to preserve. values

  • (numpy.array) – An optional argument to use instead of

  • constant.values().

Returns:

The created Constant of the same type as \(constant\).

Return type:

Constant

firedrake.adjoint_utils.blocks.dirichlet_bc module

class firedrake.adjoint_utils.blocks.dirichlet_bc.DirichletBCBlock(*args, **kwargs)[source]

Bases: Block

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute()[source]
tag
firedrake.adjoint_utils.blocks.dirichlet_bc.extract_bc_subvector(value, Vtarget, bc)[source]

Extract from value (a function in a mixed space), the sub function corresponding to the part of the space bc applies to. Vtarget is the target (collapsed) space.

firedrake.adjoint_utils.blocks.function module

class firedrake.adjoint_utils.blocks.function.CofunctionAssignBlock(lhs, rhs, ad_block_tag=None, rhs_from_assemble=False)[source]

Bases: Block

Class specifically for the case b.assign(a).

All other cofunction assignment operations are annotated via Assemble. In effect this means that this is the annotation of an identity operation.

Parameters:
  • lhs – The target of the assignment.

  • rhs – The cofunction being assigned.

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute_component(inputs, block_variable, idx, prepared=None)[source]

Recompute the assignment.

Parameters:

Notes

Recomputes the block_variable only if the checkpoint was not delegated to another Function.

Returns:

Return either the firedrake cofunction or \(BlockVariable\) checkpoint to which was delegated the checkpointing.

Return type:

Cofunction

tag
class firedrake.adjoint_utils.blocks.function.FunctionAssignBlock(func, other, ad_block_tag=None)[source]

Bases: Block

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
prepare_evaluate_adj(inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_hessian(inputs, hessian_inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_tlm(inputs, tlm_inputs, relevant_outputs)[source]
prepare_recompute_component(inputs, relevant_outputs)[source]
recompute_component(inputs, block_variable, idx, prepared=None)[source]

Recompute the assignment.

Parameters:

Notes

Recomputes the block_variable only if the checkpoint was not delegated to another Function.

Returns:

Return either the firedrake function or \(BlockVariable\) checkpoint to which was delegated the checkpointing.

Return type:

Function

tag
class firedrake.adjoint_utils.blocks.function.FunctionMergeBlock(func, idx, ad_block_tag=None)[source]

Bases: Block

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag
class firedrake.adjoint_utils.blocks.function.SubfunctionBlock(func, idx, ad_block_tag=None)[source]

Bases: Block

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag

firedrake.adjoint_utils.blocks.mesh module

class firedrake.adjoint_utils.blocks.mesh.MeshInputBlock(mesh, ad_block_tag=None)[source]

Bases: Block

Block which links a MeshGeometry to its coordinates, which is a firedrake function.

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, idx, block_variable, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag
class firedrake.adjoint_utils.blocks.mesh.MeshOutputBlock(func, mesh, ad_block_tag=None)[source]

Bases: Block

Block which is called when the coordinates of a mesh are changed.

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, idx, block_variable, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag

firedrake.adjoint_utils.blocks.solving module

class firedrake.adjoint_utils.blocks.solving.GenericSolveBlock(lhs, rhs, func, bcs, *args, **kwargs)[source]

Bases: Block

property adj_sol
adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]
pop_kwargs_keys = ['adj_cb', 'adj_bdy_cb', 'adj2_cb', 'adj2_bdy_cb', 'forward_args', 'forward_kwargs', 'adj_args', 'adj_kwargs']
prepare_evaluate_adj(inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_hessian(inputs, hessian_inputs, adj_inputs, relevant_dependencies)[source]
prepare_evaluate_tlm(inputs, tlm_inputs, relevant_outputs)[source]
prepare_recompute_component(inputs, relevant_outputs)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag
class firedrake.adjoint_utils.blocks.solving.NonlinearVariationalSolveBlock(equation, func, bcs, adj_cache, problem_forms, solver_kwargs, **kwargs)[source]

Bases: GenericSolveBlock

adj_state
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]
prepare_evaluate_adj(inputs, adj_inputs, relevant_dependencies)[source]
recompute_component(inputs, block_variable, idx, prepared)[source]
tag
class firedrake.adjoint_utils.blocks.solving.ProjectBlock(v, V, output, bcs=[], *args, **kwargs)[source]

Bases: SolveVarFormBlock

adj_state
block_helper
tag
class firedrake.adjoint_utils.blocks.solving.SolveLinearSystemBlock(A, u, b, *args, **kwargs)[source]

Bases: GenericSolveBlock

adj_state
block_helper
tag
class firedrake.adjoint_utils.blocks.solving.SolveVarFormBlock(equation, func, bcs=[], *args, **kwargs)[source]

Bases: GenericSolveBlock

adj_state
block_helper
tag
class firedrake.adjoint_utils.blocks.solving.Solver(*values)[source]

Bases: Enum

Enum for solver types.

ADJOINT = 1
FORWARD = 0
class firedrake.adjoint_utils.blocks.solving.SupermeshProjectBlock(source, target_space, target, bcs=[], **kwargs)[source]

Bases: Block

Annotates supermesh projection.

Suppose we have a source space, \(V_A\), and a target space, \(V_B\). Projecting a source from \(V_A\) to \(V_B\) amounts to solving the linear system

\[M_B * v_B = M_{AB} * v_A,\]
where
  • \(M_B\) is the mass matrix on \(V_B\),

  • \(M_{AB}\) is the mixed mass matrix for \(V_A\) and \(V_B\),

  • \(v_A\) and \(v_B\) are vector representations of the source and target Function s.

This can be broken into two steps:

Step 1. form RHS, multiplying the source with the mixed mass matrix;

Step 2. solve linear system.

adj_state
apply_mixedmass(a)[source]
block_helper
evaluate_adj_component(inputs, adj_inputs, block_variable, idx, prepared=None)[source]

Evaluate the adjoint to one output of the block

Recall that the forward propagation can be broken down as:

Step 1. multiply \(w := M_{AB} * v_A\);

Step 2. solve \(M_B * v_B = w\).

For a seed vector \(v_B^{seed}\) from the target space, the adjoint is given by:

Adjoint of step 2. solve \(M_B^T * w = v_B^{seed}\) for \(w\);

Adjoint of step 1. multiply \(v_A^{adj} := M_{AB}^T * w\).

evaluate_hessian_component(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]
evaluate_tlm_component(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]

Given that the input is a \(Function\), we just have a linear operation. As such, the tlm is just the sum of each tlm input projected into the target space.

recompute_component(inputs, block_variable, idx, prepared)[source]
tag
firedrake.adjoint_utils.blocks.solving.extract_subfunction(u, V)[source]

If V is a subspace of the function-space of u, return the component of u that is in that subspace.

firedrake.adjoint_utils.blocks.solving.solve_init_params(self, args, kwargs, varform)[source]

Module contents