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
6 dsic.upv.es!jroman 1
/*
2
   This provides a simple shell interface for programmers to
3
   create their own spectral transformations without writing much
4
   interface code.
1376 slepc 5
 
6
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
7
      SLEPc - Scalable Library for Eigenvalue Problem Computations
8
      Copyright (c) 2002-2007, Universidad Politecnica de Valencia, Spain
9
 
10
      This file is part of SLEPc. See the README file for conditions of use
11
      and additional information.
12
   - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
6 dsic.upv.es!jroman 13
*/
14
 
15
#include "src/st/stimpl.h"        /*I "slepcst.h" I*/
16
#include "slepceps.h"
17
 
1024 slepc 18
EXTERN_C_BEGIN
6 dsic.upv.es!jroman 19
typedef struct {
1024 slepc 20
  void           *ctx;                       /* user provided context */
21
  PetscErrorCode (*apply)(void *,Vec,Vec);
22
  PetscErrorCode (*applytrans)(void *,Vec,Vec);
23
  PetscErrorCode (*backtr)(void *,PetscScalar*,PetscScalar*);
24
  char           *name;
6 dsic.upv.es!jroman 25
} ST_Shell;
1024 slepc 26
EXTERN_C_END
6 dsic.upv.es!jroman 27
 
28
#undef __FUNCT__  
1024 slepc 29
#define __FUNCT__ "STShellGetContext"
1027 slepc 30
/*@C
1024 slepc 31
    STShellGetContext - Returns the user-provided context associated with a shell ST
32
 
33
    Not Collective
34
 
35
    Input Parameter:
36
.   st - spectral transformation context
37
 
38
    Output Parameter:
39
.   ctx - the user provided context
40
 
41
    Level: advanced
42
 
43
    Notes:
44
    This routine is intended for use within various shell routines
45
 
46
.seealso: STShellSetContext()
47
@*/
48
PetscErrorCode STShellGetContext(ST st,void **ctx)
49
{
50
  PetscErrorCode ierr;
51
  PetscTruth     flg;
52
 
53
  PetscFunctionBegin;
54
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
55
  PetscValidPointer(ctx,2);
56
  ierr = PetscTypeCompare((PetscObject)st,STSHELL,&flg);CHKERRQ(ierr);
57
  if (!flg) *ctx = 0;
58
  else      *ctx = ((ST_Shell*)(st->data))->ctx;
59
  PetscFunctionReturn(0);
60
}
61
 
62
#undef __FUNCT__  
63
#define __FUNCT__ "STShellSetContext"
64
/*@C
65
    STShellSetContext - sets the context for a shell ST
66
 
67
   Collective on ST
68
 
69
    Input Parameters:
70
+   st - the shell ST
71
-   ctx - the context
72
 
73
   Level: advanced
74
 
75
   Fortran Notes: The context can only be an integer or a PetscObject;
76
      unfortunately it cannot be a Fortran array or derived type.
77
 
78
.seealso: STShellGetContext()
79
@*/
80
PetscErrorCode STShellSetContext(ST st,void *ctx)
81
{
82
  ST_Shell      *shell = (ST_Shell*)st->data;
83
  PetscErrorCode ierr;
84
  PetscTruth     flg;
85
 
86
  PetscFunctionBegin;
87
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
88
  ierr = PetscTypeCompare((PetscObject)st,STSHELL,&flg);CHKERRQ(ierr);
89
  if (flg) {
90
    shell->ctx = ctx;
91
  }
92
  PetscFunctionReturn(0);
93
}
94
 
