xref: /petsc/src/ts/interface/tsreg.c (revision 77c4ece699e97450631aa6fc5b0ef04ff52df029)
1 
2 #ifndef lint
3 static char vcid[] = "$Id: tsreg.c,v 1.3 1996/03/07 20:28:24 bsmith Exp balay $";
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 .  comm   - The communicator ( usually MPI_COMM_WORLD)
114 .  prefix - prefix (usually "-")
115 .  name   - the options database name (by default "ts_type")
116 */
117 int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
118 {
119   FuncList *entry;
120   if (!__TSList) {TSRegisterAll();}
121   entry = __TSList->head;
122   MPIU_printf(comm," %s%s (one of)",prefix,name);
123   while (entry) {
124     MPIU_printf(comm," %s",entry->name);
125     entry = entry->next;
126   }
127   MPIU_printf(comm,"\n");
128   return 0;
129 }
130 
131 
132 /*
133    TSGetTypeFromOptions_Private - Sets the selected method from the
134    options database.
135 
136    Input Parameter:
137 .  ctx - the TS context
138 
139    Output Parameter:
140 .  method -  solver method
141 
142    Returns:
143    Returns 1 if the method is found; 0 otherwise.
144 
145    Options Database Key:
146 $  -ts_type  method
147 */
148 int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
149 {
150   int ierr;
151   char sbuf[50];
152   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
153   if (*flg) {
154     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
155     *method = (TSType)NRFindID( __TSList, sbuf );
156   }
157   return 0;
158 }
159