xref: /petsc/src/ts/interface/tsreg.c (revision 6831982abb6453c2f3e25efecb5d0951d333371e)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.52 1999/10/13 20:38:40 bsmith Exp bsmith $";
3 #endif
4 
5 #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
6 
7 FList TSList              = 0;
8 int   TSRegisterAllCalled = 0;
9 
10 #undef __FUNC__
11 #define __FUNC__ "TSSetType"
12 /*@C
13    TSSetType - Sets the method for the timestepping solver.
14 
15    Collective on TS
16 
17    Input Parameters:
18 +  ts - the TS context
19 -  type - a known method
20 
21    Options Database Command:
22 .  -ts_type <type> - Sets the method; use -help for a list
23    of available methods (for instance, euler)
24 
25    Notes:
26    See "petsc/include/ts.h" for available methods (for instance)
27 +  TS_EULER - Euler
28 .  TS_PVODE - PVODE interface
29 .  TS_BEULER - Backward Euler
30 -  TS_PSEUDO - Pseudo-timestepping
31 
32    Normally, it is best to use the TSSetFromOptions() command and
33    then set the TS type from the options database rather than by using
34    this routine.  Using the options database provides the user with
35    maximum flexibility in evaluating the many different solvers.
36    The TSSetType() routine is provided for those situations where it
37    is necessary to set the timestepping solver independently of the
38    command line or options database.  This might be the case, for example,
39    when the choice of solver changes during the execution of the
40    program, and the user's application is taking responsibility for
41    choosing the appropriate method.  In other words, this routine is
42    not for beginners.
43 
44    Level: intermediate
45 
46 .keywords: TS, set, type
47 @*/
48 int TSSetType(TS ts,TSType type)
49 {
50   int        ierr,(*r)(TS);
51   PetscTruth match;
52 
53   PetscFunctionBegin;
54   PetscValidHeaderSpecific(ts,TS_COOKIE);
55   PetscValidCharPointer(type);
56 
57   ierr = PetscTypeCompare((PetscObject)ts,type,&match);CHKERRQ(ierr);
58   if (match) PetscFunctionReturn(0);
59 
60   /* Get the function pointers for the method requested */
61   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
62   ierr =  FListFind(ts->comm, TSList, type, (int (**)(void *)) &r );CHKERRQ(ierr);
63   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown type: %s",type);}
64 
65   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
66   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
67   if (ts->destroy) {ierr = (*(ts)->destroy)(ts);CHKERRQ(ierr);}
68   ts->sles = 0;
69   ts->snes = 0;
70 
71   ierr = (*r)(ts);CHKERRQ(ierr);
72 
73   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
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 FListAdd().
83 
84    Not Collective
85 
86    Level: advanced
87 
88 .keywords: TS, timestepper, register, destroy
89 
90 .seealso: TSRegisterAll()
91 @*/
92 int TSRegisterDestroy(void)
93 {
94   int ierr;
95 
96   PetscFunctionBegin;
97   if (TSList) {
98     ierr = FListDestroy( TSList );CHKERRQ(ierr);
99     TSList = 0;
100   }
101   TSRegisterAllCalled = 0;
102   PetscFunctionReturn(0);
103 }
104 
105 #undef __FUNC__
106 #define __FUNC__ "TSGetType"
107 /*@C
108    TSGetType - Gets the TS method type (as a string).
109 
110    Not Collective
111 
112    Input Parameter:
113 .  ts - timestepper solver context
114 
115    Output Parameter:
116 .  type - name of TS method
117 
118    Level: intermediate
119 
120 .keywords: TS, timestepper, get, type, name
121 @*/
122 int TSGetType(TS ts, TSType *type)
123 {
124   int ierr;
125 
126   PetscFunctionBegin;
127   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
128   *type = ts->type_name;
129   PetscFunctionReturn(0);
130 }
131 
132 #undef __FUNC__
133 #define __FUNC__ "TSPrintHelp"
134 /*@
135    TSPrintHelp - Prints all options for the TS (timestepping) component.
136 
137    Collective on TS
138 
139    Input Parameter:
140 .  ts - the TS context obtained from TSCreate()
141 
142    Options Database Keys:
143 +  -help - Prints KSP options
144 -  -h - Prints KSP options
145 
146    Level: beginner
147 
148 .keywords: TS, timestep, print, help
149 
150 .seealso: TSSetFromOptions()
151 @*/
152 int TSPrintHelp(TS ts)
153 {
154   char    *prefix = "-";
155   int     ierr;
156 
157   PetscFunctionBegin;
158   PetscValidHeaderSpecific(ts,TS_COOKIE);
159   if (ts->prefix) prefix = ts->prefix;
160   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
161   ierr = (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");CHKERRQ(ierr);
162   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
163   ierr = (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);CHKERRQ(ierr);
164   ierr = (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);CHKERRQ(ierr);
165 
166   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);CHKERRQ(ierr);
167   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);CHKERRQ(ierr);
168   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
169   PetscFunctionReturn(0);
170 }
171 
172 #undef __FUNC__
173 #define __FUNC__ "TSSetTypeFromOptions"
174 /*@
175    TSSetTypeFromOptions - Sets the TS type from the options database; sets
176      a default if none is given.
177 
178    Collective on TS
179 
180    Input Parameter:
181 .  ts - the TS context obtained from TSCreate()
182 
183    Options Database Keys:
184 .  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
185 
186    Level: beginner
187 
188 .keywords: TS, timestep, set, options, database, TS type
189 
190 .seealso: TSPrintHelp(), TSSetFromOptions()
191 @*/
192 int TSSetTypeFromOptions(TS ts)
193 {
194   int  ierr,flg;
195   char type[256];
196 
197   PetscFunctionBegin;
198   PetscValidHeaderSpecific(ts,TS_COOKIE);
199   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp()");
200   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);CHKERRQ(ierr);
201   if (flg) {
202     ierr = TSSetType(ts,type);CHKERRQ(ierr);
203   }
204   if (!ts->type_name) {
205     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
206   }
207   PetscFunctionReturn(0);
208 }
209 
210 #undef __FUNC__
211 #define __FUNC__ "TSSetFromOptions"
212 /*@
213    TSSetFromOptions - Sets various TS parameters from user options.
214 
215    Collective on TS
216 
217    Input Parameter:
218 .  ts - the TS context obtained from TSCreate()
219 
220    Options Database Keys:
221 +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
222 .  -ts_max_steps maxsteps - maximum number of time-steps to take
223 .  -ts_max_time time - maximum time to compute to
224 .  -ts_monitor - print information at each timestep
225 -  -ts_xmonitor - plot information at each timestep
226 
227    Level: beginner
228 
229 .keywords: TS, timestep, set, options, database
230 
231 .seealso: TSPrintHelp(), TSSetTypeFromOptions()
232 @*/
233 int TSSetFromOptions(TS ts)
234 {
235   int    ierr,flg,loc[4],nmax;
236 
237   PetscFunctionBegin;
238   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
239 
240   PetscValidHeaderSpecific(ts,TS_COOKIE);
241   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
242 
243   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
244   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
245   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg);CHKERRQ(ierr);
246   if (flg) {
247     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
248   }
249   nmax = 4;
250   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
251   if (flg) {
252     int    rank = 0;
253     DrawLG lg;
254     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
255     if (!rank) {
256       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr);
257       PLogObjectParent(ts,(PetscObject) lg);
258       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
259     }
260   }
261   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
262   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
263   if (ts->setfromoptions) {
264     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
265   }
266   PetscFunctionReturn(0);
267 }
268 
269 
270 
271