95
#undef __FUNCT__  
6 dsic.upv.es!jroman 96
#define __FUNCT__ "STApply_Shell"
476 dsic.upv.es!antodo 97
PetscErrorCode STApply_Shell(ST st,Vec x,Vec y)
6 dsic.upv.es!jroman 98
{
476 dsic.upv.es!antodo 99
  PetscErrorCode ierr;
1024 slepc 100
  ST_Shell       *shell = (ST_Shell*)st->data;
6 dsic.upv.es!jroman 101
 
102
  PetscFunctionBegin;
1024 slepc 103
  if (!shell->apply) SETERRQ(PETSC_ERR_USER,"No apply() routine provided to Shell ST");
104
  PetscStackPush("PCSHELL user function");
105
  CHKMEMQ;
106
  ierr  = (*shell->apply)(shell->ctx,x,y);CHKERRQ(ierr);
107
  CHKMEMQ;
108
  PetscStackPop;
6 dsic.upv.es!jroman 109
  PetscFunctionReturn(0);
110
}
111
 
112
#undef __FUNCT__  
780 dsic.upv.es!jroman 113
#define __FUNCT__ "STApplyTranspose_Shell"
114
PetscErrorCode STApplyTranspose_Shell(ST st,Vec x,Vec y)
115
{
116
  PetscErrorCode ierr;
1024 slepc 117
  ST_Shell       *shell = (ST_Shell*)st->data;
780 dsic.upv.es!jroman 118
 
119
  PetscFunctionBegin;
1024 slepc 120
  if (!shell->applytrans) SETERRQ(PETSC_ERR_USER,"No applytranspose() routine provided to Shell ST");
121
  ierr  = (*shell->applytrans)(shell->ctx,x,y);CHKERRQ(ierr);
780 dsic.upv.es!jroman 122
  PetscFunctionReturn(0);
123
}
124
 
125
#undef __FUNCT__  
6 dsic.upv.es!jroman 126
#define __FUNCT__ "STBackTransform_Shell"
476 dsic.upv.es!antodo 127
PetscErrorCode STBackTransform_Shell(ST st,PetscScalar *eigr,PetscScalar *eigi)
6 dsic.upv.es!jroman 128
{
476 dsic.upv.es!antodo 129
  PetscErrorCode ierr;
1024 slepc 130
  ST_Shell       *shell = (ST_Shell*)st->data;
6 dsic.upv.es!jroman 131
 
132
  PetscFunctionBegin;
133
  if (shell->backtr) {
1024 slepc 134
    ierr  = (*shell->backtr)(shell->ctx,eigr,eigi);CHKERRQ(ierr);
6 dsic.upv.es!jroman 135
  }
136
  PetscFunctionReturn(0);
137
}
138
 
139
#undef __FUNCT__  
140
#define __FUNCT__ "STDestroy_Shell"
476 dsic.upv.es!antodo 141
PetscErrorCode STDestroy_Shell(ST st)
6 dsic.upv.es!jroman 142
{
476 dsic.upv.es!antodo 143
  PetscErrorCode ierr;
1024 slepc 144
  ST_Shell       *shell = (ST_Shell*)st->data;
6 dsic.upv.es!jroman 145
 
146
  PetscFunctionBegin;
1040 slepc 147
  ierr = PetscFree(shell->name);CHKERRQ(ierr);
6 dsic.upv.es!jroman 148
  ierr = PetscFree(shell);CHKERRQ(ierr);
149
  PetscFunctionReturn(0);
150
}
151
 
152
#undef __FUNCT__  
153
#define __FUNCT__ "STView_Shell"
476 dsic.upv.es!antodo 154
PetscErrorCode STView_Shell(ST st,PetscViewer viewer)
6 dsic.upv.es!jroman 155
{
476 dsic.upv.es!antodo 156
  PetscErrorCode ierr;
157
  ST_Shell       *ctx = (ST_Shell*)st->data;
158
  PetscTruth     isascii;
6 dsic.upv.es!jroman 159
 
160
  PetscFunctionBegin;
161
  ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);CHKERRQ(ierr);
162
  if (isascii) {
163
    if (ctx->name) {ierr = PetscViewerASCIIPrintf(viewer,"  ST Shell: %s\n",ctx->name);CHKERRQ(ierr);}
164
    else           {ierr = PetscViewerASCIIPrintf(viewer,"  ST Shell: no name\n");CHKERRQ(ierr);}
165
  } else {
166
    SETERRQ1(1,"Viewer type %s not supported for STShell",((PetscObject)viewer)->type_name);
167
  }
