Actual source code: shell.c

  2: /*
  3:    This provides a simple shell interface for programmers to 
  4:    create their own spectral transformations without writing much 
  5:    interface code.
  6: */

 8:  #include src/st/stimpl.h
 9:  #include slepceps.h

 11: typedef struct {
 12:   void *ctx;                            /* user provided context */
 13:   int  (*apply)(void *,Vec,Vec);
 14:   int  (*backtr)(void *,PetscScalar*,PetscScalar*);
 15:   char *name;
 16: } ST_Shell;

 20: PetscErrorCode STApply_Shell(ST st,Vec x,Vec y)
 21: {
 22:   PetscErrorCode ierr;
 23:   ST_Shell       *shell = (ST_Shell *) st->data;

 26:   if (!shell->apply) SETERRQ(1,"No apply() routine provided to Shell ST");
 27:   (*shell->apply)(shell->ctx,x,y);
 28:   return(0);
 29: }

 33: PetscErrorCode STBackTransform_Shell(ST st,PetscScalar *eigr,PetscScalar *eigi)
 34: {
 35:   PetscErrorCode ierr;
 36:   ST_Shell       *shell = (ST_Shell *) st->data;

 39:   if (shell->backtr) {
 40:     (*shell->backtr)(shell->ctx,eigr,eigi);
 41:   }
 42:   return(0);
 43: }

 47: PetscErrorCode STDestroy_Shell(ST st)
 48: {
 49:   PetscErrorCode ierr;
 50:   ST_Shell       *shell = (ST_Shell *) st->data;

 53:   PetscFree(shell);
 54:   return(0);
 55: }

 59: PetscErrorCode STView_Shell(ST st,PetscViewer viewer)
 60: {
 61:   PetscErrorCode ierr;
 62:   ST_Shell       *ctx = (ST_Shell*)st->data;
 63:   PetscTruth     isascii;

 66:   PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&isascii);
 67:   if (isascii) {
 68:     if (ctx->name) {PetscViewerASCIIPrintf(viewer,"  ST Shell: %s\n",ctx->name);}
 69:     else           {PetscViewerASCIIPrintf(viewer,"  ST Shell: no name\n");}
 70:   } else {
 71:     SETERRQ1(1,"Viewer type %s not supported for STShell",((PetscObject)viewer)->type_name);
 72:   }
 73:   return(0);
 74: }

 76: EXTERN_C_BEGIN
 79: PetscErrorCode STShellSetApply_Shell(ST st, int (*apply)(void*,Vec,Vec),void *ptr)
 80: {
 81:   ST_Shell *shell = (ST_Shell *) st->data;

 84:   shell->apply = apply;
 85:   shell->ctx   = ptr;
 86:   return(0);
 87: }
 88: EXTERN_C_END

 90: EXTERN_C_BEGIN
 93: PetscErrorCode STShellSetBackTransform_Shell(ST st, int (*backtr)(void*,PetscScalar*,PetscScalar*))
 94: {
 95:   ST_Shell *shell = (ST_Shell *) st->data;

 98:   shell->backtr = backtr;
 99:   return(0);
100: }
101: EXTERN_C_END

103: EXTERN_C_BEGIN
106: PetscErrorCode STShellSetName_Shell(ST st,char *name)
107: {
108:   ST_Shell *shell = (ST_Shell *) st->data;

111:   shell->name = name;
112:   return(0);
113: }
114: EXTERN_C_END

116: EXTERN_C_BEGIN
119: PetscErrorCode STShellGetName_Shell(ST st,char **name)
120: {
121:   ST_Shell *shell = (ST_Shell *) st->data;

124:   *name  = shell->name;
125:   return(0);
126: }
127: EXTERN_C_END

131: /*@C
132:    STShellSetApply - Sets routine to use as the application of the 
133:    operator to a vector in the user-defined spectral transformation.

135:    Collective on ST

137:    Input Parameters:
138: +  st    - the spectral transformation context
139: .  apply - the application-provided transformation routine
140: -  ptr   - pointer to data needed by this routine

142:    Calling sequence of apply:
143: .vb
144:    int apply (void *ptr,Vec xin,Vec xout)
145: .ve

147: +  ptr  - the application context
148: .  xin  - input vector
149: -  xout - output vector

151:    Level: developer

153: .seealso: STShellSetBackTransform()
154: @*/
155: PetscErrorCode STShellSetApply(ST st, int (*apply)(void*,Vec,Vec),void *ptr)
156: {
157:   PetscErrorCode ierr, (*f)(ST,int (*)(void*,Vec,Vec),void *);

161:   PetscObjectQueryFunction((PetscObject)st,"STShellSetApply_C",(void (**)(void))&f);
162:   if (f) {
163:     (*f)(st,apply,ptr);
164:   }
165:   return(0);
166: }

