1 /* 2 * eimex.c 3 * 4 * Created on: Jun 21, 2012 5 * Written by Hong Zhang (zhang@vt.edu), Virginia Tech 6 * Emil Constantinescu (emconsta@mcs.anl.gov), Argonne National Laboratory 7 */ 8 /* 9 Code for timestepping with Extrapolated IMEX methods 10 11 Notes: 12 The general system is written as 13 14 G(t,X,Xdot) = F(t,X) 15 16 where G represents the stiff part of the physics and F represents the non-stiff part. 17 This method is designed to be linearly implicit on G and can use an approximate and lagged Jacobian. 18 19 Another common form for the system is 20 21 y'=f(x)+g(x) 22 23 The relationship between F,G and f,g is 24 25 G = y'-g(x), F = f(x) 26 27 References 28 E. Constantinescu and A. Sandu, Extrapolated implicit-explicit time stepping, SIAM Journal on Scientific 29 Computing, 31 (2010), pp. 4452–4477. 30 31 */ 32 #include <petsc-private/tsimpl.h> /*I "petscts.h" I*/ 33 34 static const PetscInt TSEIMEXDefault = 3; 35 36 typedef struct { 37 PetscInt row_ind; /* Return the term T[row_ind][col_ind]*/ 38 PetscInt col_ind; /* Return the term T[row_ind][col_ind]*/ 39 PetscInt nstages; /* Numbers of stages in current scheme*/ 40 PetscInt max_rows; /* Maximum number of rows */ 41 PetscInt *N; /* Harmonic sequence N[max_rows] */ 42 Vec Y; /* States computed during the step, used to complete the step */ 43 Vec Z; /* For shift*(Y-Z) */ 44 Vec *T; /* Working table, size determined by nstages */ 45 Vec YdotRHS; /* f(x) Work vector holding YdotRHS during residual evaluation */ 46 Vec YdotI; /* xdot-g(x) Work vector holding YdotI = G(t,x,xdot) when xdot =0 */ 47 Vec Ydot; /* f(x)+g(x) Work vector*/ 48 Vec VecSolPrev; /* Work vector holding the solution from the previous step (used for interpolation)*/ 49 // PetscScalar *work; /* Scalar work space of length number of stages, used to prepare VecMAXPY() */ 50 PetscReal shift; 51 PetscReal ctime; 52 PetscBool recompute_jacobian; /* Recompute the Jacobian at each stage, default is to freeze the Jacobian at the start of each step */ 53 PetscBool ord_adapt; /*order adapativity*/ 54 TSStepStatus status; 55 } TS_EIMEX; 56 57 /* This function is pure */ 58 static PetscInt Map(PetscInt i, PetscInt j, PetscInt s) 59 { 60 return ((2*s-j+1)*j/2+i-j); 61 } 62 63 64 #undef __FUNCT__ 65 #define __FUNCT__ "TSEvaluateStep_EIMEX" 66 static PetscErrorCode TSEvaluateStep_EIMEX(TS ts,PetscInt order,Vec X,PetscBool *done) 67 { 68 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 69 const PetscInt ns = ext->nstages; 70 PetscErrorCode ierr; 71 PetscFunctionBegin; 72 ierr = VecCopy(ext->T[Map(ext->row_ind,ext->col_ind,ns)],X);CHKERRQ(ierr); 73 PetscFunctionReturn(0); 74 } 75 76 77 #undef __FUNCT__ 78 #define __FUNCT__ "TSStage_EIMEX" 79 static PetscErrorCode TSStage_EIMEX(TS ts,PetscInt istage) 80 { 81 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 82 PetscReal h; 83 Vec Y=ext->Y, Z=ext->Z; 84 SNES snes; 85 TSAdapt adapt; 86 PetscInt i,its,lits; 87 PetscBool accept; 88 PetscErrorCode ierr; 89 90 PetscFunctionBegin; 91 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 92 h = ts->time_step/ext->N[istage];/*step size for the istage-th stage*/ 93 ext->shift = 1./h; 94 ierr = SNESSetLagJacobian(snes,-2);CHKERRQ(ierr); /* Recompute the Jacobian on this solve, but not again */ 95 ierr = VecCopy(ext->VecSolPrev,Y);CHKERRQ(ierr); /*Take the previous solution as intial step*/ 96 97 for(i=0; i<ext->N[istage]; i++){ 98 ext->ctime = ts->ptime + h*i; 99 ierr = VecCopy(Y,Z);CHKERRQ(ierr);/*Save the solution of the previous substep*/ 100 ierr = SNESSolve(snes,PETSC_NULL,Y);CHKERRQ(ierr); 101 ierr = SNESGetIterationNumber(snes,&its);CHKERRQ(ierr); 102 ierr = SNESGetLinearSolveIterations(snes,&lits);CHKERRQ(ierr); 103 ts->snes_its += its; ts->ksp_its += lits; 104 ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr); 105 ierr = TSAdaptCheckStage(adapt,ts,&accept);CHKERRQ(ierr); 106 } 107 108 PetscFunctionReturn(0); 109 } 110 111 112 #undef __FUNCT__ 113 #define __FUNCT__ "TSStep_EIMEX" 114 static PetscErrorCode TSStep_EIMEX(TS ts) 115 { 116 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 117 const PetscInt ns = ext->nstages; 118 Vec *T=ext->T, Y=ext->Y; 119 120 SNES snes; 121 PetscInt i,j; 122 PetscBool accept = PETSC_FALSE; 123 PetscErrorCode ierr; 124 PetscReal alpha,local_error; 125 PetscFunctionBegin; 126 127 ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr); 128 ierr = SNESSetType(snes,"ksponly"); CHKERRQ(ierr); 129 ext->status = TS_STEP_INCOMPLETE; 130 131 ierr = VecCopy(ts->vec_sol,ext->VecSolPrev);CHKERRQ(ierr); 132 133 /* Apply n_j steps of the base method to obtain solutions of T(j,1),1<=j<=s*/ 134 for(j=0; j<ns; j++){ 135 ierr = TSStage_EIMEX(ts,j);CHKERRQ(ierr); 136 ierr = VecCopy(Y,T[j]); CHKERRQ(ierr); 137 } 138 139 for(i=1;i<ns;i++){ 140 for(j=i;j<ns;j++){ 141 alpha = -(PetscReal)ext->N[j]/ext->N[j-i]; 142 ierr = VecAXPBYPCZ(T[Map(j,i,ns)],alpha,1.0,0,T[Map(j,i-1,ns)], 143 T[Map(j-1,i-1,ns)]);/*T[j][i]=alpha*T[j][i-1]+T[j-1][i-1]*/ 144 alpha = 1.0/(1.0 + alpha); 145 ierr = VecScale(T[Map(j,i,ns)],alpha);CHKERRQ(ierr); 146 } 147 } 148 149 ierr = TSEvaluateStep(ts,ns,ts->vec_sol,PETSC_NULL);CHKERRQ(ierr);/*update ts solution */ 150 151 if(ext->ord_adapt && ext->nstages < ext->max_rows){ 152 accept = PETSC_FALSE; 153 while(!accept && ext->nstages < ext->max_rows){ 154 ierr = TSErrorNormWRMS(ts,T[Map(ext->nstages-1,ext->nstages-2,ext->nstages)],&local_error);CHKERRQ(ierr); 155 accept = (local_error < 1.0)? PETSC_TRUE : PETSC_FALSE; 156 157 if(!accept){/* add one more stage*/ 158 ierr = TSStage_EIMEX(ts,ext->nstages);CHKERRQ(ierr); 159 ext->nstages++; ext->row_ind++; ext->col_ind++; 160 /*T table need to be recycled*/ 161 ierr = VecDuplicateVecs(ts->vec_sol,(1+ext->nstages)*ext->nstages/2,&ext->T);CHKERRQ(ierr); 162 for(i=0; i<ext->nstages-1; i++){ 163 for(j=0; j<=i; j++){ 164 ierr = VecCopy(T[Map(i,j,ext->nstages-1)],ext->T[Map(i,j,ext->nstages)]);CHKERRQ(ierr); 165 } 166 } 167 ierr = VecDestroyVecs(ext->nstages*(ext->nstages-1)/2,&T);CHKERRQ(ierr); 168 T = ext->T; /*reset the pointer*/ 169 /*recycling finished, store the new solution*/ 170 ierr = VecCopy(Y,T[ext->nstages-1]); CHKERRQ(ierr); 171 /*extrapolation for the newly added stage*/ 172 for(i=1;i<ext->nstages;i++){ 173 alpha = -(PetscReal)ext->N[ext->nstages-1]/ext->N[ext->nstages-1-i]; 174 ierr = VecAXPBYPCZ(T[Map(ext->nstages-1,i,ext->nstages)],alpha,1.0,0,T[Map(ext->nstages-1,i-1,ext->nstages)],T[Map(ext->nstages-1-1,i-1,ext->nstages)]);/*T[ext->nstages-1][i]=alpha*T[ext->nstages-1][i-1]+T[ext->nstages-1-1][i-1]*/ 175 alpha = 1.0/(1.0 + alpha); 176 ierr = VecScale(T[Map(ext->nstages-1,i,ext->nstages)],alpha);CHKERRQ(ierr); 177 } 178 /*update ts solution */ 179 ierr = TSEvaluateStep(ts,ext->nstages,ts->vec_sol,PETSC_NULL);CHKERRQ(ierr); 180 }/*end if !accept*/ 181 }/*end while*/ 182 183 if(ext->nstages == ext->max_rows){ 184 ierr = PetscInfo(ts,"Max number of rows has been used\n");CHKERRQ(ierr); 185 } 186 }/*end if ext->ord_adapt*/ 187 188 ts->ptime += ts->time_step; 189 ts->steps++; 190 ext->status = TS_STEP_COMPLETE; 191 192 if (ext->status != TS_STEP_COMPLETE && !ts->reason) ts->reason = TS_DIVERGED_STEP_REJECTED; 193 PetscFunctionReturn(0); 194 } 195 196 197 /*cubic Hermit spline*/ 198 #undef __FUNCT__ 199 #define __FUNCT__ "TSInterpolate_EIMEX" 200 static PetscErrorCode TSInterpolate_EIMEX(TS ts,PetscReal itime,Vec X) 201 { 202 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 203 PetscReal t,a,b; 204 Vec Y0=ext->VecSolPrev,Y1=ext->Y, 205 Ydot=ext->Ydot,YdotI=ext->YdotI; 206 const PetscReal h = ts->time_step_prev; 207 PetscErrorCode ierr; 208 PetscScalar *x; 209 PetscFunctionBegin; 210 t = (itime -ts->ptime + h)/h; 211 /*YdotI = -f(x)-g(x)*/ 212 213 ierr = VecZeroEntries(Ydot);CHKERRQ(ierr); 214 ierr = TSComputeIFunction(ts,ts->ptime-h,Y0,Ydot,YdotI,PETSC_FALSE);CHKERRQ(ierr); 215 216 a = 2.0*pow(t,3) - 3.0*t*t + 1.0; 217 b = -(pow(t,3) - 2.0*t*t + t)*h; 218 ierr = VecAXPBYPCZ(X,a,b,0.0,Y0,YdotI);CHKERRQ(ierr); 219 220 ierr = TSComputeIFunction(ts,ts->ptime,Y1,Ydot,YdotI,PETSC_FALSE);CHKERRQ(ierr); 221 a = -2.0*pow(t,3)+3.0*t*t; 222 b = -(pow(t,3) - t*t)*h; 223 ierr = VecAXPBYPCZ(X,a,b,1.0,Y1,YdotI);CHKERRQ(ierr); 224 225 PetscFunctionReturn(0); 226 } 227 228 229 #undef __FUNCT__ 230 #define __FUNCT__ "TSReset_EIMEX" 231 static PetscErrorCode TSReset_EIMEX(TS ts) 232 { 233 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 234 PetscInt ns; 235 PetscErrorCode ierr; 236 237 PetscFunctionBegin; 238 ns = ext->nstages; 239 ierr = VecDestroyVecs((1+ns)*ns/2,&ext->T);CHKERRQ(ierr); 240 ierr = VecDestroy(&ext->Y);CHKERRQ(ierr); 241 ierr = VecDestroy(&ext->Z);CHKERRQ(ierr); 242 ierr = VecDestroy(&ext->YdotRHS);CHKERRQ(ierr); 243 ierr = VecDestroy(&ext->YdotI);CHKERRQ(ierr); 244 ierr = VecDestroy(&ext->Ydot);CHKERRQ(ierr); 245 ierr = VecDestroy(&ext->VecSolPrev);CHKERRQ(ierr); 246 ierr = PetscFree(ext->N);CHKERRQ(ierr); 247 PetscFunctionReturn(0); 248 } 249 250 #undef __FUNCT__ 251 #define __FUNCT__ "TSDestroy_EIMEX" 252 static PetscErrorCode TSDestroy_EIMEX(TS ts) 253 { 254 PetscErrorCode ierr; 255 256 PetscFunctionBegin; 257 ierr = TSReset_EIMEX(ts);CHKERRQ(ierr); 258 ierr = PetscFree(ts->data);CHKERRQ(ierr); 259 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetMaxRows_C","",PETSC_NULL);CHKERRQ(ierr); 260 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetRowCol_C","",PETSC_NULL);CHKERRQ(ierr); 261 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetOrdAdapt_C","",PETSC_NULL);CHKERRQ(ierr); 262 263 PetscFunctionReturn(0); 264 } 265 266 267 #undef __FUNCT__ 268 #define __FUNCT__ "TSEIMEXGetVecs" 269 static PetscErrorCode TSEIMEXGetVecs(TS ts,DM dm,Vec *Z,Vec *Ydot,Vec *YdotI, Vec *YdotRHS) 270 { 271 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 272 PetscErrorCode ierr; 273 274 PetscFunctionBegin; 275 if (Z) { 276 if (dm && dm != ts->dm) { 277 ierr = DMGetNamedGlobalVector(dm,"TSEIMEX_Z",Z);CHKERRQ(ierr); 278 } else *Z = ext->Z; 279 } 280 if (Ydot) { 281 if (dm && dm != ts->dm) { 282 ierr = DMGetNamedGlobalVector(dm,"TSEIMEX_Ydot",Ydot);CHKERRQ(ierr); 283 } else *Ydot = ext->Ydot; 284 } 285 if (YdotI) { 286 if (dm && dm != ts->dm) { 287 ierr = DMGetNamedGlobalVector(dm,"TSEIMEX_YdotI",YdotI);CHKERRQ(ierr); 288 } else *YdotI = ext->YdotI; 289 } 290 if (YdotRHS) { 291 if (dm && dm != ts->dm) { 292 ierr = DMGetNamedGlobalVector(dm,"TSEIMEX_YdotRHS",YdotRHS);CHKERRQ(ierr); 293 } else *YdotRHS = ext->YdotRHS; 294 } 295 PetscFunctionReturn(0); 296 } 297 298 299 #undef __FUNCT__ 300 #define __FUNCT__ "TSEIMEXRestoreVecs" 301 static PetscErrorCode TSEIMEXRestoreVecs(TS ts,DM dm,Vec *Z,Vec *Ydot,Vec *YdotI,Vec *YdotRHS) 302 { 303 PetscErrorCode ierr; 304 305 PetscFunctionBegin; 306 if (Z) { 307 if (dm && dm != ts->dm) { 308 ierr = DMRestoreNamedGlobalVector(dm,"TSEIMEX_Z",Z);CHKERRQ(ierr); 309 } 310 } 311 if (Ydot) { 312 if (dm && dm != ts->dm) { 313 ierr = DMRestoreNamedGlobalVector(dm,"TSEIMEX_Ydot",Ydot);CHKERRQ(ierr); 314 } 315 } 316 if (YdotI) { 317 if (dm && dm != ts->dm) { 318 ierr = DMRestoreNamedGlobalVector(dm,"TSEIMEX_YdotI",YdotI);CHKERRQ(ierr); 319 } 320 } 321 if (YdotRHS) { 322 if (dm && dm != ts->dm) { 323 ierr = DMRestoreNamedGlobalVector(dm,"TSEIMEX_YdotRHS",YdotRHS);CHKERRQ(ierr); 324 } 325 } 326 PetscFunctionReturn(0); 327 } 328 329 330 /* 331 This defines the nonlinear equation that is to be solved with SNES 332 Fn[t0+Theta*dt, U, (U-U0)*shift] = 0 333 In the case of Backward Euler, Fn = (U-U0)/h-g(t1,U)) 334 Since FormIFunction calculates G = ydot - g(t,y), ydot will be set to (U-U0)/h 335 */ 336 #undef __FUNCT__ 337 #define __FUNCT__ "SNESTSFormFunction_EIMEX" 338 static PetscErrorCode SNESTSFormFunction_EIMEX(SNES snes,Vec X,Vec G,TS ts) 339 { 340 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 341 PetscErrorCode ierr; 342 Vec Ydot,Z; 343 DM dm,dmsave; 344 345 PetscFunctionBegin; 346 ierr = VecZeroEntries(G);CHKERRQ(ierr); 347 348 ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 349 ierr = TSEIMEXGetVecs(ts,dm,&Z,&Ydot,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 350 ierr = VecZeroEntries(Ydot);CHKERRQ(ierr); 351 dmsave = ts->dm; 352 ts->dm = dm; 353 ierr = TSComputeIFunction(ts,ext->ctime,X,Ydot,G,PETSC_FALSE);CHKERRQ(ierr); 354 /* PETSC_FALSE indicates non-imex, adding explicit RHS to the implicit I function. */ 355 ierr = VecCopy(G,Ydot);CHKERRQ(ierr); 356 ts->dm = dmsave; 357 ierr = TSEIMEXRestoreVecs(ts,dm,&Z,&Ydot,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 358 359 PetscFunctionReturn(0); 360 } 361 362 /* 363 This defined the Jacobian matrix for SNES. Jn = (I/h-g'(t,y)) 364 */ 365 #undef __FUNCT__ 366 #define __FUNCT__ "SNESTSFormJacobian_EIMEX" 367 static PetscErrorCode SNESTSFormJacobian_EIMEX(SNES snes,Vec X,Mat *A,Mat *B,MatStructure *str,TS ts) 368 { 369 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 370 Vec Ydot; 371 PetscErrorCode ierr; 372 DM dm,dmsave; 373 PetscFunctionBegin; 374 ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr); 375 ierr = TSEIMEXGetVecs(ts,dm,PETSC_NULL,&Ydot,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 376 /* ierr = VecZeroEntries(Ydot);CHKERRQ(ierr);*/ 377 /* ext->Ydot have already been computed in SNESTSFormFunction_EIMEX (SNES guarantees this) */ 378 dmsave = ts->dm; 379 ts->dm = dm; 380 ierr = TSComputeIJacobian(ts,ts->ptime,X,Ydot,ext->shift,A,B,str,PETSC_TRUE);CHKERRQ(ierr); 381 ts->dm = dmsave; 382 ierr = TSEIMEXRestoreVecs(ts,dm,PETSC_NULL,&Ydot,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 383 PetscFunctionReturn(0); 384 } 385 386 #undef __FUNCT__ 387 #define __FUNCT__ "DMCoarsenHook_TSEIMEX" 388 static PetscErrorCode DMCoarsenHook_TSEIMEX(DM fine,DM coarse,void *ctx) 389 { 390 391 PetscFunctionBegin; 392 PetscFunctionReturn(0); 393 } 394 395 #undef __FUNCT__ 396 #define __FUNCT__ "DMRestrictHook_TSEIMEX" 397 static PetscErrorCode DMRestrictHook_TSEIMEX(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx) 398 { 399 TS ts = (TS)ctx; 400 PetscErrorCode ierr; 401 Vec Z,Z_c; 402 403 PetscFunctionBegin; 404 ierr = TSEIMEXGetVecs(ts,fine,&Z,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 405 ierr = TSEIMEXGetVecs(ts,coarse,&Z_c,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 406 ierr = MatRestrict(restrct,Z,Z_c);CHKERRQ(ierr); 407 ierr = VecPointwiseMult(Z_c,rscale,Z_c);CHKERRQ(ierr); 408 ierr = TSEIMEXRestoreVecs(ts,fine,&Z,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 409 ierr = TSEIMEXRestoreVecs(ts,coarse,&Z_c,PETSC_NULL,PETSC_NULL,PETSC_NULL);CHKERRQ(ierr); 410 PetscFunctionReturn(0); 411 } 412 413 414 #undef __FUNCT__ 415 #define __FUNCT__ "TSSetUp_EIMEX" 416 static PetscErrorCode TSSetUp_EIMEX(TS ts) 417 { 418 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 419 PetscErrorCode ierr; 420 DM dm; 421 PetscFunctionBegin; 422 423 if (!ext->N){ /*ext->max_rows not set*/ 424 ierr = TSEIMEXSetMaxRows(ts,TSEIMEXDefault);CHKERRQ(ierr); 425 } 426 if(-1 == ext->row_ind && -1 == ext->col_ind){ 427 ierr = TSEIMEXSetRowCol(ts,ext->max_rows,ext->max_rows);CHKERRQ(ierr); 428 }else{/*ext->row_ind and col_ind already set*/ 429 if(ext->ord_adapt){ 430 ierr = PetscInfo(ts,"Order adaptivity is enabled and TSEIMEXSetRowCol or -ts_eimex_row_col option will take no effect\n");CHKERRQ(ierr); 431 } 432 } 433 434 if(ext->ord_adapt){ 435 ext->nstages = 2; /*Start with the 2-stage scheme*/ 436 ierr = TSEIMEXSetRowCol(ts,ext->nstages,ext->nstages);CHKERRQ(ierr); 437 }else{ 438 ext->nstages = ext->max_rows; /*by default nstages is the same as max_rows, this can be changed by setting order adaptivity */ 439 } 440 441 ierr = VecDuplicateVecs(ts->vec_sol,(1+ext->nstages)*ext->nstages/2,&ext->T);CHKERRQ(ierr);/*full T table*/ 442 ierr = VecDuplicate(ts->vec_sol,&ext->YdotI);CHKERRQ(ierr); 443 ierr = VecDuplicate(ts->vec_sol,&ext->YdotRHS);CHKERRQ(ierr); 444 ierr = VecDuplicate(ts->vec_sol,&ext->Ydot);CHKERRQ(ierr); 445 ierr = VecDuplicate(ts->vec_sol,&ext->VecSolPrev);CHKERRQ(ierr); 446 ierr = VecDuplicate(ts->vec_sol,&ext->Y);CHKERRQ(ierr); 447 ierr = VecDuplicate(ts->vec_sol,&ext->Z);CHKERRQ(ierr); 448 ierr = TSGetDM(ts,&dm);CHKERRQ(ierr); 449 if (dm) { 450 ierr = DMCoarsenHookAdd(dm,DMCoarsenHook_TSEIMEX,DMRestrictHook_TSEIMEX,ts);CHKERRQ(ierr); 451 } 452 PetscFunctionReturn(0); 453 } 454 455 #undef __FUNCT__ 456 #define __FUNCT__ "TSSetFromOptions_EIMEX" 457 static PetscErrorCode TSSetFromOptions_EIMEX(TS ts) 458 { 459 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 460 PetscErrorCode ierr; 461 PetscInt tindex[2]={TSEIMEXDefault,TSEIMEXDefault}; 462 PetscInt np = 2, nrows=TSEIMEXDefault; 463 PetscFunctionBegin; 464 ierr = PetscOptionsHead("EIMEX ODE solver options");CHKERRQ(ierr); 465 { 466 PetscBool flg; 467 flg = PETSC_FALSE; 468 ierr = PetscOptionsInt("-ts_eimex_max_rows","Define the maximum number of rows used","TSEIMEXSetMaxRows",nrows,&nrows,&flg);CHKERRQ(ierr); /*default value 3*/ 469 if(flg){ 470 ierr = TSEIMEXSetMaxRows(ts,nrows);CHKERRQ(ierr); 471 } 472 ierr = PetscOptionsIntArray("-ts_eimex_row_col","Return the specific term in the T table","TSEIMEXSetRowCol",tindex,&np,&flg);CHKERRQ(ierr); 473 if(flg){ 474 ierr = TSEIMEXSetRowCol(ts,tindex[0],tindex[1]);CHKERRQ(ierr); 475 } 476 ierr = PetscOptionsBool("-ts_eimex_order_adapt","Solve the problem with adaptive order","TSEIMEXSetOrdAdapt",ext->ord_adapt,&ext->ord_adapt,PETSC_NULL);CHKERRQ(ierr); 477 ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr); 478 479 /*prevent users from changing SNESType*/ 480 } 481 ierr = PetscOptionsTail();CHKERRQ(ierr); 482 PetscFunctionReturn(0); 483 } 484 485 #undef __FUNCT__ 486 #define __FUNCT__ "TSView_EIMEX" 487 static PetscErrorCode TSView_EIMEX(TS ts,PetscViewer viewer) 488 { 489 /* TS_EIMEX *ext = (TS_EIMEX*)ts->data;*/ 490 PetscBool iascii; 491 PetscErrorCode ierr; 492 493 PetscFunctionBegin; 494 ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr); 495 if (iascii) { 496 ierr = PetscViewerASCIIPrintf(viewer," EIMEX\n");CHKERRQ(ierr); 497 } 498 ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr); 499 PetscFunctionReturn(0); 500 } 501 502 503 #undef __FUNCT__ 504 #define __FUNCT__ "TSEIMEXSetMaxRows" 505 /*@C 506 TSEIMEXSetMaxRows - Set the maximum number of rows for EIMEX schemes 507 508 Logically collective 509 510 Input Parameter: 511 + ts - timestepping context 512 - nrows - maximum number of rows 513 514 Level: intermediate 515 516 .seealso: TSEIMEXSetRowCol(), TSEIMEXSetOrdAdapt(), TSEIMEX 517 @*/ 518 PetscErrorCode TSEIMEXSetMaxRows(TS ts, PetscInt nrows) 519 { 520 PetscErrorCode ierr; 521 PetscFunctionBegin; 522 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 523 ierr = PetscTryMethod(ts,"TSEIMEXSetMaxRows_C",(TS,PetscInt),(ts,nrows));CHKERRQ(ierr); 524 PetscFunctionReturn(0); 525 } 526 527 528 #undef __FUNCT__ 529 #define __FUNCT__ "TSEIMEXSetRowCol" 530 /*@C 531 TSEIMEXSetRowCol - Set the type index in the T table for the return value 532 533 Logically collective 534 535 Input Parameter: 536 + ts - timestepping context 537 - tindex - index in the T table 538 539 Level: intermediate 540 541 .seealso: TSEIMEXSetMaxRows(), TSEIMEXSetOrdAdapt(), TSEIMEX 542 @*/ 543 PetscErrorCode TSEIMEXSetRowCol(TS ts, PetscInt row, PetscInt col) 544 { 545 PetscErrorCode ierr; 546 PetscFunctionBegin; 547 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 548 ierr = PetscTryMethod(ts,"TSEIMEXSetRowCol_C",(TS,PetscInt, PetscInt),(ts,row,col));CHKERRQ(ierr); 549 PetscFunctionReturn(0); 550 } 551 552 553 #undef __FUNCT__ 554 #define __FUNCT__ "TSEIMEXSetOrdAdapt" 555 /*@C 556 TSEIMEXSetOrdAdapt - Set the order adaptativity 557 558 Logically collective 559 560 Input Parameter: 561 + ts - timestepping context 562 - tindex - index in the T table 563 564 Level: intermediate 565 566 .seealso: TSEIMEXSetRowCol(), TSEIMEXSetOrdAdapt(), TSEIMEX 567 @*/ 568 PetscErrorCode TSEIMEXSetOrdAdapt(TS ts, PetscBool flg) 569 { 570 PetscErrorCode ierr; 571 PetscFunctionBegin; 572 PetscValidHeaderSpecific(ts,TS_CLASSID,1); 573 ierr = PetscTryMethod(ts,"TSEIMEXSetOrdAdapt_C",(TS,PetscBool),(ts,flg));CHKERRQ(ierr); 574 PetscFunctionReturn(0); 575 } 576 577 578 EXTERN_C_BEGIN 579 #undef __FUNCT__ 580 #define __FUNCT__ "TSEIMEXSetMaxRows_EIMEX" 581 PetscErrorCode TSEIMEXSetMaxRows_EIMEX(TS ts,PetscInt nrows) 582 { 583 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 584 PetscErrorCode ierr; 585 PetscInt i; 586 587 PetscFunctionBegin; 588 if (nrows < 0 || nrows > 100) SETERRQ1(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"Max number of rows (current value %D) should be an integer number between 1 and 100\n",nrows); 589 if(ext->N){ 590 ierr = PetscFree(ext->N);CHKERRQ(ierr); 591 } 592 ext->max_rows = nrows; 593 ierr = PetscMalloc(nrows*sizeof(PetscInt),&ext->N);CHKERRQ(ierr); 594 for(i=0;i<nrows;i++) ext->N[i]=i+1; 595 PetscFunctionReturn(0); 596 } 597 598 #undef __FUNCT__ 599 #define __FUNCT__ "TSEIMEXSetRowCol_EIMEX" 600 PetscErrorCode TSEIMEXSetRowCol_EIMEX(TS ts,PetscInt row,PetscInt col) 601 { 602 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 603 604 PetscFunctionBegin; 605 if (row < 1 || col < 1) SETERRQ2(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The row or column index (current value %d,%d) should not be less than 1 \n",row,col); 606 if (row > ext->max_rows || col > ext->max_rows) SETERRQ3(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The row or column index (current value %d,%d) exceeds the maximum number of rows %d\n",row,col,ext->max_rows); 607 if (col > row) SETERRQ2(((PetscObject)ts)->comm,PETSC_ERR_ARG_OUTOFRANGE,"The column index (%d) exceeds the row index (%d)\n",col,row); 608 609 ext->row_ind = row - 1; 610 ext->col_ind = col - 1; /*Array index in C starts from 0*/ 611 PetscFunctionReturn(0); 612 } 613 614 #undef __FUNCT__ 615 #define __FUNCT__ "TSEIMEXSetOrdAdapt_EIMEX" 616 PetscErrorCode TSEIMEXSetOrdAdapt_EIMEX(TS ts,PetscBool flg) 617 { 618 TS_EIMEX *ext = (TS_EIMEX*)ts->data; 619 PetscFunctionBegin; 620 ext->ord_adapt = flg; 621 PetscFunctionReturn(0); 622 } 623 EXTERN_C_END 624 625 /* ------------------------------------------------------------ */ 626 /*MC 627 TSEIMEX - ODE solver using extrapolated IMEX schemes 628 These methods are intended for problems with well-separated time scales, especially when a slow scale is strongly nonlinear such that it is expensive to solve with a fully implicit method. The user should provide the stiff part of the equation using TSSetIFunction() and the non-stiff part with TSSetRHSFunction(). 629 630 Notes: 631 The default is a 3-stage scheme, it can be changed with TSEIMEXSetMaxRows() or -ts_eimex_max_rows 632 633 This method currently only works with ODE, for which the stiff part G(t,X,Xdot) has the form Xdot + Ghat(t,X). 634 635 Level: beginner 636 637 .seealso: TSCreate(), TS 638 M*/ 639 EXTERN_C_BEGIN 640 #undef __FUNCT__ 641 #define __FUNCT__ "TSCreate_EIMEX" 642 PetscErrorCode TSCreate_EIMEX(TS ts) 643 { 644 TS_EIMEX *ext; 645 PetscErrorCode ierr; 646 647 PetscFunctionBegin; 648 649 ts->ops->reset = TSReset_EIMEX; 650 ts->ops->destroy = TSDestroy_EIMEX; 651 ts->ops->view = TSView_EIMEX; 652 ts->ops->setup = TSSetUp_EIMEX; 653 ts->ops->step = TSStep_EIMEX; 654 ts->ops->interpolate = TSInterpolate_EIMEX; 655 ts->ops->evaluatestep = TSEvaluateStep_EIMEX; 656 ts->ops->setfromoptions = TSSetFromOptions_EIMEX; 657 ts->ops->snesfunction = SNESTSFormFunction_EIMEX; 658 ts->ops->snesjacobian = SNESTSFormJacobian_EIMEX; 659 660 ierr = PetscNewLog(ts,TS_EIMEX,&ext);CHKERRQ(ierr); 661 ts->data = (void*)ext; 662 663 ext->ord_adapt = PETSC_FALSE; /*By default, no order adapativity*/ 664 ext->row_ind = -1; 665 ext->col_ind = -1; 666 ext->max_rows = TSEIMEXDefault; 667 ext->nstages = TSEIMEXDefault; 668 669 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetMaxRows_C","TSEIMEXSetMaxRows_EIMEX",TSEIMEXSetMaxRows_EIMEX);CHKERRQ(ierr); 670 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetRowCol_C","TSEIMEXSetRowCol_EIMEX",TSEIMEXSetRowCol_EIMEX);CHKERRQ(ierr); 671 ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSEIMEXSetOrdAdapt_C","TSEIMEXSetOrdAdapt_EIMEX",TSEIMEXSetOrdAdapt_EIMEX);CHKERRQ(ierr); 672 PetscFunctionReturn(0); 673 } 674 EXTERN_C_END 675 676