<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=us-ascii">
<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:DengXian;
        panose-1:2 1 6 0 3 1 1 1 1 1;}
@font-face
        {font-family:Aptos;
        panose-1:2 11 6 4 2 2 2 2 2 4;}
@font-face
        {font-family:"\@DengXian";
        panose-1:2 1 6 0 3 1 1 1 1 1;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0in;
        font-size:10.0pt;
        font-family:"Aptos",sans-serif;}
p.MsoListParagraph, li.MsoListParagraph, div.MsoListParagraph
        {mso-style-priority:34;
        margin-top:0in;
        margin-right:0in;
        margin-bottom:0in;
        margin-left:.5in;
        font-size:10.0pt;
        font-family:"Aptos",sans-serif;}
span.EmailStyle20
        {mso-style-type:personal-reply;
        font-family:"Aptos",sans-serif;
        color:windowtext;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-size:10.0pt;
        mso-ligatures:none;}
@page WordSection1
        {size:8.5in 11.0in;
        margin:1.0in 1.0in 1.0in 1.0in;}
div.WordSection1
        {page:WordSection1;}
/* List Definitions */
@list l0
        {mso-list-id:1241056979;
        mso-list-template-ids:-211106662;}
@list l0:level1
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:.5in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level2
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:1.0in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level3
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:1.5in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level4
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:2.0in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level5
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:2.5in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level6
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:3.0in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level7
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:3.5in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level8
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:4.0in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
@list l0:level9
        {mso-level-number-format:bullet;
        mso-level-text:\F0B7 ;
        mso-level-tab-stop:4.5in;
        mso-level-number-position:left;
        text-indent:-.25in;
        mso-ansi-font-size:10.0pt;
        font-family:Symbol;}
ol
        {margin-bottom:0in;}
ul
        {margin-bottom:0in;}
--></style>
</head>
<body lang="EN-US" link="#467886" vlink="#96607D" style="word-wrap:break-word">
<div class="WordSection1">
<p class="MsoNormal"><span style="font-size:11.0pt">Edit:<o:p></o:p></span></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1"><span style="font-size:11.0pt">how do you do the coloring when using PETSc finite differencing? An incorrect coloring may give you wrong Jacobian.
<span style="background:yellow;mso-highlight:yellow">For debugging purpose,</span> the simplest way to avoid an incorrect coloring is to assume the matrix is dense (slow but error proofing).
<span style="background:yellow;mso-highlight:yellow">If the numeric converges as expected, then fine tune your coloring to make it right and fast.</span></span><o:p></o:p></li></ul>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt"><o:p> </o:p></span></p>
<div id="mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">petsc-users <petsc-users-bounces@mcs.anl.gov> on behalf of Zou, Ling via petsc-users <petsc-users@mcs.anl.gov><br>
<b>Date: </b>Sunday, April 21, 2024 at 11:29 AM<br>
<b>To: </b>Mark Lohry <mlohry@gmail.com>, PETSc <petsc-users@mcs.anl.gov><br>
<b>Subject: </b>Re: [petsc-users] finite difference jacobian errors when given non-constant initial condition<o:p></o:p></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11.0pt">Hi Mark, I am working on a project having similar numeric you have, one-dimensional finite volume method with second-order slope limiter TVD, and PETSc finite differencing gives perfect Jacobian even for complex
 problems.</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt">So, I tend to believe that your implementation may have some problem. Some lessons I learned during my code development:</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<ul style="margin-top:0in" type="disc">
<li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1"><span style="font-size:11.0pt">how do you do the coloring when using PETSc finite differencing? An incorrect coloring may give you wrong Jacobian. The simplest way to avoid an incorrect
 coloring is to assume the matrix is dense (slow but error proofing).</span><o:p></o:p></li><li class="MsoListParagraph" style="margin-left:0in;mso-list:l0 level1 lfo1"><span style="font-size:11.0pt">Residual function evaluation not correctly implemented can also lead to incorrect Jacobian. In your case, you may want to take a careful look at the
 order of execution, when to update your unknown vector, when to perform P1 reconstruction, and when to evaluate the residual.</span><o:p></o:p></li></ul>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt">-Ling</span><o:p></o:p></p>
<p class="MsoNormal"><span style="font-size:11.0pt"> </span><o:p></o:p></p>
<div id="mail-editor-reference-message-container">
<div>
<div style="border:none;border-top:solid #B5C4DF 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal" style="margin-bottom:12.0pt"><b><span style="font-size:12.0pt;color:black">From:
</span></b><span style="font-size:12.0pt;color:black">petsc-users <petsc-users-bounces@mcs.anl.gov> on behalf of Mark Lohry <mlohry@gmail.com><br>
<b>Date: </b>Saturday, April 20, 2024 at 1:35 PM<br>
<b>To: </b>PETSc <petsc-users@mcs.anl.gov><br>
<b>Subject: </b>[petsc-users] finite difference jacobian errors when given non-constant initial condition</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1.0pt;color:white">I have a 1-dimensional P1 discontinuous Galerkin discretization of the linear advection equation with 4 cells and periodic boundaries on [-pi,+pi]. I'm comparing the results from SNESComputeJacobian
 with a hand-written Jacobian. Being linear, </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1.0pt;color:white">ZjQcmQRYFpfptBannerStart</span><o:p></o:p></p>
