Subversion Repositories slepc-dev

Rev

Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
6 dsic.upv.es!jroman 1
 
2
/*                      
3
       This file implements a wrapper to the ARPACK package
4
*/
5
#include "src/eps/impls/arpack/arpackp.h"
6
 
7
#undef __FUNCT__  
8
#define __FUNCT__ "EPSSetUp_ARPACK"
476 dsic.upv.es!antodo 9
PetscErrorCode EPSSetUp_ARPACK(EPS eps)
6 dsic.upv.es!jroman 10
{
476 dsic.upv.es!antodo 11
  PetscErrorCode ierr;
1089 slepc 12
  PetscInt       N, n;
13
  int            ncv;
476 dsic.upv.es!antodo 14
  EPS_ARPACK     *ar = (EPS_ARPACK *)eps->data;
6 dsic.upv.es!jroman 15
 
16
  PetscFunctionBegin;
260 dsic.upv.es!antodo 17
  ierr = VecGetSize(eps->vec_initial,&N);CHKERRQ(ierr);
18
  if (eps->ncv) {
19
    if (eps->ncv<eps->nev+2) SETERRQ(1,"The value of ncv must be at least nev+2");
1220 slepc 20
  } else /* set default value of ncv */
260 dsic.upv.es!antodo 21
    eps->ncv = PetscMin(PetscMax(20,2*eps->nev+1),N);
577 dsic.upv.es!antodo 22
  if (!eps->max_it) eps->max_it = PetscMax(300,(int)(2*N/eps->ncv));
260 dsic.upv.es!antodo 23
 
6 dsic.upv.es!jroman 24
  ncv = eps->ncv;
25
#if defined(PETSC_USE_COMPLEX)
1040 slepc 26
  ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
6 dsic.upv.es!jroman 27
  ierr = PetscMalloc(ncv*sizeof(PetscReal),&ar->rwork);CHKERRQ(ierr);
28
  ar->lworkl = 3*ncv*ncv+5*ncv;
1040 slepc 29
  ierr = PetscFree(ar->workev);CHKERRQ(ierr);
6 dsic.upv.es!jroman 30
  ierr = PetscMalloc(3*ncv*sizeof(PetscScalar),&ar->workev);CHKERRQ(ierr);
31
#else
32
  if( eps->ishermitian ) {
33
    ar->lworkl = ncv*(ncv+8);
1220 slepc 34
  } else {
6 dsic.upv.es!jroman 35
    ar->lworkl = 3*ncv*ncv+6*ncv;
1040 slepc 36
    ierr = PetscFree(ar->workev);CHKERRQ(ierr);
6 dsic.upv.es!jroman 37
    ierr = PetscMalloc(3*ncv*sizeof(PetscScalar),&ar->workev);CHKERRQ(ierr);
38
  }
39
#endif
1040 slepc 40
  ierr = PetscFree(ar->workl);CHKERRQ(ierr);
6 dsic.upv.es!jroman 41
  ierr = PetscMalloc(ar->lworkl*sizeof(PetscScalar),&ar->workl);CHKERRQ(ierr);
1040 slepc 42
  ierr = PetscFree(ar->select);CHKERRQ(ierr);
6 dsic.upv.es!jroman 43
  ierr = PetscMalloc(ncv*sizeof(PetscTruth),&ar->select);CHKERRQ(ierr);
44
  ierr = VecGetLocalSize(eps->vec_initial,&n); CHKERRQ(ierr);
1040 slepc 45
  ierr = PetscFree(ar->workd);CHKERRQ(ierr);
6 dsic.upv.es!jroman 46
  ierr = PetscMalloc(3*n*sizeof(PetscScalar),&ar->workd);CHKERRQ(ierr);
47
 
48
  ierr = EPSDefaultGetWork(eps,1);CHKERRQ(ierr);
260 dsic.upv.es!antodo 49
  ierr = EPSAllocateSolutionContiguous(eps);CHKERRQ(ierr);
6 dsic.upv.es!jroman 50
 
51
  PetscFunctionReturn(0);
52
}
53
 
