xref: /petsc/src/ts/interface/tsreg.c (revision 454a90a3eb7bca6958262e5eca1eb393ad97e108)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*454a90a3SBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.50 1999/09/27 21:32:04 bsmith Exp bsmith $";
33f3760d9SBarry Smith #endif
43f3760d9SBarry Smith 
570f55243SBarry Smith #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
63f3760d9SBarry Smith 
7488ecbafSBarry Smith FList TSList              = 0;
884cb2905SBarry Smith int   TSRegisterAllCalled = 0;
93f3760d9SBarry Smith 
105615d1e5SSatish Balay #undef __FUNC__
11d4bb536fSBarry Smith #define __FUNC__ "TSSetType"
1282bf6240SBarry Smith /*@C
13ae12b187SLois Curfman McInnes    TSSetType - Sets the method for the timestepping solver.
143f3760d9SBarry Smith 
15fee21e36SBarry Smith    Collective on TS
16fee21e36SBarry Smith 
17bef22f13SLois Curfman McInnes    Input Parameters:
18bef22f13SLois Curfman McInnes +  ts - the TS context
19*454a90a3SBarry Smith -  type - a known method
20bef22f13SLois Curfman McInnes 
21ae12b187SLois Curfman McInnes    Options Database Command:
22*454a90a3SBarry Smith .  -ts_type <type> - Sets the method; use -help for a list
23bef22f13SLois Curfman McInnes    of available methods (for instance, euler)
24ae12b187SLois Curfman McInnes 
253f3760d9SBarry Smith    Notes:
268b1af7b3SBarry Smith    See "petsc/include/ts.h" for available methods (for instance)
27d5d37b61SLois Curfman McInnes +  TS_EULER - Euler
28bef22f13SLois Curfman McInnes .  TS_PVODE - PVODE interface
29bef22f13SLois Curfman McInnes .  TS_BEULER - Backward Euler
30d5d37b61SLois Curfman McInnes -  TS_PSEUDO - Pseudo-timestepping
313f3760d9SBarry Smith 
32ae12b187SLois Curfman McInnes    Normally, it is best to use the TSSetFromOptions() command and
33ae12b187SLois Curfman McInnes    then set the TS type from the options database rather than by using
34ae12b187SLois Curfman McInnes    this routine.  Using the options database provides the user with
35ae12b187SLois Curfman McInnes    maximum flexibility in evaluating the many different solvers.
36ae12b187SLois Curfman McInnes    The TSSetType() routine is provided for those situations where it
37ae12b187SLois Curfman McInnes    is necessary to set the timestepping solver independently of the
38ae12b187SLois Curfman McInnes    command line or options database.  This might be the case, for example,
39ae12b187SLois Curfman McInnes    when the choice of solver changes during the execution of the
40ae12b187SLois Curfman McInnes    program, and the user's application is taking responsibility for
41ae12b187SLois Curfman McInnes    choosing the appropriate method.  In other words, this routine is
42d5d37b61SLois Curfman McInnes    not for beginners.
43d5d37b61SLois Curfman McInnes 
44d5d37b61SLois Curfman McInnes    Level: intermediate
453f3760d9SBarry Smith 
46ae12b187SLois Curfman McInnes .keywords: TS, set, type
473f3760d9SBarry Smith @*/
48*454a90a3SBarry Smith int TSSetType(TS ts,TSType type)
493f3760d9SBarry Smith {
50d83d6502SBarry Smith   int ierr,(*r)(TS);
518b1af7b3SBarry Smith 
523a40ed3dSBarry Smith   PetscFunctionBegin;
53c3e30b67SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
54*454a90a3SBarry Smith   if (PetscTypeCompare(ts,type)) PetscFunctionReturn(0);
55df8cb225SBarry Smith 
568b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
5782bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
58*454a90a3SBarry Smith   ierr =  FListFind(ts->comm, TSList, type, (int (**)(void *)) &r );CHKERRQ(ierr);
59*454a90a3SBarry Smith   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown type: %s",type);}
6082bf6240SBarry Smith 
61df8cb225SBarry Smith   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
62df8cb225SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
63e1311b90SBarry Smith   if (ts->destroy) {ierr = (*(ts)->destroy)(ts);CHKERRQ(ierr);}
64df8cb225SBarry Smith   ts->sles = 0;
65df8cb225SBarry Smith   ts->snes = 0;
6682bf6240SBarry Smith 
673a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
68df8cb225SBarry Smith 
69*454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
703a40ed3dSBarry Smith   PetscFunctionReturn(0);
713f3760d9SBarry Smith }
72df8cb225SBarry Smith 
733f3760d9SBarry Smith /* --------------------------------------------------------------------- */
745615d1e5SSatish Balay #undef __FUNC__
75d4bb536fSBarry Smith #define __FUNC__ "TSRegisterDestroy"
763f3760d9SBarry Smith /*@C
7784cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
78488ecbafSBarry Smith    registered by FListAdd().
793f3760d9SBarry Smith 
80fee21e36SBarry Smith    Not Collective
81fee21e36SBarry Smith 
82d5d37b61SLois Curfman McInnes    Level: advanced
83d5d37b61SLois Curfman McInnes 
842d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
853f3760d9SBarry Smith 
8682bf6240SBarry Smith .seealso: TSRegisterAll()
873f3760d9SBarry Smith @*/
88cf256101SBarry Smith int TSRegisterDestroy(void)
893f3760d9SBarry Smith {
90df8cb225SBarry Smith   int ierr;
91df8cb225SBarry Smith 
923a40ed3dSBarry Smith   PetscFunctionBegin;
9382bf6240SBarry Smith   if (TSList) {
94488ecbafSBarry Smith     ierr = FListDestroy( TSList );CHKERRQ(ierr);
9582bf6240SBarry Smith     TSList = 0;
963f3760d9SBarry Smith   }
9784cb2905SBarry Smith   TSRegisterAllCalled = 0;
983a40ed3dSBarry Smith   PetscFunctionReturn(0);
993f3760d9SBarry Smith }
1003f3760d9SBarry Smith 
1015615d1e5SSatish Balay #undef __FUNC__
102d4bb536fSBarry Smith #define __FUNC__ "TSGetType"
1033f3760d9SBarry Smith /*@C
104fee21e36SBarry Smith    TSGetType - Gets the TS method type (as a string).
1053f3760d9SBarry Smith 
106bef22f13SLois Curfman McInnes    Not Collective
107bef22f13SLois Curfman McInnes 
1083f3760d9SBarry Smith    Input Parameter:
1092d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1103f3760d9SBarry Smith 
1113f3760d9SBarry Smith    Output Parameter:
11282bf6240SBarry Smith .  type - name of TS method
1133f3760d9SBarry Smith 
114d5d37b61SLois Curfman McInnes    Level: intermediate
115d5d37b61SLois Curfman McInnes 
116df8cb225SBarry Smith .keywords: TS, timestepper, get, type, name
1173f3760d9SBarry Smith @*/
11882bf6240SBarry Smith int TSGetType(TS ts, TSType *type)
1193f3760d9SBarry Smith {
1203f3760d9SBarry Smith   int ierr;
1213a40ed3dSBarry Smith 
1223a40ed3dSBarry Smith   PetscFunctionBegin;
12382bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
12482bf6240SBarry Smith   *type = ts->type_name;
1253a40ed3dSBarry Smith   PetscFunctionReturn(0);
1263f3760d9SBarry Smith }
1273f3760d9SBarry Smith 
1285615d1e5SSatish Balay #undef __FUNC__
129ca161407SBarry Smith #define __FUNC__ "TSPrintHelp"
130ca161407SBarry Smith /*@
131ca161407SBarry Smith    TSPrintHelp - Prints all options for the TS (timestepping) component.
1323f3760d9SBarry Smith 
133bef22f13SLois Curfman McInnes    Collective on TS
134bef22f13SLois Curfman McInnes 
1353f3760d9SBarry Smith    Input Parameter:
136ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
1373f3760d9SBarry Smith 
138ca161407SBarry Smith    Options Database Keys:
139bef22f13SLois Curfman McInnes +  -help - Prints KSP options
140bef22f13SLois Curfman McInnes -  -h - Prints KSP options
141fee21e36SBarry Smith 
142d5d37b61SLois Curfman McInnes    Level: beginner
143d5d37b61SLois Curfman McInnes 
144ca161407SBarry Smith .keywords: TS, timestep, print, help
145ca161407SBarry Smith 
146ca161407SBarry Smith .seealso: TSSetFromOptions()
147ca161407SBarry Smith @*/
148ca161407SBarry Smith int TSPrintHelp(TS ts)
1493f3760d9SBarry Smith {
150ca161407SBarry Smith   char    *prefix = "-";
1518b1af7b3SBarry Smith   int     ierr;
1523a40ed3dSBarry Smith 
1533a40ed3dSBarry Smith   PetscFunctionBegin;
154ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
155ca161407SBarry Smith   if (ts->prefix) prefix = ts->prefix;
156ebb8b11fSBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
157d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");CHKERRQ(ierr);
158488ecbafSBarry Smith   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
159d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);CHKERRQ(ierr);
160d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);CHKERRQ(ierr);
161ca161407SBarry Smith 
162d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);CHKERRQ(ierr);
163d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);CHKERRQ(ierr);
164ca161407SBarry Smith   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
1653a40ed3dSBarry Smith   PetscFunctionReturn(0);
1663f3760d9SBarry Smith }
167ca161407SBarry Smith 
168ca161407SBarry Smith #undef __FUNC__
16915091d37SBarry Smith #define __FUNC__ "TSSetTypeFromOptions"
17015091d37SBarry Smith /*@
17115091d37SBarry Smith    TSSetTypeFromOptions - Sets the TS type from the options database; sets
17215091d37SBarry Smith      a default if none is given.
17315091d37SBarry Smith 
17415091d37SBarry Smith    Collective on TS
17515091d37SBarry Smith 
17615091d37SBarry Smith    Input Parameter:
17715091d37SBarry Smith .  ts - the TS context obtained from TSCreate()
17815091d37SBarry Smith 
17915091d37SBarry Smith    Options Database Keys:
18015091d37SBarry Smith .  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
18115091d37SBarry Smith 
18215091d37SBarry Smith    Level: beginner
18315091d37SBarry Smith 
18415091d37SBarry Smith .keywords: TS, timestep, set, options, database, TS type
18515091d37SBarry Smith 
18615091d37SBarry Smith .seealso: TSPrintHelp(), TSSetFromOptions()
18715091d37SBarry Smith @*/
18815091d37SBarry Smith int TSSetTypeFromOptions(TS ts)
18915091d37SBarry Smith {
19015091d37SBarry Smith   int  ierr,flg;
19115091d37SBarry Smith   char type[256];
19215091d37SBarry Smith 
19315091d37SBarry Smith   PetscFunctionBegin;
19415091d37SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
19515091d37SBarry Smith   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp()");
196888f2ed8SSatish Balay   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);CHKERRQ(ierr);
19715091d37SBarry Smith   if (flg) {
19815091d37SBarry Smith     ierr = TSSetType(ts,type);CHKERRQ(ierr);
19915091d37SBarry Smith   }
20015091d37SBarry Smith   if (!ts->type_name) {
20115091d37SBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
20215091d37SBarry Smith   }
20315091d37SBarry Smith   PetscFunctionReturn(0);
20415091d37SBarry Smith }
20515091d37SBarry Smith 
20615091d37SBarry Smith #undef __FUNC__
207ca161407SBarry Smith #define __FUNC__ "TSSetFromOptions"
208ca161407SBarry Smith /*@
209ca161407SBarry Smith    TSSetFromOptions - Sets various TS parameters from user options.
210ca161407SBarry Smith 
211bef22f13SLois Curfman McInnes    Collective on TS
212bef22f13SLois Curfman McInnes 
213ca161407SBarry Smith    Input Parameter:
214ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
215ca161407SBarry Smith 
21615091d37SBarry Smith    Options Database Keys:
21715091d37SBarry Smith +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
21815091d37SBarry Smith .  -ts_max_steps maxsteps - maximum number of time-steps to take
21915091d37SBarry Smith .  -ts_max_time time - maximum time to compute to
22015091d37SBarry Smith .  -ts_monitor - print information at each timestep
22115091d37SBarry Smith -  -ts_xmonitor - plot information at each timestep
22215091d37SBarry Smith 
223d5d37b61SLois Curfman McInnes    Level: beginner
224d5d37b61SLois Curfman McInnes 
225ca161407SBarry Smith .keywords: TS, timestep, set, options, database
226ca161407SBarry Smith 
22715091d37SBarry Smith .seealso: TSPrintHelp(), TSSetTypeFromOptions()
228ca161407SBarry Smith @*/
229ca161407SBarry Smith int TSSetFromOptions(TS ts)
230ca161407SBarry Smith {
231ca161407SBarry Smith   int    ierr,flg,loc[4],nmax;
232ca161407SBarry Smith 
233ca161407SBarry Smith   PetscFunctionBegin;
234ca161407SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
235ca161407SBarry Smith 
236ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
23715091d37SBarry Smith   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
238ca161407SBarry Smith 
239ca161407SBarry Smith   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
240ca161407SBarry Smith   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
241ca161407SBarry Smith   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg);CHKERRQ(ierr);
242ca161407SBarry Smith   if (flg) {
243ca161407SBarry Smith     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
244ca161407SBarry Smith   }
245ca161407SBarry Smith   nmax = 4;
246ca161407SBarry Smith   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
247ca161407SBarry Smith   if (flg) {
248ca161407SBarry Smith     int    rank = 0;
249ca161407SBarry Smith     DrawLG lg;
250d132466eSBarry Smith     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
251ca161407SBarry Smith     if (!rank) {
252ca161407SBarry Smith       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr);
253ca161407SBarry Smith       PLogObjectParent(ts,(PetscObject) lg);
254ca161407SBarry Smith       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
255ca161407SBarry Smith     }
256ca161407SBarry Smith   }
2576a6a5d1dSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
2586a6a5d1dSBarry Smith   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
259184914b5SBarry Smith   if (ts->setfromoptions) {
260ca161407SBarry Smith     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
261184914b5SBarry Smith   }
262ca161407SBarry Smith   PetscFunctionReturn(0);
263ca161407SBarry Smith }
264ca161407SBarry Smith 
26582bf6240SBarry Smith 
266184914b5SBarry Smith 
267