xref: /petsc/src/ts/interface/tsreg.c (revision 3a40ed3dce77c081171d005ae1a6ff4bb9d13b6f)
1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER
2*3a40ed3dSBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.23 1997/09/26 02:20:18 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
30*3a40ed3dSBarry 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 
52*3a40ed3dSBarry 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);}
56e3372554SBarry Smith   if (!__TSList) {SETERRQ(1,0,"Could not get methods");}
578b1af7b3SBarry Smith   r =  (int (*)(TS))NRFindRoutine( __TSList, (int)method, (char *)0 );
58e3372554SBarry Smith   if (!r) {SETERRQ(1,0,"Unknown method");}
598b1af7b3SBarry Smith   if (ts->data) PetscFree(ts->data);
60*3a40ed3dSBarry Smith   ierr = (*r)(ts);CHKERRQ(ierr);
61*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
623f3760d9SBarry Smith }
633f3760d9SBarry Smith 
643f3760d9SBarry Smith /* --------------------------------------------------------------------- */
655615d1e5SSatish Balay #undef __FUNC__
66d4bb536fSBarry Smith #define __FUNC__ "TSRegister"
673f3760d9SBarry Smith /*@C
682d872ea7SLois Curfman McInnes    TSRegister - Adds the method to the timestepping package, given
692d872ea7SLois Curfman McInnes    a function pointer and a solver name of the type TSType.
703f3760d9SBarry Smith 
713f3760d9SBarry Smith    Input Parameters:
722d872ea7SLois Curfman McInnes .  name - either a predefined name such as TS_BEULER, or TS_NEW
732d872ea7SLois Curfman McInnes           to indicate a new user-defined solver
742d872ea7SLois Curfman McInnes .  sname - corresponding string for name
753f3760d9SBarry Smith .  create - routine to create method context
763f3760d9SBarry Smith 
7784cb2905SBarry Smith    Output Parameter:
7884cb2905SBarry Smith .  oname - type associated with this new method
7984cb2905SBarry Smith 
802d872ea7SLois Curfman McInnes    Notes:
812d872ea7SLois Curfman McInnes    Multiple user-defined timestepping solvers can be added by calling
822d872ea7SLois Curfman McInnes    TSRegister() with the input parameter "name" set to be TS_NEW;
832d872ea7SLois Curfman McInnes    each call will return a unique solver type in the output
842d872ea7SLois Curfman McInnes    parameter "oname".
852d872ea7SLois Curfman McInnes 
862d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register
873f3760d9SBarry Smith 
888b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
893f3760d9SBarry Smith @*/
9084cb2905SBarry Smith int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
913f3760d9SBarry Smith {
923f3760d9SBarry Smith   int ierr;
9384cb2905SBarry Smith   static int numberregistered = 0;
9484cb2905SBarry Smith 
95*3a40ed3dSBarry Smith   PetscFunctionBegin;
96d252947aSBarry Smith   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
9784cb2905SBarry Smith 
9884cb2905SBarry Smith   if (oname) *oname = name;
998b1af7b3SBarry Smith   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
10084cb2905SBarry Smith   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
101*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
1023f3760d9SBarry Smith }
1033f3760d9SBarry Smith /* --------------------------------------------------------------------- */
1045615d1e5SSatish Balay #undef __FUNC__
105d4bb536fSBarry Smith #define __FUNC__ "TSRegisterDestroy"
1063f3760d9SBarry Smith /*@C
10784cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
1088b1af7b3SBarry Smith    registered by TSRegister().
1093f3760d9SBarry Smith 
1102d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
1113f3760d9SBarry Smith 
1128b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterAll()
1133f3760d9SBarry Smith @*/
1148b1af7b3SBarry Smith int TSRegisterDestroy()
1153f3760d9SBarry Smith {
116*3a40ed3dSBarry Smith   PetscFunctionBegin;
1178b1af7b3SBarry Smith   if (__TSList) {
1188b1af7b3SBarry Smith     NRDestroy( __TSList );
1198b1af7b3SBarry Smith     __TSList = 0;
1203f3760d9SBarry Smith   }
12184cb2905SBarry Smith   TSRegisterAllCalled = 0;
122*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
1233f3760d9SBarry Smith }
1243f3760d9SBarry Smith 
1255615d1e5SSatish Balay #undef __FUNC__
126d4bb536fSBarry Smith #define __FUNC__ "TSGetType"
1273f3760d9SBarry Smith /*@C
1288b1af7b3SBarry Smith    TSGetType - Gets the TS method type and name (as a string).
1293f3760d9SBarry Smith 
1303f3760d9SBarry Smith    Input Parameter:
1312d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1323f3760d9SBarry Smith 
1333f3760d9SBarry Smith    Output Parameter:
1348b1af7b3SBarry Smith .  method - TS method (or use PETSC_NULL)
1358b1af7b3SBarry Smith .  name - name of TS method (or use PETSC_NULL)
1363f3760d9SBarry Smith 
1372d872ea7SLois Curfman McInnes .keywords: TS, timestepper, get, method, name
1383f3760d9SBarry Smith @*/
1398b1af7b3SBarry Smith int TSGetType(TS ts, TSType *method,char **name)
1403f3760d9SBarry Smith {
1413f3760d9SBarry Smith   int ierr;
142*3a40ed3dSBarry Smith 
143*3a40ed3dSBarry Smith   PetscFunctionBegin;
14484cb2905SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1458b1af7b3SBarry Smith   if (method) *method = (TSType) ts->type;
1468b1af7b3SBarry Smith   if (name)  *name = NRFindName( __TSList, (int) ts->type );
147*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
1483f3760d9SBarry Smith }
1493f3760d9SBarry Smith 
1505615d1e5SSatish Balay #undef __FUNC__
151d4bb536fSBarry Smith #define __FUNC__ "TSPrintTypes_Private"
1523f3760d9SBarry Smith /*
1538b1af7b3SBarry Smith    TSPrintTypes_Private - Prints the TS methods available from the
1543f3760d9SBarry Smith    options database.
1553f3760d9SBarry Smith 
1563f3760d9SBarry Smith    Input Parameters:
1570752156aSBarry Smith .  comm   - The communicator (usually PETSC_COMM_WORLD)
1583f3760d9SBarry Smith .  prefix - prefix (usually "-")
1598b1af7b3SBarry Smith .  name   - the options database name (by default "ts_type")
1603f3760d9SBarry Smith */
161b5f69acfSSatish Balay int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
1623f3760d9SBarry Smith {
1633f3760d9SBarry Smith   FuncList *entry;
164*3a40ed3dSBarry Smith 
165*3a40ed3dSBarry Smith   PetscFunctionBegin;
1668b1af7b3SBarry Smith   if (!__TSList) {TSRegisterAll();}
1678b1af7b3SBarry Smith   entry = __TSList->head;
168c3e30b67SBarry Smith   PetscPrintf(comm," %s%s (one of)",prefix,name);
1693f3760d9SBarry Smith   while (entry) {
170c3e30b67SBarry Smith     PetscPrintf(comm," %s",entry->name);
1713f3760d9SBarry Smith     entry = entry->next;
1723f3760d9SBarry Smith   }
173c3e30b67SBarry Smith   PetscPrintf(comm,"\n");
174*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
1753f3760d9SBarry Smith }
1763f3760d9SBarry Smith 
1778b1af7b3SBarry Smith 
1785615d1e5SSatish Balay #undef __FUNC__
179d4bb536fSBarry Smith #define __FUNC__ "TSGetTypeFromOptions_Private"
1808b1af7b3SBarry Smith /*
1818b1af7b3SBarry Smith    TSGetTypeFromOptions_Private - Sets the selected method from the
1828b1af7b3SBarry Smith    options database.
1833f3760d9SBarry Smith 
1843f3760d9SBarry Smith    Input Parameter:
1858b1af7b3SBarry Smith .  ctx - the TS context
1863f3760d9SBarry Smith 
1873f3760d9SBarry Smith    Output Parameter:
1888b1af7b3SBarry Smith .  method -  solver method
1893f3760d9SBarry Smith 
1908b1af7b3SBarry Smith    Returns:
1918b1af7b3SBarry Smith    Returns 1 if the method is found; 0 otherwise.
1923f3760d9SBarry Smith 
1938b1af7b3SBarry Smith    Options Database Key:
1948b1af7b3SBarry Smith $  -ts_type  method
1958b1af7b3SBarry Smith */
1968b1af7b3SBarry Smith int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
1973f3760d9SBarry Smith {
1988b1af7b3SBarry Smith   int  ierr;
1998b1af7b3SBarry Smith   char sbuf[50];
200*3a40ed3dSBarry Smith 
201*3a40ed3dSBarry Smith   PetscFunctionBegin;
2028b1af7b3SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
2038b1af7b3SBarry Smith   if (*flg) {
2048b1af7b3SBarry Smith     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
2058b1af7b3SBarry Smith     *method = (TSType)NRFindID( __TSList, sbuf );
2068b1af7b3SBarry Smith   }
207*3a40ed3dSBarry Smith   PetscFunctionReturn(0);
2083f3760d9SBarry Smith }
209