168
  PetscFunctionReturn(0);
169
}
170
 
171
EXTERN_C_BEGIN
172
#undef __FUNCT__  
173
#define __FUNCT__ "STShellSetApply_Shell"
1024 slepc 174
PetscErrorCode STShellSetApply_Shell(ST st,PetscErrorCode (*apply)(void*,Vec,Vec))
6 dsic.upv.es!jroman 175
{
1024 slepc 176
  ST_Shell *shell = (ST_Shell*)st->data;
6 dsic.upv.es!jroman 177
 
178
  PetscFunctionBegin;
179
  shell->apply = apply;
180
  PetscFunctionReturn(0);
181
}
182
EXTERN_C_END
183
 
184
EXTERN_C_BEGIN
185
#undef __FUNCT__  
780 dsic.upv.es!jroman 186
#define __FUNCT__ "STShellSetApplyTranspose_Shell"
1024 slepc 187
PetscErrorCode STShellSetApplyTranspose_Shell(ST st,PetscErrorCode (*applytrans)(void*,Vec,Vec))
780 dsic.upv.es!jroman 188
{
1024 slepc 189
  ST_Shell *shell = (ST_Shell*)st->data;
780 dsic.upv.es!jroman 190
 
191
  PetscFunctionBegin;
192
  shell->applytrans = applytrans;
193
  PetscFunctionReturn(0);
194
}
195
EXTERN_C_END
196
 
197
EXTERN_C_BEGIN
198
#undef __FUNCT__  
6 dsic.upv.es!jroman 199
#define __FUNCT__ "STShellSetBackTransform_Shell"
1024 slepc 200
PetscErrorCode STShellSetBackTransform_Shell(ST st,PetscErrorCode (*backtr)(void*,PetscScalar*,PetscScalar*))
6 dsic.upv.es!jroman 201
{
476 dsic.upv.es!antodo 202
  ST_Shell *shell = (ST_Shell *) st->data;
6 dsic.upv.es!jroman 203
 
204
  PetscFunctionBegin;
205
  shell->backtr = backtr;
206
  PetscFunctionReturn(0);
207
}
208
EXTERN_C_END
209
 
210
EXTERN_C_BEGIN
211
#undef __FUNCT__  
212
#define __FUNCT__ "STShellSetName_Shell"
1024 slepc 213
PetscErrorCode STShellSetName_Shell(ST st,const char name[])
6 dsic.upv.es!jroman 214
{
1024 slepc 215
  ST_Shell *shell = (ST_Shell*)st->data;
216
  PetscErrorCode ierr;
6 dsic.upv.es!jroman 217
 
218
  PetscFunctionBegin;
1024 slepc 219
  ierr = PetscStrfree(shell->name);CHKERRQ(ierr);    
220
  ierr = PetscStrallocpy(name,&shell->name);CHKERRQ(ierr);
6 dsic.upv.es!jroman 221
  PetscFunctionReturn(0);
222
}
223
EXTERN_C_END
224
 
225
EXTERN_C_BEGIN
226
#undef __FUNCT__  
227
#define __FUNCT__ "STShellGetName_Shell"
1024 slepc 228
PetscErrorCode STShellGetName_Shell(ST st,char *name[])
6 dsic.upv.es!jroman 229
{
1024 slepc 230
  ST_Shell *shell = (ST_Shell*)st->data;
6 dsic.upv.es!jroman 231
 
232
  PetscFunctionBegin;
233
  *name  = shell->name;
234
  PetscFunctionReturn(0);
235
}
236
EXTERN_C_END
237
 