</div>
<div style="border:none;border-top:solid #90A4AE 3.0pt;padding:0in 0in 0in 0in;display:block!important;text-align:left!important;margin:0px!important;padding:16px!important;border-radius:4px!important;min-width:200px!important;background-color:#D0D8DC!important;border-top:#90a4ae!important" id="pfptBannerovsgilx">
<div id="pfptBannerovsgilx">
<div id="pfptBannerovsgilx">
<p class="MsoNormal" style="line-height:13.5pt;background:#D0D8DC"><b><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">This Message Is From an External Sender
</span></b><o:p></o:p></p>
</div>
<div id="pfptBannerovsgilx">
<p class="MsoNormal" style="line-height:13.5pt;background:#D0D8DC"><span style="font-size:12.0pt;font-family:"Arial",sans-serif;color:black">This message came from outside your organization.
</span><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal" style="background:#D0D8DC"><span style="font-size:12.0pt;color:black"> </span><o:p></o:p></p>
</div>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1.0pt;color:white">ZjQcmQRYFpfptBannerEnd</span><o:p></o:p></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">I have a 1-dimensional P1 discontinuous Galerkin discretization of the linear advection equation with 4 cells and periodic boundaries on [-pi,+pi]. I'm comparing the results from SNESComputeJacobian with a
 hand-written Jacobian. Being linear, the Jacobian should be constant/independent of the solution.</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">When I set the initial condition passed to SNESComputeJacobian as some constant, say f(x)=1 or 0, the petsc finite difference jacobian agrees with my hand coded-version. But when I pass it some non-constant
 value, e.g. f(x)=sin(x), something goes horribly wrong in the petsc jacobian. Implementing my own rudimentary finite difference approximation (similar to how I thought petsc computes it) it returns the correct jacobian to expected error. Any idea what could
 be going on?</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">Analytically computed Jacobian:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> 4.44089e-16     -1.10266      0.31831   -0.0852909            0            0     -0.31831      1.18795<br>
     1.10266 -4.44089e-16     -1.18795      0.31831            0            0    0.0852909     -0.31831<br>
    -0.31831      1.18795  4.44089e-16     -1.10266      0.31831   -0.0852909            0            0<br>
   0.0852909     -0.31831      1.10266 -4.44089e-16     -1.18795      0.31831            0            0<br>
           0            0     -0.31831      1.18795  4.44089e-16     -1.10266      0.31831   -0.0852909<br>
           0            0    0.0852909     -0.31831      1.10266 -4.44089e-16     -1.18795      0.31831<br>
     0.31831   -0.0852909            0            0     -0.31831      1.18795  4.44089e-16     -1.10266<br>
    -1.18795      0.31831            0            0    0.0852909     -0.31831      1.10266 -4.44089e-16</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">petsc finite difference jacobian when given f(x)=1:</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> 4.44089e-16     -1.10266      0.31831   -0.0852909            0            0     -0.31831      1.18795<br>
     1.10266 -4.44089e-16     -1.18795      0.31831            0            0    0.0852909     -0.31831<br>
    -0.31831      1.18795  4.44089e-16     -1.10266      0.31831   -0.0852909            0            0<br>
   0.0852909     -0.31831      1.10266 -4.44089e-16     -1.18795      0.31831            0            0<br>
           0            0     -0.31831      1.18795  4.44089e-16     -1.10266      0.31831   -0.0852909<br>
           0            0    0.0852909     -0.31831      1.10266 -4.44089e-16     -1.18795      0.31831<br>
     0.31831   -0.0852909            0            0     -0.31831      1.18795  4.44089e-16     -1.10266<br>
    -1.18795      0.31831            0            0    0.0852909     -0.31831      1.10266 -4.44089e-16</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">petsc finite difference jacobian when given f(x) = sin(x):</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt">-1.65547e+08 -3.31856e+08 -1.25427e+09   4.4844e+08            0            0  1.03206e+08  7.86375e+07<br>
 9.13788e+07  1.83178e+08  6.92336e+08  -2.4753e+08            0            0 -5.69678e+07 -4.34064e+07<br>
  3.7084e+07  7.43387e+07  2.80969e+08 -1.00455e+08  -5.0384e+07 -2.99747e+07            0            0<br>
  3.7084e+07  7.43387e+07  2.80969e+08 -1.00455e+08  -5.0384e+07 -2.99747e+07            0            0<br>
           0            0  2.80969e+08 -1.00455e+08  -5.0384e+07 -2.99747e+07 -2.31191e+07 -1.76155e+07<br>
           0            0  2.80969e+08 -1.00455e+08  -5.0384e+07 -2.99747e+07 -2.31191e+07 -1.76155e+07<br>
 9.13788e+07  1.83178e+08            0            0 -1.24151e+08 -7.38608e+07 -5.69678e+07 -4.34064e+07<br>
-1.65547e+08 -3.31856e+08            0            0  2.24919e+08   1.3381e+08  1.03206e+08  7.86375e+07</span><o:p></o:p></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12.0pt"> </span><o:p></o:p></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
</html>