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