2 // /home/couturie/work/petsc-3.5.1/arch-linux2-c-debug/bin/mpirun -np 3 ./ex45 -da_grid_x 160 -da_grid_y 160 -da_grid_z 160 -ksp_type fgmres
6 Laplacian in 3D. Modeled by the partial differential equation
8 - Laplacian u = 1,0 < x,y,z < 1,
10 with boundary conditions
12 u = 1 for x = 0, x = 1, y = 0, y = 1, z = 0, z = 1.
14 This uses multigrid to solve the linear system
16 See src/snes/examples/tutorials/ex50.c
18 Can also be run with -pc_type exotic -ksp_type fgmres
22 static char help[] = "Solves 3D Laplacian using multigrid.\n\n";
26 #include <petscdmda.h>
28 extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*);
29 extern PetscErrorCode ComputeRHS(KSP,Vec,void*);
30 extern PetscErrorCode ComputeInitialGuess(KSP,Vec,void*);
33 #define __FUNCT__ "main"
41 int KrylovMinimize(Mat A, Vec b, Vec x) {
46 PetscScalar gamma, alpha, oldgamma, beta;
47 PetscReal norm=20, Eprecision=5e-5, cgprec=1e-40;
48 PetscInt giter=0, ColS=12, col=0, Emaxiter=50000000, iter=0, iterations=15, Iiter=0;
60 Vec Alpha, p, ss, vect, r, q, Ax;
65 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
66 ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
67 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
74 ierr = PetscPrintf(PETSC_COMM_WORLD, "Size of vector %D\n", size); CHKERRQ(ierr);
77 MatGetOwnershipRange(A,&aa,&bb);
79 // ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%D %D\n", aa,bb); CHKERRQ(ierr);
80 //PetscSynchronizedFlush(PETSC_COMM_WORLD);
83 ierr = MatCreate(PETSC_COMM_WORLD, &S);CHKERRQ(ierr);
84 ierr = MatSetSizes(S, bb-aa, PETSC_DECIDE, size, ColS); CHKERRQ(ierr);
85 ierr = MatSetType(S, MATMPIDENSE); CHKERRQ(ierr);
86 ierr = MatSetUp(S); CHKERRQ(ierr);
88 ierr = MatGetOwnershipRange(S, &Istart, &Iend); CHKERRQ(ierr);
90 ierr = VecCreate(PETSC_COMM_WORLD, &Alpha); CHKERRQ(ierr);
91 ierr = VecSetSizes(Alpha, PETSC_DECIDE, ColS); CHKERRQ(ierr);
92 ierr = VecSetFromOptions(Alpha); CHKERRQ(ierr);
93 ierr = VecDuplicate(Alpha, &vect); CHKERRQ(ierr);
94 ierr = VecDuplicate(Alpha, &p); CHKERRQ(ierr);
95 ierr = VecDuplicate(Alpha, &ss); CHKERRQ(ierr);
96 ierr = VecDuplicate(b, &r); CHKERRQ(ierr);
97 ierr = VecDuplicate(b, &q); CHKERRQ(ierr);
98 ierr = VecDuplicate(b, &Ax); CHKERRQ(ierr);
100 ierr = VecDuplicate(b,&x_old);CHKERRQ(ierr);
101 ierr = VecDuplicate(b,&residu);CHKERRQ(ierr);
105 //indexes of row (these indexes are global)
106 ind_row = (PetscInt*)malloc(sizeof(PetscInt)*(Iend-Istart));
107 for(i=0; i<Iend-Istart; i++) ind_row[i] = i + Istart;
110 // ierr = KSPGMRESSetRestart(ksp, 16); CHKERRQ(ierr);
111 ierr = KSPSetTolerances(ksp, 1e-10, 1e-10, PETSC_DEFAULT, 16); CHKERRQ(ierr);
112 ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr);
116 //GMRES WITH MINIMIZATION
118 while(giter<Emaxiter && norm>Eprecision ){
119 for(col=0; col<ColS && norm>Eprecision; col++){
123 ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);
125 ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr);
126 total += its; Iiter ++;
131 ierr = VecGetArray(x, &array);
132 ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES);
133 VecRestoreArray(x, &array);
138 ierr = VecCopy(x, residu); CHKERRQ(ierr);
139 ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr);
140 ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr);
144 ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr);
145 ierr = VecCopy(x, x_old); CHKERRQ(ierr);
152 if( norm>Eprecision) {
154 MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY);
155 MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY);
160 MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS);
164 MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS);
169 //Minimization with CGLS
170 MatMult(AS, Alpha, r); VecAYPX(r, -1, b); //r_0 = b-AS*x_0
173 MatMultTranspose(AS, r, p); //p_0 = AS'*r_0
178 ierr = VecCopy(p, ss); CHKERRQ(ierr); //p_0 = s_0
179 VecNorm(ss, NORM_2, &gamma); gamma = gamma * gamma; //gamma = norm2(s)^2
180 while(gamma>cgprec && iter<iterations){
181 MatMult(AS, p, q); //q = AS*p
182 VecNorm(q, NORM_2, &alpha); alpha = alpha * alpha; //alpha = norm2(q)^2
183 alpha = gamma / alpha;
184 VecAXPY(Alpha, alpha, p); //Alpha += alpha*p
185 VecAXPY(r, -alpha, q); //r -= alpha*q
186 MatMultTranspose(AS, r, ss); // ss = AS'*r
188 VecNorm(ss, NORM_2, &gamma); gamma = gamma * gamma; //gamma = norm2(s)^2
189 beta = gamma / oldgamma;
190 VecAYPX(p, beta, ss); //p = s+beta*p;
196 MatMult(S, Alpha, x); //x = S*Alpha;
201 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time : %g (s)\n", T2-T1); CHKERRQ(ierr);
202 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations : %D\n", total); CHKERRQ(ierr);
216 int KrylovMinimizeLSQR(Mat A, Vec b, Vec x) {
221 PetscScalar alpha, beta;
222 PetscReal norm=20, Eprecision=5e-5, tol=1e-40;
223 PetscInt giter=0, ColS=12, col=0, Emaxiter=50000000, iter=0, iterations=15, Iiter=0;
229 PetscInt Istart,Iend;
236 PetscScalar norm_ksp;
237 Vec u,v,d,uu,vt,zero_long,zero_short,x_lsqr;
241 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
242 ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr);
243 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
250 ierr = PetscPrintf(PETSC_COMM_WORLD, "Size of vector %D\n", size); CHKERRQ(ierr);
253 MatGetOwnershipRange(A,&aa,&bb);
255 // ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%D %D\n", aa,bb); CHKERRQ(ierr);
256 //PetscSynchronizedFlush(PETSC_COMM_WORLD);
259 ierr = MatCreate(PETSC_COMM_WORLD, &S);CHKERRQ(ierr);
260 ierr = MatSetSizes(S, bb-aa, PETSC_DECIDE, size, ColS); CHKERRQ(ierr);
261 ierr = MatSetType(S, MATMPIDENSE); CHKERRQ(ierr);
262 ierr = MatSetUp(S); CHKERRQ(ierr);
264 ierr = MatGetOwnershipRange(S, &Istart, &Iend); CHKERRQ(ierr);
268 ierr = VecDuplicate(b, &Ax); CHKERRQ(ierr);
270 ierr = VecDuplicate(b,&x_old);CHKERRQ(ierr);
271 ierr = VecDuplicate(b,&residu);CHKERRQ(ierr);
275 ierr = VecDuplicate(b,&u);CHKERRQ(ierr);
278 ierr = VecDuplicate(b,&uu);CHKERRQ(ierr);
279 ierr = VecDuplicate(b,&zero_long);CHKERRQ(ierr);
280 ierr = VecSet(zero_long,0);CHKERRQ(ierr);
283 ierr = VecCreate(PETSC_COMM_WORLD, &v); CHKERRQ(ierr);
284 ierr = VecSetSizes(v, PETSC_DECIDE, ColS); CHKERRQ(ierr);
285 ierr = VecSetFromOptions(v); CHKERRQ(ierr);
286 ierr = VecDuplicate(v,&zero_short);CHKERRQ(ierr);
287 ierr = VecSet(zero_short,0);CHKERRQ(ierr);
288 ierr = VecDuplicate(v,&d);CHKERRQ(ierr);
289 ierr = VecDuplicate(v,&vt);CHKERRQ(ierr);
290 ierr = VecDuplicate(v,&x_lsqr);CHKERRQ(ierr);
293 //indexes of row (these indexes are global)
294 ind_row = (PetscInt*)malloc(sizeof(PetscInt)*(Iend-Istart));
295 for(i=0; i<Iend-Istart; i++) ind_row[i] = i + Istart;
298 // ierr = KSPGMRESSetRestart(ksp, 16); CHKERRQ(ierr);
299 ierr = KSPSetTolerances(ksp, 1e-10, 1e-10, PETSC_DEFAULT, 16); CHKERRQ(ierr);
300 ierr = KSPSetInitialGuessNonzero(ksp, PETSC_TRUE); CHKERRQ(ierr);
305 //GMRES WITH MINIMIZATION
307 while(giter<Emaxiter && norm>Eprecision ){
308 for(col=0; col<ColS && norm>Eprecision; col++){
312 ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr);
314 ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr);
315 total += its; Iiter ++;
320 ierr = VecGetArray(x, &array);
321 ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES);
322 VecRestoreArray(x, &array);
327 ierr = VecCopy(x, residu); CHKERRQ(ierr);
328 ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr);
329 ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr);
333 ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr);
334 ierr = VecCopy(x, x_old); CHKERRQ(ierr);
341 if( norm>Eprecision) {
343 MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY);
344 MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY);
351 MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS);
356 MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS);
368 PetscScalar n2b,tolb,normr,c,s,phibar,normar,norma,thet,rhot,rho,phi;
371 VecNorm(b, NORM_2, &n2b); //n2b = norm(b);
372 ierr = VecCopy(b, u); CHKERRQ(ierr); //u=b
373 VecNorm(u, NORM_2, &beta); // beta=norm(u)
376 VecAYPX(u,1/beta,zero_long); // u = u / beta;
381 MatMultTranspose(AS, u, v); //v=A'*u
382 ierr = VecSet(x_lsqr,0);CHKERRQ(ierr);
383 VecNorm(v, NORM_2, &alpha); // alpha=norm(v)
385 VecAYPX(v,1/alpha,zero_short); // v = v / alpha;
387 ierr = VecSet(d,0);CHKERRQ(ierr);
388 normar = alpha * beta;
391 for(i=0;i<iterations;i++) {
392 MatMult(AS, v, uu); //uu=A*v
393 VecAYPX(u, -alpha, uu); //u = uu-alpha*u;
394 VecNorm(u, NORM_2, &beta); // beta=norm(u)
395 VecAYPX(u,1/beta,zero_long); // u = u / beta;
396 norma=sqrt(norma*norma+alpha*alpha+beta*beta); // norma = norm([norma alpha beta]);
399 rho = sqrt(rhot*rhot + beta*beta);
403 if (phi == 0) { // stagnation of the method
407 VecAYPX(d,-thet,v); //d = (v - thet * d);
408 VecAYPX(d,1/rho,zero_short); //d=d/ rho;
411 VecAXPY(x_lsqr,phi,d); // x_lsqr=x_lsqr+phi*d
412 normr = abs(s) * normr;
413 MatMultTranspose(AS, u, vt); //vt=A'*u;
414 VecAYPX(v,-beta,vt); // v = vt - beta * v;
415 VecNorm(v, NORM_2, &alpha); // alpha=norm(v)
416 VecAYPX(v,1/alpha,zero_short); // v = v / alpha;
417 normar = alpha * abs( s * phi);
424 MatMult(S, x_lsqr, x); //x = S*x_small;
429 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time LSQR : %g (s)\n", T2-T1); CHKERRQ(ierr);
430 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations LSQR : %D\n", total); CHKERRQ(ierr);
442 int main(int argc,char **argv)
453 PetscInitialize(&argc,&argv,(char*)0,help);
455 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
456 ierr = DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,-7,-7,-7,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,0,0,0,&da);CHKERRQ(ierr);
457 ierr = KSPSetDM(ksp,da);CHKERRQ(ierr);
458 ierr = KSPSetComputeInitialGuess(ksp,ComputeInitialGuess,NULL);CHKERRQ(ierr);
459 ierr = KSPSetComputeRHS(ksp,ComputeRHS,NULL);CHKERRQ(ierr);
460 ierr = KSPSetComputeOperators(ksp,ComputeMatrix,NULL);CHKERRQ(ierr);
461 ierr = DMDestroy(&da);CHKERRQ(ierr);
463 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
466 ierr = KSPSetTolerances(ksp, 1e-7, 1e-7, PETSC_DEFAULT, 50000000); CHKERRQ(ierr);
468 ierr = KSPSolve(ksp,NULL,NULL);CHKERRQ(ierr);
473 ierr = KSPGetSolution(ksp,&x);CHKERRQ(ierr);
474 ierr = KSPGetRhs(ksp,&b);CHKERRQ(ierr);
475 ierr = VecDuplicate(b,&r);CHKERRQ(ierr);
476 ierr = KSPGetOperators(ksp,&A,NULL);CHKERRQ(ierr);
478 ierr = MatMult(A,x,r);CHKERRQ(ierr);
479 ierr = VecAXPY(r,-1.0,b);CHKERRQ(ierr);
480 ierr = VecNorm(r,NORM_2,&norm);CHKERRQ(ierr);
481 ierr = PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g\n",(double)norm);CHKERRQ(ierr);
482 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time : %g (s)\n", T2-T1); CHKERRQ(ierr);
484 ierr = KSPGetIterationNumber(ksp, &total); CHKERRQ(ierr);
485 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations : %D\n", total); CHKERRQ(ierr);
489 // ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
497 VecDuplicate(b,&sol);
499 KrylovMinimize(A, b, x);
507 VecNorm(sol, NORM_2, &norm);
508 ierr = PetscPrintf(PETSC_COMM_WORLD, "Error2 %g\n",norm);
510 ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
520 VecDuplicate(b,&sol);
522 KrylovMinimizeLSQR(A, b, x);
530 VecNorm(sol, NORM_2, &norm);
531 ierr = PetscPrintf(PETSC_COMM_WORLD, "Error LSQR %g\n",norm);
534 ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
538 ierr = VecDestroy(&r);CHKERRQ(ierr);
540 ierr = PetscFinalize();
546 #define __FUNCT__ "ComputeRHS"
547 PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
550 PetscInt i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
552 PetscScalar Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
553 PetscScalar ***barray;
555 PetscFunctionBeginUser;
556 ierr = KSPGetDM(ksp,&dm);CHKERRQ(ierr);
557 ierr = DMDAGetInfo(dm,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
558 Hx = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
559 HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
560 ierr = DMDAGetCorners(dm,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
561 ierr = DMDAVecGetArray(dm,b,&barray);CHKERRQ(ierr);
563 for (k=zs; k<zs+zm; k++) {
564 for (j=ys; j<ys+ym; j++) {
565 for (i=xs; i<xs+xm; i++) {
566 if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
567 barray[k][j][i] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
569 barray[k][j][i] = Hx*Hy*Hz;
574 ierr = DMDAVecRestoreArray(dm,b,&barray);CHKERRQ(ierr);
575 PetscFunctionReturn(0);
579 #define __FUNCT__ "ComputeInitialGuess"
580 PetscErrorCode ComputeInitialGuess(KSP ksp,Vec b,void *ctx)
584 PetscFunctionBeginUser;
585 ierr = VecSet(b,0);CHKERRQ(ierr);
586 PetscFunctionReturn(0);
590 #define __FUNCT__ "ComputeMatrix"
591 PetscErrorCode ComputeMatrix(KSP ksp,Mat jac,Mat B,void *ctx)
595 PetscInt i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
596 PetscScalar v[7],Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
597 MatStencil row,col[7];
599 PetscFunctionBeginUser;
600 ierr = KSPGetDM(ksp,&da);CHKERRQ(ierr);
601 ierr = DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
602 Hx = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
603 HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
604 ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
606 for (k=zs; k<zs+zm; k++) {
607 for (j=ys; j<ys+ym; j++) {
608 for (i=xs; i<xs+xm; i++) {
609 row.i = i; row.j = j; row.k = k;
610 if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
611 v[0] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
612 ierr = MatSetValuesStencil(B,1,&row,1,&row,v,INSERT_VALUES);CHKERRQ(ierr);
614 v[0] = -HxHydHz;col[0].i = i; col[0].j = j; col[0].k = k-1;
615 v[1] = -HxHzdHy;col[1].i = i; col[1].j = j-1; col[1].k = k;
616 v[2] = -HyHzdHx;col[2].i = i-1; col[2].j = j; col[2].k = k;
617 v[3] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);col[3].i = row.i; col[3].j = row.j; col[3].k = row.k;
618 v[4] = -HyHzdHx;col[4].i = i+1; col[4].j = j; col[4].k = k;
619 v[5] = -HxHzdHy;col[5].i = i; col[5].j = j+1; col[5].k = k;
620 v[6] = -HxHydHz;col[6].i = i; col[6].j = j; col[6].k = k+1;
621 ierr = MatSetValuesStencil(B,1,&row,7,col,v,INSERT_VALUES);CHKERRQ(ierr);
626 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
627 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
628 PetscFunctionReturn(0);