170: /*@C
171:    STShellSetBackTransform - Sets the routine to be called after the 
172:    eigensolution process has finished in order to transform back the
173:    computed eigenvalues.

175:    Collective on ST

177:    Input Parameters:
178: +  st     - the spectral transformation context
179: .  backtr - the application-provided routine

181:    Calling sequence of backtr:
182: .vb
183:    int backtr (void *ptr, PetscScalar *eigr, PetscScalar *eigi)
184: .ve

186: .  ptr  - the application context
187: .  eigr - pointer ot the real part of the eigenvalue to transform back
188: .  eigi - pointer ot the imaginary part 

190:    Level: developer

192: .seealso: STShellSetApply()
193: @*/
194: PetscErrorCode STShellSetBackTransform(ST st, int (*backtr)(void*,PetscScalar*,PetscScalar*))
195: {
196:   PetscErrorCode ierr, (*f)(ST,int (*)(void*,PetscScalar*,PetscScalar*));

200:   PetscObjectQueryFunction((PetscObject)st,"STShellSetBackTransform_C",(void (**)(void))&f);
201:   if (f) {
202:     (*f)(st,(int (*)(void*,PetscScalar*,PetscScalar*))backtr);
203:   }
204:   return(0);
205: }

209: /*@C
210:    STShellSetName - Sets an optional name to associate with a shell
211:    spectral transformation.

213:    Not Collective

215:    Input Parameters:
216: +  st   - the spectral transformation context
217: -  name - character string describing the shell spectral transformation

219:    Level: developer

221: .seealso: STShellGetName()
222: @*/
223: PetscErrorCode STShellSetName(ST st,char *name)
224: {
225:   PetscErrorCode ierr, (*f)(ST,char *);

229:   PetscObjectQueryFunction((PetscObject)st,"STShellSetName_C",(void (**)(void))&f);
230:   if (f) {
231:     (*f)(st,name);
232:   }
233:   return(0);
234: }

238: /*@C
239:    STShellGetName - Gets an optional name that the user has set for a shell
240:    spectral transformation.

242:    Not Collective

244:    Input Parameter:
245: .  st - the spectral transformation context

247:    Output Parameter:
248: .  name - character string describing the shell spectral transformation

250:    Level: developer

252: .seealso: STShellSetName()
253: @*/
254: PetscErrorCode STShellGetName(ST st,char **name)
255: {
256:   PetscErrorCode ierr, (*f)(ST,char **);

260:   PetscObjectQueryFunction((PetscObject)st,"STShellGetName_C",(void (**)(void))&f);
261:   if (f) {
262:     (*f)(st,name);
263:   } else {
264:     SETERRQ(1,"Not shell spectral transformation, cannot get name");
265:   }
266:   return(0);
267: }

269: /*
270:    STCreate_Shell - creates a new spectral transformation class.
271:           This is intended to provide a simple class to use with EPS.
272:           You should not use this if you plan to make a complete class.

274:   Usage:
275: $             int (*apply)(void *,Vec,Vec);
276: $             int (*backtr)(void *,PetscScalar*,PetscScalar*);
277: $             STCreate(comm,&st);
278: $             STSetType(st,STSHELL);
279: $             STShellSetApply(st,apply,ctx);
280: $             STShellSetBackTransform(st,backtr);    (optional)

282: */
283: EXTERN_C_BEGIN
286: PetscErrorCode STCreate_Shell(ST st)
287: {
288:   PetscErrorCode ierr;
289:   ST_Shell       *shell;

292:   st->ops->destroy = STDestroy_Shell;
293:   PetscNew(ST_Shell,&shell);
294:   PetscLogObjectMemory(st,sizeof(ST_Shell));

296:   st->data           = (void *) shell;
297:   st->name           = 0;

299:   st->ops->apply     = STApply_Shell;
300:   st->ops->backtr    = STBackTransform_Shell;
301:   st->ops->view      = STView_Shell;

303:   shell->apply       = 0;
304:   shell->name        = 0;
305:   shell->ctx         = 0;
306:   shell->backtr      = 0;

308:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetApply_C","STShellSetApply_Shell",
309:                     STShellSetApply_Shell);
310:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetBackTransform_C","STShellSetBackTransform_Shell",
311:                     STShellSetBackTransform_Shell);
312:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetName_C","STShellSetName_Shell",
313:                     STShellSetName_Shell);
314:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellGetName_C","STShellGetName_Shell",
315:                     STShellGetName_Shell);

317:   return(0);
318: }
319: EXTERN_C_END