firedrake.adjoint package¶
Submodules¶
firedrake.adjoint.blocks module¶

class
firedrake.adjoint.blocks.
AssembleBlock
(form)[source]¶ Bases:
dolfin_adjoint_common.blocks.assembly.AssembleBlock
,firedrake.adjoint.blocks.Backend

block_helper
¶


class
firedrake.adjoint.blocks.
ConstantAssignBlock
(other)[source]¶ Bases:
dolfin_adjoint_common.blocks.constant.ConstantAssignBlock
,firedrake.adjoint.blocks.Backend

block_helper
¶


class
firedrake.adjoint.blocks.
DirichletBCBlock
(*args, **kwargs)[source]¶ Bases:
dolfin_adjoint_common.blocks.dirichlet_bc.DirichletBCBlock
,firedrake.adjoint.blocks.Backend

block_helper
¶


class
firedrake.adjoint.blocks.
FunctionAssignBlock
(func, other)[source]¶ Bases:
dolfin_adjoint_common.blocks.function.FunctionAssignBlock
,firedrake.adjoint.blocks.Backend

block_helper
¶


class
firedrake.adjoint.blocks.
FunctionMergeBlock
(func, idx)[source]¶ Bases:
pyadjoint.block.Block
,firedrake.adjoint.blocks.Backend

block_helper
¶

