xref: /petsc/src/vec/vec/tests/ex19f.F90 (revision 9b88ac225e01f016352a5f4cd90e158abe5f5675)
1!
2!
3#include <petsc/finclude/petscvec.h>
4program main
5  use petscvec
6  implicit none
7!
8!  This example demonstrates basic use of the PETSc Fortran interface
9!  to vectors.
10!
11  PetscInt n
12  PetscErrorCode ierr
13  PetscBool flg
14  PetscScalar one, two, three, dot
15  PetscReal norm, rdot
16  Vec x, y, w
17  PetscOptions options
18
19  n = 20
20  one = 1.0
21  two = 2.0
22  three = 3.0
23
24  PetscCallA(PetscInitialize(ierr))
25  PetscCallA(PetscOptionsCreate(options, ierr))
26  PetscCallA(PetscOptionsGetInt(options, PETSC_NULL_CHARACTER, '-n', n, flg, ierr))
27  PetscCallA(PetscOptionsDestroy(options, ierr))
28
29! Create a vector, then duplicate it
30  PetscCallA(VecCreate(PETSC_COMM_WORLD, x, ierr))
31  PetscCallA(VecSetSizes(x, PETSC_DECIDE, n, ierr))
32  PetscCallA(VecSetFromOptions(x, ierr))
33  PetscCallA(VecDuplicate(x, y, ierr))
34  PetscCallA(VecDuplicate(x, w, ierr))
35
36  PetscCallA(VecSet(x, one, ierr))
37  PetscCallA(VecSet(y, two, ierr))
38
39  PetscCallA(VecDot(x, y, dot, ierr))
40  rdot = PetscRealPart(dot)
41  write (6, 100) rdot
42100 format('Result of inner product ', f10.4)
43
44  PetscCallA(VecScale(x, two, ierr))
45  PetscCallA(VecNorm(x, NORM_2, norm, ierr))
46  write (6, 110) norm
47110 format('Result of scaling ', f10.4)
48
49  PetscCallA(VecCopy(x, w, ierr))
50  PetscCallA(VecNorm(w, NORM_2, norm, ierr))
51  write (6, 120) norm
52120 format('Result of copy ', f10.4)
53
54  PetscCallA(VecAXPY(y, three, x, ierr))
55  PetscCallA(VecNorm(y, NORM_2, norm, ierr))
56  write (6, 130) norm
57130 format('Result of axpy ', f10.4)
58
59  PetscCallA(VecDestroy(x, ierr))
60  PetscCallA(VecDestroy(y, ierr))
61  PetscCallA(VecDestroy(w, ierr))
62  PetscCallA(PetscFinalize(ierr))
63end
64
65!/*TEST
66!
67!     test:
68!
69!TEST*/
70