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: static int STApply_Shell(ST st,Vec x,Vec y)
 21: {
 22:   ST_Shell *shell;
 23:   int       ierr;

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

 34: static int STBackTransform_Shell(ST st,PetscScalar *eigr,PetscScalar *eigi)
 35: {
 36:   ST_Shell *shell;
 37:   int       ierr;

 40:   shell = (ST_Shell *) st->data;
 41:   if (shell->backtr) {
 42:     (*shell->backtr)(shell->ctx,eigr,eigi);
 43:   }
 44:   return(0);
 45: }

 49: static int STDestroy_Shell(ST st)
 50: {
 51:   ST_Shell *shell = (ST_Shell *) st->data;
 52:   int      ierr;

 55:   PetscFree(shell);
 56:   return(0);
 57: }

 61: static int STView_Shell(ST st,PetscViewer viewer)
 62: {
 63:   ST_Shell  *ctx = (ST_Shell*)st->data;
 64:   int        ierr;
 65:   PetscTruth isascii;

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

 78: EXTERN_C_BEGIN
 81: int STShellSetApply_Shell(ST st, int (*apply)(void*,Vec,Vec),void *ptr)
 82: {
 83:   ST_Shell *shell;

 86:   shell        = (ST_Shell *) st->data;
 87:   shell->apply = apply;
 88:   shell->ctx   = ptr;
 89:   return(0);
 90: }
 91: EXTERN_C_END

 93: EXTERN_C_BEGIN
 96: int STShellSetBackTransform_Shell(ST st, int (*backtr)(void*,PetscScalar*,PetscScalar*))
 97: {
 98:   ST_Shell *shell;

101:   shell         = (ST_Shell *) st->data;
102:   shell->backtr = backtr;
103:   return(0);
104: }
105: EXTERN_C_END

107: EXTERN_C_BEGIN
110: int STShellSetName_Shell(ST st,char *name)
111: {
112:   ST_Shell *shell;

115:   shell       = (ST_Shell *) st->data;
116:   shell->name = name;
117:   return(0);
118: }
119: EXTERN_C_END

121: EXTERN_C_BEGIN
124: int STShellGetName_Shell(ST st,char **name)
125: {
126:   ST_Shell *shell;

129:   shell  = (ST_Shell *) st->data;
130:   *name  = shell->name;
131:   return(0);
132: }
133: EXTERN_C_END

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

141:    Collective on ST

143:    Input Parameters:
144: +  st    - the spectral transformation context
145: .  apply - the application-provided transformation routine
146: -  ptr   - pointer to data needed by this routine

148:    Calling sequence of apply:
149: .vb
150:    int apply (void *ptr,Vec xin,Vec xout)
151: .ve

153: +  ptr  - the application context
154: .  xin  - input vector
155: -  xout - output vector

157:    Level: developer

159: .seealso: STShellSetBackTransform()
160: @*/
161: int STShellSetApply(ST st, int (*apply)(void*,Vec,Vec),void *ptr)
162: {
163:   int ierr, (*f)(ST,int (*)(void*,Vec,Vec),void *);

167:   PetscObjectQueryFunction((PetscObject)st,"STShellSetApply_C",(void (**)(void))&f);
168:   if (f) {
169:     (*f)(st,apply,ptr);
170:   }
171:   return(0);
172: }

176: /*@C
177:    STShellSetBackTransform - Sets the routine to be called after the 
178:    eigensolution process has finished in order to transform back the
179:    computed eigenvalues.

181:    Collective on ST

183:    Input Parameters:
184: +  st     - the spectral transformation context
185: .  backtr - the application-provided routine

187:    Calling sequence of backtr:
188: .vb
189:    int backtr (void *ptr, PetscScalar *eigr, PetscScalar *eigi)
190: .ve

192: .  ptr  - the application context
193: .  eigr - pointer ot the real part of the eigenvalue to transform back
194: .  eigi - pointer ot the imaginary part 

196:    Level: developer

198: .seealso: STShellSetApply()
199: @*/
200: int STShellSetBackTransform(ST st, int (*backtr)(void*,PetscScalar*,PetscScalar*))
201: {
202:   int ierr, (*f)(ST,int (*)(void*,PetscScalar*,PetscScalar*));

206:   PetscObjectQueryFunction((PetscObject)st,"STShellSetBackTransform_C",(void (**)(void))&f);
207:   if (f) {
208:     (*f)(st,(int (*)(void*,PetscScalar*,PetscScalar*))backtr);
209:   }
210:   return(0);
211: }

215: /*@C
216:    STShellSetName - Sets an optional name to associate with a shell
217:    spectral transformation.

219:    Not Collective

221:    Input Parameters:
222: +  st   - the spectral transformation context
223: -  name - character string describing the shell spectral transformation

225:    Level: developer

227: .seealso: STShellGetName()
228: @*/
229: int STShellSetName(ST st,char *name)
230: {
231:   int ierr, (*f)(ST,char *);

235:   PetscObjectQueryFunction((PetscObject)st,"STShellSetName_C",(void (**)(void))&f);
236:   if (f) {
237:     (*f)(st,name);
238:   }
239:   return(0);
240: }

244: /*@C
245:    STShellGetName - Gets an optional name that the user has set for a shell
246:    spectral transformation.

248:    Not Collective

250:    Input Parameter:
251: .  st - the spectral transformation context

253:    Output Parameter:
254: .  name - character string describing the shell spectral transformation

256:    Level: developer

258: .seealso: STShellSetName()
259: @*/
260: int STShellGetName(ST st,char **name)
261: {
262:   int ierr, (*f)(ST,char **);

266:   PetscObjectQueryFunction((PetscObject)st,"STShellGetName_C",(void (**)(void))&f);
267:   if (f) {
268:     (*f)(st,name);
269:   } else {
270:     SETERRQ(1,"Not shell spectral transformation, cannot get name");
271:   }
272:   return(0);
273: }

275: /*
276:    STCreate_Shell - creates a new spectral transformation class.
277:           This is intended to provide a simple class to use with EPS.
278:           You should not use this if you plan to make a complete class.

280:   Usage:
281: $             int (*apply)(void *,Vec,Vec);
282: $             int (*backtr)(void *,PetscScalar*,PetscScalar*);
283: $             STCreate(comm,&st);
284: $             STSetType(st,STSHELL);
285: $             STShellSetApply(st,apply,ctx);
286: $             STShellSetBackTransform(st,backtr);    (optional)

288: */
289: EXTERN_C_BEGIN
292: int STCreate_Shell(ST st)
293: {
294:   int      ierr;
295:   ST_Shell *shell;

298:   st->ops->destroy = STDestroy_Shell;
299:   PetscNew(ST_Shell,&shell);
300:   PetscLogObjectMemory(st,sizeof(ST_Shell));

302:   st->data           = (void *) shell;
303:   st->name           = 0;
304:   st->numberofshifts = 0;

306:   st->ops->apply     = STApply_Shell;
307:   st->ops->backtr    = STBackTransform_Shell;
308:   st->ops->view      = STView_Shell;

310:   shell->apply       = 0;
311:   shell->name        = 0;
312:   shell->ctx         = 0;
313:   shell->backtr      = 0;

315:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetApply_C","STShellSetApply_Shell",
316:                     STShellSetApply_Shell);
317:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetBackTransform_C","STShellSetBackTransform_Shell",
318:                     STShellSetBackTransform_Shell);
319:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellSetName_C","STShellSetName_Shell",
320:                     STShellSetName_Shell);
321:   PetscObjectComposeFunctionDynamic((PetscObject)st,"STShellGetName_C","STShellGetName_Shell",
322:                     STShellGetName_Shell);

324:   return(0);
325: }
326: EXTERN_C_END