Actual source code: shift.c
slepc-3.7.3 2016-09-29
1: /*
2: Shift spectral transformation, applies (A + sigma I) as operator, or
3: inv(B)(A + sigma B) for generalized problems
5: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6: SLEPc - Scalable Library for Eigenvalue Problem Computations
7: Copyright (c) 2002-2016, Universitat Politecnica de Valencia, Spain
9: This file is part of SLEPc.
11: SLEPc is free software: you can redistribute it and/or modify it under the
12: terms of version 3 of the GNU Lesser General Public License as published by
13: the Free Software Foundation.
15: SLEPc is distributed in the hope that it will be useful, but WITHOUT ANY
16: WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
17: FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
18: more details.
20: You should have received a copy of the GNU Lesser General Public License
21: along with SLEPc. If not, see <http://www.gnu.org/licenses/>.
22: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
23: */
25: #include <slepc/private/stimpl.h>
29: PetscErrorCode STApply_Shift(ST st,Vec x,Vec y)
30: {
34: if (st->nmat>1) {
35: /* generalized eigenproblem: y = B^-1 (A - sB) x */
36: MatMult(st->T[0],x,st->w);
37: STMatSolve(st,st->w,y);
38: } else {
39: /* standard eigenproblem: y = (A - sI) x */
40: MatMult(st->T[0],x,y);
41: }
42: return(0);
43: }
47: PetscErrorCode STApplyTranspose_Shift(ST st,Vec x,Vec y)
48: {
52: if (st->nmat>1) {
53: /* generalized eigenproblem: y = (A - sB)^T B^-T x */
54: STMatSolveTranspose(st,x,st->w);
55: MatMultTranspose(st->T[0],st->w,y);
56: } else {
57: /* standard eigenproblem: y = (A^T - sI) x */
58: MatMultTranspose(st->T[0],x,y);
59: }
60: return(0);
61: }
65: PetscErrorCode STBackTransform_Shift(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
66: {
67: PetscInt j;
70: for (j=0;j<n;j++) {
71: eigr[j] += st->sigma;
72: }
73: return(0);
74: }
78: PetscErrorCode STPostSolve_Shift(ST st)
79: {
83: if (st->shift_matrix == ST_MATMODE_INPLACE) {
84: if (st->nmat>1) {
85: MatAXPY(st->A[0],st->sigma,st->A[1],st->str);
86: } else {
87: MatShift(st->A[0],st->sigma);
88: }
89: st->Astate[0] = ((PetscObject)st->A[0])->state;
90: st->state = ST_STATE_INITIAL;
91: }
92: return(0);
93: }
97: PetscErrorCode STSetUp_Shift(ST st)
98: {
100: PetscInt k,nc,nmat=PetscMax(st->nmat,2);
101: PetscScalar *coeffs=NULL;
104: if (st->nmat>1) {
105: ST_AllocateWorkVec(st);
106: }
107: if (nmat<3 || st->transform) {
108: if (nmat>2) {
109: nc = (nmat*(nmat+1))/2;
110: PetscMalloc1(nc,&coeffs);
111: /* Compute coeffs */
112: STCoeffs_Monomial(st,coeffs);
113: }
114: /* T[n] = A_n */
115: k = nmat-1;
116: PetscObjectReference((PetscObject)st->A[k]);
117: MatDestroy(&st->T[k]);
118: st->T[k] = st->A[k];
119: for (k=0;k<nmat-1;k++) {
120: STMatMAXPY_Private(st,nmat>2?st->sigma:-st->sigma,0.0,k,coeffs?coeffs+((nmat-k)*(nmat-k-1))/2:NULL,PetscNot(st->state==ST_STATE_UPDATED),&st->T[k]);
121: }
122: if (nmat>2) { PetscFree(coeffs); }
123: } else {
124: for (k=0;k<nmat;k++) {
125: PetscObjectReference((PetscObject)st->A[k]);
126: MatDestroy(&st->T[k]);
127: st->T[k] = st->A[k];
128: }
129: }
130: if (nmat>=2 && st->transform) {
131: PetscObjectReference((PetscObject)st->T[nmat-1]);
132: MatDestroy(&st->P);
133: st->P = st->T[nmat-1];
134: }
135: if (st->P) {
136: if (!st->ksp) { STGetKSP(st,&st->ksp); }
137: STCheckFactorPackage(st);
138: KSPSetOperators(st->ksp,st->P,st->P);
139: KSPSetErrorIfNotConverged(st->ksp,PETSC_TRUE);
140: KSPSetUp(st->ksp);
141: }
142: return(0);
143: }
147: PetscErrorCode STSetShift_Shift(ST st,PetscScalar newshift)
148: {
150: PetscInt k,nc,nmat=PetscMax(st->nmat,2);
151: PetscScalar *coeffs=NULL;
154: if (st->transform) {
155: if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
156: nc = (nmat*(nmat+1))/2;
157: PetscMalloc1(nc,&coeffs);
158: /* Compute coeffs */
159: STCoeffs_Monomial(st,coeffs);
160: }
161: for (k=0;k<nmat-1;k++) {
162: STMatMAXPY_Private(st,nmat>2?newshift:-newshift,nmat>2?st->sigma:-st->sigma,k,coeffs?coeffs+((nmat-k)*(nmat-k-1))/2:NULL,PETSC_FALSE,&st->T[k]);
163: }
164: if (st->shift_matrix == ST_MATMODE_COPY && nmat>2) {
165: PetscFree(coeffs);
166: }
167: }
168: return(0);
169: }
173: PetscErrorCode STSetFromOptions_Shift(PetscOptionItems *PetscOptionsObject,ST st)
174: {
176: PC pc;
177: PCType pctype;
178: KSPType ksptype;
181: if (!st->ksp) { STGetKSP(st,&st->ksp); }
182: KSPGetPC(st->ksp,&pc);
183: KSPGetType(st->ksp,&ksptype);
184: PCGetType(pc,&pctype);
185: if (!pctype && !ksptype) {
186: if (st->shift_matrix == ST_MATMODE_SHELL) {
187: /* in shell mode use GMRES with Jacobi as the default */
188: KSPSetType(st->ksp,KSPGMRES);
189: PCSetType(pc,PCJACOBI);
190: } else {
191: /* use direct solver as default */
192: KSPSetType(st->ksp,KSPPREONLY);
193: PCSetType(pc,PCLU);
194: }
195: }
196: return(0);
197: }
201: PETSC_EXTERN PetscErrorCode STCreate_Shift(ST st)
202: {
204: st->ops->apply = STApply_Shift;
205: st->ops->getbilinearform = STGetBilinearForm_Default;
206: st->ops->applytrans = STApplyTranspose_Shift;
207: st->ops->postsolve = STPostSolve_Shift;
208: st->ops->backtransform = STBackTransform_Shift;
209: st->ops->setfromoptions = STSetFromOptions_Shift;
210: st->ops->setup = STSetUp_Shift;
211: st->ops->setshift = STSetShift_Shift;
212: return(0);
213: }