1 #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2 3 typedef struct { 4 PetscReal safety; /* safety factor relative to target CFL constraint */ 5 PetscBool always_accept; 6 } TSAdapt_CFL; 7 8 #undef __FUNCT__ 9 #define __FUNCT__ "TSAdaptChoose_CFL" 10 static PetscErrorCode TSAdaptChoose_CFL(TSAdapt adapt,TS ts,PetscReal h,PetscInt *next_sc,PetscReal *next_h,PetscBool *accept,PetscReal *wlte,PetscReal *wltea,PetscReal *wlter) 11 { 12 TSAdapt_CFL *cfl = (TSAdapt_CFL*)adapt->data; 13 PetscErrorCode ierr; 14 PetscReal hcfl,cfltimestep,ccfl; 15 PetscInt ncandidates; 16 const PetscReal *ccflarray; 17 18 PetscFunctionBegin; 19 ierr = TSGetCFLTime(ts,&cfltimestep);CHKERRQ(ierr); 20 ierr = TSAdaptCandidatesGet(adapt,&ncandidates,NULL,NULL,&ccflarray,NULL);CHKERRQ(ierr); 21 ccfl = (ncandidates > 0) ? ccflarray[0] : 1.0; 22 23 /* Determine whether the step is accepted of rejected */ 24 *accept = PETSC_TRUE; 25 if (h > cfltimestep * ccfl) { 26 if (cfl->always_accept) { 27 ierr = PetscInfo3(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);CHKERRQ(ierr); 28 } else { 29 ierr = PetscInfo3(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);CHKERRQ(ierr); 30 *accept = PETSC_FALSE; 31 } 32 } 33 34 /* The optimal new step based purely on CFL constraint for this step. */ 35 hcfl = cfl->safety * cfltimestep * ccfl; 36 if (hcfl < adapt->dt_min) { 37 ierr = PetscInfo4(adapt,"Cannot satisfy CFL constraint %g (with %g safety) at minimum time step %g with method coefficient %g, proceding anyway\n",(double)cfltimestep,(double)cfl->safety,(double)adapt->dt_min,(double)ccfl);CHKERRQ(ierr); 38 } 39 40 *next_sc = 0; 41 *next_h = PetscClipInterval(hcfl,adapt->dt_min,adapt->dt_max); 42 *wlte = -1; /* Weighted local truncation error was not evaluated */ 43 *wltea = -1; /* Weighted absolute local truncation error was not evaluated */ 44 *wlter = -1; /* Weighted relative local truncation error was not evaluated */ 45 PetscFunctionReturn(0); 46 } 47 48 #undef __FUNCT__ 49 #define __FUNCT__ "TSAdaptDestroy_CFL" 50 static PetscErrorCode TSAdaptDestroy_CFL(TSAdapt adapt) 51 { 52 PetscErrorCode ierr; 53 54 PetscFunctionBegin; 55 ierr = PetscFree(adapt->data);CHKERRQ(ierr); 56 PetscFunctionReturn(0); 57 } 58 59 #undef __FUNCT__ 60 #define __FUNCT__ "TSAdaptSetFromOptions_CFL" 61 static PetscErrorCode TSAdaptSetFromOptions_CFL(PetscOptionItems *PetscOptionsObject,TSAdapt adapt) 62 { 63 TSAdapt_CFL *cfl = (TSAdapt_CFL*)adapt->data; 64 PetscErrorCode ierr; 65 66 PetscFunctionBegin; 67 ierr = PetscOptionsHead(PetscOptionsObject,"CFL adaptive controller options");CHKERRQ(ierr); 68 ierr = PetscOptionsReal("-ts_adapt_cfl_safety","Safety factor relative to target CFL constraint","",cfl->safety,&cfl->safety,NULL);CHKERRQ(ierr); 69 ierr = PetscOptionsBool("-ts_adapt_cfl_always_accept","Always accept the step regardless of whether CFL constraint meets goal","",cfl->always_accept,&cfl->always_accept,NULL);CHKERRQ(ierr); 70 /* The TS implementations do not currently communicate CFL information to the controller. There is a placeholder, but 71 * we do not believe it to provide sufficiently rich information. That means the CFL adaptor is incomplete and 72 * unusable. Do not delete the guard below unless you have finished the implementation. */ 73 if (!cfl->always_accept) SETERRQ(PetscObjectComm((PetscObject)adapt),PETSC_ERR_SUP,"Step rejection not implemented. The CFL implementation is incomplete/unusable"); 74 ierr = PetscOptionsTail();CHKERRQ(ierr); 75 PetscFunctionReturn(0); 76 } 77 78 #undef __FUNCT__ 79 #define __FUNCT__ "TSAdaptCreate_CFL" 80 /*MC 81 TSADAPTCFL - CFL adaptive controller for time stepping 82 83 Level: intermediate 84 85 .seealso: TS, TSAdapt, TSSetAdapt() 86 M*/ 87 PETSC_EXTERN PetscErrorCode TSAdaptCreate_CFL(TSAdapt adapt) 88 { 89 PetscErrorCode ierr; 90 TSAdapt_CFL *a; 91 92 PetscFunctionBegin; 93 ierr = PetscNewLog(adapt,&a);CHKERRQ(ierr); 94 adapt->data = (void*)a; 95 96 adapt->ops->choose = TSAdaptChoose_CFL; 97 adapt->ops->setfromoptions = TSAdaptSetFromOptions_CFL; 98 adapt->ops->destroy = TSAdaptDestroy_CFL; 99 100 a->safety = 0.9; 101 a->always_accept = PETSC_FALSE; 102 PetscFunctionReturn(0); 103 } 104