xref: /petsc/src/ts/interface/tsreg.c (revision 3638b69dbd64ba4780c119c4ea508ad72b54579c)
1 
2 #ifndef lint
3 static char vcid[] = "$Id: tsreg.c,v 1.1 1996/01/06 16:45:30 bsmith Exp bsmith $";
4 #endif
5 
6 #include "tsimpl.h"      /*I "ts.h"  I*/
7 #include "sys/nreg.h"
8 #include "pinclude/pviewer.h"
9 #include <math.h>
10 
11 static NRList *__TSList = 0;
12 
13 /*@
14    TSSetType - Sets the method for the nonlinear solver.
15 
16    Input Parameters:
17 .  ts - the TS context
18 .  method - a known method
19 
20    Notes:
21    See "petsc/include/ts.h" for available methods (for instance)
22 $   TS_EULER
23 
24   Options Database Command:
25 $ -ts_type  <method>
26 $    Use -help for a list of available methods
27 $    (for instance, euler)
28 
29 .keysords: TS, set, method
30 @*/
31 int TSSetType(TS ts,TSType method)
32 {
33   int (*r)(TS);
34 
35   PETSCVALIDHEADERSPECIFIC(ts,TS_COOKIE);
36   /* Get the function pointers for the method requested */
37   if (!__TSList) {TSRegisterAll();}
38   if (!__TSList) {SETERRQ(1,"TSSetType:Could not get methods");}
39   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
40   if (!r) {SETERRQ(1,"TSSetType:Unknown method");}
41   if (ts->data) PetscFree(ts->data);
42   return (*r)(ts);
43 }
44 
45 /* --------------------------------------------------------------------- */
46 /*@C
47    TSRegister - Adds the method to the nonlinear solver package, given
48    a function pointer and a nonlinear solver name of the type TSType.
49 
50    Input Parameters:
51 .  name - for instance TS_EQ_NLS, TS_EQ_NTR, ...
52 .  sname - corfunPonding string for name
53 .  create - routine to create method context
54 
55 .keywords: TS, nonlinear, register
56 
57 .seealso: TSRegisterAll(), TSRegisterDestroy()
58 @*/
59 int TSRegister(int name, char *sname, int (*create)(TS))
60 {
61   int ierr;
62   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
63   NRRegister( __TSList, name, sname, (int (*)(void*))create );
64   return 0;
65 }
66 /* --------------------------------------------------------------------- */
67 /*@C
68    TSRegisterDestroy - Frees the list of nonlinear solvers that were
69    registered by TSRegister().
70 
71 .keywords: TS, nonlinear, register, destroy
72 
73 .seealso: TSRegisterAll(), TSRegisterAll()
74 @*/
75 int TSRegisterDestroy()
76 {
77   if (__TSList) {
78     NRDestroy( __TSList );
79     __TSList = 0;
80   }
81   return 0;
82 }
83 
84 /*@C
85    TSGetType - Gets the TS method type and name (as a string).
86 
87    Input Parameter:
88 .  ts - nonlinear solver context
89 
90    Output Parameter:
91 .  method - TS method (or use PETSC_NULL)
92 .  name - name of TS method (or use PETSC_NULL)
93 
94 .keywords: TS, nonlinear, get, method, name
95 @*/
96 int TSGetType(TS ts, TSType *method,char **name)
97 {
98   int ierr;
99   if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
100   if (method) *method = (TSType) ts->type;
101   if (name)  *name = NRFindName( __TSList, (int) ts->type );
102   return 0;
103 }
104 
105 #include <stdio.h>
106 /*
107    TSPrintTypes_Private - Prints the TS methods available from the
108    options database.
109 
110    Input Parameters:
111 .  prefix - prefix (usually "-")
112 .  name - the options database name (by default "ts_type")
113 */
114 int TSPrintTypes_Private(char* prefix,char *name)
115 {
116   FuncList *entry;
117   if (!__TSList) {TSRegisterAll();}
118   entry = __TSList->head;
119   fprintf(stderr," %s%s (one of)",prefix,name);
120   while (entry) {
121     fprintf(stderr," %s",entry->name);
122     entry = entry->next;
123   }
124   fprintf(stderr,"\n");
125   return 0;
126 }
127 
128 
129 /*
130    TSGetTypeFromOptions_Private - Sets the selected method from the
131    options database.
132 
133    Input Parameter:
134 .  ctx - the TS context
135 
136    Output Parameter:
137 .  method -  solver method
138 
139    Returns:
140    Returns 1 if the method is found; 0 otherwise.
141 
142    Options Database Key:
143 $  -ts_type  method
144 */
145 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
146 {
147   int ierr;
148   char sbuf[50];
149   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
150   if (*flg) {
151     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
152     *method = (TSType)NRFindID( __TSList, sbuf );
153   }
154   return 0;
155 }
156