\chapter{A basic UFC-based assembler}
\label{app:assembly}

Below, we include a sketch of a UFC-based implementation of the assembly
of the global tensor~$A$ by summing the local contributions from all
cells. The contributions from all exterior and interior facets may be
computed similarly.

The implementation is incomplete and system specific details such as
interaction with mesh and linear algebra libraries have been
omitted.\footnote{For an example of a complete implementation of a
UFC-based assembler, we refer to the source code of
DOLFIN~\cite{www:dolfin}, in particular class \texttt{Assembler} as
implemented in \texttt{Assembler.cpp}.}

\scriptsize
\begin{code}
void assemble(..., ufc::form& form, ...)
{
  ...

  // Initialize mesh data structure
  ufc::mesh mesh;
  mesh.num_entities = new unsigned int[...];
  ...

  // Initialize cell data structure
  ufc::cell cell;
  cell.entity_indices = new unsigned int[...];
  cell.coordinates = new double[...];
  ...

  // Create cell integrals
  ufc::cell_integral** cell_integrals;
  cell_integrals = new ufc::cell_integral*[form.num_cell_domains()];
  for (unsigned int i = 0; i < form.num_cell_domains(); i++)
    cell_integrals[i] = form.create_cell_integral(i);

  // Create dofmaps
  ufc::dofmaps** dofmaps;
  dofmaps = new ufc::dofmap*[form.rank() + form.num_coefficients()];
  for (unsigned int i = 0; i < form.rank() + form.num_coefficients(); i++)
  {
    dofmaps[i] = form.create_dofmap(i);

    // Initialize dofmap
    if (dofmaps[i]->init_mesh(mesh))
    {
      // Iterate over cells
      for (...)
      {
        // Update cell data structure to current cell
        cell.entity_indices[...] = ...
        cell.coordinates[...] = ...
        ...

        // Initialize dofmap for cell
        dofmaps[i]->init_cell(mesh, cell);
      }

      dofmap.init_cell_finalize();
    }
  }

  // Initialize array of values for the cell tensor
  unsigned int size = 1;
  for (unsigned int i = 0; i < form.rank(); i++)
    size *= dofmaps[i]->max_local_dimension();
  double* AK = new double[size];

  // Initialize array of local to global dofmaps
  unsigned int** dofs = new unsigned int*[form.rank()];
  for (unsigned int i = 0; i < form.rank(); i++)
    dofs[i] = new unsigned int[dofmaps[i]->max_local_dimension()];

  // Initialize array of coefficient values
  double** w = new double*[form.num_coefficients()];
  for (unsigned int i = 0; i < form.num_coefficients(); i++)
    w[i] = new double[dofmaps[form.rank() + i]->max_local_dimension()];

  // Iterate over cells
  for (...)
  {
    // Get number of subdomain for current cell
    const unsigned int sub_domain = ...

    // Update cell data structure to current cell
    cell.entity_indices[...] = ...
    cell.coordinates[...] = ...
    ...

    // Interpolate coefficients (library specific so omitted here)
    ...

    // Tabulate dofs for each dimension
    for (unsigned int i = 0; i < ufc.form.rank(); i++)
      dofmaps[i]->tabulate_dofs(dofs[i], mesh, cell);

    // Tabulate cell tensor
    cell_integrals[sub_domain]->tabulate_tensor(AK, w, cell);

    // Add entries to global tensor (library specific so omitted here)
    ...
  }

  // Delete data structures
  delete [] mesh.num_entities;
  ...
}
\end{code}
\normalsize
