Actual source code: tssen.c

petsc-3.12.1 2019-10-22
Report Typos and Errors
  1:  #include <petsc/private/tsimpl.h>
  2:  #include <petscdraw.h>

  4: PetscLogEvent TS_AdjointStep,TS_ForwardStep,TS_JacobianPEval;

  6: /* #define TSADJOINT_STAGE */

  8: /* ------------------------ Sensitivity Context ---------------------------*/

 10: /*@C
 11:   TSSetRHSJacobianP - Sets the function that computes the Jacobian of G w.r.t. the parameters P where U_t = G(U,P,t), as well as the location to store the matrix.

 13:   Logically Collective on TS

 15:   Input Parameters:
 16: + ts - TS context obtained from TSCreate()
 17: . Amat - JacobianP matrix
 18: . func - function
 19: - ctx - [optional] user-defined function context

 21:   Calling sequence of func:
 22: $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
 23: +   t - current timestep
 24: .   U - input vector (current ODE solution)
 25: .   A - output matrix
 26: -   ctx - [optional] user-defined function context

 28:   Level: intermediate

 30:   Notes:
 31:     Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p

 33: .seealso: TSGetRHSJacobianP()
 34: @*/
 35: PetscErrorCode TSSetRHSJacobianP(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
 36: {


 43:   ts->rhsjacobianp    = func;
 44:   ts->rhsjacobianpctx = ctx;
 45:   if(Amat) {
 46:     PetscObjectReference((PetscObject)Amat);
 47:     MatDestroy(&ts->Jacprhs);
 48:     ts->Jacprhs = Amat;
 49:   }
 50:   return(0);
 51: }

 53: /*@C
 54:   TSGetRHSJacobianP - Gets the function that computes the Jacobian of G w.r.t. the parameters P where U_t = G(U,P,t), as well as the location to store the matrix.

 56:   Logically Collective on TS

 58:   Input Parameters:
 59: . ts - TS context obtained from TSCreate()

 61:   Output Parameters:
 62: + Amat - JacobianP matrix
 63: . func - function
 64: - ctx - [optional] user-defined function context

 66:   Calling sequence of func:
 67: $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
 68: +   t - current timestep
 69: .   U - input vector (current ODE solution)
 70: .   A - output matrix
 71: -   ctx - [optional] user-defined function context

 73:   Level: intermediate

 75:   Notes:
 76:     Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p

 78: .seealso: TSSetRHSJacobianP()
 79: @*/
 80: PetscErrorCode TSGetRHSJacobianP(TS ts,Mat *Amat,PetscErrorCode (**func)(TS,PetscReal,Vec,Mat,void*),void **ctx)
 81: {
 83:   if (func) *func = ts->rhsjacobianp;
 84:   if (ctx) *ctx  = ts->rhsjacobianpctx;
 85:   if (Amat) *Amat = ts->Jacprhs;
 86:   return(0);
 87: }

 89: /*@C
 90:   TSComputeRHSJacobianP - Runs the user-defined JacobianP function.

 92:   Collective on TS

 94:   Input Parameters:
 95: . ts   - The TS context obtained from TSCreate()

 97:   Level: developer

 99: .seealso: TSSetRHSJacobianP()
100: @*/
101: PetscErrorCode TSComputeRHSJacobianP(TS ts,PetscReal t,Vec U,Mat Amat)
102: {

106:   if (!Amat) return(0);

110:   PetscStackPush("TS user JacobianP function for sensitivity analysis");
111:   (*ts->rhsjacobianp)(ts,t,U,Amat,ts->rhsjacobianpctx);
112:   PetscStackPop;
113:   return(0);
114: }

116: /*@C
117:   TSSetIJacobianP - Sets the function that computes the Jacobian of F w.r.t. the parameters P where F(Udot,U,t) = G(U,P,t), as well as the location to store the matrix.

119:   Logically Collective on TS

121:   Input Parameters:
122: + ts - TS context obtained from TSCreate()
123: . Amat - JacobianP matrix
124: . func - function
125: - ctx - [optional] user-defined function context

127:   Calling sequence of func:
128: $ func (TS ts,PetscReal t,Vec y,Mat A,void *ctx);
129: +   t - current timestep
130: .   U - input vector (current ODE solution)
131: .   Udot - time derivative of state vector
132: .   shift - shift to apply, see note below
133: .   A - output matrix
134: -   ctx - [optional] user-defined function context

136:   Level: intermediate

138:   Notes:
139:     Amat has the same number of rows and the same row parallel layout as u, Amat has the same number of columns and parallel layout as p

141: .seealso:
142: @*/
143: PetscErrorCode TSSetIJacobianP(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Vec,PetscReal,Mat,void*),void *ctx)
144: {


151:   ts->ijacobianp    = func;
152:   ts->ijacobianpctx = ctx;
153:   if(Amat) {
154:     PetscObjectReference((PetscObject)Amat);
155:     MatDestroy(&ts->Jacp);
156:     ts->Jacp = Amat;
157:   }
158:   return(0);
159: }

161: /*@C
162:   TSComputeIJacobianP - Runs the user-defined IJacobianP function.

164:   Collective on TS

166:   Input Parameters:
167: + ts - the TS context
168: . t - current timestep
169: . U - state vector
170: . Udot - time derivative of state vector
171: . shift - shift to apply, see note below
172: - imex - flag indicates if the method is IMEX so that the RHSJacobian should be kept separate

174:   Output Parameters:
175: . A - Jacobian matrix

177:   Level: developer

179: .seealso: TSSetIJacobianP()
180: @*/
181: PetscErrorCode TSComputeIJacobianP(TS ts,PetscReal t,Vec U,Vec Udot,PetscReal shift,Mat Amat,PetscBool imex)
182: {

186:   if (!Amat) return(0);

191:   PetscLogEventBegin(TS_JacobianPEval,ts,U,Amat,0);
192:   if (ts->ijacobianp) {
193:     PetscStackPush("TS user JacobianP function for sensitivity analysis");
194:     (*ts->ijacobianp)(ts,t,U,Udot,shift,Amat,ts->ijacobianpctx);
195:     PetscStackPop;
196:   }
197:   if (imex) {
198:     if (!ts->ijacobianp) {  /* system was written as Udot = G(t,U) */
199:       PetscBool assembled;
200:       MatZeroEntries(Amat);
201:       MatAssembled(Amat,&assembled);
202:       if (!assembled) {
203:         MatAssemblyBegin(Amat,MAT_FINAL_ASSEMBLY);
204:         MatAssemblyEnd(Amat,MAT_FINAL_ASSEMBLY);
205:       }
206:     }
207:   } else {
208:     if (ts->rhsjacobianp) {
209:       TSComputeRHSJacobianP(ts,t,U,ts->Jacprhs);
210:     }
211:     if (ts->Jacprhs == Amat) { /* No IJacobian, so we only have the RHS matrix */
212:       MatScale(Amat,-1);
213:     } else if (ts->Jacprhs) { /* Both IJacobian and RHSJacobian */
214:       MatStructure axpy = DIFFERENT_NONZERO_PATTERN;
215:       if (!ts->ijacobianp) { /* No IJacobianp provided, but we have a separate RHS matrix */
216:         MatZeroEntries(Amat);
217:       }
218:       MatAXPY(Amat,-1,ts->Jacprhs,axpy);
219:     }
220:   }
221:   PetscLogEventEnd(TS_JacobianPEval,ts,U,Amat,0);
222:   return(0);
223: }

225: /*@C
226:     TSSetCostIntegrand - Sets the routine for evaluating the integral term in one or more cost functions

228:     Logically Collective on TS

230:     Input Parameters:
231: +   ts - the TS context obtained from TSCreate()
232: .   numcost - number of gradients to be computed, this is the number of cost functions
233: .   costintegral - vector that stores the integral values
234: .   rf - routine for evaluating the integrand function
235: .   drduf - function that computes the gradients of the r's with respect to u
236: .   drdpf - function that computes the gradients of the r's with respect to p, can be NULL if parametric sensitivity is not desired (mu=NULL)
237: .   fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
238: -   ctx - [optional] user-defined context for private data for the function evaluation routine (may be NULL)

240:     Calling sequence of rf:
241: $   PetscErrorCode rf(TS ts,PetscReal t,Vec U,Vec F,void *ctx);

243:     Calling sequence of drduf:
244: $   PetscErroCode drduf(TS ts,PetscReal t,Vec U,Vec *dRdU,void *ctx);

246:     Calling sequence of drdpf:
247: $   PetscErroCode drdpf(TS ts,PetscReal t,Vec U,Vec *dRdP,void *ctx);

249:     Level: deprecated

251:     Notes:
252:     For optimization there is usually a single cost function (numcost = 1). For sensitivities there may be multiple cost functions

254: .seealso: TSSetRHSJacobianP(), TSGetCostGradients(), TSSetCostGradients()
255: @*/
256: PetscErrorCode TSSetCostIntegrand(TS ts,PetscInt numcost,Vec costintegral,PetscErrorCode (*rf)(TS,PetscReal,Vec,Vec,void*),
257:                                                           PetscErrorCode (*drduf)(TS,PetscReal,Vec,Vec*,void*),
258:                                                           PetscErrorCode (*drdpf)(TS,PetscReal,Vec,Vec*,void*),
259:                                                           PetscBool fwd,void *ctx)
260: {

266:   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostGradients() or TSForwardSetIntegralGradients()");
267:   if (!ts->numcost) ts->numcost=numcost;

269:   if (costintegral) {
270:     PetscObjectReference((PetscObject)costintegral);
271:     VecDestroy(&ts->vec_costintegral);
272:     ts->vec_costintegral = costintegral;
273:   } else {
274:     if (!ts->vec_costintegral) { /* Create a seq vec if user does not provide one */
275:       VecCreateSeq(PETSC_COMM_SELF,numcost,&ts->vec_costintegral);
276:     } else {
277:       VecSet(ts->vec_costintegral,0.0);
278:     }
279:   }
280:   if (!ts->vec_costintegrand) {
281:     VecDuplicate(ts->vec_costintegral,&ts->vec_costintegrand);
282:   } else {
283:     VecSet(ts->vec_costintegrand,0.0);
284:   }
285:   ts->costintegralfwd  = fwd; /* Evaluate the cost integral in forward run if fwd is true */
286:   ts->costintegrand    = rf;
287:   ts->costintegrandctx = ctx;
288:   ts->drdufunction     = drduf;
289:   ts->drdpfunction     = drdpf;
290:   return(0);
291: }

