Actual source code: cgs.c
1: #define PETSCKSP_DLL
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 PetscErrorCode KSPSetUp_CGS(KSP ksp)
14: {
18: if (ksp->pc_side == PC_SYMMETRIC) SETERRQ(PETSC_ERR_SUP,"no symmetric preconditioning for KSPCGS");
19: KSPDefaultGetWork(ksp,7);
20: return(0);
21: }
25: static PetscErrorCode KSPSolve_CGS(KSP ksp)
26: {
28: PetscInt i;
29: PetscScalar rho,rhoold,a,s,b;
30: Vec X,B,V,P,R,RP,T,Q,U,AUQ;
31: PetscReal dp = 0.0;
32: PetscTruth diagonalscale;
35: PCDiagonalScale(ksp->pc,&diagonalscale);
36: if (diagonalscale) SETERRQ1(PETSC_ERR_SUP,"Krylov method %s does not support diagonal scaling",ksp->type_name);
38: X = ksp->vec_sol;
39: B = ksp->vec_rhs;
40: R = ksp->work[0];
41: RP = ksp->work[1];
42: V = ksp->work[2];
43: T = ksp->work[3];
44: Q = ksp->work[4];
45: P = ksp->work[5];
46: U = ksp->work[6];
47: AUQ = V;
49: /* Compute initial preconditioned residual */
50: KSPInitialResidual(ksp,X,V,T,R,B);
52: /* Test for nothing to do */
53: VecNorm(R,NORM_2,&dp);
54: if (ksp->normtype == KSP_NATURAL_NORM) {
55: dp *= dp;
56: }
57: PetscObjectTakeAccess(ksp);
58: ksp->its = 0;
59: ksp->rnorm = dp;
60: PetscObjectGrantAccess(ksp);
61: KSPLogResidualHistory(ksp,dp);
62: KSPMonitor(ksp,0,dp);
63: (*ksp->converged)(ksp,0,dp,&ksp->reason,ksp->cnvP);
64: if (ksp->reason) return(0);
66: /* Make the initial Rp == R */
67: VecCopy(R,RP);
68: /* added for Fidap */
69: /* Penalize Startup - Isaac Hasbani Trick for CGS
70: Since most initial conditions result in a mostly 0 residual,
71: we change all the 0 values in the vector RP to the maximum.
72: */
73: if (ksp->normtype == KSP_NATURAL_NORM) {
74: PetscReal vr0max;
75: PetscScalar *tmp_RP=0;
76: PetscInt numnp=0, *max_pos=0;
77: VecMax(RP, max_pos, &vr0max);
78: VecGetArray(RP, &tmp_RP);
79: VecGetLocalSize(RP, &numnp);
80: for (i=0; i<numnp; i++) {
81: if (tmp_RP[i] == 0.0) tmp_RP[i] = vr0max;
82: }
83: VecRestoreArray(RP, &tmp_RP);
84: }
85: /* end of addition for Fidap */
87: /* Set the initial conditions */
88: VecDot(R,RP,&rhoold); /* rhoold = (r,rp) */
89: VecCopy(R,U);
90: VecCopy(R,P);
91: KSP_PCApplyBAorAB(ksp,P,V,T);
93: i = 0;
94: do {
96: VecDot(V,RP,&s); /* s <- (v,rp) */
97: a = rhoold / s; /* a <- rho / s */
98: VecWAXPY(Q,-a,V,U); /* q <- u - a v */
99: VecWAXPY(T,1.0,U,Q); /* t <- u + q */
100: VecAXPY(X,a,T); /* x <- x + a (u + q) */
101: KSP_PCApplyBAorAB(ksp,T,AUQ,U);
102: VecAXPY(R,-a,AUQ); /* 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(U,b,Q,R); /* u <- r + b q */
121: VecAXPY(Q,b,P);
122: VecWAXPY(P,b,Q,U); /* p <- u + b(q + b p) */
123: KSP_PCApplyBAorAB(ksp,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.
138: Options Database Keys:
139: . see KSPSolve()
141: Level: beginner
143: Notes: Reference: Sonneveld, 1989.
145: .seealso: KSPCreate(), KSPSetType(), KSPType (for list of available types), KSP, KSPBCGS
146: M*/
150: PetscErrorCode KSPCreate_CGS(KSP ksp)
151: {
153: ksp->data = (void*)0;
154: ksp->pc_side = PC_LEFT;
155: ksp->ops->setup = KSPSetUp_CGS;
156: ksp->ops->solve = KSPSolve_CGS;
157: ksp->ops->destroy = KSPDefaultDestroy;
158: ksp->ops->buildsolution = KSPDefaultBuildSolution;
159: ksp->ops->buildresidual = KSPDefaultBuildResidual;
160: ksp->ops->setfromoptions = 0;
161: ksp->ops->view = 0;
162: return(0);
163: }