1 #define PETSCTS_DLL 2 3 /* 4 Code for timestepping with implicit Theta method 5 6 Notes: 7 This method can be applied to DAE. 8 9 This method is cast as a 1-stage implicit Runge-Kutta method. 10 11 Theta | Theta 12 ------------- 13 | 1 14 15 To apply a diagonally implicit RK method to DAE, the stage formula 16 17 X_i = x + h sum_j a_ij X'_j 18 19 is interpreted as a formula for X'_i in terms of X_i and known stuff (X'_j, j<i) 20 */ 21 #include "private/tsimpl.h" /*I "petscts.h" I*/ 22 23 typedef struct { 24 Vec X,Xdot; /* Storage for one stage */ 25 Vec res; /* DAE residuals */ 26 PetscTruth extrapolate; 27 PetscReal Theta; 28 PetscReal shift; 29 PetscReal stage_time; 30 } TS_Theta; 31 32 #undef __FUNCT__ 33 #define __FUNCT__ "TSStep_Theta" 34 static PetscErrorCode TSStep_Theta(TS ts,PetscInt *steps,PetscReal *ptime) 35 { 36 TS_Theta *th = (TS_Theta*)ts->data; 37 PetscInt i,max_steps = ts->max_steps,its,lits; 38 PetscErrorCode ierr; 39 40 PetscFunctionBegin; 41 *steps = -ts->steps; 42 *ptime = ts->ptime; 43 44 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 45 46 for (i=0; i<max_steps; i++) { 47 if (ts->ptime + ts->time_step > ts->max_time) break; 48 ierr = TSPreStep(ts);CHKERRQ(ierr); 49 50 th->stage_time = ts->ptime + th->Theta*ts->time_step; 51 th->shift = 1./(th->Theta*ts->time_step); 52 53 if (th->extrapolate) { 54 ierr = VecWAXPY(th->X,1./th->shift,th->Xdot,ts->vec_sol);CHKERRQ(ierr); 55 } else { 56 ierr = VecCopy(ts->vec_sol,th->X);CHKERRQ(ierr); 57 } 58 ierr = SNESSolve(ts->snes,PETSC_NULL,th->X);CHKERRQ(ierr); 59 ierr = SNESGetIterationNumber(ts->snes,&its);CHKERRQ(ierr); 60 ierr = SNESGetLinearSolveIterations(ts->snes,&lits);CHKERRQ(ierr); 61 ts->nonlinear_its += its; ts->linear_its += lits; 62 63 ierr = VecAXPBYPCZ(th->Xdot,-th->shift,th->shift,0,ts->vec_sol,th->X);CHKERRQ(ierr); 64 ierr = VecAXPY(ts->vec_sol,ts->time_step,th->Xdot);CHKERRQ(ierr); 65 ts->ptime += ts->time_step; 66 ts->steps++; 67 68 ierr = TSPostStep(ts);CHKERRQ(ierr); 69 ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 70 } 71 72 *steps += ts->steps; 73 *ptime = ts->ptime; 74 PetscFunctionReturn(0); 75 } 76 77 /*------------------------------------------------------------*/ 78 #undef __FUNCT__ 79 #define __FUNCT__ "TSDestroy_Theta" 80 static PetscErrorCode TSDestroy_Theta(TS ts) 81 { 82 TS_Theta *th = (TS_Theta*)ts->data; 83 PetscErrorCode ierr; 84 85 PetscFunctionBegin; 86 if (th->X) {ierr = VecDestroy(th->X);CHKERRQ(ierr);} 87 if (th->Xdot) {ierr = VecDestroy(th->Xdot);CHKERRQ(ierr);} 88 if (th->res) {ierr = VecDestroy(th->res);CHKERRQ(ierr);} 89 ierr = PetscFree(th);CHKERRQ(ierr); 90 PetscFunctionReturn(0); 91 } 92 93 /* 94 This defines the nonlinear equation that is to be solved with SNES 95 G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0 96 */ 97 #undef __FUNCT__ 98 #define __FUNCT__ "TSThetaFunction" 99 static PetscErrorCode TSThetaFunction(SNES snes,Vec x,Vec y,void *ctx) 100 { 101 TS ts = (TS)ctx; 102 TS_Theta *th = (TS_Theta*)ts->data; 103 PetscErrorCode ierr; 104 105 PetscFunctionBegin; 106 ierr = VecAXPBYPCZ(th->Xdot,-th->shift,th->shift,0,ts->vec_sol,x);CHKERRQ(ierr); 107 ierr = TSComputeIFunction(ts,th->stage_time,x,th->Xdot,y);CHKERRQ(ierr); 108 PetscFunctionReturn(0); 109 } 110 111 #undef __FUNCT__ 112 #define __FUNCT__ "TSThetaJacobian" 113 static PetscErrorCode TSThetaJacobian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *str,void *ctx) 114 { 115 TS ts = (TS)ctx; 116 TS_Theta *th = (TS_Theta*)ts->data; 117 PetscErrorCode ierr; 118 119 PetscFunctionBegin; 120 /* th->Xdot has already been computed in TSThetaFunction (SNES guarantees this) */ 121 ierr = TSComputeIJacobian(ts,th->stage_time,x,th->Xdot,th->shift,A,B,str);CHKERRQ(ierr); 122 PetscFunctionReturn(0); 123 } 124 125 126 #undef __FUNCT__ 127 #define __FUNCT__ "TSSetUp_Theta" 128 static PetscErrorCode TSSetUp_Theta(TS ts) 129 { 130 TS_Theta *th = (TS_Theta*)ts->data; 131 PetscErrorCode ierr; 132 133 PetscFunctionBegin; 134 if (ts->problem_type == TS_LINEAR) { 135 SETERRQ(PETSC_ERR_ARG_WRONG,"Only for nonlinear problems"); 136 } 137 ierr = VecDuplicate(ts->vec_sol,&th->X);CHKERRQ(ierr); 138 ierr = VecDuplicate(ts->vec_sol,&th->Xdot);CHKERRQ(ierr); 139 ierr = VecDuplicate(ts->vec_sol,&th->res);CHKERRQ(ierr); 140 ierr = SNESSetFunction(ts->snes,th->res,TSThetaFunction,ts);CHKERRQ(ierr); 141 /* This is nasty. SNESSetFromOptions() is usually called in TSSetFromOptions(). With -snes_mf_operator, it will 142 replace A and we don't want to mess with that. With -snes_mf, A and B will be replaced as well as the function and 143 context. Note that SNESSetFunction() normally has not been called before SNESSetFromOptions(), so when -snes_mf sets 144 the Jacobian user context to snes->funP, it will actually be NULL. This is not a problem because both snes->funP and 145 snes->jacP should be the TS. */ 146 { 147 Mat A,B; 148 PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 149 void *ctx; 150 ierr = SNESGetJacobian(ts->snes,&A,&B,&func,&ctx);CHKERRQ(ierr); 151 ierr = SNESSetJacobian(ts->snes,A?A:ts->A,B?B:ts->B,func?func:&TSThetaJacobian,ctx?ctx:ts);CHKERRQ(ierr); 152 } 153 PetscFunctionReturn(0); 154 } 155 /*------------------------------------------------------------*/ 156 157 #undef __FUNCT__ 158 #define __FUNCT__ "TSSetFromOptions_Theta" 159 static PetscErrorCode TSSetFromOptions_Theta(TS ts) 160 { 161 TS_Theta *th = (TS_Theta*)ts->data; 162 PetscErrorCode ierr; 163 164 PetscFunctionBegin; 165 ierr = PetscOptionsHead("Theta ODE solver options");CHKERRQ(ierr); 166 { 167 ierr = PetscOptionsReal("-ts_theta_theta","Location of stage (0<Theta<=1)","TSThetaSetTheta",th->Theta,&th->Theta,PETSC_NULL);CHKERRQ(ierr); 168 ierr = PetscOptionsTruth("-ts_theta_extrapolate","Extrapolate stage solution from previous solution (sometimes unstable)","TSThetaSetExtrapolate",th->extrapolate,&th->extrapolate,PETSC_NULL);CHKERRQ(ierr); 169 } 170 ierr = PetscOptionsTail();CHKERRQ(ierr); 171 PetscFunctionReturn(0); 172 } 173 174 #undef __FUNCT__ 175 #define __FUNCT__ "TSView_Theta" 176 static PetscErrorCode TSView_Theta(TS ts,PetscViewer viewer) 177 { 178 TS_Theta *th = (TS_Theta*)ts->data; 179 PetscTruth iascii; 180 PetscErrorCode ierr; 181 182 PetscFunctionBegin; 183 ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 184 if (iascii) { 185 ierr = PetscViewerASCIIPrintf(viewer," Theta=%G\n",th->Theta);CHKERRQ(ierr); 186 ierr = PetscViewerASCIIPrintf(viewer," Extrapolation=%s\n",th->extrapolate?"yes":"no");CHKERRQ(ierr); 187 } else { 188 SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for TS_Theta",((PetscObject)viewer)->type_name); 189 } 190 PetscFunctionReturn(0); 191 } 192 193 EXTERN_C_BEGIN 194 #undef __FUNCT__ 195 #define __FUNCT__ "TSThetaGetTheta_Theta" 196 PetscErrorCode PETSCTS_DLLEXPORT TSThetaGetTheta_Theta(TS ts,PetscReal *theta) 197 { 198 TS_Theta *th = (TS_Theta*)ts->data; 199 200 PetscFunctionBegin; 201 *theta = th->Theta; 202 PetscFunctionReturn(0); 203 } 204 205 #undef __FUNCT__ 206 #define __FUNCT__ "TSThetaSetTheta_Theta" 207 PetscErrorCode PETSCTS_DLLEXPORT TSThetaSetTheta_Theta(TS ts,PetscReal theta) 208 { 209 TS_Theta *th = (TS_Theta*)ts->data; 210 211 PetscFunctionBegin; 212 th->Theta = theta; 213 PetscFunctionReturn(0); 214 } 215 EXTERN_C_END 216 217 /* ------------------------------------------------------------ */ 218 /*MC 219 TSTHETA - DAE solver using the implicit Theta method 220 221 Level: beginner 222 223 .seealso: TSCreate(), TS, TSSetType() 224 225 M*/ 226 EXTERN_C_BEGIN 227 #undef __FUNCT__ 228 #define __FUNCT__ "TSCreate_Theta" 229 PetscErrorCode PETSCTS_DLLEXPORT TSCreate_Theta(TS ts) 230 { 231 TS_Theta *th; 232 PetscErrorCode ierr; 233 234 PetscFunctionBegin; 235 ts->ops->destroy = TSDestroy_Theta; 236 ts->ops->view = TSView_Theta; 237 ts->ops->setup = TSSetUp_Theta; 238 ts->ops->step = TSStep_Theta; 239 ts->ops->setfromoptions = TSSetFromOptions_Theta; 240 241 ts->problem_type = TS_NONLINEAR; 242 ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 243 ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 244 245 ierr = PetscNewLog(ts,TS_Theta,&th);CHKERRQ(ierr); 246 ts->data = (void*)th; 247 248 th->extrapolate = PETSC_TRUE; 249 th->Theta = 0.5; 250 251 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSThetaGetTheta_C","TSThetaGetTheta_Theta",TSThetaGetTheta_Theta);CHKERRQ(ierr); 252 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSThetaSetTheta_C","TSThetaSetTheta_Theta",TSThetaSetTheta_Theta);CHKERRQ(ierr); 253 PetscFunctionReturn(0); 254 } 255 EXTERN_C_END 256 257 #undef __FUNCT__ 258 #define __FUNCT__ "TSThetaGetTheta" 259 /*@ 260 TSThetaGetTheta - Get the abscissa of the stage in (0,1]. 261 262 Not Collective 263 264 Input Parameter: 265 . ts - timestepping context 266 267 Output Parameter: 268 . theta - stage abscissa 269 270 Note: 271 Use of this function is normally only required to hack TSTHETA to use a modified integration scheme. 272 273 Level: Advanced 274 275 .seealso: TSThetaSetTheta() 276 @*/ 277 PetscErrorCode PETSCTS_DLLEXPORT TSThetaGetTheta(TS ts,PetscReal *theta) 278 { 279 PetscErrorCode ierr,(*f)(TS,PetscReal*); 280 281 PetscFunctionBegin; 282 PetscValidHeaderSpecific(ts,TS_COOKIE,1); 283 PetscValidPointer(theta,2); 284 ierr = PetscObjectQueryFunction((PetscObject)ts,"TSThetaGetTheta_C",(void(**)(void))&f);CHKERRQ(ierr); 285 if (f) {ierr = (*f)(ts,theta);CHKERRQ(ierr);} 286 PetscFunctionReturn(0); 287 } 288 289 #undef __FUNCT__ 290 #define __FUNCT__ "TSThetaSetTheta" 291 /*@ 292 TSThetaSetTheta - Set the abscissa of the stage in (0,1]. 293 294 Not Collective 295 296 Input Parameter: 297 + ts - timestepping context 298 - theta - stage abscissa 299 300 Options Database: 301 . -ts_theta_theta <theta> 302 303 Level: Intermediate 304 305 .seealso: TSThetaGetTheta() 306 @*/ 307 PetscErrorCode PETSCTS_DLLEXPORT TSThetaSetTheta(TS ts,PetscReal theta) 308 { 309 PetscErrorCode ierr,(*f)(TS,PetscReal); 310 311 PetscFunctionBegin; 312 PetscValidHeaderSpecific(ts,TS_COOKIE,1); 313 PetscValidPointer(theta,2); 314 ierr = PetscObjectQueryFunction((PetscObject)ts,"TSThetaSetTheta_C",(void(**)(void))&f);CHKERRQ(ierr); 315 if (!f) SETERRQ1(PETSC_ERR_SUP,"TS type %s",((PetscObject)ts)->type_name); 316 ierr = (*f)(ts,theta);CHKERRQ(ierr); 317 PetscFunctionReturn(0); 318 } 319