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: int main(int argc,char **argv)
34: {
35: Vec x, y, w; /* vectors */
36: Vec *z; /* array of vectors */
37: PetscReal norm, v, v1, v2;
38: int n = 20;
39: PetscTruth flg;
40: PetscScalar one = 1.0, two = 2.0, three = 3.0, dots[3], dot;
42: _ PetscInitialize(&argc,&argv,(char*)0,help);___
43: _ PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);___
45: /*
46: Create a vector, specifying only its global dimension.
47: When using VecCreate(), VecSetSizes() and VecSetFromOptions(), the vector format
48: (currently parallel, shared, or sequential) is determined at runtime. Also, the
49: parallel partitioning of the vector is determined by PETSc at runtime.
51: Routines for creating particular vector types directly are:
52: VecCreateSeq() - uniprocessor vector
53: VecCreateMPI() - distributed vector, where the user can
54: determine the parallel partitioning
55: VecCreateShared() - parallel vector that uses shared memory
56: (available only on the SGI); otherwise,
57: is the same as VecCreateMPI()
59: With VecCreate(), VecSetSizes() and VecSetFromOptions() the option -vec_type mpi or
60: -vec_type shared causes the particular type of vector to be formed.
62: */
63: _ VecCreate(PETSC_COMM_WORLD,&x);___
64: _ VecSetSizes(x,PETSC_DECIDE,n);___
65: _ VecSetFromOptions(x);___
67: /*
68: Duplicate some work vectors (of the same format and
69: partitioning as the initial vector).
70: */
71: _ VecDuplicate(x,&y);___
72: _ VecDuplicate(x,&w);___
74: /*
75: Duplicate more work vectors (of the same format and
76: partitioning as the initial vector). Here we duplicate
77: an array of vectors, which is often more convenient than
78: duplicating individual ones.
79: */
80: _ VecDuplicateVecs(x,3,&z);___
82: /*
83: Set the vectors to entries to a constant value.
84: */
85: _ VecSet(&one,x);___
86: _ VecSet(&two,y);___
87: _ VecSet(&one,z[0]);___
88: _ VecSet(&two,z[1]);___
89: _ VecSet(&three,z[2]);___
91: /*
92: Demonstrate various basic vector routines.
93: */
94: _ VecDot(x,x,&dot);___
95: _ VecMDot(3,x,z,dots);___
97: /*
98: Note: If using a complex numbers version of PETSc, then
99: PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
100: (when using real numbers) it is undefined.
101: */
102: #if defined(PETSC_USE_COMPLEX)
103: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn", int (PetscRealPart(dot)));___
104: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)PetscRealPart(dots[0]),
105: (int)PetscRealPart(dots[1]),(int)PetscRealPart(dots[2]));___
106: #else
107: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",(int) dot);___
108: _ PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)dots[0],
109: (int)dots[1],(int)dots[2]);___
110: #endif
112: _ PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zeron");___
114: _ VecScale(&two,x);___
115: _ VecNorm(x,NORM_2,&norm);___
116: v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
117: _ PetscPrintf(PETSC_COMM_WORLD,"VecScale %gn",v);___
119: _ VecCopy(x,w);___
120: _ VecNorm(w,NORM_2,&norm);___
121: v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
122: _ PetscPrintf(PETSC_COMM_WORLD,"VecCopy %gn",v);___
124: _ VecAXPY(&three,x,y);___
125: _ VecNorm(y,NORM_2,&norm);___
126: v = norm-8.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
127: _ PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);___
129: _ VecAYPX(&two,x,y);___
130: _ VecNorm(y,NORM_2,&norm);___
131: v = norm-18.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
132: _ PetscPrintf(PETSC_COMM_WORLD,"VecAYPX %gn",v);___
134: _ VecSwap(x,y);___
135: _ VecNorm(y,NORM_2,&norm);___
136: v = norm-2.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
137: _ PetscPrintf(PETSC_COMM_WORLD,"VecSwap %gn",v);___
138: _ VecNorm(x,NORM_2,&norm);___
139: v = norm-18.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
140: _ PetscPrintf(PETSC_COMM_WORLD,"VecSwap %gn",v);___
142: _ VecWAXPY(&two,x,y,w);___
143: _ VecNorm(w,NORM_2,&norm);___
144: v = norm-38.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
145: _ PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %gn",v);___
147: _ VecPointwiseMult(y,x,w);___
148: _ VecNorm(w,NORM_2,&norm);___
149: v = norm-36.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
150: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %gn",v);___
152: _ VecPointwiseDivide(x,y,w);___
153: _ VecNorm(w,NORM_2,&norm);___
154: v = norm-9.0*sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
155: _ PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %gn",v);___
157: dots[0] = one;
158: dots[1] = three;
159: dots[2] = two;
160: _ VecSet(&one,x);___
161: _ VecMAXPY(3,dots,x,z);___
162: _ VecNorm(z[0],NORM_2,&norm);___
163: v = norm-sqrt((PetscReal) n); if (v > -PETSC_EPS && v < PETSC_EPS) v = 0.0;
164: _ VecNorm(z[1],NORM_2,&norm);___
165: v1 = norm-2.0*sqrt((PetscReal) n); if (v1 > -PETSC_EPS && v1 < PETSC_EPS) v1 = 0.0;
166: _ VecNorm(z[2],NORM_2,&norm);___
167: v2 = norm-3.0*sqrt((PetscReal) n); if (v2 > -PETSC_EPS && v2 < PETSC_EPS) v2 = 0.0;
168: _ PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g n",v,v1,v2);___
170: /*
171: Test whether vector has been corrupted (just to demonstrate this
172: routine) not needed in most application codes.
173: */
174: _ VecValid(x,&flg);___
175: if (!flg) SETERRQ(1,"Corrupted vector.");
177: /*
178: Free work space. All PETSc objects should be destroyed when they
179: are no longer needed.
180: */
181: _ VecDestroy(x);___
182: _ VecDestroy(y);___
183: _ VecDestroy(w);___
184: _ VecDestroyVecs(z,3);___
185: _ PetscFinalize();___
186: return 0;
187: }
188: