<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">
<br class="">
<div><br class="">
<blockquote type="cite" class="">
<div class="">On Dec 28, 2020, at 9:31 PM, Salazar De Troya, Miguel <<a href="mailto:salazardetro1@llnl.gov" class="">salazardetro1@llnl.gov</a>> wrote:</div>
<br class="Apple-interchange-newline">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Verdana; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Hello,<o:p class=""></o:p></div>
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Thanks for your response, Hong. I see that all cost functionals are evaluated in a single backward run.</div>
</div>
</div>
</blockquote>
<div><br class="">
</div>
<div>All gradients, not necessarily the cost functionals.</div>
<br class="">
<blockquote type="cite" class="">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Verdana; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
However, I want to do it separately. I want to isolate the evaluation of the gradients for each cost functional.</div>
</div>
</div>
</blockquote>
<div><br class="">
</div>
<div>What is the motivation of doing multiple TSAdjointSolve() calls in your case? Note that evaluating the gradients in one call is more efficient because you do not have to load the same checkpoints multiple times. </div>
<br class="">
<blockquote type="cite" class="">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Verdana; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Can you please elaborate on how to reuse the trajectory for multiple calls? Specifically, how to set the trajectory back to the end so I can call TSAdjoint() again?</div>
</div>
</div>
</blockquote>
<div><br class="">
</div>
<div>This is the last thing you want to do. Before each adjoint run, you can reset TS into the same state as when the forward run ends by specifying the final time, the step size and the step number. You will be limited to use disk (default option) for checkpointing.
 Here is an example modified from ex20adj.c:</div>
<div><br class="">
</div>
<div>
<div>diff --git a/src/ts/tutorials/ex20adj.c b/src/ts/tutorials/ex20adj.c</div>
<div>index 8ca9e0b7ba..e185bc4721 100644</div>
<div>--- a/src/ts/tutorials/ex20adj.c</div>
<div>+++ b/src/ts/tutorials/ex20adj.c</div>
<div>@@ -277,6 +277,10 @@ int main(int argc,char **argv)</div>
<div>   ierr = TSGetSolveTime(ts,&user.ftime);CHKERRQ(ierr);</div>
<div>   ierr = TSGetStepNumber(ts,&user.steps);CHKERRQ(ierr);</div>
<div><br class="">
</div>
<div>+  for (PetscInt iter=1; iter<3; iter++) {</div>
<div>+    ierr = TSSetTime(ts,user.ftime);CHKERRQ(ierr);</div>
<div>+    ierr = TSSetTimeStep(ts,0.001);CHKERRQ(ierr);</div>
<div>+    ierr = TSSetStepNumber(ts,user.steps);CHKERRQ(ierr);</div>
<div>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</div>
<div>      Adjoint model starts here</div>
<div>      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */</div>
<div>@@ -321,7 +325,7 @@ int main(int argc,char **argv)</div>
<div>   ierr = VecRestoreArray(user.mup[1],&x_ptr);CHKERRQ(ierr);</div>
<div>   ierr = VecRestoreArray(user.lambda[1],&y_ptr);CHKERRQ(ierr);</div>
<div>   ierr = PetscPrintf(PETSC_COMM_WORLD,"\n sensivitity wrt parameters: d[z(tf)]/d[mu]\n%g\n",(double)PetscRealPart(derp));CHKERRQ(ierr);</div>
<div>-</div>
<div>+  }</div>
<div>   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -</div>
<div>      Free work space.  All PETSc objects should be destroyed when they</div>
<div>      are no longer needed.</div>
<div><br class="">
</div>
<div>Hong (Mr.)</div>
</div>
<br class="">
<blockquote type="cite" class="">
<div class="">
<div class="WordSection1" style="page: WordSection1; caret-color: rgb(0, 0, 0); font-family: Verdana; font-size: 14px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration: none;">
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""></o:p></div>
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div style="margin: 0in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Miguel<o:p class=""></o:p></div>
<div style="border-style: solid none none; border-top-width: 1pt; border-top-color: rgb(181, 196, 223); padding: 3pt 0in 0in;" class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<b class=""><span style="font-size: 12pt;" class="">From:<span class="Apple-converted-space"> </span></span></b><span style="font-size: 12pt;" class="">"Zhang, Hong" <<a href="mailto:hongzhang@anl.gov" class="">hongzhang@anl.gov</a>><br class="">
<b class="">Date:<span class="Apple-converted-space"> </span></b>Monday, December 28, 2020 at 6:16 PM<br class="">
<b class="">To:<span class="Apple-converted-space"> </span></b>"Salazar De Troya, Miguel" <<a href="mailto:salazardetro1@llnl.gov" class="">salazardetro1@llnl.gov</a>><br class="">
<b class="">Cc:<span class="Apple-converted-space"> </span></b>"Salazar De Troya, Miguel via petsc-users" <<a href="mailto:petsc-users@mcs.anl.gov" class="">petsc-users@mcs.anl.gov</a>><br class="">
<b class="">Subject:<span class="Apple-converted-space"> </span></b>Re: [petsc-users] Calculating adjoint of more than one cost function separately<o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<br class="">
<br class="">
<o:p class=""></o:p></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
On Dec 27, 2020, at 5:01 PM, Salazar De Troya, Miguel via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" style="color: blue; text-decoration: underline;" class="">petsc-users@mcs.anl.gov</a>> wrote:<o:p class=""></o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Hello,<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
I am interested in calculating the gradients of an optimization problem with one goal and one constraint functions which need TSAdjoint for their adjoints. I’d like to call each of their adjoints in different calls, but it does not seem to be possible without
 making compromises.<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
