1ae2316d0SEmil Constantinescu #include <petsc/private/tsimpl.h> /*I "petscts.h" I*/ 2ae2316d0SEmil Constantinescu 3ae2316d0SEmil Constantinescu typedef struct { 4ae2316d0SEmil Constantinescu PetscBool always_accept; 5ae2316d0SEmil Constantinescu PetscReal clip[2]; /* admissible decrease/increase factors */ 6ae2316d0SEmil Constantinescu PetscReal safety; /* safety factor relative to target error */ 7ae2316d0SEmil Constantinescu PetscReal reject_safety; /* extra safety factor if the last step was rejected */ 8657c1e31SEmil Constantinescu Vec X; 9ae2316d0SEmil Constantinescu Vec Y; 10726095e4SEmil Constantinescu } TSAdapt_GLEE; 11ae2316d0SEmil Constantinescu 12ae2316d0SEmil Constantinescu #undef __FUNCT__ 13726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptChoose_GLEE" 14726095e4SEmil Constantinescu static PetscErrorCode TSAdaptChoose_GLEE(TSAdapt adapt,TS ts,PetscReal h,PetscInt *next_sc,PetscReal *next_h,PetscBool *accept,PetscReal *wlte) 15ae2316d0SEmil Constantinescu { 16657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 170a01e1b2SEmil Constantinescu TSType time_scheme; /* Type of time-integration scheme */ 18ae2316d0SEmil Constantinescu PetscErrorCode ierr; 19657c1e31SEmil Constantinescu Vec X,Y,Z; 207453f775SEmil Constantinescu PetscReal enorm,enorma,enormr,hfac_lte,h_lte,safety; 21*b1316ef9SEmil Constantinescu PetscInt order,order_add,stepno; 22ae2316d0SEmil Constantinescu 23ae2316d0SEmil Constantinescu PetscFunctionBegin; 24ae2316d0SEmil Constantinescu ierr = TSGetTimeStepNumber(ts,&stepno);CHKERRQ(ierr); 250a01e1b2SEmil Constantinescu 26657c1e31SEmil Constantinescu safety = glee->safety; 270a01e1b2SEmil Constantinescu ierr = TSGetType(ts,&time_scheme);CHKERRQ(ierr); 280a01e1b2SEmil Constantinescu if (!strcmp(time_scheme,TSGLEE)){ 290a01e1b2SEmil Constantinescu /* the method is of GLEE type */ 30*b1316ef9SEmil Constantinescu order_add=1; /* typically same order estimates */ 31657c1e31SEmil Constantinescu if (!glee->X) { 32657c1e31SEmil Constantinescu ierr = TSGetSolution(ts,&Z);CHKERRQ(ierr); 33657c1e31SEmil Constantinescu ierr = VecDuplicate(Z,&glee->X);CHKERRQ(ierr); 34657c1e31SEmil Constantinescu } 35657c1e31SEmil Constantinescu X = glee->X; 36657c1e31SEmil Constantinescu if (!glee->Y) {ierr = VecDuplicate(X,&glee->Y);CHKERRQ(ierr);} 37657c1e31SEmil Constantinescu Y = glee->Y; 380a01e1b2SEmil Constantinescu ierr = TSGetTimeError(ts,-1,&X);CHKERRQ(ierr); 390a01e1b2SEmil Constantinescu ierr = TSGetTimeError(ts, 0,&Y);CHKERRQ(ierr); 400a01e1b2SEmil Constantinescu ierr = TSErrorWeightedNorm(ts,X,Y,adapt->wnormtype,&enorm,&enorma,&enormr);CHKERRQ(ierr); 410a01e1b2SEmil Constantinescu } else { 420a01e1b2SEmil Constantinescu /* the method is NOT of GLEE type */ 43*b1316ef9SEmil Constantinescu order_add=0; /* typically lower order estimates */ 44ae2316d0SEmil Constantinescu ierr = TSGetSolution(ts,&X);CHKERRQ(ierr); 45657c1e31SEmil Constantinescu if (!glee->Y) {ierr = VecDuplicate(X,&glee->Y);CHKERRQ(ierr);} 46657c1e31SEmil Constantinescu Y = glee->Y; 47ae2316d0SEmil Constantinescu order = adapt->candidates.order[0]; 48ae2316d0SEmil Constantinescu ierr = TSEvaluateStep(ts,order-1,Y,NULL);CHKERRQ(ierr); 497453f775SEmil Constantinescu ierr = TSErrorWeightedNorm(ts,X,Y,adapt->wnormtype,&enorm,&enorma,&enormr);CHKERRQ(ierr); 500a01e1b2SEmil Constantinescu } 51ae2316d0SEmil Constantinescu if (enorm > 1.) { 52657c1e31SEmil Constantinescu if (!*accept) safety *= glee->reject_safety; /* The last attempt also failed, shorten more aggressively */ 53ae2316d0SEmil Constantinescu if (h < (1 + PETSC_SQRT_MACHINE_EPSILON)*adapt->dt_min) { 54ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, accepting because step size %g is at minimum\n",(double)enorm,(double)h);CHKERRQ(ierr); 55ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 56657c1e31SEmil Constantinescu } else if (glee->always_accept) { 57ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, accepting step of size %g because always_accept is set\n",(double)enorm,(double)h);CHKERRQ(ierr); 58ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 59ae2316d0SEmil Constantinescu } else { 60ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, rejecting step of size %g\n",(double)enorm,(double)h);CHKERRQ(ierr); 61ae2316d0SEmil Constantinescu *accept = PETSC_FALSE; 62ae2316d0SEmil Constantinescu } 63ae2316d0SEmil Constantinescu } else { 64ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, accepting step of size %g\n",(double)enorm,(double)h);CHKERRQ(ierr); 65ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 66ae2316d0SEmil Constantinescu } 67ae2316d0SEmil Constantinescu 68ae2316d0SEmil Constantinescu /* The optimal new step based purely on local truncation error for this step. */ 69ae2316d0SEmil Constantinescu if (enorm == 0.0) { 70ae2316d0SEmil Constantinescu hfac_lte = safety * PETSC_INFINITY; 71ae2316d0SEmil Constantinescu } else { 72*b1316ef9SEmil Constantinescu hfac_lte = safety * PetscPowReal(enorm,-1./(order+order_add)); 73ae2316d0SEmil Constantinescu } 74657c1e31SEmil Constantinescu h_lte = h * PetscClipInterval(hfac_lte,glee->clip[0],glee->clip[1]); 75ae2316d0SEmil Constantinescu 76ae2316d0SEmil Constantinescu *next_sc = 0; 77ae2316d0SEmil Constantinescu *next_h = PetscClipInterval(h_lte,adapt->dt_min,adapt->dt_max); 78ae2316d0SEmil Constantinescu *wlte = enorm; 79ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 80ae2316d0SEmil Constantinescu } 81ae2316d0SEmil Constantinescu 82ae2316d0SEmil Constantinescu #undef __FUNCT__ 83726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptReset_GLEE" 84726095e4SEmil Constantinescu static PetscErrorCode TSAdaptReset_GLEE(TSAdapt adapt) 85ae2316d0SEmil Constantinescu { 86657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 87ae2316d0SEmil Constantinescu PetscErrorCode ierr; 88ae2316d0SEmil Constantinescu 89ae2316d0SEmil Constantinescu PetscFunctionBegin; 90657c1e31SEmil Constantinescu ierr = VecDestroy(&glee->Y);CHKERRQ(ierr); 91ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 92ae2316d0SEmil Constantinescu } 93ae2316d0SEmil Constantinescu 94ae2316d0SEmil Constantinescu #undef __FUNCT__ 95726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptDestroy_GLEE" 96726095e4SEmil Constantinescu static PetscErrorCode TSAdaptDestroy_GLEE(TSAdapt adapt) 97ae2316d0SEmil Constantinescu { 98ae2316d0SEmil Constantinescu PetscErrorCode ierr; 99ae2316d0SEmil Constantinescu 100ae2316d0SEmil Constantinescu PetscFunctionBegin; 101726095e4SEmil Constantinescu ierr = TSAdaptReset_GLEE(adapt);CHKERRQ(ierr); 102ae2316d0SEmil Constantinescu ierr = PetscFree(adapt->data);CHKERRQ(ierr); 103ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 104ae2316d0SEmil Constantinescu } 105ae2316d0SEmil Constantinescu 106ae2316d0SEmil Constantinescu #undef __FUNCT__ 107726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptSetFromOptions_GLEE" 108726095e4SEmil Constantinescu static PetscErrorCode TSAdaptSetFromOptions_GLEE(PetscOptionItems *PetscOptionsObject,TSAdapt adapt) 109ae2316d0SEmil Constantinescu { 110657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 111ae2316d0SEmil Constantinescu PetscErrorCode ierr; 112ae2316d0SEmil Constantinescu PetscInt two; 113ae2316d0SEmil Constantinescu PetscBool set; 114ae2316d0SEmil Constantinescu 115ae2316d0SEmil Constantinescu PetscFunctionBegin; 116726095e4SEmil Constantinescu ierr = PetscOptionsHead(PetscOptionsObject,"GLEE adaptive controller options");CHKERRQ(ierr); 117ae2316d0SEmil Constantinescu two = 2; 118657c1e31SEmil Constantinescu ierr = PetscOptionsRealArray("-ts_adapt_glee_clip","Admissible decrease/increase in step size","",glee->clip,&two,&set);CHKERRQ(ierr); 119657c1e31SEmil Constantinescu if (set && (two != 2 || glee->clip[0] > glee->clip[1])) SETERRQ(PetscObjectComm((PetscObject)adapt),PETSC_ERR_ARG_OUTOFRANGE,"Must give exactly two values to -ts_adapt_glee_clip"); 120657c1e31SEmil Constantinescu ierr = PetscOptionsReal("-ts_adapt_glee_safety","Safety factor relative to target error","",glee->safety,&glee->safety,NULL);CHKERRQ(ierr); 121657c1e31SEmil Constantinescu ierr = PetscOptionsReal("-ts_adapt_glee_reject_safety","Extra safety factor to apply if the last step was rejected","",glee->reject_safety,&glee->reject_safety,NULL);CHKERRQ(ierr); 122657c1e31SEmil Constantinescu ierr = PetscOptionsBool("-ts_adapt_glee_always_accept","Always accept the step regardless of whether local truncation error meets goal","",glee->always_accept,&glee->always_accept,NULL);CHKERRQ(ierr); 123ae2316d0SEmil Constantinescu ierr = PetscOptionsTail();CHKERRQ(ierr); 124ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 125ae2316d0SEmil Constantinescu } 126ae2316d0SEmil Constantinescu 127ae2316d0SEmil Constantinescu #undef __FUNCT__ 128726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptView_GLEE" 129726095e4SEmil Constantinescu static PetscErrorCode TSAdaptView_GLEE(TSAdapt adapt,PetscViewer viewer) 130ae2316d0SEmil Constantinescu { 131657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 132ae2316d0SEmil Constantinescu PetscErrorCode ierr; 133ae2316d0SEmil Constantinescu PetscBool iascii; 134ae2316d0SEmil Constantinescu 135ae2316d0SEmil Constantinescu PetscFunctionBegin; 136ae2316d0SEmil Constantinescu ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 137ae2316d0SEmil Constantinescu if (iascii) { 138657c1e31SEmil Constantinescu if (glee->always_accept) {ierr = PetscViewerASCIIPrintf(viewer," GLEE: always accepting steps\n");CHKERRQ(ierr);} 139657c1e31SEmil Constantinescu ierr = PetscViewerASCIIPrintf(viewer," GLEE: clip fastest decrease %g, fastest increase %g\n",(double)glee->clip[0],(double)glee->clip[1]);CHKERRQ(ierr); 140657c1e31SEmil Constantinescu ierr = PetscViewerASCIIPrintf(viewer," GLEE: safety factor %g, extra factor after step rejection %g\n",(double)glee->safety,(double)glee->reject_safety);CHKERRQ(ierr); 141ae2316d0SEmil Constantinescu } 142ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 143ae2316d0SEmil Constantinescu } 144ae2316d0SEmil Constantinescu 145ae2316d0SEmil Constantinescu #undef __FUNCT__ 146726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptCreate_GLEE" 147ae2316d0SEmil Constantinescu /*MC 148657c1e31SEmil Constantinescu TSADAPTGLEE - GLEE adaptive controller for time stepping 149ae2316d0SEmil Constantinescu 150ae2316d0SEmil Constantinescu Level: intermediate 151ae2316d0SEmil Constantinescu 152ae2316d0SEmil Constantinescu .seealso: TS, TSAdapt, TSSetAdapt() 153ae2316d0SEmil Constantinescu M*/ 154726095e4SEmil Constantinescu PETSC_EXTERN PetscErrorCode TSAdaptCreate_GLEE(TSAdapt adapt) 155ae2316d0SEmil Constantinescu { 156ae2316d0SEmil Constantinescu PetscErrorCode ierr; 157726095e4SEmil Constantinescu TSAdapt_GLEE *a; 158ae2316d0SEmil Constantinescu 159ae2316d0SEmil Constantinescu PetscFunctionBegin; 160ae2316d0SEmil Constantinescu ierr = PetscNewLog(adapt,&a);CHKERRQ(ierr); 161ae2316d0SEmil Constantinescu adapt->data = (void*)a; 162726095e4SEmil Constantinescu adapt->ops->choose = TSAdaptChoose_GLEE; 163726095e4SEmil Constantinescu adapt->ops->setfromoptions = TSAdaptSetFromOptions_GLEE; 164726095e4SEmil Constantinescu adapt->ops->destroy = TSAdaptDestroy_GLEE; 165726095e4SEmil Constantinescu adapt->ops->view = TSAdaptView_GLEE; 166ae2316d0SEmil Constantinescu 167ae2316d0SEmil Constantinescu a->clip[0] = 0.1; 168ae2316d0SEmil Constantinescu a->clip[1] = 10.; 169ae2316d0SEmil Constantinescu a->safety = 0.9; 170ae2316d0SEmil Constantinescu a->reject_safety = 0.5; 171ae2316d0SEmil Constantinescu a->always_accept = PETSC_FALSE; 172ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 173ae2316d0SEmil Constantinescu } 174