Actual source code: adapthist.c

petsc-3.11.4 2019-09-28
Report Typos and Errors
  1:  #include <petsc/private/tshistoryimpl.h>

  3: typedef struct {
  4:   TSHistory hist;
  5:   PetscBool bw;
  6: } TSAdapt_History;

  8: static PetscErrorCode TSAdaptChoose_History(TSAdapt adapt,TS ts,PetscReal h,PetscInt *next_sc,PetscReal *next_h,PetscBool *accept,PetscReal *wlte,PetscReal *wltea,PetscReal *wlter)
  9: {
 10:   PetscErrorCode  ierr;
 11:   PetscInt        step;
 12:   TSAdapt_History *thadapt = (TSAdapt_History*)adapt->data;

 15:   if (!thadapt->hist) SETERRQ(PetscObjectComm((PetscObject)adapt),PETSC_ERR_USER,"Need call TSAdaptHistorySetHistory()");
 16:   TSGetStepNumber(ts,&step);
 17:   TSHistoryGetTimeStep(thadapt->hist,thadapt->bw,step+1,next_h);
 18:   *accept  = PETSC_TRUE;
 19:   *next_sc = 0;
 20:   *wlte    = -1;
 21:   *wltea   = -1;
 22:   *wlter   = -1;
 23:   return(0);
 24: }

 26: static PetscErrorCode TSAdaptReset_History(TSAdapt adapt)
 27: {
 28:   TSAdapt_History *thadapt = (TSAdapt_History*)adapt->data;
 29:   PetscErrorCode  ierr;

 32:   TSHistoryDestroy(&thadapt->hist);
 33:   return(0);
 34: }

 36: static PetscErrorCode TSAdaptDestroy_History(TSAdapt adapt)
 37: {

 41:   TSAdaptReset_History(adapt);
 42:   PetscFree(adapt->data);
 43:   return(0);
 44: }

 46: /* this is not public, as TSHistory is not a public object */
 47: PetscErrorCode TSAdaptHistorySetTSHistory(TSAdapt adapt, TSHistory hist, PetscBool backward)
 48: {
 49:   PetscReal      *hist_t;
 50:   PetscInt       n;
 51:   PetscBool      flg;

 57:   PetscObjectTypeCompare((PetscObject)adapt,TSADAPTHISTORY,&flg);
 58:   if (!flg) return(0);
 59:   TSHistoryGetHistory(hist,&n,(const PetscReal**)&hist_t,NULL,NULL);
 60:   TSAdaptHistorySetHistory(adapt,n,hist_t,backward);
 61:   return(0);
 62: }

 64: /*@
 65:    TSAdaptHistoryGetStep - Gets time and time step for a given step number in the history

 67:    Logically Collective on TSAdapt

 69:    Input Parameters:
 70: +  adapt    - the TSAdapt context
 71: -  step     - the step number

 73:    Output Parameters:
 74: +  t  - the time corresponding to the requested step (can be NULL)
 75: -  dt - the time step to be taken at the requested step (can be NULL)

 77:    Notes: The time history is internally copied, and the user can free the hist array. The user still needs to specify the termination of the solve via TSSetMaxSteps().

 79:    Level: advanced

 81: .keywords: TSAdapt
 82: .seealso: TSGetAdapt(), TSAdaptSetType(), TSAdaptHistorySetTrajectory(), TSADAPTHISTORY
 83: @*/
 84: PetscErrorCode TSAdaptHistoryGetStep(TSAdapt adapt, PetscInt step, PetscReal *t, PetscReal *dt)
 85: {
 86:   TSAdapt_History *thadapt;
 87:   PetscBool       flg;
 88:   PetscErrorCode  ierr;

 93:   PetscObjectTypeCompare((PetscObject)adapt,TSADAPTHISTORY,&flg);
 94:   if (!flg) SETERRQ1(PetscObjectComm((PetscObject)adapt),PETSC_ERR_SUP,"Not for type %s",((PetscObject)adapt)->type_name);
 95:   thadapt = (TSAdapt_History*)adapt->data;
 96:   TSHistoryGetTimeStep(thadapt->hist,thadapt->bw,step,dt);
 97:   TSHistoryGetTime(thadapt->hist,thadapt->bw,step,t);
 98:   return(0);
 99: }

