PETSc 库解线性方程组Ax = b

program main implicit none #include "finclude/petscsys.h" #include "finclude/petscvec.h" #include "finclude/petscmat.h" #include "finclude/petscksp.h" #include "finclude/petscpc.h" Vec x, b, u Mat A KSP ksp PC pc PetscReal norm, tol PetscErrorCode ierr PetscScalar v(1),w(5) PetscInt rank, one PetscInt m, n, i, j, iar(1), jar(1) PetscInt loc(5) call PetscInitialize(PETSC_NULL_CHARACTER,ierr) call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr) write(*, *) 'pid:', rank one = 1 m = 5 n = 5 !create matrix call MatCreateMPIDense(PETSC_COMM_WORLD,PETSC_DECIDE, & & PETSC_DECIDE,m,n,PETSC_NULL_SCALAR,A,ierr) !FillUpMatirx do 10, i=0,m-1 iar(1) = i do 20, j=0,n-1 jar(1) = j v(1) = 9.d0 / (i+j+1) !write(*, *) iar(1), jar(1) call MatSetValues(A,one,iar,one,jar,v,INSERT_VALUES,ierr) 20 continue 10 continue call MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY,ierr) call MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY,ierr) write(*, *) 'Matrix A:' call MatView(A,PETSC_VIEWER_STDOUT_WORLD,ierr) !create vector call VecCreate(PETSC_COMM_WORLD,b,ierr) call VecSetSizes(b,PETSC_DECIDE,n,ierr) call VecSetFromOptions(b,ierr) call VecDuplicate(b,x,ierr) !FillUpVector do 25, i = 1, n loc(i) = i-1 w(i) = i+1 25 continue call VecSetValues(b,n,loc,w,INSERT_VALUES,ierr) !do 30, i = 0, n-1 ! call VecSetValues(b,1,i,v,ADD_VALUES,ierr) !30 continue call VecAssemblyBegin(b,ierr) call VecAssemblyEnd(b,ierr) write(*, *) 'Vector b:' call VecView(b,PETSC_VIEWER_STDOUT_WORLD,ierr) call KSPCreate(PETSC_COMM_WORLD,ksp,ierr) call KSPSetOperators(ksp,A,A,DIFFERENT_NONZERO_PATTERN,ierr) call KSPGetPC(ksp,pc,ierr) call PCSetType(pc,PCJACOBI,ierr) tol = 1.d-7 call KSPSetTolerances(ksp,tol,PETSC_DEFAULT_DOUBLE_PRECISION, & & PETSC_DEFAULT_DOUBLE_PRECISION,PETSC_DEFAULT_INTEGER,ierr) call KSPSetFromOptions(ksp,ierr) call KSPSolve(ksp,x,b,ierr) write(*, *) 'x:',x call KSPView(ksp,PETSC_VIEWER_STDOUT_WORLD,ierr) call VecDestroy(x,ierr) call VecDestroy(b,ierr) call MatDestroy(A,ierr) call KSPDestroy(ksp,ierr) call PetscFinalize(ierr) end  

你可能感兴趣的:(jar,assembly,Integer,insert,character,Matrix)