<div>Thank you Jed. It works, and the result is identical to the exact solution! </div><div><br></div><div>Hope you best!</div><div><font><br></font></div><div><sign signid="99"><div><font><br></font><font><br></font><font><br></font><font><br></font><div style="color:#909090;font-family:Arial Narrow;font-size:12px"></div></div><div style="font-size:14px;font-family:Verdana;color:#000;" class="signRealArea"><div><div class="logo" style="width:305px;height:35px;line-height:35px;margin:20px 0 0 0;"><img src="https://exmail.qq.com/cgi-bin/viewfile?type=logo&domain=mail.sustech.edu.cn" onerror=""></div><div class="c_detail" style="margin:10px 0 0 0;"><h4 class="name" style="margin:0;font-size:14px;font-weight:bold;line-height:28px;zoom:1;">David Jiawei LUO LIANG</h4><p class="position" style="margin:0;line-height:22px;color:#a0a0a0;"></p><p class="department" style="margin:0;line-height:22px;color:#a0a0a0;">南方科技大学/学生/研究生/2024</p><p class="phone" style="margin:0;line-height:22px;color:#a0a0a0;"></p><p class="addr" style="margin:0;line-height:22px;color:#a0a0a0;">广东省深圳市南山区学苑大道1088号</p></div></div></div></sign></div><div> </div><div><includetail><div> </div><div> </div><div style="font:Verdana normal 14px;color:#000;"><div style="FONT-SIZE: 12px;FONT-FAMILY: Arial Narrow;padding:2px 0 2px 0;">------------------ Original ------------------</div><div style="FONT-SIZE: 12px;background:#efefef;padding:8px;"><div id="menu_sender"><b>From: </b> "Jed Brown"<jed@jedbrown.org>;</div><div><b>Date: </b> Thu, Nov 21, 2024 11:05 PM</div><div><b>To: </b> "David Jiawei LUO LIANG"<12431140@mail.sustech.edu.cn>; "petsc-users"<petsc-users@mcs.anl.gov>; <wbr></div><div></div><div><b>Subject: </b> Re: [petsc-users] Cannot iterate well when using Newton iteration of SNES</div></div><div> </div><div style="position:relative;"><div id="tmpcontent_res"></div>
You should add VecZeroEntries(f) near the top of your FormFunction (it's currently accumulating into whatever was there last) and MatZeroEntries(B) to FormJacobian.<br><br>I reduced to nElem = 5 for ease of viewing. With these changes, I see quadratic convergence but the problem is still nonlinear. To explore further, consider using these diagnostics<br><br>./SNES_heat -{snes,ksp}_monitor -{snes,ksp}_converged_reason -snes_linesearch_monitor -ksp_view_mat<br><br>with and without -snes_fd.<br><br>For readability, I would suggest consistency in "u" vs "x".<br><br>"David Jiawei LUO LIANG" <12431140@mail.sustech.edu.cn> writes:<br><br>> I am using the Newton iteration to solve a nonlinear 1D heat equation problem by using FEM.<br>><br>><br>> I attached my source code named "SNES_heat.cpp" <br>><br>><br>> when I run the code<br>><br>> 0 SNES Function norm 1.206289245288e+01<br>> <br>> 1 SNES Function norm 7.128802192789e+00<br>> <br>> 2 SNES Function norm 6.608812909525e+00<br>><br>><br>><br>> you can find that it only iterate 3 steps, and then do all the function evaluation and finally just stop the program. <br>><br>><br>> I think it is not reasonble. I check my code, it is correct if I set it as a linear problem. it means my Jacobian and Residual function is correct.<br>><br>><br>> But when I set it as a nonlinear, the residual seems reduces as not expected. <br>><br>><br>> I doubt that whether my understanding of the newton iteration is different from SNES's newton iteration process.<br>><br>><br>><br>><br>><br>><br>><br>><br>> David Jiawei LUO LIANG<br>><br>><br>><br>> 南方科技大学/学生/研究生/2024<br>><br>><br>><br>> 广东省深圳市南山区学苑大道1088号<br>><br>><br>><br>><br>> <br>
</div></div><!--<![endif]--></includetail></div>