238
#undef __FUNCT__  
239
#define __FUNCT__ "STShellSetApply"
240
/*@C
241
   STShellSetApply - Sets routine to use as the application of the
242
   operator to a vector in the user-defined spectral transformation.
243
 
244
   Collective on ST
245
 
246
   Input Parameters:
247
+  st    - the spectral transformation context
1024 slepc 248
-  apply - the application-provided transformation routine
6 dsic.upv.es!jroman 249
 
250
   Calling sequence of apply:
251
.vb
1024 slepc 252
   PetscErrorCode apply (void *ptr,Vec xin,Vec xout)
6 dsic.upv.es!jroman 253
.ve
254
 
255
+  ptr  - the application context
256
.  xin  - input vector
257
-  xout - output vector
258
 
259
   Level: developer
260
 
780 dsic.upv.es!jroman 261
.seealso: STShellSetBackTransform(), STShellSetApplyTranspose()
6 dsic.upv.es!jroman 262
@*/
1024 slepc 263
PetscErrorCode STShellSetApply(ST st,PetscErrorCode (*apply)(void*,Vec,Vec))
6 dsic.upv.es!jroman 264
{
1024 slepc 265
  PetscErrorCode ierr, (*f)(ST,PetscErrorCode (*)(void*,Vec,Vec));
6 dsic.upv.es!jroman 266
 
267
  PetscFunctionBegin;
25 dsic.upv.es!jroman 268
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
6 dsic.upv.es!jroman 269
  ierr = PetscObjectQueryFunction((PetscObject)st,"STShellSetApply_C",(void (**)(void))&f);CHKERRQ(ierr);
270
  if (f) {
1024 slepc 271
    ierr = (*f)(st,apply);CHKERRQ(ierr);
6 dsic.upv.es!jroman 272
  }
273
  PetscFunctionReturn(0);
274
}
275
 
276
#undef __FUNCT__  
780 dsic.upv.es!jroman 277
#define __FUNCT__ "STShellSetApplyTranspose"
278
/*@C
279
   STShellSetApplyTranspose - Sets routine to use as the application of the
280
   transposed operator to a vector in the user-defined spectral transformation.
281
 
282
   Collective on ST
283
 
284
   Input Parameters:
285
+  st    - the spectral transformation context
1024 slepc 286
-  applytrans - the application-provided transformation routine
780 dsic.upv.es!jroman 287
 
288
   Calling sequence of apply:
289
.vb
1024 slepc 290
   PetscErrorCode applytrans (void *ptr,Vec xin,Vec xout)
780 dsic.upv.es!jroman 291
.ve
292
 
293
+  ptr  - the application context
294
.  xin  - input vector
295
-  xout - output vector
296
 
297
   Level: developer
298
 
299
.seealso: STShellSetApply(), STShellSetBackTransform()
300
@*/
1024 slepc 301
PetscErrorCode STShellSetApplyTranspose(ST st,PetscErrorCode (*applytrans)(void*,Vec,Vec))
780 dsic.upv.es!jroman 302
{
1024 slepc 303
  PetscErrorCode ierr, (*f)(ST,PetscErrorCode (*)(void*,Vec,Vec));
780 dsic.upv.es!jroman 304
 
305
  PetscFunctionBegin;
306
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
307
  ierr = PetscObjectQueryFunction((PetscObject)st,"STShellSetApplyTranspose_C",(void (**)(void))&f);CHKERRQ(ierr);
308
  if (f) {
1024 slepc 309
    ierr = (*f)(st,applytrans);CHKERRQ(ierr);
780 dsic.upv.es!jroman 310
  }
311
  PetscFunctionReturn(0);
312
}
313
 
