1a5eb4965SSatish Balay #ifdef PETSC_RCS_HEADER 2*ca161407SBarry Smith static char vcid[] = "$Id: tsreg.c,v 1.25 1997/10/28 14:24:05 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);} 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); 603a40ed3dSBarry Smith ierr = (*r)(ts);CHKERRQ(ierr); 613a40ed3dSBarry 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 953a40ed3dSBarry 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 ); 1013a40ed3dSBarry 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 { 1163a40ed3dSBarry Smith PetscFunctionBegin; 1178b1af7b3SBarry Smith if (__TSList) { 1188b1af7b3SBarry Smith NRDestroy( __TSList ); 1198b1af7b3SBarry Smith __TSList = 0; 1203f3760d9SBarry Smith } 12184cb2905SBarry Smith TSRegisterAllCalled = 0; 1223a40ed3dSBarry 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; 1423a40ed3dSBarry Smith 1433a40ed3dSBarry 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 ); 1473a40ed3dSBarry Smith PetscFunctionReturn(0); 1483f3760d9SBarry Smith } 1493f3760d9SBarry Smith 1505615d1e5SSatish Balay #undef __FUNC__ 151*ca161407SBarry Smith #define __FUNC__ "TSPrintHelp" 152*ca161407SBarry Smith /*@ 153*ca161407SBarry Smith TSPrintHelp - Prints all options for the TS (timestepping) component. 1543f3760d9SBarry Smith 1553f3760d9SBarry Smith Input Parameter: 156*ca161407SBarry Smith . ts - the TS context obtained from TSCreate() 1573f3760d9SBarry Smith 158*ca161407SBarry Smith Options Database Keys: 159*ca161407SBarry Smith $ -help, -h 1603f3760d9SBarry Smith 161*ca161407SBarry Smith .keywords: TS, timestep, print, help 162*ca161407SBarry Smith 163*ca161407SBarry Smith .seealso: TSSetFromOptions() 164*ca161407SBarry Smith @*/ 165*ca161407SBarry Smith int TSPrintHelp(TS ts) 1663f3760d9SBarry Smith { 167*ca161407SBarry Smith char *prefix = "-"; 1688b1af7b3SBarry Smith int ierr; 1693a40ed3dSBarry Smith 1703a40ed3dSBarry Smith PetscFunctionBegin; 171*ca161407SBarry Smith PetscValidHeaderSpecific(ts,TS_COOKIE); 172*ca161407SBarry Smith if (ts->prefix) prefix = ts->prefix; 173*ca161407SBarry Smith PetscPrintf(ts->comm,"TS options --------------------------------------------------\n"); 174*ca161407SBarry Smith ierr = NRPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",__TSList);CHKERRQ(ierr); 175*ca161407SBarry Smith PetscPrintf(ts->comm," %sts_monitor: use default TS monitor\n",prefix); 176*ca161407SBarry Smith PetscPrintf(ts->comm," %sts_view: view TS info after each solve\n",prefix); 177*ca161407SBarry Smith 178*ca161407SBarry Smith PetscPrintf(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps); 179*ca161407SBarry Smith PetscPrintf(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time); 180*ca161407SBarry Smith if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);} 1813a40ed3dSBarry Smith PetscFunctionReturn(0); 1823f3760d9SBarry Smith } 183*ca161407SBarry Smith 184*ca161407SBarry Smith #undef __FUNC__ 185*ca161407SBarry Smith #define __FUNC__ "TSSetFromOptions" 186*ca161407SBarry Smith /*@ 187*ca161407SBarry Smith TSSetFromOptions - Sets various TS parameters from user options. 188*ca161407SBarry Smith 189*ca161407SBarry Smith Input Parameter: 190*ca161407SBarry Smith . ts - the TS context obtained from TSCreate() 191*ca161407SBarry Smith 192*ca161407SBarry Smith .keywords: TS, timestep, set, options, database 193*ca161407SBarry Smith 194*ca161407SBarry Smith .seealso: TSPrintHelp() 195*ca161407SBarry Smith @*/ 196*ca161407SBarry Smith int TSSetFromOptions(TS ts) 197*ca161407SBarry Smith { 198*ca161407SBarry Smith int ierr,flg,loc[4],nmax; 199*ca161407SBarry Smith TSType method; 200*ca161407SBarry Smith 201*ca161407SBarry Smith PetscFunctionBegin; 202*ca161407SBarry Smith loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300; 203*ca161407SBarry Smith 204*ca161407SBarry Smith PetscValidHeaderSpecific(ts,TS_COOKIE); 205*ca161407SBarry Smith if (ts->setup_called) SETERRQ(1,0,"Call prior to TSSetUp!"); 206*ca161407SBarry Smith if (!__TSList) {ierr = TSRegisterAll();CHKERRQ(ierr);} 207*ca161407SBarry Smith ierr = NRGetTypeFromOptions(ts->prefix,"-ts_type",__TSList,&method,&flg);CHKERRQ(ierr); 208*ca161407SBarry Smith if (flg) { 209*ca161407SBarry Smith ierr = TSSetType(ts,method); CHKERRQ(ierr); 210*ca161407SBarry Smith } 211*ca161407SBarry Smith 212*ca161407SBarry Smith ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr); 213*ca161407SBarry Smith if (flg) {ierr = TSPrintHelp(ts);CHKERRQ(ierr);} 214*ca161407SBarry Smith ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr); 215*ca161407SBarry Smith ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr); 216*ca161407SBarry Smith ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr); 217*ca161407SBarry Smith if (flg) { 218*ca161407SBarry Smith ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr); 219*ca161407SBarry Smith } 220*ca161407SBarry Smith nmax = 4; 221*ca161407SBarry Smith ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr); 222*ca161407SBarry Smith if (flg) { 223*ca161407SBarry Smith int rank = 0; 224*ca161407SBarry Smith DrawLG lg; 225*ca161407SBarry Smith MPI_Comm_rank(ts->comm,&rank); 226*ca161407SBarry Smith if (!rank) { 227*ca161407SBarry Smith ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr); 228*ca161407SBarry Smith PLogObjectParent(ts,(PetscObject) lg); 229*ca161407SBarry Smith ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr); 230*ca161407SBarry Smith } 231*ca161407SBarry Smith } 232*ca161407SBarry Smith if (!ts->setfromoptions) PetscFunctionReturn(0); 233*ca161407SBarry Smith ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr); 234*ca161407SBarry Smith PetscFunctionReturn(0); 235*ca161407SBarry Smith } 236*ca161407SBarry Smith 237