<div dir="ltr"><div dir="ltr">On Sun, Jul 2, 2023 at 7:53 AM 王赫萌 <<a href="mailto:wanghemeng@163.com">wanghemeng@163.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><div style="font-family:微软雅黑,Verdana,"Microsoft Yahei",SimSun,sans-serif;font-size:14px;line-height:1.6">
<div style="font-family:微软雅黑;font-size:14px;line-height:1.6"><div>Thanks for your reply!
</div><div>
</div><div>So sorry that I made a mistake in the description.
</div><div>
</div><div>I set the tolerances by:
</div><div>
</div><div><span style="background-color:rgb(233,233,233)">PetscCall(KSPSetTolerances(ksp, 1e-12, DBL_MIN, PETSC_DEFAULT, PETSC_DEFAULT));
</span></div><div>
</div><div>and got (by passing `-ksp_norm_type unpreconditioned -ksp_monitor_true_residual`)
</div><div>
</div><div><span style="background-color:rgb(233,233,233)">74 KSP unpreconditioned resid norm 7.256655641876e-08 true resid norm 7.256655641876e-08 ||r(i)||/||b|| 9.975738158726e-13
</span></div><div>
</div><div>I'm wondering why the ` ||r(i)||/||b||` is different with mine which calculated by:
</div><div>
</div><div>```
</div><div> PetscCall(VecNorm(b, NORM_2, &norm_b)); // (main.c, line 74) (which is 72743.044439)
</div><div>
</div><div> PetscCall(VecDuplicate(b, &u)); // (main.c, line 105)
</div><div> PetscCall(MatMult(A, x, u));
</div><div> PetscCall(VecAXPY(b, -1.0, u));
</div><div> PetscCall(VecNorm(b, NORM_2, &norm_delta)); // (which is 0.039608)
</div><div>```
</div><div>
</div><div>and (norm_delta) / (norm_b) = <span style="background-color:rgb(233,233,233)">5.44496e-07</span> which is higher and different with the rtol I set (<span style="background-color:rgb(233,233,233)">1e-12</span>).
</div><div>
</div><div>Sorry again for the waste of your time. I would really appreciated if you could help me again!</div></div></div></div></blockquote><div><br></div><div>1) 7.256655641876e-08 / 72743.044439 = 9.975738158726e-13 so ||r_i||/||b|| is correct in the output</div><div><br></div><div>2) You are asking why you calculate a different residual? I will have to run your code.</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:微软雅黑,Verdana,"Microsoft Yahei",SimSun,sans-serif;font-size:14px;line-height:1.6"><div style="font-family:微软雅黑;font-size:14px;line-height:1.6"><div>
</div><div>
</div><div>Best regards!
</div><div>
</div><div>Hemeng Wang</div><div id="m_-8237380491218595464ntes-pcmac-signature" style="font-family:"""><div style="font-size:14px;padding:0px;margin:0px"><div style="padding-bottom:6px;margin-bottom:10px;display:inline-block"><span style="color:rgb(0,0,0);font-family:微软雅黑;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:22.4px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline"><br></span></div></div><div style="font-size:14px;padding:0px;margin:0px"><div style="padding-bottom:6px;margin-bottom:10px;display:inline-block"><span style="color:rgb(0,0,0);font-family:微软雅黑;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:22.4px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline"><br></span></div></div>
</div></div>
<div style="padding-top:1px;padding-bottom:1px">
<div style="margin-top:2em;margin-bottom:1em;font-size:14px;line-height:1.25;color:rgb(120,120,122)">
---- Replied Message ----
</div>
<div style="margin-bottom:1em;font-size:12px;line-height:1.25;color:rgb(35,35,36);padding:0.5em 0.25em;border-radius:0.67em;background-color:rgb(240,240,240)">
<table width="100%" cellpadding="0" cellspacing="9" border="0">
<tbody><tr>
<td valign="top" style="width:4.25em;font-size:12px;line-height:1.25;color:rgb(120,120,122)">
From
</td>
<td valign="top" style="font-size:12px;line-height:1.25;color:rgb(35,35,36);word-break:break-all">
<a style="color:rgb(48,97,242);text-decoration:none" href="mailto:knepley@gmail.com" target="_blank">Matthew Knepley<knepley@gmail.com>
</a>
</td>
</tr>
<tr>
<td valign="top" style="width:4.25em;font-size:12px;line-height:1.25;color:rgb(120,120,122)">
Date
</td>
<td valign="top" style="font-size:12px;line-height:1.25;color:rgb(35,35,36);word-break:break-all">
7/2/2023 18:51
</td>
</tr>
<tr>
<td valign="top" style="width:4.25em;font-size:12px;line-height:1.25;color:rgb(120,120,122)">
To
</td>
<td valign="top" style="font-size:12px;line-height:1.25;color:rgb(35,35,36);word-break:break-all">
<a style="color:rgb(48,97,242);text-decoration:none" href="mailto:wanghemeng@163.com" target="_blank">
王赫萌<wanghemeng@163.com>
</a>
</td>
</tr>
<tr>
<td valign="top" style="width:4.25em;font-size:12px;line-height:1.25;color:rgb(120,120,122)">
Cc
</td>
<td valign="top" style="font-size:12px;line-height:1.25;color:rgb(35,35,36);word-break:break-all">
<a style="color:rgb(48,97,242);text-decoration:none" href="mailto:petsc-users@mcs.anl.gov" target="_blank">
petsc-users@mcs.anl.gov<petsc-users@mcs.anl.gov>
</a>
</td>
</tr>
<tr>
<td valign="top" style="width:4.25em;font-size:12px;line-height:1.25;color:rgb(120,120,122)">
Subject
</td>
<td valign="top" style="font-size:12px;line-height:1.25;color:rgb(35,35,36);word-break:break-all">
Re: [petsc-users] Question about residue norm in PETSc
</td>
</tr>
</tbody></table>
</div>
<div dir="ltr"><div dir="ltr">On Sun, Jul 2, 2023 at 2:24 AM 王赫萌 <<a href="mailto:wanghemeng@163.com" target="_blank">wanghemeng@163.com</a>> wrote:<br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<div><div style="font-family:微软雅黑;font-size:14px;line-height:1.6"><div><span>Dear PETSc Team,
</span></div><div><span><br></span></div><div><span>
</span></div><div><span>Sorry to bother! My name is Hemeng Wang, and I am currently learning the use of PETSc software package. I am confused while calculating the norm of residue.
</span></div><div><span><br></span></div><div><span>
</span></div><div><span>I calculated residue norm by myself with:
</span></div><div><span>```
</span></div><div><span> PetscCall(VecNorm(b, NORM_2, &norm_b)); // (<span style="color:rgb(0,0,0);font-family:微软雅黑;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:22.4px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline">main.c, </span>line 74)</span></div><div><span><br></span></div><div><span> PetscCall(VecDuplicate(b, &u)); // (<span style="color:rgb(0,0,0);font-family:微软雅黑;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:22.4px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline">main.c, </span>line 105)
</span></div><div><span> PetscCall(MatMult(A, x, u));
</span></div><div><span> PetscCall(VecAXPY(b, -1.0, u));
</span></div><div><span> PetscCall(VecNorm(b, NORM_2, &norm_delta));
</span></div><div><span>```
</span></div><div><span>and check the (norm_delta) / (norm_b). It seems not the `atol` which set by `KSPSetTolerances()`.
</span></div><div>(I set atol as 1e-12, but got 5e-7 finally)</div><div>(options: -ksp_type cg -pc_type gamg -ksp_converged_reason -ksp_norm_type unpreconditioned -ksp_monitor_true_residual)</div></div></div></blockquote><div><br></div><div>If you are using the default convergence test, there is an absolute tolerance (atol) _and_ a relative tolerance (rtol). It seems likely you hit the relative tolerance. You can check this using</div><div><br></div><div> -ksp_converged_reason</div><div><br></div><div>You could make rtol really small if you want to just see the atol</div><div><br></div><div> -ksp_rtol 1e-20</div><div><br></div><div> Thanks,</div><div><br></div><div> Matt</div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><div style="font-family:微软雅黑;font-size:14px;line-height:1.6"><div><span>
</span></div><div><span>I also check the soure code of `KSPSolve_CG` in `petsc/src/ksp/ksp/impls/cg/cg.c`. And could not figure out where is the difference.
</span></div><div><span><br></span></div><div><span>
</span></div><div><span>I will really really appreciated if someone can explain the calculation of `resid norm` in petsc. And where is my mistake.</span></div><div><span><br></span></div><div><span>
</span></div><div><span>To provide you with more context, here are the source code about my implementation. And the output of my test.</span></div><div><span>
</span></div><div><span><br></span></div><div><span>main.c</span></div><div>Main code of my program</div><div><br></div><div>mmio.h mmloader.h</div><div>Headers for matrix read</div><div><br></div><div>Makefile</div><div>For compiling, same as sample provided</div><div><br></div><div>task.sh</div><div>A script for running program in `slurm`</div><div><br></div><div>slurm-4803840.out</div><div>Output of my <span style="color:rgb(0,0,0);font-family:微软雅黑;font-size:14px;font-style:normal;font-variant:normal;font-weight:normal;letter-spacing:normal;line-height:22.4px;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;float:none;display:inline">test</span></div><div><br></div><div>Thank you very much for your time and attention. I greatly appreciate your support and look forward to hearing from you soon.
</div><div>
</div><div>Best regards,</div><div>Hemeng Wang</div><div><br></div></div></div>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div></div></div></div>
</blockquote></div><br clear="all"><div><br></div><span class="gmail_signature_prefix">-- </span><br><div dir="ltr" class="gmail_signature"><div dir="ltr"><div><div dir="ltr"><div><div dir="ltr"><div>What most experimenters take for granted before they begin their experiments is infinitely more interesting than any results to which their experiments lead.<br>-- Norbert Wiener</div><div><br></div><div><a href="http://www.cse.buffalo.edu/~knepley/" target="_blank">https://www.cse.buffalo.edu/~knepley/</a><br></div></div></div></div></div></div></div></div>