xref: /petsc/src/ts/interface/tsreg.c (revision 84cb2905a512499b542fd30cf471a05f503d96d2)
18b1af7b3SBarry Smith 
2fae171e0SBarry Smith 
33f3760d9SBarry Smith #ifndef lint
4*84cb2905SBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.15 1997/01/06 20:28:06 balay 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;
13*84cb2905SBarry 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
678b1af7b3SBarry Smith    TSRegister - Adds the method to the nonlinear solver package, given
688b1af7b3SBarry Smith    a function pointer and a nonlinear solver name of the type TSType.
693f3760d9SBarry Smith 
703f3760d9SBarry Smith    Input Parameters:
71*84cb2905SBarry Smith .  name - for instance TS_BEULER, or TS_NEW for a new method
723f3760d9SBarry Smith .  sname - corfunPonding string for name
733f3760d9SBarry Smith .  create - routine to create method context
743f3760d9SBarry Smith 
75*84cb2905SBarry Smith    Output Parameter:
76*84cb2905SBarry Smith .   oname - type associated with this new method
77*84cb2905SBarry Smith 
788b1af7b3SBarry Smith .keywords: TS, nonlinear, register
793f3760d9SBarry Smith 
808b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterDestroy()
813f3760d9SBarry Smith @*/
82*84cb2905SBarry Smith int TSRegister(TSType name,TSType *oname, char *sname, int (*create)(TS))
833f3760d9SBarry Smith {
843f3760d9SBarry Smith   int ierr;
85*84cb2905SBarry Smith   static int numberregistered = 0;
86*84cb2905SBarry Smith 
87*84cb2905SBarry Smith   if (name == TS_NEW) name = TS_NEW + numberregistered++;
88*84cb2905SBarry Smith 
89*84cb2905SBarry Smith   if (oname) *oname = name;
908b1af7b3SBarry Smith   if (!__TSList) {ierr = NRCreate(&__TSList); CHKERRQ(ierr);}
91*84cb2905SBarry Smith   NRRegister( __TSList, (int) name, sname, (int (*)(void*))create );
923f3760d9SBarry Smith   return 0;
933f3760d9SBarry Smith }
943f3760d9SBarry Smith /* --------------------------------------------------------------------- */
955615d1e5SSatish Balay #undef __FUNC__
965615d1e5SSatish Balay #define __FUNC__ "TSRegisterDestroy"
973f3760d9SBarry Smith /*@C
98*84cb2905SBarry Smith    TSRegisterDestroy - Frees the list of timesteppers that were
998b1af7b3SBarry Smith    registered by TSRegister().
1003f3760d9SBarry Smith 
1018b1af7b3SBarry Smith .keywords: TS, nonlinear, register, destroy
1023f3760d9SBarry Smith 
1038b1af7b3SBarry Smith .seealso: TSRegisterAll(), TSRegisterAll()
1043f3760d9SBarry Smith @*/
1058b1af7b3SBarry Smith int TSRegisterDestroy()
1063f3760d9SBarry Smith {
1078b1af7b3SBarry Smith   if (__TSList) {
1088b1af7b3SBarry Smith     NRDestroy( __TSList );
1098b1af7b3SBarry Smith     __TSList = 0;
1103f3760d9SBarry Smith   }
111*84cb2905SBarry Smith   TSRegisterAllCalled = 0;
1123f3760d9SBarry Smith   return 0;
1133f3760d9SBarry Smith }
1143f3760d9SBarry Smith 
1155615d1e5SSatish Balay #undef __FUNC__
1165615d1e5SSatish Balay #define __FUNC__ "TSGetType"
1173f3760d9SBarry Smith /*@C
1188b1af7b3SBarry Smith    TSGetType - Gets the TS method type and name (as a string).
1193f3760d9SBarry Smith 
1203f3760d9SBarry Smith    Input Parameter:
1218b1af7b3SBarry Smith .  ts - nonlinear solver context
1223f3760d9SBarry Smith 
1233f3760d9SBarry Smith    Output Parameter:
1248b1af7b3SBarry Smith .  method - TS method (or use PETSC_NULL)
1258b1af7b3SBarry Smith .  name - name of TS method (or use PETSC_NULL)
1263f3760d9SBarry Smith 
1278b1af7b3SBarry Smith .keywords: TS, nonlinear, get, method, name
1283f3760d9SBarry Smith @*/
1298b1af7b3SBarry Smith int TSGetType(TS ts, TSType *method,char **name)
1303f3760d9SBarry Smith {
1313f3760d9SBarry Smith   int ierr;
132*84cb2905SBarry Smith   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1338b1af7b3SBarry Smith   if (method) *method = (TSType) ts->type;
1348b1af7b3SBarry Smith   if (name)  *name = NRFindName( __TSList, (int) ts->type );
1353f3760d9SBarry Smith   return 0;
1363f3760d9SBarry Smith }
1373f3760d9SBarry Smith 
1383f3760d9SBarry Smith #include <stdio.h>
1395615d1e5SSatish Balay #undef __FUNC__
1405615d1e5SSatish Balay #define __FUNC__ "TSPrintTypes_Private"
1413f3760d9SBarry Smith /*
1428b1af7b3SBarry Smith    TSPrintTypes_Private - Prints the TS methods available from the
1433f3760d9SBarry Smith    options database.
1443f3760d9SBarry Smith 
1453f3760d9SBarry Smith    Input Parameters:
146b5f69acfSSatish Balay .  comm   - The communicator (usually MPI_COMM_WORLD)
1473f3760d9SBarry Smith .  prefix - prefix (usually "-")
1488b1af7b3SBarry Smith .  name   - the options database name (by default "ts_type")
1493f3760d9SBarry Smith */
150b5f69acfSSatish Balay int TSPrintTypes_Private(MPI_Comm comm,char* prefix,char *name)
1513f3760d9SBarry Smith {
1523f3760d9SBarry Smith   FuncList *entry;
1538b1af7b3SBarry Smith   if (!__TSList) {TSRegisterAll();}
1548b1af7b3SBarry Smith   entry = __TSList->head;
155c3e30b67SBarry Smith   PetscPrintf(comm," %s%s (one of)",prefix,name);
1563f3760d9SBarry Smith   while (entry) {
157c3e30b67SBarry Smith     PetscPrintf(comm," %s",entry->name);
1583f3760d9SBarry Smith     entry = entry->next;
1593f3760d9SBarry Smith   }
160c3e30b67SBarry Smith   PetscPrintf(comm,"\n");
1613f3760d9SBarry Smith   return 0;
1623f3760d9SBarry Smith }
1633f3760d9SBarry Smith 
1648b1af7b3SBarry Smith 
1655615d1e5SSatish Balay #undef __FUNC__
1665615d1e5SSatish Balay #define __FUNC__ "TSGetTypeFromOptions_Private"
1678b1af7b3SBarry Smith /*
1688b1af7b3SBarry Smith    TSGetTypeFromOptions_Private - Sets the selected method from the
1698b1af7b3SBarry Smith    options database.
1703f3760d9SBarry Smith 
1713f3760d9SBarry Smith    Input Parameter:
1728b1af7b3SBarry Smith .  ctx - the TS context
1733f3760d9SBarry Smith 
1743f3760d9SBarry Smith    Output Parameter:
1758b1af7b3SBarry Smith .  method -  solver method
1763f3760d9SBarry Smith 
1778b1af7b3SBarry Smith    Returns:
1788b1af7b3SBarry Smith    Returns 1 if the method is found; 0 otherwise.
1793f3760d9SBarry Smith 
1808b1af7b3SBarry Smith    Options Database Key:
1818b1af7b3SBarry Smith $  -ts_type  method
1828b1af7b3SBarry Smith */
1838b1af7b3SBarry Smith int TSGetTypeFromOptions_Private(TS ctx,TSType *method,int *flg)
1843f3760d9SBarry Smith {
1858b1af7b3SBarry Smith   int ierr;
1868b1af7b3SBarry Smith   char sbuf[50];
1878b1af7b3SBarry Smith   ierr = OptionsGetString(ctx->prefix,"-ts_type", sbuf, 50, flg); CHKERRQ(ierr);
1888b1af7b3SBarry Smith   if (*flg) {
1898b1af7b3SBarry Smith     if (!__TSList) {ierr = TSRegisterAll(); CHKERRQ(ierr);}
1908b1af7b3SBarry Smith     *method = (TSType)NRFindID( __TSList, sbuf );
1918b1af7b3SBarry Smith   }
1923f3760d9SBarry Smith   return 0;
1933f3760d9SBarry Smith }
194