xref: /petsc/src/ts/interface/tsreg.c (revision 1cc06b555e92f8ec64db10330b8bbd830e5bc876)
1af0996ceSBarry Smith #include <petsc/private/tsimpl.h> /*I "petscts.h"  I*/
23f3760d9SBarry Smith 
30298fd71SBarry Smith PetscFunctionList TSList              = NULL;
4ace3abfcSBarry Smith PetscBool         TSRegisterAllCalled = PETSC_FALSE;
53f3760d9SBarry Smith 
682bf6240SBarry Smith /*@C
78f6c3df8SBarry Smith   TSSetType - Sets the method to be used as the timestepping solver.
83f3760d9SBarry Smith 
9c3339decSBarry Smith   Collective
10fee21e36SBarry Smith 
11bef22f13SLois Curfman McInnes   Input Parameters:
12bcf0153eSBarry Smith + ts   - The `TS` context
13bdad233fSMatthew Knepley - type - A known method
14bef22f13SLois Curfman McInnes 
15bcf0153eSBarry Smith   Options Database Key:
16bdad233fSMatthew Knepley . -ts_type <type> - Sets the method; use -help for a list of available methods (for instance, euler)
17ae12b187SLois Curfman McInnes 
18bcf0153eSBarry Smith    Level: intermediate
19bcf0153eSBarry Smith 
203f3760d9SBarry Smith    Notes:
21e090d566SSatish Balay    See "petsc/include/petscts.h" for available methods (for instance)
229596e0b4SJed Brown +  TSEULER - Euler
239596e0b4SJed Brown .  TSSUNDIALS - SUNDIALS interface
249596e0b4SJed Brown .  TSBEULER - Backward Euler
259596e0b4SJed Brown -  TSPSEUDO - Pseudo-timestepping
263f3760d9SBarry Smith 
27bcf0153eSBarry Smith    Normally, it is best to use the `TSSetFromOptions()` command and
28bcf0153eSBarry Smith    then set the `TS` type from the options database rather than by using
29ae12b187SLois Curfman McInnes    this routine.  Using the options database provides the user with
30ae12b187SLois Curfman McInnes    maximum flexibility in evaluating the many different solvers.
31ae12b187SLois Curfman McInnes    The TSSetType() routine is provided for those situations where it
32ae12b187SLois Curfman McInnes    is necessary to set the timestepping solver independently of the
33ae12b187SLois Curfman McInnes    command line or options database.  This might be the case, for example,
34ae12b187SLois Curfman McInnes    when the choice of solver changes during the execution of the
35ae12b187SLois Curfman McInnes    program, and the user's application is taking responsibility for
36ae12b187SLois Curfman McInnes    choosing the appropriate method.  In other words, this routine is
37d5d37b61SLois Curfman McInnes    not for beginners.
38d5d37b61SLois Curfman McInnes 
39*1cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSCreate()`, `TSSetFromOptions()`, `TSDestroy()`, `TSType`
403f3760d9SBarry Smith @*/
41d71ae5a4SJacob Faibussowitsch PetscErrorCode TSSetType(TS ts, TSType type)
42d71ae5a4SJacob Faibussowitsch {
436849ba73SBarry Smith   PetscErrorCode (*r)(TS);
44ace3abfcSBarry Smith   PetscBool match;
45df8cb225SBarry Smith 
463a40ed3dSBarry Smith   PetscFunctionBegin;
470700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
48b92453a8SLisandro Dalcin   PetscValidCharPointer(type, 2);
499566063dSJacob Faibussowitsch   PetscCall(PetscObjectTypeCompare((PetscObject)ts, type, &match));
503ba16761SJacob Faibussowitsch   if (match) PetscFunctionReturn(PETSC_SUCCESS);
51bdad233fSMatthew Knepley 
529566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListFind(TSList, type, &r));
533c633725SBarry Smith   PetscCheck(r, PETSC_COMM_SELF, PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TS type: %s", type);
54dbbe0bcdSBarry Smith   PetscTryTypeMethod(ts, destroy);
559566063dSJacob Faibussowitsch   PetscCall(PetscMemzero(ts->ops, sizeof(*ts->ops)));
56825ab935SBarry Smith   ts->usessnes           = PETSC_FALSE;
57b92453a8SLisandro Dalcin   ts->default_adapt_type = TSADAPTNONE;
58bbd56ea5SKarl Rupp 
59277b19d0SLisandro Dalcin   ts->setupcalled = PETSC_FALSE;
60bbd56ea5SKarl Rupp 
619566063dSJacob Faibussowitsch   PetscCall(PetscObjectChangeTypeName((PetscObject)ts, type));
629566063dSJacob Faibussowitsch   PetscCall((*r)(ts));
633ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
643f3760d9SBarry Smith }
653f3760d9SBarry Smith 
663f3760d9SBarry Smith /*@C
67bcf0153eSBarry Smith   TSGetType - Gets the `TS` method type (as a string).
683f3760d9SBarry Smith 
69bef22f13SLois Curfman McInnes   Not Collective
70bef22f13SLois Curfman McInnes 
713f3760d9SBarry Smith   Input Parameter:
72bcf0153eSBarry Smith . ts - The `TS`
733f3760d9SBarry Smith 
743f3760d9SBarry Smith   Output Parameter:
75bcf0153eSBarry Smith . type - The name of `TS` method
763f3760d9SBarry Smith 
77d5d37b61SLois Curfman McInnes   Level: intermediate
78d5d37b61SLois Curfman McInnes 
79*1cc06b55SBarry Smith .seealso: [](ch_ts), `TS`, `TSType`, `TSSetType()`
803f3760d9SBarry Smith @*/
81d71ae5a4SJacob Faibussowitsch PetscErrorCode TSGetType(TS ts, TSType *type)
82d71ae5a4SJacob Faibussowitsch {
833a40ed3dSBarry Smith   PetscFunctionBegin;
840700a824SBarry Smith   PetscValidHeaderSpecific(ts, TS_CLASSID, 1);
854482741eSBarry Smith   PetscValidPointer(type, 2);
867adad957SLisandro Dalcin   *type = ((PetscObject)ts)->type_name;
873ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
883f3760d9SBarry Smith }
893f3760d9SBarry Smith 
90bdad233fSMatthew Knepley /*--------------------------------------------------------------------------------------------------------------------*/
913cea93caSBarry Smith 
923cea93caSBarry Smith /*@C
93bcf0153eSBarry Smith   TSRegister - Adds a creation method to the `TS` package.
941c84c290SBarry Smith 
951c84c290SBarry Smith   Not Collective
961c84c290SBarry Smith 
971c84c290SBarry Smith   Input Parameters:
982fe279fdSBarry Smith + sname        - The name of a new user-defined creation routine
992fe279fdSBarry Smith - function - The creation routine itself
1001c84c290SBarry Smith 
101bcf0153eSBarry Smith   Level: advanced
102bcf0153eSBarry Smith 
1031c84c290SBarry Smith   Notes:
104bcf0153eSBarry Smith   `TSRegister()` may be called multiple times to add several user-defined tses.
1051c84c290SBarry Smith 
1061c84c290SBarry Smith   Sample usage:
1071c84c290SBarry Smith .vb
108bdf89e91SBarry Smith   TSRegister("my_ts",  MyTSCreate);
1091c84c290SBarry Smith .ve
1101c84c290SBarry Smith 
1111c84c290SBarry Smith   Then, your ts type can be chosen with the procedural interface via
1121c84c290SBarry Smith .vb
1131c84c290SBarry Smith     TS ts;
1141c84c290SBarry Smith     TSCreate(MPI_Comm, &ts);
1151c84c290SBarry Smith     TSSetType(ts, "my_ts")
1161c84c290SBarry Smith .ve
1171c84c290SBarry Smith   or at runtime via the option
1181c84c290SBarry Smith .vb
1191c84c290SBarry Smith     -ts_type my_ts
1201c84c290SBarry Smith .ve
1213cea93caSBarry Smith 
122*1cc06b55SBarry Smith .seealso: [](ch_ts), `TSSetType()`, `TSType`, `TSRegisterAll()`, `TSRegisterDestroy()`
1233cea93caSBarry Smith @*/
124d71ae5a4SJacob Faibussowitsch PetscErrorCode TSRegister(const char sname[], PetscErrorCode (*function)(TS))
125d71ae5a4SJacob Faibussowitsch {
126bdad233fSMatthew Knepley   PetscFunctionBegin;
1279566063dSJacob Faibussowitsch   PetscCall(TSInitializePackage());
1289566063dSJacob Faibussowitsch   PetscCall(PetscFunctionListAdd(&TSList, sname, function));
1293ba16761SJacob Faibussowitsch   PetscFunctionReturn(PETSC_SUCCESS);
130bdad233fSMatthew Knepley }
131