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; 10*8a175baeSEmil Constantinescu Vec E; 11726095e4SEmil Constantinescu } TSAdapt_GLEE; 12ae2316d0SEmil Constantinescu 13ae2316d0SEmil Constantinescu #undef __FUNCT__ 14726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptChoose_GLEE" 15726095e4SEmil Constantinescu static PetscErrorCode TSAdaptChoose_GLEE(TSAdapt adapt,TS ts,PetscReal h,PetscInt *next_sc,PetscReal *next_h,PetscBool *accept,PetscReal *wlte) 16ae2316d0SEmil Constantinescu { 17657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 180a01e1b2SEmil Constantinescu TSType time_scheme; /* Type of time-integration scheme */ 19ae2316d0SEmil Constantinescu PetscErrorCode ierr; 20*8a175baeSEmil Constantinescu Vec X,Y,E; 217453f775SEmil Constantinescu PetscReal enorm,enorma,enormr,hfac_lte,h_lte,safety; 22b1316ef9SEmil Constantinescu PetscInt order,order_add,stepno; 23ae2316d0SEmil Constantinescu 24ae2316d0SEmil Constantinescu PetscFunctionBegin; 25ae2316d0SEmil Constantinescu ierr = TSGetTimeStepNumber(ts,&stepno);CHKERRQ(ierr); 260a01e1b2SEmil Constantinescu 27657c1e31SEmil Constantinescu safety = glee->safety; 280a01e1b2SEmil Constantinescu ierr = TSGetType(ts,&time_scheme);CHKERRQ(ierr); 290a01e1b2SEmil Constantinescu if (!strcmp(time_scheme,TSGLEE)){ 300a01e1b2SEmil Constantinescu /* the method is of GLEE type */ 31*8a175baeSEmil Constantinescu order_add=0; /* typically same order estimates */ 32*8a175baeSEmil Constantinescu ierr = TSGetSolution(ts,&X);CHKERRQ(ierr); 33*8a175baeSEmil Constantinescu /* ierr = TSGetPreviousSolution(ts,&Y);CHKERRQ(ierr); 34*8a175baeSEmil Constantinescu */ 35*8a175baeSEmil Constantinescu if (!glee->E) {ierr = VecDuplicate(X,&glee->E);CHKERRQ(ierr);} 36*8a175baeSEmil Constantinescu E = glee->E; 37*8a175baeSEmil Constantinescu ierr = TSGetTimeError(ts,0,&E);CHKERRQ(ierr); 38*8a175baeSEmil Constantinescu /* this should be called with Y */ 39*8a175baeSEmil Constantinescu ierr = TSErrorWeightedENorm(ts,E,X,X,adapt->wnormtype,&enorm,&enorma,&enormr);CHKERRQ(ierr); 400a01e1b2SEmil Constantinescu } else { 410a01e1b2SEmil Constantinescu /* the method is NOT of GLEE type */ 42b1316ef9SEmil Constantinescu order_add=0; /* typically lower order estimates */ 43ae2316d0SEmil Constantinescu ierr = TSGetSolution(ts,&X);CHKERRQ(ierr); 44657c1e31SEmil Constantinescu if (!glee->Y) {ierr = VecDuplicate(X,&glee->Y);CHKERRQ(ierr);} 45657c1e31SEmil Constantinescu Y = glee->Y; 46ae2316d0SEmil Constantinescu order = adapt->candidates.order[0]; 47ae2316d0SEmil Constantinescu ierr = TSEvaluateStep(ts,order-1,Y,NULL);CHKERRQ(ierr); 487453f775SEmil Constantinescu ierr = TSErrorWeightedNorm(ts,X,Y,adapt->wnormtype,&enorm,&enorma,&enormr);CHKERRQ(ierr); 490a01e1b2SEmil Constantinescu } 50ae2316d0SEmil Constantinescu if (enorm > 1.) { 51657c1e31SEmil Constantinescu if (!*accept) safety *= glee->reject_safety; /* The last attempt also failed, shorten more aggressively */ 52ae2316d0SEmil Constantinescu if (h < (1 + PETSC_SQRT_MACHINE_EPSILON)*adapt->dt_min) { 53ae2316d0SEmil 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); 54ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 55657c1e31SEmil Constantinescu } else if (glee->always_accept) { 56ae2316d0SEmil 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); 57ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 58ae2316d0SEmil Constantinescu } else { 59ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, rejecting step of size %g\n",(double)enorm,(double)h);CHKERRQ(ierr); 60ae2316d0SEmil Constantinescu *accept = PETSC_FALSE; 61ae2316d0SEmil Constantinescu } 62ae2316d0SEmil Constantinescu } else { 63ae2316d0SEmil Constantinescu ierr = PetscInfo2(adapt,"Estimated scaled local truncation error %g, accepting step of size %g\n",(double)enorm,(double)h);CHKERRQ(ierr); 64ae2316d0SEmil Constantinescu *accept = PETSC_TRUE; 65ae2316d0SEmil Constantinescu } 66ae2316d0SEmil Constantinescu 67ae2316d0SEmil Constantinescu /* The optimal new step based purely on local truncation error for this step. */ 68ae2316d0SEmil Constantinescu if (enorm == 0.0) { 69ae2316d0SEmil Constantinescu hfac_lte = safety * PETSC_INFINITY; 70ae2316d0SEmil Constantinescu } else { 71b1316ef9SEmil Constantinescu hfac_lte = safety * PetscPowReal(enorm,-1./(order+order_add)); 72ae2316d0SEmil Constantinescu } 73657c1e31SEmil Constantinescu h_lte = h * PetscClipInterval(hfac_lte,glee->clip[0],glee->clip[1]); 74ae2316d0SEmil Constantinescu 75ae2316d0SEmil Constantinescu *next_sc = 0; 76ae2316d0SEmil Constantinescu *next_h = PetscClipInterval(h_lte,adapt->dt_min,adapt->dt_max); 77ae2316d0SEmil Constantinescu *wlte = enorm; 78ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 79ae2316d0SEmil Constantinescu } 80ae2316d0SEmil Constantinescu 81ae2316d0SEmil Constantinescu #undef __FUNCT__ 82726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptReset_GLEE" 83726095e4SEmil Constantinescu static PetscErrorCode TSAdaptReset_GLEE(TSAdapt adapt) 84ae2316d0SEmil Constantinescu { 85657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 86ae2316d0SEmil Constantinescu PetscErrorCode ierr; 87ae2316d0SEmil Constantinescu 88ae2316d0SEmil Constantinescu PetscFunctionBegin; 89657c1e31SEmil Constantinescu ierr = VecDestroy(&glee->Y);CHKERRQ(ierr); 90ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 91ae2316d0SEmil Constantinescu } 92ae2316d0SEmil Constantinescu 93ae2316d0SEmil Constantinescu #undef __FUNCT__ 94726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptDestroy_GLEE" 95726095e4SEmil Constantinescu static PetscErrorCode TSAdaptDestroy_GLEE(TSAdapt adapt) 96ae2316d0SEmil Constantinescu { 97ae2316d0SEmil Constantinescu PetscErrorCode ierr; 98ae2316d0SEmil Constantinescu 99ae2316d0SEmil Constantinescu PetscFunctionBegin; 100726095e4SEmil Constantinescu ierr = TSAdaptReset_GLEE(adapt);CHKERRQ(ierr); 101ae2316d0SEmil Constantinescu ierr = PetscFree(adapt->data);CHKERRQ(ierr); 102ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 103ae2316d0SEmil Constantinescu } 104ae2316d0SEmil Constantinescu 105ae2316d0SEmil Constantinescu #undef __FUNCT__ 106726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptSetFromOptions_GLEE" 107726095e4SEmil Constantinescu static PetscErrorCode TSAdaptSetFromOptions_GLEE(PetscOptionItems *PetscOptionsObject,TSAdapt adapt) 108ae2316d0SEmil Constantinescu { 109657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 110ae2316d0SEmil Constantinescu PetscErrorCode ierr; 111ae2316d0SEmil Constantinescu PetscInt two; 112ae2316d0SEmil Constantinescu PetscBool set; 113ae2316d0SEmil Constantinescu 114ae2316d0SEmil Constantinescu PetscFunctionBegin; 115726095e4SEmil Constantinescu ierr = PetscOptionsHead(PetscOptionsObject,"GLEE adaptive controller options");CHKERRQ(ierr); 116ae2316d0SEmil Constantinescu two = 2; 117657c1e31SEmil Constantinescu ierr = PetscOptionsRealArray("-ts_adapt_glee_clip","Admissible decrease/increase in step size","",glee->clip,&two,&set);CHKERRQ(ierr); 118657c1e31SEmil 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"); 119657c1e31SEmil Constantinescu ierr = PetscOptionsReal("-ts_adapt_glee_safety","Safety factor relative to target error","",glee->safety,&glee->safety,NULL);CHKERRQ(ierr); 120657c1e31SEmil 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); 121657c1e31SEmil 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); 122ae2316d0SEmil Constantinescu ierr = PetscOptionsTail();CHKERRQ(ierr); 123ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 124ae2316d0SEmil Constantinescu } 125ae2316d0SEmil Constantinescu 126ae2316d0SEmil Constantinescu #undef __FUNCT__ 127726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptView_GLEE" 128726095e4SEmil Constantinescu static PetscErrorCode TSAdaptView_GLEE(TSAdapt adapt,PetscViewer viewer) 129ae2316d0SEmil Constantinescu { 130657c1e31SEmil Constantinescu TSAdapt_GLEE *glee = (TSAdapt_GLEE*)adapt->data; 131ae2316d0SEmil Constantinescu PetscErrorCode ierr; 132ae2316d0SEmil Constantinescu PetscBool iascii; 133ae2316d0SEmil Constantinescu 134ae2316d0SEmil Constantinescu PetscFunctionBegin; 135ae2316d0SEmil Constantinescu ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 136ae2316d0SEmil Constantinescu if (iascii) { 137657c1e31SEmil Constantinescu if (glee->always_accept) {ierr = PetscViewerASCIIPrintf(viewer," GLEE: always accepting steps\n");CHKERRQ(ierr);} 138657c1e31SEmil Constantinescu ierr = PetscViewerASCIIPrintf(viewer," GLEE: clip fastest decrease %g, fastest increase %g\n",(double)glee->clip[0],(double)glee->clip[1]);CHKERRQ(ierr); 139657c1e31SEmil Constantinescu ierr = PetscViewerASCIIPrintf(viewer," GLEE: safety factor %g, extra factor after step rejection %g\n",(double)glee->safety,(double)glee->reject_safety);CHKERRQ(ierr); 140ae2316d0SEmil Constantinescu } 141ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 142ae2316d0SEmil Constantinescu } 143ae2316d0SEmil Constantinescu 144ae2316d0SEmil Constantinescu #undef __FUNCT__ 145726095e4SEmil Constantinescu #define __FUNCT__ "TSAdaptCreate_GLEE" 146ae2316d0SEmil Constantinescu /*MC 147657c1e31SEmil Constantinescu TSADAPTGLEE - GLEE adaptive controller for time stepping 148ae2316d0SEmil Constantinescu 149ae2316d0SEmil Constantinescu Level: intermediate 150ae2316d0SEmil Constantinescu 151ae2316d0SEmil Constantinescu .seealso: TS, TSAdapt, TSSetAdapt() 152ae2316d0SEmil Constantinescu M*/ 153726095e4SEmil Constantinescu PETSC_EXTERN PetscErrorCode TSAdaptCreate_GLEE(TSAdapt adapt) 154ae2316d0SEmil Constantinescu { 155ae2316d0SEmil Constantinescu PetscErrorCode ierr; 156726095e4SEmil Constantinescu TSAdapt_GLEE *a; 157ae2316d0SEmil Constantinescu 158ae2316d0SEmil Constantinescu PetscFunctionBegin; 159ae2316d0SEmil Constantinescu ierr = PetscNewLog(adapt,&a);CHKERRQ(ierr); 160ae2316d0SEmil Constantinescu adapt->data = (void*)a; 161726095e4SEmil Constantinescu adapt->ops->choose = TSAdaptChoose_GLEE; 162726095e4SEmil Constantinescu adapt->ops->setfromoptions = TSAdaptSetFromOptions_GLEE; 163726095e4SEmil Constantinescu adapt->ops->destroy = TSAdaptDestroy_GLEE; 164726095e4SEmil Constantinescu adapt->ops->view = TSAdaptView_GLEE; 165ae2316d0SEmil Constantinescu 166ae2316d0SEmil Constantinescu a->clip[0] = 0.1; 167ae2316d0SEmil Constantinescu a->clip[1] = 10.; 168*8a175baeSEmil Constantinescu a->safety = 0.99; 169ae2316d0SEmil Constantinescu a->reject_safety = 0.5; 170ae2316d0SEmil Constantinescu a->always_accept = PETSC_FALSE; 171ae2316d0SEmil Constantinescu PetscFunctionReturn(0); 172ae2316d0SEmil Constantinescu } 173