xref: /petsc/src/ts/interface/tsreg.c (revision e1311b9049e89cb3452dcd306fde571f4b440ff2)
1 #ifdef PETSC_RCS_HEADER
2 static char vcid[] = "$Id: tsreg.c,v 1.33 1998/03/23 21:23:45 bsmith Exp bsmith $";
3 #endif
4 
5 #include "src/ts/tsimpl.h"      /*I "ts.h"  I*/
6 #include "src/sys/nreg.h"
7 #include "pinclude/pviewer.h"
8 #include <math.h>
9 
10 DLList TSList = 0;
11 int TSRegisterAllCalled = 0;
12 
13 #undef __FUNC__
14 #define __FUNC__ "TSSetType"
15 /*@C
16    TSSetType - Sets the method for the timestepping solver.
17 
18    Input Parameters:
19 .  ts - the TS context
20 .  method - a known method
21 
22   Options Database Command:
23 $ -ts_type  <method>
24 $    Use -help for a list of available methods
25 $    (for instance, euler)
26 
27    Notes:
28    See "petsc/include/ts.h" for available methods (for instance)
29 $   TS_EULER
30 $   TS_PVODE
31 $   TS_BEULER
32 $   TS_PSEUDO
33 
34   Normally, it is best to use the TSSetFromOptions() command and
35   then set the TS type from the options database rather than by using
36   this routine.  Using the options database provides the user with
37   maximum flexibility in evaluating the many different solvers.
38   The TSSetType() routine is provided for those situations where it
39   is necessary to set the timestepping solver independently of the
40   command line or options database.  This might be the case, for example,
41   when the choice of solver changes during the execution of the
42   program, and the user's application is taking responsibility for
43   choosing the appropriate method.  In other words, this routine is
44   for the advanced user.
45 
46 .keywords: TS, set, type
47 @*/
48 int TSSetType(TS ts,TSType method)
49 {
50   int ierr,(*r)(TS);
51 
52   PetscFunctionBegin;
53   PetscValidHeaderSpecific(ts,TS_COOKIE);
54   if (!PetscStrcmp(ts->type_name,method)) PetscFunctionReturn(0);
55 
56   /* Get the function pointers for the method requested */
57   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
58   ierr =  DLRegisterFind(ts->comm, TSList, method, (int (**)(void *)) &r );CHKERRQ(ierr);
59   if (!r) {SETERRQ(PETSC_ERR_ARG_OUTOFRANGE,0,"Unknown method");}
60 
61   if (ts->sles) {ierr = SLESDestroy(ts->sles); CHKERRQ(ierr);}
62   if (ts->snes) {ierr = SNESDestroy(ts->snes); CHKERRQ(ierr);}
63   if (ts->destroy) {ierr = (*(ts)->destroy)(ts); CHKERRQ(ierr);}
64   ts->sles = 0;
65   ts->snes = 0;
66 
67   ierr = (*r)(ts);CHKERRQ(ierr);
68 
69   if (ts->type_name) PetscFree(ts->type_name);
70   ts->type_name = (char *) PetscMalloc((PetscStrlen(method)+1)*sizeof(char));CHKPTRQ(ts->type_name);
71   PetscStrcpy(ts->type_name,method);
72   PetscFunctionReturn(0);
73 }
74 
75 /* --------------------------------------------------------------------- */
76 #undef __FUNC__
77 #define __FUNC__ "TSRegisterDestroy"
78 /*@C
79    TSRegisterDestroy - Frees the list of timesteppers that were
80    registered by DLRegister().
81 
82 .keywords: TS, timestepper, register, destroy
83 
84 .seealso: TSRegisterAll()
85 @*/
86 int TSRegisterDestroy(void)
87 {
88   int ierr;
89 
90   PetscFunctionBegin;
91   if (TSList) {
92     ierr = DLRegisterDestroy( TSList );CHKERRQ(ierr);
93     TSList = 0;
94   }
95   TSRegisterAllCalled = 0;
96   PetscFunctionReturn(0);
97 }
98 
99 #undef __FUNC__
100 #define __FUNC__ "TSGetType"
101 /*@C
102    TSGetType - Gets the TS method type and name (as a string).
103 
104    Input Parameter:
105 .  ts - timestepper solver context
106 
107    Output Parameter:
108 .  type - name of TS method
109 
110 .keywords: TS, timestepper, get, type, name
111 @*/
112 int TSGetType(TS ts, TSType *type)
113 {
114   int ierr;
115 
116   PetscFunctionBegin;
117   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL); CHKERRQ(ierr);}
118   *type = ts->type_name;
119   PetscFunctionReturn(0);
120 }
121 
122 #undef __FUNC__
123 #define __FUNC__ "TSPrintHelp"
124 /*@
125    TSPrintHelp - Prints all options for the TS (timestepping) component.
126 
127    Input Parameter:
128 .  ts - the TS context obtained from TSCreate()
129 
130    Options Database Keys:
131 $  -help, -h
132 
133 .keywords: TS, timestep, print, help
134 
135 .seealso: TSSetFromOptions()
136 @*/
137 int TSPrintHelp(TS ts)
138 {
139   char    *prefix = "-";
140   int     ierr;
141 
142   PetscFunctionBegin;
143   PetscValidHeaderSpecific(ts,TS_COOKIE);
144   if (ts->prefix) prefix = ts->prefix;
145   (*PetscHelpPrintf)(ts->comm,"TS options --------------------------------------------------\n");
146   ierr = DLRegisterPrintTypes(ts->comm,stdout,ts->prefix,"ts_type",TSList);CHKERRQ(ierr);
147   (*PetscHelpPrintf)(ts->comm," %sts_monitor: use default TS monitor\n",prefix);
148   (*PetscHelpPrintf)(ts->comm," %sts_view: view TS info after each solve\n",prefix);
149 
150   (*PetscHelpPrintf)(ts->comm," %sts_max_steps <steps>: maximum steps, defaults to %d\n",prefix,ts->max_steps);
151   (*PetscHelpPrintf)(ts->comm," %sts_max_time <steps>: maximum time, defaults to %g\n",prefix,ts->max_time);
152   if (ts->printhelp) {ierr = (*ts->printhelp)(ts,prefix);CHKERRQ(ierr);}
153   PetscFunctionReturn(0);
154 }
155 
156 #undef __FUNC__
157 #define __FUNC__ "TSSetFromOptions"
158 /*@
159    TSSetFromOptions - Sets various TS parameters from user options.
160 
161    Input Parameter:
162 .  ts - the TS context obtained from TSCreate()
163 
164 .keywords: TS, timestep, set, options, database
165 
166 .seealso: TSPrintHelp()
167 @*/
168 int TSSetFromOptions(TS ts)
169 {
170   int    ierr,flg,loc[4],nmax;
171   char   type[256];
172 
173   PetscFunctionBegin;
174   loc[0] = PETSC_DECIDE; loc[1] = PETSC_DECIDE; loc[2] = 300; loc[3] = 300;
175 
176   PetscValidHeaderSpecific(ts,TS_COOKIE);
177   if (ts->setupcalled) SETERRQ(PETSC_ERR_ARG_WRONGSTATE,0,"Must call prior to TSSetUp!");
178   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
179   ierr = OptionsGetString(ts->prefix,"-ts_type",(char *) type,256,&flg);
180   if (flg) {
181     ierr = TSSetType(ts,type); CHKERRQ(ierr);
182   }
183 
184   ierr = OptionsGetInt(ts->prefix,"-ts_max_steps",&ts->max_steps,&flg);CHKERRQ(ierr);
185   ierr = OptionsGetDouble(ts->prefix,"-ts_max_time",&ts->max_time,&flg);CHKERRQ(ierr);
186   ierr = OptionsHasName(ts->prefix,"-ts_monitor",&flg); CHKERRQ(ierr);
187   if (flg) {
188     ierr = TSSetMonitor(ts,TSDefaultMonitor,0);CHKERRQ(ierr);
189   }
190   nmax = 4;
191   ierr = OptionsGetIntArray(ts->prefix,"-ts_xmonitor",loc,&nmax,&flg); CHKERRQ(ierr);
192   if (flg) {
193     int    rank = 0;
194     DrawLG lg;
195     MPI_Comm_rank(ts->comm,&rank);
196     if (!rank) {
197       ierr = TSLGMonitorCreate(0,0,loc[0],loc[1],loc[2],loc[3],&lg); CHKERRQ(ierr);
198       PLogObjectParent(ts,(PetscObject) lg);
199       ierr = TSSetMonitor(ts,TSLGMonitor,(void *)lg);CHKERRQ(ierr);
200     }
201   }
202   if (!ts->type_name) {
203     ierr = TSSetType(ts,TS_EULER);CHKERRQ(ierr);
204   }
205   ierr = OptionsHasName(PETSC_NULL,"-help",&flg); CHKERRQ(ierr);
206   if (flg)  {ierr = TSPrintHelp(ts);CHKERRQ(ierr);}
207   if (!ts->setfromoptions) PetscFunctionReturn(0);
208   ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
209   PetscFunctionReturn(0);
210 }
211 
212 
213