1316643e7SJed Brown #define PETSCTS_DLL 2316643e7SJed Brown 3316643e7SJed Brown /* 4316643e7SJed Brown Code for timestepping with implicit Theta method 5316643e7SJed Brown 6316643e7SJed Brown Notes: 7316643e7SJed Brown This method can be applied to DAE. 8316643e7SJed Brown 9316643e7SJed Brown This method is cast as a 1-stage implicit Runge-Kutta method. 10316643e7SJed Brown 11316643e7SJed Brown Theta | Theta 12316643e7SJed Brown ------------- 13316643e7SJed Brown | 1 14316643e7SJed Brown 15316643e7SJed Brown To apply a diagonally implicit RK method to DAE, the stage formula 16316643e7SJed Brown 17316643e7SJed Brown X_i = x + h sum_j a_ij X'_j 18316643e7SJed Brown 19316643e7SJed Brown is interpreted as a formula for X'_i in terms of X_i and known stuff (X'_j, j<i) 20316643e7SJed Brown */ 21316643e7SJed Brown #include "private/tsimpl.h" /*I "petscts.h" I*/ 22316643e7SJed Brown 23316643e7SJed Brown typedef struct { 24316643e7SJed Brown Vec X,Xdot; /* Storage for one stage */ 25316643e7SJed Brown Vec res; /* DAE residuals */ 26ace68cafSJed Brown PetscTruth extrapolate; 27316643e7SJed Brown PetscReal Theta; 28316643e7SJed Brown PetscReal shift; 29316643e7SJed Brown PetscReal stage_time; 30316643e7SJed Brown } TS_Theta; 31316643e7SJed Brown 32316643e7SJed Brown #undef __FUNCT__ 33316643e7SJed Brown #define __FUNCT__ "TSStep_Theta" 34316643e7SJed Brown static PetscErrorCode TSStep_Theta(TS ts,PetscInt *steps,PetscReal *ptime) 35316643e7SJed Brown { 36316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 372b5a38e1SLisandro Dalcin PetscInt i,max_steps = ts->max_steps,its,lits; 382b5a38e1SLisandro Dalcin PetscErrorCode ierr; 39316643e7SJed Brown 40316643e7SJed Brown PetscFunctionBegin; 41316643e7SJed Brown *steps = -ts->steps; 422b5a38e1SLisandro Dalcin *ptime = ts->ptime; 432b5a38e1SLisandro Dalcin 442b5a38e1SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 45316643e7SJed Brown 46316643e7SJed Brown for (i=0; i<max_steps; i++) { 47316643e7SJed Brown if (ts->ptime + ts->time_step > ts->max_time) break; 483f2090d5SJed Brown ierr = TSPreStep(ts);CHKERRQ(ierr); 492b5a38e1SLisandro Dalcin 50316643e7SJed Brown th->stage_time = ts->ptime + th->Theta*ts->time_step; 51316643e7SJed Brown th->shift = 1./(th->Theta*ts->time_step); 52316643e7SJed Brown 53ace68cafSJed Brown if (th->extrapolate) { 542b5a38e1SLisandro Dalcin ierr = VecWAXPY(th->X,1./th->shift,th->Xdot,ts->vec_sol);CHKERRQ(ierr); 55ace68cafSJed Brown } else { 562b5a38e1SLisandro Dalcin ierr = VecCopy(ts->vec_sol,th->X);CHKERRQ(ierr); 57ace68cafSJed Brown } 58316643e7SJed Brown ierr = SNESSolve(ts->snes,PETSC_NULL,th->X);CHKERRQ(ierr); 59316643e7SJed Brown ierr = SNESGetIterationNumber(ts->snes,&its);CHKERRQ(ierr); 60316643e7SJed Brown ierr = SNESGetLinearSolveIterations(ts->snes,&lits);CHKERRQ(ierr); 61316643e7SJed Brown ts->nonlinear_its += its; ts->linear_its += lits; 622b5a38e1SLisandro Dalcin 632b5a38e1SLisandro Dalcin ierr = VecAXPBYPCZ(th->Xdot,-th->shift,th->shift,0,ts->vec_sol,th->X);CHKERRQ(ierr); 642b5a38e1SLisandro Dalcin ierr = VecAXPY(ts->vec_sol,ts->time_step,th->Xdot);CHKERRQ(ierr); 652b5a38e1SLisandro Dalcin ts->ptime += ts->time_step; 66316643e7SJed Brown ts->steps++; 672b5a38e1SLisandro Dalcin 683f2090d5SJed Brown ierr = TSPostStep(ts);CHKERRQ(ierr); 692b5a38e1SLisandro Dalcin ierr = TSMonitor(ts,ts->steps,ts->ptime,ts->vec_sol);CHKERRQ(ierr); 70316643e7SJed Brown } 71316643e7SJed Brown 72316643e7SJed Brown *steps += ts->steps; 73316643e7SJed Brown *ptime = ts->ptime; 74316643e7SJed Brown PetscFunctionReturn(0); 75316643e7SJed Brown } 76316643e7SJed Brown 77316643e7SJed Brown /*------------------------------------------------------------*/ 78316643e7SJed Brown #undef __FUNCT__ 79316643e7SJed Brown #define __FUNCT__ "TSDestroy_Theta" 80316643e7SJed Brown static PetscErrorCode TSDestroy_Theta(TS ts) 81316643e7SJed Brown { 82316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 83316643e7SJed Brown PetscErrorCode ierr; 84316643e7SJed Brown 85316643e7SJed Brown PetscFunctionBegin; 86a5cbb462SJed Brown if (th->X) {ierr = VecDestroy(th->X);CHKERRQ(ierr);} 87a5cbb462SJed Brown if (th->Xdot) {ierr = VecDestroy(th->Xdot);CHKERRQ(ierr);} 88a5cbb462SJed Brown if (th->res) {ierr = VecDestroy(th->res);CHKERRQ(ierr);} 89316643e7SJed Brown ierr = PetscFree(th);CHKERRQ(ierr); 90316643e7SJed Brown PetscFunctionReturn(0); 91316643e7SJed Brown } 92316643e7SJed Brown 93316643e7SJed Brown /* 94316643e7SJed Brown This defines the nonlinear equation that is to be solved with SNES 952b5a38e1SLisandro Dalcin G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0 96316643e7SJed Brown */ 97316643e7SJed Brown #undef __FUNCT__ 98316643e7SJed Brown #define __FUNCT__ "TSThetaFunction" 99316643e7SJed Brown static PetscErrorCode TSThetaFunction(SNES snes,Vec x,Vec y,void *ctx) 100316643e7SJed Brown { 101316643e7SJed Brown TS ts = (TS)ctx; 102316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 103316643e7SJed Brown PetscErrorCode ierr; 104316643e7SJed Brown 105316643e7SJed Brown PetscFunctionBegin; 1062b5a38e1SLisandro Dalcin ierr = VecAXPBYPCZ(th->Xdot,-th->shift,th->shift,0,ts->vec_sol,x);CHKERRQ(ierr); 107316643e7SJed Brown ierr = TSComputeIFunction(ts,th->stage_time,x,th->Xdot,y);CHKERRQ(ierr); 108316643e7SJed Brown PetscFunctionReturn(0); 109316643e7SJed Brown } 110316643e7SJed Brown 111316643e7SJed Brown #undef __FUNCT__ 112316643e7SJed Brown #define __FUNCT__ "TSThetaJacobian" 113316643e7SJed Brown static PetscErrorCode TSThetaJacobian(SNES snes,Vec x,Mat *A,Mat *B,MatStructure *str,void *ctx) 114316643e7SJed Brown { 115316643e7SJed Brown TS ts = (TS)ctx; 116316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 117316643e7SJed Brown PetscErrorCode ierr; 118316643e7SJed Brown 119316643e7SJed Brown PetscFunctionBegin; 120a5ffc9a2SJed Brown /* th->Xdot has already been computed in TSThetaFunction (SNES guarantees this) */ 121316643e7SJed Brown ierr = TSComputeIJacobian(ts,th->stage_time,x,th->Xdot,th->shift,A,B,str);CHKERRQ(ierr); 122316643e7SJed Brown PetscFunctionReturn(0); 123316643e7SJed Brown } 124316643e7SJed Brown 125316643e7SJed Brown 126316643e7SJed Brown #undef __FUNCT__ 127316643e7SJed Brown #define __FUNCT__ "TSSetUp_Theta" 128316643e7SJed Brown static PetscErrorCode TSSetUp_Theta(TS ts) 129316643e7SJed Brown { 130316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 131316643e7SJed Brown PetscErrorCode ierr; 132316643e7SJed Brown 133316643e7SJed Brown PetscFunctionBegin; 1342b5a38e1SLisandro Dalcin if (ts->problem_type == TS_LINEAR) { 1352b5a38e1SLisandro Dalcin SETERRQ(PETSC_ERR_ARG_WRONG,"Only for nonlinear problems"); 1362b5a38e1SLisandro Dalcin } 137316643e7SJed Brown ierr = VecDuplicate(ts->vec_sol,&th->X);CHKERRQ(ierr); 138316643e7SJed Brown ierr = VecDuplicate(ts->vec_sol,&th->Xdot);CHKERRQ(ierr); 139316643e7SJed Brown ierr = VecDuplicate(ts->vec_sol,&th->res);CHKERRQ(ierr); 140316643e7SJed Brown ierr = SNESSetFunction(ts->snes,th->res,TSThetaFunction,ts);CHKERRQ(ierr); 1417c0b301bSJed Brown /* This is nasty. SNESSetFromOptions() is usually called in TSSetFromOptions(). With -snes_mf_operator, it will 1427c0b301bSJed Brown 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 1437c0b301bSJed Brown context. Note that SNESSetFunction() normally has not been called before SNESSetFromOptions(), so when -snes_mf sets 1447c0b301bSJed Brown the Jacobian user context to snes->funP, it will actually be NULL. This is not a problem because both snes->funP and 1457c0b301bSJed Brown snes->jacP should be the TS. */ 1467c0b301bSJed Brown { 1477c0b301bSJed Brown Mat A,B; 1487c0b301bSJed Brown PetscErrorCode (*func)(SNES,Vec,Mat*,Mat*,MatStructure*,void*); 1497c0b301bSJed Brown void *ctx; 1507c0b301bSJed Brown ierr = SNESGetJacobian(ts->snes,&A,&B,&func,&ctx);CHKERRQ(ierr); 1517c0b301bSJed Brown ierr = SNESSetJacobian(ts->snes,A?A:ts->A,B?B:ts->B,func?func:&TSThetaJacobian,ctx?ctx:ts);CHKERRQ(ierr); 1527c0b301bSJed Brown } 153316643e7SJed Brown PetscFunctionReturn(0); 154316643e7SJed Brown } 155316643e7SJed Brown /*------------------------------------------------------------*/ 156316643e7SJed Brown 157316643e7SJed Brown #undef __FUNCT__ 158316643e7SJed Brown #define __FUNCT__ "TSSetFromOptions_Theta" 159316643e7SJed Brown static PetscErrorCode TSSetFromOptions_Theta(TS ts) 160316643e7SJed Brown { 161316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 162316643e7SJed Brown PetscErrorCode ierr; 163316643e7SJed Brown 164316643e7SJed Brown PetscFunctionBegin; 165d73342a9SJed Brown ierr = PetscOptionsHead("Theta ODE solver options");CHKERRQ(ierr); 166316643e7SJed Brown { 167316643e7SJed Brown ierr = PetscOptionsReal("-ts_theta_theta","Location of stage (0<Theta<=1)","TSThetaSetTheta",th->Theta,&th->Theta,PETSC_NULL);CHKERRQ(ierr); 168ace68cafSJed Brown ierr = PetscOptionsTruth("-ts_theta_extrapolate","Extrapolate stage solution from previous solution (sometimes unstable)","TSThetaSetExtrapolate",th->extrapolate,&th->extrapolate,PETSC_NULL);CHKERRQ(ierr); 169316643e7SJed Brown } 170316643e7SJed Brown ierr = PetscOptionsTail();CHKERRQ(ierr); 171316643e7SJed Brown PetscFunctionReturn(0); 172316643e7SJed Brown } 173316643e7SJed Brown 174316643e7SJed Brown #undef __FUNCT__ 175316643e7SJed Brown #define __FUNCT__ "TSView_Theta" 176316643e7SJed Brown static PetscErrorCode TSView_Theta(TS ts,PetscViewer viewer) 177316643e7SJed Brown { 178316643e7SJed Brown TS_Theta *th = (TS_Theta*)ts->data; 179316643e7SJed Brown PetscTruth iascii; 180316643e7SJed Brown PetscErrorCode ierr; 181316643e7SJed Brown 182316643e7SJed Brown PetscFunctionBegin; 183316643e7SJed Brown ierr = PetscTypeCompare((PetscObject)viewer,PETSC_VIEWER_ASCII,&iascii);CHKERRQ(ierr); 184316643e7SJed Brown if (iascii) { 185316643e7SJed Brown ierr = PetscViewerASCIIPrintf(viewer," Theta=%G\n",th->Theta);CHKERRQ(ierr); 186ace68cafSJed Brown ierr = PetscViewerASCIIPrintf(viewer," Extrapolation=%s\n",th->extrapolate?"yes":"no");CHKERRQ(ierr); 187316643e7SJed Brown } else { 188316643e7SJed Brown SETERRQ1(PETSC_ERR_SUP,"Viewer type %s not supported for TS_Theta",((PetscObject)viewer)->type_name); 189316643e7SJed Brown } 190316643e7SJed Brown PetscFunctionReturn(0); 191316643e7SJed Brown } 192316643e7SJed Brown 193*0de4c49aSJed Brown EXTERN_C_BEGIN 194*0de4c49aSJed Brown #undef __FUNCT__ 195*0de4c49aSJed Brown #define __FUNCT__ "TSThetaGetTheta_Theta" 196*0de4c49aSJed Brown PetscErrorCode PETSCTS_DLLEXPORT TSThetaGetTheta_Theta(TS ts,PetscReal *theta) 197*0de4c49aSJed Brown { 198*0de4c49aSJed Brown TS_Theta *th = (TS_Theta*)ts->data; 199*0de4c49aSJed Brown 200*0de4c49aSJed Brown PetscFunctionBegin; 201*0de4c49aSJed Brown *theta = th->Theta; 202*0de4c49aSJed Brown PetscFunctionReturn(0); 203*0de4c49aSJed Brown } 204*0de4c49aSJed Brown 205*0de4c49aSJed Brown #undef __FUNCT__ 206*0de4c49aSJed Brown #define __FUNCT__ "TSThetaSetTheta_Theta" 207*0de4c49aSJed Brown PetscErrorCode PETSCTS_DLLEXPORT TSThetaSetTheta_Theta(TS ts,PetscReal theta) 208*0de4c49aSJed Brown { 209*0de4c49aSJed Brown TS_Theta *th = (TS_Theta*)ts->data; 210*0de4c49aSJed Brown 211*0de4c49aSJed Brown PetscFunctionBegin; 212*0de4c49aSJed Brown th->Theta = theta; 213*0de4c49aSJed Brown PetscFunctionReturn(0); 214*0de4c49aSJed Brown } 215*0de4c49aSJed Brown EXTERN_C_END 216*0de4c49aSJed Brown 217316643e7SJed Brown /* ------------------------------------------------------------ */ 218316643e7SJed Brown /*MC 21996f5712cSJed Brown TSTHETA - DAE solver using the implicit Theta method 220316643e7SJed Brown 221316643e7SJed Brown Level: beginner 222316643e7SJed Brown 223316643e7SJed Brown .seealso: TSCreate(), TS, TSSetType() 224316643e7SJed Brown 225316643e7SJed Brown M*/ 226316643e7SJed Brown EXTERN_C_BEGIN 227316643e7SJed Brown #undef __FUNCT__ 228316643e7SJed Brown #define __FUNCT__ "TSCreate_Theta" 229316643e7SJed Brown PetscErrorCode PETSCTS_DLLEXPORT TSCreate_Theta(TS ts) 230316643e7SJed Brown { 231316643e7SJed Brown TS_Theta *th; 232316643e7SJed Brown PetscErrorCode ierr; 233316643e7SJed Brown 234316643e7SJed Brown PetscFunctionBegin; 235316643e7SJed Brown ts->ops->destroy = TSDestroy_Theta; 236316643e7SJed Brown ts->ops->view = TSView_Theta; 237316643e7SJed Brown ts->ops->setup = TSSetUp_Theta; 238316643e7SJed Brown ts->ops->step = TSStep_Theta; 239316643e7SJed Brown ts->ops->setfromoptions = TSSetFromOptions_Theta; 240316643e7SJed Brown 2412b5a38e1SLisandro Dalcin ts->problem_type = TS_NONLINEAR; 242316643e7SJed Brown ierr = SNESCreate(((PetscObject)ts)->comm,&ts->snes);CHKERRQ(ierr); 243316643e7SJed Brown ierr = PetscObjectIncrementTabLevel((PetscObject)ts->snes,(PetscObject)ts,1);CHKERRQ(ierr); 244316643e7SJed Brown 245316643e7SJed Brown ierr = PetscNewLog(ts,TS_Theta,&th);CHKERRQ(ierr); 246316643e7SJed Brown ts->data = (void*)th; 247316643e7SJed Brown 248ace68cafSJed Brown th->extrapolate = PETSC_TRUE; 249316643e7SJed Brown th->Theta = 0.5; 250316643e7SJed Brown 251*0de4c49aSJed Brown ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSThetaGetTheta_C","TSThetaGetTheta_Theta",TSThetaGetTheta_Theta);CHKERRQ(ierr); 252*0de4c49aSJed Brown ierr = PetscObjectComposeFunctionDynamic((PetscObject)ts,"TSThetaSetTheta_C","TSThetaSetTheta_Theta",TSThetaSetTheta_Theta);CHKERRQ(ierr); 253316643e7SJed Brown PetscFunctionReturn(0); 254316643e7SJed Brown } 255316643e7SJed Brown EXTERN_C_END 256*0de4c49aSJed Brown 257*0de4c49aSJed Brown #undef __FUNCT__ 258*0de4c49aSJed Brown #define __FUNCT__ "TSThetaGetTheta" 259*0de4c49aSJed Brown /*@ 260*0de4c49aSJed Brown TSThetaGetTheta - Get the abscissa of the stage in (0,1]. 261*0de4c49aSJed Brown 262*0de4c49aSJed Brown Not Collective 263*0de4c49aSJed Brown 264*0de4c49aSJed Brown Input Parameter: 265*0de4c49aSJed Brown . ts - timestepping context 266*0de4c49aSJed Brown 267*0de4c49aSJed Brown Output Parameter: 268*0de4c49aSJed Brown . theta - stage abscissa 269*0de4c49aSJed Brown 270*0de4c49aSJed Brown Note: 271*0de4c49aSJed Brown Use of this function is normally only required to hack TSTHETA to use a modified integration scheme. 272*0de4c49aSJed Brown 273*0de4c49aSJed Brown Level: Advanced 274*0de4c49aSJed Brown 275*0de4c49aSJed Brown .seealso: TSThetaSetTheta() 276*0de4c49aSJed Brown @*/ 277*0de4c49aSJed Brown PetscErrorCode PETSCTS_DLLEXPORT TSThetaGetTheta(TS ts,PetscReal *theta) 278*0de4c49aSJed Brown { 279*0de4c49aSJed Brown PetscErrorCode ierr,(*f)(TS,PetscReal*); 280*0de4c49aSJed Brown 281*0de4c49aSJed Brown PetscFunctionBegin; 282*0de4c49aSJed Brown PetscValidHeaderSpecific(ts,TS_COOKIE,1); 283*0de4c49aSJed Brown PetscValidPointer(theta,2); 284*0de4c49aSJed Brown ierr = PetscObjectQueryFunction((PetscObject)ts,"TSThetaGetTheta_C",(void(**)(void))&f);CHKERRQ(ierr); 285*0de4c49aSJed Brown if (f) {ierr = (*f)(ts,theta);CHKERRQ(ierr);} 286*0de4c49aSJed Brown PetscFunctionReturn(0); 287*0de4c49aSJed Brown } 288*0de4c49aSJed Brown 289*0de4c49aSJed Brown #undef __FUNCT__ 290*0de4c49aSJed Brown #define __FUNCT__ "TSThetaSetTheta" 291*0de4c49aSJed Brown /*@ 292*0de4c49aSJed Brown TSThetaSetTheta - Set the abscissa of the stage in (0,1]. 293*0de4c49aSJed Brown 294*0de4c49aSJed Brown Not Collective 295*0de4c49aSJed Brown 296*0de4c49aSJed Brown Input Parameter: 297*0de4c49aSJed Brown + ts - timestepping context 298*0de4c49aSJed Brown - theta - stage abscissa 299*0de4c49aSJed Brown 300*0de4c49aSJed Brown Options Database: 301*0de4c49aSJed Brown . -ts_theta_theta <theta> 302*0de4c49aSJed Brown 303*0de4c49aSJed Brown Level: Intermediate 304*0de4c49aSJed Brown 305*0de4c49aSJed Brown .seealso: TSThetaGetTheta() 306*0de4c49aSJed Brown @*/ 307*0de4c49aSJed Brown PetscErrorCode PETSCTS_DLLEXPORT TSThetaSetTheta(TS ts,PetscReal theta) 308*0de4c49aSJed Brown { 309*0de4c49aSJed Brown PetscErrorCode ierr,(*f)(TS,PetscReal); 310*0de4c49aSJed Brown 311*0de4c49aSJed Brown PetscFunctionBegin; 312*0de4c49aSJed Brown PetscValidHeaderSpecific(ts,TS_COOKIE,1); 313*0de4c49aSJed Brown PetscValidPointer(theta,2); 314*0de4c49aSJed Brown ierr = PetscObjectQueryFunction((PetscObject)ts,"TSThetaSetTheta_C",(void(**)(void))&f);CHKERRQ(ierr); 315*0de4c49aSJed Brown if (!f) SETERRQ1(PETSC_ERR_SUP,"TS type %s",((PetscObject)ts)->type_name); 316*0de4c49aSJed Brown ierr = (*f)(ts,theta);CHKERRQ(ierr); 317*0de4c49aSJed Brown PetscFunctionReturn(0); 318*0de4c49aSJed Brown } 319