Date: 2021-03-03T22:25:59+00:00

Actual source code: ex11f90.F90
petsc-3.14.5 2021-03-03
Report Typos and Errors

      1:    !Concepts: vectors^norms of sub-vectors
      2:    !Processors: n

      4:   program main
      5: #include <petsc/finclude/petscvec.h>
      6:   use petscvec
      7:   implicit none

      9:   Vec        ::   x
     10:   PetscReal  :: norm
     11:   PetscMPIInt :: rank
     12:   PetscInt,parameter :: n = 20
     13:   PetscErrorCode :: ierr
     14:   PetscScalar,parameter :: sone = 1.0
     15:   PetscBool :: flg
     16:   character(len=PETSC_MAX_PATH_LEN) :: outputString
     17:   PetscInt,parameter :: zero = 0, one = 1, two = 2

     19:   call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
     20:   if (ierr /= 0) then
     21:    print*,'PetscInitialize failed'
     22:    stop
     23:   endif

     25:   call MPI_Comm_rank(PETSC_COMM_WORLD,rank,ierr)

     27:   call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER,"-n",n,flg,ierr);CHKERRA(ierr)

     30:      !Create a vector, specifying only its global dimension.
     31:      !When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
     32:      !the vector format (currently parallel,
     33:      !shared, or sequential) is determined at runtime.  Also, the parallel
     34:      !partitioning of the vector is determined by PETSc at runtime.

     36:      !Routines for creating particular vector types directly are:
     37:      !VecCreateSeq() - uniprocessor vector
     38:      !VecCreateMPI() - distributed vector, where the user can
     39:                          !determine the parallel partitioning
     40:         !VecCreateShared() - parallel vector that uses shared memory
     41:                             !(available only on the SGI) otherwise,
     42:                             !is the same as VecCreateMPI()

     44:      !With VecCreate(), VecSetSizes() and VecSetFromOptions() the option
     45:      !-vec_type mpi or -vec_type shared causes the
     46:      !particular type of vector to be formed.

     48:   call VecCreate(PETSC_COMM_WORLD,x,ierr);CHKERRA(ierr)

     50:   call VecSetSizes(x,PETSC_DECIDE,n,ierr);CHKERRA(ierr)
     51:   !
     52:   call VecSetBlockSize(x,two,ierr);CHKERRA(ierr)
     53:   call VecSetFromOptions(x,ierr);CHKERRA(ierr)

     56:      !Set the vectors to entries to a constant value.

     58:   call VecSet(x,sone,ierr);CHKERRA(ierr)

     60:   call VecNorm(x,NORM_2,norm,ierr);CHKERRA(ierr)
     61:   write(outputString,*) norm
     62:   call PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of entire vector: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     64:   call VecNorm(x,NORM_1,norm,ierr);CHKERRA(ierr)
     65:   write(outputString,*) norm
     66:   call PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of entire vector: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     68:   call VecNorm(x,NORM_INFINITY,norm,ierr);CHKERRA(ierr)
     69:   write(outputString,*) norm
     70:   call PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of entire vector: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     72:   call VecStrideNorm(x,zero,NORM_2,norm,ierr);CHKERRA(ierr)
     73:   write(outputString,*) norm
     74:   call PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of sub-vector 0: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     76:   call VecStrideNorm(x,zero,NORM_1,norm,ierr);CHKERRA(ierr)
     77:   write(outputString,*) norm
     78:   call PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of sub-vector 0: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     80:   call VecStrideNorm(x,zero,NORM_INFINITY,norm,ierr);CHKERRA(ierr)
     81:   write(outputString,*) norm
     82:   call PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of sub-vector 0: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     84:   call VecStrideNorm(x,one,NORM_2,norm,ierr);CHKERRA(ierr)
     85:   write(outputString,*) norm
     86:   call PetscPrintf(PETSC_COMM_WORLD,"L_2 Norm of sub-vector 1: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     88:   call VecStrideNorm(x,one,NORM_1,norm,ierr);CHKERRA(ierr)
     89:   write(outputString,*) norm
     90:   call PetscPrintf(PETSC_COMM_WORLD,"L_1 Norm of sub-vector 1: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     92:   call VecStrideNorm(x,one,NORM_INFINITY,norm,ierr);CHKERRA(ierr)
     93:   write(outputString,*) norm
     94:   call PetscPrintf(PETSC_COMM_WORLD,"L_inf Norm of sub-vector 1: "//trim(outputString)//"\n",ierr);CHKERRA(ierr)

     97:      !Free work space.  All PETSc objects should be destroyed when they
     98:      !are no longer needed.
     99:   call VecDestroy(x,ierr);CHKERRA(ierr)
    100:   call PetscFinalize(ierr);CHKERRA(ierr)

    102: end program

    105: !/*TEST
    106: !
    107: !     test:
    108: !       nsize: 2
    109: !
    110: !TEST*/