[petsc-users] a line of code is weird

Barry Smith bsmith at mcs.anl.gov
Sat Mar 7 12:11:32 CST 2015


 Note the lines immediately above it.

 ierr = VecGetOwnershipRange(x1,&start,&end);CHKERRQ(ierr);
.....
for (j=start; j<end; j++) {
       if (PetscAbsScalar(y[j-start]) > amax || j == i) {
        ierr = MatSetValues(B,1,&j,1,&i,y+j-start,INSERT_VALUES);CHKERRQ(ierr);

 j is looping over the global rows of the vector. The -start is to convert to the local row inside the array y[] since y only contains the local part of the vector. 

Note that y+j-start is equivalent to &y[j-start] so this is inserting into the jth global row the jth global row value in the vector which is the j-start local entry in the vector.

Barry


> On Mar 7, 2015, at 11:53 AM, Fande Kong <fdkong.jd at gmail.com> wrote:
> 
> Hi all,
> 
> I am trying to understand how to compute Jacobian using finite difference methods. There is a function called SNESComputeJacobianDefault, where a line of code at 120th line, MatSetValues(B,1,&j,1,&i,y+j-start,INSERT_VALUES), is difficult to understand for me. Because I think inserting  'y' is enough, but there is two extra values 'j-start'. Any explanation is welcome.
> 
> Thanks,
> 
> Fande,



More information about the petsc-users mailing list