[petsc-users] MatSetValuesStencil for cols not in the dmda stencil width

Matthew Knepley knepley at gmail.com
Wed Jan 25 09:53:19 CST 2017


On Wed, Jan 25, 2017 at 9:17 AM, Xiangdong <epscodes at gmail.com> wrote:

> Hello everyone,
>
> I have a question on setting matrix entries which are not in the stencil
> width. Take ksp ex45.c as an example,
> http://www.mcs.anl.gov/petsc/petsc-current/src/ksp/ksp/
> examples/tutorials/ex45.c.html
>
> Instead of setting up the standard 7-point stencil, now for each cell, the
> matrix also has a additional dependency on the cell (Mx, My, Mz). Namely,
> for each row, the col corresponding to (Mx, My, Mz) is always nonzero. I
> modify the example code to add this entries:
>
> +  MatSetOption(B,MAT_NEW_NONZERO_LOCATIONS,PETSC_TRUE);
> +  MatSetOption(jac,MAT_NEW_NONZERO_LOCATIONS,PETSC_TRUE);
>
> +  v[7] = 100; col[7].i = mx-1; col[7].j = my-1; col[7].k = mz-1;
> +  ierr = MatSetValuesStencil(B,1,&row,8,col,v,INSERT_VALUES);
> CHKERRQ(ierr);
>
> It is okay to for np=1, but crash for np>=2 with the error message:
>

You can do this, but

1) You cannot use MatSetStencil, since your entry is not actually in your
stencil. You will have to make a separate call to MatSetValues() using the
global index.

2) The nonzero pattern we have allocated will be wrong. You will have to
set the MatOption which gives an error on new nonzeros to PETSC_FALSE.

3) You will have a dense row in your Jacobian, which is hard to make
perform well, and also stymies most preconditioners.

  Thanks,

    Matt


> [0]PETSC ERROR: --------------------- Error Message
> --------------------------------------------------------------
> [0]PETSC ERROR: Argument out of range
> [0]PETSC ERROR: Local index 342 too large 244 (max) at 7
>
> [0]PETSC ERROR: #1 ISLocalToGlobalMappingApply() line 423 in
> petsc-3.7.4/src/vec/is/utils/isltog.c
> [0]PETSC ERROR: #2 MatSetValuesLocal() line 2052 in
> petsc-3.7.4/src/mat/interface/matrix.c
> [0]PETSC ERROR: #3 MatSetValuesStencil() line 1447 in
> petsc-3.7.4/src/mat/interface/matrix.c
> [0]PETSC ERROR: #4 ComputeMatrix() line 151 in extest.c
>
> Can I add new entries to the cols not in the stencil width into the dmda
> matrix or Jacobian?
>
> Attached please find the modifed ex45 example, the diff file as well as
> the run log.
>
> Thanks for your help.
>
> Best,
> Xiangdong
>
>


-- 
What most experimenters take for granted before they begin their
experiments is infinitely more interesting than any results to which their
experiments lead.
-- Norbert Wiener
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/petsc-users/attachments/20170125/fd9f88c4/attachment.html>


More information about the petsc-users mailing list