Actual source code: ex219f.F90
1: program newnonzero
2: #include <petsc/finclude/petscmat.h>
3: use petscmat
4: implicit none
6: Mat :: A
7: PetscInt :: n,m,idxm(1),idxn(1),nl1,nl2,zero,one,i
8: PetscScalar :: v(1)
9: PetscErrorCode :: ierr
11: PetscCallA(PetscInitialize(ierr))
12: zero = 0
13: one = 1
14: n=3
15: m=n
16: PetscCallA(MatCreateAIJ(PETSC_COMM_WORLD,PETSC_DECIDE,PETSC_DECIDE,n,m,one,PETSC_NULL_INTEGER,zero,PETSC_NULL_INTEGER,A,ierr))
18: PetscCallA(MatGetOwnershipRange(A,nl1,nl2,ierr))
19: do i=nl1,nl2-1
20: idxn(1)=i
21: idxm(1)=i
22: v(1)=1.0
23: PetscCallA(MatSetValues(A,one,idxn,one,idxm, v,INSERT_VALUES,ierr))
24: end do
25: PetscCallA(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr))
26: PetscCallA(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr))
28: ! Ignore any values set into new nonzero locations
29: PetscCallA(MatSetOption(A,MAT_NEW_NONZERO_LOCATIONS,PETSC_FALSE,ierr))
31: idxn(1)=0
32: idxm(1)=n-1
33: if ((idxn(1).ge.nl1).and.(idxn(1).le.nl2-1)) then
34: v(1)=2.0
35: PetscCallA(MatSetValues(A,one,idxn,one,idxm, v,INSERT_VALUES,ierr))
36: end if
37: PetscCallA(MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr))
38: PetscCallA(MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr))
40: if ((idxn(1).ge.nl1).and.(idxn(1).le.nl2-1)) then
41: PetscCallA(MatGetValues(A,one,idxn,one,idxm, v,ierr))
42: write(6,*) PetscRealPart(v)
43: end if
45: PetscCallA(MatDestroy(A,ierr))
46: PetscCallA(PetscFinalize(ierr))
48: end program newnonzero
50: !/*TEST
51: !
52: ! test:
53: ! nsize: 2
54: ! filter: Error:
55: !
56: ! test:
57: ! requires: defined(PETSC_USE_INFO)
58: ! suffix: 2
59: ! nsize: 2
60: ! args: -info
61: ! filter: grep "Skipping"
62: !
63: !TEST*/