[petsc-users] Re-use the matrix in petsc

Matthew Knepley knepley at gmail.com
Fri Nov 24 14:56:05 CST 2017


On Fri, Nov 24, 2017 at 3:47 PM, Edoardo alinovi <edoardo.alinovi at gmail.com>
wrote:

> Dear petsc users,
>
> I am new to petsc, but I am really enjoying it.
>
> I am developing a CFD code in fortran and I have a (newby) question for
> you.
>
> I have a subroutine that assembles the matrices arising from the finite
> volume method. They are sparse and the coefficients change every time step,
> but their sparsity remains unchanged (i.e. non-zeros entries are always in
> the same position).
>
> In the subroutine, which is called every time step, I am basically
> following the manual and I do something like:
>
> -  call MatCreate(PETSC_COMM_WORLD,A,ierr)
> -   call MatSetSizes(A,lm,lm,M,M,ierr)
> -   call MatSetType(A, myType,ierr)
> -    call MatMPIAIJSetPreallocation(A,d_nz,PETSC_NULL_INTEGER, o_nz,
> PETSC_NULL_INTEGER,ierr)
>            call MatSetUp(A,ierr)
>
> -   call MatSetValues
> -   call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr)
> -   call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr)
>
> -   Solve the system
>
> -  call MatDestroy
>
>
> Here is my question:
>
> As you can note, every time step, I allocate the matrix, fill it, use it
> and the destroy it. Is there a way to avoid the matrix preallocation every
> time? I just would like to change matrix the entries and avoid the
> preallocation.
>

Make the first 5 calls outside your timestep loop. At each step, (after the
first timestep call MatZeroEntries()), MatSetValues() for all your values,
and then matAssemblyBegin/End().
After all your timesteps, call MatDestroy(). If you use a factorization
preconditioner, it will detect that you only change values, not structure.

  Thanks,

    Matt


> Thank you for the help that you can give me,
>
> Edoardo
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener

https://www.cse.buffalo.edu/~knepley/ <http://www.caam.rice.edu/~mk51/>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20171124/9fd273a0/attachment.html>


More information about the petsc-users mailing list