1program newnonzero 2#include <petsc/finclude/petscis.h> 3#include <petsc/finclude/petscmat.h> 4 use petscmat 5 implicit none 6 7 Mat :: A 8 PetscInt :: n, m, idxm(1), idxn(1), nl1, nl2, zero, one, i 9 PetscScalar :: v(1), value(1), values(2) 10 PetscErrorCode :: ierr 11 IS :: is 12 ISLocalToGlobalMapping :: ismap 13 14 PetscCallA(PetscInitialize(ierr)) 15 zero = 0 16 one = 1 17 n = 3 18 m = n 19 PetscCallA(MatCreateAIJ(PETSC_COMM_WORLD, PETSC_DECIDE, PETSC_DECIDE, n, m, one, PETSC_NULL_INTEGER_ARRAY, zero, PETSC_NULL_INTEGER_ARRAY, A, ierr)) 20 21 PetscCallA(MatGetOwnershipRange(A, nl1, nl2, ierr)) 22 do i = nl1, nl2 - 1 23 idxn(1) = i 24 idxm(1) = i 25 v(1) = 1.0 26 PetscCallA(MatSetValues(A, one, idxn, one, idxm, v, INSERT_VALUES, ierr)) 27 end do 28 PetscCallA(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY, ierr)) 29 PetscCallA(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY, ierr)) 30 31! Ignore any values set into new nonzero locations 32 PetscCallA(MatSetOption(A, MAT_NEW_NONZERO_LOCATIONS, PETSC_FALSE, ierr)) 33 34 idxn(1) = 0 35 idxm(1) = n - 1 36 if ((idxn(1) >= nl1) .and. (idxn(1) <= nl2 - 1)) then 37 v(1) = 2.0 38 PetscCallA(MatSetValues(A, one, idxn, one, idxm, v, INSERT_VALUES, ierr)) 39 end if 40 PetscCallA(MatAssemblyBegin(A, MAT_FINAL_ASSEMBLY, ierr)) 41 PetscCallA(MatAssemblyEnd(A, MAT_FINAL_ASSEMBLY, ierr)) 42 43 if ((idxn(1) >= nl1) .and. (idxn(1) <= nl2 - 1)) then 44 PetscCallA(MatGetValues(A, one, idxn, one, idxm, v, ierr)) 45 write (6, *) PetscRealPart(v) 46 end if 47 48 PetscCallA(ISCreateStride(PETSC_COMM_WORLD, nl2 - nl1, nl1, one, is, ierr)) 49 PetscCallA(ISLocalToGlobalMappingCreateIS(is, ismap, ierr)) 50 PetscCallA(MatSetLocalToGlobalMapping(A, ismap, ismap, ierr)) 51 PetscCallA(ISLocalToGlobalMappingDestroy(ismap, ierr)) 52 PetscCallA(ISDestroy(is, ierr)) 53 PetscCallA(MatGetValuesLocal(A, one, [zero], one, [zero], value, ierr)) 54 PetscCallA(MatGetValuesLocal(A, one, [zero], one, [zero], values, ierr)) 55 idxn(1) = 0 56 PetscCallA(MatGetValuesLocal(A, one, idxn, one, [zero], values, ierr)) 57 PetscCallA(MatGetValuesLocal(A, one, idxn, one, idxn, values, ierr)) 58 59 PetscCallA(MatDestroy(A, ierr)) 60 PetscCallA(PetscFinalize(ierr)) 61 62end program newnonzero 63 64!/*TEST 65! 66! test: 67! nsize: 2 68! filter: Error: 69! 70! test: 71! requires: defined(PETSC_USE_INFO) 72! suffix: 2 73! nsize: 2 74! args: -info 75! filter: grep "Skipping" 76! 77!TEST*/ 78