xref: /petsc/src/ts/interface/tsreg.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.23 1997/09/26 02:20:18 bsmith Exp bsmith $";
3 #endif
4 
5 #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
6 #include "src/sys/nreg.h"
7 #include "pinclude/pviewer.h"
8 #include <math.h>
9 
10 static NRList *__TSList = 0;
11 int TSRegisterAllCalled = 0;
12 
13 #undef __FUNC__
14 #define __FUNC__ "TSSetType"
15 /*@
16    TSSetType - Sets the method for the timestepping solver.
17 
18    Input Parameters:
19 .  ts - the TS context
20 .  method - a known method
21 
22   Options Database Command:
23 $ -ts_type  <method>
24 $    Use -help for a list of available methods
25 $    (for instance, euler)
26 
27    Notes:
28    See "petsc/include/ts.h" for available methods (for instance)
29 $   TS_EULER
30 $   TS_PVODE
31 $   TS_BEULER
32 $   TS_PSEUDO
33 
34   Normally, it is best to use the TSSetFromOptions() command and
35   then set the TS type from the options database rather than by using
36   this routine.  Using the options database provides the user with
37   maximum flexibility in evaluating the many different solvers.
38   The TSSetType() routine is provided for those situations where it
39   is necessary to set the timestepping solver independently of the
40   command line or options database.  This might be the case, for example,
41   when the choice of solver changes during the execution of the
42   program, and the user's application is taking responsibility for
43   choosing the appropriate method.  In other words, this routine is
44   for the advanced user.
45 
46 .keywords: TS, set, type
47 @*/
48 int TSSetType(TS ts,TSType method)
49 {
50   int ierr,(*r)(TS);
51 
52   PetscFunctionBegin;
53   PetscValidHeaderSpecific(ts,TS_COOKIE);
54   /* Get the function pointers for the method requested */
55   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
56   if (!__TSList) {SETERRQ(1,0,"Could not get methods");}
57   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
58   if (!r) {SETERRQ(1,0,"Unknown method");}
59   if (ts->data) PetscFree(ts->data);
60   ierr = (*r)(ts);CHKERRQ(ierr);
61   PetscFunctionReturn(0);
62 }
63 
64 /* --------------------------------------------------------------------- */
65 #undef __FUNC__
66 #define __FUNC__ "TSRegister"
67 /*@C
68    TSRegister - Adds the method to the timestepping package, given
69    a function pointer and a solver name of the type TSType.
70 
71    Input Parameters:
72 .  name - either a predefined name such as TS_BEULER, or TS_NEW
73           to indicate a new user-defined solver
74 .  sname - corresponding string for name
75 .  create - routine to create method context
76 
77    Output Parameter:
78 .  oname - type associated with this new method
79 
80    Notes:
81    Multiple user-defined timestepping solvers can be added by calling
82    TSRegister() with the input parameter "name" set to be TS_NEW;
83    each call will return a unique solver type in the output
84    parameter "oname".
85 
86 .keywords: TS, timestepper, register
87 
88 .seealso: TSRegisterAll(), TSRegisterDestroy()
89 @*/
90 int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
91 {
92   int ierr;
93   static int numberregistered = 0;
94 
95   PetscFunctionBegin;
96   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
97 
98   if (oname) *oname = name;
99   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
100   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
101   PetscFunctionReturn(0);
102 }
103 /* --------------------------------------------------------------------- */
104 #undef __FUNC__
105 #define __FUNC__ "TSRegisterDestroy"
106 /*@C
107    TSRegisterDestroy - Frees the list of timesteppers that were
108    registered by TSRegister().
109 
110 .keywords: TS, timestepper, register, destroy
111 
112 .seealso: TSRegisterAll(), TSRegisterAll()
113 @*/
114 int TSRegisterDestroy()
115 {
116   PetscFunctionBegin;
117   if (__TSList) {
118     NRDestroy( __TSList );
119     __TSList = 0;
120   }
121   TSRegisterAllCalled = 0;
122   PetscFunctionReturn(0);
123 }
124 
125 #undef __FUNC__
126 #define __FUNC__ "TSGetType"
127 /*@C
128    TSGetType - Gets the TS method type and name (as a string).
129 
130    Input Parameter:
131 .  ts - timestepper solver context
132 
133    Output Parameter:
134 .  method - TS method (or use PETSC_NULL)
135 .  name - name of TS method (or use PETSC_NULL)
136 
137 .keywords: TS, timestepper, get, method, name
138 @*/
139 int TSGetType(TS ts, TSType *method,char **name)
140 {
141   int ierr;
142 
143   PetscFunctionBegin;
144   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
145   if (method) *method = (TSType) ts->type;
146   if (name)  *name = NRFindName( __TSList, (int) ts->type );
147   PetscFunctionReturn(0);
148 }
149 
150 #undef __FUNC__
151 #define __FUNC__ "TSPrintTypes_Private"
152 /*
153    TSPrintTypes_Private - Prints the TS methods available from the
154    options database.
155 
156    Input Parameters:
157 .  comm   - The communicator (usually PETSC_COMM_WORLD)
158 .  prefix - prefix (usually "-")
159 .  name   - the options database name (by default "ts_type")
160 */
161 int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
162 {
163   FuncList *entry;
164 
165   PetscFunctionBegin;
166   if (!__TSList) {TSRegisterAll();}
167   entry = __TSList->head;
168   PetscPrintf(comm," %s%s (one of)",prefix,name);
169   while (entry) {
170     PetscPrintf(comm," %s",entry->name);
171     entry = entry->next;
172   }
173   PetscPrintf(comm,"\n");
174   PetscFunctionReturn(0);
175 }
176 
177 
178 #undef __FUNC__
179 #define __FUNC__ "TSGetTypeFromOptions_Private"
180 /*
181    TSGetTypeFromOptions_Private - Sets the selected method from the
182    options database.
183 
184    Input Parameter:
185 .  ctx - the TS context
186 
187    Output Parameter:
188 .  method -  solver method
189 
190    Returns:
191    Returns 1 if the method is found; 0 otherwise.
192 
193    Options Database Key:
194 $  -ts_type  method
195 */
196 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
197 {
198   int  ierr;
199   char sbuf[50];
200 
201   PetscFunctionBegin;
202   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
203   if (*flg) {
204     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
205     *method = (TSType)NRFindID( __TSList, sbuf );
206   }
207   PetscFunctionReturn(0);
208 }
209