54
#undef __FUNCT__  
55
#define __FUNCT__ "EPSSolve_ARPACK"
476 dsic.upv.es!antodo 56
PetscErrorCode EPSSolve_ARPACK(EPS eps)
6 dsic.upv.es!jroman 57
{
1089 slepc 58
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 59
  EPS_ARPACK *ar = (EPS_ARPACK *)eps->data;
1023 slepc 60
  char        bmat[1], howmny[] = "A";
61
  const char  *which;
1089 slepc 62
  PetscInt    nn;
1220 slepc 63
  int         n, iparam[11], ipntr[14], ido, info;
740 dsic.upv.es!antodo 64
  PetscScalar sigmar = 0.0, sigmai, *pV, *resid;
6 dsic.upv.es!jroman 65
  Vec         x, y, w;
740 dsic.upv.es!antodo 66
  Mat         A,B;
67
  PetscTruth  isSinv,isShift,rvec;
6 dsic.upv.es!jroman 68
  MPI_Fint    fcomm;
69
 
70
  PetscFunctionBegin;
71
 
72
  fcomm = MPI_Comm_c2f(eps->comm);
1089 slepc 73
  ierr = VecGetLocalSize(eps->vec_initial,&nn); CHKERRQ(ierr);
74
  n = nn;
6 dsic.upv.es!jroman 75
  ierr = VecCreateMPIWithArray(eps->comm,n,PETSC_DECIDE,PETSC_NULL,&x);CHKERRQ(ierr);
76
  ierr = VecCreateMPIWithArray(eps->comm,n,PETSC_DECIDE,PETSC_NULL,&y);CHKERRQ(ierr);
77
  ierr = VecGetArray(eps->V[0],&pV);CHKERRQ(ierr);
78
  ierr = VecGetArray(eps->vec_initial,&resid);CHKERRQ(ierr);
79
 
80
  ido  = 0;            /* first call to reverse communication interface */
81
  info = 1;            /* indicates a initial vector is provided */
82
  iparam[0] = 1;       /* use exact shifts */
83
  iparam[2] = eps->max_it;  /* maximum number of Arnoldi update iterations */
84
  iparam[3] = 1;       /* blocksize */
85
  iparam[4] = 0;       /* number of converged Ritz values */
86
 
87
  /*
88
     Computational modes ([]=not supported):
89
            symmetric    non-symmetric    complex
90
        1     1  'I'        1  'I'         1  'I'
91
        2     3  'I'        3  'I'         3  'I'
92
        3     2  'G'        2  'G'         2  'G'
93
        4     3  'G'        3  'G'         3  'G'
94
        5   [ 4  'G' ]    [ 3  'G' ]
95
        6   [ 5  'G' ]    [ 4  'G' ]
96
   */
740 dsic.upv.es!antodo 97
  bmat[0] = 'I';
98
  iparam[6] = 1;
99
  if (eps->ishermitian && eps->isgeneralized) {
100
    ierr = PetscTypeCompare((PetscObject)eps->OP,STSHIFT,&isShift);CHKERRQ(ierr);
101
    ierr = PetscTypeCompare((PetscObject)eps->OP,STSINV,&isSinv);CHKERRQ(ierr);
102
    if (isSinv) {
103
      bmat[0] = 'G';
104
      iparam[6] = 3;
105
      ierr = STGetShift(eps->OP,&sigmar);CHKERRQ(ierr);
106
      sigmai = 0.0;
107
    } else if (isShift) {
108
      bmat[0] = 'G';
109
      iparam[6] = 2;
110
    }
6 dsic.upv.es!jroman 111
  }
112
 
113
#if !defined(PETSC_USE_COMPLEX)
114
    if (eps->ishermitian) {
91 dsic.upv.es!antodo 115
      switch(eps->which) {
116
        case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
117
        case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
118
        case EPS_LARGEST_REAL:       which = "LA"; break;
119
        case EPS_SMALLEST_REAL:      which = "SA"; break;
120
        default: SETERRQ(1,"Wrong value of eps->which");
121
      }
176 dsic.upv.es!antodo 122
    } else {
123
#endif
91 dsic.upv.es!antodo 124
      switch(eps->which) {
125
        case EPS_LARGEST_MAGNITUDE:  which = "LM"; break;
126
        case EPS_SMALLEST_MAGNITUDE: which = "SM"; break;
127
        case EPS_LARGEST_REAL:       which = "LR"; break;
128
        case EPS_SMALLEST_REAL:      which = "SR"; break;
129
        case EPS_LARGEST_IMAGINARY:  which = "LI"; break;
130
        case EPS_SMALLEST_IMAGINARY: which = "SI"; break;
131
        default: SETERRQ(1,"Wrong value of eps->which");
132
      }
176 dsic.upv.es!antodo 133
#if !defined(PETSC_USE_COMPLEX)
134
    }
135
#endif
136
 
1220 slepc 137
  eps->its = iparam[2];
740 dsic.upv.es!antodo 138
  do {
176 dsic.upv.es!antodo 139
 
140
#if !defined(PETSC_USE_COMPLEX)
141
    if (eps->ishermitian) {
142
      ARsaupd_( &fcomm, &ido, bmat, &n, which, &eps->nev, &eps->tol,
143
                resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
144
                ar->workl, &ar->lworkl, &info, 1, 2 );
1220 slepc 145
      if (eps->its != iparam[2]) {
146
        eps->its = iparam[2];
147
        EPSMonitor(eps,iparam[2],iparam[4],&ar->workl[ipntr[5]-1],eps->eigi,&ar->workl[ipntr[6]-1],eps->ncv);
148
      }
176 dsic.upv.es!antodo 149
    }
150
    else {
6 dsic.upv.es!jroman 151
      ARnaupd_( &fcomm, &ido, bmat, &n, which, &eps->nev, &eps->tol,
152
                resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
153
                ar->workl, &ar->lworkl, &info, 1, 2 );
1220 slepc 154
      if (eps->its != iparam[2]) {
155
        eps->its = iparam[2];
156
        EPSMonitor(eps,iparam[2],iparam[4],&ar->workl[ipntr[5]-1],&ar->workl[ipntr[6]-1],&ar->workl[ipntr[7]-1],eps->ncv);
157
      }
6 dsic.upv.es!jroman 158
    }
159
#else
160
    ARnaupd_( &fcomm, &ido, bmat, &n, which, &eps->nev, &eps->tol,
161
              resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
162
              ar->workl, &ar->lworkl, ar->rwork, &info, 1, 2 );
1220 slepc 163
    if (eps->its != iparam[2]) {
164
      eps->its = iparam[2];
165
      EPSMonitor(eps,eps->its,iparam[4],&ar->workl[ipntr[5]-1],eps->eigi,(PetscReal*)&ar->workl[ipntr[7]-1],eps->ncv);
166
    }
6 dsic.upv.es!jroman 167
#endif
571 dsic.upv.es!antodo 168
 
740 dsic.upv.es!antodo 169
    if (ido >= -1 && ido <= 2) {
170
      ierr = VecPlaceArray(x,&ar->workd[ipntr[0]-1]); CHKERRQ(ierr);
171
      ierr = VecPlaceArray(y,&ar->workd[ipntr[1]-1]); CHKERRQ(ierr);
172
      if (ido == 1 || ido == -1) { /* Y=OP*X */
173
        ierr = STApply(eps->OP,x,y); CHKERRQ(ierr);
1163 slepc 174
        ierr = EPSOrthogonalize(eps,eps->nds,PETSC_NULL,eps->DS,y,PETSC_NULL,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr);
740 dsic.upv.es!antodo 175
        if (ido == 1 && iparam[6] == 2) { /* X=A*X */
176
          w = eps->work[0];
177
          ierr = STGetOperators(eps->OP,&A,PETSC_NULL); CHKERRQ(ierr);
178
          ierr = MatMult(A,x,w); CHKERRQ(ierr);
179
          ierr = VecCopy(w,x); CHKERRQ(ierr);
1163 slepc 180
          ierr = EPSOrthogonalize(eps,eps->nds,PETSC_NULL,eps->DS,x,PETSC_NULL,PETSC_NULL,PETSC_NULL); CHKERRQ(ierr);    
740 dsic.upv.es!antodo 181
        }
182
      } else if (ido == 2) { /* Y=B*X */
183
        ierr = STGetOperators(eps->OP,PETSC_NULL,&B); CHKERRQ(ierr);
184
        ierr = MatMult(B,x,y); CHKERRQ(ierr);
6 dsic.upv.es!jroman 185
      }
850 dsic.upv.es!antodo 186
      ierr = VecResetArray(x); CHKERRQ(ierr);
187
      ierr = VecResetArray(y); CHKERRQ(ierr);
742 dsic.upv.es!antodo 188
    } else if (ido != 99) {
740 dsic.upv.es!antodo 189
      SETERRQ1(1,"Internal error in ARPACK reverse comunication interface (ido=%i)\n",ido);
6 dsic.upv.es!jroman 190
    }
740 dsic.upv.es!antodo 191
 
192
  } while (ido != 99);
6 dsic.upv.es!jroman 193
 
194
  eps->nconv = iparam[4];
195
 
740 dsic.upv.es!antodo 196
  if (info==3) { SETERRQ(1,"No shift could be applied in xxAUPD.\n"
197
                           "Try increasing the size of NCV relative to NEV."); }
198
  else if (info!=0 && info!=1) { SETERRQ1(PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxAUPD (%d)",info);}
6 dsic.upv.es!jroman 199
 
393 dsic.upv.es!antodo 200
  rvec = PETSC_TRUE;
6 dsic.upv.es!jroman 201
 
740 dsic.upv.es!antodo 202
  if (eps->nconv > 0) {
6 dsic.upv.es!jroman 203
#if !defined(PETSC_USE_COMPLEX)
740 dsic.upv.es!antodo 204
    if (eps->ishermitian) {
205
      ARseupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr,  
206
                 pV, &n, &sigmar,
207
                 bmat, &n, which, &eps->nev, &eps->tol,
208
                 resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
209
                 ar->workl, &ar->lworkl, &info, 1, 1, 2 );
210
    }
211
    else {
212
      ARneupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr, eps->eigi,
213
                 pV, &n, &sigmar, &sigmai, ar->workev,
214
                 bmat, &n, which, &eps->nev, &eps->tol,
215
                 resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
216
                 ar->workl, &ar->lworkl, &info, 1, 1, 2 );
217
    }
218
#else
219
    ARneupd_ ( &fcomm, &rvec, howmny, ar->select, eps->eigr,
220
               pV, &n, &sigmar, ar->workev,
6 dsic.upv.es!jroman 221
               bmat, &n, which, &eps->nev, &eps->tol,
222
               resid, &eps->ncv, pV, &n, iparam, ipntr, ar->workd,
740 dsic.upv.es!antodo 223
               ar->workl, &ar->lworkl, ar->rwork, &info, 1, 1, 2 );
6 dsic.upv.es!jroman 224
#endif
740 dsic.upv.es!antodo 225
    if (info!=0) { SETERRQ1(PETSC_ERR_LIB,"Error reported by ARPACK subroutine xxEUPD (%d)",info); }
226
  }
6 dsic.upv.es!jroman 227
 
228
  ierr = VecRestoreArray( eps->V[0], &pV ); CHKERRQ(ierr);
229
  ierr = VecRestoreArray( eps->vec_initial, &resid ); CHKERRQ(ierr);
740 dsic.upv.es!antodo 230
  if( eps->nconv >= eps->nev ) eps->reason = EPS_CONVERGED_TOL;
231
  else eps->reason = EPS_DIVERGED_ITS;
6 dsic.upv.es!jroman 232
 
233
  if (eps->ishermitian) {
234
    ierr = PetscMemcpy(eps->errest,&ar->workl[ipntr[8]-1],eps->nconv);CHKERRQ(ierr);
235
  } else {
236
    ierr = PetscMemcpy(eps->errest,&ar->workl[ipntr[10]-1],eps->nconv);CHKERRQ(ierr);
237
  }
238
 
239
  ierr = VecDestroy(x);CHKERRQ(ierr);
240
  ierr = VecDestroy(y);CHKERRQ(ierr);
241
 
242
  PetscFunctionReturn(0);
243
}
244
 
