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 if (normar/(norma*normr) <= tol) { // check for convergence in min{|b-A*x|}
414 if (normr <= tolb) { // check for convergence in A*x=b
419 VecAXPY(x_lsqr,phi,d); // x_lsqr=x_lsqr+phi*d
420 normr = abs(s) * normr;
421 MatMultTranspose(AS, u, vt); //vt=A'*u;
422 VecAYPX(v,-beta,vt); // v = vt - beta * v;
423 VecNorm(v, NORM_2, &alpha); // alpha=norm(v)
424 VecAYPX(v,1/alpha,zero_short); // v = v / alpha;
425 normar = alpha * abs( s * phi);
432 MatMult(S, x_lsqr, x); //x = S*x_small;
437 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time LSQR : %g (s)\n", T2-T1); CHKERRQ(ierr);
438 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations LSQR : %D\n", total); CHKERRQ(ierr);
450 int main(int argc,char **argv)
461 PetscInitialize(&argc,&argv,(char*)0,help);
463 ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr);
464 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);
465 ierr = KSPSetDM(ksp,da);CHKERRQ(ierr);
466 ierr = KSPSetComputeInitialGuess(ksp,ComputeInitialGuess,NULL);CHKERRQ(ierr);
467 ierr = KSPSetComputeRHS(ksp,ComputeRHS,NULL);CHKERRQ(ierr);
468 ierr = KSPSetComputeOperators(ksp,ComputeMatrix,NULL);CHKERRQ(ierr);
469 ierr = DMDestroy(&da);CHKERRQ(ierr);
471 ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr);
474 ierr = KSPSetTolerances(ksp, 1e-7, 1e-7, PETSC_DEFAULT, 50000000); CHKERRQ(ierr);
476 ierr = KSPSolve(ksp,NULL,NULL);CHKERRQ(ierr);
481 ierr = KSPGetSolution(ksp,&x);CHKERRQ(ierr);
482 ierr = KSPGetRhs(ksp,&b);CHKERRQ(ierr);
483 ierr = VecDuplicate(b,&r);CHKERRQ(ierr);
484 ierr = KSPGetOperators(ksp,&A,NULL);CHKERRQ(ierr);
486 ierr = MatMult(A,x,r);CHKERRQ(ierr);
487 ierr = VecAXPY(r,-1.0,b);CHKERRQ(ierr);
488 ierr = VecNorm(r,NORM_2,&norm);CHKERRQ(ierr);
489 ierr = PetscPrintf(PETSC_COMM_WORLD,"Residual norm %g\n",(double)norm);CHKERRQ(ierr);
490 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Execution time : %g (s)\n", T2-T1); CHKERRQ(ierr);
492 ierr = KSPGetIterationNumber(ksp, &total); CHKERRQ(ierr);
493 ierr = PetscPrintf(PETSC_COMM_WORLD, "\t\t\t -- Total number of iterations : %D\n", total); CHKERRQ(ierr);
497 // ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
505 VecDuplicate(b,&sol);
507 KrylovMinimize(A, b, x);
515 VecNorm(sol, NORM_2, &norm);
516 ierr = PetscPrintf(PETSC_COMM_WORLD, "Error2 %g\n",norm);
518 ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
528 VecDuplicate(b,&sol);
530 KrylovMinimizeLSQR(A, b, x);
538 VecNorm(sol, NORM_2, &norm);
539 ierr = PetscPrintf(PETSC_COMM_WORLD, "Error LSQR %g\n",norm);
542 ierr = KSPDestroy(&ksp);CHKERRQ(ierr);
546 ierr = VecDestroy(&r);CHKERRQ(ierr);
548 ierr = PetscFinalize();
554 #define __FUNCT__ "ComputeRHS"
555 PetscErrorCode ComputeRHS(KSP ksp,Vec b,void *ctx)
558 PetscInt i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
560 PetscScalar Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
561 PetscScalar ***barray;
563 PetscFunctionBeginUser;
564 ierr = KSPGetDM(ksp,&dm);CHKERRQ(ierr);
565 ierr = DMDAGetInfo(dm,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
566 Hx = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
567 HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
568 ierr = DMDAGetCorners(dm,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
569 ierr = DMDAVecGetArray(dm,b,&barray);CHKERRQ(ierr);
571 for (k=zs; k<zs+zm; k++) {
572 for (j=ys; j<ys+ym; j++) {
573 for (i=xs; i<xs+xm; i++) {
574 if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
575 barray[k][j][i] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
577 barray[k][j][i] = Hx*Hy*Hz;
582 ierr = DMDAVecRestoreArray(dm,b,&barray);CHKERRQ(ierr);
583 PetscFunctionReturn(0);
587 #define __FUNCT__ "ComputeInitialGuess"
588 PetscErrorCode ComputeInitialGuess(KSP ksp,Vec b,void *ctx)
592 PetscFunctionBeginUser;
593 ierr = VecSet(b,0);CHKERRQ(ierr);
594 PetscFunctionReturn(0);
598 #define __FUNCT__ "ComputeMatrix"
599 PetscErrorCode ComputeMatrix(KSP ksp,Mat jac,Mat B,void *ctx)
603 PetscInt i,j,k,mx,my,mz,xm,ym,zm,xs,ys,zs;
604 PetscScalar v[7],Hx,Hy,Hz,HxHydHz,HyHzdHx,HxHzdHy;
605 MatStencil row,col[7];
607 PetscFunctionBeginUser;
608 ierr = KSPGetDM(ksp,&da);CHKERRQ(ierr);
609 ierr = DMDAGetInfo(da,0,&mx,&my,&mz,0,0,0,0,0,0,0,0,0);CHKERRQ(ierr);
610 Hx = 1.0 / (PetscReal)(mx-1); Hy = 1.0 / (PetscReal)(my-1); Hz = 1.0 / (PetscReal)(mz-1);
611 HxHydHz = Hx*Hy/Hz; HxHzdHy = Hx*Hz/Hy; HyHzdHx = Hy*Hz/Hx;
612 ierr = DMDAGetCorners(da,&xs,&ys,&zs,&xm,&ym,&zm);CHKERRQ(ierr);
614 for (k=zs; k<zs+zm; k++) {
615 for (j=ys; j<ys+ym; j++) {
616 for (i=xs; i<xs+xm; i++) {
617 row.i = i; row.j = j; row.k = k;
618 if (i==0 || j==0 || k==0 || i==mx-1 || j==my-1 || k==mz-1) {
619 v[0] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);
620 ierr = MatSetValuesStencil(B,1,&row,1,&row,v,INSERT_VALUES);CHKERRQ(ierr);
622 v[0] = -HxHydHz;col[0].i = i; col[0].j = j; col[0].k = k-1;
623 v[1] = -HxHzdHy;col[1].i = i; col[1].j = j-1; col[1].k = k;
624 v[2] = -HyHzdHx;col[2].i = i-1; col[2].j = j; col[2].k = k;
625 v[3] = 2.0*(HxHydHz + HxHzdHy + HyHzdHx);col[3].i = row.i; col[3].j = row.j; col[3].k = row.k;
626 v[4] = -HyHzdHx;col[4].i = i+1; col[4].j = j; col[4].k = k;
627 v[5] = -HxHzdHy;col[5].i = i; col[5].j = j+1; col[5].k = k;
628 v[6] = -HxHydHz;col[6].i = i; col[6].j = j; col[6].k = k+1;
629 ierr = MatSetValuesStencil(B,1,&row,7,col,v,INSERT_VALUES);CHKERRQ(ierr);
634 ierr = MatAssemblyBegin(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
635 ierr = MatAssemblyEnd(B,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
636 PetscFunctionReturn(0);