xref: /petsc/src/ts/interface/tsreg.c (revision 6a6a5d1dfdd5fcb6e31127fc1e7bbfb8f4c48c0a)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*6a6a5d1dSBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.29 1998/01/17 17:38:11 bsmith Exp bsmith $";
33f3760d9SBarry Smith #endif
43f3760d9SBarry Smith 
570f55243SBarry Smith #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
6f5eb4b81SSatish Balay #include "src/sys/nreg.h"
73f3760d9SBarry Smith #include "pinclude/pviewer.h"
83f3760d9SBarry Smith #include <math.h>
93f3760d9SBarry Smith 
10df8cb225SBarry Smith static DLList __TSList = 0;
1184cb2905SBarry Smith int TSRegisterAllCalled = 0;
123f3760d9SBarry Smith 
135615d1e5SSatish Balay #undef __FUNC__
14d4bb536fSBarry Smith #define __FUNC__ "TSSetType"
153f3760d9SBarry Smith /*@
16ae12b187SLois Curfman McInnes    TSSetType - Sets the method for the timestepping solver.
173f3760d9SBarry Smith 
183f3760d9SBarry Smith    Input Parameters:
198b1af7b3SBarry Smith .  ts - the TS context
203f3760d9SBarry Smith .  method - a known method
213f3760d9SBarry Smith 
22ae12b187SLois Curfman McInnes   Options Database Command:
23ae12b187SLois Curfman McInnes $ -ts_type  <method>
24ae12b187SLois Curfman McInnes $    Use -help for a list of available methods
25ae12b187SLois Curfman McInnes $    (for instance, euler)
26ae12b187SLois Curfman McInnes 
273f3760d9SBarry Smith    Notes:
288b1af7b3SBarry Smith    See "petsc/include/ts.h" for available methods (for instance)
298b1af7b3SBarry Smith $   TS_EULER
303a40ed3dSBarry Smith $   TS_PVODE
31ca90a507SBarry Smith $   TS_BEULER
32ca90a507SBarry Smith $   TS_PSEUDO
333f3760d9SBarry Smith 
34ae12b187SLois Curfman McInnes   Normally, it is best to use the TSSetFromOptions() command and
35ae12b187SLois Curfman McInnes   then set the TS type from the options database rather than by using
36ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
37ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many different solvers.
38ae12b187SLois Curfman McInnes   The TSSetType() routine is provided for those situations where it
39ae12b187SLois Curfman McInnes   is necessary to set the timestepping solver independently of the
40ae12b187SLois Curfman McInnes   command line or options database.  This might be the case, for example,
41ae12b187SLois Curfman McInnes   when the choice of solver changes during the execution of the
42ae12b187SLois Curfman McInnes   program, and the user's application is taking responsibility for
43ae12b187SLois Curfman McInnes   choosing the appropriate method.  In other words, this routine is
44ae12b187SLois Curfman McInnes   for the advanced user.
453f3760d9SBarry Smith 
46ae12b187SLois Curfman McInnes .keywords: TS, set, type
473f3760d9SBarry Smith @*/
488b1af7b3SBarry Smith int TSSetType(TS ts,TSType method)
493f3760d9SBarry Smith {
50d83d6502SBarry Smith   int ierr,(*r)(TS);
518b1af7b3SBarry Smith 
523a40ed3dSBarry Smith   PetscFunctionBegin;
53c3e30b67SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
54df8cb225SBarry Smith   if (ts->type == method) PetscFunctionReturn(0);
55df8cb225SBarry Smith 
568b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
57d83d6502SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
58df8cb225SBarry Smith   ierr =  DLFindRoutine( __TSList, (int)method, (char *)0,(int (**)(void *)) &r );CHKERRQ(ierr);
59a8c6a408SBarry Smith   if (!r) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method");}
60df8cb225SBarry Smith   if (ts->type != TS_UNKNOWN) {
61df8cb225SBarry Smith     if (ts->sles) {ierr = SLESDestroy(ts->sles); CHKERRQ(ierr);}
62df8cb225SBarry Smith     if (ts->snes) {ierr = SNESDestroy(ts->snes); CHKERRQ(ierr);}
63df8cb225SBarry Smith     ierr = (*(ts)->destroy)((PetscObject)ts); CHKERRQ(ierr);
64df8cb225SBarry Smith     ts->sles = 0;
65df8cb225SBarry Smith     ts->snes = 0;
66df8cb225SBarry Smith   }
673a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
68df8cb225SBarry Smith 
69df8cb225SBarry Smith   /* override the type that the create routine put in */
70df8cb225SBarry Smith   ts->type = method;
713a40ed3dSBarry Smith   PetscFunctionReturn(0);
723f3760d9SBarry Smith }
733f3760d9SBarry Smith 
743f3760d9SBarry Smith /* --------------------------------------------------------------------- */
755615d1e5SSatish Balay #undef __FUNC__
76df8cb225SBarry Smith #define __FUNC__ "TSRegister_Private"
77df8cb225SBarry Smith /*
78df8cb225SBarry Smith    TSRegister_Private - Adds the method to the timestepping package, given
792d872ea7SLois Curfman McInnes    a function pointer and a solver name of the type TSType.
803f3760d9SBarry Smith 
813f3760d9SBarry Smith    Input Parameters:
822d872ea7SLois Curfman McInnes .  name - either a predefined name such as TS_BEULER, or TS_NEW
832d872ea7SLois Curfman McInnes           to indicate a new user-defined solver
842d872ea7SLois Curfman McInnes .  sname - corresponding string for name
853f3760d9SBarry Smith .  create - routine to create method context
863f3760d9SBarry Smith 
8784cb2905SBarry Smith    Output Parameter:
8884cb2905SBarry Smith .  oname - type associated with this new method
8984cb2905SBarry Smith 
902d872ea7SLois Curfman McInnes    Notes:
912d872ea7SLois Curfman McInnes    Multiple user-defined timestepping solvers can be added by calling
922d872ea7SLois Curfman McInnes    TSRegister() with the input parameter "name" set to be TS_NEW;
932d872ea7SLois Curfman McInnes    each call will return a unique solver type in the output
942d872ea7SLois Curfman McInnes    parameter "oname".
952d872ea7SLois Curfman McInnes 
962d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register
973f3760d9SBarry Smith 
988b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
99df8cb225SBarry Smith */
100df8cb225SBarry Smith int TSRegister_Private(TSType name, char *sname, char *fname,int (*create)(TS),TSType *oname)
1013f3760d9SBarry Smith {
1023f3760d9SBarry Smith   int ierr;
10384cb2905SBarry Smith 
1043a40ed3dSBarry Smith   PetscFunctionBegin;
10584cb2905SBarry Smith 
106df8cb225SBarry Smith   if (!__TSList) {ierr = DLCreate((int)TS_NEW,&__TSList); CHKERRQ(ierr);}
107df8cb225SBarry Smith   ierr = DLRegister( __TSList, (int) name, sname, fname,(int (*)(void*))create,(int*)oname );
1083a40ed3dSBarry Smith   PetscFunctionReturn(0);
1093f3760d9SBarry Smith }
110df8cb225SBarry Smith 
1113f3760d9SBarry Smith /* --------------------------------------------------------------------- */
1125615d1e5SSatish Balay #undef __FUNC__
113d4bb536fSBarry Smith #define __FUNC__ "TSRegisterDestroy"
1143f3760d9SBarry Smith /*@C
11584cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
1168b1af7b3SBarry Smith    registered by TSRegister().
1173f3760d9SBarry Smith 
1182d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
1193f3760d9SBarry Smith 
1208b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterAll()
1213f3760d9SBarry Smith @*/
1228b1af7b3SBarry Smith int TSRegisterDestroy()
1233f3760d9SBarry Smith {
124df8cb225SBarry Smith   int ierr;
125df8cb225SBarry Smith 
1263a40ed3dSBarry Smith   PetscFunctionBegin;
1278b1af7b3SBarry Smith   if (__TSList) {
128df8cb225SBarry Smith     ierr = DLDestroy( __TSList );CHKERRQ(ierr);
1298b1af7b3SBarry Smith     __TSList = 0;
1303f3760d9SBarry Smith   }
13184cb2905SBarry Smith   TSRegisterAllCalled = 0;
1323a40ed3dSBarry Smith   PetscFunctionReturn(0);
1333f3760d9SBarry Smith }
1343f3760d9SBarry Smith 
1355615d1e5SSatish Balay #undef __FUNC__
136d4bb536fSBarry Smith #define __FUNC__ "TSGetType"
1373f3760d9SBarry Smith /*@C
1388b1af7b3SBarry Smith    TSGetType - Gets the TS method type and name (as a string).
1393f3760d9SBarry Smith 
1403f3760d9SBarry Smith    Input Parameter:
1412d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1423f3760d9SBarry Smith 
1433f3760d9SBarry Smith    Output Parameter:
1448b1af7b3SBarry Smith .  method - TS method (or use PETSC_NULL)
1458b1af7b3SBarry Smith .  name - name of TS method (or use PETSC_NULL)
1463f3760d9SBarry Smith 
147df8cb225SBarry Smith .keywords: TS, timestepper, get, type, name
1483f3760d9SBarry Smith @*/
149df8cb225SBarry Smith int TSGetType(TS ts, TSType *type,char **name)
1503f3760d9SBarry Smith {
1513f3760d9SBarry Smith   int ierr;
1523a40ed3dSBarry Smith 
1533a40ed3dSBarry Smith   PetscFunctionBegin;
15484cb2905SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
155df8cb225SBarry Smith   if (type) *type = (TSType) ts->type;
156df8cb225SBarry Smith   if (name) {ierr = DLFindName( __TSList, (int) ts->type,name ); CHKERRQ(ierr);}
1573a40ed3dSBarry Smith   PetscFunctionReturn(0);
1583f3760d9SBarry Smith }
1593f3760d9SBarry Smith 
1605615d1e5SSatish Balay #undef __FUNC__
161ca161407SBarry Smith #define __FUNC__ "TSPrintHelp"
162ca161407SBarry Smith /*@
163ca161407SBarry Smith    TSPrintHelp - Prints all options for the TS (timestepping) component.
1643f3760d9SBarry Smith 
1653f3760d9SBarry Smith    Input Parameter:
166ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
1673f3760d9SBarry Smith 
168ca161407SBarry Smith    Options Database Keys:
169ca161407SBarry Smith $  -help, -h
1703f3760d9SBarry Smith 
171ca161407SBarry Smith .keywords: TS, timestep, print, help
172ca161407SBarry Smith 
173ca161407SBarry Smith .seealso: TSSetFromOptions()
174ca161407SBarry Smith @*/
175ca161407SBarry Smith int TSPrintHelp(TS ts)
1763f3760d9SBarry Smith {
177ca161407SBarry Smith   char    *prefix = "-";
1788b1af7b3SBarry Smith   int     ierr;
1793a40ed3dSBarry Smith 
1803a40ed3dSBarry Smith   PetscFunctionBegin;
181ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
182ca161407SBarry Smith   if (ts->prefix) prefix = ts->prefix;
18376be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
184df8cb225SBarry Smith   ierr = DLPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",__TSList);CHKERRQ(ierr);
18576be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
18676be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
187ca161407SBarry Smith 
18876be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
18976be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
190ca161407SBarry Smith   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
1913a40ed3dSBarry Smith   PetscFunctionReturn(0);
1923f3760d9SBarry Smith }
193ca161407SBarry Smith 
194ca161407SBarry Smith #undef __FUNC__
195ca161407SBarry Smith #define __FUNC__ "TSSetFromOptions"
196ca161407SBarry Smith /*@
197ca161407SBarry Smith    TSSetFromOptions - Sets various TS parameters from user options.
198ca161407SBarry Smith 
199ca161407SBarry Smith    Input Parameter:
200ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
201ca161407SBarry Smith 
202ca161407SBarry Smith .keywords: TS, timestep, set, options, database
203ca161407SBarry Smith 
204ca161407SBarry Smith .seealso: TSPrintHelp()
205ca161407SBarry Smith @*/
206ca161407SBarry Smith int TSSetFromOptions(TS ts)
207ca161407SBarry Smith {
208ca161407SBarry Smith   int    ierr,flg,loc[4],nmax;
209df8cb225SBarry Smith   TSType type;
210df8cb225SBarry Smith   char   fname[256];
211ca161407SBarry Smith 
212ca161407SBarry Smith   PetscFunctionBegin;
213ca161407SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
214ca161407SBarry Smith 
215ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
216a8c6a408SBarry Smith   if (ts->setup_called) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp!");
217df8cb225SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll();CHKERRQ(ierr);}
218df8cb225SBarry Smith   ierr = DLGetTypeFromOptions(ts->prefix,"-ts_type",__TSList,(int *)&type,fname,256,&flg);CHKERRQ(ierr);
219ca161407SBarry Smith   if (flg) {
220df8cb225SBarry Smith #if defined(USE_DYNAMIC_LIBRARIES)
221df8cb225SBarry Smith     if (type == (TSType) -1) { /* indicates method not yet registered */
222df8cb225SBarry Smith       ierr = TSRegister(TS_NEW,fname,fname,0,&type); CHKERRQ(ierr);
223df8cb225SBarry Smith     }
224df8cb225SBarry Smith #endif
225df8cb225SBarry Smith     ierr = TSSetType(ts,type); CHKERRQ(ierr);
226ca161407SBarry Smith   }
227ca161407SBarry Smith 
228ca161407SBarry Smith   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
229ca161407SBarry Smith   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
230ca161407SBarry Smith   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
231ca161407SBarry Smith   if (flg) {
232ca161407SBarry Smith     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
233ca161407SBarry Smith   }
234ca161407SBarry Smith   nmax = 4;
235ca161407SBarry Smith   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
236ca161407SBarry Smith   if (flg) {
237ca161407SBarry Smith     int    rank = 0;
238ca161407SBarry Smith     DrawLG lg;
239ca161407SBarry Smith     MPI_Comm_rank(ts->comm,&rank);
240ca161407SBarry Smith     if (!rank) {
241ca161407SBarry Smith       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
242ca161407SBarry Smith       PLogObjectParent(ts,(PetscObject) lg);
243ca161407SBarry Smith       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
244ca161407SBarry Smith     }
245ca161407SBarry Smith   }
246*6a6a5d1dSBarry Smith   if (ts->type == TS_UNKNOWN) {
247*6a6a5d1dSBarry Smith     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
248*6a6a5d1dSBarry Smith   }
249*6a6a5d1dSBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
250*6a6a5d1dSBarry Smith   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
251ca161407SBarry Smith   if (!ts->setfromoptions) PetscFunctionReturn(0);
252ca161407SBarry Smith   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
253ca161407SBarry Smith   PetscFunctionReturn(0);
254ca161407SBarry Smith }
255ca161407SBarry Smith 
256