xref: /petsc/src/ts/interface/tsreg.c (revision 87828ca270d8140797fd4271705413c4ecfcb535)
1 /*$Id: tsreg.c,v 1.70 2001/08/02 20:29:40 balay 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 __FUNCT__
9 #define __FUNCT__ "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,(void (**)(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 __FUNCT__
77 #define __FUNCT__ "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 __FUNCT__
104 #define __FUNCT__ "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 __FUNCT__
131 #define __FUNCT__ "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_dt dt - initial time step
145 .  -ts_monitor - print information at each timestep
146 -  -ts_xmonitor - plot information at each timestep
147 
148    Level: beginner
149 
150 .keywords: TS, timestep, set, options, database
151 
152 .seealso: TSGetType
153 @*/
154 int TSSetFromOptions(TS ts)
155 {
156   int        ierr;
157   PetscTruth flg;
158   char       *deft,type[256];
159   PetscReal  dt;
160 
161   PetscFunctionBegin;
162   PetscValidHeaderSpecific(ts,TS_COOKIE);
163 
164   ierr = PetscOptionsBegin(ts->comm,ts->prefix,"Time step options","TS");CHKERRQ(ierr);
165     if (ts->type_name) {
166       deft = ts->type_name;
167     } else {
168       deft = TS_EULER;
169     }
170     if (!TSRegisterAllCalled) {ierr = TSRegisterAll(PETSC_NULL);CHKERRQ(ierr);}
171     ierr = PetscOptionsList("-ts_type","Timestep method","TSSetType",TSList,deft,type,256,&flg);CHKERRQ(ierr);
172     if (flg) {
173       ierr = TSSetType(ts,type);CHKERRQ(ierr);
174     } else if (!ts->type_name) {
175       ierr = TSSetType(ts,deft);CHKERRQ(ierr);
176     }
177 
178     ierr = PetscOptionsInt("-ts_max_steps","Maximum number of time steps","TSSetDuration",ts->max_steps,&ts->max_steps,PETSC_NULL);CHKERRQ(ierr);
179     ierr = PetscOptionsReal("-ts_max_time","Time to run to","TSSetDuration",ts->max_time,&ts->max_time,PETSC_NULL);CHKERRQ(ierr);
180     ierr = PetscOptionsReal("-ts_dt","Initial time step","TSSetInitialTimeStep",ts->initial_time_step,&dt,&flg);CHKERRQ(ierr);
181     if (flg) {
182       ts->initial_time_step = ts->time_step = dt;
183     }
184     ierr = PetscOptionsName("-ts_monitor","Monitor timestep size","TSDefaultMonitor",&flg);CHKERRQ(ierr);
185     if (flg) {
186       ierr = TSSetMonitor(ts,TSDefaultMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
187     }
188     ierr = PetscOptionsName("-ts_xmonitor","Monitor timestep size graphically","TSLGMonitor",&flg);CHKERRQ(ierr);
189     if (flg) {
190       ierr = TSSetMonitor(ts,TSLGMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
191     }
192     ierr = PetscOptionsName("-ts_vecmonitor","Monitor solution graphically","TSVecViewMonitor",&flg);CHKERRQ(ierr);
193     if (flg) {
194       ierr = TSSetMonitor(ts,TSVecViewMonitor,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr);
195     }
196     if (ts->setfromoptions) {
197       ierr = (*ts->setfromoptions)(ts);CHKERRQ(ierr);
198     }
199   ierr = PetscOptionsEnd();CHKERRQ(ierr);
200   PetscFunctionReturn(0);
201 }
202 
203 
204 
205