xref: /petsc/src/ts/interface/tsreg.c (revision 76be9ce4a233aaa47cda2bc7f5a27cd7faabecaa)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*76be9ce4SBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.27 1997/12/01 01:56:01 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 
108b1af7b3SBarry Smith static NRList *__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);
548b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
55d83d6502SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
568b1af7b3SBarry Smith   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
57a8c6a408SBarry Smith   if (!r) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method");}
588b1af7b3SBarry Smith   if (ts->data) PetscFree(ts->data);
593a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
603a40ed3dSBarry Smith   PetscFunctionReturn(0);
613f3760d9SBarry Smith }
623f3760d9SBarry Smith 
633f3760d9SBarry Smith /* --------------------------------------------------------------------- */
645615d1e5SSatish Balay #undef __FUNC__
65d4bb536fSBarry Smith #define __FUNC__ "TSRegister"
663f3760d9SBarry Smith /*@C
672d872ea7SLois Curfman McInnes    TSRegister - Adds the method to the timestepping package, given
682d872ea7SLois Curfman McInnes    a function pointer and a solver name of the type TSType.
693f3760d9SBarry Smith 
703f3760d9SBarry Smith    Input Parameters:
712d872ea7SLois Curfman McInnes .  name - either a predefined name such as TS_BEULER, or TS_NEW
722d872ea7SLois Curfman McInnes           to indicate a new user-defined solver
732d872ea7SLois Curfman McInnes .  sname - corresponding string for name
743f3760d9SBarry Smith .  create - routine to create method context
753f3760d9SBarry Smith 
7684cb2905SBarry Smith    Output Parameter:
7784cb2905SBarry Smith .  oname - type associated with this new method
7884cb2905SBarry Smith 
792d872ea7SLois Curfman McInnes    Notes:
802d872ea7SLois Curfman McInnes    Multiple user-defined timestepping solvers can be added by calling
812d872ea7SLois Curfman McInnes    TSRegister() with the input parameter "name" set to be TS_NEW;
822d872ea7SLois Curfman McInnes    each call will return a unique solver type in the output
832d872ea7SLois Curfman McInnes    parameter "oname".
842d872ea7SLois Curfman McInnes 
852d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register
863f3760d9SBarry Smith 
878b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
883f3760d9SBarry Smith @*/
8984cb2905SBarry Smith int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
903f3760d9SBarry Smith {
913f3760d9SBarry Smith   int ierr;
9284cb2905SBarry Smith   static int numberregistered = 0;
9384cb2905SBarry Smith 
943a40ed3dSBarry Smith   PetscFunctionBegin;
95d252947aSBarry Smith   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
9684cb2905SBarry Smith 
9784cb2905SBarry Smith   if (oname) *oname = name;
988b1af7b3SBarry Smith   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
9984cb2905SBarry Smith   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
1003a40ed3dSBarry Smith   PetscFunctionReturn(0);
1013f3760d9SBarry Smith }
1023f3760d9SBarry Smith /* --------------------------------------------------------------------- */
1035615d1e5SSatish Balay #undef __FUNC__
104d4bb536fSBarry Smith #define __FUNC__ "TSRegisterDestroy"
1053f3760d9SBarry Smith /*@C
10684cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
1078b1af7b3SBarry Smith    registered by TSRegister().
1083f3760d9SBarry Smith 
1092d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
1103f3760d9SBarry Smith 
1118b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterAll()
1123f3760d9SBarry Smith @*/
1138b1af7b3SBarry Smith int TSRegisterDestroy()
1143f3760d9SBarry Smith {
1153a40ed3dSBarry Smith   PetscFunctionBegin;
1168b1af7b3SBarry Smith   if (__TSList) {
1178b1af7b3SBarry Smith     NRDestroy( __TSList );
1188b1af7b3SBarry Smith     __TSList = 0;
1193f3760d9SBarry Smith   }
12084cb2905SBarry Smith   TSRegisterAllCalled = 0;
1213a40ed3dSBarry Smith   PetscFunctionReturn(0);
1223f3760d9SBarry Smith }
1233f3760d9SBarry Smith 
1245615d1e5SSatish Balay #undef __FUNC__
125d4bb536fSBarry Smith #define __FUNC__ "TSGetType"
1263f3760d9SBarry Smith /*@C
1278b1af7b3SBarry Smith    TSGetType - Gets the TS method type and name (as a string).
1283f3760d9SBarry Smith 
1293f3760d9SBarry Smith    Input Parameter:
1302d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1313f3760d9SBarry Smith 
1323f3760d9SBarry Smith    Output Parameter:
1338b1af7b3SBarry Smith .  method - TS method (or use PETSC_NULL)
1348b1af7b3SBarry Smith .  name - name of TS method (or use PETSC_NULL)
1353f3760d9SBarry Smith 
1362d872ea7SLois Curfman McInnes .keywords: TS, timestepper, get, method, name
1373f3760d9SBarry Smith @*/
1388b1af7b3SBarry Smith int TSGetType(TS ts, TSType *method,char **name)
1393f3760d9SBarry Smith {
1403f3760d9SBarry Smith   int ierr;
1413a40ed3dSBarry Smith 
1423a40ed3dSBarry Smith   PetscFunctionBegin;
14384cb2905SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1448b1af7b3SBarry Smith   if (method) *method = (TSType) ts->type;
1458b1af7b3SBarry Smith   if (name)  *name = NRFindName( __TSList, (int) ts->type );
1463a40ed3dSBarry Smith   PetscFunctionReturn(0);
1473f3760d9SBarry Smith }
1483f3760d9SBarry Smith 
1495615d1e5SSatish Balay #undef __FUNC__
150ca161407SBarry Smith #define __FUNC__ "TSPrintHelp"
151ca161407SBarry Smith /*@
152ca161407SBarry Smith    TSPrintHelp - Prints all options for the TS (timestepping) component.
1533f3760d9SBarry Smith 
1543f3760d9SBarry Smith    Input Parameter:
155ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
1563f3760d9SBarry Smith 
157ca161407SBarry Smith    Options Database Keys:
158ca161407SBarry Smith $  -help, -h
1593f3760d9SBarry Smith 
160ca161407SBarry Smith .keywords: TS, timestep, print, help
161ca161407SBarry Smith 
162ca161407SBarry Smith .seealso: TSSetFromOptions()
163ca161407SBarry Smith @*/
164ca161407SBarry Smith int TSPrintHelp(TS ts)
1653f3760d9SBarry Smith {
166ca161407SBarry Smith   char    *prefix = "-";
1678b1af7b3SBarry Smith   int     ierr;
1683a40ed3dSBarry Smith 
1693a40ed3dSBarry Smith   PetscFunctionBegin;
170ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
171ca161407SBarry Smith   if (ts->prefix) prefix = ts->prefix;
172*76be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
173ca161407SBarry Smith   ierr = NRPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",__TSList);CHKERRQ(ierr);
174*76be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
175*76be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
176ca161407SBarry Smith 
177*76be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
178*76be9ce4SBarry Smith   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
179ca161407SBarry Smith   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
1803a40ed3dSBarry Smith   PetscFunctionReturn(0);
1813f3760d9SBarry Smith }
182ca161407SBarry Smith 
183ca161407SBarry Smith #undef __FUNC__
184ca161407SBarry Smith #define __FUNC__ "TSSetFromOptions"
185ca161407SBarry Smith /*@
186ca161407SBarry Smith    TSSetFromOptions - Sets various TS parameters from user options.
187ca161407SBarry Smith 
188ca161407SBarry Smith    Input Parameter:
189ca161407SBarry Smith .  ts - the TS context obtained from TSCreate()
190ca161407SBarry Smith 
191ca161407SBarry Smith .keywords: TS, timestep, set, options, database
192ca161407SBarry Smith 
193ca161407SBarry Smith .seealso: TSPrintHelp()
194ca161407SBarry Smith @*/
195ca161407SBarry Smith int TSSetFromOptions(TS ts)
196ca161407SBarry Smith {
197ca161407SBarry Smith   int    ierr,flg,loc[4],nmax;
198ca161407SBarry Smith   TSType method;
199ca161407SBarry Smith 
200ca161407SBarry Smith   PetscFunctionBegin;
201ca161407SBarry Smith   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
202ca161407SBarry Smith 
203ca161407SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
204a8c6a408SBarry Smith   if (ts->setup_called) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp!");
205ca161407SBarry Smith   if (!__TSList) {ierr = TSRegisterAll();CHKERRQ(ierr);}
206ca161407SBarry Smith   ierr = NRGetTypeFromOptions(ts->prefix,"-ts_type",__TSList,&method,&flg);CHKERRQ(ierr);
207ca161407SBarry Smith   if (flg) {
208ca161407SBarry Smith     ierr = TSSetType(ts,method); CHKERRQ(ierr);
209ca161407SBarry Smith   }
210ca161407SBarry Smith 
211ca161407SBarry Smith   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
212ca161407SBarry Smith   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
213ca161407SBarry Smith   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
214ca161407SBarry Smith   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
215ca161407SBarry Smith   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
216ca161407SBarry Smith   if (flg) {
217ca161407SBarry Smith     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
218ca161407SBarry Smith   }
219ca161407SBarry Smith   nmax = 4;
220ca161407SBarry Smith   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
221ca161407SBarry Smith   if (flg) {
222ca161407SBarry Smith     int    rank = 0;
223ca161407SBarry Smith     DrawLG lg;
224ca161407SBarry Smith     MPI_Comm_rank(ts->comm,&rank);
225ca161407SBarry Smith     if (!rank) {
226ca161407SBarry Smith       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
227ca161407SBarry Smith       PLogObjectParent(ts,(PetscObject) lg);
228ca161407SBarry Smith       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
229ca161407SBarry Smith     }
230ca161407SBarry Smith   }
231ca161407SBarry Smith   if (!ts->setfromoptions) PetscFunctionReturn(0);
232ca161407SBarry Smith   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
233ca161407SBarry Smith   PetscFunctionReturn(0);
234ca161407SBarry Smith }
235ca161407SBarry Smith 
236