Subversion Repositories slepc-dev

Rev

Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
228 dsic.upv.es!jroman 1
static char help[] = "Illustrates the use of shell spectral transformations. "
161 dsic.upv.es!antodo 2
  "The problem to be solved is the same as ex1.c and"
6 dsic.upv.es!jroman 3
  "corresponds to the Laplacian operator in 1 dimension.\n\n"
4
  "The command line options are:\n\n"
5
  "  -n <n>, where <n> = number of grid subdivisions = matrix dimension.\n\n";
6
 
7
#include "slepceps.h"
8
 
9
/* Define context for user-provided spectral transformation */
10
typedef struct {
18 dsic.upv.es!jroman 11
  KSP        ksp;
6 dsic.upv.es!jroman 12
} SampleShellST;
13
 
14
/* Declare routines for user-provided spectral transformation */
15
extern int SampleShellSTCreate(SampleShellST**);
16
extern int SampleShellSTSetUp(SampleShellST*,ST);
17
extern int SampleShellSTApply(void*,Vec,Vec);
18
extern int SampleShellSTBackTransform(void*,PetscScalar*,PetscScalar*);
19
extern int SampleShellSTDestroy(SampleShellST*);
20
 
21
#undef __FUNCT__
22
#define __FUNCT__ "main"
23
int main( int argc, char **argv )
24
{
25
  Mat           A;               /* operator matrix */
26
  EPS           eps;             /* eigenproblem solver context */
27
  ST            st;              /* spectral transformation context */
28
  SampleShellST *shell;          /* user-defined spectral transform context */
29
  EPSType       type;
132 dsic.upv.es!antodo 30
  PetscReal     error, tol, re, im;
97 dsic.upv.es!antodo 31
  PetscScalar   kr, ki;
982 slepc 32
  int           nev, ierr, maxit, its, nconv;
33
  PetscInt      n=30, i, col[3], Istart, Iend, FirstBlock=0, LastBlock=0;
6 dsic.upv.es!jroman 34
  PetscScalar   value[3];
35
  PetscTruth    isShell;
36
 
37
  SlepcInitialize(&argc,&argv,(char*)0,help);
38
 
39
  ierr = PetscOptionsGetInt(PETSC_NULL,"-n",&n,PETSC_NULL);CHKERRQ(ierr);
40
  ierr = PetscPrintf(PETSC_COMM_WORLD,"\n1-D Laplacian Eigenproblem (shell-enabled), n=%d\n\n",n);
41
         CHKERRQ(ierr);
42
 
43
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
44
     Compute the operator matrix that defines the eigensystem, Ax=kx
45
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
46
 
828 dsic.upv.es!antodo 47
  ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr);
48
  ierr = MatSetSizes(A,PETSC_DECIDE,PETSC_DECIDE,n,n);CHKERRQ(ierr);
6 dsic.upv.es!jroman 49
  ierr = MatSetFromOptions(A);CHKERRQ(ierr);
50
 
51
  ierr = MatGetOwnershipRange(A,&Istart,&Iend);CHKERRQ(ierr);
52
  if (Istart==0) FirstBlock=PETSC_TRUE;
53
  if (Iend==n) LastBlock=PETSC_TRUE;
54
  value[0]=-1.0; value[1]=2.0; value[2]=-1.0;
55
  for( i=(FirstBlock? Istart+1: Istart); i<(LastBlock? Iend-1: Iend); i++ ) {
56
    col[0]=i-1; col[1]=i; col[2]=i+1;
57
    ierr = MatSetValues(A,1,&i,3,col,value,INSERT_VALUES);CHKERRQ(ierr);
58
  }
59
  if (LastBlock) {
60
    i=n-1; col[0]=n-2; col[1]=n-1;
61
    ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
62
  }
63
  if (FirstBlock) {
64
    i=0; col[0]=0; col[1]=1; value[0]=2.0; value[1]=-1.0;
65
    ierr = MatSetValues(A,1,&i,2,col,value,INSERT_VALUES);CHKERRQ(ierr);
66
  }
