What version of PETSc are you using? The output looks a little funny.

  Would you be willing to send your code to petsc-maint@mcs.anl.gov so I can debug it quickly?


On May 29, 2023, at 9:54 PM, Waltz Jan <jl2862237661@gmail.com> wrote:

Thank you for your reply and help. I followed your advice to try it out, but found that the results were not quite right. It may be due to my misunderstanding. Could you please help me? The specific information is as follows:

Codes:
    /* Solve J Y = F, where J is Jacobian matrix */
    ierr = SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre);
    CHKERRQ(ierr);

    PetscViewer viewer;
    /* View the matrix in a text file */
    ierr = PetscViewerASCIIOpen(PETSC_COMM_WORLD, "/share/userfile/jianglei/Muti-layers-well-working/petsc/jacobianmatrix.m", &viewer);CHKERRQ(ierr);
    ierr = PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);CHKERRQ(ierr); /* Optional: use MATLAB format */
    ierr = MatView(snes->jacobian, viewer);CHKERRQ(ierr);
    ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr);

    MatAssemblyBegin(snes->jacobian,MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(snes->jacobian,MAT_FINAL_ASSEMBLY);
    PetscInt rstart, rend;
    MatGetOwnershipRange(snes->jacobian, &rstart, &rend);
    PetscInt row=1000, col=1000;
    PetscScalar v;

    if (row>=rstart && row<rend)
    {
      MatGetValues(snes->jacobian, 1, &row, 1, &col, &v);
      PetscPrintf(PETSC_COMM_WORLD, "rstart: %d, rend: %d, row: %d, col: %d, v: %e\n", rstart, rend, row, col, PetscRealPart(v));
    }

Results:
<image.png>
<image.png>
<image.png>
====================== Step: 1, time: 0. days==================== 0 SNES Function norm 1.772062116708e-01 rstart: 0, rend: 4000, row: 1000, col: 1000, v: 7.443232e-06
 
That's all. Could you help me?


On Tue, May 30, 2023 at 1:00 AM <petsc-users-request@mcs.anl.gov> wrote:
Send petsc-users mailing list submissions to
        petsc-users@mcs.anl.gov

To subscribe or unsubscribe via the World Wide Web, visit
        https://lists.mcs.anl.gov/mailman/listinfo/petsc-users
or, via email, send a message with subject or body 'help' to
        petsc-users-request@mcs.anl.gov

You can reach the person managing the list at
        petsc-users-owner@mcs.anl.gov

When replying, please edit your Subject line so it is more specific
than "Re: Contents of petsc-users digest..."


Today's Topics:

   1.  MatGetValues() can't return the correct values (Waltz Jan)
   2. Re:  MatGetValues() can't return the correct values (Barry Smith)


----------------------------------------------------------------------

Message: 1
Date: Mon, 29 May 2023 16:49:40 +0800
From: Waltz Jan <jl2862237661@gmail.com>
To: petsc-users@mcs.anl.gov
Subject: [petsc-users] MatGetValues() can't return the correct values
Message-ID:
        <CAEj=jGKvgY5ys=4_=YoEZrE5Xu4Pj6FVxSq8XgUH=N=QF_C-vA@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

/* Solve J Y = F, where J is Jacobian matrix */
ierr = SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre);
CHKERRQ(ierr);

PetscInt rstart, rend;
MatGetOwnershipRange(snes->jacobian, &rstart, &rend);
PetscInt row=1000, col=1000;
PetscScalar v;
if (row>=rstart && row<rend)
{
    MatGetValues(snes->jacobian, 1, &row, 1, &col, &v);
    PetscPrintf(PETSC_COMM_WORLD, "rstart: %d, rend: %d, row: %d, col: %d,
v: %e\n", rstart, rend, row, col, v);
}

It was supposed to return the value of the matrix at row 1001 and column
1001, but it returned the value at row 2001 and column 2001 instead. There
is a two-fold relationship between these coordinates, and I'm not sure if
it's related to the fact that I set the number of processes to 2.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230529/e6c3eb3b/attachment-0001.html>

------------------------------

Message: 2
Date: Mon, 29 May 2023 11:01:32 -0400
From: Barry Smith <bsmith@petsc.dev>
To: Waltz Jan <jl2862237661@gmail.com>
Cc: petsc-users@mcs.anl.gov
Subject: Re: [petsc-users] MatGetValues() can't return the correct
        values
Message-ID: <99A3D1B8-6CA1-41C3-8B19-E68F90A67102@petsc.dev>
Content-Type: text/plain; charset="us-ascii"


    Perhaps run a very small problem with 2 ranks and use MatView() to see the matrix before getting the values. Maybe use the debugger (-start_in_debugger) and step through the code as it gets values. I would say there is very little chance it is getting the "wrong values" and is more likely due to a misunderstanding of the matrix usage.

  Barry




> On May 29, 2023, at 4:49 AM, Waltz Jan <jl2862237661@gmail.com> wrote:
>
> /* Solve J Y = F, where J is Jacobian matrix */
> ierr = SNESComputeJacobian(snes, X, snes->jacobian, snes->jacobian_pre);
> CHKERRQ(ierr);
>
> PetscInt rstart, rend;
> MatGetOwnershipRange(snes->jacobian, &rstart, &rend);
> PetscInt row=1000, col=1000;
> PetscScalar v;
> if (row>=rstart && row<rend)
> {
>     MatGetValues(snes->jacobian, 1, &row, 1, &col, &v);
>     PetscPrintf(PETSC_COMM_WORLD, "rstart: %d, rend: %d, row: %d, col: %d, v: %e\n", rstart, rend, row, col, v);
> }
>
> It was supposed to return the value of the matrix at row 1001 and column 1001, but it returned the value at row 2001 and column 2001 instead. There is a two-fold relationship between these coordinates, and I'm not sure if it's related to the fact that I set the number of processes to 2.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20230529/345ddb55/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
petsc-users mailing list
petsc-users@mcs.anl.gov
https://lists.mcs.anl.gov/mailman/listinfo/petsc-users


------------------------------

End of petsc-users Digest, Vol 173, Issue 131
*********************************************