00001
00002
00006
00007 subroutine newton(X,n)
00008 use solverParameters
00009 use numerical_libraries
00010
00011 use myFunctions
00012
00013 implicit none
00014 integer i,j
00015 integer n
00016 integer dummyBacktrackCount
00017 integer NumberOfCalculatedNorms
00018
00019 real(8) X(n),deltaX(n),F0(n),F1(n),F2(n)
00020 real(8) fNorm(maxNewtonIter),lambdaVec(maxNewtonIter)
00021 real(8) JAC(n,n)
00022 real(8) initialNorm
00023 real(8) convergenceTolerance
00024 real(8) fZero,fOne,fTwo
00025 real(8) alpha,mybeta,lambda,lambda1,lambda2,lambdaTest
00026 real(8) slope
00027
00028
00029
00030 real(8) inexactLinearTol
00031
00032 real(8) eta
00033
00034
00035
00036
00037 alpha=1e-4
00038 mybeta=0.9d0
00039
00040 eta=0.5d0
00041
00042
00043 newtonSuccess=.false.
00044
00045
00046
00047 call F(X,F0,n)
00048
00049 initialNorm=dot_product(F0,F0)
00050 fnorm(1)=initialNorm
00051 fZero=0.5d0*fnorm(1)
00052
00053 i=0
00054
00055 if (fnorm(i+1).le.convergenceTolerance) then
00056
00057 print*,'---------------------------------------------------------------------------'
00058 print*,'newton converged @ iter=',i
00059 print*,'current norm is ',fnorm(i+1)
00060 print*,'convergence tolerance is',convergenceTolerance
00061 print*,''
00062 print*,'-----> already converged solution is given!'
00063 print*,''
00064
00065 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00066 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00067 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00068
00069 print*,'---------------------------------------------------------------------------'
00070 newtonSuccess=.true.
00071 NumberOfCalculatedNorms=i+1
00072 return
00073
00074 end if
00075
00076
00077
00078 select case(NewtonConvergenceOption)
00079
00080 case(1)
00081
00082 convergenceTolerance=nonlinearTolAbs
00083
00084 case(2)
00085
00086 convergenceTolerance=nonlinearTolRel*initialNorm
00087
00088 case(3)
00089
00090 convergenceTolerance=nonlinearTolRel*initialNorm+nonlinearTolAbs
00091
00092 case default
00093
00094 print*,'!!!!!!!!!!!!!!!!!!!!!!!!!!!!!'
00095 print*,'non valid convergence option'
00096 print*,'!!!!!!!!!!kNaN STOP!!!!!!!!!!'
00097 stop
00098
00099 end select
00100
00101
00102
00103
00104
00105
00106
00107
00108 do i=1,maxNewtonIter
00109
00110
00111
00112
00113
00114 if (enableNewtonBasic.EQV.ON) print*,'Newton iter=',i
00115
00116
00117 deltaX=0.d0
00118
00119 call JF(X,JAC,n)
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132 inexactLinearTol= min(fnorm(i)*eta,maxLinearTol)
00133 inexactLinearTol= max(inexactLinearTol,LinearTol)
00134
00135
00136
00137
00138
00139 call bicgstab_no_s(JAC,-F0,deltax,n,inexactLinearTol,maxLinearIter,i)
00140
00141
00142
00143
00144
00145 lambda=1.d0;
00146 lambda2=lambda;
00147
00148 call F(X+lambda*deltaX,F2,n)
00149
00150
00151 fTwo=0.5d0*dot_product(F2,F2)
00152
00153 slope=-dot_product(F0,F0)
00154
00155
00156
00157
00158 if (fTwo .LT. fZero + alpha*lambda*slope) then
00159
00160 X=X+lambda*deltaX
00161 lambdaVec(i)=lambda
00162
00163 if (enableNewtonDebug.EQV.ON) print*,'valid alpha condition (direct) in iter',i
00164
00165
00166
00167
00168
00169
00170 F0=F2
00171
00172
00173 if ((i+1).GT.maxNewtonIter) then
00174 print*,'!!!----------------------------------------------------------------------'
00175 print*,'Newtons method failed to converge in ',maxNewtonIter,'iterations'
00176 print*,'current norm is ',fnorm(i)
00177 print*,'convergence tolerance is',convergenceTolerance
00178
00179 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00180 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00181 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00182
00183 print*,'!!!------------------------------------------------------------------------'
00184
00185 NumberOfCalculatedNorms=i
00186
00187 exit
00188 end if
00189
00190
00191 fnorm(i+1)=fTwo*2.d0
00192
00193
00194 if (fnorm(i+1).le.convergenceTolerance) then
00195
00196 print*,'---------------------------------------------------------------------------'
00197 print*,'newton converged @ iter=',i
00198 print*,'current norm is ',fnorm(i+1)
00199 print*,'convergence tolerance is',convergenceTolerance
00200
00201 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00202 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00203 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00204
00205 print*,'---------------------------------------------------------------------------'
00206 newtonSuccess=.true.
00207 NumberOfCalculatedNorms=i+1
00208 exit
00209
00210 end if
00211
00212
00213 fZero=fTwo
00214
00215 if (enableNewtonBasic.EQV.ON) print*,' |----> current norm=',fnorm(i+1)
00216
00217
00218
00219
00220
00221
00222 cycle
00223
00224 else
00225
00226 dummyBacktrackCount=1
00227
00228
00229
00230 lambda1=quadratic(slope,fZero,fTwo)
00231
00232
00233 if (lambda1.lt.0.1d0*lambda) then
00234
00235 lambda1=0.1d0*lambda
00236
00237 elseif (lambda1.gt.0.5d0*lambda) then
00238
00239 lambda1=0.5d0*lambda;
00240
00241 end if
00242
00243
00244
00245
00246 call F(X+lambda1*deltaX,F1,n)
00247
00248 fOne=0.5d0*dot_product(F1,F1)
00249
00250
00251 if (fOne .lt. fZero + alpha*lambda*slope) then
00252
00253 lambda=lambda1
00254 X=X+lambda*deltaX
00255 lambdaVec(i)=lambda
00256
00257 if (enableNewtonDebug.EQV.ON) print*,'valid alpha condition (quadratic) in iter',i
00258
00259
00260
00261
00262
00263
00264 F0=F1
00265
00266
00267 if ((i+1).GT.maxNewtonIter) then
00268 print*,'!!!----------------------------------------------------------------------'
00269 print*,'Newtons method failed to converge in ',maxNewtonIter,'iterations'
00270 print*,'current norm is ',fnorm(i)
00271 print*,'convergence tolerance is',convergenceTolerance
00272
00273 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00274 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00275 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00276
00277 print*,'!!!------------------------------------------------------------------------'
00278 NumberOfCalculatedNorms=i
00279
00280 exit
00281 end if
00282
00283
00284 fnorm(i+1)=fOne*2.d0
00285
00286
00287 if (fnorm(i+1).le.convergenceTolerance) then
00288
00289 print*,'---------------------------------------------------------------------------'
00290 print*,'newton converged @ iter=',i
00291 print*,'current norm is ',fnorm(i+1)
00292 print*,'convergence tolerance is',convergenceTolerance
00293
00294 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00295 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00296 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00297
00298 print*,'---------------------------------------------------------------------------'
00299 NumberOfCalculatedNorms=i+1
00300 newtonSuccess=.true.
00301 exit
00302
00303 end if
00304
00305
00306
00307
00308 fZero=fOne
00309
00310 if (enableNewtonBasic.EQV.ON) print*,' |----> current norm=',fnorm(i+1)
00311
00312
00313
00314
00315
00316 cycle
00317
00318 else
00319
00320
00321 do while (fOne .gt. fZero + alpha*lambda*slope)
00322
00323 if (enableNewtonDebugExtra.EQV.ON) print*,'cubic backtrack number ', dummyBacktrackCount
00324
00325
00326 lambdaTest=cubic(slope,fZero,fOne,fTwo,lambda1,lambda2)
00327
00328 if (lambdaTest.lt.0.1d0*lambda1) then
00329
00330 lambdaTest=0.1d0*lambda1
00331
00332 elseif (lambdaTest.gt.0.5d0*lambda1) then
00333
00334 lambdaTest=0.5d0*lambda1
00335
00336 end if
00337
00338 fTwo=fOne;
00339 lambda2=lambda1
00340
00341 lambda1=lambdaTest
00342
00343
00344
00345 call F(X+lambda1*deltaX,F1,n)
00346
00347 fOne=0.5d0*dot_product(F1,F1)
00348
00349
00350
00351
00352 dummyBacktrackCount=dummyBacktrackCount+1
00353
00354
00355 if (dummyBacktrackCount.GT.maxBacktrackCount) then
00356
00357 if (enableNewtonDebugExtra.EQV.ON) print*,'too many backtracks=',maxBacktrackCount
00358 exit
00359
00360 end if
00361
00362
00363
00364
00365 end do
00366
00367
00368 lambda=lambda1
00369
00370 X=X+lambda*deltaX
00371 lambdaVec(i)=lambda
00372
00373 if (enableNewtonDebug.EQV.ON) print*,'valid alpha condition (cubic) in iter',i
00374
00375
00376
00377
00378
00379
00380 F0=F1
00381
00382
00383 if ((i+1).GT.maxNewtonIter) then
00384 print*,'!!!----------------------------------------------------------------------'
00385 print*,'Newtons method failed to converge in ',maxNewtonIter,'iterations'
00386 print*,'current norm is ',fnorm(i+1)
00387 print*,'convergence tolerance is',convergenceTolerance
00388
00389 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00390 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00391 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00392
00393 print*,'!!!------------------------------------------------------------------------'
00394 NumberOfCalculatedNorms=i
00395 exit
00396 end if
00397
00398
00399 fnorm(i+1)=fOne*2.d0
00400
00401
00402 if (fnorm(i+1).le.convergenceTolerance) then
00403
00404 print*,'---------------------------------------------------------------------------'
00405 print*,'newton converged @ iter=',i
00406 print*,'current norm is ',fnorm(i+1)
00407 print*,'convergence tolerance is',convergenceTolerance
00408
00409 if (enableNewtonDebug.EQV.ON) print*,'convergence option is', NewtonConvergenceOption
00410 if (enableNewtonDebugExtra.EQV.ON) print*,'absolute residual=',fnorm(i+1)
00411 if (enableNewtonDebugExtra.EQV.ON) print*,'relative residual=',fnorm(i+1)/initialNorm
00412
00413 print*,'---------------------------------------------------------------------------'
00414 NumberOfCalculatedNorms=i+1
00415 newtonSuccess=.true.
00416 exit
00417
00418 end if
00419
00420
00421 fZero=fOne
00422
00423 if (enableNewtonBasic.EQV.ON) print*,' |----> current norm=',fnorm(i+1)
00424
00425
00426
00427
00428
00429 end if
00430
00431
00432 end if
00433
00434
00435
00436
00437
00438 end do
00439
00440
00441
00442
00443
00444
00445
00446
00447
00448 910 FORMAT(I4,2(E18.10,1X))
00449
00450
00451 select case(NewtonConvergenceOption)
00452
00453 case(1)
00454
00455
00456 Open (1,File='NewtonResidual.dat',Status='unknown')
00457 WRITE(1,*) 'TITLE="results"'
00458 WRITE(1,*) 'VARIABLES="iter","||f||"'
00459 WRITE(1,*) 'ZONE I=', NumberOfCalculatedNorms
00460 DO j= 1, NumberOfCalculatedNorms
00461 WRITE(1,910) j, fnorm(j)
00462 END DO
00463 close(1)
00464
00465
00466 case(2)
00467
00468 Open (1,File='NewtonResidual.dat',Status='unknown')
00469 WRITE(1,*) 'TITLE="results"'
00470 WRITE(1,*) 'VARIABLES="iter","||f||"'
00471 WRITE(1,*) 'ZONE I=', NumberOfCalculatedNorms
00472 DO j= 1, NumberOfCalculatedNorms
00473 WRITE(1,910) j, fnorm(j)/fnorm(1)
00474 END DO
00475 close(1)
00476
00477 end select
00478
00479
00480 end subroutine newton