101: /*@
102:    TSAdaptHistorySetHistory - Sets a time history in the adaptor

104:    Logically Collective on TSAdapt

106:    Input Parameters:
107: +  adapt    - the TSAdapt context
108: .  n        - size of the time history
109: .  hist     - the time history
110: -  backward - if the time history has to be followed backward

112:    Notes: The time history is internally copied, and the user can free the hist array. The user still needs to specify the termination of the solve via TSSetMaxSteps().

114:    Level: advanced

116: .keywords: TSAdapt
117: .seealso: TSGetAdapt(), TSAdaptSetType(), TSAdaptHistorySetTrajectory(), TSADAPTHISTORY
118: @*/
119: PetscErrorCode TSAdaptHistorySetHistory(TSAdapt adapt, PetscInt n, PetscReal hist[], PetscBool backward)
120: {
121:   TSAdapt_History *thadapt;
122:   PetscBool       flg;
123:   PetscErrorCode  ierr;

130:   PetscObjectTypeCompare((PetscObject)adapt,TSADAPTHISTORY,&flg);
131:   if (!flg) return(0);
132:   thadapt = (TSAdapt_History*)adapt->data;
133:   TSHistoryDestroy(&thadapt->hist);
134:   TSHistoryCreate(PetscObjectComm((PetscObject)adapt),&thadapt->hist);
135:   TSHistorySetHistory(thadapt->hist,n,hist,NULL,PETSC_FALSE);
136:   thadapt->bw = backward;
137:   return(0);
138: }

140: /*@
141:    TSAdaptHistorySetTrajectory - Sets a time history in the adaptor from a given TSTrajectory

143:    Logically Collective on TSAdapt

145:    Input Parameters:
146: +  adapt    - the TSAdapt context
147: .  tj       - the TSTrajectory context
148: -  backward - if the time history has to be followed backward

150:    Notes: The time history is internally copied, and the user can destroy the TSTrajectory if not needed. The user still needs to specify the termination of the solve via TSSetMaxSteps().

152:    Level: advanced

154: .keywords: TSAdapt
155: .seealso: TSGetAdapt(), TSAdaptSetType(), TSAdaptHistorySetHistory(), TSADAPTHISTORY
156: @*/
157: PetscErrorCode TSAdaptHistorySetTrajectory(TSAdapt adapt, TSTrajectory tj, PetscBool backward)
158: {
159:   PetscBool       flg;
160:   PetscErrorCode  ierr;

166:   PetscObjectTypeCompare((PetscObject)adapt,TSADAPTHISTORY,&flg);
167:   if (!flg) return(0);
168:   TSAdaptHistorySetTSHistory(adapt,tj->tsh,backward);
169:   return(0);
170: }


173: /*MC
174:    TSADAPTHISTORY - Time stepping controller that follows a given time history, used for Tangent Linear Model simulations

176:    Level: developer

178: .seealso: TS, TSAdapt, TSGetAdapt(), TSAdaptHistorySetHistory()
179: M*/
180: PETSC_EXTERN PetscErrorCode TSAdaptCreate_History(TSAdapt adapt)
181: {
182:   PetscErrorCode     ierr;
183:   TSAdapt_History *thadapt;

186:   PetscNew(&thadapt);
187:   adapt->matchstepfac[0] = PETSC_SMALL; /* prevent from accumulation errors */
188:   adapt->matchstepfac[1] = 0.0;         /* we will always match the final step, prevent TSAdaptChoose to mess with it */
189:   adapt->data            = thadapt;

191:   adapt->ops->choose  = TSAdaptChoose_History;
192:   adapt->ops->reset   = TSAdaptReset_History;
193:   adapt->ops->destroy = TSAdaptDestroy_History;
194:   return(0);
195: }