xref: /petsc/src/ts/interface/tsreg.c (revision 84cb2905a512499b542fd30cf471a05f503d96d2)
1 
2 
3 #ifndef lint
4 static char vcid[] = "$Id: tsreg.c,v 1.15 1997/01/06 20:28:06 balay 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 nonlinear solver package, given
68    a function pointer and a nonlinear solver name of the type TSType.
69 
70    Input Parameters:
71 .  name - for instance TS_BEULER, or TS_NEW for a new method
72 .  sname - corfunPonding string for name
73 .  create - routine to create method context
74 
75    Output Parameter:
76 .   oname - type associated with this new method
77 
78 .keywords: TS, nonlinear, register
79 
80 .seealso: TSRegisterAll(), TSRegisterDestroy()
81 @*/
82 int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
83 {
84   int ierr;
85   static int numberregistered = 0;
86 
87   if (name == TS_NEW) name = TS_NEW + numberregistered++;
88 
89   if (oname) *oname = name;
90   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
91   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
92   return 0;
93 }
94 /* --------------------------------------------------------------------- */
95 #undef __FUNC__
96 #define __FUNC__ "TSRegisterDestroy"
97 /*@C
98    TSRegisterDestroy - Frees the list of timesteppers that were
99    registered by TSRegister().
100 
101 .keywords: TS, nonlinear, register, destroy
102 
103 .seealso: TSRegisterAll(), TSRegisterAll()
104 @*/
105 int TSRegisterDestroy()
106 {
107   if (__TSList) {
108     NRDestroy( __TSList );
109     __TSList = 0;
110   }
111   TSRegisterAllCalled = 0;
112   return 0;
113 }
114 
115 #undef __FUNC__
116 #define __FUNC__ "TSGetType"
117 /*@C
118    TSGetType - Gets the TS method type and name (as a string).
119 
120    Input Parameter:
121 .  ts - nonlinear solver context
122 
123    Output Parameter:
124 .  method - TS method (or use PETSC_NULL)
125 .  name - name of TS method (or use PETSC_NULL)
126 
127 .keywords: TS, nonlinear, get, method, name
128 @*/
129 int TSGetType(TS ts, TSType *method,char **name)
130 {
131   int ierr;
132   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
133   if (method) *method = (TSType) ts->type;
134   if (name)  *name = NRFindName( __TSList, (int) ts->type );
135   return 0;
136 }
137 
138 #include <stdio.h>
139 #undef __FUNC__
140 #define __FUNC__ "TSPrintTypes_Private"
141 /*
142    TSPrintTypes_Private - Prints the TS methods available from the
143    options database.
144 
145    Input Parameters:
146 .  comm   - The communicator (usually MPI_COMM_WORLD)
147 .  prefix - prefix (usually "-")
148 .  name   - the options database name (by default "ts_type")
149 */
150 int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
151 {
152   FuncList *entry;
153   if (!__TSList) {TSRegisterAll();}
154   entry = __TSList->head;
155   PetscPrintf(comm," %s%s (one of)",prefix,name);
156   while (entry) {
157     PetscPrintf(comm," %s",entry->name);
158     entry = entry->next;
159   }
160   PetscPrintf(comm,"\n");
161   return 0;
162 }
163 
164 
165 #undef __FUNC__
166 #define __FUNC__ "TSGetTypeFromOptions_Private"
167 /*
168    TSGetTypeFromOptions_Private - Sets the selected method from the
169    options database.
170 
171    Input Parameter:
172 .  ctx - the TS context
173 
174    Output Parameter:
175 .  method -  solver method
176 
177    Returns:
178    Returns 1 if the method is found; 0 otherwise.
179 
180    Options Database Key:
181 $  -ts_type  method
182 */
183 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
184 {
185   int ierr;
186   char sbuf[50];
187   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
188   if (*flg) {
189     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
190     *method = (TSType)NRFindID( __TSList, sbuf );
191   }
192   return 0;
193 }
194