Actual source code: ex1e.c

  1: /*$Id: ex1e.c,v 1.15 2001/09/11 18:05:40 bsmith Exp $*/

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

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

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

 12: /* 

 14:    This uses the PETSc _ error checking routines. Put _ before the PETSc function call
 15:   and __ after the call (or ___ in a subroutine, not the main program). This is equivalent
 16:   to using the ...  macros


 19:   Include "petscvec.h" so that we can use vectors.  Note that this file
 20:   automatically includes:
 21:      petsc.h       - base PETSc routines   petscis.h     - index sets
 22:      petscsys.h    - system routines       petscviewer.h - viewers
 23: */

 25:  #include petscvec.h

 27: #if defined(PETSC_USE_SINGLE)
 28: #define PETSC_EPS 1.e-5
 29: #else
 30: #define PETSC_EPS 1.e-10
 31: #endif

 33: #undef __FUNCT__
 35: int main(int argc,char **argv)
 36: {
 37:   Vec         x, y, w;               /* vectors */
 38:   Vec         *z;                    /* array of vectors */
 39:   PetscReal   norm, v, v1, v2;
 40:   int         n = 20;
 41:   PetscTruth  flg;
 42:   PetscScalar one = 1.0, two = 2.0, three = 3.0, dots[3], dot;

 44: _ PetscInitialize(&argc,&argv,(char*)0,help);___
 45: _ PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);___

 47:   /* 
 48:      Create a vector, specifying only its global dimension.
 49:      When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format 
 50:      (currently parallel, shared, or sequential) is determined at runtime.  Also, the 
 51:      parallel partitioning of the vector is determined by PETSc at runtime.

 53:      Routines for creating particular vector types directly are:
 54:         VecCreateSeq() - uniprocessor vector
 55:         VecCreateMPI() - distributed vector, where the user can
 56:                          determine the parallel partitioning
 57:         VecCreateShared() - parallel vector that uses shared memory
 58:                             (available only on the SGI); otherwise,
 59:                             is the same as VecCreateMPI()

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

 64:   */
 65: _ VecCreate(PETSC_COMM_WORLD,&x);___
 66: _ VecSetSizes(x,PETSC_DECIDE,n);___
 67: _ VecSetFromOptions(x);___

 69:   /*
 70:      Duplicate some work vectors (of the same format and
 71:      partitioning as the initial vector).
 72:   */
 73: _ VecDuplicate(x,&y);___
 74: _ VecDuplicate(x,&w);___

 76:   /*
 77:      Duplicate more work vectors (of the same format and
 78:      partitioning as the initial vector).  Here we duplicate
 79:      an array of vectors, which is often more convenient than
 80:      duplicating individual ones.
 81:   */
 82: _ VecDuplicateVecs(x,3,&z);___

 84:   /*
 85:      Set the vectors to entries to a constant value.
 86:   */
 87: _ VecSet(&one,x);___
 88: _ VecSet(&two,y);___
 89: _ VecSet(&one,z[0]);___
 90: _ VecSet(&two,z[1]);___
 91: _ VecSet(&three,z[2]);___

 93:   /*
 94:      Demonstrate various basic vector routines.
 95:   */
 96: _ VecDot(x,x,&dot);___
 97: _ VecMDot(3,x,z,dots);___

 99:   /* 
100:      Note: If using a complex numbers version of PETSc, then
101:      PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
102:      (when using real numbers) it is undefined.
103:   */
104: #if defined(PETSC_USE_COMPLEX)
105: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn", int (PetscRealPart(dot)));___
106: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)PetscRealPart(dots[0]),
107:                              (int)PetscRealPart(dots[1]),(int)PetscRealPart(dots[2]));___
108: #else
109: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",(int) dot);___
110: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)dots[0],
111:                              (int)dots[1],(int)dots[2]);___
112: #endif

114: _ PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zeron");___

116: _ VecScale(&two,x);___
117: _ VecNorm(x,NORM_2,&norm);___
118:   v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
119: _ PetscPrintf(PETSC_COMM_WORLD,"VecScale %gn",v);___

121: _ VecCopy(x,w);___
122: _ VecNorm(w,NORM_2,&norm);___
123:   v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
124: _ PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %gn",v);___

126: _ VecAXPY(&three,x,y);___
127: _ VecNorm(y,NORM_2,&norm);___
128:   v = norm-8.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
129: _ PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);___

131: _ VecAYPX(&two,x,y);___
132: _ VecNorm(y,NORM_2,&norm);___
133:   v = norm-18.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
134: _ PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %gn",v);___

136: _ VecSwap(x,y);___
137: _ VecNorm(y,NORM_2,&norm);___
138:   v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
139: _ PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %gn",v);___
140: _ VecNorm(x,NORM_2,&norm);___
141:   v = norm-18.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
142: _ PetscPrintf(PETSC_COMM_WORLD,"VecSwap  %gn",v);___

144: _ VecWAXPY(&two,x,y,w);___
145: _ VecNorm(w,NORM_2,&norm);___
146:   v = norm-38.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
147: _ PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %gn",v);___

149: _ VecPointwiseMult(y,x,w);___
150: _ VecNorm(w,NORM_2,&norm);___
151:   v = norm-36.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
152: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %gn",v);___

154: _ VecPointwiseDivide(x,y,w);___
155: _ VecNorm(w,NORM_2,&norm);___
156:   v = norm-9.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
157: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %gn",v);___

159:   dots[0] = one;
160:   dots[1] = three;
161:   dots[2] = two;
162: _ VecSet(&one,x);___
163: _ VecMAXPY(3,dots,x,z);___
164: _ VecNorm(z[0],NORM_2,&norm);___
165:   v = norm-sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
166: _ VecNorm(z[1],NORM_2,&norm);___
167:   v1 = norm-2.0*sqrt((PetscReal) n); if (v1 > -PETSC_EPS && v1 < PETSC_EPS) v1 = 0.0;
168: _ VecNorm(z[2],NORM_2,&norm);___
169:   v2 = norm-3.0*sqrt((PetscReal) n); if (v2 > -PETSC_EPS && v2 < PETSC_EPS) v2 = 0.0;
170: _ PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g n",v,v1,v2);___

172:   /* 
173:      Test whether vector has been corrupted (just to demonstrate this
174:      routine) not needed in most application codes.
175:   */
176: _ VecValid(x,&flg);___
177:   if (!flg) SETERRQ(1,"Corrupted vector.");

179:   /* 
180:      Free work space.  All PETSc objects should be destroyed when they
181:      are no longer needed.
182:   */
183: _ VecDestroy(x);___
184: _ VecDestroy(y);___
185: _ VecDestroy(w);___
186: _ VecDestroyVecs(z,3);___
187: _ PetscFinalize();___
188:   return 0;
189: }
190: