Actual source code: ex12f.F90

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1: program main

  3:  #include <petsc/finclude/petscvec.h>

  5: use petscvec
  6: implicit none

  8:   PetscErrorCode ierr
  9:   Vec   v,s
 10:   PetscInt,parameter ::      n   = 20
 11:   PetscScalar,parameter ::    one = 1.0
 12:   PetscBool :: flg

 14:  call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
 15:   if (ierr /= 0) then
 16:    print*,'PetscInitialize failed'
 17:    stop
 18:   endif
 19: 
 20:   call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-n",n,flg,ierr);CHKERRA(ierr)


 23:   !Create multi-component vector with 2 components
 24:   call VecCreate(PETSC_COMM_WORLD,v,ierr);CHKERRA(ierr)
 25:   call VecSetSizes(v,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
 26:   call VecSetBlockSize(v,2,ierr);CHKERRA(ierr)
 27:   call VecSetFromOptions(v,ierr);CHKERRA(ierr)


 30:   !Create single-component vector
 31:   call VecCreate(PETSC_COMM_WORLD,s,ierr);CHKERRA(ierr)
 32:   call VecSetSizes(s,PETSC_DECIDE,n/2,ierr);CHKERRA(ierr)
 33:   call VecSetFromOptions(s,ierr);CHKERRA(ierr)

 35:   !Set the vectors to entries to a constant value.
 36:   call VecSet(v,one,ierr);CHKERRA(ierr)

 38:   !Get the first component from the multi-component vector to the single vector
 39:   call VecStrideGather(v,0,s,INSERT_VALUES,ierr);CHKERRA(ierr)

 41:   call VecView(s,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)


 44:   !Put the values back into the second component
 45:   call VecStrideScatter(s,1,v,ADD_VALUES,ierr);CHKERRA(ierr)

 47:   call VecView(v,PETSC_VIEWER_STDOUT_WORLD,ierr);CHKERRA(ierr)


 50:   !Free work space.All PETSc objects should be destroyed when they are no longer needed.

 52:   call VecDestroy(v,ierr);CHKERRA(ierr)
 53:   call VecDestroy(s,ierr);CHKERRA(ierr)
 54:   call PetscFinalize(ierr);CHKERRA(ierr)

 56:   end program