xref: /petsc/src/ts/interface/tsreg.c (revision b94d7ded0a05f1bbd5e48daa6f92b28259c75b44)
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 .seealso: `TS`, `TSSolve()`, `TSCreate()`, `TSSetFromOptions()`, `TSDestroy()`, `TSType`
40 
41 @*/
42 PetscErrorCode  TSSetType(TS ts,TSType type)
43 {
44   PetscErrorCode (*r)(TS);
45   PetscBool      match;
46 
47   PetscFunctionBegin;
48   PetscValidHeaderSpecific(ts, TS_CLASSID,1);
49   PetscValidCharPointer(type,2);
50   PetscCall(PetscObjectTypeCompare((PetscObject) ts, type, &match));
51   if (match) PetscFunctionReturn(0);
52 
53   PetscCall(PetscFunctionListFind(TSList,type,&r));
54   PetscCheck(r,PETSC_COMM_SELF,PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TS type: %s", type);
55   if (ts->ops->destroy) PetscCall((*(ts)->ops->destroy)(ts));
56   PetscCall(PetscMemzero(ts->ops,sizeof(*ts->ops)));
57   ts->usessnes           = PETSC_FALSE;
58   ts->default_adapt_type = TSADAPTNONE;
59 
60   ts->setupcalled = PETSC_FALSE;
61 
62   PetscCall(PetscObjectChangeTypeName((PetscObject)ts, type));
63   PetscCall((*r)(ts));
64   PetscFunctionReturn(0);
65 }
66 
67 /*@C
68   TSGetType - Gets the TS method type (as a string).
69 
70   Not Collective
71 
72   Input Parameter:
73 . ts - The TS
74 
75   Output Parameter:
76 . type - The name of TS method
77 
78   Level: intermediate
79 
80 .seealso `TSSetType()`
81 @*/
82 PetscErrorCode  TSGetType(TS ts, TSType *type)
83 {
84   PetscFunctionBegin;
85   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
86   PetscValidPointer(type,2);
87   *type = ((PetscObject)ts)->type_name;
88   PetscFunctionReturn(0);
89 }
90 
91 /*--------------------------------------------------------------------------------------------------------------------*/
92 
93 /*@C
94   TSRegister - Adds a creation method to the TS package.
95 
96   Not Collective
97 
98   Input Parameters:
99 + name        - The name of a new user-defined creation routine
100 - create_func - The creation routine itself
101 
102   Notes:
103   TSRegister() may be called multiple times to add several user-defined tses.
104 
105   Sample usage:
106 .vb
107   TSRegister("my_ts",  MyTSCreate);
108 .ve
109 
110   Then, your ts type can be chosen with the procedural interface via
111 .vb
112     TS ts;
113     TSCreate(MPI_Comm, &ts);
114     TSSetType(ts, "my_ts")
115 .ve
116   or at runtime via the option
117 .vb
118     -ts_type my_ts
119 .ve
120 
121   Level: advanced
122 
123 .seealso: `TSRegisterAll()`, `TSRegisterDestroy()`
124 @*/
125 PetscErrorCode  TSRegister(const char sname[], PetscErrorCode (*function)(TS))
126 {
127   PetscFunctionBegin;
128   PetscCall(TSInitializePackage());
129   PetscCall(PetscFunctionListAdd(&TSList,sname,function));
130   PetscFunctionReturn(0);
131 }
132