Date: 2026-07-30T21:14:35+00:00

Actual source code: ex12f.F90

      1: #include <petsc/finclude/petscvec.h>
      2: program main
      3:   use petscvec
      4:   implicit none

      6:   PetscErrorCode ierr
      7:   Vec v, s
      8:   PetscInt :: n
      9:   PetscScalar, parameter :: one = 1.0
     10:   PetscBool :: flg

     12:   PetscCallA(PetscInitialize(ierr))

     14:   n = 20
     15:   PetscCallA(PetscOptionsGetInt(PETSC_NULL_OPTIONS, PETSC_NULL_CHARACTER, '-n', n, flg, ierr))

     17:   ! Create multi-component vector with 2 components
     18:   PetscCallA(VecCreate(PETSC_COMM_WORLD, v, ierr))
     19:   PetscCallA(VecSetSizes(v, PETSC_DECIDE, n, ierr))
     20:   PetscCallA(VecSetBlockSize(v, 2_PETSC_INT_KIND, ierr))
     21:   PetscCallA(VecSetFromOptions(v, ierr))

     23:   ! Create single-component vector
     24:   PetscCallA(VecCreate(PETSC_COMM_WORLD, s, ierr))
     25:   PetscCallA(VecSetSizes(s, PETSC_DECIDE, n/2, ierr))
     26:   PetscCallA(VecSetFromOptions(s, ierr))

     28:   !Set the vectors to entries to a constant value.
     29:   PetscCallA(VecSet(v, one, ierr))

     31:   !Get the first component from the multi-component vector to the single vector
     32:   PetscCallA(VecStrideGather(v, 0_PETSC_INT_KIND, s, INSERT_VALUES, ierr))

     34:   PetscCallA(VecView(s, PETSC_VIEWER_STDOUT_WORLD, ierr))

     36:   !Put the values back into the second component
     37:   PetscCallA(VecStrideScatter(s, 1_PETSC_INT_KIND, v, ADD_VALUES, ierr))

     39:   PetscCallA(VecView(v, PETSC_VIEWER_STDOUT_WORLD, ierr))

     41:   ! Free work space.All PETSc objects should be destroyed when they are no longer needed.
     42:   PetscCallA(VecDestroy(v, ierr))
     43:   PetscCallA(VecDestroy(s, ierr))
     44:   PetscCallA(PetscFinalize(ierr))

     46: end program

     48: !/*TEST
     49: !
     50: !     test:
     51: !       nsize: 2
     52: !       output_file: output/ex12_1.out
     53: !
     54: !TEST*/