Actual source code: ex1.c

  1: /*$Id: ex1.c,v 1.74 2001/09/19 16:08:15 bsmith Exp $*/

  3: /* Program usage:  mpirun ex1 [-help] [all PETSc options] */

  5: static char help[] = "Basic vector routines.nn";

  7: /*T
  8:    Concepts: vectors^basic routines;
  9:    Processors: n
 10: T*/

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

 19:  #include petscvec.h

 21: int main(int argc,char **argv)
 22: {
 23:   Vec         x,y,w;               /* vectors */
 24:   Vec         *z;                    /* array of vectors */
 25:   PetscReal   norm,v,v1,v2;
 26:   int         n = 20,ierr;
 27:   PetscTruth  flg;
 28:   PetscScalar one = 1.0,two = 2.0,three = 3.0,dots[3],dot;

 30:   PetscInitialize(&argc,&argv,(char*)0,help);
 31:   PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);

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

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

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

 50:   */

 52:   VecCreate(PETSC_COMM_WORLD,&x);
 53:   VecSetSizes(x,PETSC_DECIDE,n);
 54:   VecSetFromOptions(x);

 56:   /*
 57:      Duplicate some work vectors (of the same format and
 58:      partitioning as the initial vector).
 59:   */
 60:   VecDuplicate(x,&y);
 61:   VecDuplicate(x,&w);
 62:   VecNorm(w,NORM_2,&norm);


 65:   /*
 66:      Duplicate more work vectors (of the same format and
 67:      partitioning as the initial vector).  Here we duplicate
 68:      an array of vectors, which is often more convenient than
 69:      duplicating individual ones.
 70:   */
 71:   VecDuplicateVecs(x,3,&z);

 73:   /*
 74:      Set the vectors to entries to a constant value.
 75:   */
 76:   VecSet(&one,x);
 77:   VecSet(&two,y);
 78:   VecSet(&one,z[0]);
 79:   VecSet(&two,z[1]);
 80:   VecSet(&three,z[2]);

 82:   /*
 83:      Demonstrate various basic vector routines.
 84:   */
 85:   VecDot(x,x,&dot);
 86:   VecMDot(3,x,z,dots);

 88:   /* 
 89:      Note: If using a complex numbers version of PETSc, then
 90:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
 91:      (when using real numbers) it is undefined.
 92:   */
 93: #if defined(PETSC_USE_COMPLEX)
 94:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",int (PetscRealPart(dot)));
 95:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)PetscRealPart(dots[0]),
 96:                              (int)PetscRealPart(dots[1]),(int)PetscRealPart(dots[2]));
 97: #else
 98:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",(int)dot);
 99:   PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)dots[0],
100:                              (int)dots[1],(int)dots[2]);
101: #endif

103:   PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zeron");

105:   VecScale(&two,x);
106:   VecNorm(x,NORM_2,&norm);
107:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
108:   PetscPrintf(PETSC_COMM_WORLD,"VecScale %gn",v);


111:   VecCopy(x,w);
112:   VecNorm(w,NORM_2,&norm);
113:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
114:   PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %gn",v);

116:   VecAXPY(&three,x,y);
117:   VecNorm(y,NORM_2,&norm);
118:   v = norm-8.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
119:   PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);

121:   VecAYPX(&two,x,y);
122:   VecNorm(y,NORM_2,&norm);
123:   v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
124:   PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %gn",v);

126:   VecSwap(x,y);
127:   VecNorm(y,NORM_2,&norm);
128:   v = norm-2.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
129:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %gn",v);
130:   VecNorm(x,NORM_2,&norm);
131:   v = norm-18.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
132:   PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %gn",v);

134:   VecWAXPY(&two,x,y,w);
135:   VecNorm(w,NORM_2,&norm);
136:   v = norm-38.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
137:   PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %gn",v);

139:   VecPointwiseMult(y,x,w);
140:   VecNorm(w,NORM_2,&norm);
141:   v = norm-36.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
142:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %gn",v);

144:   VecPointwiseDivide(x,y,w);
145:   VecNorm(w,NORM_2,&norm);
146:   v = norm-9.0*sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
147:   PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %gn",v);

149:   dots[0] = one;
150:   dots[1] = three;
151:   dots[2] = two;
152:   VecSet(&one,x);
153:   VecMAXPY(3,dots,x,z);
154:   VecNorm(z[0],NORM_2,&norm);
155:   v = norm-sqrt((double)n); if (v > -PETSC_SMALL && v < PETSC_SMALL) v = 0.0;
156:   VecNorm(z[1],NORM_2,&norm);
157:   v1 = norm-2.0*sqrt((double)n); if (v1 > -PETSC_SMALL && v1 < PETSC_SMALL) v1 = 0.0;
158:   VecNorm(z[2],NORM_2,&norm);
159:   v2 = norm-3.0*sqrt((double)n); if (v2 > -PETSC_SMALL && v2 < PETSC_SMALL) v2 = 0.0;
160:   PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g n",v,v1,v2);

162:   /* 
163:      Test whether vector has been corrupted (just to demonstrate this
164:      routine) not needed in most application codes.
165:   */
166:   VecValid(x,&flg);
167:   if (!flg) SETERRQ(1,"Corrupted vector.");

169:   /* 
170:      Free work space.  All PETSc objects should be destroyed when they
171:      are no longer needed.
172:   */
173:   VecDestroy(x);
174:   VecDestroy(y);
175:   VecDestroy(w);
176:   VecDestroyVecs(z,3);
177:   PetscFinalize();
178:   return 0;
179: }
180: