You need to insert explicitly a zero in all the missing diagonal entries of the matrix
Stefano On Fri, Jul 31, 2026, 4:07 PM bruchon--- via petsc-users < [email protected]> wrote: > Hello, > > I would like to implement periodic boundary conditions. I first > initialized the matrix with > > MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE); > > and everything works fine. Then, for efficiency reasons, I wanted to > switch to > > PetscCall(MatSetOption(A, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_TRUE)); > > but the code now crashes at > > PetscCall(MatZeroRows(A, numRows, rows, diag, NULL, NULL)); > > with the following error: > > [0]PETSC ERROR: Argument out of range > [0]PETSC ERROR: New nonzero at (15387,15387) caused a malloc > > I also tried (just for testing) > > numRows = 1; > std::vector<int> ro = {15387}; > PetscCall(MatZeroRows(A, numRows, ro.data(), diag, NULL, NULL)); > > but, unsurprisingly, this did not help. > > Finally, I also tried > > PetscCall(MatSetOption(mat, MAT_KEEP_NONZERO_PATTERN, PETSC_TRUE)); > > but without success. Instead, I get: > > [0]PETSC ERROR: Object is in wrong state > [0]PETSC ERROR: Matrix is missing diagonal entry in the zeroed row 15387 > > Does anyone know what mistake I am making here? All I want to do is set > a row to zero and put a 1 on its diagonal... > > Julien > >
