Actual source code: ex12f.F90

petsc-master 2019-05-20
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 ::   sone = 1.0
 12:   PetscBool :: flg
 13:   PetscInt,parameter :: zero = 0, one = 1, two = 2

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


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


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

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

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

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


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

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


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

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

 57:   end program

 59: !/*TEST
 60: !
 61: !     test:
 62: !       nsize: 2
 63: !       output_file: output/ex12_1.out
 64: !
 65: !TEST*/