293: /*@C
294:    TSGetCostIntegral - Returns the values of the integral term in the cost functions.
295:    It is valid to call the routine after a backward run.

297:    Not Collective

299:    Input Parameter:
300: .  ts - the TS context obtained from TSCreate()

302:    Output Parameter:
303: .  v - the vector containing the integrals for each cost function

305:    Level: intermediate

307: .seealso: TSSetCostIntegrand()

309: @*/
310: PetscErrorCode  TSGetCostIntegral(TS ts,Vec *v)
311: {
312:   TS             quadts;

318:   TSGetQuadratureTS(ts,NULL,&quadts);
319:   *v = quadts->vec_sol;
320:   return(0);
321: }

323: /*@C
324:    TSComputeCostIntegrand - Evaluates the integral function in the cost functions.

326:    Input Parameters:
327: +  ts - the TS context
328: .  t - current time
329: -  U - state vector, i.e. current solution

331:    Output Parameter:
332: .  Q - vector of size numcost to hold the outputs

334:    Notes:
335:    Most users should not need to explicitly call this routine, as it
336:    is used internally within the sensitivity analysis context.

338:    Level: deprecated

340: .seealso: TSSetCostIntegrand()
341: @*/
342: PetscErrorCode TSComputeCostIntegrand(TS ts,PetscReal t,Vec U,Vec Q)
343: {


351:   PetscLogEventBegin(TS_FunctionEval,ts,U,Q,0);
352:   if (ts->costintegrand) {
353:     PetscStackPush("TS user integrand in the cost function");
354:     (*ts->costintegrand)(ts,t,U,Q,ts->costintegrandctx);
355:     PetscStackPop;
356:   } else {
357:     VecZeroEntries(Q);
358:   }

360:   PetscLogEventEnd(TS_FunctionEval,ts,U,Q,0);
361:   return(0);
362: }

364: /*@C
365:   TSComputeDRDUFunction - Deprecated, use TSGetQuadratureTS() then TSComputeRHSJacobian()

367:   Level: deprecated

369: @*/
370: PetscErrorCode TSComputeDRDUFunction(TS ts,PetscReal t,Vec U,Vec *DRDU)
371: {

375:   if (!DRDU) return(0);

379:   PetscStackPush("TS user DRDU function for sensitivity analysis");
380:   (*ts->drdufunction)(ts,t,U,DRDU,ts->costintegrandctx);
381:   PetscStackPop;
382:   return(0);
383: }

385: /*@C
386:   TSComputeDRDPFunction - Deprecated, use TSGetQuadratureTS() then TSComputeRHSJacobianP()

388:   Level: deprecated

390: @*/
391: PetscErrorCode TSComputeDRDPFunction(TS ts,PetscReal t,Vec U,Vec *DRDP)
392: {

396:   if (!DRDP) return(0);

400:   PetscStackPush("TS user DRDP function for sensitivity analysis");
401:   (*ts->drdpfunction)(ts,t,U,DRDP,ts->costintegrandctx);
402:   PetscStackPop;
403:   return(0);
404: }

406: /*@C
407:   TSSetIHessianProduct - Sets the function that computes the vector-Hessian-vector product. The Hessian is the second-order derivative of F (IFunction) w.r.t. the state variable.

409:   Logically Collective on TS

411:   Input Parameters:
412: + ts - TS context obtained from TSCreate()
413: . ihp1 - an array of vectors storing the result of vector-Hessian-vector product for F_UU
414: . hessianproductfunc1 - vector-Hessian-vector product function for F_UU
415: . ihp2 - an array of vectors storing the result of vector-Hessian-vector product for F_UP
416: . hessianproductfunc2 - vector-Hessian-vector product function for F_UP
417: . ihp3 - an array of vectors storing the result of vector-Hessian-vector product for F_PU
418: . hessianproductfunc3 - vector-Hessian-vector product function for F_PU
419: . ihp4 - an array of vectors storing the result of vector-Hessian-vector product for F_PP
420: . hessianproductfunc4 - vector-Hessian-vector product function for F_PP

422:   Calling sequence of ihessianproductfunc:
423: $ ihessianproductfunc (TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV,void *ctx);
424: +   t - current timestep
425: .   U - input vector (current ODE solution)
426: .   Vl - an array of input vectors to be left-multiplied with the Hessian
427: .   Vr - input vector to be right-multiplied with the Hessian
428: .   VHV - an array of output vectors for vector-Hessian-vector product
429: -   ctx - [optional] user-defined function context

431:   Level: intermediate

433:   Notes:
434:   The first Hessian function and the working array are required.
435:   As an example to implement the callback functions, the second callback function calculates the vector-Hessian-vector product
436:   $ Vl_n^T*F_UP*Vr
437:   where the vector Vl_n (n-th element in the array Vl) and Vr are of size N and M respectively, and the Hessian F_UP is of size N x N x M.
438:   Each entry of F_UP corresponds to the derivative
439:   $ F_UP[i][j][k] = \frac{\partial^2 F[i]}{\partial U[j] \partial P[k]}.
440:   The result of the vector-Hessian-vector product for Vl_n needs to be stored in vector VHV_n with the j-th entry being
441:   $ VHV_n[j] = \sum_i \sum_k {Vl_n[i] * F_UP[i][j][k] * Vr[k]}
442:   If the cost function is a scalar, there will be only one vector in Vl and VHV.

444: .seealso:
445: @*/
446: PetscErrorCode TSSetIHessianProduct(TS ts,Vec *ihp1,PetscErrorCode (*ihessianproductfunc1)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
447:                                           Vec *ihp2,PetscErrorCode (*ihessianproductfunc2)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
448:                                           Vec *ihp3,PetscErrorCode (*ihessianproductfunc3)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
449:                                           Vec *ihp4,PetscErrorCode (*ihessianproductfunc4)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
450:                                     void *ctx)
451: {

456:   ts->ihessianproductctx = ctx;
457:   if (ihp1) ts->vecs_fuu = ihp1;
458:   if (ihp2) ts->vecs_fup = ihp2;
459:   if (ihp3) ts->vecs_fpu = ihp3;
460:   if (ihp4) ts->vecs_fpp = ihp4;
461:   ts->ihessianproduct_fuu = ihessianproductfunc1;
462:   ts->ihessianproduct_fup = ihessianproductfunc2;
463:   ts->ihessianproduct_fpu = ihessianproductfunc3;
464:   ts->ihessianproduct_fpp = ihessianproductfunc4;
465:   return(0);
466: }