67
 
68
  ierr = MatAssemblyBegin(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
69
  ierr = MatAssemblyEnd(A,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
70
 
71
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
72
                Create the eigensolver and set various options
73
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
74
 
75
  /*
76
     Create eigensolver context
77
  */
78
  ierr = EPSCreate(PETSC_COMM_WORLD,&eps);CHKERRQ(ierr);
79
 
80
  /*
81
     Set operators. In this case, it is a standard eigenvalue problem
82
  */
83
  ierr = EPSSetOperators(eps,A,PETSC_NULL);CHKERRQ(ierr);
408 dsic.upv.es!antodo 84
  ierr = EPSSetProblemType(eps,EPS_HEP);CHKERRQ(ierr);
6 dsic.upv.es!jroman 85
 
86
  /*
87
     Set solver parameters at runtime
88
  */
89
  ierr = EPSSetFromOptions(eps);CHKERRQ(ierr);
90
 
91
  /*
92
     Initialize shell spectral transformation if selected by user
93
  */
94
  ierr = EPSGetST(eps,&st);CHKERRQ(ierr);
95
  ierr = PetscTypeCompare((PetscObject)st,STSHELL,&isShell);CHKERRQ(ierr);
96
  if (isShell) {
97
    /* (Optional) Create a context for the user-defined spectral tranform;
98
       this context can be defined to contain any application-specific data. */
99
    ierr = SampleShellSTCreate(&shell);CHKERRQ(ierr);
100
 
101
    /* (Required) Set the user-defined routine for applying the operator */
102
    ierr = STShellSetApply(st,SampleShellSTApply,(void*)shell);CHKERRQ(ierr);
103
 
104
    /* (Optional) Set the user-defined routine for back-transformation */
105
    ierr = STShellSetBackTransform(st,SampleShellSTBackTransform);CHKERRQ(ierr);
106
 
107
    /* (Optional) Set a name for the transformation, used for STView() */
108
    ierr = STShellSetName(st,"MyTransformation");CHKERRQ(ierr);
109
 
110
    /* (Optional) Do any setup required for the new transformation */
111
    ierr = SampleShellSTSetUp(shell,st);CHKERRQ(ierr);
112
  }
113
 
114
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
115
                      Solve the eigensystem
116
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
117
 
78 dsic.upv.es!antodo 118
  ierr = EPSSolve(eps);CHKERRQ(ierr);
119
  ierr = EPSGetIterationNumber(eps, &its);CHKERRQ(ierr);
6 dsic.upv.es!jroman 120
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of iterations of the method: %d\n",its);
121
         CHKERRQ(ierr);
122
 
123
  /*
124
     Optional: Get some information from the solver and display it
125
  */
126
  ierr = EPSGetType(eps,&type);CHKERRQ(ierr);
127
  ierr = PetscPrintf(PETSC_COMM_WORLD," Solution method: %s\n\n",type);CHKERRQ(ierr);
128
  ierr = EPSGetDimensions(eps,&nev,PETSC_NULL);CHKERRQ(ierr);
129
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of requested eigenvalues: %d\n",nev);
130
         CHKERRQ(ierr);
131
  ierr = EPSGetTolerances(eps,&tol,&maxit);CHKERRQ(ierr);
132
  ierr = PetscPrintf(PETSC_COMM_WORLD," Stopping condition: tol=%.4g, maxit=%d\n",tol,maxit);
133
         CHKERRQ(ierr);
134
 
135
  /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
136
                    Display solution and clean up
137
     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
138
 
139
  /*
140
     Get number of converged approximate eigenpairs
141
  */
132 dsic.upv.es!antodo 142
  ierr = EPSGetConverged(eps,&nconv);CHKERRQ(ierr);
143
  ierr = PetscPrintf(PETSC_COMM_WORLD," Number of converged eigenpairs: %d\n\n",nconv);
6 dsic.upv.es!jroman 144
         CHKERRQ(ierr);
145
 
132 dsic.upv.es!antodo 146
  if (nconv>0) {
6 dsic.upv.es!jroman 147
    /*
148
       Display eigenvalues and relative errors
149
    */
150
    ierr = PetscPrintf(PETSC_COMM_WORLD,
97 dsic.upv.es!antodo 151
         "           k          ||Ax-kx||/||kx||\n"
152
         "   ----------------- ------------------\n" );CHKERRQ(ierr);
153
 
132 dsic.upv.es!antodo 154
    for( i=0; i<nconv; i++ ) {
97 dsic.upv.es!antodo 155
      /*
156
        Get converged eigenpairs: i-th eigenvalue is stored in kr (real part) and
157
        ki (imaginary part)
158
      */
159
      ierr = EPSGetEigenpair(eps,i,&kr,&ki,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
160
      /*
161
         Compute the relative error associated to each eigenpair
162
      */
163
      ierr = EPSComputeRelativeError(eps,i,&error);CHKERRQ(ierr);
164
 
165
#ifdef PETSC_USE_COMPLEX
132 dsic.upv.es!antodo 166
      re = PetscRealPart(kr);
167
      im = PetscImaginaryPart(kr);
168
#else
169
      re = kr;
170
      im = ki;
97 dsic.upv.es!antodo 171
#endif
132 dsic.upv.es!antodo 172
      if (im!=0.0) {
173
        ierr = PetscPrintf(PETSC_COMM_WORLD," %9f%+9f j %12f\n",re,im,error);CHKERRQ(ierr);
97 dsic.upv.es!antodo 174
      } else {
132 dsic.upv.es!antodo 175
        ierr = PetscPrintf(PETSC_COMM_WORLD,"   %12f       %12f\n",re,error);CHKERRQ(ierr);
97 dsic.upv.es!antodo 176
      }
6 dsic.upv.es!jroman 177
    }
178
    ierr = PetscPrintf(PETSC_COMM_WORLD,"\n" );CHKERRQ(ierr);
179
  }
180
 
181
  /*
182
     Free work space
183
  */
184
  if (isShell) {
185
    ierr = SampleShellSTDestroy(shell);CHKERRQ(ierr);
186
  }
187
  ierr = EPSDestroy(eps);CHKERRQ(ierr);
188
  ierr = MatDestroy(A);CHKERRQ(ierr);
189
  ierr = SlepcFinalize();CHKERRQ(ierr);
190
  return 0;
191
}
192
 
193
/***********************************************************************/
194
/*          Routines for a user-defined shell transformation           */
195
/***********************************************************************/
196
 
197
#undef __FUNCT__  
198
#define __FUNCT__ "SampleShellSTCreate"
199
/*
200
   SampleShellSTCreate - This routine creates a user-defined
201
   spectral transformation context.
202
 
203
   Output Parameter:
204
.  shell - user-defined spectral transformation context
205
*/
206
int SampleShellSTCreate(SampleShellST **shell)
207
{
208
  SampleShellST *newctx;
209
  int           ierr;
210
 
211
  ierr   = PetscNew(SampleShellST,&newctx);CHKERRQ(ierr);
18 dsic.upv.es!jroman 212
  ierr   = KSPCreate(PETSC_COMM_WORLD,&newctx->ksp);CHKERRQ(ierr);
213
  ierr   = KSPAppendOptionsPrefix(newctx->ksp,"st_"); CHKERRQ(ierr);
6 dsic.upv.es!jroman 214
  *shell = newctx;
215
  return 0;
216
}
217
/* ------------------------------------------------------------------- */
218
#undef __FUNCT__  
219
#define __FUNCT__ "SampleShellSTSetUp"
220
/*
221
   SampleShellSTSetUp - This routine sets up a user-defined
222
   spectral transformation context.  
223
 
224
   Input Parameters:
225
.  shell - user-defined spectral transformation context
226
.  st    - spectral transformation context containing the operator matrices
227
 
228
   Output Parameter:
229
.  shell - fully set up user-defined transformation context
230
 
231
   Notes:
232
   In this example, the user-defined transformation is simply OP=A^-1.
18 dsic.upv.es!jroman 233
   Therefore, the eigenpairs converge in reversed order. The KSP object
6 dsic.upv.es!jroman 234
   used for the solution of linear systems with A is handled via the
235
   user-defined context SampleShellST.
236
*/
237
int SampleShellSTSetUp(SampleShellST *shell,ST st)
238
{
239
  Mat  A,B;
240
  int  ierr;
241
 
242
  ierr = STGetOperators( st, &A, &B ); CHKERRQ(ierr);
243
  if (B) { SETERRQ(0,"Warning: This transformation is not intended for generalized problems"); }
18 dsic.upv.es!jroman 244
  ierr = KSPSetOperators(shell->ksp,A,A,DIFFERENT_NONZERO_PATTERN);CHKERRQ(ierr);
245
  ierr = KSPSetFromOptions(shell->ksp);CHKERRQ(ierr);
6 dsic.upv.es!jroman 246
 
247
  return 0;
248
}
249
/* ------------------------------------------------------------------- */
250
#undef __FUNCT__  
251
#define __FUNCT__ "SampleShellSTApply"
252
/*
253
   SampleShellSTApply - This routine demonstrates the use of a
254
   user-provided spectral transformation.
255
 
256
   Input Parameters:
257
.  ctx - optional user-defined context, as set by STShellSetApply()
258
.  x - input vector
259
 
260
   Output Parameter:
261
.  y - output vector
262
 
263
   Notes:
264
   The transformation implemented in this code is just OP=A^-1 and
265
   therefore it is of little use, merely as an example of working with
266
   a STSHELL.
267
*/
268
int SampleShellSTApply(void *ctx,Vec x,Vec y)
269
{
270
  SampleShellST *shell = (SampleShellST*)ctx;
271
  int           ierr;
272
 
239 dsic.upv.es!antodo 273
  ierr = KSPSolve(shell->ksp,x,y);CHKERRQ(ierr);
6 dsic.upv.es!jroman 274
 
275
  return 0;
276
}
277
/* ------------------------------------------------------------------- */
278
#undef __FUNCT__  
279
#define __FUNCT__ "SampleShellSTBackTransform"
280
/*
281
   SampleShellSTBackTransform - This routine demonstrates the use of a
282
   user-provided spectral transformation.
283
 
284
   Input Parameters:
285
.  ctx  - optional user-defined context, as set by STShellSetApply()
286
.  eigr - pointer to real part of eigenvalues
287
.  eigi - pointer to imaginary part of eigenvalues
288
 
289
   Output Parameters:
290
.  eigr - modified real part of eigenvalues
291
.  eigi - modified imaginary part of eigenvalues
292
 
293
   Notes:
294
   This code implements the back transformation of eigenvalues in
295
   order to retrieve the eigenvalues of the original problem. In this
296
   example, simply set k_i = 1/k_i.
297
*/
298
int SampleShellSTBackTransform(void *ctx,PetscScalar *eigr,PetscScalar *eigi)
299
{
300
  *eigr = 1.0 / *eigr;
301
 
302
  return 0;
303
}
304
/* ------------------------------------------------------------------- */
305
#undef __FUNCT__  
306
#define __FUNCT__ "SampleShellSTDestroy"
307
/*
308
   SampleShellSTDestroy - This routine destroys a user-defined
309
   spectral transformation context.
310
 
311
   Input Parameter:
312
.  shell - user-defined spectral transformation context
313
*/
314
int SampleShellSTDestroy(SampleShellST *shell)
315
{
316
  int ierr;
317
 
18 dsic.upv.es!jroman 318
  ierr = KSPDestroy(shell->ksp);CHKERRQ(ierr);
6 dsic.upv.es!jroman 319
  ierr = PetscFree(shell);CHKERRQ(ierr);
320
 
321
  return 0;
322
}
323
 
324