[petsc-users] Copy stiffness matrix to jacobian matrix in form jacobian

Kaushik Vijaykumar kaushikv318 at gmail.com
Thu Jun 10 15:29:08 CDT 2021


Hi everyone,

I am trying to copy the stiffness matrix that I generated in form function
to the jacobian matrix jac in form jacobian. The piece of code for that:


my_ctx* ptr = (my_ctx*) ctx; // cast the pointer to void into pointer to
struct

ierr = MatGetOwnershipRange(jac,&istart,&iend);
ierr = MatGetSize(ptr->K,&m,&n); CHKERRQ(ierr);

ierr = MatAssemblyBegin(ptr->K,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(ptr->K,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);

 for(i=istart;i<iend;i++)
{
 for(j=0;j<n;j++)
 {
 ierr = MatGetValue(ptr->K, i, j, &v);CHKERRQ(ierr);
 ierr = MatSetValue(jac, i, j, v, INSERT_VALUES);CHKERRQ(ierr);
 ierr = MatSetValue(B, i, j, v, INSERT_VALUES);CHKERRQ(ierr);
 }
 }

The following works for a single process run only. For a multiprocessor run
the ptr->K does not get copied correctly to jac

I have also tried the following commands

ierr = MatDuplicate(ptr->K,MAT_COPY_VALUES,&jac);CHKERRQ(ierr);
ierr = MatDuplicate(ptr->K,MAT_COPY_VALUES,&B);CHKERRQ(ierr);

ierr = MatCopy(ptr->K,jac,SAME_NONZERO_PATTERN);CHKERRQ(ierr);
ierr = MatCopy(ptr->K,B,SAME_NONZERO_PATTERN);CHKERRQ(ierr);


I get Petsc object in a wrong state error.

Could you please advise on what is most efficient way to copy the matrix
from the user defined struct to jacobian.

Thanks
Kaushik
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20210610/58d63225/attachment.html>


More information about the petsc-users mailing list