Actual source code: cgs.c
1: /*$Id: cgs.c,v 1.64 2001/08/07 03:03:51 balay Exp $*/
3: /*
5: Note that for the complex numbers version, the VecDot() arguments
6: within the code MUST remain in the order given for correct computation
7: of inner products.
8: */
9: #include src/ksp/ksp/kspimpl.h
13: static int KSPSetUp_CGS(KSP ksp)
14: {
18: if (ksp->pc_side == PC_SYMMETRIC) SETERRQ(2,"no symmetric preconditioning for KSPCGS");
19: KSPDefaultGetWork(ksp,7);
20: return(0);
21: }
25: static int KSPSolve_CGS(KSP ksp)
26: {
27: int i,ierr;
28: PetscScalar rho,rhoold,a,s,b,tmp,one = 1.0;
29: Vec X,B,V,P,R,RP,T,Q,U,AUQ;
30: PetscReal dp = 0.0;
31: PetscTruth diagonalscale;
34: PCDiagonalScale(ksp->B,&diagonalscale);
35: if (diagonalscale) SETERRQ1(1,"Krylov method %s does not support diagonal scaling",ksp->type_name);
37: X = ksp->vec_sol;
38: B = ksp->vec_rhs;
39: R = ksp->work[0];
40: RP = ksp->work[1];
41: V = ksp->work[2];
42: T = ksp->work[3];
43: Q = ksp->work[4];
44: P = ksp->work[5];
45: U = ksp->work[6];
46: AUQ = V;
48: /* Compute initial preconditioned residual */
49: KSPInitialResidual(ksp,X,V,T,R,B);
51: /* Test for nothing to do */
52: VecNorm(R,NORM_2,&dp);
53: if (ksp->normtype == KSP_NATURAL_NORM) {
54: dp *= dp;
55: }
56: PetscObjectTakeAccess(ksp);
57: ksp->its = 0;
58: ksp->rnorm = dp;
59: PetscObjectGrantAccess(ksp);
60: KSPLogResidualHistory(ksp,dp);
61: KSPMonitor(ksp,0,dp);
62: (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
63: if (ksp->reason) return(0);
65: /* Make the initial Rp == R */
66: VecCopy(R,RP);
67: /* added for Fidap */
68: /* Penalize Startup - Isaac Hasbani Trick for CGS
69: Since most initial conditions result in a mostly 0 residual,
70: we change all the 0 values in the vector RP to the maximum.
71: */
72: if (ksp->normtype == KSP_NATURAL_NORM) {
73: PetscReal vr0max;
74: PetscScalar *tmp_RP=0;
75: int numnp=0, *max_pos=0;
76: VecMax(RP, max_pos, &vr0max);
77: VecGetArray(RP, &tmp_RP);
78: VecGetLocalSize(RP, &numnp);
79: for (i=0; i<numnp; i++) {
80: if (tmp_RP[i] == 0.0) tmp_RP[i] = vr0max;
81: }
82: VecRestoreArray(RP, &tmp_RP);
83: }
84: /* end of addition for Fidap */
86: /* Set the initial conditions */
87: VecDot(R,RP,&rhoold); /* rhoold = (r,rp) */
88: VecCopy(R,U);
89: VecCopy(R,P);
90: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,T);
92: i = 0;
93: do {
95: VecDot(V,RP,&s); /* s <- (v,rp) */
96: a = rhoold / s; /* a <- rho / s */
97: tmp = -a;
98: VecWAXPY(&tmp,V,U,Q); /* q <- u - a v */
99: VecWAXPY(&one,U,Q,T); /* t <- u + q */
100: VecAXPY(&a,T,X); /* x <- x + a (u + q) */
101: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,T,AUQ,U);
102: VecAXPY(&tmp,AUQ,R); /* r <- r - a K (u + q) */
103: VecDot(R,RP,&rho); /* rho <- (r,rp) */
104: if (ksp->normtype == KSP_PRECONDITIONED_NORM) {
105: VecNorm(R,NORM_2,&dp);
106: } else if (ksp->normtype == KSP_NATURAL_NORM) {
107: dp = PetscAbsScalar(rho);
108: }
110: PetscObjectTakeAccess(ksp);
111: ksp->its++;
112: ksp->rnorm = dp;
113: PetscObjectGrantAccess(ksp);
114: KSPLogResidualHistory(ksp,dp);
115: KSPMonitor(ksp,i+1,dp);
116: (*ksp->converged)(ksp,i+1,dp,&ksp->reason,ksp->cnvP);
117: if (ksp->reason) break;
119: b = rho / rhoold; /* b <- rho / rhoold */
120: VecWAXPY(&b,Q,R,U); /* u <- r + b q */
121: VecAXPY(&b,P,Q);
122: VecWAXPY(&b,Q,U,P); /* p <- u + b(q + b p) */
123: KSP_PCApplyBAorAB(ksp,ksp->B,ksp->pc_side,P,V,Q); /* v <- K p */
124: rhoold = rho;
125: i++;
126: } while (i<ksp->max_it);
127: if (i == ksp->max_it) {
128: ksp->reason = KSP_DIVERGED_ITS;
129: }
131: KSPUnwindPreconditioner(ksp,X,T);
132: return(0);
133: }
135: /*MC
136: KSPCGS - This code implements the CGS (Conjugate Gradient Squared) method.
137: Reference: Sonneveld, 1989.
139: Options Database Keys:
140: . see KSPSolve()
142: Level: beginner
144: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBCGS
146: M*/
148: EXTERN_C_BEGIN
151: int KSPCreate_CGS(KSP ksp)
152: {
154: ksp->data = (void*)0;
155: ksp->pc_side = PC_LEFT;
156: ksp->ops->setup = KSPSetUp_CGS;
157: ksp->ops->solve = KSPSolve_CGS;
158: ksp->ops->destroy = KSPDefaultDestroy;
159: ksp->ops->buildsolution = KSPDefaultBuildSolution;
160: ksp->ops->buildresidual = KSPDefaultBuildResidual;
161: ksp->ops->setfromoptions = 0;
162: ksp->ops->view = 0;
163: return(0);
164: }
165: EXTERN_C_END