[petsc-users] MatSetValues() can't work right

Waltz Jan jl2862237661 at gmail.com
Mon Mar 18 07:06:48 CDT 2024


PETSc version: 3.20.4
Program:
#include <petscdmda.h>
#include <petscsys.h>
#include <petscdm.h>
#include <petscdmda.h>
#include <petscsys.h>
#include <petscdm.h>
#include <petsc/private/snesimpl.h>

int main()
{
    PetscInitialize(NULL, NULL, NULL, NULL);
    DM da;
    DMDACreate3d(PETSC_COMM_WORLD, DM_BOUNDARY_GHOSTED,
DM_BOUNDARY_GHOSTED, DM_BOUNDARY_GHOSTED, DMDA_STENCIL_STAR,
                 10, 1, 10, PETSC_DECIDE, PETSC_DECIDE, PETSC_DECIDE, 3, 1,
NULL, NULL, NULL, &da);
    DMSetFromOptions(da);
    DMSetUp(da);
    Mat Jac;
    DMCreateMatrix(da, &Jac);
    int row = 100, col = 100;
    double val = 1.;
    MatSetValues(Jac, 1, &row, 1, &col, &val, INSERT_VALUES);
    MatAssemblyBegin(Jac, MAT_FINAL_ASSEMBLY);
    MatAssemblyEnd(Jac, MAT_FINAL_ASSEMBLY);

    PetscViewer viewer;
    PetscViewerASCIIOpen(PETSC_COMM_WORLD, "./jacobianmatrix.m", &viewer);
    PetscViewerPushFormat(viewer, PETSC_VIEWER_ASCII_MATLAB);
    MatView(Jac, viewer);
    PetscViewerDestroy(&viewer);

    PetscFinalize();
} When I ran the program with np = 6, I got the result as the below [image:
image.png]
It's obviously wrong. When I ran the program with np = 1 or 8, I got the
right result as [image: image.png]
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240318/702a05c0/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 51007 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240318/702a05c0/attachment-0002.png>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 47126 bytes
Desc: not available
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20240318/702a05c0/attachment-0003.png>


More information about the petsc-users mailing list