xref: /petsc/src/ts/interface/tsreg.c (revision ca90a50798d8c9dc72aff46126a49670b03a2421)
1 
2 #ifndef lint
3 static char vcid[] = "$Id: tsreg.c,v 1.2 1996/01/31 03:59:25 bsmith Exp bsmith $";
4 #endif
5 
6 #include "tsimpl.h"      /*I "ts.h"  I*/
7 #include "sys/nreg.h"
8 #include "pinclude/pviewer.h"
9 #include <math.h>
10 
11 static NRList *__TSList = 0;
12 
13 /*@
14    TSSetType - Sets the method for the nonlinear solver.
15 
16    Input Parameters:
17 .  ts - the TS context
18 .  method - a known method
19 
20    Notes:
21    See "petsc/include/ts.h" for available methods (for instance)
22 $   TS_EULER
23 $   TS_BEULER
24 $   TS_PSEUDO
25 
26   Options Database Command:
27 $ -ts_type  <method>
28 $    Use -help for a list of available methods
29 $    (for instance, euler)
30 
31 .keysords: TS, set, method
32 @*/
33 int TSSetType(TS ts,TSType method)
34 {
35   int (*r)(TS);
36 
37   PETSCVALIDHEADERSPECIFIC(ts,TS_COOKIE);
38   /* Get the function pointers for the method requested */
39   if (!__TSList) {TSRegisterAll();}
40   if (!__TSList) {SETERRQ(1,"TSSetType:Could not get methods");}
41   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
42   if (!r) {SETERRQ(1,"TSSetType:Unknown method");}
43   if (ts->data) PetscFree(ts->data);
44   return (*r)(ts);
45 }
46 
47 /* --------------------------------------------------------------------- */
48 /*@C
49    TSRegister - Adds the method to the nonlinear solver package, given
50    a function pointer and a nonlinear solver name of the type TSType.
51 
52    Input Parameters:
53 .  name - for instance TS_EQ_NLS, TS_EQ_NTR, ...
54 .  sname - corfunPonding string for name
55 .  create - routine to create method context
56 
57 .keywords: TS, nonlinear, register
58 
59 .seealso: TSRegisterAll(), TSRegisterDestroy()
60 @*/
61 int TSRegister(int name, char *sname, int (*create)(TS))
62 {
63   int ierr;
64   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
65   NRRegister( __TSList, name, sname, (int (*)(void*))create );
66   return 0;
67 }
68 /* --------------------------------------------------------------------- */
69 /*@C
70    TSRegisterDestroy - Frees the list of nonlinear solvers that were
71    registered by TSRegister().
72 
73 .keywords: TS, nonlinear, register, destroy
74 
75 .seealso: TSRegisterAll(), TSRegisterAll()
76 @*/
77 int TSRegisterDestroy()
78 {
79   if (__TSList) {
80     NRDestroy( __TSList );
81     __TSList = 0;
82   }
83   return 0;
84 }
85 
86 /*@C
87    TSGetType - Gets the TS method type and name (as a string).
88 
89    Input Parameter:
90 .  ts - nonlinear solver context
91 
92    Output Parameter:
93 .  method - TS method (or use PETSC_NULL)
94 .  name - name of TS method (or use PETSC_NULL)
95 
96 .keywords: TS, nonlinear, get, method, name
97 @*/
98 int TSGetType(TS ts, TSType *method,char **name)
99 {
100   int ierr;
101   if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
102   if (method) *method = (TSType) ts->type;
103   if (name)  *name = NRFindName( __TSList, (int) ts->type );
104   return 0;
105 }
106 
107 #include <stdio.h>
108 /*
109    TSPrintTypes_Private - Prints the TS methods available from the
110    options database.
111 
112    Input Parameters:
113 .  prefix - prefix (usually "-")
114 .  name - the options database name (by default "ts_type")
115 */
116 int TSPrintTypes_Private(char* prefix,char *name)
117 {
118   FuncList *entry;
119   if (!__TSList) {TSRegisterAll();}
120   entry = __TSList->head;
121   fprintf(stderr," %s%s (one of)",prefix,name);
122   while (entry) {
123     fprintf(stderr," %s",entry->name);
124     entry = entry->next;
125   }
126   fprintf(stderr,"\n");
127   return 0;
128 }
129 
130 
131 /*
132    TSGetTypeFromOptions_Private - Sets the selected method from the
133    options database.
134 
135    Input Parameter:
136 .  ctx - the TS context
137 
138    Output Parameter:
139 .  method -  solver method
140 
141    Returns:
142    Returns 1 if the method is found; 0 otherwise.
143 
144    Options Database Key:
145 $  -ts_type  method
146 */
147 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
148 {
149   int ierr;
150   char sbuf[50];
151   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
152   if (*flg) {
153     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
154     *method = (TSType)NRFindID( __TSList, sbuf );
155   }
156   return 0;
157 }
158