468: /*@C
469:   TSComputeIHessianProductFunctionUU - Runs the user-defined vector-Hessian-vector product function for Fuu.

471:   Collective on TS

473:   Input Parameters:
474: . ts   - The TS context obtained from TSCreate()

476:   Notes:
477:   TSComputeIHessianProductFunctionUU() is typically used for sensitivity implementation,
478:   so most users would not generally call this routine themselves.

480:   Level: developer

482: .seealso: TSSetIHessianProduct()
483: @*/
484: PetscErrorCode TSComputeIHessianProductFunctionUU(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
485: {

489:   if (!VHV) return(0);

493:   if (ts->ihessianproduct_fuu) {
494:     PetscStackPush("TS user IHessianProduct function 1 for sensitivity analysis");
495:     (*ts->ihessianproduct_fuu)(ts,t,U,Vl,Vr,VHV,ts->ihessianproductctx);
496:     PetscStackPop;
497:   }
498:   /* does not consider IMEX for now, so either IHessian or RHSHessian will be calculated, using the same output VHV */
499:   if (ts->rhshessianproduct_guu) {
500:     PetscInt nadj;
501:     TSComputeRHSHessianProductFunctionUU(ts,t,U,Vl,Vr,VHV);
502:     for (nadj=0; nadj<ts->numcost; nadj++) {
503:       VecScale(VHV[nadj],-1);
504:     }
505:   }
506:   return(0);
507: }

509: /*@C
510:   TSComputeIHessianProductFunctionUP - Runs the user-defined vector-Hessian-vector product function for Fup.

512:   Collective on TS

514:   Input Parameters:
515: . ts   - The TS context obtained from TSCreate()

517:   Notes:
518:   TSComputeIHessianProductFunctionUP() is typically used for sensitivity implementation,
519:   so most users would not generally call this routine themselves.

521:   Level: developer

523: .seealso: TSSetIHessianProduct()
524: @*/
525: PetscErrorCode TSComputeIHessianProductFunctionUP(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
526: {

530:   if (!VHV) return(0);

534:   if (ts->ihessianproduct_fup) {
535:     PetscStackPush("TS user IHessianProduct function 2 for sensitivity analysis");
536:     (*ts->ihessianproduct_fup)(ts,t,U,Vl,Vr,VHV,ts->ihessianproductctx);
537:     PetscStackPop;
538:   }
539:   /* does not consider IMEX for now, so either IHessian or RHSHessian will be calculated, using the same output VHV */
540:   if (ts->rhshessianproduct_gup) {
541:     PetscInt nadj;
542:     TSComputeRHSHessianProductFunctionUP(ts,t,U,Vl,Vr,VHV);
543:     for (nadj=0; nadj<ts->numcost; nadj++) {
544:       VecScale(VHV[nadj],-1);
545:     }
546:   }
547:   return(0);
548: }

550: /*@C
551:   TSComputeIHessianProductFunctionPU - Runs the user-defined vector-Hessian-vector product function for Fpu.

553:   Collective on TS

555:   Input Parameters:
556: . ts   - The TS context obtained from TSCreate()

558:   Notes:
559:   TSComputeIHessianProductFunctionPU() is typically used for sensitivity implementation,
560:   so most users would not generally call this routine themselves.

562:   Level: developer

564: .seealso: TSSetIHessianProduct()
565: @*/
566: PetscErrorCode TSComputeIHessianProductFunctionPU(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
567: {

571:   if (!VHV) return(0);

575:   if (ts->ihessianproduct_fpu) {
576:     PetscStackPush("TS user IHessianProduct function 3 for sensitivity analysis");
577:     (*ts->ihessianproduct_fpu)(ts,t,U,Vl,Vr,VHV,ts->ihessianproductctx);
578:     PetscStackPop;
579:   }
580:   /* does not consider IMEX for now, so either IHessian or RHSHessian will be calculated, using the same output VHV */
581:   if (ts->rhshessianproduct_gpu) {
582:     PetscInt nadj;
583:     TSComputeRHSHessianProductFunctionPU(ts,t,U,Vl,Vr,VHV);
584:     for (nadj=0; nadj<ts->numcost; nadj++) {
585:       VecScale(VHV[nadj],-1);
586:     }
587:   }
588:   return(0);
589: }

591: /*@C
592:   TSComputeIHessianProductFunctionPP - Runs the user-defined vector-Hessian-vector product function for Fpp.

594:   Collective on TS

596:   Input Parameters:
597: . ts   - The TS context obtained from TSCreate()

599:   Notes:
600:   TSComputeIHessianProductFunctionPP() is typically used for sensitivity implementation,
601:   so most users would not generally call this routine themselves.

603:   Level: developer

605: .seealso: TSSetIHessianProduct()
606: @*/
607: PetscErrorCode TSComputeIHessianProductFunctionPP(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
608: {

612:   if (!VHV) return(0);

616:   if (ts->ihessianproduct_fpp) {
617:     PetscStackPush("TS user IHessianProduct function 3 for sensitivity analysis");
618:     (*ts->ihessianproduct_fpp)(ts,t,U,Vl,Vr,VHV,ts->ihessianproductctx);
619:     PetscStackPop;
620:   }
621:   /* does not consider IMEX for now, so either IHessian or RHSHessian will be calculated, using the same output VHV */
622:   if (ts->rhshessianproduct_gpp) {
623:     PetscInt nadj;
624:     TSComputeRHSHessianProductFunctionPP(ts,t,U,Vl,Vr,VHV);
625:     for (nadj=0; nadj<ts->numcost; nadj++) {
626:       VecScale(VHV[nadj],-1);
627:     }
628:   }
629:   return(0);
630: }

632: /*@C
633:   TSSetRHSHessianProduct - Sets the function that computes the vector-Hessian-vector product. The Hessian is the second-order derivative of G (RHSFunction) w.r.t. the state variable.

635:   Logically Collective on TS

637:   Input Parameters:
638: + ts - TS context obtained from TSCreate()
639: . rhshp1 - an array of vectors storing the result of vector-Hessian-vector product for G_UU
640: . hessianproductfunc1 - vector-Hessian-vector product function for G_UU
641: . rhshp2 - an array of vectors storing the result of vector-Hessian-vector product for G_UP
642: . hessianproductfunc2 - vector-Hessian-vector product function for G_UP
643: . rhshp3 - an array of vectors storing the result of vector-Hessian-vector product for G_PU
644: . hessianproductfunc3 - vector-Hessian-vector product function for G_PU
645: . rhshp4 - an array of vectors storing the result of vector-Hessian-vector product for G_PP
646: . hessianproductfunc4 - vector-Hessian-vector product function for G_PP

648:   Calling sequence of ihessianproductfunc:
649: $ rhshessianproductfunc (TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV,void *ctx);
650: +   t - current timestep
651: .   U - input vector (current ODE solution)
652: .   Vl - an array of input vectors to be left-multiplied with the Hessian
653: .   Vr - input vector to be right-multiplied with the Hessian
654: .   VHV - an array of output vectors for vector-Hessian-vector product
655: -   ctx - [optional] user-defined function context

657:   Level: intermediate

659:   Notes:
660:   The first Hessian function and the working array are required.
661:   As an example to implement the callback functions, the second callback function calculates the vector-Hessian-vector product
662:   $ Vl_n^T*G_UP*Vr
663:   where the vector Vl_n (n-th element in the array Vl) and Vr are of size N and M respectively, and the Hessian G_UP is of size N x N x M.
664:   Each entry of G_UP corresponds to the derivative
665:   $ G_UP[i][j][k] = \frac{\partial^2 G[i]}{\partial U[j] \partial P[k]}.
666:   The result of the vector-Hessian-vector product for Vl_n needs to be stored in vector VHV_n with j-th entry being
667:   $ VHV_n[j] = \sum_i \sum_k {Vl_n[i] * G_UP[i][j][k] * Vr[k]}
668:   If the cost function is a scalar, there will be only one vector in Vl and VHV.

670: .seealso:
671: @*/
672: PetscErrorCode TSSetRHSHessianProduct(TS ts,Vec *rhshp1,PetscErrorCode (*rhshessianproductfunc1)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
673:                                           Vec *rhshp2,PetscErrorCode (*rhshessianproductfunc2)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
674:                                           Vec *rhshp3,PetscErrorCode (*rhshessianproductfunc3)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
675:                                           Vec *rhshp4,PetscErrorCode (*rhshessianproductfunc4)(TS,PetscReal,Vec,Vec*,Vec,Vec*,void*),
676:                                     void *ctx)
677: {

682:   ts->rhshessianproductctx = ctx;
683:   if (rhshp1) ts->vecs_guu = rhshp1;
684:   if (rhshp2) ts->vecs_gup = rhshp2;
685:   if (rhshp3) ts->vecs_gpu = rhshp3;
686:   if (rhshp4) ts->vecs_gpp = rhshp4;
687:   ts->rhshessianproduct_guu = rhshessianproductfunc1;
688:   ts->rhshessianproduct_gup = rhshessianproductfunc2;
689:   ts->rhshessianproduct_gpu = rhshessianproductfunc3;
690:   ts->rhshessianproduct_gpp = rhshessianproductfunc4;
691:   return(0);
692: }

694: /*@C
695:   TSComputeRHSHessianProductFunctionUU - Runs the user-defined vector-Hessian-vector product function for Guu.

697:   Collective on TS

699:   Input Parameters:
700: . ts   - The TS context obtained from TSCreate()

702:   Notes:
703:   TSComputeRHSHessianProductFunctionUU() is typically used for sensitivity implementation,
704:   so most users would not generally call this routine themselves.

706:   Level: developer

708: .seealso: TSSetRHSHessianProduct()
709: @*/
710: PetscErrorCode TSComputeRHSHessianProductFunctionUU(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
711: {

715:   if (!VHV) return(0);

719:   PetscStackPush("TS user RHSHessianProduct function 1 for sensitivity analysis");
720:   (*ts->rhshessianproduct_guu)(ts,t,U,Vl,Vr,VHV,ts->rhshessianproductctx);
721:   PetscStackPop;
722:   return(0);
723: }

725: /*@C
726:   TSComputeRHSHessianProductFunctionUP - Runs the user-defined vector-Hessian-vector product function for Gup.

728:   Collective on TS

730:   Input Parameters:
731: . ts   - The TS context obtained from TSCreate()

733:   Notes:
734:   TSComputeRHSHessianProductFunctionUP() is typically used for sensitivity implementation,
735:   so most users would not generally call this routine themselves.

737:   Level: developer

739: .seealso: TSSetRHSHessianProduct()
740: @*/
741: PetscErrorCode TSComputeRHSHessianProductFunctionUP(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
742: {

746:   if (!VHV) return(0);

750:   PetscStackPush("TS user RHSHessianProduct function 2 for sensitivity analysis");
751:   (*ts->rhshessianproduct_gup)(ts,t,U,Vl,Vr,VHV,ts->rhshessianproductctx);
752:   PetscStackPop;
753:   return(0);
754: }

756: /*@C
757:   TSComputeRHSHessianProductFunctionPU - Runs the user-defined vector-Hessian-vector product function for Gpu.

759:   Collective on TS

761:   Input Parameters:
762: . ts   - The TS context obtained from TSCreate()

764:   Notes:
765:   TSComputeRHSHessianProductFunctionPU() is typically used for sensitivity implementation,
766:   so most users would not generally call this routine themselves.

768:   Level: developer

770: .seealso: TSSetRHSHessianProduct()
771: @*/
772: PetscErrorCode TSComputeRHSHessianProductFunctionPU(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
773: {

777:   if (!VHV) return(0);

781:   PetscStackPush("TS user RHSHessianProduct function 3 for sensitivity analysis");
782:   (*ts->rhshessianproduct_gpu)(ts,t,U,Vl,Vr,VHV,ts->rhshessianproductctx);
783:   PetscStackPop;
784:   return(0);
785: }

787: /*@C
788:   TSComputeRHSHessianProductFunctionPP - Runs the user-defined vector-Hessian-vector product function for Gpp.

790:   Collective on TS

792:   Input Parameters:
793: . ts   - The TS context obtained from TSCreate()

795:   Notes:
796:   TSComputeRHSHessianProductFunctionPP() is typically used for sensitivity implementation,
797:   so most users would not generally call this routine themselves.

799:   Level: developer

801: .seealso: TSSetRHSHessianProduct()
802: @*/
803: PetscErrorCode TSComputeRHSHessianProductFunctionPP(TS ts,PetscReal t,Vec U,Vec *Vl,Vec Vr,Vec *VHV)
804: {

808:   if (!VHV) return(0);

812:   PetscStackPush("TS user RHSHessianProduct function 3 for sensitivity analysis");
813:   (*ts->rhshessianproduct_gpp)(ts,t,U,Vl,Vr,VHV,ts->rhshessianproductctx);
814:   PetscStackPop;
815:   return(0);
816: }

818: /* --------------------------- Adjoint sensitivity ---------------------------*/

820: /*@
821:    TSSetCostGradients - Sets the initial value of the gradients of the cost function w.r.t. initial values and w.r.t. the problem parameters
822:       for use by the TSAdjoint routines.

824:    Logically Collective on TS

826:    Input Parameters:
827: +  ts - the TS context obtained from TSCreate()
828: .  lambda - gradients with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
829: -  mu - gradients with respect to the parameters, the number of entries in these vectors is the same as the number of parameters

831:    Level: beginner

833:    Notes:
834:     the entries in these vectors must be correctly initialized with the values lamda_i = df/dy|finaltime  mu_i = df/dp|finaltime

836:    After TSAdjointSolve() is called the lamba and the mu contain the computed sensitivities

838: .seealso TSGetCostGradients()
839: @*/
840: PetscErrorCode TSSetCostGradients(TS ts,PetscInt numcost,Vec *lambda,Vec *mu)
841: {
845:   ts->vecs_sensi  = lambda;
846:   ts->vecs_sensip = mu;
847:   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
848:   ts->numcost  = numcost;
849:   return(0);
850: }

852: /*@
853:    TSGetCostGradients - Returns the gradients from the TSAdjointSolve()

855:    Not Collective, but Vec returned is parallel if TS is parallel

857:    Input Parameter:
858: .  ts - the TS context obtained from TSCreate()

860:    Output Parameter:
861: +  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
862: -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters

864:    Level: intermediate

866: .seealso: TSSetCostGradients()
867: @*/
868: PetscErrorCode TSGetCostGradients(TS ts,PetscInt *numcost,Vec **lambda,Vec **mu)
869: {
872:   if (numcost) *numcost = ts->numcost;
873:   if (lambda)  *lambda  = ts->vecs_sensi;
874:   if (mu)      *mu      = ts->vecs_sensip;
875:   return(0);
876: }

878: /*@
879:    TSSetCostHessianProducts - Sets the initial value of the Hessian-vector products of the cost function w.r.t. initial values and w.r.t. the problem parameters
880:       for use by the TSAdjoint routines.

882:    Logically Collective on TS

884:    Input Parameters:
885: +  ts - the TS context obtained from TSCreate()
886: .  numcost - number of cost functions
887: .  lambda2 - Hessian-vector product with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
888: .  mu2 - Hessian-vector product with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
889: -  dir - the direction vector that are multiplied with the Hessian of the cost functions

891:    Level: beginner

893:    Notes: Hessian of the cost function is completely different from Hessian of the ODE/DAE system

895:    For second-order adjoint, one needs to call this function and then TSAdjointSetForward() before TSSolve().

897:    After TSAdjointSolve() is called, the lamba2 and the mu2 will contain the computed second-order adjoint sensitivities, and can be used to produce Hessian-vector product (not the full Hessian matrix). Users must provide a direction vector; it is usually generated by an optimization solver.

899:    Passing NULL for lambda2 disables the second-order calculation.
900: .seealso: TSAdjointSetForward()
901: @*/
902: PetscErrorCode TSSetCostHessianProducts(TS ts,PetscInt numcost,Vec *lambda2,Vec *mu2,Vec dir)
903: {
906:   if (ts->numcost && ts->numcost!=numcost) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand");
907:   ts->numcost       = numcost;
908:   ts->vecs_sensi2   = lambda2;
909:   ts->vecs_sensi2p  = mu2;
910:   ts->vec_dir       = dir;
911:   return(0);
912: }

914: /*@
915:    TSGetCostHessianProducts - Returns the gradients from the TSAdjointSolve()

917:    Not Collective, but Vec returned is parallel if TS is parallel

919:    Input Parameter:
920: .  ts - the TS context obtained from TSCreate()

922:    Output Parameter:
923: +  numcost - number of cost functions
924: .  lambda2 - Hessian-vector product with respect to the initial condition variables, the dimension and parallel layout of these vectors is the same as the ODE solution vector
925: .  mu2 - Hessian-vector product with respect to the parameters, the number of entries in these vectors is the same as the number of parameters
926: -  dir - the direction vector that are multiplied with the Hessian of the cost functions

928:    Level: intermediate

930: .seealso: TSSetCostHessianProducts()
931: @*/
932: PetscErrorCode TSGetCostHessianProducts(TS ts,PetscInt *numcost,Vec **lambda2,Vec **mu2, Vec *dir)
933: {
936:   if (numcost) *numcost = ts->numcost;
937:   if (lambda2) *lambda2 = ts->vecs_sensi2;
938:   if (mu2)     *mu2     = ts->vecs_sensi2p;
939:   if (dir)     *dir     = ts->vec_dir;
940:   return(0);
941: }

943: /*@
944:   TSAdjointSetForward - Trigger the tangent linear solver and initialize the forward sensitivities

946:   Logically Collective on TS

948:   Input Parameters:
949: +  ts - the TS context obtained from TSCreate()
950: -  didp - the derivative of initial values w.r.t. parameters

952:   Level: intermediate

954:   Notes: When computing sensitivies w.r.t. initial condition, set didp to NULL so that the solver will take it as an identity matrix mathematically. TSAdjoint does not reset the tangent linear solver automatically, TSAdjointResetForward() should be called to reset the tangent linear solver.

956: .seealso: TSSetCostHessianProducts(), TSAdjointResetForward()
957: @*/
958: PetscErrorCode TSAdjointSetForward(TS ts,Mat didp)
959: {
960:   Mat            A;
961:   Vec            sp;
962:   PetscScalar    *xarr;
963:   PetscInt       lsize;

967:   ts->forward_solve = PETSC_TRUE; /* turn on tangent linear mode */
968:   if (!ts->vecs_sensi2) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Must call TSSetCostHessianProducts() first");
969:   if (!ts->vec_dir) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"Directional vector is missing. Call TSSetCostHessianProducts() to set it.");
970:   /* create a single-column dense matrix */
971:   VecGetLocalSize(ts->vec_sol,&lsize);
972:   MatCreateDense(PetscObjectComm((PetscObject)ts),lsize,PETSC_DECIDE,PETSC_DECIDE,1,NULL,&A);

974:   VecDuplicate(ts->vec_sol,&sp);
975:   MatDenseGetColumn(A,0,&xarr);
976:   VecPlaceArray(sp,xarr);
977:   if (ts->vecs_sensi2p) { /* tangent linear variable initialized as 2*dIdP*dir */
978:     if (didp) {
979:       MatMult(didp,ts->vec_dir,sp);
980:       VecScale(sp,2.);
981:     } else {
982:       VecZeroEntries(sp);
983:     }
984:   } else { /* tangent linear variable initialized as dir */
985:     VecCopy(ts->vec_dir,sp);
986:   }
987:   VecResetArray(sp);
988:   MatDenseRestoreColumn(A,&xarr);
989:   VecDestroy(&sp);

991:   TSForwardSetInitialSensitivities(ts,A); /* if didp is NULL, identity matrix is assumed */

993:   MatDestroy(&A);
994:   return(0);
995: }

997: /*@
998:   TSAdjointResetForward - Reset the tangent linear solver and destroy the tangent linear context

1000:   Logically Collective on TS

1002:   Input Parameters:
1003: .  ts - the TS context obtained from TSCreate()

1005:   Level: intermediate

1007: .seealso: TSAdjointSetForward()
1008: @*/
1009: PetscErrorCode TSAdjointResetForward(TS ts)
1010: {

1014:   ts->forward_solve = PETSC_FALSE; /* turn off tangent linear mode */
1015:   TSForwardReset(ts);
1016:   return(0);
1017: }

1019: /*@
1020:    TSAdjointSetUp - Sets up the internal data structures for the later use
1021:    of an adjoint solver

1023:    Collective on TS

1025:    Input Parameter:
1026: .  ts - the TS context obtained from TSCreate()

1028:    Level: advanced

1030: .seealso: TSCreate(), TSAdjointStep(), TSSetCostGradients()
1031: @*/
1032: PetscErrorCode TSAdjointSetUp(TS ts)
1033: {
1034:   TSTrajectory     tj;
1035:   PetscBool        match;
1036:   PetscErrorCode   ierr;

1040:   if (ts->adjointsetupcalled) return(0);
1041:   if (!ts->vecs_sensi) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetCostGradients() first");
1042:   if (ts->vecs_sensip && !ts->Jacp && !ts->Jacprhs) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_WRONGSTATE,"Must call TSSetRHSJacobianP() or TSSetIJacobianP() first");
1043:   TSGetTrajectory(ts,&tj);
1044:   PetscObjectTypeCompare((PetscObject)tj,TSTRAJECTORYBASIC,&match);
1045:   if (match) {
1046:     PetscBool solution_only;
1047:     TSTrajectoryGetSolutionOnly(tj,&solution_only);
1048:     if (solution_only) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"TSAdjoint cannot use the solution-only mode when choosing the Basic TSTrajectory type. Turn it off with -ts_trajectory_solution_only 0");
1049:   }
1050:   TSTrajectorySetUseHistory(tj,PETSC_FALSE); /* not use TSHistory */

1052:   if (ts->quadraturets) { /* if there is integral in the cost function */
1053:     VecDuplicate(ts->vecs_sensi[0],&ts->vec_drdu_col);
1054:     if (ts->vecs_sensip){
1055:       VecDuplicate(ts->vecs_sensip[0],&ts->vec_drdp_col);
1056:     }
1057:   }

1059:   if (ts->ops->adjointsetup) {
1060:     (*ts->ops->adjointsetup)(ts);
1061:   }
1062:   ts->adjointsetupcalled = PETSC_TRUE;
1063:   return(0);
1064: }

1066: /*@
1067:    TSAdjointReset - Resets a TSAdjoint context and removes any allocated Vecs and Mats.

1069:    Collective on TS

1071:    Input Parameter:
1072: .  ts - the TS context obtained from TSCreate()

1074:    Level: beginner

1076: .seealso: TSCreate(), TSAdjointSetUp(), TSADestroy()
1077: @*/
1078: PetscErrorCode TSAdjointReset(TS ts)
1079: {

1084:   if (ts->ops->adjointreset) {
1085:     (*ts->ops->adjointreset)(ts);
1086:   }
1087:   if (ts->quadraturets) { /* if there is integral in the cost function */
1088:     VecDestroy(&ts->vec_drdu_col);
1089:     if (ts->vecs_sensip){
1090:       VecDestroy(&ts->vec_drdp_col);
1091:     }
1092:   }
1093:   ts->vecs_sensi         = NULL;
1094:   ts->vecs_sensip        = NULL;
1095:   ts->vecs_sensi2        = NULL;
1096:   ts->vecs_sensi2p       = NULL;
1097:   ts->vec_dir            = NULL;
1098:   ts->adjointsetupcalled = PETSC_FALSE;
1099:   return(0);
1100: }

1102: /*@
1103:    TSAdjointSetSteps - Sets the number of steps the adjoint solver should take backward in time

1105:    Logically Collective on TS

1107:    Input Parameters:
1108: +  ts - the TS context obtained from TSCreate()
1109: -  steps - number of steps to use

1111:    Level: intermediate

1113:    Notes:
1114:     Normally one does not call this and TSAdjointSolve() integrates back to the original timestep. One can call this
1115:           so as to integrate back to less than the original timestep

1117: .seealso: TSSetExactFinalTime()
1118: @*/
1119: PetscErrorCode TSAdjointSetSteps(TS ts,PetscInt steps)
1120: {
1124:   if (steps < 0) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back a negative number of steps");
1125:   if (steps > ts->steps) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_ARG_OUTOFRANGE,"Cannot step back more than the total number of forward steps");
1126:   ts->adjoint_max_steps = steps;
1127:   return(0);
1128: }

1130: /*@C
1131:   TSAdjointSetRHSJacobian - Deprecated, use TSSetRHSJacobianP()

1133:   Level: deprecated

1135: @*/
1136: PetscErrorCode TSAdjointSetRHSJacobian(TS ts,Mat Amat,PetscErrorCode (*func)(TS,PetscReal,Vec,Mat,void*),void *ctx)
1137: {


1144:   ts->rhsjacobianp    = func;
1145:   ts->rhsjacobianpctx = ctx;
1146:   if(Amat) {
1147:     PetscObjectReference((PetscObject)Amat);
1148:     MatDestroy(&ts->Jacp);
1149:     ts->Jacp = Amat;
1150:   }
1151:   return(0);
1152: }

1154: /*@C
1155:   TSAdjointComputeRHSJacobian - Deprecated, use TSComputeRHSJacobianP()

1157:   Level: deprecated

1159: @*/
1160: PetscErrorCode TSAdjointComputeRHSJacobian(TS ts,PetscReal t,Vec U,Mat Amat)
1161: {


1169:   PetscStackPush("TS user JacobianP function for sensitivity analysis");
1170:   (*ts->rhsjacobianp)(ts,t,U,Amat,ts->rhsjacobianpctx);
1171:   PetscStackPop;
1172:   return(0);
1173: }

1175: /*@
1176:   TSAdjointComputeDRDYFunction - Deprecated, use TSGetQuadratureTS() then TSComputeRHSJacobian()

1178:   Level: deprecated

1180: @*/
1181: PetscErrorCode TSAdjointComputeDRDYFunction(TS ts,PetscReal t,Vec U,Vec *DRDU)
1182: {


1189:   PetscStackPush("TS user DRDY function for sensitivity analysis");
1190:   (*ts->drdufunction)(ts,t,U,DRDU,ts->costintegrandctx);
1191:   PetscStackPop;
1192:   return(0);
1193: }

1195: /*@
1196:   TSAdjointComputeDRDPFunction - Deprecated, use TSGetQuadratureTS() then TSComputeRHSJacobianP()

1198:   Level: deprecated

1200: @*/
1201: PetscErrorCode TSAdjointComputeDRDPFunction(TS ts,PetscReal t,Vec U,Vec *DRDP)
1202: {


1209:   PetscStackPush("TS user DRDP function for sensitivity analysis");
1210:   (*ts->drdpfunction)(ts,t,U,DRDP,ts->costintegrandctx);
1211:   PetscStackPop;
1212:   return(0);
1213: }

1215: /*@C
1216:    TSAdjointMonitorSensi - monitors the first lambda sensitivity

1218:    Level: intermediate

1220: .seealso: TSAdjointMonitorSet()
1221: @*/
1222: PetscErrorCode TSAdjointMonitorSensi(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
1223: {
1225:   PetscViewer    viewer = vf->viewer;

1229:   PetscViewerPushFormat(viewer,vf->format);
1230:   VecView(lambda[0],viewer);
1231:   PetscViewerPopFormat(viewer);
1232:   return(0);
1233: }

1235: /*@C
1236:    TSAdjointMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user

1238:    Collective on TS

1240:    Input Parameters:
1241: +  ts - TS object you wish to monitor
1242: .  name - the monitor type one is seeking
1243: .  help - message indicating what monitoring is done
1244: .  manual - manual page for the monitor
1245: .  monitor - the monitor function
1246: -  monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the TS or PetscViewer objects

1248:    Level: developer

1250: .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
1251:           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
1252:           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
1253:           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
1254:           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
1255:           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
1256:           PetscOptionsFList(), PetscOptionsEList()
1257: @*/
1258: PetscErrorCode TSAdjointMonitorSetFromOptions(TS ts,const char name[],const char help[], const char manual[],PetscErrorCode (*monitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,PetscViewerAndFormat*),PetscErrorCode (*monitorsetup)(TS,PetscViewerAndFormat*))
1259: {
1260:   PetscErrorCode    ierr;
1261:   PetscViewer       viewer;
1262:   PetscViewerFormat format;
1263:   PetscBool         flg;

1266:   PetscOptionsGetViewer(PetscObjectComm((PetscObject)ts),((PetscObject) ts)->options,((PetscObject)ts)->prefix,name,&viewer,&format,&flg);
1267:   if (flg) {
1268:     PetscViewerAndFormat *vf;
1269:     PetscViewerAndFormatCreate(viewer,format,&vf);
1270:     PetscObjectDereference((PetscObject)viewer);
1271:     if (monitorsetup) {
1272:       (*monitorsetup)(ts,vf);
1273:     }
1274:     TSAdjointMonitorSet(ts,(PetscErrorCode (*)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*))monitor,vf,(PetscErrorCode (*)(void**))PetscViewerAndFormatDestroy);
1275:   }
1276:   return(0);
1277: }

1279: /*@C
1280:    TSAdjointMonitorSet - Sets an ADDITIONAL function that is to be used at every
1281:    timestep to display the iteration's  progress.

1283:    Logically Collective on TS

1285:    Input Parameters:
1286: +  ts - the TS context obtained from TSCreate()
1287: .  adjointmonitor - monitoring routine
1288: .  adjointmctx - [optional] user-defined context for private data for the
1289:              monitor routine (use NULL if no context is desired)
1290: -  adjointmonitordestroy - [optional] routine that frees monitor context
1291:           (may be NULL)

1293:    Calling sequence of monitor:
1294: $    int adjointmonitor(TS ts,PetscInt steps,PetscReal time,Vec u,PetscInt numcost,Vec *lambda, Vec *mu,void *adjointmctx)

1296: +    ts - the TS context
1297: .    steps - iteration number (after the final time step the monitor routine is called with a step of -1, this is at the final time which may have
1298:                                been interpolated to)
1299: .    time - current time
1300: .    u - current iterate
1301: .    numcost - number of cost functionos
1302: .    lambda - sensitivities to initial conditions
1303: .    mu - sensitivities to parameters
1304: -    adjointmctx - [optional] adjoint monitoring context

1306:    Notes:
1307:    This routine adds an additional monitor to the list of monitors that
1308:    already has been loaded.

1310:    Fortran Notes:
1311:     Only a single monitor function can be set for each TS object

1313:    Level: intermediate

1315: .seealso: TSAdjointMonitorCancel()
1316: @*/
1317: PetscErrorCode TSAdjointMonitorSet(TS ts,PetscErrorCode (*adjointmonitor)(TS,PetscInt,PetscReal,Vec,PetscInt,Vec*,Vec*,void*),void *adjointmctx,PetscErrorCode (*adjointmdestroy)(void**))
1318: {
1320:   PetscInt       i;
1321:   PetscBool      identical;

1325:   for (i=0; i<ts->numbermonitors;i++) {
1326:     PetscMonitorCompare((PetscErrorCode (*)(void))adjointmonitor,adjointmctx,adjointmdestroy,(PetscErrorCode (*)(void))ts->adjointmonitor[i],ts->adjointmonitorcontext[i],ts->adjointmonitordestroy[i],&identical);
1327:     if (identical) return(0);
1328:   }
1329:   if (ts->numberadjointmonitors >= MAXTSMONITORS) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_OUTOFRANGE,"Too many adjoint monitors set");
1330:   ts->adjointmonitor[ts->numberadjointmonitors]          = adjointmonitor;
1331:   ts->adjointmonitordestroy[ts->numberadjointmonitors]   = adjointmdestroy;
1332:   ts->adjointmonitorcontext[ts->numberadjointmonitors++] = (void*)adjointmctx;
1333:   return(0);
1334: }

1336: /*@C
1337:    TSAdjointMonitorCancel - Clears all the adjoint monitors that have been set on a time-step object.

1339:    Logically Collective on TS

1341:    Input Parameters:
1342: .  ts - the TS context obtained from TSCreate()

1344:    Notes:
1345:    There is no way to remove a single, specific monitor.

1347:    Level: intermediate

1349: .seealso: TSAdjointMonitorSet()
1350: @*/
1351: PetscErrorCode TSAdjointMonitorCancel(TS ts)
1352: {
1354:   PetscInt       i;

1358:   for (i=0; i<ts->numberadjointmonitors; i++) {
1359:     if (ts->adjointmonitordestroy[i]) {
1360:       (*ts->adjointmonitordestroy[i])(&ts->adjointmonitorcontext[i]);
1361:     }
1362:   }
1363:   ts->numberadjointmonitors = 0;
1364:   return(0);
1365: }

1367: /*@C
1368:    TSAdjointMonitorDefault - the default monitor of adjoint computations

1370:    Level: intermediate

1372: .seealso: TSAdjointMonitorSet()
1373: @*/
1374: PetscErrorCode TSAdjointMonitorDefault(TS ts,PetscInt step,PetscReal ptime,Vec v,PetscInt numcost,Vec *lambda,Vec *mu,PetscViewerAndFormat *vf)
1375: {
1377:   PetscViewer    viewer = vf->viewer;

1381:   PetscViewerPushFormat(viewer,vf->format);
1382:   PetscViewerASCIIAddTab(viewer,((PetscObject)ts)->tablevel);
1383:   PetscViewerASCIIPrintf(viewer,"%D TS dt %g time %g%s",step,(double)ts->time_step,(double)ptime,ts->steprollback ? " (r)\n" : "\n");
1384:   PetscViewerASCIISubtractTab(viewer,((PetscObject)ts)->tablevel);
1385:   PetscViewerPopFormat(viewer);
1386:   return(0);
1387: }

1389: /*@C
1390:    TSAdjointMonitorDrawSensi - Monitors progress of the adjoint TS solvers by calling
1391:    VecView() for the sensitivities to initial states at each timestep

1393:    Collective on TS

1395:    Input Parameters:
1396: +  ts - the TS context
1397: .  step - current time-step
1398: .  ptime - current time
1399: .  u - current state
1400: .  numcost - number of cost functions
1401: .  lambda - sensitivities to initial conditions
1402: .  mu - sensitivities to parameters
1403: -  dummy - either a viewer or NULL

1405:    Level: intermediate

1407: .seealso: TSAdjointMonitorSet(), TSAdjointMonitorDefault(), VecView()
1408: @*/
1409: PetscErrorCode TSAdjointMonitorDrawSensi(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda,Vec *mu,void *dummy)
1410: {
1411:   PetscErrorCode   ierr;
1412:   TSMonitorDrawCtx ictx = (TSMonitorDrawCtx)dummy;
1413:   PetscDraw        draw;
1414:   PetscReal        xl,yl,xr,yr,h;
1415:   char             time[32];

1418:   if (!(((ictx->howoften > 0) && (!(step % ictx->howoften))) || ((ictx->howoften == -1) && ts->reason))) return(0);

1420:   VecView(lambda[0],ictx->viewer);
1421:   PetscViewerDrawGetDraw(ictx->viewer,0,&draw);
1422:   PetscSNPrintf(time,32,"Timestep %d Time %g",(int)step,(double)ptime);
1423:   PetscDrawGetCoordinates(draw,&xl,&yl,&xr,&yr);
1424:   h    = yl + .95*(yr - yl);
1425:   PetscDrawStringCentered(draw,.5*(xl+xr),h,PETSC_DRAW_BLACK,time);
1426:   PetscDrawFlush(draw);
1427:   return(0);
1428: }

1430: /*
1431:    TSAdjointSetFromOptions - Sets various TSAdjoint parameters from user options.

1433:    Collective on TSAdjoint

1435:    Input Parameter:
1436: .  ts - the TS context

1438:    Options Database Keys:
1439: +  -ts_adjoint_solve <yes,no> After solving the ODE/DAE solve the adjoint problem (requires -ts_save_trajectory)
1440: .  -ts_adjoint_monitor - print information at each adjoint time step
1441: -  -ts_adjoint_monitor_draw_sensi - monitor the sensitivity of the first cost function wrt initial conditions (lambda[0]) graphically

1443:    Level: developer

1445:    Notes:
1446:     This is not normally called directly by users

1448: .seealso: TSSetSaveTrajectory(), TSTrajectorySetUp()
1449: */
1450: PetscErrorCode TSAdjointSetFromOptions(PetscOptionItems *PetscOptionsObject,TS ts)
1451: {
1452:   PetscBool      tflg,opt;

1457:   PetscOptionsHead(PetscOptionsObject,"TS Adjoint options");
1458:   tflg = ts->adjoint_solve ? PETSC_TRUE : PETSC_FALSE;
1459:   PetscOptionsBool("-ts_adjoint_solve","Solve the adjoint problem immediately after solving the forward problem","",tflg,&tflg,&opt);
1460:   if (opt) {
1461:     TSSetSaveTrajectory(ts);
1462:     ts->adjoint_solve = tflg;
1463:   }
1464:   TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor","Monitor adjoint timestep size","TSAdjointMonitorDefault",TSAdjointMonitorDefault,NULL);
1465:   TSAdjointMonitorSetFromOptions(ts,"-ts_adjoint_monitor_sensi","Monitor sensitivity in the adjoint computation","TSAdjointMonitorSensi",TSAdjointMonitorSensi,NULL);
1466:   opt  = PETSC_FALSE;
1467:   PetscOptionsName("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",&opt);
1468:   if (opt) {
1469:     TSMonitorDrawCtx ctx;
1470:     PetscInt         howoften = 1;

1472:     PetscOptionsInt("-ts_adjoint_monitor_draw_sensi","Monitor adjoint sensitivities (lambda only) graphically","TSAdjointMonitorDrawSensi",howoften,&howoften,NULL);
1473:     TSMonitorDrawCtxCreate(PetscObjectComm((PetscObject)ts),0,0,PETSC_DECIDE,PETSC_DECIDE,300,300,howoften,&ctx);
1474:     TSAdjointMonitorSet(ts,TSAdjointMonitorDrawSensi,ctx,(PetscErrorCode (*)(void**))TSMonitorDrawCtxDestroy);
1475:   }
1476:   return(0);
1477: }

1479: /*@
1480:    TSAdjointStep - Steps one time step backward in the adjoint run

1482:    Collective on TS

1484:    Input Parameter:
1485: .  ts - the TS context obtained from TSCreate()

1487:    Level: intermediate

1489: .seealso: TSAdjointSetUp(), TSAdjointSolve()
1490: @*/
1491: PetscErrorCode TSAdjointStep(TS ts)
1492: {
1493:   DM               dm;
1494:   PetscErrorCode   ierr;

1498:   TSGetDM(ts,&dm);
1499:   TSAdjointSetUp(ts);

1501:   ts->reason = TS_CONVERGED_ITERATING;
1502:   ts->ptime_prev = ts->ptime;
1503:   if (!ts->ops->adjointstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSStep has failed because the adjoint of  %s has not been implemented, try other time stepping methods for adjoint sensitivity analysis",((PetscObject)ts)->type_name);
1504:   PetscLogEventBegin(TS_AdjointStep,ts,0,0,0);
1505:   (*ts->ops->adjointstep)(ts);
1506:   PetscLogEventEnd(TS_AdjointStep,ts,0,0,0);
1507:   ts->adjoint_steps++; ts->steps--;

1509:   if (ts->reason < 0) {
1510:     if (ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSAdjointStep has failed due to %s",TSConvergedReasons[ts->reason]);
1511:   } else if (!ts->reason) {
1512:     if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;
1513:   }
1514:   return(0);
1515: }

1517: /*@
1518:    TSAdjointSolve - Solves the discrete ajoint problem for an ODE/DAE

1520:    Collective on TS

1522:    Input Parameter:
1523: .  ts - the TS context obtained from TSCreate()

1525:    Options Database:
1526: . -ts_adjoint_view_solution <viewerinfo> - views the first gradient with respect to the initial values

1528:    Level: intermediate

1530:    Notes:
1531:    This must be called after a call to TSSolve() that solves the forward problem

1533:    By default this will integrate back to the initial time, one can use TSAdjointSetSteps() to step back to a later time

1535: .seealso: TSCreate(), TSSetCostGradients(), TSSetSolution(), TSAdjointStep()
1536: @*/
1537: PetscErrorCode TSAdjointSolve(TS ts)
1538: {
1539: #if defined(TSADJOINT_STAGE)
1540:   PetscLogStage  adjoint_stage;
1541: #endif

1546: #if defined(TSADJOINT_STAGE)
1547:   PetscLogStageRegister("TSAdjoint",&adjoint_stage);
1548:   PetscLogStagePush(adjoint_stage);
1549: #endif
1550:   TSAdjointSetUp(ts);

1552:   /* reset time step and iteration counters */
1553:   ts->adjoint_steps     = 0;
1554:   ts->ksp_its           = 0;
1555:   ts->snes_its          = 0;
1556:   ts->num_snes_failures = 0;
1557:   ts->reject            = 0;
1558:   ts->reason            = TS_CONVERGED_ITERATING;

1560:   if (!ts->adjoint_max_steps) ts->adjoint_max_steps = ts->steps;
1561:   if (ts->adjoint_steps >= ts->adjoint_max_steps) ts->reason = TS_CONVERGED_ITS;

1563:   while (!ts->reason) {
1564:     TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);
1565:     TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);
1566:     TSAdjointEventHandler(ts);
1567:     TSAdjointStep(ts);
1568:     if ((ts->vec_costintegral || ts->quadraturets) && !ts->costintegralfwd) {
1569:       TSAdjointCostIntegral(ts);
1570:     }
1571:   }
1572:   TSTrajectoryGet(ts->trajectory,ts,ts->steps,&ts->ptime);
1573:   TSAdjointMonitor(ts,ts->steps,ts->ptime,ts->vec_sol,ts->numcost,ts->vecs_sensi,ts->vecs_sensip);
1574:   ts->solvetime = ts->ptime;
1575:   TSTrajectoryViewFromOptions(ts->trajectory,NULL,"-ts_trajectory_view");
1576:   VecViewFromOptions(ts->vecs_sensi[0],(PetscObject) ts, "-ts_adjoint_view_solution");
1577:   ts->adjoint_max_steps = 0;
1578: #if defined(TSADJOINT_STAGE)
1579:   PetscLogStagePop();
1580: #endif
1581:   return(0);
1582: }

1584: /*@C
1585:    TSAdjointMonitor - Runs all user-provided adjoint monitor routines set using TSAdjointMonitorSet()

1587:    Collective on TS

1589:    Input Parameters:
1590: +  ts - time stepping context obtained from TSCreate()
1591: .  step - step number that has just completed
1592: .  ptime - model time of the state
1593: .  u - state at the current model time
1594: .  numcost - number of cost functions (dimension of lambda  or mu)
1595: .  lambda - vectors containing the gradients of the cost functions with respect to the ODE/DAE solution variables
1596: -  mu - vectors containing the gradients of the cost functions with respect to the problem parameters

1598:    Notes:
1599:    TSAdjointMonitor() is typically used automatically within the time stepping implementations.
1600:    Users would almost never call this routine directly.

1602:    Level: developer

1604: @*/
1605: PetscErrorCode TSAdjointMonitor(TS ts,PetscInt step,PetscReal ptime,Vec u,PetscInt numcost,Vec *lambda, Vec *mu)
1606: {
1608:   PetscInt       i,n = ts->numberadjointmonitors;

1613:   VecLockReadPush(u);
1614:   for (i=0; i<n; i++) {
1615:     (*ts->adjointmonitor[i])(ts,step,ptime,u,numcost,lambda,mu,ts->adjointmonitorcontext[i]);
1616:   }
1617:   VecLockReadPop(u);
1618:   return(0);
1619: }

1621: /*@
1622:  TSAdjointCostIntegral - Evaluate the cost integral in the adjoint run.

1624:  Collective on TS

1626:  Input Arguments:
1627:  .  ts - time stepping context

1629:  Level: advanced

1631:  Notes:
1632:  This function cannot be called until TSAdjointStep() has been completed.

1634:  .seealso: TSAdjointSolve(), TSAdjointStep
1635:  @*/
1636: PetscErrorCode TSAdjointCostIntegral(TS ts)
1637: {
1640:     if (!ts->ops->adjointintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the adjoint run",((PetscObject)ts)->type_name);
1641:     (*ts->ops->adjointintegral)(ts);
1642:     return(0);
1643: }

1645: /* ------------------ Forward (tangent linear) sensitivity  ------------------*/

1647: /*@
1648:   TSForwardSetUp - Sets up the internal data structures for the later use
1649:   of forward sensitivity analysis

1651:   Collective on TS

1653:   Input Parameter:
1654: . ts - the TS context obtained from TSCreate()

1656:   Level: advanced

1658: .seealso: TSCreate(), TSDestroy(), TSSetUp()
1659: @*/
1660: PetscErrorCode TSForwardSetUp(TS ts)
1661: {

1666:   if (ts->forwardsetupcalled) return(0);
1667:   if (ts->ops->forwardsetup) {
1668:     (*ts->ops->forwardsetup)(ts);
1669:   }
1670:   VecDuplicate(ts->vec_sol,&ts->vec_sensip_col);
1671:   ts->forwardsetupcalled = PETSC_TRUE;
1672:   return(0);
1673: }

1675: /*@
1676:   TSForwardReset - Reset the internal data structures used by forward sensitivity analysis

1678:   Collective on TS

1680:   Input Parameter:
1681: . ts - the TS context obtained from TSCreate()

1683:   Level: advanced

1685: .seealso: TSCreate(), TSDestroy(), TSForwardSetUp()
1686: @*/
1687: PetscErrorCode TSForwardReset(TS ts)
1688: {
1689:   TS             quadts = ts->quadraturets;

1694:   if (ts->ops->forwardreset) {
1695:     (*ts->ops->forwardreset)(ts);
1696:   }
1697:   MatDestroy(&ts->mat_sensip);
1698:   if (quadts) {
1699:     MatDestroy(&quadts->mat_sensip);
1700:   }
1701:   VecDestroy(&ts->vec_sensip_col);
1702:   ts->forward_solve      = PETSC_FALSE;
1703:   ts->forwardsetupcalled = PETSC_FALSE;
1704:   return(0);
1705: }

1707: /*@
1708:   TSForwardSetIntegralGradients - Set the vectors holding forward sensitivities of the integral term.

1710:   Input Parameter:
1711: + ts- the TS context obtained from TSCreate()
1712: . numfwdint- number of integrals
1713: - vp = the vectors containing the gradients for each integral w.r.t. parameters

1715:   Level: deprecated

1717: .seealso: TSForwardGetSensitivities(), TSForwardSetIntegralGradients(), TSForwardGetIntegralGradients(), TSForwardStep()
1718: @*/
1719: PetscErrorCode TSForwardSetIntegralGradients(TS ts,PetscInt numfwdint,Vec *vp)
1720: {
1723:   if (ts->numcost && ts->numcost!=numfwdint) SETERRQ(PetscObjectComm((PetscObject)ts),PETSC_ERR_USER,"The number of cost functions (2rd parameter of TSSetCostIntegrand()) is inconsistent with the one set by TSSetCostIntegrand()");
1724:   if (!ts->numcost) ts->numcost = numfwdint;

1726:   ts->vecs_integral_sensip = vp;
1727:   return(0);
1728: }

1730: /*@
1731:   TSForwardGetIntegralGradients - Returns the forward sensitivities ofthe integral term.

1733:   Input Parameter:
1734: . ts- the TS context obtained from TSCreate()

1736:   Output Parameter:
1737: . vp = the vectors containing the gradients for each integral w.r.t. parameters

1739:   Level: deprecated

1741: .seealso: TSForwardSetSensitivities(), TSForwardSetIntegralGradients(), TSForwardGetIntegralGradients(), TSForwardStep()
1742: @*/
1743: PetscErrorCode TSForwardGetIntegralGradients(TS ts,PetscInt *numfwdint,Vec **vp)
1744: {
1748:   if (numfwdint) *numfwdint = ts->numcost;
1749:   if (vp) *vp = ts->vecs_integral_sensip;
1750:   return(0);
1751: }

1753: /*@
1754:   TSForwardStep - Compute the forward sensitivity for one time step.

1756:   Collective on TS

1758:   Input Arguments:
1759: . ts - time stepping context

1761:   Level: advanced

1763:   Notes:
1764:   This function cannot be called until TSStep() has been completed.

1766: .seealso: TSForwardSetSensitivities(), TSForwardGetSensitivities(), TSForwardSetIntegralGradients(), TSForwardGetIntegralGradients(), TSForwardSetUp()
1767: @*/
1768: PetscErrorCode TSForwardStep(TS ts)
1769: {
1772:   if (!ts->ops->forwardstep) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide forward sensitivity analysis",((PetscObject)ts)->type_name);
1773:   PetscLogEventBegin(TS_ForwardStep,ts,0,0,0);
1774:   (*ts->ops->forwardstep)(ts);
1775:   PetscLogEventEnd(TS_ForwardStep,ts,0,0,0);
1776:   if (ts->reason < 0 && ts->errorifstepfailed) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_NOT_CONVERGED,"TSFowardStep has failed due to %s",TSConvergedReasons[ts->reason]);
1777:   return(0);
1778: }

1780: /*@
1781:   TSForwardSetSensitivities - Sets the initial value of the trajectory sensitivities of solution  w.r.t. the problem parameters and initial values.

1783:   Logically Collective on TS

1785:   Input Parameters:
1786: + ts - the TS context obtained from TSCreate()
1787: . nump - number of parameters
1788: - Smat - sensitivities with respect to the parameters, the number of entries in these vectors is the same as the number of parameters

1790:   Level: beginner

1792:   Notes:
1793:   Forward sensitivity is also called 'trajectory sensitivity' in some fields such as power systems.
1794:   This function turns on a flag to trigger TSSolve() to compute forward sensitivities automatically.
1795:   You must call this function before TSSolve().
1796:   The entries in the sensitivity matrix must be correctly initialized with the values S = dy/dp|startingtime.

1798: .seealso: TSForwardGetSensitivities(), TSForwardSetIntegralGradients(), TSForwardGetIntegralGradients(), TSForwardStep()
1799: @*/
1800: PetscErrorCode TSForwardSetSensitivities(TS ts,PetscInt nump,Mat Smat)
1801: {

1807:   ts->forward_solve  = PETSC_TRUE;
1808:   if (nump == PETSC_DEFAULT) {
1809:     MatGetSize(Smat,NULL,&ts->num_parameters);
1810:   } else ts->num_parameters = nump;
1811:   PetscObjectReference((PetscObject)Smat);
1812:   MatDestroy(&ts->mat_sensip);
1813:   ts->mat_sensip = Smat;
1814:   return(0);
1815: }

1817: /*@
1818:   TSForwardGetSensitivities - Returns the trajectory sensitivities

1820:   Not Collective, but Vec returned is parallel if TS is parallel

1822:   Output Parameter:
1823: + ts - the TS context obtained from TSCreate()
1824: . nump - number of parameters
1825: - Smat - sensitivities with respect to the parameters, the number of entries in these vectors is the same as the number of parameters

1827:   Level: intermediate

1829: .seealso: TSForwardSetSensitivities(), TSForwardSetIntegralGradients(), TSForwardGetIntegralGradients(), TSForwardStep()
1830: @*/
1831: PetscErrorCode TSForwardGetSensitivities(TS ts,PetscInt *nump,Mat *Smat)
1832: {
1835:   if (nump) *nump = ts->num_parameters;
1836:   if (Smat) *Smat = ts->mat_sensip;
1837:   return(0);
1838: }

1840: /*@
1841:    TSForwardCostIntegral - Evaluate the cost integral in the forward run.

1843:    Collective on TS

1845:    Input Arguments:
1846: .  ts - time stepping context

1848:    Level: advanced

1850:    Notes:
1851:    This function cannot be called until TSStep() has been completed.

1853: .seealso: TSSolve(), TSAdjointCostIntegral()
1854: @*/
1855: PetscErrorCode TSForwardCostIntegral(TS ts)
1856: {
1859:   if (!ts->ops->forwardintegral) SETERRQ1(PetscObjectComm((PetscObject)ts),PETSC_ERR_SUP,"%s does not provide integral evaluation in the forward run",((PetscObject)ts)->type_name);
1860:   (*ts->ops->forwardintegral)(ts);
1861:   return(0);
1862: }

1864: /*@
1865:   TSForwardSetInitialSensitivities - Set initial values for tangent linear sensitivities

1867:   Collective on TS

1869:   Input Parameter
1870: + ts - the TS context obtained from TSCreate()
1871: - didp - parametric sensitivities of the initial condition

1873:   Level: intermediate

1875:   Notes: TSSolve() allows users to pass the initial solution directly to TS. But the tangent linear variables cannot be initialized in this way. This function is used to set initial values for tangent linear variables.

1877: .seealso: TSForwardSetSensitivities()
1878: @*/
1879: PetscErrorCode TSForwardSetInitialSensitivities(TS ts,Mat didp)
1880: {

1885:   if (!ts->mat_sensip) {
1886:     TSForwardSetSensitivities(ts,PETSC_DEFAULT,didp);
1887:   }
1888:   return(0);
1889: }

1891: /*@
1892:    TSForwardGetStages - Get the number of stages and the tangent linear sensitivities at the intermediate stages

1894:    Input Parameter:
1895: .  ts - the TS context obtained from TSCreate()

1897:    Output Parameters:
1898: +  ns - number of stages
1899: -  S - tangent linear sensitivities at the intermediate stages

1901:    Level: advanced

1903: @*/
1904: PetscErrorCode TSForwardGetStages(TS ts,PetscInt *ns,Mat **S)
1905: {


1911:   if (!ts->ops->getstages) *S=NULL;
1912:   else {
1913:     (*ts->ops->forwardgetstages)(ts,ns,S);
1914:   }
1915:   return(0);
1916: }

1918: /*@
1919:    TSCreateQuadratureTS - Create a sub-TS that evaluates integrals over time

1921:    Input Parameter:
1922: +  ts - the TS context obtained from TSCreate()
1923: -  fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run

1925:    Output Parameters:
1926: .  quadts - the child TS context

1928:    Level: intermediate

1930: .seealso: TSGetQuadratureTS()
1931: @*/
1932: PetscErrorCode TSCreateQuadratureTS(TS ts,PetscBool fwd,TS *quadts)
1933: {
1934:   char prefix[128];

1940:   TSDestroy(&ts->quadraturets);
1941:   TSCreate(PetscObjectComm((PetscObject)ts),&ts->quadraturets);
1942:   PetscObjectIncrementTabLevel((PetscObject)ts->quadraturets,(PetscObject)ts,1);
1943:   PetscLogObjectParent((PetscObject)ts,(PetscObject)ts->quadraturets);
1944:   PetscSNPrintf(prefix,sizeof(prefix),"%squad_",((PetscObject)ts)->prefix ? ((PetscObject)ts)->prefix : "");
1945:   TSSetOptionsPrefix(ts->quadraturets,prefix);
1946:   *quadts = ts->quadraturets;

1948:   if (ts->numcost) {
1949:     VecCreateSeq(PETSC_COMM_SELF,ts->numcost,&(*quadts)->vec_sol);
1950:   } else {
1951:     VecCreateSeq(PETSC_COMM_SELF,1,&(*quadts)->vec_sol);
1952:   }
1953:   ts->costintegralfwd = fwd;
1954:   return(0);
1955: }

1957: /*@
1958:    TSGetQuadratureTS - Return the sub-TS that evaluates integrals over time

1960:    Input Parameter:
1961: .  ts - the TS context obtained from TSCreate()

1963:    Output Parameters:
1964: +  fwd - flag indicating whether to evaluate cost integral in the forward run or the adjoint run
1965: -  quadts - the child TS context

1967:    Level: intermediate

1969: .seealso: TSCreateQuadratureTS()
1970: @*/
1971: PetscErrorCode TSGetQuadratureTS(TS ts,PetscBool *fwd,TS *quadts)
1972: {
1975:   if (fwd) *fwd = ts->costintegralfwd;
1976:   if (quadts) *quadts = ts->quadraturets;
1977:   return(0);
1978: }

1980: /*@
1981:    TSComputeSNESJacobian - Compute the SNESJacobian

1983:    Input Parameters:
1984: +  ts - the TS context obtained from TSCreate()
1985: -  x - state vector

1987:    Output Parameters:
1988: +  J - Jacobian matrix
1989: -  Jpre - preconditioning matrix for J (may be same as J)

1991:    Level: developer

1993:    Notes:
1994:    Using SNES to compute the Jacobian enables finite differencing when TS Jacobian is not available.
1995: @*/
1996: PetscErrorCode TSComputeSNESJacobian(TS ts,Vec x,Mat J,Mat Jpre)
1997: {
1998:   SNES           snes = ts->snes;
1999:   PetscErrorCode (*jac)(SNES,Vec,Mat,Mat,void*) = NULL;

2003:   /*
2004:     Unlike implicit methods, explicit methods do not have SNESMatFDColoring in the snes object
2005:     because SNESSolve() has not been called yet; so querying SNESMatFDColoring does not work for
2006:     explicit methods. Instead, we check the Jacobian compute function directly to determin if FD
2007:     coloring is used.
2008:   */
2009:   SNESGetJacobian(snes,NULL,NULL,&jac,NULL);
2010:   if (jac == SNESComputeJacobianDefaultColor) {
2011:     Vec f;
2012:     SNESSetSolution(snes,x);
2013:     SNESGetFunction(snes,&f,NULL,NULL);
2014:     /* Force MatFDColoringApply to evaluate the SNES residual function for the base vector */
2015:     SNESComputeFunction(snes,x,f);
2016:   }
2017:   SNESComputeJacobian(snes,x,J,Jpre);
2018:   return(0);
2019: }