dolfinx_adjoint.graph module

class dolfinx_adjoint.graph.Graph

Bases: object

Class for the computational graph

The computational graph is a directed acyclic graph (DAG) that represents the operations, objects and dependencies in a forward simulation in DOLFINx.

nodes

List of nodes in the graph

Type:

list

edes

List of edges in the graph

Type:

list

Example

The graph object can be initialised and the default nodes and edges can be added as follows: >>> graph = Graph() >>> node1 = Node(object_1, name = “Node 1”) >>> node2 = Node(object_2, name = “Node 2”) >>> edge = Edge(node1, node2) >>> graph.add_node(node1) >>> graph.add_node(node2) >>> graph.add_edge(edge)

The derivatives can then be caculated using the backpropagation method: >>> graph.backprop(object_2, object_1)

add_edge(edge: Edge)

Add an edge to the graph

Parameters:

edge (Edge) – The edge to be added to the graph

add_node(node: Node)

Add a node to the graph

Parameters:

node (Node) – The node to be added to the graph

backprop(function_id: int, variable_id=None)

Perform backpropagation in the graph

Parameters:
  • function_id (int) – The id of the function to be differentiated

  • variable_id (int, optional) – The id of the variable with respect to which the differentiation is performed. Defaults to None. If None, the differentiation is performed with respect to all variables in the graph.

get_edge(predecessor: Node, successor: Node)

Get an edge from the graph

Parameters:
  • predecessor (Node) – The predecessor node of the edge

  • successor (Node) – The successor node of the edge

Returns:

The edge with the given predecessor and successor

Return type:

Edge

get_node(id: int, version=None)

Get a node from the graph

Parameters:
  • id (int) – The python-id of the node to be retrieved

  • version (int, optional) – The version of the node to be retrieved. Defaults to None.

Returns:

The node with the given id and version

Return type:

Node

get_path(start_id: int, end_id: int)

Get the path from the start node to the end node by marking the edges

Parameters:
  • start_id (int) – The id of the start node

  • end_id (int) – The id of the end node

print(detailed=False) None

Print the graph structure

Parameters:

detailed (bool, optional) – Whether to print the graph in detailed mode. Defaults to False.

recalculate()

Recalculate the graph

reset_grads()

Reset the gradients in the graph

to_networkx() DiGraph

Convert the graph to a networkx graph

Returns:

The networkx graph representation of the graph

Return type:

nx_graph (nx.DiGraph)

visualise(filename='graph.pdf', style='planar', print_edge_labels=True)