xref: /petsc/src/ts/adapt/impls/cfl/adaptcfl.c (revision 9371c9d470a9602b6d10a8bf50c9b2280a79e45a)
1 #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/
2 
3 static PetscErrorCode TSAdaptChoose_CFL(TSAdapt adapt, TS ts, PetscReal h, PetscInt *next_sc, PetscReal *next_h, PetscBool *accept, PetscReal *wlte, PetscReal *wltea, PetscReal *wlter) {
4   PetscReal        hcfl, cfltimestep, ccfl;
5   PetscInt         ncandidates;
6   const PetscReal *ccflarray;
7 
8   PetscFunctionBegin;
9   PetscCall(TSGetCFLTime(ts, &cfltimestep));
10   PetscCall(TSAdaptCandidatesGet(adapt, &ncandidates, NULL, NULL, &ccflarray, NULL));
11   ccfl = (ncandidates > 0) ? ccflarray[0] : 1.0;
12 
13   PetscCheck(adapt->always_accept, PetscObjectComm((PetscObject)adapt), PETSC_ERR_SUP, "Step rejection not implemented. The CFL implementation is incomplete/unusable");
14 
15   /* Determine whether the step is accepted of rejected */
16   *accept = PETSC_TRUE;
17   if (h > cfltimestep * ccfl) {
18     if (adapt->always_accept) {
19       PetscCall(PetscInfo(adapt, "Step length %g with scheme of CFL coefficient %g did not satisfy user-provided CFL constraint %g, proceeding anyway\n", (double)h, (double)ccfl, (double)cfltimestep));
20     } else {
21       PetscCall(PetscInfo(adapt, "Step length %g with scheme of CFL coefficient %g did not satisfy user-provided CFL constraint %g, step REJECTED\n", (double)h, (double)ccfl, (double)cfltimestep));
22       *accept = PETSC_FALSE;
23     }
24   }
25 
26   /* The optimal new step based purely on CFL constraint for this step. */
27   hcfl = adapt->safety * cfltimestep * ccfl;
28   if (hcfl < adapt->dt_min) {
29     PetscCall(PetscInfo(adapt, "Cannot satisfy CFL constraint %g (with %g safety) at minimum time step %g with method coefficient %g, proceding anyway\n", (double)cfltimestep, (double)adapt->safety, (double)adapt->dt_min, (double)ccfl));
30   }
31 
32   *next_sc = 0;
33   *next_h  = PetscClipInterval(hcfl, adapt->dt_min, adapt->dt_max);
34   *wlte    = -1; /* Weighted local truncation error was not evaluated */
35   *wltea   = -1; /* Weighted absolute local truncation error was not evaluated */
36   *wlter   = -1; /* Weighted relative local truncation error was not evaluated */
37   PetscFunctionReturn(0);
38 }
39 
40 /*MC
41    TSADAPTCFL - CFL adaptive controller for time stepping
42 
43    Level: intermediate
44 
45 .seealso: `TS`, `TSAdapt`, `TSGetAdapt()`
46 M*/
47 PETSC_EXTERN PetscErrorCode TSAdaptCreate_CFL(TSAdapt adapt) {
48   PetscFunctionBegin;
49   adapt->ops->choose = TSAdaptChoose_CFL;
50   PetscFunctionReturn(0);
51 }
52