From 877ef102aef5b45478a81fe60764c56a50cbf5f3 Mon Sep 17 00:00:00 2001 From: raphael couturier Date: Wed, 13 Aug 2014 16:34:14 +0200 Subject: [PATCH] ajout ex45 LSQR pas rapide... --- code/ex45.c | 637 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 637 insertions(+) create mode 100644 code/ex45.c diff --git a/code/ex45.c b/code/ex45.c new file mode 100644 index 0000000..a44a2d0 --- /dev/null +++ b/code/ex45.c @@ -0,0 +1,637 @@ + +// /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 + + +/* +Laplacian in 3D. Modeled by the partial differential equation + + - Laplacian u = 1,0 < x,y,z < 1, + +with boundary conditions + + u = 1 for x = 0, x = 1, y = 0, y = 1, z = 0, z = 1. + + This uses multigrid to solve the linear system + + See src/snes/examples/tutorials/ex50.c + + Can also be run with -pc_type exotic -ksp_type fgmres + +*/ + +static char help[] = "Solves 3D Laplacian using multigrid.\n\n"; + +#include +#include +#include + +extern PetscErrorCode ComputeMatrix(KSP,Mat,Mat,void*); +extern PetscErrorCode ComputeRHS(KSP,Vec,void*); +extern PetscErrorCode ComputeInitialGuess(KSP,Vec,void*); + +#undef __FUNCT__ +#define __FUNCT__ "main" + + + + + + + +int KrylovMinimize(Mat A, Vec b, Vec x) { + + + //Variables + + PetscScalar gamma, alpha, oldgamma, beta; + PetscReal norm=20, Eprecision=5e-5, cgprec=1e-40; + PetscInt giter=0, ColS=12, col=0, Emaxiter=50000000, iter=0, iterations=15, Iiter=0; + PetscErrorCode ierr; + PetscScalar T1, T2; + KSP ksp; + PetscInt total=0; + PetscInt size; + PetscInt Istart,Iend; + PetscInt i,its; + Vec x_old, residu; + Mat S, AS; + PetscScalar *array; + PetscInt *ind_row; + Vec Alpha, p, ss, vect, r, q, Ax; + + + PetscInt first=1; + + ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); + ierr = KSPSetOperators(ksp,A,A);CHKERRQ(ierr); + ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); + + + + + VecGetSize(b,&size); + + ierr = PetscPrintf(PETSC_COMM_WORLD, "Size of vector %D\n", size); CHKERRQ(ierr); + + PetscInt aa,bb; + MatGetOwnershipRange(A,&aa,&bb); + + // ierr = PetscSynchronizedPrintf(PETSC_COMM_WORLD, "%D %D\n", aa,bb); CHKERRQ(ierr); + //PetscSynchronizedFlush(PETSC_COMM_WORLD); + + + ierr = MatCreate(PETSC_COMM_WORLD, &S);CHKERRQ(ierr); + ierr = MatSetSizes(S, bb-aa, PETSC_DECIDE, size, ColS); CHKERRQ(ierr); + ierr = MatSetType(S, MATMPIDENSE); CHKERRQ(ierr); + ierr = MatSetUp(S); CHKERRQ(ierr); + + ierr = MatGetOwnershipRange(S, &Istart, &Iend); CHKERRQ(ierr); + + ierr = VecCreate(PETSC_COMM_WORLD, &Alpha); CHKERRQ(ierr); + ierr = VecSetSizes(Alpha, PETSC_DECIDE, ColS); CHKERRQ(ierr); + ierr = VecSetFromOptions(Alpha); CHKERRQ(ierr); + ierr = VecDuplicate(Alpha, &vect); CHKERRQ(ierr); + ierr = VecDuplicate(Alpha, &p); CHKERRQ(ierr); + ierr = VecDuplicate(Alpha, &ss); CHKERRQ(ierr); + ierr = VecDuplicate(b, &r); CHKERRQ(ierr); + ierr = VecDuplicate(b, &q); CHKERRQ(ierr); + ierr = VecDuplicate(b, &Ax); CHKERRQ(ierr); + + ierr = VecDuplicate(b,&x_old);CHKERRQ(ierr); + ierr = VecDuplicate(b,&residu);CHKERRQ(ierr); + + + + //indexes of row (these indexes are global) + ind_row = (PetscInt*)malloc(sizeof(PetscInt)*(Iend-Istart)); + for(i=0; iEprecision ){ + for(col=0; colEprecision; col++){ + + + //Solve + ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr); + + ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr); + total += its; Iiter ++; + + + + //Build S' + ierr = VecGetArray(x, &array); + ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES); + VecRestoreArray(x, &array); + + + + //Error + ierr = VecCopy(x, residu); CHKERRQ(ierr); + ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr); + ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr); + + + + ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr); + ierr = VecCopy(x, x_old); CHKERRQ(ierr); + + + } + + + //minimization step + if( norm>Eprecision) { + + MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY); + MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY); + + + //Build AS + if(first) { + MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS); + first=0; + } + else + MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS); + + + + + //Minimization with CGLS + MatMult(AS, Alpha, r); VecAYPX(r, -1, b); //r_0 = b-AS*x_0 + + + MatMultTranspose(AS, r, p); //p_0 = AS'*r_0 + + + + + ierr = VecCopy(p, ss); CHKERRQ(ierr); //p_0 = s_0 + VecNorm(ss, NORM_2, &gamma); gamma = gamma * gamma; //gamma = norm2(s)^2 + while(gamma>cgprec && iterEprecision ){ + for(col=0; colEprecision; col++){ + + + //Solve + ierr = KSPSolve(ksp, b, x); CHKERRQ(ierr); + + ierr = KSPGetIterationNumber(ksp, &its); CHKERRQ(ierr); + total += its; Iiter ++; + + + + //Build S' + ierr = VecGetArray(x, &array); + ierr = MatSetValues(S, Iend-Istart, ind_row, 1, &col, array, INSERT_VALUES); + VecRestoreArray(x, &array); + + + + //Error + ierr = VecCopy(x, residu); CHKERRQ(ierr); + ierr = VecAXPY(residu, -1, x_old); CHKERRQ(ierr); + ierr = VecNorm(residu, NORM_INFINITY, &norm); CHKERRQ(ierr); + + + + ierr = PetscPrintf(PETSC_COMM_WORLD, "Norm of error %g, outer iteration %D\n", norm, giter); CHKERRQ(ierr); + ierr = VecCopy(x, x_old); CHKERRQ(ierr); + + + } + + + //minimization step + if( norm>Eprecision) { + + MatAssemblyBegin(S, MAT_FINAL_ASSEMBLY); + MatAssemblyEnd(S, MAT_FINAL_ASSEMBLY); + + + + + //Build AS + if(first) { + MatMatMult(A,S, MAT_INITIAL_MATRIX,PETSC_DEFAULT,&AS); + + first=0; + } + else + MatMatMult(A,S, MAT_REUSE_MATRIX,PETSC_DEFAULT,&AS); + + + + + + //LSQR + //LSQR + //LSQR + + + + PetscScalar n2b,tolb,normr,c,s,phibar,normar,norma,thet,rhot,rho,phi; + PetscInt stag; + tolb = tol * n2b; + VecNorm(b, NORM_2, &n2b); //n2b = norm(b); + ierr = VecCopy(b, u); CHKERRQ(ierr); //u=b + VecNorm(u, NORM_2, &beta); // beta=norm(u) + normr=beta; + if (beta != 0) { + VecAYPX(u,1/beta,zero_long); // u = u / beta; + } + c=1; + s=0; + phibar=beta; + MatMultTranspose(AS, u, v); //v=A'*u + ierr = VecSet(x_lsqr,0);CHKERRQ(ierr); + VecNorm(v, NORM_2, &alpha); // alpha=norm(v) + if (alpha != 0) { + VecAYPX(v,1/alpha,zero_short); // v = v / alpha; + } + ierr = VecSet(d,0);CHKERRQ(ierr); + normar = alpha * beta; + norma=0; + //stag=0; + for(i=0;i