<head><!-- BaNnErBlUrFlE-HeAdEr-start -->
<style>
#pfptBannerwaiv669 { all: revert !important; display: block !important;
visibility: visible !important; opacity: 1 !important;
background-color: #D0D8DC !important;
max-width: none !important; max-height: none !important }
.pfptPrimaryButtonwaiv669:hover, .pfptPrimaryButtonwaiv669:focus {
background-color: #b4c1c7 !important; }
.pfptPrimaryButtonwaiv669:active {
background-color: #90a4ae !important; }
</style>
<!-- BaNnErBlUrFlE-HeAdEr-end -->
</head><!-- BaNnErBlUrFlE-BoDy-start -->
<!-- Preheader Text : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">
The coloring I'm fairly confident is correct -- I use the same process for 3D unstructured grids and everything seems to work. The residual function is also validated. As a test I did as you suggested -- assume the matrix is dense -- and
</div>
<!-- Preheader Text : END -->
<!-- Email Banner : BEGIN -->
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerStart</div>
<!--[if ((ie)|(mso))]>
<table border="0" cellspacing="0" cellpadding="0" width="100%" style="padding: 16px 0px 16px 0px; direction: ltr" ><tr><td>
<table border="0" cellspacing="0" cellpadding="0" style="padding: 0px 10px 5px 6px; width: 100%; border-radius:4px; border-top:4px solid #90a4ae;background-color:#D0D8DC;"><tr><td valign="top">
<table align="left" border="0" cellspacing="0" cellpadding="0" style="padding: 4px 8px 4px 8px">
<tr><td style="color:#000000; font-family: 'Arial', sans-serif; font-weight:bold; font-size:14px; direction: ltr">
This Message Is From an External Sender
</td></tr>
<tr><td style="color:#000000; font-weight:normal; font-family: 'Arial', sans-serif; font-size:12px; direction: ltr">
This message came from outside your organization.
</td></tr>
</table>
</td></tr></table>
</td></tr></table>
<![endif]-->
<![if !((ie)|(mso))]>
<div dir="ltr" id="pfptBannerwaiv669" style="all: revert !important; display:block !important; text-align: left !important; margin:16px 0px 16px 0px !important; padding:8px 16px 8px 16px !important; border-radius: 4px !important; min-width: 200px !important; background-color: #D0D8DC !important; background-color: #D0D8DC; border-top: 4px solid #90a4ae !important; border-top: 4px solid #90a4ae;">
<div id="pfptBannerwaiv669" style="all: unset !important; float:left !important; display:block !important; margin: 0px 0px 1px 0px !important; max-width: 600px !important;">
<div id="pfptBannerwaiv669" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #D0D8DC !important; color:#000000 !important; color:#000000; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-weight:bold !important; font-weight:bold; font-size:14px !important; line-height:18px !important; line-height:18px">
This Message Is From an External Sender
</div>
<div id="pfptBannerwaiv669" style="all: unset !important; display:block !important; visibility: visible !important; background-color: #D0D8DC !important; color:#000000 !important; color:#000000; font-weight:normal; font-family: 'Arial', sans-serif !important; font-family: 'Arial', sans-serif; font-size:12px !important; line-height:18px !important; line-height:18px; margin-top:2px !important;">
This message came from outside your organization.
</div>
</div>
<div style="clear: both !important; display: block !important; visibility: hidden !important; line-height: 0 !important; font-size: 0.01px !important; height: 0px"> </div>
</div>
<![endif]>
<div style="display:none !important;display:none;visibility:hidden;mso-hide:all;font-size:1px;color:#ffffff;line-height:1px;height:0px;max-height:0px;opacity:0;overflow:hidden;">ZjQcmQRYFpfptBannerEnd</div>
<!-- Email Banner : END -->
<!-- BaNnErBlUrFlE-BoDy-end -->
<div dir="ltr"><div>The coloring I'm fairly confident is correct -- I use the same process for 3D unstructured grids and everything seems to work. The residual function is also validated.</div><div><br></div><div>As a test I did as you suggested -- assume the matrix is dense -- and I get the same bad results, just now the zero blocks are filled.</div><div><br></div><div>Assuming dense, giving it a constant vector, all is good:</div><div><br></div><div> 4.23516e-16 -1.10266 0.31831 -0.0852909 0 0 -0.31831 1.18795<br> 1.10266 -4.23516e-16 -1.18795 0.31831 0 0 0.0852909 -0.31831<br> -0.31831 1.18795 2.11758e-16 -1.10266 0.31831 -0.0852909 0 0<br> 0.0852909 -0.31831 1.10266 -4.23516e-16 -1.18795 0.31831 0 0<br> 0 0 -0.31831 1.18795 2.11758e-16 -1.10266 0.31831 -0.0852909<br> 0 0 0.0852909 -0.31831 1.10266 -4.23516e-16 -1.18795 0.31831<br> 0.31831 -0.0852909 0 0 -0.31831 1.18795 4.23516e-16 -1.10266</div><div><br></div><div><br></div><div>Assuming dense, giving it sin(x), all is bad:</div><div><br></div><div>-1.76177e+08 -6.07287e+07 -6.07287e+07 -1.76177e+08 1.76177e+08 6.07287e+07 6.07287e+07 1.76177e+08<br>-1.31161e+08 -4.52116e+07 -4.52116e+07 -1.31161e+08 1.31161e+08 4.52116e+07 4.52116e+07 1.31161e+08<br> 1.31161e+08 4.52116e+07 4.52116e+07 1.31161e+08 -1.31161e+08 -4.52116e+07 -4.52116e+07 -1.31161e+08<br> 1.76177e+08 6.07287e+07 6.07287e+07 1.76177e+08 -1.76177e+08 -6.07287e+07 -6.07287e+07 -1.76177e+08<br> 1.76177e+08 6.07287e+07 6.07287e+07 1.76177e+08 -1.76177e+08 -6.07287e+07 -6.07287e+07 -1.76177e+08<br> 1.31161e+08 4.52116e+07 4.52116e+07 1.31161e+08 -1.31161e+08 -4.52116e+07 -4.52116e+07 -1.31161e+08<br>-1.31161e+08 -4.52116e+07 -4.52116e+07 -1.31161e+08 1.31161e+08 4.52116e+07 4.52116e+07 1.31161e+08<br>-1.76177e+08 -6.07287e+07 -6.07287e+07 -1.76177e+08 1.76177e+08 6.07287e+07 6.07287e+07 1.76177e+08</div><div><br></div><div>Scratching my head over here... I've been using these routines successfully for years in much more complex code.<br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Apr 21, 2024 at 12:36 PM Zou, Ling <<a href="mailto:lzou@anl.gov">lzou@anl.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div class="msg5206826287588213837">
<div lang="EN-US" style="overflow-wrap: break-word;">
<div class="m_5206826287588213837WordSection1">
<p class="MsoNormal"><span style="font-size:11pt">Edit:<u></u><u></u></span></p>
<ul style="margin-top:0in" type="disc">
<li class="m_5206826287588213837MsoListParagraph" style="margin-left:0in"><span style="font-size:11pt">how do you do the coloring when using PETSc finite differencing? An incorrect coloring may give you wrong Jacobian.
<span style="background: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">If the numeric converges as expected, then fine tune your coloring to make it right and fast.</span></span><u></u><u></u></li></ul>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11pt"><u></u> <u></u></span></p>
<div id="m_5206826287588213837mail-editor-reference-message-container">
<div>
<div style="border-width:1pt medium medium;border-style:solid none none;border-color:rgb(181,196,223) currentcolor currentcolor;padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From:
</span></b><span style="font-size:12pt;color:black">petsc-users <<a href="mailto:petsc-users-bounces@mcs.anl.gov" target="_blank">petsc-users-bounces@mcs.anl.gov</a>> on behalf of Zou, Ling via petsc-users <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
<b>Date: </b>Sunday, April 21, 2024 at 11:29 AM<br>
<b>To: </b>Mark Lohry <<a href="mailto:mlohry@gmail.com" target="_blank">mlohry@gmail.com</a>>, PETSc <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
<b>Subject: </b>Re: [petsc-users] finite difference jacobian errors when given non-constant initial condition<u></u><u></u></span></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:11pt">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><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt">So, I tend to believe that your implementation may have some problem. Some lessons I learned during my code development:</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p>
<ul style="margin-top:0in" type="disc">
<li class="m_5206826287588213837MsoListParagraph" style="margin-left:0in"><span style="font-size:11pt">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><u></u><u></u></li><li class="m_5206826287588213837MsoListParagraph" style="margin-left:0in"><span style="font-size:11pt">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><u></u><u></u></li></ul>
<p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt">-Ling</span><u></u><u></u></p>
<p class="MsoNormal"><span style="font-size:11pt"> </span><u></u><u></u></p>
<div id="m_5206826287588213837mail-editor-reference-message-container">
<div>
<div style="border-width:1pt medium medium;border-style:solid none none;border-color:rgb(181,196,223) currentcolor currentcolor;padding:3pt 0in 0in">
<p class="MsoNormal" style="margin-bottom:12pt"><b><span style="font-size:12pt;color:black">From:
</span></b><span style="font-size:12pt;color:black">petsc-users <<a href="mailto:petsc-users-bounces@mcs.anl.gov" target="_blank">petsc-users-bounces@mcs.anl.gov</a>> on behalf of Mark Lohry <<a href="mailto:mlohry@gmail.com" target="_blank">mlohry@gmail.com</a>><br>
<b>Date: </b>Saturday, April 20, 2024 at 1:35 PM<br>
<b>To: </b>PETSc <<a href="mailto:petsc-users@mcs.anl.gov" target="_blank">petsc-users@mcs.anl.gov</a>><br>
<b>Subject: </b>[petsc-users] finite difference jacobian errors when given non-constant initial condition</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1pt;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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1pt;color:white">ZjQcmQRYFpfptBannerStart</span><u></u><u></u></p>
</div>
<div style="border-width:medium;border-style:none;border-color:rgb(144,164,174) currentcolor currentcolor;display:block;text-align:left;margin:0px;padding:16px;border-radius:4px;min-width:200px;background-color:rgb(208,216,220)" id="m_5206826287588213837pfptBannerovsgilx">
<div id="m_5206826287588213837pfptBannerovsgilx">
<div id="m_5206826287588213837pfptBannerovsgilx">
<p class="MsoNormal" style="line-height:13.5pt;background:rgb(208,216,220)"><b><span style="font-size:12pt;font-family:"Arial",sans-serif;color:black">This Message Is From an External Sender
</span></b><u></u><u></u></p>
</div>
<div id="m_5206826287588213837pfptBannerovsgilx">
<p class="MsoNormal" style="line-height:13.5pt;background:rgb(208,216,220)"><span style="font-size:12pt;font-family:"Arial",sans-serif;color:black">This message came from outside your organization.
</span><u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal" style="background:rgb(208,216,220)"><span style="font-size:12pt;color:black"> </span><u></u><u></u></p>
</div>
</div>
<div>
<p class="MsoNormal"><span style="font-size:1pt;color:white">ZjQcmQRYFpfptBannerEnd</span><u></u><u></u></p>
</div>
<div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">Analytically computed Jacobian:</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> 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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">petsc finite difference jacobian when given f(x)=1:</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> 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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">petsc finite difference jacobian when given f(x) = sin(x):</span><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt">-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><u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><span style="font-size:12pt"> </span><u></u><u></u></p>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div></blockquote></div>