Actual source code: ex3f.F
petsc-3.9.4 2018-09-11
1: !
2: !
3: ! Description: Displays a vector visually.
4: !
5: !/*T
6: ! Concepts: vectors^drawing vectors;
7: ! Processors: n
8: !T*/
9: ! -----------------------------------------------------------------------
11: program main
12: #include <petsc/finclude/petscvec.h>
13: use petscvec
14: implicit none
16: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
17: ! Beginning of program
18: ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
20: Vec x
21: PetscViewer viewer
22: PetscScalar v
23: PetscInt i,istart,iend,n,ione
24: PetscErrorCode ierr
25: PetscBool flg
27: call PetscInitialize(PETSC_NULL_CHARACTER,ierr)
28: if (ierr .ne. 0) then
29: print*,'PetscInitialize failed'
30: stop
31: endif
32: n = 50
33: call PetscOptionsGetInt(PETSC_NULL_OPTIONS,PETSC_NULL_CHARACTER, &
34: & '-n',n,flg,ierr)
36: ! Create a vector, specifying only its global dimension.
37: ! When using VecCreate(), VecSetSizes() and VecSetFromOptions(),
38: ! the vector format (currently parallel
39: ! or sequential) is determined at runtime. Also, the parallel
40: ! partitioning of the vector is determined by PETSc at runtime.
41: call VecCreate(PETSC_COMM_WORLD,x,ierr)
42: call VecSetSizes(x,PETSC_DECIDE,n,ierr)
43: call VecSetFromOptions(x,ierr)
45: ! Currently, all PETSc parallel vectors are partitioned by
46: ! contiguous chunks of rows across the processors. Determine
47: ! which vector are locally owned.
48: call VecGetOwnershipRange(x,istart,iend,ierr)
50: ! Set the vector elements.
51: ! - Always specify global locations of vector entries.
52: ! - Each processor needs to insert only elements that it owns locally.
53: ione = 1
54: do 100 i=istart,iend-1
55: v = 1.0*real(i)
56: call VecSetValues(x,ione,i,v,INSERT_VALUES,ierr)
57: 100 continue
59: ! Assemble vector, using the 2-step process:
60: ! VecAssemblyBegin(), VecAssemblyEnd()
61: ! Computations can be done while messages are in transition
62: ! by placing code between these two statements.
63: call VecAssemblyBegin(x,ierr)
64: call VecAssemblyEnd(x,ierr)
66: ! Open an X-window viewer. Note that we specify the same communicator
67: ! for the viewer as we used for the distributed vector (PETSC_COMM_WORLD).
68: ! - Helpful runtime option:
69: ! -draw_pause <pause> : sets time (in seconds) that the
70: ! program pauses after PetscDrawPause() has been called
71: ! (0 is default, -1 implies until user input).
73: call PetscViewerDrawOpen(PETSC_COMM_WORLD,PETSC_NULL_CHARACTER, &
74: & PETSC_NULL_CHARACTER,0,0,300,300,viewer,ierr)
76: ! View the vector
77: call VecView(x,viewer,ierr)
79: ! Free work space. All PETSc objects should be destroyed when they
80: ! are no longer needed.
82: call PetscViewerDestroy(viewer,ierr)
83: call VecDestroy(x,ierr)
85: call PetscFinalize(ierr)
86: end
89: !/*TEST
90: !
91: ! test:
92: ! nsize: 2
93: !
94: !TEST*/