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