xref: /petsc/src/ts/interface/tsreg.c (revision 4e126f44ee86c2181996ac598c997002e40713ba)
1 
2 
3 #ifndef lint
4 static char vcid[] = "$Id: tsreg.c,v 1.10 1996/10/15 18:48:05 balay Exp curfman $";
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 
14 /*@
15    TSSetType - Sets the method for the timestepping solver.
16 
17    Input Parameters:
18 .  ts - the TS context
19 .  method - a known method
20 
21   Options Database Command:
22 $ -ts_type  <method>
23 $    Use -help for a list of available methods
24 $    (for instance, euler)
25 
26    Notes:
27    See "petsc/include/ts.h" for available methods (for instance)
28 $   TS_EULER
29 $   TS_BEULER
30 $   TS_PSEUDO
31 
32   Normally, it is best to use the TSSetFromOptions() command and
33   then set the TS type from the options database rather than by using
34   this routine.  Using the options database provides the user with
35   maximum flexibility in evaluating the many different solvers.
36   The TSSetType() routine is provided for those situations where it
37   is necessary to set the timestepping solver independently of the
38   command line or options database.  This might be the case, for example,
39   when the choice of solver changes during the execution of the
40   program, and the user's application is taking responsibility for
41   choosing the appropriate method.  In other words, this routine is
42   for the advanced user.
43 
44 .keywords: TS, set, type
45 @*/
46 int TSSetType(TS ts,TSType method)
47 {
48   int (*r)(TS);
49 
50   PetscValidHeaderSpecific(ts,TS_COOKIE);
51   /* Get the function pointers for the method requested */
52   if (!__TSList) {TSRegisterAll();}
53   if (!__TSList) {SETERRQ(1,"TSSetType:Could not get methods");}
54   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
55   if (!r) {SETERRQ(1,"TSSetType:Unknown method");}
56   if (ts->data) PetscFree(ts->data);
57   return (*r)(ts);
58 }
59 
60 /* --------------------------------------------------------------------- */
61 /*@C
62    TSRegister - Adds the method to the nonlinear solver package, given
63    a function pointer and a nonlinear solver name of the type TSType.
64 
65    Input Parameters:
66 .  name - for instance TS_EQ_NLS, TS_EQ_NTR, ...
67 .  sname - corfunPonding string for name
68 .  create - routine to create method context
69 
70 .keywords: TS, nonlinear, register
71 
72 .seealso: TSRegisterAll(), TSRegisterDestroy()
73 @*/
74 int TSRegister(int name, char *sname, int (*create)(TS))
75 {
76   int ierr;
77   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
78   NRRegister( __TSList, name, sname, (int (*)(void*))create );
79   return 0;
80 }
81 /* --------------------------------------------------------------------- */
82 /*@C
83    TSRegisterDestroy - Frees the list of nonlinear solvers that were
84    registered by TSRegister().
85 
86 .keywords: TS, nonlinear, register, destroy
87 
88 .seealso: TSRegisterAll(), TSRegisterAll()
89 @*/
90 int TSRegisterDestroy()
91 {
92   if (__TSList) {
93     NRDestroy( __TSList );
94     __TSList = 0;
95   }
96   return 0;
97 }
98 
99 /*@C
100    TSGetType - Gets the TS method type and name (as a string).
101 
102    Input Parameter:
103 .  ts - nonlinear solver context
104 
105    Output Parameter:
106 .  method - TS method (or use PETSC_NULL)
107 .  name - name of TS method (or use PETSC_NULL)
108 
109 .keywords: TS, nonlinear, get, method, name
110 @*/
111 int TSGetType(TS ts, TSType *method,char **name)
112 {
113   int ierr;
114   if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
115   if (method) *method = (TSType) ts->type;
116   if (name)  *name = NRFindName( __TSList, (int) ts->type );
117   return 0;
118 }
119 
120 #include <stdio.h>
121 /*
122    TSPrintTypes_Private - Prints the TS methods available from the
123    options database.
124 
125    Input Parameters:
126 .  comm   - The communicator (usually MPI_COMM_WORLD)
127 .  prefix - prefix (usually "-")
128 .  name   - the options database name (by default "ts_type")
129 */
130 int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
131 {
132   FuncList *entry;
133   if (!__TSList) {TSRegisterAll();}
134   entry = __TSList->head;
135   PetscPrintf(comm," %s%s (one of)",prefix,name);
136   while (entry) {
137     PetscPrintf(comm," %s",entry->name);
138     entry = entry->next;
139   }
140   PetscPrintf(comm,"\n");
141   return 0;
142 }
143 
144 
145 /*
146    TSGetTypeFromOptions_Private - Sets the selected method from the
147    options database.
148 
149    Input Parameter:
150 .  ctx - the TS context
151 
152    Output Parameter:
153 .  method -  solver method
154 
155    Returns:
156    Returns 1 if the method is found; 0 otherwise.
157 
158    Options Database Key:
159 $  -ts_type  method
160 */
161 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
162 {
163   int ierr;
164   char sbuf[50];
165   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
166   if (*flg) {
167     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
168     *method = (TSType)NRFindID( __TSList, sbuf );
169   }
170   return 0;
171 }
172