Actual source code: ex1e.c

  1: /*$Id: ex1e.c,v 1.10 2001/03/23 23:21:37 balay 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: int main(int argc,char **argv)
 28: {
 29:   Vec        x, y, w;               /* vectors */
 30:   Vec        *z;                    /* array of vectors */
 31:   double     norm, v, v1, v2;
 32:   int        n = 20;
 33:   PetscTruth flg;
 34:   Scalar     one = 1.0, two = 2.0, three = 3.0, dots[3], dot;

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

 39:   /* 
 40:      Create a vector, specifying only its global dimension.
 41:      When using VecCreate() and VecSetFromOptions(), the vector format (currently parallel,
 42:      shared, or sequential) is determined at runtime.  Also, the parallel
 43:      partitioning of the vector is determined by PETSc at runtime.

 45:      Routines for creating particular vector types directly are:
 46:         VecCreateSeq() - uniprocessor vector
 47:         VecCreateMPI() - distributed vector, where the user can
 48:                          determine the parallel partitioning
 49:         VecCreateShared() - parallel vector that uses shared memory
 50:                             (available only on the SGI); otherwise,
 51:                             is the same as VecCreateMPI()

 53:      With VecCreate() and VecSetFromOptions() the option -vec_type mpi or -vec_type shared causes the 
 54:      particular type of vector to be formed.

 56:   */
 57: _ VecCreate(PETSC_COMM_WORLD,PETSC_DECIDE,n,&x);___
 58: _ VecSetFromOptions(x);___

 60:   /*
 61:      Duplicate some work vectors (of the same format and
 62:      partitioning as the initial vector).
 63:   */
 64: _ VecDuplicate(x,&y);___
 65: _ VecDuplicate(x,&w);___

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

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

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

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

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

107: _ VecScale(&two,x);___
108: _ VecNorm(x,NORM_2,&norm);___
109:   v = norm-2.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
110: _ PetscPrintf(PETSC_COMM_WORLD,"VecScale %gn",v);___

112: _ VecCopy(x,w);___
113: _ VecNorm(w,NORM_2,&norm);___
114:   v = norm-2.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
115: _ PetscPrintf(PETSC_COMM_WORLD,"VecCopy  %gn",v);___

117: _ VecAXPY(&three,x,y);___
118: _ VecNorm(y,NORM_2,&norm);___
119:   v = norm-8.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
120: _ PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);___

122: _ VecAYPX(&two,x,y);___
123: _ VecNorm(y,NORM_2,&norm);___
124:   v = norm-18.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
125: _ PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);___

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

135: _ VecWAXPY(&two,x,y,w);___
136: _ VecNorm(w,NORM_2,&norm);___
137:   v = norm-38.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
138: _ PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %gn",v);___

140: _ VecPointwiseMult(y,x,w);___
141: _ VecNorm(w,NORM_2,&norm);___
142:   v = norm-36.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
143: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %gn",v);___

145: _ VecPointwiseDivide(x,y,w);___
146: _ VecNorm(w,NORM_2,&norm);___
147:   v = norm-9.0*sqrt((double) n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
148: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %gn",v);___

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

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

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