xref: /petsc/src/ts/interface/tsreg.c (revision 94b7f48cc472a54ea2ce57edf1fe19e8a254237c)
173f4d377SMatthew Knepley /*$Id: tsreg.c,v 1.71 2001/08/06 21:18:08 bsmith Exp $*/
23f3760d9SBarry Smith 
3e090d566SSatish Balay #include "src/ts/tsimpl.h"      /*I "petscts.h"  I*/
43f3760d9SBarry Smith 
5bdad233fSMatthew Knepley PetscFList TSList                       = PETSC_NULL;
64c49b128SBarry Smith PetscTruth TSRegisterAllCalled          = PETSC_FALSE;
72030b9a2SMatthew Knepley PetscFList TSSerializeList              = PETSC_NULL;
82030b9a2SMatthew Knepley PetscTruth TSSerializeRegisterAllCalled = PETSC_FALSE;
93f3760d9SBarry Smith 
104a2ae208SSatish Balay #undef __FUNCT__
114a2ae208SSatish Balay #define __FUNCT__ "TSSetType"
1282bf6240SBarry Smith /*@C
13ae12b187SLois Curfman McInnes   TSSetType - Sets the method for the timestepping solver.
143f3760d9SBarry Smith 
15fee21e36SBarry Smith   Collective on TS
16fee21e36SBarry Smith 
17bef22f13SLois Curfman McInnes   Input Parameters:
18bdad233fSMatthew Knepley + ts   - The TS context
19bdad233fSMatthew Knepley - type - A known method
20bef22f13SLois Curfman McInnes 
21ae12b187SLois Curfman McInnes   Options Database Command:
22bdad233fSMatthew Knepley . -ts_type <type> - Sets the method; use -help for a list of available methods (for instance, euler)
23ae12b187SLois Curfman McInnes 
243f3760d9SBarry Smith    Notes:
25e090d566SSatish Balay    See "petsc/include/petscts.h" for available methods (for instance)
26d5d37b61SLois Curfman McInnes +  TS_EULER - Euler
27bef22f13SLois Curfman McInnes .  TS_PVODE - PVODE interface
28bef22f13SLois Curfman McInnes .  TS_BEULER - Backward Euler
29d5d37b61SLois Curfman McInnes -  TS_PSEUDO - Pseudo-timestepping
303f3760d9SBarry Smith 
31ae12b187SLois Curfman McInnes    Normally, it is best to use the TSSetFromOptions() command and
32ae12b187SLois Curfman McInnes    then set the TS type from the options database rather than by using
33ae12b187SLois Curfman McInnes    this routine.  Using the options database provides the user with
34ae12b187SLois Curfman McInnes    maximum flexibility in evaluating the many different solvers.
35ae12b187SLois Curfman McInnes    The TSSetType() routine is provided for those situations where it
36ae12b187SLois Curfman McInnes    is necessary to set the timestepping solver independently of the
37ae12b187SLois Curfman McInnes    command line or options database.  This might be the case, for example,
38ae12b187SLois Curfman McInnes    when the choice of solver changes during the execution of the
39ae12b187SLois Curfman McInnes    program, and the user's application is taking responsibility for
40ae12b187SLois Curfman McInnes    choosing the appropriate method.  In other words, this routine is
41d5d37b61SLois Curfman McInnes    not for beginners.
42d5d37b61SLois Curfman McInnes 
43d5d37b61SLois Curfman McInnes    Level: intermediate
443f3760d9SBarry Smith 
45ae12b187SLois Curfman McInnes .keywords: TS, set, type
46bdad233fSMatthew Knepley .seealso TSSetSerializeType()
473f3760d9SBarry Smith @*/
48454a90a3SBarry Smith int TSSetType(TS ts, TSType type)
493f3760d9SBarry Smith {
50bdad233fSMatthew Knepley   int      (*r)(TS);
516831982aSBarry Smith   PetscTruth match;
52df8cb225SBarry Smith   int        ierr;
53df8cb225SBarry Smith 
543a40ed3dSBarry Smith   PetscFunctionBegin;
55bdad233fSMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
56bdad233fSMatthew Knepley   ierr = PetscTypeCompare((PetscObject) ts, type, &match);                                                CHKERRQ(ierr);
57bdad233fSMatthew Knepley   if (match == PETSC_TRUE) PetscFunctionReturn(0);
58bdad233fSMatthew Knepley 
59bdad233fSMatthew Knepley   /* Get the function pointers for the method requested */
60bdad233fSMatthew Knepley   if (TSRegisterAllCalled == PETSC_FALSE) {
61bdad233fSMatthew Knepley     ierr = TSRegisterAll(PETSC_NULL);                                                                     CHKERRQ(ierr);
623f3760d9SBarry Smith   }
63bdad233fSMatthew Knepley   ierr = PetscFListFind(ts->comm, TSList, type, (void (**)(void)) &r);                                    CHKERRQ(ierr);
64bdad233fSMatthew Knepley   if (!r) SETERRQ1(PETSC_ERR_ARG_OUTOFRANGE, "Unknown TS type: %s", type);
65bdad233fSMatthew Knepley 
66*94b7f48cSBarry Smith   if (ts->ksp != PETSC_NULL) {
67*94b7f48cSBarry Smith     ierr = KSPDestroy(ts->ksp);                                                                         CHKERRQ(ierr);
68*94b7f48cSBarry Smith     ts->ksp = PETSC_NULL;
69bdad233fSMatthew Knepley   }
70bdad233fSMatthew Knepley   if (ts->snes != PETSC_NULL) {
71bdad233fSMatthew Knepley     ierr = SNESDestroy(ts->snes);                                                                         CHKERRQ(ierr);
72bdad233fSMatthew Knepley     ts->snes = PETSC_NULL;
73bdad233fSMatthew Knepley   }
74bdad233fSMatthew Knepley   if (ts->ops->destroy != PETSC_NULL) {
75bdad233fSMatthew Knepley     ierr = (*(ts)->ops->destroy)(ts);                                                                     CHKERRQ(ierr);
76bdad233fSMatthew Knepley   }
77bdad233fSMatthew Knepley   ierr = (*r)(ts);                                                                                        CHKERRQ(ierr);
78bdad233fSMatthew Knepley 
79bdad233fSMatthew Knepley   ierr = PetscObjectChangeTypeName((PetscObject)ts, type);                                                CHKERRQ(ierr);
803a40ed3dSBarry Smith   PetscFunctionReturn(0);
813f3760d9SBarry Smith }
823f3760d9SBarry Smith 
834a2ae208SSatish Balay #undef __FUNCT__
844a2ae208SSatish Balay #define __FUNCT__ "TSGetType"
853f3760d9SBarry Smith /*@C
86fee21e36SBarry Smith   TSGetType - Gets the TS method type (as a string).
873f3760d9SBarry Smith 
88bef22f13SLois Curfman McInnes   Not Collective
89bef22f13SLois Curfman McInnes 
903f3760d9SBarry Smith   Input Parameter:
91bdad233fSMatthew Knepley . ts - The TS
923f3760d9SBarry Smith 
933f3760d9SBarry Smith   Output Parameter:
94bdad233fSMatthew Knepley . type - The name of TS method
953f3760d9SBarry Smith 
96d5d37b61SLois Curfman McInnes   Level: intermediate
97d5d37b61SLois Curfman McInnes 
98df8cb225SBarry Smith .keywords: TS, timestepper, get, type, name
99bdad233fSMatthew Knepley .seealso TSSetType()
1003f3760d9SBarry Smith @*/
10182bf6240SBarry Smith int TSGetType(TS ts, TSType *type)
1023f3760d9SBarry Smith {
1033f3760d9SBarry Smith   int ierr;
1043a40ed3dSBarry Smith 
1053a40ed3dSBarry Smith   PetscFunctionBegin;
106bdad233fSMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
107bdad233fSMatthew Knepley   PetscValidPointer(type);
108bdad233fSMatthew Knepley   if (TSRegisterAllCalled == PETSC_FALSE) {
109bdad233fSMatthew Knepley     ierr = TSRegisterAll(PETSC_NULL);                                                                     CHKERRQ(ierr);
110bdad233fSMatthew Knepley   }
11182bf6240SBarry Smith   *type = ts->type_name;
1123a40ed3dSBarry Smith   PetscFunctionReturn(0);
1133f3760d9SBarry Smith }
1143f3760d9SBarry Smith 
1154a2ae208SSatish Balay #undef __FUNCT__
116bdad233fSMatthew Knepley #define __FUNCT__ "TSSetSerializeType"
117bdad233fSMatthew Knepley /*@C
118bdad233fSMatthew Knepley   TSSetSerializeType - Sets the serialization method for the ts.
119ca161407SBarry Smith 
120bef22f13SLois Curfman McInnes   Collective on TS
121bef22f13SLois Curfman McInnes 
122bdad233fSMatthew Knepley   Input Parameters:
123bdad233fSMatthew Knepley + ts     - The TS context
124bdad233fSMatthew Knepley - method - A known method
125ca161407SBarry Smith 
126bdad233fSMatthew Knepley   Options Database Command:
127bdad233fSMatthew Knepley . -ts_serialize_type <method> - Sets the method; use -help for a list
128bdad233fSMatthew Knepley                                 of available methods (for instance, gbeuler_binary)
12915091d37SBarry Smith 
130bdad233fSMatthew Knepley   Notes:
131bdad233fSMatthew Knepley   See "petsc/include/ts.h" for available methods (for instance)
132bdad233fSMatthew Knepley . GTS_SER_BEULER_BINARY - Grid Backwards Euler TS to binary file
133d5d37b61SLois Curfman McInnes 
134bdad233fSMatthew Knepley   Normally, it is best to use the TSSetFromOptions() command and
135bdad233fSMatthew Knepley   then set the TS type from the options database rather than by using
136bdad233fSMatthew Knepley   this routine.  Using the options database provides the user with
137bdad233fSMatthew Knepley   maximum flexibility in evaluating the many different solvers.
138bdad233fSMatthew Knepley   The TSSetSerializeType() routine is provided for those situations
139bdad233fSMatthew Knepley   where it is necessary to set the application ordering independently of the
140bdad233fSMatthew Knepley   command line or options database.  This might be the case, for example,
141bdad233fSMatthew Knepley   when the choice of solver changes during the execution of the
142bdad233fSMatthew Knepley   program, and the user's application is taking responsibility for
143bdad233fSMatthew Knepley   choosing the appropriate method.  In other words, this routine is
144bdad233fSMatthew Knepley   not for beginners.
145ca161407SBarry Smith 
146bdad233fSMatthew Knepley   Level: intermediate
147bdad233fSMatthew Knepley 
148bdad233fSMatthew Knepley .keywords: TS, set, type, serialization
149bdad233fSMatthew Knepley .seealso TSSetType()
150ca161407SBarry Smith @*/
151bdad233fSMatthew Knepley int TSSetSerializeType(TS ts, TSSerializeType method)
152ca161407SBarry Smith {
153bdad233fSMatthew Knepley   int      (*r)(MPI_Comm, TS *, PetscViewer, PetscTruth);
154bdad233fSMatthew Knepley   PetscTruth match;
1554bbc92c1SBarry Smith   int        ierr;
156ca161407SBarry Smith 
157ca161407SBarry Smith   PetscFunctionBegin;
158ca161407SBarry Smith   PetscValidHeaderSpecific(ts, TS_COOKIE);
159bdad233fSMatthew Knepley   ierr = PetscSerializeCompare((PetscObject) ts, method, &match);                                         CHKERRQ(ierr);
160bdad233fSMatthew Knepley   if (match == PETSC_TRUE) PetscFunctionReturn(0);
161ca161407SBarry Smith 
162bdad233fSMatthew Knepley   /* Get the function pointers for the method requested but do not call */
163bdad233fSMatthew Knepley   if (TSSerializeRegisterAllCalled == PETSC_FALSE) {
164bdad233fSMatthew Knepley     ierr = TSSerializeRegisterAll(PETSC_NULL);                                                            CHKERRQ(ierr);
1654bbc92c1SBarry Smith   }
166bdad233fSMatthew Knepley   ierr = PetscFListFind(ts->comm, TSSerializeList, method, (void (**)(void)) &r);                         CHKERRQ(ierr);
167bdad233fSMatthew Knepley   if (!r) SETERRQ1(PETSC_ERR_ARG_WRONG, "Unknown ts serialization type: %s", method);
1684bbc92c1SBarry Smith 
169bdad233fSMatthew Knepley   ierr = PetscObjectChangeSerializeName((PetscObject) ts, method);                                        CHKERRQ(ierr);
170ca161407SBarry Smith   PetscFunctionReturn(0);
171ca161407SBarry Smith }
172ca161407SBarry Smith 
173bdad233fSMatthew Knepley #undef __FUNCT__
174bdad233fSMatthew Knepley #define __FUNCT__ "TSGetSerializeType"
175bdad233fSMatthew Knepley /*@C
176bdad233fSMatthew Knepley   TSGetSerializeType - Gets the TS serialization method (as a string).
17782bf6240SBarry Smith 
178bdad233fSMatthew Knepley   Not collective
179184914b5SBarry Smith 
180bdad233fSMatthew Knepley   Input Parameter:
181bdad233fSMatthew Knepley . ts   - The ts
182bdad233fSMatthew Knepley 
183bdad233fSMatthew Knepley   Output Parameter:
184bdad233fSMatthew Knepley . type - The name of TS serialization method
185bdad233fSMatthew Knepley 
186bdad233fSMatthew Knepley   Level: intermediate
187bdad233fSMatthew Knepley 
188bdad233fSMatthew Knepley .keywords: TS, get, serialize, type, name
189bdad233fSMatthew Knepley .seealso TSSetType()
190bdad233fSMatthew Knepley @*/
191bdad233fSMatthew Knepley int TSGetSerializeType(TS ts, TSSerializeType *type)
192bdad233fSMatthew Knepley {
193bdad233fSMatthew Knepley   int ierr;
194bdad233fSMatthew Knepley 
195bdad233fSMatthew Knepley   PetscFunctionBegin;
196bdad233fSMatthew Knepley   PetscValidHeaderSpecific(ts, TS_COOKIE);
197bdad233fSMatthew Knepley   PetscValidPointer(type);
198bdad233fSMatthew Knepley   if (TSSerializeRegisterAllCalled == PETSC_FALSE) {
199bdad233fSMatthew Knepley     ierr = TSSerializeRegisterAll(PETSC_NULL);                                                            CHKERRQ(ierr);
200bdad233fSMatthew Knepley   }
201bdad233fSMatthew Knepley   *type = ts->serialize_name;
202bdad233fSMatthew Knepley   PetscFunctionReturn(0);
203bdad233fSMatthew Knepley }
204bdad233fSMatthew Knepley 
205bdad233fSMatthew Knepley /*--------------------------------------------------------------------------------------------------------------------*/
2063cea93caSBarry Smith 
207bdad233fSMatthew Knepley #undef __FUNCT__
208bdad233fSMatthew Knepley #define __FUNCT__ "TSRegister"
2093cea93caSBarry Smith /*@C
2103cea93caSBarry Smith   TSRegister - See TSRegisterDynamic()
2113cea93caSBarry Smith 
2127f6c08e0SMatthew Knepley   Level: advanced
2133cea93caSBarry Smith @*/
214bdad233fSMatthew Knepley int TSRegister(const char sname[], const char path[], const char name[], int (*function)(TS))
215bdad233fSMatthew Knepley {
216bdad233fSMatthew Knepley   char fullname[256];
217bdad233fSMatthew Knepley   int  ierr;
218bdad233fSMatthew Knepley 
219bdad233fSMatthew Knepley   PetscFunctionBegin;
220bdad233fSMatthew Knepley   ierr = PetscStrcpy(fullname, path);                                                                     CHKERRQ(ierr);
221bdad233fSMatthew Knepley   ierr = PetscStrcat(fullname, ":");                                                                      CHKERRQ(ierr);
222bdad233fSMatthew Knepley   ierr = PetscStrcat(fullname, name);                                                                     CHKERRQ(ierr);
223bdad233fSMatthew Knepley   ierr = PetscFListAdd(&TSList, sname, fullname, (void (*)(void)) function);                              CHKERRQ(ierr);
224bdad233fSMatthew Knepley   PetscFunctionReturn(0);
225bdad233fSMatthew Knepley }
226bdad233fSMatthew Knepley 
227bdad233fSMatthew Knepley /*@C
228bdad233fSMatthew Knepley   TSSerializeRegister - Adds a serialization method to the ts package.
229bdad233fSMatthew Knepley 
230bdad233fSMatthew Knepley   Synopsis:
231bdad233fSMatthew Knepley 
232bdad233fSMatthew Knepley   TSSerializeRegister(char *name, char *path, char *func_name,
233bdad233fSMatthew Knepley                         int (*serialize_func)(MPI_Comm, TS *, PetscViewer, PetscTruth))
234bdad233fSMatthew Knepley 
235bdad233fSMatthew Knepley   Not Collective
236bdad233fSMatthew Knepley 
237bdad233fSMatthew Knepley   Input Parameters:
238bdad233fSMatthew Knepley + name           - The name of a new user-defined serialization routine
239bdad233fSMatthew Knepley . path           - The path (either absolute or relative) of the library containing this routine
240bdad233fSMatthew Knepley . func_name      - The name of the serialization routine
241bdad233fSMatthew Knepley - serialize_func - The serialization routine itself
242bdad233fSMatthew Knepley 
243bdad233fSMatthew Knepley   Notes:
244bdad233fSMatthew Knepley   TSSerializeRegister() may be called multiple times to add several user-defined serializers.
245bdad233fSMatthew Knepley 
246bdad233fSMatthew Knepley   If dynamic libraries are used, then the fourth input argument (serialize_func) is ignored.
247bdad233fSMatthew Knepley 
248bdad233fSMatthew Knepley   Sample usage:
249bdad233fSMatthew Knepley .vb
250bdad233fSMatthew Knepley   TSSerializeRegisterDynamic("my_store", "/home/username/my_lib/lib/libO/solaris/libmy.a", "MyStoreFunc", MyStoreFunc);
251bdad233fSMatthew Knepley .ve
252bdad233fSMatthew Knepley 
253bdad233fSMatthew Knepley   Then, your serialization can be chosen with the procedural interface via
254bdad233fSMatthew Knepley .vb
255bdad233fSMatthew Knepley     TSSetSerializeType(ts, "my_store")
256bdad233fSMatthew Knepley .ve
257bdad233fSMatthew Knepley   or at runtime via the option
258bdad233fSMatthew Knepley .vb
259bdad233fSMatthew Knepley     -ts_serialize_type my_store
260bdad233fSMatthew Knepley .ve
261bdad233fSMatthew Knepley 
262bdad233fSMatthew Knepley   Note: $PETSC_ARCH and $BOPT occuring in pathname will be replaced with appropriate values.
263bdad233fSMatthew Knepley 
264bdad233fSMatthew Knepley   Level: advanced
265bdad233fSMatthew Knepley 
266bdad233fSMatthew Knepley .keywords: ts, register
267bdad233fSMatthew Knepley .seealso: TSSerializeRegisterAll(), TSSerializeRegisterDestroy()
268bdad233fSMatthew Knepley M*/
269bdad233fSMatthew Knepley #undef __FUNCT__
270bdad233fSMatthew Knepley #define __FUNCT__ "TSSerializeRegister"
271bdad233fSMatthew Knepley int TSSerializeRegister(const char sname[], const char path[], const char name[],
272bdad233fSMatthew Knepley                           int (*function)(MPI_Comm, TS *, PetscViewer, PetscTruth))
273bdad233fSMatthew Knepley {
274bdad233fSMatthew Knepley   char fullname[256];
275bdad233fSMatthew Knepley   int  ierr;
276bdad233fSMatthew Knepley 
277bdad233fSMatthew Knepley   PetscFunctionBegin;
278bdad233fSMatthew Knepley   ierr = PetscStrcpy(fullname, path);                                                                     CHKERRQ(ierr);
279bdad233fSMatthew Knepley   ierr = PetscStrcat(fullname, ":");                                                                      CHKERRQ(ierr);
280bdad233fSMatthew Knepley   ierr = PetscStrcat(fullname, name);                                                                     CHKERRQ(ierr);
281bdad233fSMatthew Knepley   ierr = PetscFListAdd(&TSSerializeList, sname, fullname, (void (*)(void)) function);                     CHKERRQ(ierr);
282bdad233fSMatthew Knepley   PetscFunctionReturn(0);
283bdad233fSMatthew Knepley }
284bdad233fSMatthew Knepley 
285bdad233fSMatthew Knepley /*-------------------------------------------------------------------------------------------------------------------*/
286bdad233fSMatthew Knepley #undef __FUNCT__
287bdad233fSMatthew Knepley #define __FUNCT__ "TSRegisterDestroy"
288bdad233fSMatthew Knepley /*@C
2893cea93caSBarry Smith    TSRegisterDestroy - Frees the list of timestepping routines that were registered by TSRegister()/TSRegisterDynamic().
290bdad233fSMatthew Knepley 
291bdad233fSMatthew Knepley    Not Collective
292bdad233fSMatthew Knepley 
293bdad233fSMatthew Knepley    Level: advanced
294bdad233fSMatthew Knepley 
295bdad233fSMatthew Knepley .keywords: TS, timestepper, register, destroy
2963cea93caSBarry Smith .seealso: TSRegister(), TSRegisterAll(), TSSerializeRegisterDestroy(), TSRegisterDynamic()
297bdad233fSMatthew Knepley @*/
298bdad233fSMatthew Knepley int TSRegisterDestroy(void)
299bdad233fSMatthew Knepley {
300bdad233fSMatthew Knepley   int ierr;
301bdad233fSMatthew Knepley 
302bdad233fSMatthew Knepley   PetscFunctionBegin;
303bdad233fSMatthew Knepley   if (TSList != PETSC_NULL) {
304bdad233fSMatthew Knepley     ierr = PetscFListDestroy(&TSList);                                                                    CHKERRQ(ierr);
305bdad233fSMatthew Knepley     TSList = PETSC_NULL;
306bdad233fSMatthew Knepley   }
307bdad233fSMatthew Knepley   TSRegisterAllCalled = PETSC_FALSE;
308bdad233fSMatthew Knepley   PetscFunctionReturn(0);
309bdad233fSMatthew Knepley }
310bdad233fSMatthew Knepley 
311bdad233fSMatthew Knepley #undef __FUNCT__
312bdad233fSMatthew Knepley #define __FUNCT__ "TSSerializeRegisterDestroy"
313bdad233fSMatthew Knepley /*@C
314bdad233fSMatthew Knepley   TSSerializeRegisterDestroy - Frees the list of serialization routines for
315bdad233fSMatthew Knepley   timesteppers that were registered by FListAdd().
316bdad233fSMatthew Knepley 
317bdad233fSMatthew Knepley   Not collective
318bdad233fSMatthew Knepley 
319bdad233fSMatthew Knepley   Level: advanced
320bdad233fSMatthew Knepley 
321bdad233fSMatthew Knepley .keywords: ts, serialization, register, destroy
322bdad233fSMatthew Knepley .seealso: TSSerializeRegisterAll(), TSRegisterDestroy()
323bdad233fSMatthew Knepley @*/
324bdad233fSMatthew Knepley int TSSerializeRegisterDestroy()
325bdad233fSMatthew Knepley {
326bdad233fSMatthew Knepley   int ierr;
327bdad233fSMatthew Knepley 
328bdad233fSMatthew Knepley   PetscFunctionBegin;
329bdad233fSMatthew Knepley   if (TSSerializeList != PETSC_NULL) {
330bdad233fSMatthew Knepley     ierr = PetscFListDestroy(&TSSerializeList);                                                           CHKERRQ(ierr);
331bdad233fSMatthew Knepley     TSSerializeList = PETSC_NULL;
332bdad233fSMatthew Knepley   }
333bdad233fSMatthew Knepley   TSSerializeRegisterAllCalled = PETSC_FALSE;
334bdad233fSMatthew Knepley   PetscFunctionReturn(0);
335bdad233fSMatthew Knepley }
336