245
#undef __FUNCT__  
176 dsic.upv.es!antodo 246
#define __FUNCT__ "EPSBackTransform_ARPACK"
476 dsic.upv.es!antodo 247
PetscErrorCode EPSBackTransform_ARPACK(EPS eps)
176 dsic.upv.es!antodo 248
{
476 dsic.upv.es!antodo 249
  PetscErrorCode ierr;
742 dsic.upv.es!antodo 250
  PetscTruth     isShift,isSinv;
176 dsic.upv.es!antodo 251
 
252
  PetscFunctionBegin;
742 dsic.upv.es!antodo 253
  if (eps->ishermitian && eps->isgeneralized) {
254
    ierr = PetscTypeCompare((PetscObject)eps->OP,STSHIFT,&isShift);CHKERRQ(ierr);
255
    ierr = PetscTypeCompare((PetscObject)eps->OP,STSINV,&isSinv);CHKERRQ(ierr);
256
    if (isSinv || isShift) PetscFunctionReturn(0);
442 dsic.upv.es!antodo 257
  }
742 dsic.upv.es!antodo 258
  ierr = EPSBackTransform_Default(eps);CHKERRQ(ierr);
176 dsic.upv.es!antodo 259
  PetscFunctionReturn(0);
260
}
261
 
262
#undef __FUNCT__  
6 dsic.upv.es!jroman 263
#define __FUNCT__ "EPSDestroy_ARPACK"
476 dsic.upv.es!antodo 264
PetscErrorCode EPSDestroy_ARPACK(EPS eps)
6 dsic.upv.es!jroman 265
{
476 dsic.upv.es!antodo 266
  PetscErrorCode ierr;
267
  EPS_ARPACK     *ar = (EPS_ARPACK *)eps->data;
6 dsic.upv.es!jroman 268
 
269
  PetscFunctionBegin;
24 dsic.upv.es!jroman 270
  PetscValidHeaderSpecific(eps,EPS_COOKIE,1);
1040 slepc 271
  ierr = PetscFree(ar->workev);CHKERRQ(ierr);
272
  ierr = PetscFree(ar->workl);CHKERRQ(ierr);
273
  ierr = PetscFree(ar->select);CHKERRQ(ierr);
274
  ierr = PetscFree(ar->workd);CHKERRQ(ierr);
6 dsic.upv.es!jroman 275
#if defined(PETSC_USE_COMPLEX)
1040 slepc 276
  ierr = PetscFree(ar->rwork);CHKERRQ(ierr);
6 dsic.upv.es!jroman 277
#endif
1040 slepc 278
  ierr = PetscFree(eps->data);CHKERRQ(ierr);
260 dsic.upv.es!antodo 279
  ierr = EPSDefaultFreeWork(eps);CHKERRQ(ierr);
280
  ierr = EPSFreeSolutionContiguous(eps);CHKERRQ(ierr);
6 dsic.upv.es!jroman 281
  PetscFunctionReturn(0);
282
}
283
 
