Actual source code: shift.c
slepc-3.12.2 2020-01-13
1: /*
2: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
3: SLEPc - Scalable Library for Eigenvalue Problem Computations
4: Copyright (c) 2002-2019, Universitat Politecnica de Valencia, Spain
6: This file is part of SLEPc.
7: SLEPc is distributed under a 2-clause BSD license (see LICENSE).
8: - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
9: */
10: /*
11: Shift spectral transformation, applies (A + sigma I) as operator, or
12: inv(B)(A + sigma B) for generalized problems
13: */
15: #include <slepc/private/stimpl.h>
17: PetscErrorCode STApply_Shift(ST st,Vec x,Vec y)
18: {
22: if (st->nmat>1) {
23: /* generalized eigenproblem: y = B^-1 (A - sB) x */
24: MatMult(st->T[0],x,st->work[0]);
25: STMatSolve(st,st->work[0],y);
26: } else {
27: /* standard eigenproblem: y = (A - sI) x */
28: MatMult(st->T[0],x,y);
29: }
30: return(0);
31: }
33: PetscErrorCode STApplyTranspose_Shift(ST st,Vec x,Vec y)
34: {
38: if (st->nmat>1) {
39: /* generalized eigenproblem: y = (A - sB)^T B^-T x */
40: STMatSolveTranspose(st,x,st->work[0]);
41: MatMultTranspose(st->T[0],st->work[0],y);
42: } else {
43: /* standard eigenproblem: y = (A^T - sI) x */
44: MatMultTranspose(st->T[0],x,y);
45: }
46: return(0);
47: }
49: PetscErrorCode STBackTransform_Shift(ST st,PetscInt n,PetscScalar *eigr,PetscScalar *eigi)
50: {
51: PetscInt j;
54: for (j=0;j<n;j++) {
55: eigr[j] += st->sigma;
56: }
57: return(0);
58: }
60: PetscErrorCode STPostSolve_Shift(ST st)
61: {
65: if (st->matmode == ST_MATMODE_INPLACE) {
66: if (st->nmat>1) {
67: MatAXPY(st->A[0],st->sigma,st->A[1],st->str);
68: } else {
69: MatShift(st->A[0],st->sigma);
70: }
71: st->Astate[0] = ((PetscObject)st->A[0])->state;
72: st->state = ST_STATE_INITIAL;
73: }
74: return(0);
75: }
77: PetscErrorCode STSetUp_Shift(ST st)
78: {
80: PetscInt k,nc,nmat=PetscMax(st->nmat,2);
81: PetscScalar *coeffs=NULL;
84: if (st->nmat>1) {
85: STSetWorkVecs(st,1);
86: }
87: st->usesksp = (st->nmat>1)? PETSC_TRUE: PETSC_FALSE;
88: if (nmat<3 || st->transform) {
89: if (nmat>2) {
90: nc = (nmat*(nmat+1))/2;
91: PetscMalloc1(nc,&coeffs);
92: /* Compute coeffs */
93: STCoeffs_Monomial(st,coeffs);
94: }
95: /* T[n] = A_n */
96: k = nmat-1;
97: PetscObjectReference((PetscObject)st->A[k]);
98: MatDestroy(&st->T[k]);
99: st->T[k] = st->A[k];
100: for (k=0;k<nmat-1;k++) {
101: 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]);
102: }
103: if (nmat>2) { PetscFree(coeffs); }
104: } else {
105: for (k=0;k<nmat;k++) {
106: PetscObjectReference((PetscObject)st->A[k]);
107: MatDestroy(&st->T[k]);
108: st->T[k] = st->A[k];
109: }
110: }
111: if (nmat>=2 && st->transform) {
112: PetscObjectReference((PetscObject)st->T[nmat-1]);
113: MatDestroy(&st->P);
114: st->P = st->T[nmat-1];
115: }
116: if (st->P) {
117: if (!st->ksp) { STGetKSP(st,&st->ksp); }
118: STCheckFactorPackage(st);
119: KSPSetOperators(st->ksp,st->P,st->P);
120: KSPSetUp(st->ksp);
121: }
122: return(0);
123: }
125: PetscErrorCode STSetShift_Shift(ST st,PetscScalar newshift)
126: {
128: PetscInt k,nc,nmat=PetscMax(st->nmat,2);
129: PetscScalar *coeffs=NULL;
132: if (st->transform) {
133: if (st->matmode == ST_MATMODE_COPY && nmat>2) {
134: nc = (nmat*(nmat+1))/2;
135: PetscMalloc1(nc,&coeffs);
136: /* Compute coeffs */
137: STCoeffs_Monomial(st,coeffs);
138: }
139: for (k=0;k<nmat-1;k++) {
140: 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]);
141: }
142: if (st->matmode == ST_MATMODE_COPY && nmat>2) {
143: PetscFree(coeffs);
144: }
145: }
146: return(0);
147: }
149: SLEPC_EXTERN PetscErrorCode STCreate_Shift(ST st)
150: {
152: st->usesksp = PETSC_TRUE;
154: st->ops->apply = STApply_Shift;
155: st->ops->getbilinearform = STGetBilinearForm_Default;
156: st->ops->applytrans = STApplyTranspose_Shift;
157: st->ops->postsolve = STPostSolve_Shift;
158: st->ops->backtransform = STBackTransform_Shift;
159: st->ops->setup = STSetUp_Shift;
160: st->ops->setshift = STSetShift_Shift;
161: st->ops->setdefaultksp = STSetDefaultKSP_Default;
162: return(0);
163: }