On Wed, Jul 22, 2020 at 8:05 AM Adolfo Rodriguez <adan...@gmail.com> wrote:
> I am trying to replace the non-linear solver in a flow simulation problem > where the matrix sparsity can change during the iterations. I tried > successfully to create the matrix within the FormJacobian function but I > have a memory leak which I don't know how to fix. > > My FormJacobian function is something like this: > > .. FormJacobian( ..){ > matrixconstruction(); // returns csr matrix in c++ regular vectors > MatSeqAIJSetPreallocationCSR ...; > //loop over the entries and > MatSetValues; > MatAssemblyBegin(); > MatAssemblyEnd(); > return 0; > } > > This approach works for small problems but, when dealing with large ones I > observe a memory leak. > How are you detecting a memory leak? It's odd that you would not see it on a small problem. You can run with -malloc_debug to get a listing of PETSc memory that was not freed. > I am pretty sure that It is related to the fact that I am creating new > matrices every time FormJacobian is called but I don't know how to destroy > it between iterations. > You don't create the matrix in FormJacobian, do you? Do your changes (eg, AMR) before or after the time step (you can do this with a TSSetPostStep and TSSetPreStep), then destroy the old matrix and vectors. > > Any suggestions? > > Regards, > > Adolfo >