Function parameters

Hi all,

In my problem, i wanna modify the assembled matrix for some reason. Suppose a is a defined bilinear form, then we do the following

A=assemble(a,finalize_tensor=False)
A.set(...) ! to modify some elements of the assembled matrix  
A.apply('add')

Here it seems we must have A.apply() in order to apply the boundary condition or do further operation on the matrix A in the program.

My question is, what is this A.apply() for? I also check it out and the instruction of this function is given by the follows

apply (...) method of dolfin.cpp.la.Matrix instance
    apply(self: dolfin.cpp.la.GenericMatrix, arg0: str) -> None

So what is this arg0: str supposed to be? I just found ‘add’ works by coincidence. Thanks for the time and explanation.

Best regards

Hi,

The function apply() is used to finalize the assembly of your tensor. It is generically defined, for any type of matrices (see dolfin/la/GenericMatrix.h):

    /// Finalize assembly of tensor
    virtual void apply(std::string mode) = 0;

If your matrix A is a PETSc matrix (I would guess so), you can do apply("add"), apply("insert") (which does exactly the same, from what I can see) or apply("flush").
And this is what the apply function does (see dolfin/la/PETScMatrix.cpp) :

void PETScMatrix::apply(std::string mode)
{
  Timer timer("Apply (PETScMatrix)");

  dolfin_assert(_matA);
  PetscErrorCode ierr;
  if (mode == "add")
  {
    ierr = MatAssemblyBegin(_matA, MAT_FINAL_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyBegin");
    ierr = MatAssemblyEnd(_matA, MAT_FINAL_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyEnd");
  }
  else if (mode == "insert")
  {
    ierr = MatAssemblyBegin(_matA, MAT_FINAL_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyBegin");
    ierr = MatAssemblyEnd(_matA, MAT_FINAL_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyEnd");
  }
  else if (mode == "flush")
  {
    ierr = MatAssemblyBegin(_matA, MAT_FLUSH_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyBegin");
    ierr = MatAssemblyEnd(_matA, MAT_FLUSH_ASSEMBLY);
    if (ierr != 0) petsc_error(ierr, __FILE__, "MatAssemblyEnd");
  }
...

More details on what the PETSc function MatAssemblyBegin does can be found here : https://www.mcs.anl.gov/petsc/petsc-current/docs/manualpages/Mat/MatAssemblyBegin.html

1 Like

Thanks for the detailed explanations.