If you are calculating the derivatives to the same set of parameters, the adjoints of all cost functionals can be done with a single backward run. <o:p class=""></o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<br class="">
<br class="">
<o:p class=""></o:p></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
For instance, one could set<span class="apple-converted-space"> </span>TSCreateQuadratureTS() and TSSetCostGradients() with different quadratures (and their gradients) for each adjoint call (one at a time). This would evaluate the cost functions in the backwards
 run though, whereas one typically computes the cost functions in a different routine than the adjoint call (like in line searches evaluations)<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
The second argument of TSCreateQuadratureTS() allows you to choose if the quadrature is evaluated in the forward run or in the backward run.  The choice typically depends on the optimization algorithms. Some optimization algorithms may expect users to provide
 an objective function and its gradient as a bundle; in this case, the choice does not make a difference. Some other algorithms may occasionally evaluate the objective function without evaluating its gradient, then evaluating the quadrature in the forward run
 is definitely a better choice.<br class="">
 <o:p class=""></o:p></div>
</div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
One could also set TSCreateQuadratureTS() with the goal and the constraint functions to be evaluated at the forward run (as typically done when computing the cost function). The problem would be that the adjoint call now requires two sets of gradients for TSSetCostGradients()
 and their adjoint are calculated together, costing twice if your routines for the cost and the constraint gradients are separated.<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
You can put the two sets of gradients in vector arrays and pass them to TSSetCostGradients() together. Only one call to TSAdjointSolve() is needed. See the example src/ts/tutorials/ex20adj.c, where we have two independent cost functionals, and their adjoints
 correspond to lambda[0]/mup[0] and lambda[1]/mup[1] respectively. After performing a TSAdjontSolve, you will get the gradients for both cost functionals.<o:p class=""></o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<br class="">
<br class="">
<o:p class=""></o:p></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
The only solution I can think of is to set TSCreateQuadratureTS() with both the goal and constraint functions in the forward run. Then, in the adjoint calls, reset TSCreateQuadratureTS() with just the cost function I am interested in (either the goal or the
 constraint) and set just a single TSSetCostGradients(). Will this work? Are there better alternatives?<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
TSCreateQuadratureTS() is needed only when you have integral terms in the cost functionals. It has nothing to do with the procedure to compute the adjoints for multiple cost functionals simultaneously. Do you have integrals in both the goal and the constraint?
 If so, you can create one quadrature TS and evaluate both integrals together. For example, you may have r[0] (the first element of the output vector in your cost integrand) for the goal and r[1] for the constraint. Just be careful that the adjoint variables
 (array lambda[]/mup[]) should be organized in the same order.<br class="">
<br class="">
<br class="">
<o:p class=""></o:p></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Even if successful, there is the problem that the trajectory goes back to the beginning when we perform a TSAdjointSolve() call. Subsequent calls to TSAdjointSolve() (for instance for another cost function) are invalid because the trajectory is not set at the
 end of the simulation. One needs to call the forward problem to bring it back to the end. Is there a quick way to set the trajectory state to the last time step without having to run the forward problem? I am attaching an example to illustrate this issue.
 One can uncomment lines 120-122 to obtain the right value of the derivative.<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
</blockquote>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Most likely you need only one call to TSAdjointSolve(). Reusing the trajectory for multiple calls is also doable. But I doubt you would need it.<o:p class=""></o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<o:p class=""> </o:p></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Hong (Mr.)<o:p class=""></o:p></div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<br class="">
<br class="">
<o:p class=""></o:p></div>
<blockquote style="margin-top: 5pt; margin-bottom: 5pt;" class="" type="cite">
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Thanks<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
Miguel<span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
 <span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span lang="ES" style="font-size: 9pt; font-family: Consolas;" class="">Miguel A. Salazar de Troya</span><span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 9pt; font-family: Consolas;" class="">Postdoctoral Researcher, Lawrence Livermore National Laboratory</span><span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 9pt; font-family: Consolas;" class="">B141</span><span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 9pt; font-family: Consolas;" class="">Rm: 1085-5</span><span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
<div class="">
<div class="">
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<span style="font-size: 9pt; font-family: Consolas;" class="">Ph: 1(925) 422-6411</span><span style="font-size: 12pt;" class=""><o:p class=""></o:p></span></div>
</div>
</div>
<div style="margin: 0in 0in 0in 0.5in; font-size: 11pt; font-family: Calibri, sans-serif;" class="">
<simple-ode.py></div>
</div>
</blockquote>
</div>
</div>
</div>
</blockquote>
</div>
<br class="">
</body>
</html>