314
#undef __FUNCT__  
6 dsic.upv.es!jroman 315
#define __FUNCT__ "STShellSetBackTransform"
316
/*@C
317
   STShellSetBackTransform - Sets the routine to be called after the
318
   eigensolution process has finished in order to transform back the
319
   computed eigenvalues.
320
 
321
   Collective on ST
322
 
323
   Input Parameters:
324
+  st     - the spectral transformation context
1024 slepc 325
-  backtr - the application-provided backtransform routine
6 dsic.upv.es!jroman 326
 
327
   Calling sequence of backtr:
328
.vb
1024 slepc 329
   PetscErrorCode backtr (void *ptr,PetscScalar *eigr,PetscScalar *eigi)
6 dsic.upv.es!jroman 330
.ve
331
 
1024 slepc 332
+  ptr  - the application context
6 dsic.upv.es!jroman 333
.  eigr - pointer ot the real part of the eigenvalue to transform back
1024 slepc 334
-  eigi - pointer ot the imaginary part
6 dsic.upv.es!jroman 335
 
336
   Level: developer
337
 
780 dsic.upv.es!jroman 338
.seealso: STShellSetApply(), STShellSetApplyTranspose()
6 dsic.upv.es!jroman 339
@*/
1024 slepc 340
PetscErrorCode STShellSetBackTransform(ST st,PetscErrorCode (*backtr)(void*,PetscScalar*,PetscScalar*))
6 dsic.upv.es!jroman 341
{
1024 slepc 342
  PetscErrorCode ierr, (*f)(ST,PetscErrorCode (*)(void*,PetscScalar*,PetscScalar*));
6 dsic.upv.es!jroman 343
 
344
  PetscFunctionBegin;
25 dsic.upv.es!jroman 345
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
6 dsic.upv.es!jroman 346
  ierr = PetscObjectQueryFunction((PetscObject)st,"STShellSetBackTransform_C",(void (**)(void))&f);CHKERRQ(ierr);
347
  if (f) {
348
    ierr = (*f)(st,(int (*)(void*,PetscScalar*,PetscScalar*))backtr);CHKERRQ(ierr);
349
  }
350
  PetscFunctionReturn(0);
351
}
352
 
353
#undef __FUNCT__  
354
#define __FUNCT__ "STShellSetName"
355
/*@C
356
   STShellSetName - Sets an optional name to associate with a shell
357
   spectral transformation.
358
 
359
   Not Collective
360
 
361
   Input Parameters:
362
+  st   - the spectral transformation context
363
-  name - character string describing the shell spectral transformation
364
 
365
   Level: developer
366
 
367
.seealso: STShellGetName()
368
@*/
1024 slepc 369
PetscErrorCode STShellSetName(ST st,const char name[])
6 dsic.upv.es!jroman 370
{
1024 slepc 371
  PetscErrorCode ierr, (*f)(ST,const char []);
6 dsic.upv.es!jroman 372
 
373
  PetscFunctionBegin;
25 dsic.upv.es!jroman 374
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
6 dsic.upv.es!jroman 375
  ierr = PetscObjectQueryFunction((PetscObject)st,"STShellSetName_C",(void (**)(void))&f);CHKERRQ(ierr);
376
  if (f) {
377
    ierr = (*f)(st,name);CHKERRQ(ierr);
378
  }
379
  PetscFunctionReturn(0);
380
}
381
 
382
#undef __FUNCT__  
383
#define __FUNCT__ "STShellGetName"
384
/*@C
385
   STShellGetName - Gets an optional name that the user has set for a shell
386
   spectral transformation.
387
 
388
   Not Collective
389
 
390
   Input Parameter:
391
.  st - the spectral transformation context
392
 
393
   Output Parameter:
1024 slepc 394
.  name - character string describing the shell spectral transformation
395
          (you should not free this)
6 dsic.upv.es!jroman 396
 
397
   Level: developer
398
 
399
.seealso: STShellSetName()
400
@*/
1024 slepc 401
PetscErrorCode STShellGetName(ST st,char *name[])
6 dsic.upv.es!jroman 402
{
1024 slepc 403
  PetscErrorCode ierr, (*f)(ST,char *[]);
6 dsic.upv.es!jroman 404
 
405
  PetscFunctionBegin;
25 dsic.upv.es!jroman 406
  PetscValidHeaderSpecific(st,ST_COOKIE,1);
6 dsic.upv.es!jroman 407
  ierr = PetscObjectQueryFunction((PetscObject)st,"STShellGetName_C",(void (**)(void))&f);CHKERRQ(ierr);
408
  if (f) {
409
    ierr = (*f)(st,name);CHKERRQ(ierr);
410
  } else {
1024 slepc 411
    SETERRQ(PETSC_ERR_ARG_WRONG,"Not shell spectral transformation, cannot get name");
6 dsic.upv.es!jroman 412
  }
413
  PetscFunctionReturn(0);
414
}
415
 
