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