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