xref: /petsc/src/ts/interface/tsreg.c (revision e090d5668ba2b2ea997ebb925e3a05be0dc5d9ab)
1*e090d566SSatish Balay /*$Id: tsreg.c,v 1.60 2000/04/12 04:25:55 bsmith Exp balay $*/
23f3760d9SBarry Smith 
3*e090d566SSatish Balay #include "src/ts/tsimpl.h"      /*I "petscts.h"  I*/
43f3760d9SBarry Smith 
5488ecbafSBarry Smith FList      TSList              = 0;
64c49b128SBarry Smith PetscTruth TSRegisterAllCalled = PETSC_FALSE;
73f3760d9SBarry Smith 
85615d1e5SSatish Balay #undef __FUNC__
9b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSSetType"
1082bf6240SBarry Smith /*@C
11ae12b187SLois Curfman McInnes    TSSetType - Sets the method for the timestepping solver.
123f3760d9SBarry Smith 
13fee21e36SBarry Smith    Collective on TS
14fee21e36SBarry Smith 
15bef22f13SLois Curfman McInnes    Input Parameters:
16bef22f13SLois Curfman McInnes +  ts - the TS context
17454a90a3SBarry Smith -  type - a known method
18bef22f13SLois Curfman McInnes 
19ae12b187SLois Curfman McInnes    Options Database Command:
20454a90a3SBarry Smith .  -ts_type <type> - Sets the method; use -help for a list
21bef22f13SLois Curfman McInnes    of available methods (for instance, euler)
22ae12b187SLois Curfman McInnes 
233f3760d9SBarry Smith    Notes:
24*e090d566SSatish Balay    See "petsc/include/petscts.h" for available methods (for instance)
25d5d37b61SLois Curfman McInnes +  TS_EULER - Euler
26bef22f13SLois Curfman McInnes .  TS_PVODE - PVODE interface
27bef22f13SLois Curfman McInnes .  TS_BEULER - Backward Euler
28d5d37b61SLois Curfman McInnes -  TS_PSEUDO - Pseudo-timestepping
293f3760d9SBarry Smith 
30ae12b187SLois Curfman McInnes    Normally, it is best to use the TSSetFromOptions() command and
31ae12b187SLois Curfman McInnes    then set the TS type from the options database rather than by using
32ae12b187SLois Curfman McInnes    this routine.  Using the options database provides the user with
33ae12b187SLois Curfman McInnes    maximum flexibility in evaluating the many different solvers.
34ae12b187SLois Curfman McInnes    The TSSetType() routine is provided for those situations where it
35ae12b187SLois Curfman McInnes    is necessary to set the timestepping solver independently of the
36ae12b187SLois Curfman McInnes    command line or options database.  This might be the case, for example,
37ae12b187SLois Curfman McInnes    when the choice of solver changes during the execution of the
38ae12b187SLois Curfman McInnes    program, and the user's application is taking responsibility for
39ae12b187SLois Curfman McInnes    choosing the appropriate method.  In other words, this routine is
40d5d37b61SLois Curfman McInnes    not for beginners.
41d5d37b61SLois Curfman McInnes 
42d5d37b61SLois Curfman McInnes    Level: intermediate
433f3760d9SBarry Smith 
44ae12b187SLois Curfman McInnes .keywords: TS, set, type
453f3760d9SBarry Smith @*/
46454a90a3SBarry Smith int TSSetType(TS ts,TSType type)
473f3760d9SBarry Smith {
48d83d6502SBarry Smith   int        ierr,(*r)(TS);
496831982aSBarry Smith   PetscTruth match;
508b1af7b3SBarry Smith 
513a40ed3dSBarry Smith   PetscFunctionBegin;
52c3e30b67SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
530f5bd95cSBarry Smith   PetscValidCharPointer(type);
540f5bd95cSBarry Smith 
556831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ts,type,&match);CHKERRQ(ierr);
560f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
57df8cb225SBarry Smith 
588b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
5982bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
60454a90a3SBarry Smith   ierr =  FListFind(ts->comm,TSList,type,(int (**)(void *)) &r);CHKERRQ(ierr);
61454a90a3SBarry Smith   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown type: %s",type);}
6282bf6240SBarry Smith 
63df8cb225SBarry Smith   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
64df8cb225SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
65e1311b90SBarry Smith   if (ts->destroy) {ierr = (*(ts)->destroy)(ts);CHKERRQ(ierr);}
66df8cb225SBarry Smith   ts->sles = 0;
67df8cb225SBarry Smith   ts->snes = 0;
6882bf6240SBarry Smith 
693a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
70df8cb225SBarry Smith 
71454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
723a40ed3dSBarry Smith   PetscFunctionReturn(0);
733f3760d9SBarry Smith }
74df8cb225SBarry Smith 
753f3760d9SBarry Smith /* --------------------------------------------------------------------- */
765615d1e5SSatish Balay #undef __FUNC__
77b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSRegisterDestroy"
783f3760d9SBarry Smith /*@C
7984cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
80f1af5d2fSBarry Smith    registered by FListAddDynamic().
813f3760d9SBarry Smith 
82fee21e36SBarry Smith    Not Collective
83fee21e36SBarry Smith 
84d5d37b61SLois Curfman McInnes    Level: advanced
85d5d37b61SLois Curfman McInnes 
862d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
873f3760d9SBarry Smith 
8882bf6240SBarry Smith .seealso: TSRegisterAll()
893f3760d9SBarry Smith @*/
90cf256101SBarry Smith int TSRegisterDestroy(void)
913f3760d9SBarry Smith {
92df8cb225SBarry Smith   int ierr;
93df8cb225SBarry Smith 
943a40ed3dSBarry Smith   PetscFunctionBegin;
9582bf6240SBarry Smith   if (TSList) {
96488ecbafSBarry Smith     ierr = FListDestroy(TSList);CHKERRQ(ierr);
9782bf6240SBarry Smith     TSList = 0;
983f3760d9SBarry Smith   }
994c49b128SBarry Smith   TSRegisterAllCalled = PETSC_FALSE;
1003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1013f3760d9SBarry Smith }
1023f3760d9SBarry Smith 
1035615d1e5SSatish Balay #undef __FUNC__
104b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSGetType"
1053f3760d9SBarry Smith /*@C
106fee21e36SBarry Smith    TSGetType - Gets the TS method type (as a string).
1073f3760d9SBarry Smith 
108bef22f13SLois Curfman McInnes    Not Collective
109bef22f13SLois Curfman McInnes 
1103f3760d9SBarry Smith    Input Parameter:
1112d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1123f3760d9SBarry Smith 
1133f3760d9SBarry Smith    Output Parameter:
11482bf6240SBarry Smith .  type - name of TS method
1153f3760d9SBarry Smith 
116d5d37b61SLois Curfman McInnes    Level: intermediate
117d5d37b61SLois Curfman McInnes 
118df8cb225SBarry Smith .keywords: TS, timestepper, get, type, name
1193f3760d9SBarry Smith @*/
12082bf6240SBarry Smith int TSGetType(TS ts,TSType *type)
1213f3760d9SBarry Smith {
1223f3760d9SBarry Smith   int ierr;
1233a40ed3dSBarry Smith 
1243a40ed3dSBarry Smith   PetscFunctionBegin;
12582bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
12682bf6240SBarry Smith   *type = ts->type_name;
1273a40ed3dSBarry Smith   PetscFunctionReturn(0);
1283f3760d9SBarry Smith }
1293f3760d9SBarry Smith 
1305615d1e5SSatish Balay #undef __FUNC__
131b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSPrintHelp"
132ca161407SBarry Smith /*@
133ca161407SBarry Smith    TSPrintHelp - Prints all options for the TS (timestepping) component.
1343f3760d9SBarry Smith 
135bef22f13SLois Curfman McInnes    Collective on TS
136bef22f13SLois Curfman McInnes 
1373f3760d9SBarry Smith    Input Parameter:
138ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
1393f3760d9SBarry Smith 
140ca161407SBarry Smith    Options Database Keys:
141bef22f13SLois Curfman McInnes +  -help - Prints KSP options
142bef22f13SLois Curfman McInnes -  -h - Prints KSP options
143fee21e36SBarry Smith 
144d5d37b61SLois Curfman McInnes    Level: beginner
145d5d37b61SLois Curfman McInnes 
146ca161407SBarry Smith .keywords: TS, timestep, print, help
147ca161407SBarry Smith 
148ca161407SBarry Smith .seealso: TSSetFromOptions()
149ca161407SBarry Smith @*/
150ca161407SBarry Smith int TSPrintHelp(TS ts)
1513f3760d9SBarry Smith {
152ca161407SBarry Smith   char    *prefix = "-";
1538b1af7b3SBarry Smith   int     ierr;
1543a40ed3dSBarry Smith 
1553a40ed3dSBarry Smith   PetscFunctionBegin;
156ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
157ca161407SBarry Smith   if (ts->prefix) prefix = ts->prefix;
158ebb8b11fSBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
159d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");CHKERRQ(ierr);
160488ecbafSBarry Smith   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
161d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);CHKERRQ(ierr);
162d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);CHKERRQ(ierr);
163ca161407SBarry Smith 
164d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);CHKERRQ(ierr);
165d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);CHKERRQ(ierr);
166ca161407SBarry Smith   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
1673a40ed3dSBarry Smith   PetscFunctionReturn(0);
1683f3760d9SBarry Smith }
169ca161407SBarry Smith 
170ca161407SBarry Smith #undef __FUNC__
171b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSSetTypeFromOptions"
17215091d37SBarry Smith /*@
17315091d37SBarry Smith    TSSetTypeFromOptions - Sets the TS type from the options database; sets
17415091d37SBarry Smith      a default if none is given.
17515091d37SBarry Smith 
17615091d37SBarry Smith    Collective on TS
17715091d37SBarry Smith 
17815091d37SBarry Smith    Input Parameter:
17915091d37SBarry Smith .  ts - the TS context obtained from TSCreate()
18015091d37SBarry Smith 
18115091d37SBarry Smith    Options Database Keys:
18215091d37SBarry Smith .  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
18315091d37SBarry Smith 
18415091d37SBarry Smith    Level: beginner
18515091d37SBarry Smith 
18615091d37SBarry Smith .keywords: TS, timestep, set, options, database, TS type
18715091d37SBarry Smith 
18815091d37SBarry Smith .seealso: TSPrintHelp(), TSSetFromOptions()
18915091d37SBarry Smith @*/
19015091d37SBarry Smith int TSSetTypeFromOptions(TS ts)
19115091d37SBarry Smith {
192f1af5d2fSBarry Smith   int        ierr;
193f1af5d2fSBarry Smith   PetscTruth flg;
19415091d37SBarry Smith   char       type[256];
19515091d37SBarry Smith 
19615091d37SBarry Smith   PetscFunctionBegin;
19715091d37SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
19815091d37SBarry Smith   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp()");
199888f2ed8SSatish Balay   ierr = OptionsGetString(ts->prefix,"-ts_type",(char*)type,256,&flg);CHKERRQ(ierr);
20015091d37SBarry Smith   if (flg) {
20115091d37SBarry Smith     ierr = TSSetType(ts,type);CHKERRQ(ierr);
20215091d37SBarry Smith   }
20315091d37SBarry Smith   if (!ts->type_name) {
20415091d37SBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
20515091d37SBarry Smith   }
20615091d37SBarry Smith   PetscFunctionReturn(0);
20715091d37SBarry Smith }
20815091d37SBarry Smith 
20915091d37SBarry Smith #undef __FUNC__
210b2863d3aSBarry Smith #define __FUNC__ /*<a name=""></a>*/"TSSetFromOptions"
211ca161407SBarry Smith /*@
212ca161407SBarry Smith    TSSetFromOptions - Sets various TS parameters from user options.
213ca161407SBarry Smith 
214bef22f13SLois Curfman McInnes    Collective on TS
215bef22f13SLois Curfman McInnes 
216ca161407SBarry Smith    Input Parameter:
217ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
218ca161407SBarry Smith 
21915091d37SBarry Smith    Options Database Keys:
22015091d37SBarry Smith +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
22115091d37SBarry Smith .  -ts_max_steps maxsteps - maximum number of time-steps to take
22215091d37SBarry Smith .  -ts_max_time time - maximum time to compute to
22315091d37SBarry Smith .  -ts_monitor - print information at each timestep
22415091d37SBarry Smith -  -ts_xmonitor - plot information at each timestep
22515091d37SBarry Smith 
226d5d37b61SLois Curfman McInnes    Level: beginner
227d5d37b61SLois Curfman McInnes 
228ca161407SBarry Smith .keywords: TS, timestep, set, options, database
229ca161407SBarry Smith 
23015091d37SBarry Smith .seealso: TSPrintHelp(), TSSetTypeFromOptions()
231ca161407SBarry Smith @*/
232ca161407SBarry Smith int TSSetFromOptions(TS ts)
233ca161407SBarry Smith {
234f1af5d2fSBarry Smith   int        ierr,loc[4],nmax;
235f1af5d2fSBarry Smith   PetscTruth flg;
236ca161407SBarry Smith 
237ca161407SBarry Smith   PetscFunctionBegin;
238ca161407SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
239ca161407SBarry Smith 
240ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
24115091d37SBarry Smith   ierr = TSSetTypeFromOptions(ts);CHKERRQ(ierr);
242ca161407SBarry Smith 
243f1af5d2fSBarry Smith   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
244f1af5d2fSBarry Smith   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
245ca161407SBarry Smith   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg);CHKERRQ(ierr);
246ca161407SBarry Smith   if (flg) {
247329f5518SBarry Smith     ierr = TSSetMonitor(ts,TSDefaultMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
248ca161407SBarry Smith   }
249ca161407SBarry Smith   nmax = 4;
250ca161407SBarry Smith   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg);CHKERRQ(ierr);
251ca161407SBarry Smith   if (flg) {
2527c922b88SBarry Smith     int    rank;
253d132466eSBarry Smith     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
254ca161407SBarry Smith     if (!rank) {
255329f5518SBarry Smith       ierr = TSSetMonitor(ts,TSLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
256ca161407SBarry Smith     }
257ca161407SBarry Smith   }
2586a6a5d1dSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
2596a6a5d1dSBarry Smith   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
260184914b5SBarry Smith   if (ts->setfromoptions) {
261ca161407SBarry Smith     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
262184914b5SBarry Smith   }
263ca161407SBarry Smith   PetscFunctionReturn(0);
264ca161407SBarry Smith }
265ca161407SBarry Smith 
26682bf6240SBarry Smith 
267184914b5SBarry Smith 
268