xref: /petsc/src/ts/interface/tsreg.c (revision 2ffb926422a8b5c382798f6a505f1ae8e898069a)
1 #include <petsc/private/tsimpl.h>      /*I "petscts.h"  I*/
2 
3 PetscFunctionList TSList              = NULL;
4 PetscBool         TSRegisterAllCalled = PETSC_FALSE;
5 
6 /*@C
7   TSSetType - Sets the method to be used as the timestepping solver.
8 
9   Collective on TS
10 
11   Input Parameters:
12 + ts   - The TS context
13 - type - A known method
14 
15   Options Database Command:
16 . -ts_type <type> - Sets the method; use -help for a list of available methods (for instance, euler)
17 
18    Notes:
19    See "petsc/include/petscts.h" for available methods (for instance)
20 +  TSEULER - Euler
21 .  TSSUNDIALS - SUNDIALS interface
22 .  TSBEULER - Backward Euler
23 -  TSPSEUDO - Pseudo-timestepping
24 
25    Normally, it is best to use the TSSetFromOptions() command and
26    then set the TS type from the options database rather than by using
27    this routine.  Using the options database provides the user with
28    maximum flexibility in evaluating the many different solvers.
29    The TSSetType() routine is provided for those situations where it
30    is necessary to set the timestepping solver independently of the
31    command line or options database.  This might be the case, for example,
32    when the choice of solver changes during the execution of the
33    program, and the user's application is taking responsibility for
34    choosing the appropriate method.  In other words, this routine is
35    not for beginners.
36 
37    Level: intermediate
38 
39 .keywords: TS, set, type
40 
41 .seealso: TS, TSSolve(), TSCreate(), TSSetFromOptions(), TSDestroy(), TSType
42 
43 @*/
44 PetscErrorCode  TSSetType(TS ts,TSType type)
45 {
46   PetscErrorCode (*r)(TS);
47   PetscBool      match;
48   PetscErrorCode ierr;
49 
50   PetscFunctionBegin;
51   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
52   ierr = PetscObjectTypeCompare((PetscObject) ts, type, &match);CHKERRQ(ierr);
53   if (match) PetscFunctionReturn(0);
54 
55   ierr = PetscFunctionListFind(TSList,type,&r);CHKERRQ(ierr);
56   if (!r) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TS type: %s", type);
57   if (ts->ops->destroy) {
58     ierr = (*(ts)->ops->destroy)(ts);CHKERRQ(ierr);
59   }
60   ierr = PetscMemzero(ts->ops,sizeof(*ts->ops));CHKERRQ(ierr);
61   ts->default_adapt_type = NULL;
62 
63   ts->setupcalled = PETSC_FALSE;
64 
65   ierr = PetscObjectChangeTypeName((PetscObject)ts, type);CHKERRQ(ierr);
66   ierr = (*r)(ts);CHKERRQ(ierr);
67   PetscFunctionReturn(0);
68 }
69 
70 /*@C
71   TSGetType - Gets the TS method type (as a string).
72 
73   Not Collective
74 
75   Input Parameter:
76 . ts - The TS
77 
78   Output Parameter:
79 . type - The name of TS method
80 
81   Level: intermediate
82 
83 .keywords: TS, timestepper, get, type, name
84 .seealso TSSetType()
85 @*/
86 PetscErrorCode  TSGetType(TS ts, TSType *type)
87 {
88   PetscFunctionBegin;
89   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
90   PetscValidPointer(type,2);
91   *type = ((PetscObject)ts)->type_name;
92   PetscFunctionReturn(0);
93 }
94 
95 /*--------------------------------------------------------------------------------------------------------------------*/
96 
97 /*@C
98   TSRegister - Adds a creation method to the TS package.
99 
100   Not Collective
101 
102   Input Parameters:
103 + name        - The name of a new user-defined creation routine
104 - create_func - The creation routine itself
105 
106   Notes:
107   TSRegister() may be called multiple times to add several user-defined tses.
108 
109   Sample usage:
110 .vb
111   TSRegister("my_ts",  MyTSCreate);
112 .ve
113 
114   Then, your ts type can be chosen with the procedural interface via
115 .vb
116     TS ts;
117     TSCreate(MPI_Comm, &ts);
118     TSSetType(ts, "my_ts")
119 .ve
120   or at runtime via the option
121 .vb
122     -ts_type my_ts
123 .ve
124 
125   Level: advanced
126 
127 .keywords: TS, register
128 
129 .seealso: TSRegisterAll(), TSRegisterDestroy()
130 @*/
131 PetscErrorCode  TSRegister(const char sname[], PetscErrorCode (*function)(TS))
132 {
133   PetscErrorCode ierr;
134 
135   PetscFunctionBegin;
136   ierr = PetscFunctionListAdd(&TSList,sname,function);CHKERRQ(ierr);
137   PetscFunctionReturn(0);
138 }
139 
140