xref: /petsc/src/ts/interface/tsreg.c (revision e0e703c18709862a0d755d6cf34ba2e20a8376a1)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.34 1998/04/03 23:16:43 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 DLList TSList = 0;
11 int TSRegisterAllCalled = 0;
12 
13 #undef __FUNC__
14 #define __FUNC__ "TSSetType"
15 /*@C
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    Collective on TS
23 
24   Options Database Command:
25 $ -ts_type  <method>
26 $    Use -help for a list of available methods
27 $    (for instance, euler)
28 
29    Notes:
30    See "petsc/include/ts.h" for available methods (for instance)
31 $   TS_EULER
32 $   TS_PVODE
33 $   TS_BEULER
34 $   TS_PSEUDO
35 
36   Normally, it is best to use the TSSetFromOptions() command and
37   then set the TS type from the options database rather than by using
38   this routine.  Using the options database provides the user with
39   maximum flexibility in evaluating the many different solvers.
40   The TSSetType() routine is provided for those situations where it
41   is necessary to set the timestepping solver independently of the
42   command line or options database.  This might be the case, for example,
43   when the choice of solver changes during the execution of the
44   program, and the user's application is taking responsibility for
45   choosing the appropriate method.  In other words, this routine is
46   for the advanced user.
47 
48 .keywords: TS, set, type
49 @*/
50 int TSSetType(TS ts,TSType method)
51 {
52   int ierr,(*r)(TS);
53 
54   PetscFunctionBegin;
55   PetscValidHeaderSpecific(ts,TS_COOKIE);
56   if (!PetscStrcmp(ts->type_name,method)) PetscFunctionReturn(0);
57 
58   /* Get the function pointers for the method requested */
59   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
60   ierr =  DLRegisterFind(ts->comm, TSList, method, (int (**)(void *)) &r );CHKERRQ(ierr);
61   if (!r) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method");}
62 
63   if (ts->sles) {ierr = SLESDestroy(ts->sles); CHKERRQ(ierr);}
64   if (ts->snes) {ierr = SNESDestroy(ts->snes); CHKERRQ(ierr);}
65   if (ts->destroy) {ierr = (*(ts)->destroy)(ts); CHKERRQ(ierr);}
66   ts->sles = 0;
67   ts->snes = 0;
68 
69   ierr = (*r)(ts);CHKERRQ(ierr);
70 
71   if (ts->type_name) PetscFree(ts->type_name);
72   ts->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(ts->type_name);
73   PetscStrcpy(ts->type_name,method);
74   PetscFunctionReturn(0);
75 }
76 
77 /* --------------------------------------------------------------------- */
78 #undef __FUNC__
79 #define __FUNC__ "TSRegisterDestroy"
80 /*@C
81    TSRegisterDestroy - Frees the list of timesteppers that were
82    registered by DLRegister().
83 
84    Not Collective
85 
86 .keywords: TS, timestepper, register, destroy
87 
88 .seealso: TSRegisterAll()
89 @*/
90 int TSRegisterDestroy(void)
91 {
92   int ierr;
93 
94   PetscFunctionBegin;
95   if (TSList) {
96     ierr = DLRegisterDestroy( TSList );CHKERRQ(ierr);
97     TSList = 0;
98   }
99   TSRegisterAllCalled = 0;
100   PetscFunctionReturn(0);
101 }
102 
103 #undef __FUNC__
104 #define __FUNC__ "TSGetType"
105 /*@C
106    TSGetType - Gets the TS method type (as a string).
107 
108    Input Parameter:
109 .  ts - timestepper solver context
110 
111    Output Parameter:
112 .  type - name of TS method
113 
114    Not Collective
115 
116 .keywords: TS, timestepper, get, type, name
117 @*/
118 int TSGetType(TS ts, TSType *type)
119 {
120   int ierr;
121 
122   PetscFunctionBegin;
123   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
124   *type = ts->type_name;
125   PetscFunctionReturn(0);
126 }
127 
128 #undef __FUNC__
129 #define __FUNC__ "TSPrintHelp"
130 /*@
131    TSPrintHelp - Prints all options for the TS (timestepping) component.
132 
133    Input Parameter:
134 .  ts - the TS context obtained from TSCreate()
135 
136    Options Database Keys:
137 $  -help, -h
138 
139    Collective on TS
140 
141 .keywords: TS, timestep, print, help
142 
143 .seealso: TSSetFromOptions()
144 @*/
145 int TSPrintHelp(TS ts)
146 {
147   char    *prefix = "-";
148   int     ierr;
149 
150   PetscFunctionBegin;
151   PetscValidHeaderSpecific(ts,TS_COOKIE);
152   if (ts->prefix) prefix = ts->prefix;
153   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
154   ierr = DLRegisterPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
155   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
156   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
157 
158   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
159   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
160   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
161   PetscFunctionReturn(0);
162 }
163 
164 #undef __FUNC__
165 #define __FUNC__ "TSSetFromOptions"
166 /*@
167    TSSetFromOptions - Sets various TS parameters from user options.
168 
169    Input Parameter:
170 .  ts - the TS context obtained from TSCreate()
171 
172    Collective on TS
173 
174 .keywords: TS, timestep, set, options, database
175 
176 .seealso: TSPrintHelp()
177 @*/
178 int TSSetFromOptions(TS ts)
179 {
180   int    ierr,flg,loc[4],nmax;
181   char   type[256];
182 
183   PetscFunctionBegin;
184   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
185 
186   PetscValidHeaderSpecific(ts,TS_COOKIE);
187   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp!");
188   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
189   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);
190   if (flg) {
191     ierr = TSSetType(ts,type); CHKERRQ(ierr);
192   }
193 
194   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
195   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
196   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
197   if (flg) {
198     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
199   }
200   nmax = 4;
201   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
202   if (flg) {
203     int    rank = 0;
204     DrawLG lg;
205     MPI_Comm_rank(ts->comm,&rank);
206     if (!rank) {
207       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
208       PLogObjectParent(ts,(PetscObject) lg);
209       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
210     }
211   }
212   if (!ts->type_name) {
213     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
214   }
215   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
216   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
217   if (!ts->setfromoptions) PetscFunctionReturn(0);
218   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
219   PetscFunctionReturn(0);
220 }
221 
222 
223