<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="Generator" content="Microsoft Word 15 (filtered medium)">
<style><!--
/* Font Definitions */
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:Consolas;
        panose-1:2 11 6 9 2 2 4 3 2 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:11.0pt;
        font-family:"Calibri",sans-serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
span.apple-converted-space
        {mso-style-name:apple-converted-space;}
span.EmailStyle19
        {mso-style-type:personal-reply;
        font-family:"Calibri",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
--></style>
</head>
<body lang="EN-US" link="blue" vlink="purple" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal">Hi Hong,<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">I wanted to have separate calls to TSAdjointSolve() for each cost functional just for design purposes (separation of concerns). In pyadjoint, there is the ReducedFunctional object that encapsulates the functionality of a single cost functional
 and its derivative. Now I understand that there is a very compelling reason to actually calculate all cost functional gradients together (saving on checkpoint loadings). Thanks for clarifying that. I will work with that in mind from now on.<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<p class="MsoNormal">Best,<o:p></o:p></p>
<p class="MsoNormal">Miguel<o:p></o:p></p>
<p class="MsoNormal"><o:p> </o:p></p>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-left:.5in"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">"Zhang, Hong" <hongzhang@anl.gov><br>
<b>Date: </b>Monday, December 28, 2020 at 8:43 PM<br>
<b>To: </b>"Salazar De Troya, Miguel" <salazardetro1@llnl.gov><br>
<b>Cc: </b>"Salazar De Troya, Miguel via petsc-users" <petsc-users@mcs.anl.gov><br>
<b>Subject: </b>Re: [petsc-users] Calculating adjoint of more than one cost function separately<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div>
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<p class="MsoNormal" style="margin-left:.5in">On Dec 28, 2020, at 9:31 PM, Salazar De Troya, Miguel <<a href="mailto:salazardetro1@llnl.gov">salazardetro1@llnl.gov</a>> wrote:<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Hello,<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Thanks for your response, Hong. I see that all cost functionals are evaluated in a single backward run.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">All gradients, not necessarily the cost functionals.<o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">However, I want to do it separately. I want to isolate the evaluation of the gradients for each cost functional.<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">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. <o:p></o:p></p>
</div>
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">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?<o:p></o:p></p>
</div>
</div>
</blockquote>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">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:<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in">diff --git a/src/ts/tutorials/ex20adj.c b/src/ts/tutorials/ex20adj.c<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">index 8ca9e0b7ba..e185bc4721 100644<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">--- a/src/ts/tutorials/ex20adj.c<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+++ b/src/ts/tutorials/ex20adj.c<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">@@ -277,6 +277,10 @@ int main(int argc,char **argv)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   ierr = TSGetSolveTime(ts,&user.ftime);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   ierr = TSGetStepNumber(ts,&user.steps);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+  for (PetscInt iter=1; iter<3; iter++) {<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+    ierr = TSSetTime(ts,user.ftime);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+    ierr = TSSetTimeStep(ts,0.001);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+    ierr = TSSetStepNumber(ts,user.steps);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">      Adjoint model starts here<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">      - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">@@ -321,7 +325,7 @@ int main(int argc,char **argv)<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   ierr = VecRestoreArray(user.mup[1],&x_ptr);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   ierr = VecRestoreArray(user.lambda[1],&y_ptr);CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   ierr = PetscPrintf(PETSC_COMM_WORLD,"\n sensivitity wrt parameters: d[z(tf)]/d[mu]\n%g\n",(double)PetscRealPart(derp));CHKERRQ(ierr);<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">-<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">+  }<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">   /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">      Free work space.  All PETSc objects should be destroyed when they<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">      are no longer needed.<o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in"><o:p> </o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Hong (Mr.)<o:p></o:p></p>
</div>
</div>
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<o:p></o:p></p>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
<div>
<p class="MsoNormal" style="margin-left:.5in">Miguel<o:p></o:p></p>
</div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><b><span style="font-size:12.0pt">From:<span class="apple-converted-space"> </span></span></b><span style="font-size:12.0pt">"Zhang, Hong" <<a href="mailto:hongzhang@anl.gov">hongzhang@anl.gov</a>><br>
<b>Date:<span class="apple-converted-space"> </span></b>Monday, December 28, 2020 at 6:16 PM<br>
<b>To:<span class="apple-converted-space"> </span></b>"Salazar De Troya, Miguel" <<a href="mailto:salazardetro1@llnl.gov">salazardetro1@llnl.gov</a>><br>
<b>Cc:<span class="apple-converted-space"> </span></b>"Salazar De Troya, Miguel via petsc-users" <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>><br>
<b>Subject:<span class="apple-converted-space"> </span></b>Re: [petsc-users] Calculating adjoint of more than one cost function separately</span><o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<br>
<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">On Dec 27, 2020, at 5:01 PM, Salazar De Troya, Miguel via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov">petsc-users@mcs.anl.gov</a>> wrote:<o:p></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">Hello,<o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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.<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<br>
<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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)<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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>
 <o:p></o:p></p>
</div>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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.<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<br>
<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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?<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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>
<br>
<br>
<br>
<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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.<o:p></o:p></p>
</div>
</div>
</div>
</blockquote>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">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></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">Hong (Mr.)<o:p></o:p></p>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<br>
<o:p></o:p></p>
</div>
<blockquote style="margin-top:5.0pt;margin-bottom:5.0pt">
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">Thanks<o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in">Miguel<o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"> <o:p></o:p></p>
</div>
</div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><span lang="ES" style="font-size:9.0pt;font-family:Consolas">Miguel A. Salazar de Troya</span><o:p></o:p></p>
</div>
</div>
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:9.0pt;font-family:Consolas">Postdoctoral Researcher, Lawrence Livermore National Laboratory</span><o:p></o:p></p>
</div>
</div>
</div>
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:9.0pt;font-family:Consolas">B141</span><o:p></o:p></p>
</div>
</div>
</div>
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:9.0pt;font-family:Consolas">Rm: 1085-5</span><o:p></o:p></p>
</div>
</div>
</div>
<div>
<div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><span style="font-size:9.0pt;font-family:Consolas">Ph: 1(925) 422-6411</span><o:p></o:p></p>
</div>
</div>
</div>
<div style="margin-left:.5in">
<p class="MsoNormal" style="margin-left:.5in"><simple-ode.py><o:p></o:p></p>
</div>
</div>
</blockquote>
</div>
</div>
</blockquote>
</div>
<p class="MsoNormal" style="margin-left:.5in"><br>
<br>
<o:p></o:p></p>
</div>
</body>
</html>