evaluate_adj_component
(inputs, adj_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for evaluating the adjoint of the block that corresponds to one dependency. If one considers the adjoint action a vector right multiplied with the Jacobian matrix, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. adj_inputs (list): A list of the adjoint input values, determined by the outputs list. block_variable (BlockVariable): The block variable of the dependency corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_adj method. Default is None.
 Returns:
An object of a type consistent with the adj_value type of block_variable: The resulting product.

evaluate_hessian_component
(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]¶ This method must be overridden.
The method should implement a routine for evaluating the hessian of the block. It is preferable that a “ForwardoverReverse” scheme is used. Thus the hessians are evaluated in reverse (starting with the last block on the tape).

evaluate_tlm
()[source]¶ Computes the tangent linear action and stores the result in the tlm_value attribute of the outputs.
This method will by default call the evaluate_tlm_component method for each output.
 Args:
 markings (bool): If True, then each block_variable will have set marked_in_path attribute indicating
whether their tlm components are relevant for computing the final target tlm values. Default is False.

recompute
()[source]¶  Recomputes the overloaded function with new inputs
and stores the results in the checkpoint attribute of the outputs.
This method will by default call the recompute_component method for each output.
 Args:
 markings (bool): If True, then each block_variable will have set marked_in_path attribute indicating
whether their checkpoints need to be recomputed for recomputing the final target function value. Default is False.


class
firedrake.adjoint.blocks.
FunctionSplitBlock
(func, idx)[source]¶ Bases:
pyadjoint.block.Block
,firedrake.adjoint.blocks.Backend

block_helper
¶

evaluate_adj_component
(inputs, adj_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for evaluating the adjoint of the block that corresponds to one dependency. If one considers the adjoint action a vector right multiplied with the Jacobian matrix, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. adj_inputs (list): A list of the adjoint input values, determined by the outputs list. block_variable (BlockVariable): The block variable of the dependency corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_adj method. Default is None.
 Returns:
An object of a type consistent with the adj_value type of block_variable: The resulting product.

evaluate_hessian_component
(inputs, hessian_inputs, adj_inputs, block_variable, idx, relevant_dependencies, prepared=None)[source]¶ This method must be overridden.
The method should implement a routine for evaluating the hessian of the block. It is preferable that a “ForwardoverReverse” scheme is used. Thus the hessians are evaluated in reverse (starting with the last block on the tape).

evaluate_tlm_component
(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for computing the tangent linear model of the block that corresponds to one output. If one considers the tangent linear action as a Jacobian matrix multiplied with a vector, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. tlm_inputs (list): A list of the tlm input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_tlm method. Default is None.
 Returns:
An object of the same type as block_variable.saved_output: The resulting product.

recompute_component
(inputs, block_variable, idx, prepared)[source]¶ This method must be overridden.
The method should implement a routine for recomputing one output of the block in the forward computations. The output to recompute is determined by the idx argument, which corresponds to the index of the output in the outputs list. If the block only has a single output, then idx will always be 0.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the output to compute. prepared (object): Anything returned by the prepare_recompute_component method. Default is None.
 Returns:
An object of the same type as block_variable.checkpoint which is determined by OverloadedType._ad_create_checkpoint (often the same as block_variable.saved_output): The new output.


class
firedrake.adjoint.blocks.
GenericSolveBlock
(lhs, rhs, func, bcs, *args, **kwargs)[source]¶ Bases:
dolfin_adjoint_common.blocks.solving.GenericSolveBlock
,firedrake.adjoint.blocks.Backend

block_helper
¶


class
firedrake.adjoint.blocks.
InterpolateBlock
(interpolator, *functions, **kwargs)[source]¶ Bases:
pyadjoint.block.Block
,firedrake.adjoint.blocks.Backend

block_helper
¶

evaluate_adj_component
(inputs, adj_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for evaluating the adjoint of the block that corresponds to one dependency. If one considers the adjoint action a vector right multiplied with the Jacobian matrix, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. adj_inputs (list): A list of the adjoint input values, determined by the outputs list. block_variable (BlockVariable): The block variable of the dependency corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_adj method. Default is None.
 Returns:
An object of a type consistent with the adj_value type of block_variable: The resulting product.

evaluate_tlm_component
(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for computing the tangent linear model of the block that corresponds to one output. If one considers the tangent linear action as a Jacobian matrix multiplied with a vector, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. tlm_inputs (list): A list of the tlm input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_tlm method. Default is None.
 Returns:
An object of the same type as block_variable.saved_output: The resulting product.

prepare_evaluate_adj
(inputs, adj_inputs, relevant_outputs)[source]¶ Runs preparations before evalute_adj_component is ran.
The return value is supplied to each of the subsequent evaluate_adj_component calls. This method is intended to be overridden for blocks that require such preparations, by default there is none.
 Args:
inputs: The values of the inputs adj_inputs: The adjoint inputs relevant_dependencies: A list of the relevant block variables for evaluate_adj_component.
 Returns:
Anything. The returned value is supplied to evaluate_adj_component

prepare_evaluate_tlm
(inputs, tlm_inputs, relevant_outputs)[source]¶ Runs preparations before evalute_tlm_component is ran.
The return value is supplied to each of the subsequent evaluate_tlm_component calls. This method is intended to be overridden for blocks that require such preparations, by default there is none.
 Args:
inputs: The values of the inputs tlm_inputs: The tlm inputs relevant_outputs: A list of the relevant block variables for evaluate_tlm_component.
 Returns:
Anything. The returned value is supplied to evaluate_tlm_component

prepare_recompute_component
(inputs, relevant_outputs)[source]¶ Runs preparations before recompute_component is ran.
The return value is supplied to each of the subsequent recompute_component calls. This method is intended to be overridden for blocks that require such preparations, by default there is none.
 Args:
inputs: The values of the inputs relevant_outputs: A list of the relevant block variables for recompute_component.
 Returns:
Anything. The returned value is supplied to recompute_component

recompute_component
(inputs, block_variable, idx, prepared)[source]¶ This method must be overridden.
The method should implement a routine for recomputing one output of the block in the forward computations. The output to recompute is determined by the idx argument, which corresponds to the index of the output in the outputs list. If the block only has a single output, then idx will always be 0.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the output to compute. prepared (object): Anything returned by the prepare_recompute_component method. Default is None.
 Returns:
An object of the same type as block_variable.checkpoint which is determined by OverloadedType._ad_create_checkpoint (often the same as block_variable.saved_output): The new output.


class
firedrake.adjoint.blocks.
MeshInputBlock
(mesh)[source]¶ Bases:
pyadjoint.block.Block
Block which links a MeshGeometry to its coordinates, which is a firedrake function.

block_helper
¶

evaluate_adj_component
(inputs, adj_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for evaluating the adjoint of the block that corresponds to one dependency. If one considers the adjoint action a vector right multiplied with the Jacobian matrix, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. adj_inputs (list): A list of the adjoint input values, determined by the outputs list. block_variable (BlockVariable): The block variable of the dependency corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_adj method. Default is None.
 Returns:
An object of a type consistent with the adj_value type of block_variable: The resulting product.

evaluate_hessian_component
(inputs, hessian_inputs, adj_inputs, idx, block_variable, relevant_dependencies, prepared=None)[source]¶ This method must be overridden.
The method should implement a routine for evaluating the hessian of the block. It is preferable that a “ForwardoverReverse” scheme is used. Thus the hessians are evaluated in reverse (starting with the last block on the tape).

evaluate_tlm_component
(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for computing the tangent linear model of the block that corresponds to one output. If one considers the tangent linear action as a Jacobian matrix multiplied with a vector, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. tlm_inputs (list): A list of the tlm input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_tlm method. Default is None.
 Returns:
An object of the same type as block_variable.saved_output: The resulting product.

recompute_component
(inputs, block_variable, idx, prepared)[source]¶ This method must be overridden.
The method should implement a routine for recomputing one output of the block in the forward computations. The output to recompute is determined by the idx argument, which corresponds to the index of the output in the outputs list. If the block only has a single output, then idx will always be 0.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the output to compute. prepared (object): Anything returned by the prepare_recompute_component method. Default is None.
 Returns:
An object of the same type as block_variable.checkpoint which is determined by OverloadedType._ad_create_checkpoint (often the same as block_variable.saved_output): The new output.


class
firedrake.adjoint.blocks.
MeshOutputBlock
(func, mesh)[source]¶ Bases:
pyadjoint.block.Block
Block which is called when the coordinates of a mesh are changed.

block_helper
¶

evaluate_adj_component
(inputs, adj_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for evaluating the adjoint of the block that corresponds to one dependency. If one considers the adjoint action a vector right multiplied with the Jacobian matrix, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. adj_inputs (list): A list of the adjoint input values, determined by the outputs list. block_variable (BlockVariable): The block variable of the dependency corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_adj method. Default is None.
 Returns:
An object of a type consistent with the adj_value type of block_variable: The resulting product.

evaluate_hessian_component
(inputs, hessian_inputs, adj_inputs, idx, block_variable, relevant_dependencies, prepared=None)[source]¶ This method must be overridden.
The method should implement a routine for evaluating the hessian of the block. It is preferable that a “ForwardoverReverse” scheme is used. Thus the hessians are evaluated in reverse (starting with the last block on the tape).

evaluate_tlm_component
(inputs, tlm_inputs, block_variable, idx, prepared=None)[source]¶ This method should be overridden.
The method should implement a routine for computing the tangent linear model of the block that corresponds to one output. If one considers the tangent linear action as a Jacobian matrix multiplied with a vector, then this method should return one entry in the resulting product, where the entry returned is decided by the argument idx.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. tlm_inputs (list): A list of the tlm input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the component to compute. prepared (object): Anything returned by the prepare_evaluate_tlm method. Default is None.
 Returns:
An object of the same type as block_variable.saved_output: The resulting product.

recompute_component
(inputs, block_variable, idx, prepared)[source]¶ This method must be overridden.
The method should implement a routine for recomputing one output of the block in the forward computations. The output to recompute is determined by the idx argument, which corresponds to the index of the output in the outputs list. If the block only has a single output, then idx will always be 0.
 Args:
inputs (list): A list of the saved input values, determined by the dependencies list. block_variable (BlockVariable): The block variable of the output corresponding to index idx. idx (int): The index of the output to compute. prepared (object): Anything returned by the prepare_recompute_component method. Default is None.
 Returns:
An object of the same type as block_variable.checkpoint which is determined by OverloadedType._ad_create_checkpoint (often the same as block_variable.saved_output): The new output.


class
firedrake.adjoint.blocks.
NonlinearVariationalSolveBlock
(equation, func, bcs, problem_J, solver_params, solver_kwargs, **kwargs)[source]¶ Bases:
firedrake.adjoint.blocks.GenericSolveBlock

block_helper
¶


class
firedrake.adjoint.blocks.
ProjectBlock
(v, V, output, bcs=[], *args, **kwargs)[source]¶ Bases:
firedrake.adjoint.blocks.SolveVarFormBlock

block_helper
¶


class
firedrake.adjoint.blocks.
SolveLinearSystemBlock
(A, u, b, *args, **kwargs)[source]¶ Bases:
firedrake.adjoint.blocks.GenericSolveBlock

block_helper
¶


class
firedrake.adjoint.blocks.
SolveVarFormBlock
(equation, func, bcs=[], *args, **kwargs)[source]¶ Bases:
firedrake.adjoint.blocks.GenericSolveBlock

block_helper
¶

firedrake.adjoint.constant module¶

class
firedrake.adjoint.constant.
ConstantMixin
(*args, **kwargs)[source]¶ Bases:
pyadjoint.overloaded_type.OverloadedType
firedrake.adjoint.dirichletbc module¶
firedrake.adjoint.function module¶
firedrake.adjoint.interpolate module¶
firedrake.adjoint.mesh module¶
firedrake.adjoint.projection module¶
firedrake.adjoint.solving module¶

firedrake.adjoint.solving.
annotate_solve
(solve)[source]¶ This solve routine wraps the Firedrake
solve()
call. Its purpose is to annotate the model, recording what solves occur and what forms are involved, so that the adjoint and tangent linear models may be constructed automatically by pyadjoint.To disable the annotation, just pass
annotate=False
to this routine, and it acts exactly like the Firedrake solve call. This is useful in cases where the solve is known to be irrelevant or diagnostic for the purposes of the adjoint computation (such as projecting fields to other function spaces for the purposes of visualisation).The overloaded solve takes optional callback functions to extract adjoint solutions. All of the callback functions follow the same signature, taking a single argument of type Function.
 Keyword Args:
 adj_cb (function, optional): callback function supplying the adjoint solution in the interior.
The boundary values are zero.
 adj_bdy_cb (function, optional): callback function supplying the adjoint solution on the boundary.
The interior values are not guaranteed to be zero.
 adj2_cb (function, optional): callback function supplying the secondorder adjoint solution in the interior.
The boundary values are zero.
 adj2_bdy_cb (function, optional): callback function supplying the secondorder adjoint solution on
the boundary. The interior values are not guaranteed to be zero.