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) 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 PetscFunctionReturn(0); 44 } 45 46 #undef __FUNCT__ 47 #define __FUNCT__ "TSAdaptDestroy_CFL" 48 static PetscErrorCode TSAdaptDestroy_CFL(TSAdapt adapt) 49 { 50 PetscErrorCode ierr; 51 52 PetscFunctionBegin; 53 ierr = PetscFree(adapt->data);CHKERRQ(ierr); 54 PetscFunctionReturn(0); 55 } 56 57 #undef __FUNCT__ 58 #define __FUNCT__ "TSAdaptSetFromOptions_CFL" 59 static PetscErrorCode TSAdaptSetFromOptions_CFL(PetscOptionItems *PetscOptionsObject,TSAdapt adapt) 60 { 61 TSAdapt_CFL *cfl = (TSAdapt_CFL*)adapt->data; 62 PetscErrorCode ierr; 63 64 PetscFunctionBegin; 65 ierr = PetscOptionsHead(PetscOptionsObject,"CFL adaptive controller options");CHKERRQ(ierr); 66 ierr = PetscOptionsReal("-ts_adapt_cfl_safety","Safety factor relative to target CFL constraint","",cfl->safety,&cfl->safety,NULL);CHKERRQ(ierr); 67 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); 68 /* The TS implementations do not currently communicate CFL information to the controller. There is a placeholder, but 69 * we do not believe it to provide sufficiently rich information. That means the CFL adaptor is incomplete and 70 * unusable. Do not delete the guard below unless you have finished the implementation. */ 71 if (!cfl->always_accept) SETERRQ(PetscObjectComm((PetscObject)adapt),PETSC_ERR_SUP,"Step rejection not implemented. The CFL implementation is incomplete/unusable"); 72 ierr = PetscOptionsTail();CHKERRQ(ierr); 73 PetscFunctionReturn(0); 74 } 75 76 #undef __FUNCT__ 77 #define __FUNCT__ "TSAdaptCreate_CFL" 78 /*MC 79 TSADAPTCFL - CFL adaptive controller for time stepping 80 81 Level: intermediate 82 83 .seealso: TS, TSAdapt, TSSetAdapt() 84 M*/ 85 PETSC_EXTERN PetscErrorCode TSAdaptCreate_CFL(TSAdapt adapt) 86 { 87 PetscErrorCode ierr; 88 TSAdapt_CFL *a; 89 90 PetscFunctionBegin; 91 ierr = PetscNewLog(adapt,&a);CHKERRQ(ierr); 92 adapt->data = (void*)a; 93 94 adapt->ops->choose = TSAdaptChoose_CFL; 95 adapt->ops->setfromoptions = TSAdaptSetFromOptions_CFL; 96 adapt->ops->destroy = TSAdaptDestroy_CFL; 97 98 a->safety = 0.9; 99 a->always_accept = PETSC_FALSE; 100 PetscFunctionReturn(0); 101 } 102