Actual source code: tsreg.c

  1: #include <petsc/private/tsimpl.h>

  3: PetscFunctionList TSList              = NULL;
  4: PetscBool         TSRegisterAllCalled = PETSC_FALSE;

  6: /*@C
  7:   TSSetType - Sets the method to be used as the timestepping solver.

  9:   Collective

 11:   Input Parameters:
 12: + ts   - The `TS` context
 13: - type - A known method

 15:   Options Database Key:
 16: . -ts_type <type> - Sets the method; use -help for a list of available methods (for instance, euler)

 18:   Level: intermediate

 20:   Notes:
 21:   See "petsc/include/petscts.h" for available methods (for instance)
 22: +  TSEULER - Euler
 23: .  TSSUNDIALS - SUNDIALS interface
 24: .  TSBEULER - Backward Euler
 25: -  TSPSEUDO - Pseudo-timestepping

 27:   Normally, it is best to use the `TSSetFromOptions()` command and
 28:   then set the `TS` type from the options database rather than by using
 29:   this routine.  Using the options database provides the user with
 30:   maximum flexibility in evaluating the many different solvers.
 31:   The TSSetType() routine is provided for those situations where it
 32:   is necessary to set the timestepping solver independently of the
 33:   command line or options database.  This might be the case, for example,
 34:   when the choice of solver changes during the execution of the
 35:   program, and the user's application is taking responsibility for
 36:   choosing the appropriate method.  In other words, this routine is
 37:   not for beginners.

 39: .seealso: [](ch_ts), `TS`, `TSSolve()`, `TSCreate()`, `TSSetFromOptions()`, `TSDestroy()`, `TSType`
 40: @*/
 41: PetscErrorCode TSSetType(TS ts, TSType type)
 42: {
 43:   PetscErrorCode (*r)(TS);
 44:   PetscBool match;

 46:   PetscFunctionBegin;
 48:   PetscAssertPointer(type, 2);
 49:   PetscCall(PetscObjectTypeCompare((PetscObject)ts, type, &match));
 50:   if (match) PetscFunctionReturn(PETSC_SUCCESS);

 52:   PetscCall(PetscFunctionListFind(TSList, type, &r));
 53:   PetscCheck(r, PetscObjectComm((PetscObject)ts), PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown TS type: %s", type);
 54:   PetscTryTypeMethod(ts, destroy);
 55:   PetscCall(PetscMemzero(ts->ops, sizeof(*ts->ops)));
 56:   ts->usessnes           = PETSC_FALSE;
 57:   ts->default_adapt_type = TSADAPTNONE;

 59:   ts->setupcalled = PETSC_FALSE;

 61:   PetscCall(PetscObjectChangeTypeName((PetscObject)ts, type));
 62:   PetscCall((*r)(ts));
 63:   PetscFunctionReturn(PETSC_SUCCESS);
 64: }

 66: /*@C
 67:   TSGetType - Gets the `TS` method type (as a string).

 69:   Not Collective

 71:   Input Parameter:
 72: . ts - The `TS`

 74:   Output Parameter:
 75: . type - The name of `TS` method

 77:   Level: intermediate

 79: .seealso: [](ch_ts), `TS`, `TSType`, `TSSetType()`
 80: @*/
 81: PetscErrorCode TSGetType(TS ts, TSType *type)
 82: {
 83:   PetscFunctionBegin;
 85:   PetscAssertPointer(type, 2);
 86:   *type = ((PetscObject)ts)->type_name;
 87:   PetscFunctionReturn(PETSC_SUCCESS);
 88: }

 90: /*--------------------------------------------------------------------------------------------------------------------*/

 92: /*@C
 93:   TSRegister - Adds a creation method to the `TS` package.

 95:   Not Collective

 97:   Input Parameters:
 98: + sname    - The name of a new user-defined creation routine
 99: - function - The creation routine itself

101:   Level: advanced

103:   Notes:
104:   `TSRegister()` may be called multiple times to add several user-defined tses.

106:   Example Usage:
107: .vb
108:   TSRegister("my_ts",  MyTSCreate);
109: .ve

111:   Then, your ts type can be chosen with the procedural interface via
112: .vb
113:     TS ts;
114:     TSCreate(MPI_Comm, &ts);
115:     TSSetType(ts, "my_ts")
116: .ve
117:   or at runtime via the option
118: .vb
119:     -ts_type my_ts
120: .ve

122: .seealso: [](ch_ts), `TSSetType()`, `TSType`, `TSRegisterAll()`, `TSRegisterDestroy()`
123: @*/
124: PetscErrorCode TSRegister(const char sname[], PetscErrorCode (*function)(TS))
125: {
126:   PetscFunctionBegin;
127:   PetscCall(TSInitializePackage());
128:   PetscCall(PetscFunctionListAdd(&TSList, sname, function));
129:   PetscFunctionReturn(PETSC_SUCCESS);
130: }