Hi Matt, 
   Yes, the residual and Jacobin function are both incorrect. 


Both of the Vec f and Mat B haven't initialized as zeros. Jed caught that bug, 
thanks Jed.


Anyway, thank you for your method to debug my program for the next time bug..


Hope you the best!










David Jiawei LUO LIANG



南方科技大学/学生/研究生/2024



广东省深圳市南山区学苑大道1088号




 
 
 
------------------ Original ------------------
From: &nbsp;"Matthew&nbsp;Knepley"<knep...@gmail.com&gt;;
Date: &nbsp;Thu, Nov 21, 2024 11:21 PM
To: &nbsp;"David Jiawei LUO LIANG"<12431...@mail.sustech.edu.cn&gt;; 
Cc: &nbsp;"petsc-users"<petsc-users@mcs.anl.gov&gt;; 
Subject: &nbsp;Re: [petsc-users] Cannot iterate well when using Newton 
iteration of SNES

&nbsp;

On Thu, Nov 21, 2024 at 8:57 AM David Jiawei LUO LIANG 
<12431...@mail.sustech.edu.cn&gt; wrote:
I am using the Newton iteration to solve a nonlinear 1D heat equation problem 
by using FEM.


I attached my source code named "SNES_heat.cpp"&nbsp;


when I run the code

&nbsp; 0 SNES Function norm 1.206289245288e+01
 
&nbsp; 1 SNES Function norm 7.128802192789e+00
 
&nbsp; 2 SNES Function norm 6.608812909525e+00



you can find that it only iterate 3 steps, and then do all the function 
evaluation and finally just stop the program.&nbsp;


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.


But when I set it as a nonlinear, the residual seems reduces as not 
expected.&nbsp;


I doubt that whether my understanding of the newton iteration is different from 
SNES's newton iteration process.



Here is what happens with the code as it is:


master *:~/Downloads/tmp/Liang$ ./SNES_heat -snes_monitor -ksp_converged_reason 
-snes_converged_reason -pc_type lu -snes_view -snes_linesearch_monitor
pp 1
nElem 10
nqp 2
n_np 11
n_en 2
n_eq 10
qp: 
0.57735 -0.57735        
wq: 
1       1       
IEN: 
1       2       3       4       5       6       7       8       9       10      
2       3       4       5       6       7       8       9       10      11      
x_coor: 
0       0.1     0.2     0.3     0.4     0.5     0.6     0.7     0.8     0.9     
1       
ID: 
1       2       3       4       5       6       7       8       9       10      
0       
&nbsp; 0 SNES Function norm 1.206289245288e+01
&nbsp; &nbsp; Linear solve converged due to CONVERGED_RTOL iterations 1
&nbsp; &nbsp; &nbsp; Line search: Using full step: fnorm 1.206289245288e+01 
gnorm 7.128802192789e+00
&nbsp; 1 SNES Function norm 7.128802192789e+00
&nbsp; &nbsp; Linear solve converged due to CONVERGED_RTOL iterations 1
&nbsp; &nbsp; &nbsp; Line search: Using full step: fnorm 7.128802192789e+00 
gnorm 6.608812909525e+00
&nbsp; 2 SNES Function norm 6.608812909525e+00
&nbsp; &nbsp; Linear solve converged due to CONVERGED_RTOL iterations 1
&nbsp; &nbsp; &nbsp; Line search: gnorm after quadratic fit 1.265375106867e+01
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.328962011911e+01 lambda=1.7500506382162818e-02
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275802797864e+01 lambda=1.7500506382162819e-03
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.327920917220e+01 lambda=1.7500506382162821e-04
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275906891232e+01 lambda=1.7500506382162820e-05
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.327910508109e+01 lambda=1.7500506382162821e-06
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275907932147e+01 lambda=1.7500506382162822e-07
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.327910404018e+01 lambda=1.7500506382162823e-08
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275907942556e+01 lambda=1.7500506382162823e-09
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.327910402977e+01 lambda=1.7500506382162824e-10
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275907942660e+01 lambda=1.7500506382162825e-11
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.327910402966e+01 lambda=1.7500506382162826e-12
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 1.275907942661e+01 lambda=1.7500506382162828e-13
&nbsp; &nbsp; &nbsp; Line search: unable to find good step length! After 12 
tries 
&nbsp; &nbsp; &nbsp; Line search: fnorm=6.6088129095253478e+00, 
gnorm=1.2759079426614502e+01, ynorm=5.3714153713436097e-01, 
minlambda=9.9999999999999998e-13, lambda=1.7500506382162828e-13, initial 
slope=-4.3676408073108860e+01
&nbsp; Nonlinear solve did not converge due to DIVERGED_LINE_SEARCH iterations 2



