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