xref: /petsc/src/ts/interface/tsreg.c (revision 76be9ce4a233aaa47cda2bc7f5a27cd7faabecaa)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.27 1997/12/01 01:56:01 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   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
57   if (!r) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method");}
58   if (ts->data) PetscFree(ts->data);
59   ierr = (*r)(ts);CHKERRQ(ierr);
60   PetscFunctionReturn(0);
61 }
62 
63 /* --------------------------------------------------------------------- */
64 #undef __FUNC__
65 #define __FUNC__ "TSRegister"
66 /*@C
67    TSRegister - Adds the method to the timestepping package, given
68    a function pointer and a solver name of the type TSType.
69 
70    Input Parameters:
71 .  name - either a predefined name such as TS_BEULER, or TS_NEW
72           to indicate a new user-defined solver
73 .  sname - corresponding string for name
74 .  create - routine to create method context
75 
76    Output Parameter:
77 .  oname - type associated with this new method
78 
79    Notes:
80    Multiple user-defined timestepping solvers can be added by calling
81    TSRegister() with the input parameter "name" set to be TS_NEW;
82    each call will return a unique solver type in the output
83    parameter "oname".
84 
85 .keywords: TS, timestepper, register
86 
87 .seealso: TSRegisterAll(), TSRegisterDestroy()
88 @*/
89 int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
90 {
91   int ierr;
92   static int numberregistered = 0;
93 
94   PetscFunctionBegin;
95   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
96 
97   if (oname) *oname = name;
98   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
99   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
100   PetscFunctionReturn(0);
101 }
102 /* --------------------------------------------------------------------- */
103 #undef __FUNC__
104 #define __FUNC__ "TSRegisterDestroy"
105 /*@C
106    TSRegisterDestroy - Frees the list of timesteppers that were
107    registered by TSRegister().
108 
109 .keywords: TS, timestepper, register, destroy
110 
111 .seealso: TSRegisterAll(), TSRegisterAll()
112 @*/
113 int TSRegisterDestroy()
114 {
115   PetscFunctionBegin;
116   if (__TSList) {
117     NRDestroy( __TSList );
118     __TSList = 0;
119   }
120   TSRegisterAllCalled = 0;
121   PetscFunctionReturn(0);
122 }
123 
124 #undef __FUNC__
125 #define __FUNC__ "TSGetType"
126 /*@C
127    TSGetType - Gets the TS method type and name (as a string).
128 
129    Input Parameter:
130 .  ts - timestepper solver context
131 
132    Output Parameter:
133 .  method - TS method (or use PETSC_NULL)
134 .  name - name of TS method (or use PETSC_NULL)
135 
136 .keywords: TS, timestepper, get, method, name
137 @*/
138 int TSGetType(TS ts, TSType *method,char **name)
139 {
140   int ierr;
141 
142   PetscFunctionBegin;
143   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
144   if (method) *method = (TSType) ts->type;
145   if (name)  *name = NRFindName( __TSList, (int) ts->type );
146   PetscFunctionReturn(0);
147 }
148 
149 #undef __FUNC__
150 #define __FUNC__ "TSPrintHelp"
151 /*@
152    TSPrintHelp - Prints all options for the TS (timestepping) component.
153 
154    Input Parameter:
155 .  ts - the TS context obtained from TSCreate()
156 
157    Options Database Keys:
158 $  -help, -h
159 
160 .keywords: TS, timestep, print, help
161 
162 .seealso: TSSetFromOptions()
163 @*/
164 int TSPrintHelp(TS ts)
165 {
166   char    *prefix = "-";
167   int     ierr;
168 
169   PetscFunctionBegin;
170   PetscValidHeaderSpecific(ts,TS_COOKIE);
171   if (ts->prefix) prefix = ts->prefix;
172   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
173   ierr = NRPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",__TSList);CHKERRQ(ierr);
174   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
175   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
176 
177   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
178   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
179   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
180   PetscFunctionReturn(0);
181 }
182 
183 #undef __FUNC__
184 #define __FUNC__ "TSSetFromOptions"
185 /*@
186    TSSetFromOptions - Sets various TS parameters from user options.
187 
188    Input Parameter:
189 .  ts - the TS context obtained from TSCreate()
190 
191 .keywords: TS, timestep, set, options, database
192 
193 .seealso: TSPrintHelp()
194 @*/
195 int TSSetFromOptions(TS ts)
196 {
197   int    ierr,flg,loc[4],nmax;
198   TSType method;
199 
200   PetscFunctionBegin;
201   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
202 
203   PetscValidHeaderSpecific(ts,TS_COOKIE);
204   if (ts->setup_called) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp!");
205   if (!__TSList) {ierr = TSRegisterAll();CHKERRQ(ierr);}
206   ierr = NRGetTypeFromOptions(ts->prefix,"-ts_type",__TSList,&method,&flg);CHKERRQ(ierr);
207   if (flg) {
208     ierr = TSSetType(ts,method); CHKERRQ(ierr);
209   }
210 
211   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
212   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
213   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
214   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
215   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
216   if (flg) {
217     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
218   }
219   nmax = 4;
220   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
221   if (flg) {
222     int    rank = 0;
223     DrawLG lg;
224     MPI_Comm_rank(ts->comm,&rank);
225     if (!rank) {
226       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
227       PLogObjectParent(ts,(PetscObject) lg);
228       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
229     }
230   }
231   if (!ts->setfromoptions) PetscFunctionReturn(0);
232   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
233   PetscFunctionReturn(0);
234 }
235 
236