Usually, we suspect that the Jacobian is incorrect in this case. Thus we can 
have it formed automatically,


master *:~/Downloads/tmp/Liang$ ./SNES_heat -snes_monitor -ksp_converged_reason 
-snes_converged_reason -snes_fd -pc_type lu -snes_view -snes_linesearch_monitor
pp 1
nElem 10
nqp 2
n_np 11
n_en 2
n_eq 10
qp: 
0.57735 -0.57735        
wq: 
1       1       
IEN: 
1       2       3       4       5       6       7       8       9       10      
2       3       4       5       6       7       8       9       10      11      
x_coor: 
0       0.1     0.2     0.3     0.4     0.5     0.6     0.7     0.8     0.9     
1       
ID: 
1       2       3       4       5       6       7       8       9       10      
0       
&nbsp; 0 SNES Function norm 1.206289245288e+01
&nbsp; &nbsp; Linear solve converged due to CONVERGED_RTOL iterations 1
&nbsp; &nbsp; &nbsp; Line search: Scaling step by 1.837216392007e-47 old ynorm 
5.443016970405e+54
&nbsp; &nbsp; &nbsp; Line search: gnorm after quadratic fit 3.704240795372e+16
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704614960636e+16 lambda=1.0000000000000002e-02
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611219026e+16 lambda=1.0000000000000002e-03
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256438e+16 lambda=1.0000000000000003e-04
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256064e+16 lambda=1.0000000000000004e-05
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000004e-06
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000005e-07
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000005e-08
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000005e-09
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000006e-10
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000006e-11
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000006e-12
&nbsp; &nbsp; &nbsp; Line search: Cubic step no good, shrinking lambda, current 
gnorm 3.704611256068e+16 lambda=1.0000000000000007e-13
&nbsp; &nbsp; &nbsp; Line search: unable to find good step length! After 12 
tries 
&nbsp; &nbsp; &nbsp; Line search: fnorm=1.2062892452882465e+01, 
gnorm=3.7046112560677824e+16, ynorm=1.0000000000000000e+08, 
minlambda=9.9999999999999998e-13, lambda=1.0000000000000007e-13, initial 
slope=-4.6079597780656769e+00
&nbsp; Nonlinear solve did not converge due to DIVERGED_LINE_SEARCH iterations 0


So it is clear that the Jacobian do not match. Moreover, it appears that Newton 
is not going to converge from this initial guess. It suggests that the residual 
is wrong somehow. I suggest coding up a MMS to prove to yourself that the 
residual is correct.


&nbsp; Thanks,


&nbsp; &nbsp; Matt
&nbsp;




David Jiawei LUO LIANG



南方科技大学/学生/研究生/2024



广东省深圳市南山区学苑大道1088号




&nbsp;





-- 
What most experimenters take for granted before they begin their experiments is 
infinitely more interesting than any results to which their experiments lead.
-- Norbert Wiener


https://urldefense.us/v3/__https://www.cse.buffalo.edu/*knepley/__;fg!!G_uCfscf7eWS!d_hNum3xt9CQi75aQNB-b7ZmECc6WzDow92m6xvlE63WjUrrDlHvycebtTBgGerF6a61W-336JbK4GLm2mUcnH8hv2SLJQY-$
 

Reply via email to