1024 slepc 416
/*MC
417
   STSHELL - Creates a new spectral transformation class.
6 dsic.upv.es!jroman 418
          This is intended to provide a simple class to use with EPS.
419
          You should not use this if you plan to make a complete class.
420
 
1024 slepc 421
  Level: advanced
422
 
6 dsic.upv.es!jroman 423
  Usage:
1024 slepc 424
$             PetscErrorCode (*apply)(void*,Vec,Vec);
425
$             PetscErrorCode (*applytrans)(void*,Vec,Vec);
426
$             PetscErrorCode (*backtr)(void*,PetscScalar*,PetscScalar*);
6 dsic.upv.es!jroman 427
$             STCreate(comm,&st);
428
$             STSetType(st,STSHELL);
1024 slepc 429
$             STShellSetApply(st,apply);
430
$             STShellSetApplyTranspose(st,applytrans);
6 dsic.upv.es!jroman 431
$             STShellSetBackTransform(st,backtr);    (optional)
432
 
1024 slepc 433
M*/
434
 
6 dsic.upv.es!jroman 435
EXTERN_C_BEGIN
436
#undef __FUNCT__  
437
#define __FUNCT__ "STCreate_Shell"
476 dsic.upv.es!antodo 438
PetscErrorCode STCreate_Shell(ST st)
6 dsic.upv.es!jroman 439
{
476 dsic.upv.es!antodo 440
  PetscErrorCode ierr;
441
  ST_Shell       *shell;
6 dsic.upv.es!jroman 442
 
443
  PetscFunctionBegin;
444
  st->ops->destroy = STDestroy_Shell;
1024 slepc 445
  ierr = PetscNew(ST_Shell,&shell);CHKERRQ(ierr);
446
  ierr = PetscLogObjectMemory(st,sizeof(ST_Shell));CHKERRQ(ierr);
6 dsic.upv.es!jroman 447
 
448
  st->data           = (void *) shell;
1422 slepc 449
  ((PetscObject)st)->name           = 0;
6 dsic.upv.es!jroman 450
 
451
  st->ops->apply     = STApply_Shell;
780 dsic.upv.es!jroman 452
  st->ops->applytrans= STApplyTranspose_Shell;
6 dsic.upv.es!jroman 453
  st->ops->backtr    = STBackTransform_Shell;
454
  st->ops->view      = STView_Shell;
455
 
456
  shell->apply       = 0;
1024 slepc 457
  shell->applytrans  = 0;
458
  shell->backtr      = 0;
6 dsic.upv.es!jroman 459
  shell->name        = 0;
460
  shell->ctx         = 0;
461
 
462
  ierr = PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetApply_C","STShellSetApply_Shell",
463
                    STShellSetApply_Shell);CHKERRQ(ierr);
780 dsic.upv.es!jroman 464
  ierr = PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetApplyTranspose_C","STShellSetApplyTranspose_Shell",
465
                    STShellSetApplyTranspose_Shell);CHKERRQ(ierr);
6 dsic.upv.es!jroman 466
  ierr = PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetBackTransform_C","STShellSetBackTransform_Shell",
467
                    STShellSetBackTransform_Shell);CHKERRQ(ierr);
468
  ierr = PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetName_C","STShellSetName_Shell",
469
                    STShellSetName_Shell);CHKERRQ(ierr);
470
  ierr = PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellGetName_C","STShellGetName_Shell",
471
                    STShellGetName_Shell);CHKERRQ(ierr);
472
 
473
  PetscFunctionReturn(0);
474
}
475
EXTERN_C_END
476