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