xref: /petsc/src/ts/interface/tsreg.c (revision 5a5d4f665b3034485fbcdfbe42345f740a4bd170)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.25 1997/10/28 14:24:05 bsmith 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_PVODE
31 $   TS_BEULER
32 $   TS_PSEUDO
33 
34   Normally, it is best to use the TSSetFromOptions() command and
35   then set the TS type from the options database rather than by using
36   this routine.  Using the options database provides the user with
37   maximum flexibility in evaluating the many different solvers.
38   The TSSetType() routine is provided for those situations where it
39   is necessary to set the timestepping solver independently of the
40   command line or options database.  This might be the case, for example,
41   when the choice of solver changes during the execution of the
42   program, and the user's application is taking responsibility for
43   choosing the appropriate method.  In other words, this routine is
44   for the advanced user.
45 
46 .keywords: TS, set, type
47 @*/
48 int TSSetType(TS ts,TSType method)
49 {
50   int ierr,(*r)(TS);
51 
52   PetscFunctionBegin;
53   PetscValidHeaderSpecific(ts,TS_COOKIE);
54   /* Get the function pointers for the method requested */
55   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
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   ierr = (*r)(ts);CHKERRQ(ierr);
61   PetscFunctionReturn(0);
62 }
63 
64 /* --------------------------------------------------------------------- */
65 #undef __FUNC__
66 #define __FUNC__ "TSRegister"
67 /*@C
68    TSRegister - Adds the method to the timestepping package, given
69    a function pointer and a solver name of the type TSType.
70 
71    Input Parameters:
72 .  name - either a predefined name such as TS_BEULER, or TS_NEW
73           to indicate a new user-defined solver
74 .  sname - corresponding string for name
75 .  create - routine to create method context
76 
77    Output Parameter:
78 .  oname - type associated with this new method
79 
80    Notes:
81    Multiple user-defined timestepping solvers can be added by calling
82    TSRegister() with the input parameter "name" set to be TS_NEW;
83    each call will return a unique solver type in the output
84    parameter "oname".
85 
86 .keywords: TS, timestepper, register
87 
88 .seealso: TSRegisterAll(), TSRegisterDestroy()
89 @*/
90 int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
91 {
92   int ierr;
93   static int numberregistered = 0;
94 
95   PetscFunctionBegin;
96   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
97 
98   if (oname) *oname = name;
99   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
100   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
101   PetscFunctionReturn(0);
102 }
103 /* --------------------------------------------------------------------- */
104 #undef __FUNC__
105 #define __FUNC__ "TSRegisterDestroy"
106 /*@C
107    TSRegisterDestroy - Frees the list of timesteppers that were
108    registered by TSRegister().
109 
110 .keywords: TS, timestepper, register, destroy
111 
112 .seealso: TSRegisterAll(), TSRegisterAll()
113 @*/
114 int TSRegisterDestroy()
115 {
116   PetscFunctionBegin;
117   if (__TSList) {
118     NRDestroy( __TSList );
119     __TSList = 0;
120   }
121   TSRegisterAllCalled = 0;
122   PetscFunctionReturn(0);
123 }
124 
125 #undef __FUNC__
126 #define __FUNC__ "TSGetType"
127 /*@C
128    TSGetType - Gets the TS method type and name (as a string).
129 
130    Input Parameter:
131 .  ts - timestepper solver context
132 
133    Output Parameter:
134 .  method - TS method (or use PETSC_NULL)
135 .  name - name of TS method (or use PETSC_NULL)
136 
137 .keywords: TS, timestepper, get, method, name
138 @*/
139 int TSGetType(TS ts, TSType *method,char **name)
140 {
141   int ierr;
142 
143   PetscFunctionBegin;
144   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
145   if (method) *method = (TSType) ts->type;
146   if (name)  *name = NRFindName( __TSList, (int) ts->type );
147   PetscFunctionReturn(0);
148 }
149 
150 #undef __FUNC__
151 #define __FUNC__ "TSPrintHelp"
152 /*@
153    TSPrintHelp - Prints all options for the TS (timestepping) component.
154 
155    Input Parameter:
156 .  ts - the TS context obtained from TSCreate()
157 
158    Options Database Keys:
159 $  -help, -h
160 
161 .keywords: TS, timestep, print, help
162 
163 .seealso: TSSetFromOptions()
164 @*/
165 int TSPrintHelp(TS ts)
166 {
167   char    *prefix = "-";
168   int     ierr;
169 
170   PetscFunctionBegin;
171   PetscValidHeaderSpecific(ts,TS_COOKIE);
172   if (ts->prefix) prefix = ts->prefix;
173   PetscPrintf(ts->comm,"TS options --------------------------------------------------\n");
174   ierr = NRPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",__TSList);CHKERRQ(ierr);
175   PetscPrintf(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
176   PetscPrintf(ts->comm," %sts_view: view TS info after each solve\n",prefix);
177 
178   PetscPrintf(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
179   PetscPrintf(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
180   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
181   PetscFunctionReturn(0);
182 }
183 
184 #undef __FUNC__
185 #define __FUNC__ "TSSetFromOptions"
186 /*@
187    TSSetFromOptions - Sets various TS parameters from user options.
188 
189    Input Parameter:
190 .  ts - the TS context obtained from TSCreate()
191 
192 .keywords: TS, timestep, set, options, database
193 
194 .seealso: TSPrintHelp()
195 @*/
196 int TSSetFromOptions(TS ts)
197 {
198   int    ierr,flg,loc[4],nmax;
199   TSType method;
200 
201   PetscFunctionBegin;
202   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
203 
204   PetscValidHeaderSpecific(ts,TS_COOKIE);
205   if (ts->setup_called) SETERRQ(1,0,"Call prior to TSSetUp!");
206   if (!__TSList) {ierr = TSRegisterAll();CHKERRQ(ierr);}
207   ierr = NRGetTypeFromOptions(ts->prefix,"-ts_type",__TSList,&method,&flg);CHKERRQ(ierr);
208   if (flg) {
209     ierr = TSSetType(ts,method); CHKERRQ(ierr);
210   }
211 
212   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
213   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
214   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
215   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
216   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
217   if (flg) {
218     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
219   }
220   nmax = 4;
221   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
222   if (flg) {
223     int    rank = 0;
224     DrawLG lg;
225     MPI_Comm_rank(ts->comm,&rank);
226     if (!rank) {
227       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
228       PLogObjectParent(ts,(PetscObject) lg);
229       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
230     }
231   }
232   if (!ts->setfromoptions) PetscFunctionReturn(0);
233   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
234   PetscFunctionReturn(0);
235 }
236 
237