284
EXTERN_C_BEGIN
285
#undef __FUNCT__  
286
#define __FUNCT__ "EPSCreate_ARPACK"
476 dsic.upv.es!antodo 287
PetscErrorCode EPSCreate_ARPACK(EPS eps)
6 dsic.upv.es!jroman 288
{
476 dsic.upv.es!antodo 289
  PetscErrorCode ierr;
290
  EPS_ARPACK     *arpack;
6 dsic.upv.es!jroman 291
 
292
  PetscFunctionBegin;
293
  ierr = PetscNew(EPS_ARPACK,&arpack);CHKERRQ(ierr);
294
  PetscLogObjectMemory(eps,sizeof(EPS_ARPACK));
295
  eps->data                      = (void *) arpack;
296
  eps->ops->solve                = EPSSolve_ARPACK;
503 dsic.upv.es!antodo 297
  eps->ops->setup                = EPSSetUp_ARPACK;
6 dsic.upv.es!jroman 298
  eps->ops->destroy              = EPSDestroy_ARPACK;
176 dsic.upv.es!antodo 299
  eps->ops->backtransform        = EPSBackTransform_ARPACK;
503 dsic.upv.es!antodo 300
  eps->ops->computevectors       = EPSComputeVectors_Default;
6 dsic.upv.es!jroman 301
  PetscFunctionReturn(0);
302
}
303
EXTERN_C_END