xref: /petsc/src/ts/interface/tsreg.c (revision 6831982abb6453c2f3e25efecb5d0951d333371e)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*6831982aSBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.52 1999/10/13 20:38:40 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
19454a90a3SBarry Smith -  type - a known method
20bef22f13SLois Curfman McInnes 
21ae12b187SLois Curfman McInnes    Options Database Command:
22454a90a3SBarry 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 @*/
48454a90a3SBarry Smith int TSSetType(TS ts,TSType type)
493f3760d9SBarry Smith {
50d83d6502SBarry Smith   int        ierr,(*r)(TS);
51*6831982aSBarry Smith   PetscTruth match;
528b1af7b3SBarry Smith 
533a40ed3dSBarry Smith   PetscFunctionBegin;
54c3e30b67SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
550f5bd95cSBarry Smith   PetscValidCharPointer(type);
560f5bd95cSBarry Smith 
57*6831982aSBarry Smith   ierr = PetscTypeCompare((PetscObject)ts,type,&match);CHKERRQ(ierr);
580f5bd95cSBarry Smith   if (match) PetscFunctionReturn(0);
59df8cb225SBarry Smith 
608b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
6182bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
62454a90a3SBarry Smith   ierr =  FListFind(ts->comm, TSList, type, (int (**)(void *)) &r );CHKERRQ(ierr);
63454a90a3SBarry Smith   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown type: %s",type);}
6482bf6240SBarry Smith 
65df8cb225SBarry Smith   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
66df8cb225SBarry Smith   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
67e1311b90SBarry Smith   if (ts->destroy) {ierr = (*(ts)->destroy)(ts);CHKERRQ(ierr);}
68df8cb225SBarry Smith   ts->sles = 0;
69df8cb225SBarry Smith   ts->snes = 0;
7082bf6240SBarry Smith 
713a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
72df8cb225SBarry Smith 
73454a90a3SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
743a40ed3dSBarry Smith   PetscFunctionReturn(0);
753f3760d9SBarry Smith }
76df8cb225SBarry Smith 
773f3760d9SBarry Smith /* --------------------------------------------------------------------- */
785615d1e5SSatish Balay #undef __FUNC__
79d4bb536fSBarry Smith #define __FUNC__ "TSRegisterDestroy"
803f3760d9SBarry Smith /*@C
8184cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
82488ecbafSBarry Smith    registered by FListAdd().
833f3760d9SBarry Smith 
84fee21e36SBarry Smith    Not Collective
85fee21e36SBarry Smith 
86d5d37b61SLois Curfman McInnes    Level: advanced
87d5d37b61SLois Curfman McInnes 
882d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
893f3760d9SBarry Smith 
9082bf6240SBarry Smith .seealso: TSRegisterAll()
913f3760d9SBarry Smith @*/
92cf256101SBarry Smith int TSRegisterDestroy(void)
933f3760d9SBarry Smith {
94df8cb225SBarry Smith   int ierr;
95df8cb225SBarry Smith 
963a40ed3dSBarry Smith   PetscFunctionBegin;
9782bf6240SBarry Smith   if (TSList) {
98488ecbafSBarry Smith     ierr = FListDestroy( TSList );CHKERRQ(ierr);
9982bf6240SBarry Smith     TSList = 0;
1003f3760d9SBarry Smith   }
10184cb2905SBarry Smith   TSRegisterAllCalled = 0;
1023a40ed3dSBarry Smith   PetscFunctionReturn(0);
1033f3760d9SBarry Smith }
1043f3760d9SBarry Smith 
1055615d1e5SSatish Balay #undef __FUNC__
106d4bb536fSBarry Smith #define __FUNC__ "TSGetType"
1073f3760d9SBarry Smith /*@C
108fee21e36SBarry Smith    TSGetType - Gets the TS method type (as a string).
1093f3760d9SBarry Smith 
110bef22f13SLois Curfman McInnes    Not Collective
111bef22f13SLois Curfman McInnes 
1123f3760d9SBarry Smith    Input Parameter:
1132d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1143f3760d9SBarry Smith 
1153f3760d9SBarry Smith    Output Parameter:
11682bf6240SBarry Smith .  type - name of TS method
1173f3760d9SBarry Smith 
118d5d37b61SLois Curfman McInnes    Level: intermediate
119d5d37b61SLois Curfman McInnes 
120df8cb225SBarry Smith .keywords: TS, timestepper, get, type, name
1213f3760d9SBarry Smith @*/
12282bf6240SBarry Smith int TSGetType(TS ts, TSType *type)
1233f3760d9SBarry Smith {
1243f3760d9SBarry Smith   int ierr;
1253a40ed3dSBarry Smith 
1263a40ed3dSBarry Smith   PetscFunctionBegin;
12782bf6240SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
12882bf6240SBarry Smith   *type = ts->type_name;
1293a40ed3dSBarry Smith   PetscFunctionReturn(0);
1303f3760d9SBarry Smith }
1313f3760d9SBarry Smith 
1325615d1e5SSatish Balay #undef __FUNC__
133ca161407SBarry Smith #define __FUNC__ "TSPrintHelp"
134ca161407SBarry Smith /*@
135ca161407SBarry Smith    TSPrintHelp - Prints all options for the TS (timestepping) component.
1363f3760d9SBarry Smith 
137bef22f13SLois Curfman McInnes    Collective on TS
138bef22f13SLois Curfman McInnes 
1393f3760d9SBarry Smith    Input Parameter:
140ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
1413f3760d9SBarry Smith 
142ca161407SBarry Smith    Options Database Keys:
143bef22f13SLois Curfman McInnes +  -help - Prints KSP options
144bef22f13SLois Curfman McInnes -  -h - Prints KSP options
145fee21e36SBarry Smith 
146d5d37b61SLois Curfman McInnes    Level: beginner
147d5d37b61SLois Curfman McInnes 
148ca161407SBarry Smith .keywords: TS, timestep, print, help
149ca161407SBarry Smith 
150ca161407SBarry Smith .seealso: TSSetFromOptions()
151ca161407SBarry Smith @*/
152ca161407SBarry Smith int TSPrintHelp(TS ts)
1533f3760d9SBarry Smith {
154ca161407SBarry Smith   char    *prefix = "-";
1558b1af7b3SBarry Smith   int     ierr;
1563a40ed3dSBarry Smith 
1573a40ed3dSBarry Smith   PetscFunctionBegin;
158ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
159ca161407SBarry Smith   if (ts->prefix) prefix = ts->prefix;
160ebb8b11fSBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
161d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");CHKERRQ(ierr);
162488ecbafSBarry Smith   ierr = FListPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
163d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);CHKERRQ(ierr);
164d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);CHKERRQ(ierr);
165ca161407SBarry Smith 
166d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);CHKERRQ(ierr);
167d132466eSBarry Smith   ierr = (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);CHKERRQ(ierr);
168ca161407SBarry Smith   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
1693a40ed3dSBarry Smith   PetscFunctionReturn(0);
1703f3760d9SBarry Smith }
171ca161407SBarry Smith 
172ca161407SBarry Smith #undef __FUNC__
17315091d37SBarry Smith #define __FUNC__ "TSSetTypeFromOptions"
17415091d37SBarry Smith /*@
17515091d37SBarry Smith    TSSetTypeFromOptions - Sets the TS type from the options database; sets
17615091d37SBarry Smith      a default if none is given.
17715091d37SBarry Smith 
17815091d37SBarry Smith    Collective on TS
17915091d37SBarry Smith 
18015091d37SBarry Smith    Input Parameter:
18115091d37SBarry Smith .  ts - the TS context obtained from TSCreate()
18215091d37SBarry Smith 
18315091d37SBarry Smith    Options Database Keys:
18415091d37SBarry Smith .  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
18515091d37SBarry Smith 
18615091d37SBarry Smith    Level: beginner
18715091d37SBarry Smith 
18815091d37SBarry Smith .keywords: TS, timestep, set, options, database, TS type
18915091d37SBarry Smith 
19015091d37SBarry Smith .seealso: TSPrintHelp(), TSSetFromOptions()
19115091d37SBarry Smith @*/
19215091d37SBarry Smith int TSSetTypeFromOptions(TS ts)
19315091d37SBarry Smith {
19415091d37SBarry Smith   int  ierr,flg;
19515091d37SBarry Smith   char type[256];
19615091d37SBarry Smith 
19715091d37SBarry Smith   PetscFunctionBegin;
19815091d37SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
19915091d37SBarry Smith   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp()");
200888f2ed8SSatish Balay   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);CHKERRQ(ierr);
20115091d37SBarry Smith   if (flg) {
20215091d37SBarry Smith     ierr = TSSetType(ts,type);CHKERRQ(ierr);
20315091d37SBarry Smith   }
20415091d37SBarry Smith   if (!ts->type_name) {
20515091d37SBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
20615091d37SBarry Smith   }
20715091d37SBarry Smith   PetscFunctionReturn(0);
20815091d37SBarry Smith }
20915091d37SBarry Smith 
21015091d37SBarry Smith #undef __FUNC__
211ca161407SBarry Smith #define __FUNC__ "TSSetFromOptions"
212ca161407SBarry Smith /*@
213ca161407SBarry Smith    TSSetFromOptions - Sets various TS parameters from user options.
214ca161407SBarry Smith 
215bef22f13SLois Curfman McInnes    Collective on TS
216bef22f13SLois Curfman McInnes 
217ca161407SBarry Smith    Input Parameter:
218ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
219ca161407SBarry Smith 
22015091d37SBarry Smith    Options Database Keys:
22115091d37SBarry Smith +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
22215091d37SBarry Smith .  -ts_max_steps maxsteps - maximum number of time-steps to take
22315091d37SBarry Smith .  -ts_max_time time - maximum time to compute to
22415091d37SBarry Smith .  -ts_monitor - print information at each timestep
22515091d37SBarry Smith -  -ts_xmonitor - plot information at each timestep
22615091d37SBarry Smith 
227d5d37b61SLois Curfman McInnes    Level: beginner
228d5d37b61SLois Curfman McInnes 
229ca161407SBarry Smith .keywords: TS, timestep, set, options, database
230ca161407SBarry Smith 
23115091d37SBarry Smith .seealso: TSPrintHelp(), TSSetTypeFromOptions()
232ca161407SBarry Smith @*/
233ca161407SBarry Smith int TSSetFromOptions(TS ts)
234ca161407SBarry Smith {
235ca161407SBarry Smith   int    ierr,flg,loc[4],nmax;
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 
243ca161407SBarry Smith   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
244ca161407SBarry Smith   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
245ca161407SBarry Smith   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg);CHKERRQ(ierr);
246ca161407SBarry Smith   if (flg) {
247ca161407SBarry Smith     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);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) {
252ca161407SBarry Smith     int    rank = 0;
253ca161407SBarry Smith     DrawLG lg;
254d132466eSBarry Smith     ierr = MPI_Comm_rank(ts->comm,&rank);CHKERRQ(ierr);
255ca161407SBarry Smith     if (!rank) {
256ca161407SBarry Smith       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg);CHKERRQ(ierr);
257ca161407SBarry Smith       PLogObjectParent(ts,(PetscObject) lg);
258ca161407SBarry Smith       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
259ca161407SBarry Smith     }
260ca161407SBarry Smith   }
2616a6a5d1dSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg);CHKERRQ(ierr);
2626a6a5d1dSBarry Smith   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
263184914b5SBarry Smith   if (ts->setfromoptions) {
264ca161407SBarry Smith     ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
265184914b5SBarry Smith   }
266ca161407SBarry Smith   PetscFunctionReturn(0);
267ca161407SBarry Smith }
268ca161407SBarry Smith 
26982bf6240SBarry Smith 
270184914b5SBarry Smith 
271