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