Actual source code: test1.c
slepc-3.18.0 2022-10-01
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
11: static char help[] = "Test VecComp.\n\n";
13: #include <slepcsys.h>
15: int main(int argc,char **argv)
16: {
17: Vec v,w,x,y,vc,wc,xc,yc,vparent,vchild[2],vecs[2];
18: const Vec *varray;
19: PetscMPIInt size,rank;
20: PetscInt i,n,k,Nx[2];
21: PetscReal norm,normc,norm12[2],norm12c[2],vmax,vmin;
22: PetscScalar dot[2],dotc[2];
25: SlepcInitialize(&argc,&argv,(char*)0,help);
26: MPI_Comm_size(PETSC_COMM_WORLD,&size);
27: MPI_Comm_rank(PETSC_COMM_WORLD,&rank);
29: PetscPrintf(PETSC_COMM_WORLD,"VecComp test\n");
31: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32: Create standard vectors
33: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
35: VecCreate(PETSC_COMM_WORLD,&v);
36: VecSetSizes(v,8/size,8);
37: VecSetFromOptions(v);
39: if (!rank) {
40: VecSetValue(v,0,2.0,INSERT_VALUES);
41: VecSetValue(v,1,-1.0,INSERT_VALUES);
42: VecSetValue(v,2,3.0,INSERT_VALUES);
43: VecSetValue(v,3,3.5,INSERT_VALUES);
44: }
45: if ((!rank && size==1) || (rank && size==2)) {
46: VecSetValue(v,4,1.2,INSERT_VALUES);
47: VecSetValue(v,5,1.8,INSERT_VALUES);
48: VecSetValue(v,6,-2.2,INSERT_VALUES);
49: VecSetValue(v,7,2.0,INSERT_VALUES);
50: }
51: VecAssemblyBegin(v);
52: VecAssemblyEnd(v);
53: VecDuplicate(v,&w);
54: VecSet(w,1.0);
55: VecDuplicate(v,&x);
56: VecDuplicate(v,&y);
57: if (!rank) VecSetValue(y,0,1.0,INSERT_VALUES);
58: VecAssemblyBegin(y);
59: VecAssemblyEnd(y);
61: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
62: Create veccomp vectors
63: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
65: VecCreate(PETSC_COMM_WORLD,&vparent);
66: VecSetSizes(vparent,4/size,4);
67: VecSetFromOptions(vparent);
69: /* create a veccomp vector with two subvectors */
70: VecDuplicate(vparent,&vchild[0]);
71: VecDuplicate(vparent,&vchild[1]);
72: if (!rank) {
73: VecSetValue(vchild[0],0,2.0,INSERT_VALUES);
74: VecSetValue(vchild[0],1,-1.0,INSERT_VALUES);
75: VecSetValue(vchild[1],0,1.2,INSERT_VALUES);
76: VecSetValue(vchild[1],1,1.8,INSERT_VALUES);
77: }
78: if ((!rank && size==1) || (rank && size==2)) {
79: VecSetValue(vchild[0],2,3.0,INSERT_VALUES);
80: VecSetValue(vchild[0],3,3.5,INSERT_VALUES);
81: VecSetValue(vchild[1],2,-2.2,INSERT_VALUES);
82: VecSetValue(vchild[1],3,2.0,INSERT_VALUES);
83: }
84: VecAssemblyBegin(vchild[0]);
85: VecAssemblyBegin(vchild[1]);
86: VecAssemblyEnd(vchild[0]);
87: VecAssemblyEnd(vchild[1]);
88: VecCreateCompWithVecs(vchild,2,vparent,&vc);
89: VecDestroy(&vchild[0]);
90: VecDestroy(&vchild[1]);
91: VecView(vc,NULL);
93: VecGetSize(vc,&k);
96: /* create an empty veccomp vector with two subvectors */
97: Nx[0] = 4;
98: Nx[1] = 4;
99: VecCreateComp(PETSC_COMM_WORLD,Nx,2,VECSTANDARD,vparent,&wc);
100: VecCompGetSubVecs(wc,&n,&varray);
102: for (i=0;i<2;i++) VecSet(varray[i],1.0);
104: VecGetSize(wc,&k);
107: /* duplicate a veccomp */
108: VecDuplicate(vc,&xc);
110: /* create a veccomp via VecSetType */
111: VecCreate(PETSC_COMM_WORLD,&yc);
112: VecSetType(yc,VECCOMP);
113: VecSetSizes(yc,8/size,8);
114: VecCompSetSubVecs(yc,2,NULL);
116: VecCompGetSubVecs(yc,&n,&varray);
118: if (!rank) VecSetValue(varray[0],0,1.0,INSERT_VALUES);
119: VecAssemblyBegin(varray[0]);
120: VecAssemblyEnd(varray[0]);
122: /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
123: Operate with vectors
124: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
126: VecCopy(w,x);
127: VecAXPBY(x,1.0,-2.0,v);
128: VecNorm(x,NORM_2,&norm);
129: VecCopy(wc,xc);
130: VecAXPBY(xc,1.0,-2.0,vc);
131: VecNorm(xc,NORM_2,&normc);
134: VecCopy(w,x);
135: VecWAXPY(x,-2.0,w,v);
136: VecNorm(x,NORM_2,&norm);
137: VecCopy(wc,xc);
138: VecWAXPY(xc,-2.0,wc,vc);
139: VecNorm(xc,NORM_2,&normc);
142: VecAXPBYPCZ(y,3.0,-1.0,1.0,w,v);
143: VecNorm(y,NORM_2,&norm);
144: VecAXPBYPCZ(yc,3.0,-1.0,1.0,wc,vc);
145: VecNorm(yc,NORM_2,&normc);
148: VecMax(xc,NULL,&vmax);
149: VecMin(xc,NULL,&vmin);
150: PetscPrintf(PETSC_COMM_WORLD,"xc has max value %g min value %g\n",(double)vmax,(double)vmin);
152: VecMaxPointwiseDivide(wc,xc,&vmax);
153: PetscPrintf(PETSC_COMM_WORLD,"wc/xc has max value %g\n",(double)vmax);
155: VecDot(x,y,&dot[0]);
156: VecDot(xc,yc,&dotc[0]);
158: VecTDot(x,y,&dot[0]);
159: VecTDot(xc,yc,&dotc[0]);
162: vecs[0] = w; vecs[1] = y;
163: VecMDot(x,2,vecs,dot);
164: vecs[0] = wc; vecs[1] = yc;
165: VecMDot(xc,2,vecs,dotc);
167: vecs[0] = w; vecs[1] = y;
168: VecMTDot(x,2,vecs,dot);
169: vecs[0] = wc; vecs[1] = yc;
170: VecMTDot(xc,2,vecs,dotc);
173: VecDotNorm2(x,y,&dot[0],&norm);
174: VecDotNorm2(xc,yc,&dotc[0],&normc);
178: VecAbs(w);
179: VecAbs(wc);
180: VecConjugate(x);
181: VecConjugate(xc);
182: VecShift(y,0.5);
183: VecShift(yc,0.5);
184: VecReciprocal(y);
185: VecReciprocal(yc);
186: VecExp(y);
187: VecExp(yc);
188: VecLog(y);
189: VecLog(yc);
190: VecNorm(y,NORM_1,&norm);
191: VecNorm(yc,NORM_1,&normc);
194: VecPointwiseMult(w,x,y);
195: VecPointwiseMult(wc,xc,yc);
196: VecNorm(w,NORM_INFINITY,&norm);
197: VecNorm(wc,NORM_INFINITY,&normc);
200: VecPointwiseMax(w,x,y);
201: VecPointwiseMax(wc,xc,yc);
202: VecNorm(w,NORM_INFINITY,&norm);
203: VecNorm(wc,NORM_INFINITY,&normc);
206: VecSwap(x,y);
207: VecSwap(xc,yc);
208: VecPointwiseDivide(w,x,y);
209: VecPointwiseDivide(wc,xc,yc);
210: VecScale(w,0.3);
211: VecScale(wc,0.3);
212: VecSqrtAbs(w);
213: VecSqrtAbs(wc);
214: VecNorm(w,NORM_1_AND_2,norm12);
215: VecNorm(wc,NORM_1_AND_2,norm12c);
218: VecPointwiseMin(w,x,y);
219: VecPointwiseMin(wc,xc,yc);
220: VecPointwiseMaxAbs(x,y,w);
221: VecPointwiseMaxAbs(xc,yc,wc);
222: VecNorm(x,NORM_INFINITY,&norm);
223: VecNorm(xc,NORM_INFINITY,&normc);
226: VecSetRandom(wc,NULL);
228: VecDestroy(&v);
229: VecDestroy(&w);
230: VecDestroy(&x);
231: VecDestroy(&y);
232: VecDestroy(&vparent);
233: VecDestroy(&vc);
234: VecDestroy(&wc);
235: VecDestroy(&xc);
236: VecDestroy(&yc);
237: SlepcFinalize();
238: return 0;
239: }
241: /*TEST
243: test:
244: suffix: 1
246: test:
247: suffix: 2
248: nsize: 2
250: TEST*/