Actual source code: ex1.c

petsc-3.8.4 2018-03-24
Report Typos and Errors

  2: static char help[] = "Basic vector routines.\n\n";

  4: /*T
  5:    Concepts: vectors^basic routines;
  6:    Processors: n
  7: T*/

  9: /*
 10:   Include "petscvec.h" so that we can use vectors.  Note that this file
 11:   automatically includes:
 12:      petscsys.h       - base PETSc routines   petscis.h     - index sets
 13:      petscviewer.h - viewers
 14: */

 16:  #include <petscvec.h>

 18: int main(int argc,char **argv)
 19: {
 20:   Vec            x,y,w;               /* vectors */
 21:   Vec            *z;                    /* array of vectors */
 22:   PetscReal      norm,v,v1,v2,maxval;
 23:   PetscInt       n = 20,maxind;
 25:   PetscScalar    one = 1.0,two = 2.0,three = 3.0,dots[3],dot;

 27:   PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
 28:   PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);

 30:   /*
 31:      Create a vector, specifying only its global dimension.
 32:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format
 33:      (currently parallel, shared, or sequential) is determined at runtime.  Also, the
 34:      parallel 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 -vec_type mpi or
 45:      -vec_type shared causes the particular type of vector to be formed.

 47:   */

 49:   VecCreate(PETSC_COMM_WORLD,&x);
 50:   VecSetSizes(x,PETSC_DECIDE,n);
 51:   VecSetFromOptions(x);
 52:   /*
 53:      Duplicate some work vectors (of the same format and
 54:      partitioning as the initial vector).
 55:   */
 56:   VecDuplicate(x,&y);
 57:   VecDuplicate(x,&w);

 59:   /*
 60:      Duplicate more work vectors (of the same format and
 61:      partitioning as the initial vector).  Here we duplicate
 62:      an array of vectors, which is often more convenient than
 63:      duplicating individual ones.
 64:   */
 65:   VecDuplicateVecs(x,3,&z);
 66:   /*
 67:      Set the vectors to entries to a constant value.
 68:   */
 69:   VecSet(x,one);
 70:   VecSet(y,two);
 71:   VecSet(z[0],one);
 72:   VecSet(z[1],two);
 73:   VecSet(z[2],three);
 74:   /*
 75:      Demonstrate various basic vector routines.
 76:   */
 77:   VecDot(x,y,&dot);
 78:   VecMDot(x,3,z,dots);

 80:   /*
 81:      Note: If using a complex numbers version of PETSc, then
 82:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
 83:      (when using real numbers) it is undefined.
 84:   */

 86:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %D\n",n);
 87:   VecMax(x,&maxind,&maxval);
 88:   PetscPrintf(PETSC_COMM_WORLD,"VecMax %g, VecInd %D\n",(double)maxval,maxind);

 90:   VecMin(x,&maxind,&maxval);
 91:   PetscPrintf(PETSC_COMM_WORLD,"VecMin %g, VecInd %D\n",(double)maxval,maxind);
 92:   PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zero\n");


 95:   VecScale(x,two);
 96:   VecNorm(x,NORM_2,&norm);
 97:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
 98:   PetscPrintf(PETSC_COMM_WORLD,"VecScale %g\n",(double)v);


101:   VecCopy(x,w);
102:   VecNorm(w,NORM_2,&norm);
103:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
104:   PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %g\n",(double)v);

106:   VecAXPY(y,three,x);
107:   VecNorm(y,NORM_2,&norm);
108:   v    = norm-8.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
109:   PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %g\n",(double)v);

111:   VecAYPX(y,two,x);
112:   VecNorm(y,NORM_2,&norm);
113:   v    = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
114:   PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %g\n",(double)v);

116:   VecSwap(x,y);
117:   VecNorm(y,NORM_2,&norm);
118:   v    = norm-2.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
119:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);
120:   VecNorm(x,NORM_2,&norm);
121:   v = norm-18.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
122:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %g\n",(double)v);

124:   VecWAXPY(w,two,x,y);
125:   VecNorm(w,NORM_2,&norm);
126:   v    = norm-38.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
127:   PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %g\n",(double)v);

129:   VecPointwiseMult(w,y,x);
130:   VecNorm(w,NORM_2,&norm);
131:   v    = norm-36.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
132:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %g\n",(double)v);

134:   VecPointwiseDivide(w,x,y);
135:   VecNorm(w,NORM_2,&norm);
136:   v    = norm-9.0*PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
137:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %g\n",(double)v);

139:   dots[0] = one;
140:   dots[1] = three;
141:   dots[2] = two;

143:   VecSet(x,one);
144:   VecMAXPY(x,3,dots,z);
145:   VecNorm(z[0],NORM_2,&norm);
146:   v    = norm-PetscSqrtReal((PetscReal)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
147:   VecNorm(z[1],NORM_2,&norm);
148:   v1   = norm-2.0*PetscSqrtReal((PetscReal)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
149:   VecNorm(z[2],NORM_2,&norm);
150:   v2   = norm-3.0*PetscSqrtReal((PetscReal)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
151:   PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g \n",(double)v,(double)v1,(double)v2);

153:   /*
154:      Free work space.  All PETSc objects should be destroyed when they
155:      are no longer needed.
156:   */
157:   VecDestroy(&x);
158:   VecDestroy(&y);
159:   VecDestroy(&w);
160:   VecDestroyVecs(3,&z);
161:   PetscFinalize();
162:   return ierr;
163: }