xref: /petsc/src/ts/interface/tsreg.c (revision e090d5668ba2b2ea997ebb925e3a05be0dc5d9ab)
1 /*$Id: tsreg.c,v 1.60 2000/04/12 04:25:55 bsmith Exp balay $*/
2 
3 #include "src/ts/tsimpl.h"      /*I "petscts.h"  I*/
4 
5 FList      TSList              = 0;
6 PetscTruth TSRegisterAllCalled = PETSC_FALSE;
7 
8 #undef __FUNC__
9 #define __FUNC__ /*<a name=""></a>*/"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/petscts.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__ /*<a name=""></a>*/"TSRegisterDestroy"
78 /*@C
79    TSRegisterDestroy - Frees the list of timesteppers that were
80    registered by FListAddDynamic().
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 = PETSC_FALSE;
100   PetscFunctionReturn(0);
101 }
102 
103 #undef __FUNC__
104 #define __FUNC__ /*<a name=""></a>*/"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__ /*<a name=""></a>*/"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__ /*<a name=""></a>*/"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;
193   PetscTruth 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);CHKERRQ(ierr);
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__ /*<a name=""></a>*/"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,loc[4],nmax;
235   PetscTruth flg;
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,PETSC_NULL);CHKERRQ(ierr);
244   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
245   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg);CHKERRQ(ierr);
246   if (flg) {
247     ierr = TSSetMonitor(ts,TSDefaultMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
248   }
249   nmax = 4;
250   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
251   if (flg) {
252     int    rank;
253     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
254     if (!rank) {
255       ierr = TSSetMonitor(ts,TSLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
256     }
257   }
258   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
259   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
260   if (ts->setfromoptions) {
261     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
262   }
263   PetscFunctionReturn(0);
264 }
265 
266 
267 
268