In addition to the excellent idea by Juan Carlos:
> When I initialize the PETSc sparsity matrix, I don't know where the nonzeros are.
> Then I loop over the locally owned cells on each processor, and guadually know
> for each row of the matrix the locations and values of the nonzeros. Do you
> mean that I can't directly use the PETSc_sparsity_matrix.add() function to
> insert nonzero values into the matrix without telling the sparsity pattern the
> nonzeros locations?
The way we set up these data structures is that we want to know up front where
the nonzeros are. It's far more efficient to do it that way, but it is
possible to add nonzeros on the fly as shown in the post by Juan Carlos.
How do we know where the nonzeros are? We "simulate" what matrix assembly
would look like, i.e., in a first stage, we record those locations that we
want to write into later on, and this gives us the sparsity pattern (e.g.,
using a DynamicSparsityPattern). Then, we can in a second stage do the actual
assembly into a matrix where we already know where the nonzeros are going to be.