xref: /petsc/src/ts/interface/tsreg.c (revision b0a32e0c6855ee6a6cd3495fa7da12ea9885bc5d)
1 /*$Id: tsreg.c,v 1.65 2000/09/28 21:14:45 bsmith Exp bsmith $*/
2 
3 #include "src/ts/tsimpl.h"      /*I "petscts.h"  I*/
4 
5 PetscFList      TSList              = 0;
6 PetscTruth TSRegisterAllCalled = PETSC_FALSE;
7 
8 #undef __FUNC__
9 #define __FUNC__ "TSSetType"
10 /*@C
11    TSSetType - Sets the method for the timestepping solver.
12 
13    Collective on TS
14 
15    Input Parameters:
16 +  ts - the TS context
17 -  type - a known method
18 
19    Options Database Command:
20 .  -ts_type <type> - Sets the method; use -help for a list
21    of available methods (for instance, euler)
22 
23    Notes:
24    See "petsc/include/petscts.h" for available methods (for instance)
25 +  TS_EULER - Euler
26 .  TS_PVODE - PVODE interface
27 .  TS_BEULER - Backward Euler
28 -  TS_PSEUDO - Pseudo-timestepping
29 
30    Normally, it is best to use the TSSetFromOptions() command and
31    then set the TS type from the options database rather than by using
32    this routine.  Using the options database provides the user with
33    maximum flexibility in evaluating the many different solvers.
34    The TSSetType() routine is provided for those situations where it
35    is necessary to set the timestepping solver independently of the
36    command line or options database.  This might be the case, for example,
37    when the choice of solver changes during the execution of the
38    program, and the user's application is taking responsibility for
39    choosing the appropriate method.  In other words, this routine is
40    not for beginners.
41 
42    Level: intermediate
43 
44 .keywords: TS, set, type
45 @*/
46 int TSSetType(TS ts,TSType type)
47 {
48   int        ierr,(*r)(TS);
49   PetscTruth match;
50 
51   PetscFunctionBegin;
52   PetscValidHeaderSpecific(ts,TS_COOKIE);
53   PetscValidCharPointer(type);
54 
55   ierr = PetscTypeCompare((PetscObject)ts,type,&match);CHKERRQ(ierr);
56   if (match) PetscFunctionReturn(0);
57 
58   /* Get the function pointers for the method requested */
59   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
60   ierr =  PetscFListFind(ts->comm,TSList,type,(int (**)(void *)) &r);CHKERRQ(ierr);
61   if (!r) {SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE,"Unknown type: %s",type);}
62 
63   if (ts->sles) {ierr = SLESDestroy(ts->sles);CHKERRQ(ierr);}
64   if (ts->snes) {ierr = SNESDestroy(ts->snes);CHKERRQ(ierr);}
65   if (ts->destroy) {ierr = (*(ts)->destroy)(ts);CHKERRQ(ierr);}
66   ts->sles = 0;
67   ts->snes = 0;
68 
69   ierr = (*r)(ts);CHKERRQ(ierr);
70 
71   ierr = PetscObjectChangeTypeName((PetscObject)ts,type);CHKERRQ(ierr);
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 PetscFListAddDynamic().
81 
82    Not Collective
83 
84    Level: advanced
85 
86 .keywords: TS, timestepper, register, destroy
87 
88 .seealso: TSRegisterAll()
89 @*/
90 int TSRegisterDestroy(void)
91 {
92   int ierr;
93 
94   PetscFunctionBegin;
95   if (TSList) {
96     ierr = PetscFListDestroy(&TSList);CHKERRQ(ierr);
97     TSList = 0;
98   }
99   TSRegisterAllCalled = PETSC_FALSE;
100   PetscFunctionReturn(0);
101 }
102 
103 #undef __FUNC__
104 #define __FUNC__ "TSGetType"
105 /*@C
106    TSGetType - Gets the TS method type (as a string).
107 
108    Not Collective
109 
110    Input Parameter:
111 .  ts - timestepper solver context
112 
113    Output Parameter:
114 .  type - name of TS method
115 
116    Level: intermediate
117 
118 .keywords: TS, timestepper, get, type, name
119 @*/
120 int TSGetType(TS ts,TSType *type)
121 {
122   int ierr;
123 
124   PetscFunctionBegin;
125   if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
126   *type = ts->type_name;
127   PetscFunctionReturn(0);
128 }
129 
130 #undef __FUNC__
131 #define __FUNC__ "TSSetFromOptions"
132 /*@
133    TSSetFromOptions - Sets various TS parameters from user options.
134 
135    Collective on TS
136 
137    Input Parameter:
138 .  ts - the TS context obtained from TSCreate()
139 
140    Options Database Keys:
141 +  -ts_type <type> - TS_EULER, TS_BEULER, TS_PVODE, TS_PSEUDO, TS_CRANK_NICHOLSON
142 .  -ts_max_steps maxsteps - maximum number of time-steps to take
143 .  -ts_max_time time - maximum time to compute to
144 .  -ts_monitor - print information at each timestep
145 -  -ts_xmonitor - plot information at each timestep
146 
147    Level: beginner
148 
149 .keywords: TS, timestep, set, options, database
150 
151 .seealso: TSSetTypeFromOptions()
152 @*/
153 int TSSetFromOptions(TS ts)
154 {
155   int        ierr;
156   PetscTruth flg;
157   char       *deft,type[256];
158 
159   PetscFunctionBegin;
160   PetscValidHeaderSpecific(ts,TS_COOKIE);
161 
162   ierr = PetscOptionsBegin(ts->comm,ts->prefix,"Time step options","TS");CHKERRQ(ierr);
163     if (ts->type_name) {
164       deft = ts->type_name;
165     } else {
166       deft = TS_EULER;
167     }
168     if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
169     ierr = PetscOptionsList("-ts_type","Timestep method","TSSetType",TSList,deft,type,256,&flg);CHKERRQ(ierr);
170     if (flg) {
171       ierr = TSSetType(ts,type);CHKERRQ(ierr);
172     } else if (!ts->type_name) {
173       ierr = TSSetType(ts,deft);CHKERRQ(ierr);
174     }
175 
176     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
177     ierr = PetscOptionsDouble("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
178     ierr = PetscOptionsName("-ts_monitor","Monitor timestep size","TSDefaultMonitor",&flg);CHKERRQ(ierr);
179     if (flg) {
180       ierr = TSSetMonitor(ts,TSDefaultMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
181     }
182     ierr = PetscOptionsName("-ts_xmonitor","Monitor timestep size graphically","TSLGMonitor",&flg);CHKERRQ(ierr);
183     if (flg) {
184       ierr = TSSetMonitor(ts,TSLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
185     }
186     if (ts->setfromoptions) {
187       ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
188     }
189   ierr = PetscOptionsEnd();CHKERRQ(ierr);
190   PetscFunctionReturn(0);
191 }
192 
193 
194 
195