xref: /petsc/src/ts/impls/implicit/theta/theta.c (revision 224748a49db2c458f0a882a2cb5d83eb59c1eacb)
1 /*
2   Code for timestepping with implicit Theta method
3 */
4 #include <petsc/private/tsimpl.h>                /*I   "petscts.h"   I*/
5 #include <petscsnes.h>
6 #include <petscdm.h>
7 #include <petscmat.h>
8 
9 typedef struct {
10   Vec          X,Xdot;                   /* Storage for one stage */
11   Vec          X0;                       /* work vector to store X0 */
12   Vec          affine;                   /* Affine vector needed for residual at beginning of step */
13   Vec          *VecsDeltaLam;             /* Increment of the adjoint sensitivity w.r.t IC at stage*/
14   Vec          *VecsDeltaMu;              /* Increment of the adjoint sensitivity w.r.t P at stage*/
15   Vec          *VecsSensiTemp;            /* Vector to be timed with Jacobian transpose*/
16   PetscBool    extrapolate;
17   PetscBool    endpoint;
18   PetscReal    Theta;
19   PetscReal    stage_time;
20   TSStepStatus status;
21   char         *name;
22   PetscInt     order;
23   PetscReal    ccfl;               /* Placeholder for CFL coefficient relative to forward Euler */
24   PetscBool    adapt;  /* use time-step adaptivity ? */
25   PetscReal    ptime;
26 } TS_Theta;
27 
28 #undef __FUNCT__
29 #define __FUNCT__ "TSThetaGetX0AndXdot"
30 static PetscErrorCode TSThetaGetX0AndXdot(TS ts,DM dm,Vec *X0,Vec *Xdot)
31 {
32   TS_Theta       *th = (TS_Theta*)ts->data;
33   PetscErrorCode ierr;
34 
35   PetscFunctionBegin;
36   if (X0) {
37     if (dm && dm != ts->dm) {
38       ierr = DMGetNamedGlobalVector(dm,"TSTheta_X0",X0);CHKERRQ(ierr);
39     } else *X0 = ts->vec_sol;
40   }
41   if (Xdot) {
42     if (dm && dm != ts->dm) {
43       ierr = DMGetNamedGlobalVector(dm,"TSTheta_Xdot",Xdot);CHKERRQ(ierr);
44     } else *Xdot = th->Xdot;
45   }
46   PetscFunctionReturn(0);
47 }
48 
49 
50 #undef __FUNCT__
51 #define __FUNCT__ "TSThetaRestoreX0AndXdot"
52 static PetscErrorCode TSThetaRestoreX0AndXdot(TS ts,DM dm,Vec *X0,Vec *Xdot)
53 {
54   PetscErrorCode ierr;
55 
56   PetscFunctionBegin;
57   if (X0) {
58     if (dm && dm != ts->dm) {
59       ierr = DMRestoreNamedGlobalVector(dm,"TSTheta_X0",X0);CHKERRQ(ierr);
60     }
61   }
62   if (Xdot) {
63     if (dm && dm != ts->dm) {
64       ierr = DMRestoreNamedGlobalVector(dm,"TSTheta_Xdot",Xdot);CHKERRQ(ierr);
65     }
66   }
67   PetscFunctionReturn(0);
68 }
69 
70 #undef __FUNCT__
71 #define __FUNCT__ "DMCoarsenHook_TSTheta"
72 static PetscErrorCode DMCoarsenHook_TSTheta(DM fine,DM coarse,void *ctx)
73 {
74 
75   PetscFunctionBegin;
76   PetscFunctionReturn(0);
77 }
78 
79 #undef __FUNCT__
80 #define __FUNCT__ "DMRestrictHook_TSTheta"
81 static PetscErrorCode DMRestrictHook_TSTheta(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse,void *ctx)
82 {
83   TS             ts = (TS)ctx;
84   PetscErrorCode ierr;
85   Vec            X0,Xdot,X0_c,Xdot_c;
86 
87   PetscFunctionBegin;
88   ierr = TSThetaGetX0AndXdot(ts,fine,&X0,&Xdot);CHKERRQ(ierr);
89   ierr = TSThetaGetX0AndXdot(ts,coarse,&X0_c,&Xdot_c);CHKERRQ(ierr);
90   ierr = MatRestrict(restrct,X0,X0_c);CHKERRQ(ierr);
91   ierr = MatRestrict(restrct,Xdot,Xdot_c);CHKERRQ(ierr);
92   ierr = VecPointwiseMult(X0_c,rscale,X0_c);CHKERRQ(ierr);
93   ierr = VecPointwiseMult(Xdot_c,rscale,Xdot_c);CHKERRQ(ierr);
94   ierr = TSThetaRestoreX0AndXdot(ts,fine,&X0,&Xdot);CHKERRQ(ierr);
95   ierr = TSThetaRestoreX0AndXdot(ts,coarse,&X0_c,&Xdot_c);CHKERRQ(ierr);
96   PetscFunctionReturn(0);
97 }
98 
99 #undef __FUNCT__
100 #define __FUNCT__ "DMSubDomainHook_TSTheta"
101 static PetscErrorCode DMSubDomainHook_TSTheta(DM dm,DM subdm,void *ctx)
102 {
103 
104   PetscFunctionBegin;
105   PetscFunctionReturn(0);
106 }
107 
108 #undef __FUNCT__
109 #define __FUNCT__ "DMSubDomainRestrictHook_TSTheta"
110 static PetscErrorCode DMSubDomainRestrictHook_TSTheta(DM dm,VecScatter gscat,VecScatter lscat,DM subdm,void *ctx)
111 {
112   TS             ts = (TS)ctx;
113   PetscErrorCode ierr;
114   Vec            X0,Xdot,X0_sub,Xdot_sub;
115 
116   PetscFunctionBegin;
117   ierr = TSThetaGetX0AndXdot(ts,dm,&X0,&Xdot);CHKERRQ(ierr);
118   ierr = TSThetaGetX0AndXdot(ts,subdm,&X0_sub,&Xdot_sub);CHKERRQ(ierr);
119 
120   ierr = VecScatterBegin(gscat,X0,X0_sub,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
121   ierr = VecScatterEnd(gscat,X0,X0_sub,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
122 
123   ierr = VecScatterBegin(gscat,Xdot,Xdot_sub,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
124   ierr = VecScatterEnd(gscat,Xdot,Xdot_sub,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
125 
126   ierr = TSThetaRestoreX0AndXdot(ts,dm,&X0,&Xdot);CHKERRQ(ierr);
127   ierr = TSThetaRestoreX0AndXdot(ts,subdm,&X0_sub,&Xdot_sub);CHKERRQ(ierr);
128   PetscFunctionReturn(0);
129 }
130 
131 #undef __FUNCT__
132 #define __FUNCT__ "TSEvaluateStep_Theta"
133 static PetscErrorCode TSEvaluateStep_Theta(TS ts,PetscInt order,Vec U,PetscBool *done)
134 {
135   PetscErrorCode ierr;
136   TS_Theta       *th = (TS_Theta*)ts->data;
137 
138   PetscFunctionBegin;
139   if (order == 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"No time-step adaptivity implemented for 1st order theta method; Run with -ts_adapt_type none");
140   if (order == th->order) {
141     if (th->endpoint) {
142       ierr = VecCopy(th->X,U);CHKERRQ(ierr);
143     } else {
144       PetscReal shift = 1./(th->Theta*ts->time_step);
145       ierr = VecAXPBYPCZ(th->Xdot,-shift,shift,0,U,th->X);CHKERRQ(ierr);
146       ierr = VecAXPY(U,ts->time_step,th->Xdot);CHKERRQ(ierr);
147     }
148   } else if (order == th->order-1 && order) {
149     ierr = VecWAXPY(U,ts->time_step,th->Xdot,th->X0);CHKERRQ(ierr);
150   }
151   PetscFunctionReturn(0);
152 }
153 
154 #undef __FUNCT__
155 #define __FUNCT__ "TSRollBack_Theta"
156 static PetscErrorCode TSRollBack_Theta(TS ts)
157 {
158   TS_Theta       *th = (TS_Theta*)ts->data;
159   PetscErrorCode ierr;
160 
161   PetscFunctionBegin;
162   ierr = VecCopy(th->X0,ts->vec_sol);CHKERRQ(ierr);
163   th->status    = TS_STEP_INCOMPLETE;
164   PetscFunctionReturn(0);
165 }
166 
167 #undef __FUNCT__
168 #define __FUNCT__ "TSStep_Theta"
169 static PetscErrorCode TSStep_Theta(TS ts)
170 {
171   TS_Theta       *th = (TS_Theta*)ts->data;
172   PetscInt       its,lits,reject,next_scheme;
173   PetscReal      next_time_step;
174   TSAdapt        adapt;
175   PetscBool      stageok,accept = PETSC_TRUE;
176   PetscErrorCode ierr;
177 
178   PetscFunctionBegin;
179   th->status = TS_STEP_INCOMPLETE;
180   ierr = VecCopy(ts->vec_sol,th->X0);CHKERRQ(ierr);
181   for (reject=0; !ts->reason && th->status != TS_STEP_COMPLETE; ts->reject++) {
182     PetscReal shift = 1./(th->Theta*ts->time_step);
183     th->stage_time = ts->ptime + (th->endpoint ? 1. : th->Theta)*ts->time_step;
184     ierr = TSPreStep(ts);CHKERRQ(ierr);
185     ierr = TSPreStage(ts,th->stage_time);CHKERRQ(ierr);
186 
187     if (th->endpoint) {           /* This formulation assumes linear time-independent mass matrix */
188       ierr = VecZeroEntries(th->Xdot);CHKERRQ(ierr);
189       if (!th->affine) {ierr = VecDuplicate(ts->vec_sol,&th->affine);CHKERRQ(ierr);}
190       ierr = TSComputeIFunction(ts,ts->ptime,ts->vec_sol,th->Xdot,th->affine,PETSC_FALSE);CHKERRQ(ierr);
191       ierr = VecScale(th->affine,(th->Theta-1.)/th->Theta);CHKERRQ(ierr);
192     }
193     if (th->extrapolate) {
194       ierr = VecWAXPY(th->X,1./shift,th->Xdot,ts->vec_sol);CHKERRQ(ierr);
195     } else {
196       ierr = VecCopy(ts->vec_sol,th->X);CHKERRQ(ierr);
197     }
198     ierr = SNESSolve(ts->snes,th->affine,th->X);CHKERRQ(ierr);
199     ierr = SNESGetIterationNumber(ts->snes,&its);CHKERRQ(ierr);
200     ierr = SNESGetLinearSolveIterations(ts->snes,&lits);CHKERRQ(ierr);
201     ts->snes_its += its; ts->ksp_its += lits;
202     ierr = TSPostStage(ts,th->stage_time,0,&(th->X));CHKERRQ(ierr);
203     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
204     ierr = TSAdaptCheckStage(adapt,ts,&stageok);CHKERRQ(ierr);
205     if (!stageok) {accept = PETSC_FALSE; goto reject_step;}
206 
207     ierr = TSEvaluateStep(ts,th->order,ts->vec_sol,NULL);CHKERRQ(ierr);
208     th->status = TS_STEP_PENDING;
209     /* Register only the current method as a candidate because we're not supporting multiple candidates yet. */
210     ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
211     ierr = TSAdaptCandidatesClear(adapt);CHKERRQ(ierr);
212     ierr = TSAdaptCandidateAdd(adapt,NULL,th->order,1,th->ccfl,1.0,PETSC_TRUE);CHKERRQ(ierr);
213     ierr = TSAdaptChoose(adapt,ts,ts->time_step,&next_scheme,&next_time_step,&accept);CHKERRQ(ierr);
214     if (!accept) {           /* Roll back the current step */
215       ts->ptime += next_time_step; /* This will be undone in rollback */
216       th->status = TS_STEP_INCOMPLETE;
217       ierr = TSRollBack(ts);CHKERRQ(ierr);
218       goto reject_step;
219     }
220 
221     if (ts->vec_costintegral) {
222       /* Evolve ts->vec_costintegral to compute integrals */
223       if (th->endpoint) {
224         ierr = TSAdjointComputeCostIntegrand(ts,ts->ptime,th->X0,ts->vec_costintegrand);CHKERRQ(ierr);
225         ierr = VecAXPY(ts->vec_costintegral,ts->time_step*(1.-th->Theta),ts->vec_costintegrand);CHKERRQ(ierr);
226       }
227       ierr = TSAdjointComputeCostIntegrand(ts,th->stage_time,th->X,ts->vec_costintegrand);CHKERRQ(ierr);
228       if (th->endpoint) {
229         ierr = VecAXPY(ts->vec_costintegral,ts->time_step*th->Theta,ts->vec_costintegrand);CHKERRQ(ierr);
230       }else {
231         ierr = VecAXPY(ts->vec_costintegral,ts->time_step,ts->vec_costintegrand);CHKERRQ(ierr);
232       }
233     }
234 
235     /* ignore next_scheme for now */
236     ts->ptime    += ts->time_step;
237     ts->time_step = next_time_step;
238     ts->steps++;
239     th->status = TS_STEP_COMPLETE;
240     break;
241 
242 reject_step:
243     if (!ts->reason && ++reject > ts->max_reject && ts->max_reject >= 0) {
244       ts->reason = TS_DIVERGED_STEP_REJECTED;
245       ierr = PetscInfo2(ts,"Step=%D, step rejections %D greater than current TS allowed, stopping solve\n",ts->steps,reject);CHKERRQ(ierr);
246     }
247     continue;
248   }
249   PetscFunctionReturn(0);
250 }
251 
252 #undef __FUNCT__
253 #define __FUNCT__ "TSAdjointStep_Theta"
254 static PetscErrorCode TSAdjointStep_Theta(TS ts)
255 {
256   TS_Theta            *th = (TS_Theta*)ts->data;
257   Vec                 *VecsDeltaLam = th->VecsDeltaLam,*VecsDeltaMu = th->VecsDeltaMu,*VecsSensiTemp = th->VecsSensiTemp;
258   PetscInt            nadj;
259   PetscErrorCode      ierr;
260   Mat                 J,Jp;
261   KSP                 ksp;
262   PetscReal           shift;
263 
264   PetscFunctionBegin;
265 
266   th->status = TS_STEP_INCOMPLETE;
267   ierr = SNESGetKSP(ts->snes,&ksp);CHKERRQ(ierr);
268   ierr = TSGetIJacobian(ts,&J,&Jp,NULL,NULL);CHKERRQ(ierr);
269 
270   /* If endpoint=1, th->ptime and th->X0 will be used; if endpoint=0, th->stage_time and th->X will be used. */
271   th->stage_time = ts->ptime + (th->endpoint ? ts->time_step : (1.-th->Theta)*ts->time_step); /* time_step is negative*/
272   th->ptime      = ts->ptime + ts->time_step;
273 
274   ierr = TSPreStep(ts);CHKERRQ(ierr);
275 
276   /* Build RHS */
277   if (ts->vec_costintegral) { /* Cost function has an integral term */
278     if (th->endpoint) {
279       ierr = TSAdjointComputeDRDYFunction(ts,ts->ptime,ts->vec_sol,ts->vecs_drdy);CHKERRQ(ierr);
280     }else {
281       ierr = TSAdjointComputeDRDYFunction(ts,th->stage_time,th->X,ts->vecs_drdy);CHKERRQ(ierr);
282     }
283   }
284   for (nadj=0; nadj<ts->numcost; nadj++) {
285     ierr = VecCopy(ts->vecs_sensi[nadj],VecsSensiTemp[nadj]);CHKERRQ(ierr);
286     ierr = VecScale(VecsSensiTemp[nadj],-1./(th->Theta*ts->time_step));CHKERRQ(ierr);
287     if (ts->vec_costintegral) {
288       ierr = VecAXPY(VecsSensiTemp[nadj],1.,ts->vecs_drdy[nadj]);CHKERRQ(ierr);
289     }
290   }
291 
292   /* Build LHS */
293   shift = -1./(th->Theta*ts->time_step);
294   if (th->endpoint) {
295     ierr = TSComputeIJacobian(ts,ts->ptime,ts->vec_sol,th->Xdot,shift,J,Jp,PETSC_FALSE);CHKERRQ(ierr);
296   }else {
297     ierr = TSComputeIJacobian(ts,th->stage_time,th->X,th->Xdot,shift,J,Jp,PETSC_FALSE);CHKERRQ(ierr);
298   }
299   ierr = KSPSetOperators(ksp,J,Jp);CHKERRQ(ierr);
300 
301   /* Solve LHS X = RHS */
302   for (nadj=0; nadj<ts->numcost; nadj++) {
303     ierr = KSPSolveTranspose(ksp,VecsSensiTemp[nadj],VecsDeltaLam[nadj]);CHKERRQ(ierr);
304   }
305 
306   /* Update sensitivities, and evaluate integrals if there is any */
307   if(th->endpoint) { /* two-stage case */
308     if (th->Theta!=1.) {
309       shift = -1./((th->Theta-1.)*ts->time_step);
310       ierr  = TSComputeIJacobian(ts,th->ptime,th->X0,th->Xdot,shift,J,Jp,PETSC_FALSE);CHKERRQ(ierr);
311       if (ts->vec_costintegral) {
312         ierr = TSAdjointComputeDRDYFunction(ts,th->ptime,th->X0,ts->vecs_drdy);CHKERRQ(ierr);
313         if (!ts->costintegralfwd) {
314           /* Evolve ts->vec_costintegral to compute integrals */
315           ierr = TSAdjointComputeCostIntegrand(ts,ts->ptime,ts->vec_sol,ts->vec_costintegrand);CHKERRQ(ierr);
316           ierr = VecAXPY(ts->vec_costintegral,-ts->time_step*th->Theta,ts->vec_costintegrand);CHKERRQ(ierr);
317           ierr = TSAdjointComputeCostIntegrand(ts,th->ptime,th->X0,ts->vec_costintegrand);CHKERRQ(ierr);
318           ierr = VecAXPY(ts->vec_costintegral,ts->time_step*(th->Theta-1.),ts->vec_costintegrand);CHKERRQ(ierr);
319         }
320       }
321       for (nadj=0; nadj<ts->numcost; nadj++) {
322         ierr = MatMultTranspose(J,VecsDeltaLam[nadj],ts->vecs_sensi[nadj]);CHKERRQ(ierr);
323         if (ts->vec_costintegral) {
324           ierr = VecAXPY(ts->vecs_sensi[nadj],-1.,ts->vecs_drdy[nadj]);CHKERRQ(ierr);
325         }
326         ierr = VecScale(ts->vecs_sensi[nadj],1./shift);CHKERRQ(ierr);
327       }
328     }else { /* backward Euler */
329       shift = 0.0;
330       ierr  = TSComputeIJacobian(ts,ts->ptime,ts->vec_sol,th->Xdot,shift,J,Jp,PETSC_FALSE);CHKERRQ(ierr); /* get -f_y */
331       for (nadj=0; nadj<ts->numcost; nadj++) {
332         ierr = MatMultTranspose(J,VecsDeltaLam[nadj],VecsSensiTemp[nadj]);CHKERRQ(ierr);
333         ierr = VecAXPY(ts->vecs_sensi[nadj],ts->time_step,VecsSensiTemp[nadj]);CHKERRQ(ierr);
334         if (ts->vec_costintegral) {
335           ierr = VecAXPY(ts->vecs_sensi[nadj],-ts->time_step,ts->vecs_drdy[nadj]);CHKERRQ(ierr);
336           if (!ts->costintegralfwd) {
337           /* Evolve ts->vec_costintegral to compute integrals */
338             ierr = TSAdjointComputeCostIntegrand(ts,ts->ptime,ts->vec_sol,ts->vec_costintegrand);CHKERRQ(ierr);
339             ierr = VecAXPY(ts->vec_costintegral,-ts->time_step*th->Theta,ts->vec_costintegrand);CHKERRQ(ierr);
340           }
341         }
342       }
343     }
344 
345     if (ts->vecs_sensip) { /* sensitivities wrt parameters */
346       ierr = TSAdjointComputeRHSJacobian(ts,ts->ptime,ts->vec_sol,ts->Jacp);CHKERRQ(ierr);
347       for (nadj=0; nadj<ts->numcost; nadj++) {
348         ierr = MatMultTranspose(ts->Jacp,VecsDeltaLam[nadj],VecsDeltaMu[nadj]);CHKERRQ(ierr);
349         ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step*th->Theta,VecsDeltaMu[nadj]);CHKERRQ(ierr);
350       }
351       if (th->Theta!=1.) {
352         ierr = TSAdjointComputeRHSJacobian(ts,th->ptime,th->X0,ts->Jacp);CHKERRQ(ierr);
353         for (nadj=0; nadj<ts->numcost; nadj++) {
354           ierr = MatMultTranspose(ts->Jacp,VecsDeltaLam[nadj],VecsDeltaMu[nadj]);CHKERRQ(ierr);
355           ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step*(1.-th->Theta),VecsDeltaMu[nadj]);CHKERRQ(ierr);
356         }
357       }
358       if (ts->vec_costintegral) {
359         ierr = TSAdjointComputeDRDPFunction(ts,ts->ptime,ts->vec_sol,ts->vecs_drdp);CHKERRQ(ierr);
360         for (nadj=0; nadj<ts->numcost; nadj++) {
361           ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step*th->Theta,ts->vecs_drdp[nadj]);CHKERRQ(ierr);
362         }
363         if (th->Theta!=1.) {
364           ierr = TSAdjointComputeDRDPFunction(ts,th->ptime,th->X0,ts->vecs_drdp);CHKERRQ(ierr);
365           for (nadj=0; nadj<ts->numcost; nadj++) {
366             ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step*(1.-th->Theta),ts->vecs_drdp[nadj]);CHKERRQ(ierr);
367           }
368         }
369       }
370     }
371   }else { /* one-stage case */
372     shift = 0.0;
373     ierr  = TSComputeIJacobian(ts,th->stage_time,th->X,th->Xdot,shift,J,Jp,PETSC_FALSE);CHKERRQ(ierr); /* get -f_y */
374     if (ts->vec_costintegral) {
375       ierr = TSAdjointComputeDRDYFunction(ts,th->stage_time,th->X,ts->vecs_drdy);CHKERRQ(ierr);
376       if (!ts->costintegralfwd) {
377         /* Evolve ts->vec_costintegral to compute integrals */
378         ierr = TSAdjointComputeCostIntegrand(ts,th->stage_time,th->X,ts->vec_costintegrand);CHKERRQ(ierr);
379         ierr = VecAXPY(ts->vec_costintegral,-ts->time_step,ts->vec_costintegrand);CHKERRQ(ierr);
380       }
381     }
382     for (nadj=0; nadj<ts->numcost; nadj++) {
383       ierr = MatMultTranspose(J,VecsDeltaLam[nadj],VecsSensiTemp[nadj]);CHKERRQ(ierr);
384       ierr = VecAXPY(ts->vecs_sensi[nadj],ts->time_step,VecsSensiTemp[nadj]);CHKERRQ(ierr);
385       if (ts->vec_costintegral) {
386         ierr = VecAXPY(ts->vecs_sensi[nadj],-ts->time_step,ts->vecs_drdy[nadj]);CHKERRQ(ierr);
387       }
388     }
389     if (ts->vecs_sensip) {
390       ierr = TSAdjointComputeRHSJacobian(ts,th->stage_time,th->X,ts->Jacp);CHKERRQ(ierr);
391       for (nadj=0; nadj<ts->numcost; nadj++) {
392         ierr = MatMultTranspose(ts->Jacp,VecsDeltaLam[nadj],VecsDeltaMu[nadj]);CHKERRQ(ierr);
393         ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step,VecsDeltaMu[nadj]);CHKERRQ(ierr);
394       }
395       if (ts->vec_costintegral) {
396         ierr = TSAdjointComputeDRDPFunction(ts,th->stage_time,th->X,ts->vecs_drdp);CHKERRQ(ierr);
397         for (nadj=0; nadj<ts->numcost; nadj++) {
398           ierr = VecAXPY(ts->vecs_sensip[nadj],-ts->time_step,ts->vecs_drdp[nadj]);CHKERRQ(ierr);
399         }
400       }
401     }
402   }
403 
404   ts->ptime += ts->time_step;
405   ts->steps++;
406   th->status = TS_STEP_COMPLETE;
407   PetscFunctionReturn(0);
408 }
409 
410 #undef __FUNCT__
411 #define __FUNCT__ "TSInterpolate_Theta"
412 static PetscErrorCode TSInterpolate_Theta(TS ts,PetscReal t,Vec X)
413 {
414   TS_Theta       *th   = (TS_Theta*)ts->data;
415   PetscReal      alpha = t - ts->ptime;
416   PetscErrorCode ierr;
417 
418   PetscFunctionBegin;
419   ierr = VecCopy(ts->vec_sol,th->X);CHKERRQ(ierr);
420   if (th->endpoint) alpha *= th->Theta;
421   ierr = VecWAXPY(X,alpha,th->Xdot,th->X);CHKERRQ(ierr);
422   PetscFunctionReturn(0);
423 }
424 
425 /*------------------------------------------------------------*/
426 #undef __FUNCT__
427 #define __FUNCT__ "TSReset_Theta"
428 static PetscErrorCode TSReset_Theta(TS ts)
429 {
430   TS_Theta       *th = (TS_Theta*)ts->data;
431   PetscErrorCode ierr;
432 
433   PetscFunctionBegin;
434   ierr = VecDestroy(&th->X);CHKERRQ(ierr);
435   ierr = VecDestroy(&th->Xdot);CHKERRQ(ierr);
436   ierr = VecDestroy(&th->X0);CHKERRQ(ierr);
437   ierr = VecDestroy(&th->affine);CHKERRQ(ierr);
438   ierr = VecDestroyVecs(ts->numcost,&th->VecsDeltaLam);CHKERRQ(ierr);
439   ierr = VecDestroyVecs(ts->numcost,&th->VecsDeltaMu);CHKERRQ(ierr);
440   ierr = VecDestroyVecs(ts->numcost,&th->VecsSensiTemp);CHKERRQ(ierr);
441   PetscFunctionReturn(0);
442 }
443 
444 #undef __FUNCT__
445 #define __FUNCT__ "TSDestroy_Theta"
446 static PetscErrorCode TSDestroy_Theta(TS ts)
447 {
448   PetscErrorCode ierr;
449 
450   PetscFunctionBegin;
451   ierr = TSReset_Theta(ts);CHKERRQ(ierr);
452   ierr = PetscFree(ts->data);CHKERRQ(ierr);
453   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetTheta_C",NULL);CHKERRQ(ierr);
454   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetTheta_C",NULL);CHKERRQ(ierr);
455   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetEndpoint_C",NULL);CHKERRQ(ierr);
456   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetEndpoint_C",NULL);CHKERRQ(ierr);
457   PetscFunctionReturn(0);
458 }
459 
460 /*
461   This defines the nonlinear equation that is to be solved with SNES
462   G(U) = F[t0+Theta*dt, U, (U-U0)*shift] = 0
463 */
464 #undef __FUNCT__
465 #define __FUNCT__ "SNESTSFormFunction_Theta"
466 static PetscErrorCode SNESTSFormFunction_Theta(SNES snes,Vec x,Vec y,TS ts)
467 {
468   TS_Theta       *th = (TS_Theta*)ts->data;
469   PetscErrorCode ierr;
470   Vec            X0,Xdot;
471   DM             dm,dmsave;
472   PetscReal      shift = 1./(th->Theta*ts->time_step);
473 
474   PetscFunctionBegin;
475   ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
476   /* When using the endpoint variant, this is actually 1/Theta * Xdot */
477   ierr = TSThetaGetX0AndXdot(ts,dm,&X0,&Xdot);CHKERRQ(ierr);
478   ierr = VecAXPBYPCZ(Xdot,-shift,shift,0,X0,x);CHKERRQ(ierr);
479 
480   /* DM monkey-business allows user code to call TSGetDM() inside of functions evaluated on levels of FAS */
481   dmsave = ts->dm;
482   ts->dm = dm;
483   ierr   = TSComputeIFunction(ts,th->stage_time,x,Xdot,y,PETSC_FALSE);CHKERRQ(ierr);
484   ts->dm = dmsave;
485   ierr   = TSThetaRestoreX0AndXdot(ts,dm,&X0,&Xdot);CHKERRQ(ierr);
486   PetscFunctionReturn(0);
487 }
488 
489 #undef __FUNCT__
490 #define __FUNCT__ "SNESTSFormJacobian_Theta"
491 static PetscErrorCode SNESTSFormJacobian_Theta(SNES snes,Vec x,Mat A,Mat B,TS ts)
492 {
493   TS_Theta       *th = (TS_Theta*)ts->data;
494   PetscErrorCode ierr;
495   Vec            Xdot;
496   DM             dm,dmsave;
497   PetscReal      shift = 1./(th->Theta*ts->time_step);
498 
499   PetscFunctionBegin;
500   ierr = SNESGetDM(snes,&dm);CHKERRQ(ierr);
501 
502   /* th->Xdot has already been computed in SNESTSFormFunction_Theta (SNES guarantees this) */
503   ierr = TSThetaGetX0AndXdot(ts,dm,NULL,&Xdot);CHKERRQ(ierr);
504 
505   dmsave = ts->dm;
506   ts->dm = dm;
507   ierr   = TSComputeIJacobian(ts,th->stage_time,x,Xdot,shift,A,B,PETSC_FALSE);CHKERRQ(ierr);
508   ts->dm = dmsave;
509   ierr   = TSThetaRestoreX0AndXdot(ts,dm,NULL,&Xdot);CHKERRQ(ierr);
510   PetscFunctionReturn(0);
511 }
512 
513 #undef __FUNCT__
514 #define __FUNCT__ "TSSetUp_Theta"
515 static PetscErrorCode TSSetUp_Theta(TS ts)
516 {
517   TS_Theta       *th = (TS_Theta*)ts->data;
518   PetscErrorCode ierr;
519   SNES           snes;
520   TSAdapt        adapt;
521   DM             dm;
522 
523   PetscFunctionBegin;
524   if (!th->X) {
525     ierr = VecDuplicate(ts->vec_sol,&th->X);CHKERRQ(ierr);
526   }
527   if (!th->Xdot) {
528     ierr = VecDuplicate(ts->vec_sol,&th->Xdot);CHKERRQ(ierr);
529   }
530   if (!th->X0) {
531     ierr = VecDuplicate(ts->vec_sol,&th->X0);CHKERRQ(ierr);
532   }
533   ierr = TSGetSNES(ts,&snes);CHKERRQ(ierr);
534   ierr = TSGetDM(ts,&dm);CHKERRQ(ierr);
535   if (dm) {
536     ierr = DMCoarsenHookAdd(dm,DMCoarsenHook_TSTheta,DMRestrictHook_TSTheta,ts);CHKERRQ(ierr);
537     ierr = DMSubDomainHookAdd(dm,DMSubDomainHook_TSTheta,DMSubDomainRestrictHook_TSTheta,ts);CHKERRQ(ierr);
538   }
539   if (th->Theta == 0.5 && th->endpoint) th->order = 2;
540   else th->order = 1;
541 
542   ierr = TSGetAdapt(ts,&adapt);CHKERRQ(ierr);
543   if (!th->adapt) {
544     ierr = TSAdaptSetType(adapt,TSADAPTNONE);CHKERRQ(ierr);
545   }
546   PetscFunctionReturn(0);
547 }
548 /*------------------------------------------------------------*/
549 
550 #undef __FUNCT__
551 #define __FUNCT__ "TSAdjointSetUp_Theta"
552 static PetscErrorCode TSAdjointSetUp_Theta(TS ts)
553 {
554   TS_Theta       *th = (TS_Theta*)ts->data;
555   PetscErrorCode ierr;
556 
557   PetscFunctionBegin;
558   ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&th->VecsDeltaLam);CHKERRQ(ierr);
559   if(ts->vecs_sensip) {
560     ierr = VecDuplicateVecs(ts->vecs_sensip[0],ts->numcost,&th->VecsDeltaMu);CHKERRQ(ierr);
561   }
562   ierr = VecDuplicateVecs(ts->vecs_sensi[0],ts->numcost,&th->VecsSensiTemp);CHKERRQ(ierr);
563   PetscFunctionReturn(0);
564 }
565 /*------------------------------------------------------------*/
566 
567 #undef __FUNCT__
568 #define __FUNCT__ "TSSetFromOptions_Theta"
569 static PetscErrorCode TSSetFromOptions_Theta(PetscOptions *PetscOptionsObject,TS ts)
570 {
571   TS_Theta       *th = (TS_Theta*)ts->data;
572   PetscErrorCode ierr;
573 
574   PetscFunctionBegin;
575   ierr = PetscOptionsHead(PetscOptionsObject,"Theta ODE solver options");CHKERRQ(ierr);
576   {
577     ierr = PetscOptionsReal("-ts_theta_theta","Location of stage (0<Theta<=1)","TSThetaSetTheta",th->Theta,&th->Theta,NULL);CHKERRQ(ierr);
578     ierr = PetscOptionsBool("-ts_theta_extrapolate","Extrapolate stage solution from previous solution (sometimes unstable)","TSThetaSetExtrapolate",th->extrapolate,&th->extrapolate,NULL);CHKERRQ(ierr);
579     ierr = PetscOptionsBool("-ts_theta_endpoint","Use the endpoint instead of midpoint form of the Theta method","TSThetaSetEndpoint",th->endpoint,&th->endpoint,NULL);CHKERRQ(ierr);
580     ierr = PetscOptionsBool("-ts_theta_adapt","Use time-step adaptivity with the Theta method","",th->adapt,&th->adapt,NULL);CHKERRQ(ierr);
581     ierr = SNESSetFromOptions(ts->snes);CHKERRQ(ierr);
582   }
583   ierr = PetscOptionsTail();CHKERRQ(ierr);
584   PetscFunctionReturn(0);
585 }
586 
587 #undef __FUNCT__
588 #define __FUNCT__ "TSView_Theta"
589 static PetscErrorCode TSView_Theta(TS ts,PetscViewer viewer)
590 {
591   TS_Theta       *th = (TS_Theta*)ts->data;
592   PetscBool      iascii;
593   PetscErrorCode ierr;
594 
595   PetscFunctionBegin;
596   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERASCII,&iascii);CHKERRQ(ierr);
597   if (iascii) {
598     ierr = PetscViewerASCIIPrintf(viewer,"  Theta=%g\n",(double)th->Theta);CHKERRQ(ierr);
599     ierr = PetscViewerASCIIPrintf(viewer,"  Extrapolation=%s\n",th->extrapolate ? "yes" : "no");CHKERRQ(ierr);
600   }
601   if (ts->snes) {ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);}
602   PetscFunctionReturn(0);
603 }
604 
605 #undef __FUNCT__
606 #define __FUNCT__ "TSThetaGetTheta_Theta"
607 PetscErrorCode  TSThetaGetTheta_Theta(TS ts,PetscReal *theta)
608 {
609   TS_Theta *th = (TS_Theta*)ts->data;
610 
611   PetscFunctionBegin;
612   *theta = th->Theta;
613   PetscFunctionReturn(0);
614 }
615 
616 #undef __FUNCT__
617 #define __FUNCT__ "TSThetaSetTheta_Theta"
618 PetscErrorCode  TSThetaSetTheta_Theta(TS ts,PetscReal theta)
619 {
620   TS_Theta *th = (TS_Theta*)ts->data;
621 
622   PetscFunctionBegin;
623   if (theta <= 0 || 1 < theta) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Theta %g not in range (0,1]",(double)theta);
624   th->Theta = theta;
625   PetscFunctionReturn(0);
626 }
627 
628 #undef __FUNCT__
629 #define __FUNCT__ "TSThetaGetEndpoint_Theta"
630 PetscErrorCode  TSThetaGetEndpoint_Theta(TS ts,PetscBool *endpoint)
631 {
632   TS_Theta *th = (TS_Theta*)ts->data;
633 
634   PetscFunctionBegin;
635   *endpoint = th->endpoint;
636   PetscFunctionReturn(0);
637 }
638 
639 #undef __FUNCT__
640 #define __FUNCT__ "TSThetaSetEndpoint_Theta"
641 PetscErrorCode  TSThetaSetEndpoint_Theta(TS ts,PetscBool flg)
642 {
643   TS_Theta *th = (TS_Theta*)ts->data;
644 
645   PetscFunctionBegin;
646   th->endpoint = flg;
647   PetscFunctionReturn(0);
648 }
649 
650 #if defined(PETSC_HAVE_COMPLEX)
651 #undef __FUNCT__
652 #define __FUNCT__ "TSComputeLinearStability_Theta"
653 static PetscErrorCode TSComputeLinearStability_Theta(TS ts,PetscReal xr,PetscReal xi,PetscReal *yr,PetscReal *yi)
654 {
655   PetscComplex z   = xr + xi*PETSC_i,f;
656   TS_Theta     *th = (TS_Theta*)ts->data;
657   const PetscReal one = 1.0;
658 
659   PetscFunctionBegin;
660   f   = (one + (one - th->Theta)*z)/(one - th->Theta*z);
661   *yr = PetscRealPartComplex(f);
662   *yi = PetscImaginaryPartComplex(f);
663   PetscFunctionReturn(0);
664 }
665 #endif
666 
667 #undef __FUNCT__
668 #define __FUNCT__ "TSGetStages_Theta"
669 static PetscErrorCode  TSGetStages_Theta(TS ts,PetscInt *ns,Vec **Y)
670 {
671   TS_Theta     *th = (TS_Theta*)ts->data;
672 
673   PetscFunctionBegin;
674   *ns = 1;
675   if(Y) {
676     *Y  = (th->endpoint)?&(th->X0):&(th->X);
677   }
678   PetscFunctionReturn(0);
679 }
680 
681 /* ------------------------------------------------------------ */
682 /*MC
683       TSTHETA - DAE solver using the implicit Theta method
684 
685    Level: beginner
686 
687    Options Database:
688       -ts_theta_theta <Theta> - Location of stage (0<Theta<=1)
689       -ts_theta_extrapolate <flg> Extrapolate stage solution from previous solution (sometimes unstable)
690       -ts_theta_endpoint <flag> - Use the endpoint (like Crank-Nicholson) instead of midpoint form of the Theta method
691 
692    Notes:
693 $  -ts_type theta -ts_theta_theta 1.0 corresponds to backward Euler (TSBEULER)
694 $  -ts_type theta -ts_theta_theta 0.5 corresponds to the implicit midpoint rule
695 $  -ts_type theta -ts_theta_theta 0.5 -ts_theta_endpoint corresponds to Crank-Nicholson (TSCN)
696 
697 
698 
699    This method can be applied to DAE.
700 
701    This method is cast as a 1-stage implicit Runge-Kutta method.
702 
703 .vb
704   Theta | Theta
705   -------------
706         |  1
707 .ve
708 
709    For the default Theta=0.5, this is also known as the implicit midpoint rule.
710 
711    When the endpoint variant is chosen, the method becomes a 2-stage method with first stage explicit:
712 
713 .vb
714   0 | 0         0
715   1 | 1-Theta   Theta
716   -------------------
717     | 1-Theta   Theta
718 .ve
719 
720    For the default Theta=0.5, this is the trapezoid rule (also known as Crank-Nicolson, see TSCN).
721 
722    To apply a diagonally implicit RK method to DAE, the stage formula
723 
724 $  Y_i = X + h sum_j a_ij Y'_j
725 
726    is interpreted as a formula for Y'_i in terms of Y_i and known values (Y'_j, j<i)
727 
728 .seealso:  TSCreate(), TS, TSSetType(), TSCN, TSBEULER, TSThetaSetTheta(), TSThetaSetEndpoint()
729 
730 M*/
731 #undef __FUNCT__
732 #define __FUNCT__ "TSCreate_Theta"
733 PETSC_EXTERN PetscErrorCode TSCreate_Theta(TS ts)
734 {
735   TS_Theta       *th;
736   PetscErrorCode ierr;
737 
738   PetscFunctionBegin;
739   ts->ops->reset           = TSReset_Theta;
740   ts->ops->destroy         = TSDestroy_Theta;
741   ts->ops->view            = TSView_Theta;
742   ts->ops->setup           = TSSetUp_Theta;
743   ts->ops->adjointsetup    = TSAdjointSetUp_Theta;
744   ts->ops->step            = TSStep_Theta;
745   ts->ops->interpolate     = TSInterpolate_Theta;
746   ts->ops->evaluatestep    = TSEvaluateStep_Theta;
747   ts->ops->rollback        = TSRollBack_Theta;
748   ts->ops->setfromoptions  = TSSetFromOptions_Theta;
749   ts->ops->snesfunction    = SNESTSFormFunction_Theta;
750   ts->ops->snesjacobian    = SNESTSFormJacobian_Theta;
751 #if defined(PETSC_HAVE_COMPLEX)
752   ts->ops->linearstability = TSComputeLinearStability_Theta;
753 #endif
754   ts->ops->getstages       = TSGetStages_Theta;
755   ts->ops->adjointstep     = TSAdjointStep_Theta;
756 
757   ierr = PetscNewLog(ts,&th);CHKERRQ(ierr);
758   ts->data = (void*)th;
759 
760   th->extrapolate = PETSC_FALSE;
761   th->Theta       = 0.5;
762   th->ccfl        = 1.0;
763   th->adapt       = PETSC_FALSE;
764   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetTheta_C",TSThetaGetTheta_Theta);CHKERRQ(ierr);
765   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetTheta_C",TSThetaSetTheta_Theta);CHKERRQ(ierr);
766   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaGetEndpoint_C",TSThetaGetEndpoint_Theta);CHKERRQ(ierr);
767   ierr = PetscObjectComposeFunction((PetscObject)ts,"TSThetaSetEndpoint_C",TSThetaSetEndpoint_Theta);CHKERRQ(ierr);
768   PetscFunctionReturn(0);
769 }
770 
771 #undef __FUNCT__
772 #define __FUNCT__ "TSThetaGetTheta"
773 /*@
774   TSThetaGetTheta - Get the abscissa of the stage in (0,1].
775 
776   Not Collective
777 
778   Input Parameter:
779 .  ts - timestepping context
780 
781   Output Parameter:
782 .  theta - stage abscissa
783 
784   Note:
785   Use of this function is normally only required to hack TSTHETA to use a modified integration scheme.
786 
787   Level: Advanced
788 
789 .seealso: TSThetaSetTheta()
790 @*/
791 PetscErrorCode  TSThetaGetTheta(TS ts,PetscReal *theta)
792 {
793   PetscErrorCode ierr;
794 
795   PetscFunctionBegin;
796   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
797   PetscValidPointer(theta,2);
798   ierr = PetscUseMethod(ts,"TSThetaGetTheta_C",(TS,PetscReal*),(ts,theta));CHKERRQ(ierr);
799   PetscFunctionReturn(0);
800 }
801 
802 #undef __FUNCT__
803 #define __FUNCT__ "TSThetaSetTheta"
804 /*@
805   TSThetaSetTheta - Set the abscissa of the stage in (0,1].
806 
807   Not Collective
808 
809   Input Parameter:
810 +  ts - timestepping context
811 -  theta - stage abscissa
812 
813   Options Database:
814 .  -ts_theta_theta <theta>
815 
816   Level: Intermediate
817 
818 .seealso: TSThetaGetTheta()
819 @*/
820 PetscErrorCode  TSThetaSetTheta(TS ts,PetscReal theta)
821 {
822   PetscErrorCode ierr;
823 
824   PetscFunctionBegin;
825   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
826   ierr = PetscTryMethod(ts,"TSThetaSetTheta_C",(TS,PetscReal),(ts,theta));CHKERRQ(ierr);
827   PetscFunctionReturn(0);
828 }
829 
830 #undef __FUNCT__
831 #define __FUNCT__ "TSThetaGetEndpoint"
832 /*@
833   TSThetaGetEndpoint - Gets whether to use the endpoint variant of the method (e.g. trapezoid/Crank-Nicolson instead of midpoint rule).
834 
835   Not Collective
836 
837   Input Parameter:
838 .  ts - timestepping context
839 
840   Output Parameter:
841 .  endpoint - PETSC_TRUE when using the endpoint variant
842 
843   Level: Advanced
844 
845 .seealso: TSThetaSetEndpoint(), TSTHETA, TSCN
846 @*/
847 PetscErrorCode TSThetaGetEndpoint(TS ts,PetscBool *endpoint)
848 {
849   PetscErrorCode ierr;
850 
851   PetscFunctionBegin;
852   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
853   PetscValidPointer(endpoint,2);
854   ierr = PetscTryMethod(ts,"TSThetaGetEndpoint_C",(TS,PetscBool*),(ts,endpoint));CHKERRQ(ierr);
855   PetscFunctionReturn(0);
856 }
857 
858 #undef __FUNCT__
859 #define __FUNCT__ "TSThetaSetEndpoint"
860 /*@
861   TSThetaSetEndpoint - Sets whether to use the endpoint variant of the method (e.g. trapezoid/Crank-Nicolson instead of midpoint rule).
862 
863   Not Collective
864 
865   Input Parameter:
866 +  ts - timestepping context
867 -  flg - PETSC_TRUE to use the endpoint variant
868 
869   Options Database:
870 .  -ts_theta_endpoint <flg>
871 
872   Level: Intermediate
873 
874 .seealso: TSTHETA, TSCN
875 @*/
876 PetscErrorCode TSThetaSetEndpoint(TS ts,PetscBool flg)
877 {
878   PetscErrorCode ierr;
879 
880   PetscFunctionBegin;
881   PetscValidHeaderSpecific(ts,TS_CLASSID,1);
882   ierr = PetscTryMethod(ts,"TSThetaSetEndpoint_C",(TS,PetscBool),(ts,flg));CHKERRQ(ierr);
883   PetscFunctionReturn(0);
884 }
885 
886 /*
887  * TSBEULER and TSCN are straightforward specializations of TSTHETA.
888  * The creation functions for these specializations are below.
889  */
890 
891 #undef __FUNCT__
892 #define __FUNCT__ "TSView_BEuler"
893 static PetscErrorCode TSView_BEuler(TS ts,PetscViewer viewer)
894 {
895   PetscErrorCode ierr;
896 
897   PetscFunctionBegin;
898   ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
899   PetscFunctionReturn(0);
900 }
901 
902 /*MC
903       TSBEULER - ODE solver using the implicit backward Euler method
904 
905   Level: beginner
906 
907   Notes:
908   TSBEULER is equivalent to TSTHETA with Theta=1.0
909 
910 $  -ts_type theta -ts_theta_theta 1.
911 
912 .seealso:  TSCreate(), TS, TSSetType(), TSEULER, TSCN, TSTHETA
913 
914 M*/
915 #undef __FUNCT__
916 #define __FUNCT__ "TSCreate_BEuler"
917 PETSC_EXTERN PetscErrorCode TSCreate_BEuler(TS ts)
918 {
919   PetscErrorCode ierr;
920 
921   PetscFunctionBegin;
922   ierr = TSCreate_Theta(ts);CHKERRQ(ierr);
923   ierr = TSThetaSetTheta(ts,1.0);CHKERRQ(ierr);
924   ts->ops->view = TSView_BEuler;
925   PetscFunctionReturn(0);
926 }
927 
928 #undef __FUNCT__
929 #define __FUNCT__ "TSView_CN"
930 static PetscErrorCode TSView_CN(TS ts,PetscViewer viewer)
931 {
932   PetscErrorCode ierr;
933 
934   PetscFunctionBegin;
935   ierr = SNESView(ts->snes,viewer);CHKERRQ(ierr);
936   PetscFunctionReturn(0);
937 }
938 
939 /*MC
940       TSCN - ODE solver using the implicit Crank-Nicolson method.
941 
942   Level: beginner
943 
944   Notes:
945   TSCN is equivalent to TSTHETA with Theta=0.5 and the "endpoint" option set. I.e.
946 
947 $  -ts_type theta -ts_theta_theta 0.5 -ts_theta_endpoint
948 
949 .seealso:  TSCreate(), TS, TSSetType(), TSBEULER, TSTHETA
950 
951 M*/
952 #undef __FUNCT__
953 #define __FUNCT__ "TSCreate_CN"
954 PETSC_EXTERN PetscErrorCode TSCreate_CN(TS ts)
955 {
956   PetscErrorCode ierr;
957 
958   PetscFunctionBegin;
959   ierr = TSCreate_Theta(ts);CHKERRQ(ierr);
960   ierr = TSThetaSetTheta(ts,0.5);CHKERRQ(ierr);
961   ierr = TSThetaSetEndpoint(ts,PETSC_TRUE);CHKERRQ(ierr);
962   ts->ops->view = TSView_CN;
963   PetscFunctionReturn(0);
964 }
965