Actual source code: qcg.c
1: #define PETSCKSP_DLL
3: #include include/private/kspimpl.h
4: #include src/ksp/ksp/impls/qcg/qcg.h
6: static PetscErrorCode QuadraticRoots_Private(Vec,Vec,PetscReal*,PetscReal*,PetscReal*);
10: /*@
11: KSPQCGSetTrustRegionRadius - Sets the radius of the trust region.
13: Collective on KSP
15: Input Parameters:
16: + ksp - the iterative context
17: - delta - the trust region radius (Infinity is the default)
19: Options Database Key:
20: . -ksp_qcg_trustregionradius <delta>
22: Level: advanced
24: .keywords: KSP, QCG, set, trust region radius
25: @*/
26: PetscErrorCode KSPQCGSetTrustRegionRadius(KSP ksp,PetscReal delta)
27: {
28: PetscErrorCode ierr,(*f)(KSP,PetscReal);
32: if (delta < 0.0) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Tolerance must be non-negative");
33: PetscObjectQueryFunction((PetscObject)ksp,"KSPQCGSetTrustRegionRadius_C",(void (**)(void))&f);
34: if (f) {
35: (*f)(ksp,delta);
36: }
38: return(0);
39: }
43: /*@
44: KSPQCGGetTrialStepNorm - Gets the norm of a trial step vector. The WCG step may be
45: constrained, so this is not necessarily the length of the ultimate step taken in QCG.
47: Collective on KSP
49: Input Parameter:
50: . ksp - the iterative context
52: Output Parameter:
53: . tsnorm - the norm
55: Level: advanced
56: @*/
57: PetscErrorCode KSPQCGGetTrialStepNorm(KSP ksp,PetscReal *tsnorm)
58: {
59: PetscErrorCode ierr,(*f)(KSP,PetscReal*);
63: PetscObjectQueryFunction((PetscObject)ksp,"KSPQCGGetTrialStepNorm_C",(void (**)(void))&f);
64: if (f) {
65: (*f)(ksp,tsnorm);
66: }
67: return(0);
68: }
72: /*@
73: KSPQCGGetQuadratic - Gets the value of the quadratic function, evaluated at the new iterate:
75: q(s) = g^T * s + 0.5 * s^T * H * s
77: which satisfies the Euclidian Norm trust region constraint
79: || D * s || <= delta,
81: where
83: delta is the trust region radius,
84: g is the gradient vector, and
85: H is Hessian matrix,
86: D is a scaling matrix.
88: Collective on KSP
90: Input Parameter:
91: . ksp - the iterative context
93: Output Parameter:
94: . quadratic - the quadratic function evaluated at the new iterate
96: Level: advanced
97: @*/
98: PetscErrorCode KSPQCGGetQuadratic(KSP ksp,PetscReal *quadratic)
99: {
100: PetscErrorCode ierr,(*f)(KSP,PetscReal*);
104: PetscObjectQueryFunction((PetscObject)ksp,"KSPQCGGetQuadratic_C",(void (**)(void))&f);
105: if (f) {
106: (*f)(ksp,quadratic);
107: }
108: return(0);
109: }
113: /*
114: KSPSolve_QCG - Use preconditioned conjugate gradient to compute
115: an approximate minimizer of the quadratic function
117: q(s) = g^T * s + .5 * s^T * H * s
119: subject to the Euclidean norm trust region constraint
121: || D * s || <= delta,
123: where
125: delta is the trust region radius,
126: g is the gradient vector, and
127: H is Hessian matrix,
128: D is a scaling matrix.
130: KSPConvergedReason may be
131: $ KSP_CONVERGED_CG_NEG_CURVE if convergence is reached along a negative curvature direction,
132: $ KSP_CONVERGED_CG_CONSTRAINED if convergence is reached along a constrained step,
133: $ other KSP converged/diverged reasons
136: Notes:
137: Currently we allow symmetric preconditioning with the following scaling matrices:
138: PCNONE: D = Identity matrix
139: PCJACOBI: D = diag [d_1, d_2, ...., d_n], where d_i = sqrt(H[i,i])
140: PCICC: D = L^T, implemented with forward and backward solves.
141: Here L is an incomplete Cholesky factor of H.
143: We should perhaps rewrite using PCApplyBAorAB().
144: */
145: PetscErrorCode KSPSolve_QCG(KSP ksp)
146: {
147: /*
148: Correpondence with documentation above:
149: B = g = gradient,
150: X = s = step
151: Note: This is not coded correctly for complex arithmetic!
152: */
154: KSP_QCG *pcgP = (KSP_QCG*)ksp->data;
155: MatStructure pflag;
156: Mat Amat,Pmat;
157: Vec W,WA,WA2,R,P,ASP,BS,X,B;
158: PetscScalar scal,btx,xtax,beta,rntrn,step;
159: PetscReal ptasp,q1,q2,wtasp,bstp,rtr,xnorm,step1,step2,rnrm,p5 = 0.5;
160: PetscReal dzero = 0.0,bsnrm;
162: PetscInt i,maxit;
163: PC pc = ksp->pc;
164: PCSide side;
165: #if defined(PETSC_USE_COMPLEX)
166: PetscScalar cstep1,cstep2,cbstp,crtr,cwtasp,cptasp;
167: #endif
168: PetscTruth diagonalscale;
171: PCDiagonalScale(ksp->pc,&diagonalscale);
172: if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",ksp->type_name);
173: if (ksp->transpose_solve) {
174: SETERRQ(PETSC_ERR_SUP,"Currently does not support transpose solve");
175: }
177: ksp->its = 0;
178: maxit = ksp->max_it;
179: WA = ksp->work[0];
180: R = ksp->work[1];
181: P = ksp->work[2];
182: ASP = ksp->work[3];
183: BS = ksp->work[4];
184: W = ksp->work[5];
185: WA2 = ksp->work[6];
186: X = ksp->vec_sol;
187: B = ksp->vec_rhs;
189: if (pcgP->delta <= dzero) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Input error: delta <= 0");
190: KSPGetPreconditionerSide(ksp,&side);
191: if (side != PC_SYMMETRIC) SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,"Requires symmetric preconditioner!");
193: /* Initialize variables */
194: VecSet(W,0.0); /* W = 0 */
195: VecSet(X,0.0); /* X = 0 */
196: PCGetOperators(pc,&Amat,&Pmat,&pflag);
198: /* Compute: BS = D^{-1} B */
199: PCApplySymmetricLeft(pc,B,BS);
201: VecNorm(BS,NORM_2,&bsnrm);
202: PetscObjectTakeAccess(ksp);
203: ksp->its = 0;
204: ksp->rnorm = bsnrm;
205: PetscObjectGrantAccess(ksp);
206: KSPLogResidualHistory(ksp,bsnrm);
207: KSPMonitor(ksp,0,bsnrm);
208: (*ksp->converged)(ksp,0,bsnrm,&ksp->reason,ksp->cnvP);
209: if (ksp->reason) return(0);
211: /* Compute the initial scaled direction and scaled residual */
212: VecCopy(BS,R);
213: VecScale(R,-1.0);
214: VecCopy(R,P);
215: #if defined(PETSC_USE_COMPLEX)
216: VecDot(R,R,&crtr); rtr = PetscRealPart(crtr);
217: #else
218: VecDot(R,R,&rtr);
219: #endif
221: for (i=0; i<=maxit; i++) {
222: PetscObjectTakeAccess(ksp);
223: ksp->its++;
224: PetscObjectGrantAccess(ksp);
226: /* Compute: asp = D^{-T}*A*D^{-1}*p */
227: PCApplySymmetricRight(pc,P,WA);
228: MatMult(Amat,WA,WA2);
229: PCApplySymmetricLeft(pc,WA2,ASP);
231: /* Check for negative curvature */
232: #if defined(PETSC_USE_COMPLEX)
233: VecDot(P,ASP,&cptasp);
234: ptasp = PetscRealPart(cptasp);
235: #else
236: VecDot(P,ASP,&ptasp); /* ptasp = p^T asp */
237: #endif
238: if (ptasp <= dzero) {
240: /* Scaled negative curvature direction: Compute a step so that
241: ||w + step*p|| = delta and QS(w + step*p) is least */
243: if (!i) {
244: VecCopy(P,X);
245: VecNorm(X,NORM_2,&xnorm);
246: scal = pcgP->delta / xnorm;
247: VecScale(X,scal);
248: } else {
249: /* Compute roots of quadratic */
250: QuadraticRoots_Private(W,P,&pcgP->delta,&step1,&step2);
251: #if defined(PETSC_USE_COMPLEX)
252: VecDot(W,ASP,&cwtasp); wtasp = PetscRealPart(cwtasp);
253: VecDot(BS,P,&cbstp); bstp = PetscRealPart(cbstp);
254: #else
255: VecDot(W,ASP,&wtasp);
256: VecDot(BS,P,&bstp);
257: #endif
258: VecCopy(W,X);
259: q1 = step1*(bstp + wtasp + p5*step1*ptasp);
260: q2 = step2*(bstp + wtasp + p5*step2*ptasp);
261: #if defined(PETSC_USE_COMPLEX)
262: if (q1 <= q2) {
263: cstep1 = step1; VecAXPY(X,cstep1,P);
264: } else {
265: cstep2 = step2; VecAXPY(X,cstep2,P);
266: }
267: #else
268: if (q1 <= q2) {VecAXPY(X,step1,P);}
269: else {VecAXPY(X,step2,P);}
270: #endif
271: }
272: pcgP->ltsnrm = pcgP->delta; /* convergence in direction of */
273: ksp->reason = KSP_CONVERGED_CG_NEG_CURVE; /* negative curvature */
274: if (!i) {
275: PetscInfo1(ksp,"negative curvature: delta=%G\n",pcgP->delta);
276: } else {
277: PetscInfo3(ksp,"negative curvature: step1=%G, step2=%G, delta=%G\n",step1,step2,pcgP->delta);
278: }
279:
280: } else {
281:
282: /* Compute step along p */
284: step = rtr/ptasp;
285: VecCopy(W,X); /* x = w */
286: VecAXPY(X,step,P); /* x <- step*p + x */
287: VecNorm(X,NORM_2,&pcgP->ltsnrm);
289: if (pcgP->ltsnrm > pcgP->delta) {
291: /* Since the trial iterate is outside the trust region,
292: evaluate a constrained step along p so that
293: ||w + step*p|| = delta
294: The positive step is always better in this case. */
296: if (!i) {
297: scal = pcgP->delta / pcgP->ltsnrm;
298: VecScale(X,scal);
299: } else {
300: /* Compute roots of quadratic */
301: QuadraticRoots_Private(W,P,&pcgP->delta,&step1,&step2);
302: VecCopy(W,X);
303: #if defined(PETSC_USE_COMPLEX)
304: cstep1 = step1; VecAXPY(X,cstep1,P);
305: #else
306: VecAXPY(X,step1,P); /* x <- step1*p + x */
307: #endif
308: }
309: pcgP->ltsnrm = pcgP->delta;
310: ksp->reason = KSP_CONVERGED_CG_CONSTRAINED; /* convergence along constrained step */
311: if (!i) {
312: PetscInfo1(ksp,"constrained step: delta=%G\n",pcgP->delta);
313: } else {
314: PetscInfo3(ksp,"constrained step: step1=%G, step2=%G, delta=%G\n",step1,step2,pcgP->delta);
315: }
317: } else {
319: /* Evaluate the current step */
321: VecCopy(X,W); /* update interior iterate */
322: VecAXPY(R,-step,ASP); /* r <- -step*asp + r */
323: VecNorm(R,NORM_2,&rnrm);
325: PetscObjectTakeAccess(ksp);
326: ksp->rnorm = rnrm;
327: PetscObjectGrantAccess(ksp);
328: KSPLogResidualHistory(ksp,rnrm);
329: KSPMonitor(ksp,i+1,rnrm);
330: (*ksp->converged)(ksp,i+1,rnrm,&ksp->reason,ksp->cnvP);
331: if (ksp->reason) { /* convergence for */
332: #if defined(PETSC_USE_COMPLEX)
333: PetscInfo3(ksp,"truncated step: step=%G, rnrm=%G, delta=%G\n",PetscRealPart(step),rnrm,pcgP->delta);
334: #else
335: PetscInfo3(ksp,"truncated step: step=%G, rnrm=%G, delta=%G\n",step,rnrm,pcgP->delta);
336: #endif
337: }
338: }
339: }
340: if (ksp->reason) break; /* Convergence has been attained */
341: else { /* Compute a new AS-orthogonal direction */
342: VecDot(R,R,&rntrn);
343: beta = rntrn/rtr;
344: VecAYPX(P,beta,R); /* p <- r + beta*p */
345: #if defined(PETSC_USE_COMPLEX)
346: rtr = PetscRealPart(rntrn);
347: #else
348: rtr = rntrn;
349: #endif
350: }
351: }
352: if (!ksp->reason) {
353: ksp->reason = KSP_DIVERGED_ITS;
354: }
356: /* Unscale x */
357: VecCopy(X,WA2);
358: PCApplySymmetricRight(pc,WA2,X);
360: MatMult(Amat,X,WA);
361: VecDot(B,X,&btx);
362: VecDot(X,WA,&xtax);
363: #if defined(PETSC_USE_COMPLEX)
364: pcgP->quadratic = PetscRealPart(btx) + p5* PetscRealPart(xtax);
365: #else
366: pcgP->quadratic = btx + p5*xtax; /* Compute q(x) */
367: #endif
368: return(0);
369: }
373: PetscErrorCode KSPSetUp_QCG(KSP ksp)
374: {
378: /* Check user parameters and functions */
379: if (ksp->pc_side == PC_RIGHT) {
380: SETERRQ(PETSC_ERR_SUP,"no right preconditioning for QCG");
381: } else if (ksp->pc_side == PC_LEFT) {
382: SETERRQ(PETSC_ERR_SUP,"no left preconditioning for QCG");
383: }
385: /* Get work vectors from user code */
386: KSPDefaultGetWork(ksp,7);
387: return(0);
388: }
392: PetscErrorCode KSPDestroy_QCG(KSP ksp)
393: {
397: KSPDefaultDestroy(ksp);
398: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGGetQuadratic_C","",PETSC_NULL);
399: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGGetTrialStepNorm_C","",PETSC_NULL);
400: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGSetTrustRegionRadius_C","",PETSC_NULL);
401: return(0);
402: }
407: PetscErrorCode KSPQCGSetTrustRegionRadius_QCG(KSP ksp,PetscReal delta)
408: {
409: KSP_QCG *cgP = (KSP_QCG*)ksp->data;
412: cgP->delta = delta;
413: return(0);
414: }
420: PetscErrorCode KSPQCGGetTrialStepNorm_QCG(KSP ksp,PetscReal *ltsnrm)
421: {
422: KSP_QCG *cgP = (KSP_QCG*)ksp->data;
425: *ltsnrm = cgP->ltsnrm;
426: return(0);
427: }
433: PetscErrorCode KSPQCGGetQuadratic_QCG(KSP ksp,PetscReal *quadratic)
434: {
435: KSP_QCG *cgP = (KSP_QCG*)ksp->data;
438: *quadratic = cgP->quadratic;
439: return(0);
440: }
445: PetscErrorCode KSPSetFromOptions_QCG(KSP ksp)
446: {
448: PetscReal delta;
449: KSP_QCG *cgP = (KSP_QCG*)ksp->data;
450: PetscTruth flg;
453: PetscOptionsHead("KSP QCG Options");
454: PetscOptionsReal("-ksp_qcg_trustregionradius","Trust Region Radius","KSPQCGSetTrustRegionRadius",cgP->delta,&delta,&flg);
455: if (flg) { KSPQCGSetTrustRegionRadius(ksp,delta); }
456: PetscOptionsTail();
457: return(0);
458: }
460: /*MC
461: KSPQCG - Code to run conjugate gradient method subject to a constraint
462: on the solution norm. This is used in Trust Region methods for nonlinear equations, SNESTR
464: Options Database Keys:
465: . -ksp_qcg_trustregionradius <r> - Trust Region Radius
467: Notes: This is rarely used directly
469: Level: developer
471: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPQCGSetTrustRegionRadius()
472: KSPQCGGetTrialStepNorm(), KSPQCGGetQuadratic()
473: M*/
478: PetscErrorCode KSPCreate_QCG(KSP ksp)
479: {
481: KSP_QCG *cgP;
484: PetscMalloc(sizeof(KSP_QCG),&cgP);
485: PetscMemzero(cgP,sizeof(KSP_QCG));
486: PetscLogObjectMemory(ksp,sizeof(KSP_QCG));
487: ksp->data = (void*)cgP;
488: ksp->pc_side = PC_SYMMETRIC;
489: ksp->ops->setup = KSPSetUp_QCG;
490: ksp->ops->setfromoptions = KSPSetFromOptions_QCG;
491: ksp->ops->solve = KSPSolve_QCG;
492: ksp->ops->destroy = KSPDestroy_QCG;
493: ksp->ops->buildsolution = KSPDefaultBuildSolution;
494: ksp->ops->buildresidual = KSPDefaultBuildResidual;
495: ksp->ops->setfromoptions = 0;
496: ksp->ops->view = 0;
498: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGGetQuadratic_C",
499: "KSPQCGGetQuadratic_QCG",
500: KSPQCGGetQuadratic_QCG);
501: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGGetTrialStepNorm_C",
502: "KSPQCGGetTrialStepNorm_QCG",
503: KSPQCGGetTrialStepNorm_QCG);
504: PetscObjectComposeFunctionDynamic((PetscObject)ksp,"KSPQCGSetTrustRegionRadius_C",
505: "KSPQCGSetTrustRegionRadius_QCG",
506: KSPQCGSetTrustRegionRadius_QCG);
507: cgP->delta = PETSC_MAX; /* default trust region radius is infinite */
508: return(0);
509: }
512: /* ---------------------------------------------------------- */
515: /*
516: QuadraticRoots_Private - Computes the roots of the quadratic,
517: ||s + step*p|| - delta = 0
518: such that step1 >= 0 >= step2.
519: where
520: delta:
521: On entry delta must contain scalar delta.
522: On exit delta is unchanged.
523: step1:
524: On entry step1 need not be specified.
525: On exit step1 contains the non-negative root.
526: step2:
527: On entry step2 need not be specified.
528: On exit step2 contains the non-positive root.
529: C code is translated from the Fortran version of the MINPACK-2 Project,
530: Argonne National Laboratory, Brett M. Averick and Richard G. Carter.
531: */
532: static PetscErrorCode QuadraticRoots_Private(Vec s,Vec p,PetscReal *delta,PetscReal *step1,PetscReal *step2)
533: {
534: PetscReal dsq,ptp,pts,rad,sts;
536: #if defined(PETSC_USE_COMPLEX)
537: PetscScalar cptp,cpts,csts;
538: #endif
541: #if defined(PETSC_USE_COMPLEX)
542: VecDot(p,s,&cpts); pts = PetscRealPart(cpts);
543: VecDot(p,p,&cptp); ptp = PetscRealPart(cptp);
544: VecDot(s,s,&csts); sts = PetscRealPart(csts);
545: #else
546: VecDot(p,s,&pts);
547: VecDot(p,p,&ptp);
548: VecDot(s,s,&sts);
549: #endif
550: dsq = (*delta)*(*delta);
551: rad = sqrt((pts*pts) - ptp*(sts - dsq));
552: if (pts > 0.0) {
553: *step2 = -(pts + rad)/ptp;
554: *step1 = (sts - dsq)/(ptp * *step2);
555: } else {
556: *step1 = -(pts - rad)/ptp;
557: *step2 = (sts - dsq)/(ptp * *step1);
558: }
559: return(0);
560: }