xref: /petsc/src/ts/interface/tsreg.c (revision d252947ab307443ec0f8cabd384072ff75afeb3e)
18b1af7b3SBarry Smith 
2fae171e0SBarry Smith 
33f3760d9SBarry Smith #ifndef lint
4*d252947aSBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.17 1997/02/03 15:55:15 curfman Exp bsmith $";
53f3760d9SBarry Smith #endif
63f3760d9SBarry Smith 
770f55243SBarry Smith #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
8f5eb4b81SSatish Balay #include "src/sys/nreg.h"
93f3760d9SBarry Smith #include "pinclude/pviewer.h"
103f3760d9SBarry Smith #include <math.h>
113f3760d9SBarry Smith 
128b1af7b3SBarry Smith static NRList *__TSList = 0;
1384cb2905SBarry Smith int TSRegisterAllCalled = 0;
143f3760d9SBarry Smith 
155615d1e5SSatish Balay #undef __FUNC__
165615d1e5SSatish Balay #define __FUNC__ "TSSetType"
173f3760d9SBarry Smith /*@
18ae12b187SLois Curfman McInnes    TSSetType - Sets the method for the timestepping solver.
193f3760d9SBarry Smith 
203f3760d9SBarry Smith    Input Parameters:
218b1af7b3SBarry Smith .  ts - the TS context
223f3760d9SBarry Smith .  method - a known method
233f3760d9SBarry Smith 
24ae12b187SLois Curfman McInnes   Options Database Command:
25ae12b187SLois Curfman McInnes $ -ts_type  <method>
26ae12b187SLois Curfman McInnes $    Use -help for a list of available methods
27ae12b187SLois Curfman McInnes $    (for instance, euler)
28ae12b187SLois Curfman McInnes 
293f3760d9SBarry Smith    Notes:
308b1af7b3SBarry Smith    See "petsc/include/ts.h" for available methods (for instance)
318b1af7b3SBarry Smith $   TS_EULER
32ca90a507SBarry Smith $   TS_BEULER
33ca90a507SBarry Smith $   TS_PSEUDO
343f3760d9SBarry Smith 
35ae12b187SLois Curfman McInnes   Normally, it is best to use the TSSetFromOptions() command and
36ae12b187SLois Curfman McInnes   then set the TS type from the options database rather than by using
37ae12b187SLois Curfman McInnes   this routine.  Using the options database provides the user with
38ae12b187SLois Curfman McInnes   maximum flexibility in evaluating the many different solvers.
39ae12b187SLois Curfman McInnes   The TSSetType() routine is provided for those situations where it
40ae12b187SLois Curfman McInnes   is necessary to set the timestepping solver independently of the
41ae12b187SLois Curfman McInnes   command line or options database.  This might be the case, for example,
42ae12b187SLois Curfman McInnes   when the choice of solver changes during the execution of the
43ae12b187SLois Curfman McInnes   program, and the user's application is taking responsibility for
44ae12b187SLois Curfman McInnes   choosing the appropriate method.  In other words, this routine is
45ae12b187SLois Curfman McInnes   for the advanced user.
463f3760d9SBarry Smith 
47ae12b187SLois Curfman McInnes .keywords: TS, set, type
483f3760d9SBarry Smith @*/
498b1af7b3SBarry Smith int TSSetType(TS ts,TSType method)
503f3760d9SBarry Smith {
518b1af7b3SBarry Smith   int (*r)(TS);
528b1af7b3SBarry Smith 
53c3e30b67SBarry Smith   PetscValidHeaderSpecific(ts,TS_COOKIE);
548b1af7b3SBarry Smith   /* Get the function pointers for the method requested */
558b1af7b3SBarry Smith   if (!__TSList) {TSRegisterAll();}
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);
608b1af7b3SBarry Smith   return (*r)(ts);
613f3760d9SBarry Smith }
623f3760d9SBarry Smith 
633f3760d9SBarry Smith /* --------------------------------------------------------------------- */
645615d1e5SSatish Balay #undef __FUNC__
655615d1e5SSatish Balay #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 
94*d252947aSBarry Smith   if (name == TS_NEW) name = (TSType) ((int) TS_NEW + numberregistered++);
9584cb2905SBarry Smith 
9684cb2905SBarry Smith   if (oname) *oname = name;
978b1af7b3SBarry Smith   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
9884cb2905SBarry Smith   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
993f3760d9SBarry Smith   return 0;
1003f3760d9SBarry Smith }
1013f3760d9SBarry Smith /* --------------------------------------------------------------------- */
1025615d1e5SSatish Balay #undef __FUNC__
1035615d1e5SSatish Balay #define __FUNC__ "TSRegisterDestroy"
1043f3760d9SBarry Smith /*@C
10584cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
1068b1af7b3SBarry Smith    registered by TSRegister().
1073f3760d9SBarry Smith 
1082d872ea7SLois Curfman McInnes .keywords: TS, timestepper, register, destroy
1093f3760d9SBarry Smith 
1108b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterAll()
1113f3760d9SBarry Smith @*/
1128b1af7b3SBarry Smith int TSRegisterDestroy()
1133f3760d9SBarry Smith {
1148b1af7b3SBarry Smith   if (__TSList) {
1158b1af7b3SBarry Smith     NRDestroy( __TSList );
1168b1af7b3SBarry Smith     __TSList = 0;
1173f3760d9SBarry Smith   }
11884cb2905SBarry Smith   TSRegisterAllCalled = 0;
1193f3760d9SBarry Smith   return 0;
1203f3760d9SBarry Smith }
1213f3760d9SBarry Smith 
1225615d1e5SSatish Balay #undef __FUNC__
1235615d1e5SSatish Balay #define __FUNC__ "TSGetType"
1243f3760d9SBarry Smith /*@C
1258b1af7b3SBarry Smith    TSGetType - Gets the TS method type and name (as a string).
1263f3760d9SBarry Smith 
1273f3760d9SBarry Smith    Input Parameter:
1282d872ea7SLois Curfman McInnes .  ts - timestepper solver context
1293f3760d9SBarry Smith 
1303f3760d9SBarry Smith    Output Parameter:
1318b1af7b3SBarry Smith .  method - TS method (or use PETSC_NULL)
1328b1af7b3SBarry Smith .  name - name of TS method (or use PETSC_NULL)
1333f3760d9SBarry Smith 
1342d872ea7SLois Curfman McInnes .keywords: TS, timestepper, get, method, name
1353f3760d9SBarry Smith @*/
1368b1af7b3SBarry Smith int TSGetType(TS ts, TSType *method,char **name)
1373f3760d9SBarry Smith {
1383f3760d9SBarry Smith   int ierr;
13984cb2905SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1408b1af7b3SBarry Smith   if (method) *method = (TSType) ts->type;
1418b1af7b3SBarry Smith   if (name)  *name = NRFindName( __TSList, (int) ts->type );
1423f3760d9SBarry Smith   return 0;
1433f3760d9SBarry Smith }
1443f3760d9SBarry Smith 
1453f3760d9SBarry Smith #include <stdio.h>
1465615d1e5SSatish Balay #undef __FUNC__
1475615d1e5SSatish Balay #define __FUNC__ "TSPrintTypes_Private"
1483f3760d9SBarry Smith /*
1498b1af7b3SBarry Smith    TSPrintTypes_Private - Prints the TS methods available from the
1503f3760d9SBarry Smith    options database.
1513f3760d9SBarry Smith 
1523f3760d9SBarry Smith    Input Parameters:
153b5f69acfSSatish Balay .  comm   - The communicator (usually MPI_COMM_WORLD)
1543f3760d9SBarry Smith .  prefix - prefix (usually "-")
1558b1af7b3SBarry Smith .  name   - the options database name (by default "ts_type")
1563f3760d9SBarry Smith */
157b5f69acfSSatish Balay int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
1583f3760d9SBarry Smith {
1593f3760d9SBarry Smith   FuncList *entry;
1608b1af7b3SBarry Smith   if (!__TSList) {TSRegisterAll();}
1618b1af7b3SBarry Smith   entry = __TSList->head;
162c3e30b67SBarry Smith   PetscPrintf(comm," %s%s (one of)",prefix,name);
1633f3760d9SBarry Smith   while (entry) {
164c3e30b67SBarry Smith     PetscPrintf(comm," %s",entry->name);
1653f3760d9SBarry Smith     entry = entry->next;
1663f3760d9SBarry Smith   }
167c3e30b67SBarry Smith   PetscPrintf(comm,"\n");
1683f3760d9SBarry Smith   return 0;
1693f3760d9SBarry Smith }
1703f3760d9SBarry Smith 
1718b1af7b3SBarry Smith 
1725615d1e5SSatish Balay #undef __FUNC__
1735615d1e5SSatish Balay #define __FUNC__ "TSGetTypeFromOptions_Private"
1748b1af7b3SBarry Smith /*
1758b1af7b3SBarry Smith    TSGetTypeFromOptions_Private - Sets the selected method from the
1768b1af7b3SBarry Smith    options database.
1773f3760d9SBarry Smith 
1783f3760d9SBarry Smith    Input Parameter:
1798b1af7b3SBarry Smith .  ctx - the TS context
1803f3760d9SBarry Smith 
1813f3760d9SBarry Smith    Output Parameter:
1828b1af7b3SBarry Smith .  method -  solver method
1833f3760d9SBarry Smith 
1848b1af7b3SBarry Smith    Returns:
1858b1af7b3SBarry Smith    Returns 1 if the method is found; 0 otherwise.
1863f3760d9SBarry Smith 
1878b1af7b3SBarry Smith    Options Database Key:
1888b1af7b3SBarry Smith $  -ts_type  method
1898b1af7b3SBarry Smith */
1908b1af7b3SBarry Smith int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
1913f3760d9SBarry Smith {
1928b1af7b3SBarry Smith   int ierr;
1938b1af7b3SBarry Smith   char sbuf[50];
1948b1af7b3SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
1958b1af7b3SBarry Smith   if (*flg) {
1968b1af7b3SBarry Smith     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1978b1af7b3SBarry Smith     *method = (TSType)NRFindID( __TSList, sbuf );
1988b1af7b3SBarry Smith   }
1993f3760d9SBarry Smith   return 0;
2003f3760d9SBarry Smith }
201