Actual source code: ex1.c
1: /*$Id: ex1.c,v 1.68 2001/03/23 23:21:37 balay 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: double norm,v,v1,v2;
26: int n = 20,ierr;
27: PetscTruth flg;
28: Scalar 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() and VecSetFromOptions(), the vector format (currently parallel,
36: shared, or sequential) is determined at runtime. Also, the parallel
37: 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() and VecSetFromOptions() the option -vec_type mpi or -vec_type shared causes the
48: particular type of vector to be formed.
50: */
51: VecCreate(PETSC_COMM_WORLD,PETSC_DECIDE,n,&x);
52: VecSetFromOptions(x);
54: /*
55: Duplicate some work vectors (of the same format and
56: partitioning as the initial vector).
57: */
58: VecDuplicate(x,&y);
59: VecDuplicate(x,&w);
61: /*
62: Duplicate more work vectors (of the same format and
63: partitioning as the initial vector). Here we duplicate
64: an array of vectors, which is often more convenient than
65: duplicating individual ones.
66: */
67: VecDuplicateVecs(x,3,&z);
69: /*
70: Set the vectors to entries to a constant value.
71: */
72: VecSet(&one,x);
73: VecSet(&two,y);
74: VecSet(&one,z[0]);
75: VecSet(&two,z[1]);
76: VecSet(&three,z[2]);
78: /*
79: Demonstrate various basic vector routines.
80: */
81: VecDot(x,x,&dot);
82: VecMDot(3,x,z,dots);
84: /*
85: Note: If using a complex numbers version of PETSc, then
86: PETSC_USE_COMPLEX is defined in the makefiles; otherwise,
87: (when using real numbers) it is undefined.
88: */
89: #if defined(PETSC_USE_COMPLEX)
90: PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",int (PetscRealPart(dot)));
91: PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)PetscRealPart(dots[0]),
92: (int)PetscRealPart(dots[1]),(int)PetscRealPart(dots[2]));
93: #else
94: PetscPrintf(PETSC_COMM_WORLD,"Vector length %dn",(int)dot);
95: PetscPrintf(PETSC_COMM_WORLD,"Vector length %d %d %dn",(int)dots[0],
96: (int)dots[1],(int)dots[2]);
97: #endif
99: PetscPrintf(PETSC_COMM_WORLD,"All other values should be near zeron");
101: VecScale(&two,x);
102: VecNorm(x,NORM_2,&norm);
103: v = norm-2.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
104: PetscPrintf(PETSC_COMM_WORLD,"VecScale %gn",v);
106: VecCopy(x,w);
107: VecNorm(w,NORM_2,&norm);
108: v = norm-2.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
109: PetscPrintf(PETSC_COMM_WORLD,"VecCopy %gn",v);
111: VecAXPY(&three,x,y);
112: VecNorm(y,NORM_2,&norm);
113: v = norm-8.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
114: PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);
116: VecAYPX(&two,x,y);
117: VecNorm(y,NORM_2,&norm);
118: v = norm-18.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
119: PetscPrintf(PETSC_COMM_WORLD,"VecAXPY %gn",v);
121: VecSwap(x,y);
122: VecNorm(y,NORM_2,&norm);
123: v = norm-2.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
124: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %gn",v);
125: VecNorm(x,NORM_2,&norm);
126: v = norm-18.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
127: PetscPrintf(PETSC_COMM_WORLD,"VecSwap %gn",v);
129: VecWAXPY(&two,x,y,w);
130: VecNorm(w,NORM_2,&norm);
131: v = norm-38.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
132: PetscPrintf(PETSC_COMM_WORLD,"VecWAXPY %gn",v);
134: VecPointwiseMult(y,x,w);
135: VecNorm(w,NORM_2,&norm);
136: v = norm-36.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
137: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseMult %gn",v);
139: VecPointwiseDivide(x,y,w);
140: VecNorm(w,NORM_2,&norm);
141: v = norm-9.0*sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
142: PetscPrintf(PETSC_COMM_WORLD,"VecPointwiseDivide %gn",v);
144: dots[0] = one;
145: dots[1] = three;
146: dots[2] = two;
147: VecSet(&one,x);
148: VecMAXPY(3,dots,x,z);
149: VecNorm(z[0],NORM_2,&norm);
150: v = norm-sqrt((double)n); if (v > -1.e-10 && v < 1.e-10) v = 0.0;
151: VecNorm(z[1],NORM_2,&norm);
152: v1 = norm-2.0*sqrt((double)n); if (v1 > -1.e-10 && v1 < 1.e-10) v1 = 0.0;
153: VecNorm(z[2],NORM_2,&norm);
154: v2 = norm-3.0*sqrt((double)n); if (v2 > -1.e-10 && v2 < 1.e-10) v2 = 0.0;
155: PetscPrintf(PETSC_COMM_WORLD,"VecMAXPY %g %g %g n",v,v1,v2);
157: /*
158: Test whether vector has been corrupted (just to demonstrate this
159: routine) not needed in most application codes.
160: */
161: VecValid(x,&flg);
162: if (!flg) SETERRQ(1,"Corrupted vector.");
164: /*
165: Free work space. All PETSc objects should be destroyed when they
166: are no longer needed.
167: */
168: VecDestroy(x);
169: VecDestroy(y);
170: VecDestroy(w);
171: VecDestroyVecs(z,3);
172: PetscFinalize();
173: return 0;
174: }
175: