xref: /petsc/src/ts/interface/tsreg.c (revision 15091d3721b14bd18f7efa625bb8738e103dca31)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.41 1999/02/01 03:41:50 curfman 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   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
160   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
161   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
162   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
163 
164   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
165   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
166   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
167   PetscFunctionReturn(0);
168 }
169 
170 #undef __FUNC__
171 #define __FUNC__ "TSSetTypeFromOptions"
172 /*@
173    TSSetTypeFromOptions - Sets the TS type from the options database; sets
174      a default if none is given.
175 
176    Collective on TS
177 
178    Input Parameter:
179 .  ts - the TS context obtained from TSCreate()
180 
181    Options Database Keys:
182 .  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
183 
184    Level: beginner
185 
186 .keywords: TS, timestep, set, options, database, TS type
187 
188 .seealso: TSPrintHelp(), TSSetFromOptions()
189 @*/
190 int TSSetTypeFromOptions(TS ts)
191 {
192   int  ierr,flg;
193   char type[256];
194 
195   PetscFunctionBegin;
196   PetscValidHeaderSpecific(ts,TS_COOKIE);
197   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp()");
198   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);
199   if (flg) {
200     ierr = TSSetType(ts,type); CHKERRQ(ierr);
201   }
202   if (!ts->type_name) {
203     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
204   }
205   PetscFunctionReturn(0);
206 }
207 
208 #undef __FUNC__
209 #define __FUNC__ "TSSetFromOptions"
210 /*@
211    TSSetFromOptions - Sets various TS parameters from user options.
212 
213    Collective on TS
214 
215    Input Parameter:
216 .  ts - the TS context obtained from TSCreate()
217 
218    Options Database Keys:
219 +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
220 .  -ts_max_steps maxsteps - maximum number of time-steps to take
221 .  -ts_max_time time - maximum time to compute to
222 .  -ts_monitor - print information at each timestep
223 -  -ts_xmonitor - plot information at each timestep
224 
225    Level: beginner
226 
227 .keywords: TS, timestep, set, options, database
228 
229 .seealso: TSPrintHelp(), TSSetTypeFromOptions()
230 @*/
231 int TSSetFromOptions(TS ts)
232 {
233   int    ierr,flg,loc[4],nmax;
234 
235   PetscFunctionBegin;
236   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
237 
238   PetscValidHeaderSpecific(ts,TS_COOKIE);
239   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
240 
241   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
242   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
243   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
244   if (flg) {
245     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
246   }
247   nmax = 4;
248   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
249   if (flg) {
250     int    rank = 0;
251     DrawLG lg;
252     MPI_Comm_rank(ts->comm,&rank);
253     if (!rank) {
254       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
255       PLogObjectParent(ts,(PetscObject) lg);
256       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
257     }
258   }
259   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
260   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
261   if (!ts->setfromoptions) PetscFunctionReturn(0);
262   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
263   PetscFunctionReturn(0);
264 }
265 
266 
267