xref: /petsc/src/ts/interface/tsreg.c (revision ac2a4f0d24b3b6a4ee93edbcad41f4bb9e923944)
1 /*$Id: tsreg.c,v 1.52 1999/10/13 20:38:40 bsmith Exp bsmith $*/
2 
3 #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
4 
5 FList TSList              = 0;
6 int   TSRegisterAllCalled = 0;
7 
8 #undef __FUNC__
9 #define __FUNC__ "TSSetType"
10 /*@C
11    TSSetType - Sets the method for the timestepping solver.
12 
13    Collective on TS
14 
15    Input Parameters:
16 +  ts - the TS context
17 -  type - a known method
18 
19    Options Database Command:
20 .  -ts_type <type> - Sets the method; use -help for a list
21    of available methods (for instance, euler)
22 
23    Notes:
24    See "petsc/include/ts.h" for available methods (for instance)
25 +  TS_EULER - Euler
26 .  TS_PVODE - PVODE interface
27 .  TS_BEULER - Backward Euler
28 -  TS_PSEUDO - Pseudo-timestepping
29 
30    Normally, it is best to use the TSSetFromOptions() command and
31    then set the TS type from the options database rather than by using
32    this routine.  Using the options database provides the user with
33    maximum flexibility in evaluating the many different solvers.
34    The TSSetType() routine is provided for those situations where it
35    is necessary to set the timestepping solver independently of the
36    command line or options database.  This might be the case, for example,
37    when the choice of solver changes during the execution of the
38    program, and the user's application is taking responsibility for
39    choosing the appropriate method.  In other words, this routine is
40    not for beginners.
41 
42    Level: intermediate
43 
44 .keywords: TS, set, type
45 @*/
46 int TSSetType(TS ts,TSType type)
47 {
48   int        ierr,(*r)(TS);
49   PetscTruth match;
50 
51   PetscFunctionBegin;
52   PetscValidHeaderSpecific(ts,TS_COOKIE);
53   PetscValidCharPointer(type);
54 
55   ierr = PetscTypeCompare((PetscObject)ts,type,&match);CHKERRQ(ierr);
56   if (match) PetscFunctionReturn(0);
57 
58   /* Get the function pointers for the method requested */
59   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
60   ierr =  FListFind(ts->comm, TSList, type, (int (**)(void *)) &r );CHKERRQ(ierr);
61   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown type: %s",type);}
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   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
72   PetscFunctionReturn(0);
73 }
74 
75 /* --------------------------------------------------------------------- */
76 #undef __FUNC__
77 #define __FUNC__ "TSRegisterDestroy"
78 /*@C
79    TSRegisterDestroy - Frees the list of timesteppers that were
80    registered by FListAdd().
81 
82    Not Collective
83 
84    Level: advanced
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 = FListDestroy( 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    Not Collective
109 
110    Input Parameter:
111 .  ts - timestepper solver context
112 
113    Output Parameter:
114 .  type - name of TS method
115 
116    Level: intermediate
117 
118 .keywords: TS, timestepper, get, type, name
119 @*/
120 int TSGetType(TS ts, TSType *type)
121 {
122   int ierr;
123 
124   PetscFunctionBegin;
125   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
126   *type = ts->type_name;
127   PetscFunctionReturn(0);
128 }
129 
130 #undef __FUNC__
131 #define __FUNC__ "TSPrintHelp"
132 /*@
133    TSPrintHelp - Prints all options for the TS (timestepping) component.
134 
135    Collective on TS
136 
137    Input Parameter:
138 .  ts - the TS context obtained from TSCreate()
139 
140    Options Database Keys:
141 +  -help - Prints KSP options
142 -  -h - Prints KSP options
143 
144    Level: beginner
145 
146 .keywords: TS, timestep, print, help
147 
148 .seealso: TSSetFromOptions()
149 @*/
150 int TSPrintHelp(TS ts)
151 {
152   char    *prefix = "-";
153   int     ierr;
154 
155   PetscFunctionBegin;
156   PetscValidHeaderSpecific(ts,TS_COOKIE);
157   if (ts->prefix) prefix = ts->prefix;
158   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
159   ierr = (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");CHKERRQ(ierr);
160   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
161   ierr = (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);CHKERRQ(ierr);
162   ierr = (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);CHKERRQ(ierr);
163 
164   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);CHKERRQ(ierr);
165   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);CHKERRQ(ierr);
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);CHKERRQ(ierr);
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     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
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) {
262     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
263   }
264   PetscFunctionReturn(0);
265 }
266 
267 
268 
269