1a312c225SMatthew G Knepley /* Defines the basic SNES object */ 2a312c225SMatthew G Knepley #include <private/snesimpl.h> 3a312c225SMatthew G Knepley 4a312c225SMatthew G Knepley /* Private structure for the Anderson mixing method aka nonlinear Krylov */ 5a312c225SMatthew G Knepley typedef struct { 6a312c225SMatthew G Knepley Vec *v, *w; 7a312c225SMatthew G Knepley PetscReal *f2; /* 2-norms of function (residual) at each stage */ 8a312c225SMatthew G Knepley PetscInt msize; /* maximum size of space */ 9a312c225SMatthew G Knepley PetscInt csize; /* current size of space */ 10a312c225SMatthew G Knepley } SNES_NGMRES; 11a312c225SMatthew G Knepley 12a312c225SMatthew G Knepley #undef __FUNCT__ 13a312c225SMatthew G Knepley #define __FUNCT__ "SNESReset_NGMRES" 14a312c225SMatthew G Knepley PetscErrorCode SNESReset_NGMRES(SNES snes) 15a312c225SMatthew G Knepley { 16a312c225SMatthew G Knepley SNES_NGMRES *ngmres = (SNES_NGMRES*) snes->data; 17a312c225SMatthew G Knepley PetscErrorCode ierr; 18a312c225SMatthew G Knepley 19a312c225SMatthew G Knepley PetscFunctionBegin; 20a312c225SMatthew G Knepley ierr = VecDestroyVecs(ngmres->msize, &ngmres->v);CHKERRQ(ierr); 21a312c225SMatthew G Knepley ierr = VecDestroyVecs(ngmres->msize, &ngmres->w);CHKERRQ(ierr); 22a312c225SMatthew G Knepley PetscFunctionReturn(0); 23a312c225SMatthew G Knepley } 24a312c225SMatthew G Knepley 25a312c225SMatthew G Knepley #undef __FUNCT__ 26a312c225SMatthew G Knepley #define __FUNCT__ "SNESDestroy_NGMRES" 27a312c225SMatthew G Knepley PetscErrorCode SNESDestroy_NGMRES(SNES snes) 28a312c225SMatthew G Knepley { 29a312c225SMatthew G Knepley PetscErrorCode ierr; 30a312c225SMatthew G Knepley 31a312c225SMatthew G Knepley PetscFunctionBegin; 32a312c225SMatthew G Knepley ierr = SNESReset_NGMRES(snes);CHKERRQ(ierr); 33a312c225SMatthew G Knepley if (snes->work) {ierr = VecDestroyVecs(snes->nwork, &snes->work);CHKERRQ(ierr);} 34a312c225SMatthew G Knepley PetscFunctionReturn(0); 35a312c225SMatthew G Knepley } 36a312c225SMatthew G Knepley 37a312c225SMatthew G Knepley #undef __FUNCT__ 38a312c225SMatthew G Knepley #define __FUNCT__ "SNESSetUp_NGMRES" 39a312c225SMatthew G Knepley PetscErrorCode SNESSetUp_NGMRES(SNES snes) 40a312c225SMatthew G Knepley { 41a312c225SMatthew G Knepley SNES_NGMRES *ngmres = (SNES_NGMRES *) snes->data; 42a312c225SMatthew G Knepley PetscErrorCode ierr; 43a312c225SMatthew G Knepley 44a312c225SMatthew G Knepley PetscFunctionBegin; 45a312c225SMatthew G Knepley #if 0 46a312c225SMatthew G Knepley if (snes->pc_side != PC_LEFT) {SETERRQ(((PetscObject) snes)->comm, PETSC_ERR_SUP, "Only left preconditioning allowed for SNESNGMRES");} 47a312c225SMatthew G Knepley #endif 48a312c225SMatthew G Knepley ierr = VecDuplicateVecs(snes->vec_sol, ngmres->msize, &ngmres->v);CHKERRQ(ierr); 49a312c225SMatthew G Knepley ierr = VecDuplicateVecs(snes->vec_sol, ngmres->msize, &ngmres->w);CHKERRQ(ierr); 50a312c225SMatthew G Knepley ierr = SNESDefaultGetWork(snes, 2);CHKERRQ(ierr); 51a312c225SMatthew G Knepley PetscFunctionReturn(0); 52a312c225SMatthew G Knepley } 53a312c225SMatthew G Knepley 54a312c225SMatthew G Knepley #undef __FUNCT__ 55a312c225SMatthew G Knepley #define __FUNCT__ "SNESSetFromOptions_NGMRES" 56a312c225SMatthew G Knepley PetscErrorCode SNESSetFromOptions_NGMRES(SNES snes) 57a312c225SMatthew G Knepley { 58a312c225SMatthew G Knepley SNES_NGMRES *ngmres = (SNES_NGMRES *) snes->data; 59a312c225SMatthew G Knepley PetscErrorCode ierr; 60a312c225SMatthew G Knepley 61a312c225SMatthew G Knepley PetscFunctionBegin; 62a312c225SMatthew G Knepley ierr = PetscOptionsHead("SNES NGMRES options");CHKERRQ(ierr); 63a312c225SMatthew G Knepley ierr = PetscOptionsInt("-snes_gmres_restart", "Number of directions", "SNES", ngmres->msize, &ngmres->msize, PETSC_NULL);CHKERRQ(ierr); 64a312c225SMatthew G Knepley ierr = PetscOptionsTail();CHKERRQ(ierr); 65a312c225SMatthew G Knepley PetscFunctionReturn(0); 66a312c225SMatthew G Knepley } 67a312c225SMatthew G Knepley 68a312c225SMatthew G Knepley #undef __FUNCT__ 69a312c225SMatthew G Knepley #define __FUNCT__ "SNESView_NGMRES" 70a312c225SMatthew G Knepley PetscErrorCode SNESView_NGMRES(SNES snes, PetscViewer viewer) 71a312c225SMatthew G Knepley { 72a312c225SMatthew G Knepley SNES_NGMRES *ngmres = (SNES_NGMRES *) snes->data; 73a312c225SMatthew G Knepley PetscBool iascii; 74a312c225SMatthew G Knepley PetscErrorCode ierr; 75a312c225SMatthew G Knepley 76a312c225SMatthew G Knepley PetscFunctionBegin; 77a312c225SMatthew G Knepley ierr = PetscTypeCompare((PetscObject) viewer, PETSCVIEWERASCII, &iascii);CHKERRQ(ierr); 78a312c225SMatthew G Knepley if (iascii) { 79a312c225SMatthew G Knepley ierr = PetscViewerASCIIPrintf(viewer, " Size of space %d\n", ngmres->msize);CHKERRQ(ierr); 80a312c225SMatthew G Knepley } else { 81a312c225SMatthew G Knepley SETERRQ1(((PetscObject) snes)->comm, PETSC_ERR_SUP, "Viewer type %s not supported for SNESNGMRES", ((PetscObject) viewer)->type_name); 82a312c225SMatthew G Knepley } 83a312c225SMatthew G Knepley PetscFunctionReturn(0); 84a312c225SMatthew G Knepley } 85a312c225SMatthew G Knepley 86a312c225SMatthew G Knepley #undef __FUNCT__ 87a312c225SMatthew G Knepley #define __FUNCT__ "SNESSolve_NGMRES" 88a312c225SMatthew G Knepley PetscErrorCode SNESSolve_NGMRES(SNES snes) 89a312c225SMatthew G Knepley { 90a312c225SMatthew G Knepley SNES_NGMRES *ngmres = (SNES_NGMRES *) snes->data; 91a312c225SMatthew G Knepley Vec X, Y, F, Pold, P, *V = ngmres->v, *W = ngmres->w; 92a312c225SMatthew G Knepley //Vec y, w; 93a312c225SMatthew G Knepley PetscScalar wdot; 94a312c225SMatthew G Knepley PetscReal fnorm; 95a312c225SMatthew G Knepley //PetscScalar rdot, abr, A0; 96a312c225SMatthew G Knepley PetscInt i, j, k; 97a312c225SMatthew G Knepley PetscErrorCode ierr; 98a312c225SMatthew G Knepley 99a312c225SMatthew G Knepley PetscFunctionBegin; 100a312c225SMatthew G Knepley snes->reason = SNES_CONVERGED_ITERATING; 101a312c225SMatthew G Knepley X = snes->vec_sol; 102a312c225SMatthew G Knepley Y = snes->vec_sol_update; 103a312c225SMatthew G Knepley F = snes->vec_func; 104a312c225SMatthew G Knepley Pold = snes->work[0]; 105a312c225SMatthew G Knepley P = snes->work[1]; 106a312c225SMatthew G Knepley 107a312c225SMatthew G Knepley ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 108a312c225SMatthew G Knepley snes->iter = 0; 109a312c225SMatthew G Knepley snes->norm = 0.; 110a312c225SMatthew G Knepley ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 111a312c225SMatthew G Knepley ierr = SNESComputeFunction(snes, X, F);CHKERRQ(ierr); /* r = F(x) */ 112a312c225SMatthew G Knepley #if 0 113a312c225SMatthew G Knepley ierr = SNESSolve(snes->pc, F, Pold);CHKERRQ(ierr); /* p = P(r) */ 114a312c225SMatthew G Knepley #else 115a312c225SMatthew G Knepley ierr = VecCopy(F, Pold);CHKERRQ(ierr); /* p = r */ 116a312c225SMatthew G Knepley #endif 117a312c225SMatthew G Knepley if (snes->domainerror) { 118a312c225SMatthew G Knepley snes->reason = SNES_DIVERGED_FUNCTION_DOMAIN; 119a312c225SMatthew G Knepley PetscFunctionReturn(0); 120a312c225SMatthew G Knepley } 121a312c225SMatthew G Knepley ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm = ||r|| */ 122a312c225SMatthew G Knepley if (PetscIsInfOrNanReal(fnorm)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_FP, "Infinite or not-a-number generated in norm"); 123a312c225SMatthew G Knepley ierr = PetscObjectTakeAccess(snes);CHKERRQ(ierr); 124a312c225SMatthew G Knepley snes->norm = fnorm; 125a312c225SMatthew G Knepley ierr = PetscObjectGrantAccess(snes);CHKERRQ(ierr); 126a312c225SMatthew G Knepley SNESLogConvHistory(snes, fnorm, 0); 127a312c225SMatthew G Knepley ierr = SNESMonitor(snes, 0, fnorm);CHKERRQ(ierr); 128a312c225SMatthew G Knepley 129a312c225SMatthew G Knepley /* set parameter for default relative tolerance convergence test */ 130a312c225SMatthew G Knepley snes->ttol = fnorm*snes->rtol; 131a312c225SMatthew G Knepley /* test convergence */ 132a312c225SMatthew G Knepley ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 133a312c225SMatthew G Knepley if (snes->reason) PetscFunctionReturn(0); 134a312c225SMatthew G Knepley 135*31823bd8SMatthew G Knepley #if 0 /* Barry: What the heck is this part doing? */ 136a312c225SMatthew G Knepley /* determine optimal scale factor -- slow code */ 137a312c225SMatthew G Knepley ierr = VecDuplicate(P, &y);CHKERRQ(ierr); 138a312c225SMatthew G Knepley ierr = VecDuplicate(P, &w);CHKERRQ(ierr); 139a312c225SMatthew G Knepley ierr = MatMult(Amat, Pold, y);CHKERRQ(ierr); 140a312c225SMatthew G Knepley /*ierr = KSP_PCApplyBAorAB(ksp,Pold,y,w);CHKERRQ(ierr); */ /* y = BAp */ 141a312c225SMatthew G Knepley ierr = VecDotNorm2(Pold, y, &rdot, &abr);CHKERRQ(ierr); /* rdot = (p)^T(BAp); abr = (BAp)^T (BAp) */ 142a312c225SMatthew G Knepley ierr = VecDestroy(&y);CHKERRQ(ierr); 143a312c225SMatthew G Knepley ierr = VecDestroy(&w);CHKERRQ(ierr); 144a312c225SMatthew G Knepley A0 = rdot/abr; 145a312c225SMatthew G Knepley ierr = VecAXPY(X,A0,Pold);CHKERRQ(ierr); /* x <- x + scale p */ 146a312c225SMatthew G Knepley #endif 147a312c225SMatthew G Knepley 148a312c225SMatthew G Knepley /* Loop over batches of directions */ 149*31823bd8SMatthew G Knepley /* Code from Barry I do not understand to solve the least-squares problem, Time to try again */ 150a312c225SMatthew G Knepley for(k = 0; k < snes->max_its; k += ngmres->msize) { 151a312c225SMatthew G Knepley /* Loop over updates for this batch */ 152a312c225SMatthew G Knepley /* TODO: Incorporate the variant which use the analytic Jacobian */ 153a312c225SMatthew G Knepley /* TODO: Incorporate criteria for restarting from paper */ 154a312c225SMatthew G Knepley for(i = 0; i < ngmres->msize && k+i < snes->max_its; ++i) { 155a312c225SMatthew G Knepley ierr = SNESComputeFunction(snes, X, F);CHKERRQ(ierr); /* r = F(x) */ 156a312c225SMatthew G Knepley #if 0 157a312c225SMatthew G Knepley ierr = SNESSolve(snes->pc, F, P);CHKERRQ(ierr); /* p = P(r) */ 158a312c225SMatthew G Knepley #else 159a312c225SMatthew G Knepley ierr = VecCopy(F, P);CHKERRQ(ierr); /* p = r */ 160a312c225SMatthew G Knepley #endif 161a312c225SMatthew G Knepley ierr = VecNorm(F, NORM_2, &fnorm);CHKERRQ(ierr); /* fnorm = ||r|| */ 162a312c225SMatthew G Knepley SNESLogConvHistory(snes, fnorm, 0); 163a312c225SMatthew G Knepley ierr = SNESMonitor(snes, 0, fnorm);CHKERRQ(ierr); 164a312c225SMatthew G Knepley ierr = (*snes->ops->converged)(snes,0,0.0,0.0,fnorm,&snes->reason,snes->cnvP);CHKERRQ(ierr); 165a312c225SMatthew G Knepley if (snes->reason) PetscFunctionReturn(0); 166a312c225SMatthew G Knepley 167a312c225SMatthew G Knepley for(j = 0; j < i; ++j) { /* p = \prod_i (I + v_i w^T_i) p */ 168a312c225SMatthew G Knepley ierr = VecDot(W[j], P, &wdot);CHKERRQ(ierr); 169a312c225SMatthew G Knepley ierr = VecAXPY(P, wdot, V[j]);CHKERRQ(ierr); 170a312c225SMatthew G Knepley } 171a312c225SMatthew G Knepley ierr = VecCopy(Pold, W[i]);CHKERRQ(ierr); /* w_i = p_{old} */ 172a312c225SMatthew G Knepley 173a312c225SMatthew G Knepley ierr = VecAXPY(Pold, -1.0, P);CHKERRQ(ierr); /* v_i = P */ 174a312c225SMatthew G Knepley ierr = VecDot(W[i], Pold, &wdot);CHKERRQ(ierr); /* ------------------- */ 175a312c225SMatthew G Knepley ierr = VecCopy(P, V[i]);CHKERRQ(ierr); /* w^T_i (p_{old} - p) */ 176a312c225SMatthew G Knepley ierr = VecScale(V[i], 1.0/wdot);CHKERRQ(ierr); 177a312c225SMatthew G Knepley 178a312c225SMatthew G Knepley ierr = VecDot(W[i], P, &wdot);CHKERRQ(ierr); /* p = (I + v_i w^T_i) p */ 179a312c225SMatthew G Knepley ierr = VecAXPY(P, wdot, V[i]);CHKERRQ(ierr); 180a312c225SMatthew G Knepley ierr = VecCopy(P, Pold);CHKERRQ(ierr); /* p_{old} = p */ 181a312c225SMatthew G Knepley 182a312c225SMatthew G Knepley ierr = VecAXPY(X, 1.0, P);CHKERRQ(ierr); /* x = x + p */ 183a312c225SMatthew G Knepley } 184a312c225SMatthew G Knepley } 185a312c225SMatthew G Knepley snes->reason = SNES_DIVERGED_MAX_IT; 186a312c225SMatthew G Knepley PetscFunctionReturn(0); 187a312c225SMatthew G Knepley } 188a312c225SMatthew G Knepley 189a312c225SMatthew G Knepley /*MC 190a312c225SMatthew G Knepley SNESNGMRES - The Nonlinear Generalized Minimum Residual (NGMRES) method of Oosterlee and Washio. 191a312c225SMatthew G Knepley 192a312c225SMatthew G Knepley Level: beginner 193a312c225SMatthew G Knepley 194a312c225SMatthew G Knepley Notes: Supports only left preconditioning 195a312c225SMatthew G Knepley 196a312c225SMatthew G Knepley "Krylov Subspace Acceleration of Nonlinear Multigrid with Application to Recirculating Flows", C. W. Oosterlee and T. Washio, 197a312c225SMatthew G Knepley SIAM Journal on Scientific Computing, 21(5), 2000. 198a312c225SMatthew G Knepley 199a312c225SMatthew G Knepley .seealso: SNESCreate(), SNES, SNESSetType(), SNESType (for list of available types) 200a312c225SMatthew G Knepley M*/ 201a312c225SMatthew G Knepley EXTERN_C_BEGIN 202a312c225SMatthew G Knepley #undef __FUNCT__ 203a312c225SMatthew G Knepley #define __FUNCT__ "SNESCreate_NGMRES" 204a312c225SMatthew G Knepley PetscErrorCode SNESCreate_NGMRES(SNES snes) 205a312c225SMatthew G Knepley { 206a312c225SMatthew G Knepley SNES_NGMRES *ngmres; 207a312c225SMatthew G Knepley PetscErrorCode ierr; 208a312c225SMatthew G Knepley 209a312c225SMatthew G Knepley PetscFunctionBegin; 210a312c225SMatthew G Knepley snes->ops->destroy = SNESDestroy_NGMRES; 211a312c225SMatthew G Knepley snes->ops->setup = SNESSetUp_NGMRES; 212a312c225SMatthew G Knepley snes->ops->setfromoptions = SNESSetFromOptions_NGMRES; 213a312c225SMatthew G Knepley snes->ops->view = SNESView_NGMRES; 214a312c225SMatthew G Knepley snes->ops->solve = SNESSolve_NGMRES; 215a312c225SMatthew G Knepley snes->ops->reset = SNESReset_NGMRES; 216a312c225SMatthew G Knepley 217a312c225SMatthew G Knepley ierr = PetscNewLog(snes, SNES_NGMRES, &ngmres);CHKERRQ(ierr); 218a312c225SMatthew G Knepley snes->data = (void*) ngmres; 219a312c225SMatthew G Knepley ngmres->msize = 30; 220a312c225SMatthew G Knepley ngmres->csize = 0; 221a312c225SMatthew G Knepley #if 0 222a312c225SMatthew G Knepley if (ksp->pc_side != PC_LEFT) {ierr = PetscInfo(ksp,"WARNING! Setting PC_SIDE for NGMRES to left!\n");CHKERRQ(ierr);} 223a312c225SMatthew G Knepley snes->pc_side = PC_LEFT; 224a312c225SMatthew G Knepley #endif 225a312c225SMatthew G Knepley PetscFunctionReturn(0); 226a312c225SMatthew G Knepley } 227a312c225SMatthew G Knepley EXTERN_C_END 228