-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaxl_solvers.f90
1091 lines (1069 loc) · 37.6 KB
/
axl_solvers.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
MODULE axl_solvers
!
! It contains algorithms and numerical methods used for the inversion of the
! Span-Wagner EoS, necessary for the evaluation of properties in each node of the
! grid.
!
! It contains a 2D Newton-Raphson algorithm, a 1D Newton-Raphson algorithm, a
! Brent algorithm and the corresponding functions of which we seek the zeros.
!==============================================================================
!==============================================================================
!
USE def_constants
USE properties
!USE def_variables, ONLY: vL_psat_spline, vV_psat_spline,&
! &uL_psat_spline, uV_psat_spline,&
! &Tsat_psat_spline, saturP
!USE Interp_table
IMPLICIT NONE
!
!PRIVATE
!PUBLIC New_Rap2D, New_Rap3D, New_Rap1D, BrentRoots
CONTAINS
!
!
!
!===============================================================================
SUBROUTINE New_Rap2Daxl(MODE, out1_NewRap, out2_NewRap, &
& resnorm, Niter, exitflag, in_1, in_2,in_3,in_4, guess_1, guess_2)
!===============================================================================
!
IMPLICIT NONE
!
! Global variables
INTEGER, INTENT(IN) :: MODE
INTEGER, INTENT(OUT) :: exitflag, Niter
REAL(pr),INTENT(IN) :: guess_1, guess_2, in_1, in_2,in_3,in_4
REAL(pr),INTENT(OUT) :: out1_NewRap, out2_NewRap, resnorm
!
! Local variables
INTEGER :: n, nx, nf, INFO, MAX_ITER
INTEGER,POINTER :: IPIV(:)
REAL(8) :: x, TOL_X, TOL_FUN, ALPHA, MIN_LAMBDA, MAX_LAMBDA,&
& Dx_J, lambda, slope, fffold, lambda_min, fff, &
& fff2, A_l, lambda2, lambda_OLD, aa, bb, discriminant
REAL(8), DIMENSION (2,1) :: XXX, F, delta, XXX_forw, XXX_back,&
& F_forw, F_back, dF, TYPX, dx_star, dx, XXX_old, C_l, aabb
REAL(8), DIMENSION (1,2) :: g
REAL(8), DIMENSION (2,2) :: J, J0, Jstar, AAA, B_l
!
! ------------- INITIALIZATION ----------
XXX(1,1) = guess_1
XXX(2,1) = guess_2
!
! Options
!
TOL_X = 1.d-20 ! relative max step tolerance-20
TOL_FUN = 1.d-12 ! function tolerance -12
MAX_ITER = 500 ! max number of iterations
ALPHA = 1.d-4 ! criteria for decrease -4
MIN_LAMBDA = 0.1d0 ! min lambda 0.1,0.5
MAX_LAMBDA = 0.5d0
TYPX = abs(XXX) ! x scaling value, remove zeros
!
!
call F_New_Rap2D (MODE, F, XXX, in_1, in_2,in_3,in_4)
!
! ---- Jacobian estimation
!
Dx_J = 0.7d-9 ! finite difference delta
nx = 2 ! degrees of freedom
nf = 2 ! number of functions
DO n = 1,nx
delta(1,1) = 0.d0; delta(2,1) = 0.d0
delta(n,1) = delta(n,1) + Dx_J
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap2D (MODE, F_forw, XXX_forw, in_1, in_2,in_3,in_4)
call F_New_Rap2D (MODE, F_back, XXX_back, in_1, in_2,in_3,in_4)
dF = F_forw - F_back ! delta F
J(1, n) = dF(1,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(2, n) = dF(2,1)/Dx_J/2.d0 ! derivatives dF/d_n
ENDDO
J0(1,1) = 1.d0 / TYPX(1,1) ! Jacobian scaling matrix
J0(1,2) = 1.d0 / TYPX(2,1)
J0(2,1) = J0(1,1)
J0(2,2) = J0(1,2)
Jstar = J/J0; ! scale Jacobian
!
resnorm = sqrt(F(1,1)*F(1,1) + F(2,1)*F(2,1))
exitflag = 1
!
!--- SOLVER -------------------
!
Niter = 0 ! start counter
lambda = 1.d0 ! backtracking
lambda_OLD = lambda
!
allocate(IPIV(nf))
!
1 DO WHILE (((resnorm .GT. TOL_FUN) .OR. (lambda .LT. 1.d0)) &
& .AND. (exitflag >= 0) .AND. (Niter <= MAX_ITER))
Niter = Niter + 1
!
IF (lambda==1.d0) THEN
! Newton-Raphson solver
! dx_star = -Jstar\F calculate Newton step
AAA = -Jstar
dx_star = F
!call PLU decomposition routine from LAPACK
call DGETRF( nf, nf, AAA,nf, IPIV, INFO)
call DGETRS('N', nf,1,AAA,nf, IPIV, dx_star,nf,INFO)
dx = dx_star*TYPX
g = MATMUL(TRANSPOSE(F),Jstar)
slope = g(1,1)*dx_star(1,1) + g(1,2)*dx_star(2,1)
fffold = SUM(F*F)
XXX_old = XXX
lambda_min = TOL_X/MAXVAL(ABS(dx)/ABS(XXX_old))
ENDIF
!
! print*, 'LAMBDA = ', lambda, 'resnorm',resnorm
IF (lambda < lambda_min) THEN
exitflag = -2; ! XXX is too close to XXX_OLD
! print*,'XXX is too close to XXX_OLD'
!ELSEIF any(isnan(dx)) || any(isinf(dx))
! exitflag = -1; ! matrix may be singular
! STOP
ENDIF
!
! XXX at new step
!
XXX = XXX_old + dx*lambda
! print*, 'resnorm',resnorm
! print*,'dx*lambda= ',dx*lambda
! print*,'xxx', XXX
! print*, 'lambda_min', lambda_min
call F_New_Rap2D (MODE, F, XXX, in_1, in_2,in_3,in_4)
!
! ---- Jacobian estimation
!
DO n = 1,nx
delta(1,1) = 0.d0; delta(2,1) = 0.d0
delta(n,1) = delta(n,1) + Dx_J
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap2D (MODE, F_forw, XXX_forw, in_1, in_2,in_3,in_4)
call F_New_Rap2D (MODE, F_back, XXX_back, in_1, in_2,in_3,in_4)
dF = F_forw - F_back ! delta F
J(1, n) = dF(1,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(2, n) = dF(2,1)/Dx_J/2.d0 ! derivatives dF/d_n
ENDDO
Jstar = J/J0 ! scale Jacobian
fff = SUM(F*F) ! f to be minimized
!
! Optimization
!
IF (fff > fffold + ALPHA*lambda*slope) THEN
IF (lambda .eq. 1.d0) THEN
lambda = -slope/2.d0/(fff-fffold-slope) ! calculate lambda
ELSE
A_l = 1.d0/(lambda_OLD - lambda2)
B_l(1,1) = 1.d0/lambda_OLD**2.d0
B_l(1,2) = -1.d0/lambda2**2.d0
B_l(2,1) = -lambda2/lambda_OLD**2.d0
B_l(2,2) = lambda_OLD/lambda2**2.d0
C_l(1,1) = fff-fffold-lambda_OLD*slope
C_l(2,1) = fff2-fffold-lambda2*slope
!
aabb = A_l* MATMUL(B_l,C_l)
aa = aabb(1,1)
bb = aabb(2,1)
IF (aa == 0.d0) THEN
lambda = -slope/2.d0/bb;
ELSE
discriminant = bb**2.d0 - 3.d0*aa*slope;
IF (discriminant < 0.d0) THEN
lambda = MAX_LAMBDA*lambda_OLD;
ELSEIF (bb <= 0.d0) THEN
lambda = (-bb + sqrt(discriminant))/3.d0/aa
ELSE
lambda = -slope/(bb + sqrt(discriminant))
ENDIF
ENDIF
lambda = min(lambda,MAX_LAMBDA*lambda_OLD)! minimum step length
ENDIF
ELSE
lambda = 1.d0; ! fraction of Newton step
ENDIF
IF (lambda < 1.d0) THEN
lambda2 = lambda_OLD;
fff2 = fff; ! save 2nd most previous value
lambda = max(lambda,MIN_LAMBDA*lambda_OLD) ! minimum step length
GO TO 1
ENDIF
!
!
resnorm = sqrt(F(1,1)*F(1,1) + F(2,1)*F(2,1))
!
ENDDO
!
deallocate(IPIV)
out1_NewRap = XXX(1,1)
out2_NewRap = XXX(2,1)
END SUBROUTINE New_Rap2Daxl
!
!
!
SUBROUTINE F_New_Rap2D (MODE, F, XXX, in_1, in_2,in_3,in_4)
IMPLICIT NONE
INTEGER, INTENT(IN) :: MODE
REAL(pr), INTENT(IN) :: in_1, in_2, in_3,in_4
REAL(pr), DIMENSION (2,1), INTENT(IN) :: XXX
REAL(pr), DIMENSION (2,1), INTENT(OUT) :: F
REAL(pr), DIMENSION (2,1) :: F_out
REAL(pr) :: F1_MODE, F2_MODE, Helmholtz, T, v, p, e, cv, cp, s, c, &
& v_l, p_l, s_l, e_l, g_l, v_v, p_v, s_v, e_v, g_v,&
& helmho_v,helmho_l,deriv,dummy1,dummy2,x_out,a_out,entro,enth
REAL(pr) :: psat, Tsat, vvsat, vlsat, uvsat, ulsat,sv,sl,qual,velo,rhoinv
REAL(pr) :: p1,p2,p3,duL_dp, duV_dp, dvL_dp, dvV_dp,sound,du_dp_x,dv_dp_x,ratio
!
IF (MODE .EQ. 1) THEN
! Calculation of the saturation specific volumes of vapor
! and liquid phase.
! The saturation specific volume is obtained by imposing:
! p_l = p_v pressure equality
! g_l = g_v Gibbs free enthalpy equality
!
! That is the Maxwell criterion for saturation.
!
v_l = XXX(1,1)
v_v = XXX(2,1)
T = in_1
!
CALL pressure(T,v_l,p_l)
CALL pressure(T,v_v,p_v)
!
CALL helmho(T,v_l,helmho_l)
CALL helmho(T,v_v,helmho_v)
! g_l = Helmholtz(v_l,T) + p_l*v_l*1e-3_pr ! kJ / kg
! g_v = Helmholtz(v_v,T) + p_v*v_v*1e-3_pr ! kJ / kg
!
g_l = helmho_l + p_l*v_l ! J / kg
g_v = helmho_v + p_v*v_v
F(1,1) = (p_l - p_v)*1e-6_pr
F(2,1) = (g_l - g_v)*1e-3_pr
!
ELSEIF (MODE .EQ. 2) THEN
! Calculation of temperature and specific volume for imposed pressure p
! and specific internal energy, e.
!
! For this function:
! in_1 = p
! in_2 = e
! XXX(1,1) = T
! XXX(2,1) = v
!
!
T = XXX(1,1)
v = XXX(2,1)
CALL pressure(T,v,p)
CALL inter_energy(T,v,e)
F(1,1) = (in_1 - p)*1e-6_pr ! Pa
F(2,1) = (in_2 - e)*1e-3_pr ! J/kg
!print*, in_1,in_2, p,e
!
!
ELSEIF (MODE .EQ. 3) THEN
! META liquid: dpdv_T = 0 and e(t,v) = e_in
! For this function:
! in_1 = e
! in_2 =
! XXX(1,1) = T
! XXX(2,1) = v_l
!
!
T = XXX(1,1)
v_l = XXX(2,1)
!
CALL inter_energy(T,v_l,e)
CALL dpdv_T(T,v_l,deriv)
!
F(1,1) = deriv * 1e-6_pr
F(2,1) = (in_1 - e) *1e-3_pr
!
!
ELSEIF (MODE .EQ. 4) THEN
! isentropic
psat = XXX(1,1)
rhoinv = XXX(2,1)
! s = in1
! htot = in_2
! dA = in_3
! u(i-1) = in_4
!
CALL satprop(3, psat, Tsat, vvsat, vlsat, uvsat, ulsat)
CALL axlpress(Tsat,vvsat,vlsat,p1,p2,p3)
psat = (psat+p1+p2+p3)/4.0
CALL satprop(3, psat, Tsat, vvsat, vlsat, uvsat, ulsat)
CALL entropy(Tsat, vvsat, sv)
CALL entropy(Tsat, vlsat, sl)
!
qual = abs((rhoinv - vlsat) / (vvsat-vlsat))
! print*, 'v',rhoinv, 'qual',qual, vlsat
CALL satderiv(3, psat, duL_dp, duV_dp, dvL_dp, dvV_dp)
!!
ratio = ((uvsat - ulsat)/(vvsat - vlsat)) ! (J/kg)/(m3/kg)
du_dp_x = x_out * duV_dp + (1_pr - x_out) * duL_dp
dv_dp_x = x_out * dvV_dp + (1_pr - x_out) * dvL_dp
!!
sound = SQRT((psat + ratio)/(du_dp_x - ratio * dv_dp_x)) * vlsat
velo = 1.0/((in_4/sound)-1) * in_4*in_3 + in_4
! print*, 'velo', velo
! velo = in_3 * rhoinv
enth = in_2 - 0.5*velo*velo
! print*, 'p,x',psat,qual,'T',Tsat
! print*, 'Sv',sv, 'Sl',sl
! print*, ' '
F(1,1) = (in_1 - qual*sv + (1.0-qual)*sl) *1e-3_pr
F(2,1) = (enth - (uvsat+psat*vvsat)*qual - (ulsat+psat*vlsat)*(1.0-qual)) *1e-3_pr
ELSE
print*,'MODE of New_Rap2D unknown'
STOP
ENDIF
!
END SUBROUTINE F_New_Rap2D
!
!
!===============================================================================
SUBROUTINE New_Rap3Daxl(MODE, out1_NewRap, out2_NewRap, out3_NewRap, &
& resnorm, Niter, exitflag, in_1,in_2, guess_1, guess_2, guess_3)
!===============================================================================
!
IMPLICIT NONE
!
! Global variables
INTEGER, INTENT(IN) :: MODE
INTEGER, INTENT(OUT) :: exitflag, Niter
REAL(pr),INTENT(IN) :: guess_1, guess_2, guess_3, in_1, in_2!,in_3
REAL(pr),INTENT(OUT) :: out1_NewRap, out2_NewRap, out3_NewRap, resnorm
!
! Local variables
INTEGER :: n, nx, nf, INFO, MAX_ITER
INTEGER,POINTER :: IPIV(:)
REAL(8) :: x, TOL_X, TOL_FUN, ALPHA, MIN_LAMBDA, MAX_LAMBDA,&
& Dx_J, lambda, slope, fffold, lambda_min, fff, &
& fff2, A_l, lambda2, lambda_OLD, aa, bb, discriminant
REAL(8), DIMENSION (3,1) :: XXX, F, delta, XXX_forw, XXX_back,&
& F_forw, F_back, dF, TYPX, dx_star, dx, XXX_old, C_l, aabb
REAL(8), DIMENSION (1,3) :: g
REAL(8), DIMENSION (3,3) :: J, J0, Jstar, AAA, B_l
!
! ------------- INITIALIZATION ----------
XXX(1,1) = guess_1
XXX(2,1) = guess_2
XXX(3,1) = guess_3
!
! Options
!
TOL_X = 1.d-20 ! relative max step tolerance
TOL_FUN = 1.d-12 ! function tolerance
MAX_ITER = 500 ! max number of iterations
ALPHA = 1.d-4 ! criteria for decrease
MIN_LAMBDA = 0.1d0 ! min lambda
MAX_LAMBDA = 0.5d0
TYPX = abs(XXX) ! x scaling value, remove zeros
!
!
call F_New_Rap3D (MODE, F, XXX, in_1,in_2)
!
! ---- Jacobian estimation
!
Dx_J = 1.d-6 ! finite difference delta
nx = 3 ! degrees of freedom
nf = 3 ! number of functions
DO n = 1,nx
delta(1,1) = 0.d0; delta(2,1) = 0.d0; delta(3,1) = 0.d0
delta(n,1) = delta(n,1) + Dx_J
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap3D (MODE, F_forw, XXX_forw, in_1,in_2)
call F_New_Rap3D (MODE, F_back, XXX_back, in_1,in_2)
dF = F_forw - F_back ! delta F
J(1, n) = dF(1,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(2, n) = dF(2,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(3, n) = dF(3,1)/Dx_J/2.d0 ! derivatives dF/d_n
ENDDO
J0(1,1) = 1.d0 / TYPX(1,1) ! Jacobian scaling matrix
J0(1,2) = 1.d0 / TYPX(2,1)
J0(1,3) = 1.d0 / TYPX(3,1)
J0(2,1) = J0(1,1)
J0(2,2) = J0(1,2)
J0(2,3) = J0(1,3)
J0(3,1) = J0(1,1)
J0(3,2) = J0(1,2)
J0(3,3) = J0(1,3)
Jstar = J/J0; ! scale Jacobian
!
resnorm = sqrt(F(1,1)*F(1,1) + F(2,1)*F(2,1)+ F(3,1)*F(3,1))
exitflag = 1
!
!--- SOLVER -------------------
!
Niter = 0 ! start counter
lambda = 1.d0 ! backtracking
lambda_OLD = lambda
!
allocate(IPIV(nf))
!
1 DO WHILE (((resnorm .GT. TOL_FUN) .OR. (lambda .LT. 1.d0)) &
& .AND. (exitflag >= 0) .AND. (Niter <= MAX_ITER))
Niter = Niter + 1
!
IF (lambda==1.d0) THEN
! Newton-Raphson solver
! dx_star = -Jstar\F calculate Newton step
AAA = -Jstar
dx_star = F
!call PLU decomposition routine from LAPACK
call DGETRF( nf, nf, AAA,nf, IPIV, INFO)
call DGETRS('N', nf,1,AAA,nf, IPIV, dx_star,nf,INFO)
dx = dx_star*TYPX
g = MATMUL(TRANSPOSE(F),Jstar)
slope = g(1,1)*dx_star(1,1) + g(1,2)*dx_star(2,1) +&
& g(1,3)*dx_star(3,1)
fffold = SUM(F*F)
XXX_old = XXX
lambda_min = TOL_X/MAXVAL(ABS(dx)/ABS(XXX_old))
ENDIF
!
! print*, 'LAMBDA = ', lambda, 'resnorm',resnorm
IF (lambda < lambda_min) THEN
exitflag = -2; ! XXX is too close to XXX_OLD
! print*,'XXX is too close to XXX_OLD'
!ELSEIF any(isnan(dx)) || any(isinf(dx))
! exitflag = -1; ! matrix may be singular
! STOP
ENDIF
!
! XXX at new step
!
XXX = XXX_old + dx*lambda
! print*, 'resnorm',resnorm
! print*,'dx*lambda= ',dx*lambda
! print*,'xxx', XXX
! print*, 'lambda_min', lambda_min
call F_New_Rap3D (MODE, F, XXX, in_1,in_2)
!
! ---- Jacobian estimation
!
DO n = 1,nx
delta(1,1) = 0.d0; delta(2,1) = 0.d0; delta(3,1)=0.d0
delta(n,1) = delta(n,1) + Dx_J
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap3D (MODE, F_forw, XXX_forw, in_1,in_2)
call F_New_Rap3D (MODE, F_back, XXX_back, in_1,in_2)
dF = F_forw - F_back ! delta F
J(1, n) = dF(1,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(2, n) = dF(2,1)/Dx_J/2.d0 ! derivatives dF/d_n
J(3, n) = dF(3,1)/Dx_J/2.d0 ! derivatives dF/d_n
ENDDO
Jstar = J/J0 ! scale Jacobian
fff = SUM(F*F) ! f to be minimized
!
! Optimization
!
IF (fff > fffold + ALPHA*lambda*slope) THEN
IF (lambda .eq. 1.d0) THEN
lambda = -slope/2.d0/(fff-fffold-slope) ! calculate lambda
ELSE
A_l = 1.d0/(lambda_OLD - lambda2)
B_l(1,1) = 1.d0/lambda_OLD**2.d0
B_l(1,2) = -1.d0/lambda2**2.d0
B_l(2,1) = -lambda2/lambda_OLD**2.d0
B_l(2,2) = lambda_OLD/lambda2**2.d0
C_l(1,1) = fff-fffold-lambda_OLD*slope
C_l(2,1) = fff2-fffold-lambda2*slope
!
aabb = A_l* MATMUL(B_l,C_l)
aa = aabb(1,1)
bb = aabb(2,1)
IF (aa == 0.d0) THEN
lambda = -slope/2.d0/bb;
ELSE
discriminant = bb**2.d0 - 3.d0*aa*slope;
IF (discriminant < 0.d0) THEN
lambda = MAX_LAMBDA*lambda_OLD;
ELSEIF (bb <= 0.d0) THEN
lambda = (-bb + sqrt(discriminant))/3.d0/aa
ELSE
lambda = -slope/(bb + sqrt(discriminant))
ENDIF
ENDIF
lambda = min(lambda,MAX_LAMBDA*lambda_OLD)! minimum step length
ENDIF
ELSE
lambda = 1.d0; ! fraction of Newton step
ENDIF
IF (lambda < 1.d0) THEN
lambda2 = lambda_OLD;
fff2 = fff; ! save 2nd most previous value
lambda = max(lambda,MIN_LAMBDA*lambda_OLD) ! minimum step length
GO TO 1
ENDIF
!
!
resnorm = sqrt(F(1,1)*F(1,1) + F(2,1)*F(2,1) + F(3,1)*F(3,1))
!
ENDDO
!
deallocate(IPIV)
out1_NewRap = XXX(1,1)
out2_NewRap = XXX(2,1)
out3_NewRap = XXX(3,1)
END SUBROUTINE New_Rap3Daxl
!
!
!
SUBROUTINE F_New_Rap3D (MODE, F, XXX, in_1,in_2)
IMPLICIT NONE
INTEGER, INTENT(IN) :: MODE
REAL(pr), INTENT(IN) :: in_1, in_2!, in_3
REAL(pr), DIMENSION (3,1), INTENT(IN) :: XXX
REAL(pr), DIMENSION (3,1), INTENT(OUT) :: F
REAL(pr), DIMENSION (3,1) :: F_out
REAL(pr) :: F1_MODE, F2_MODE, Helmholtz, T, v, p, e, cv, cp, s, c, &
& v_l, p_l, s_l, e_l, g_l, v_v, p_v, s_v, e_v, g_v,&
& helmho_v,helmho_l,x,y,z,qual
!
IF (MODE .EQ. 1) THEN
! FOR LL right boundary (liquid saturation line):
! p_l = p_v pressure equality
! g_l = g_v Gibbs free enthalpy equality
! el = e at one interal energy
! That is the Maxwell criterion for saturation.
!
v_l = XXX(1,1)
v_v = XXX(2,1)
T = XXX(3,1)
e = in_1
!
CALL pressure(T,v_l,p_l)
CALL pressure(T,v_v,p_v)
!
CALL helmho(T,v_l,helmho_l)
CALL helmho(T,v_v,helmho_v)
!
CALL inter_energy(T,v_l,e_l)
!
g_l = helmho_l + p_l*v_l ! J / kg
g_v = helmho_v + p_v*v_v
F(1,1) = (p_l - p_v)*1e-6_pr
F(2,1) = (g_l - g_v)*1e-3_pr
F(3,1) = (e_l - e)*1e-3_pr
!cas test for 3D solver
ELSEIF (MODE .EQ. 2) THEN
x= XXX(1,1)
y= XXX(2,1)
z= XXX(3,1)
!
F(1,1) = sin(x)
F(2,1) = cos(y)
F(3,1) = z*z*z - 6_pr*z*z + 11_pr*z -6
!
!
ELSEIF (MODE .EQ. 3) THEN
! FOR TP region
! p=p_l pressure equality
! p =p_v pressure equality
! g_l = g_v Gibbs free enthalpy equality
! That is the Maxwell criterion for saturation.
!
v_l = XXX(1,1)
v_v = XXX(2,1)
T = XXX(3,1)
p = in_1
!
CALL pressure(T,v_l,p_l)
CALL pressure(T,v_v,p_v)
!
CALL helmho(T,v_l,helmho_l)
CALL helmho(T,v_v,helmho_v)
!
!
g_l = helmho_l + p_l*v_l ! J / kg
g_v = helmho_v + p_v*v_v
!
F(1,1) = (p_l - p_v)*1e-6_pr
F(2,1) = (p_v - p)*1e-6_pr
F(3,1) = (g_l - g_v)*1e-3_pr
ELSEIF (MODE .EQ. 4) THEN
! FOR LH right boundary (vapor saturation line)
! p_l = p_v pressure equality
! g_l = g_v Gibbs free enthalpy equality
! ev = e at one interal energy
! That is the Maxwell criterion for saturation.
!
v_l = XXX(1,1)
v_v = XXX(2,1)
T = XXX(3,1)
e = in_1
!
CALL pressure(T,v_l,p_l)
CALL pressure(T,v_v,p_v)
!
CALL helmho(T,v_l,helmho_l)
CALL helmho(T,v_v,helmho_v)
!
CALL inter_energy(T,v_v,e_v)
!
g_l = helmho_l + p_l*v_l ! J / kg
g_v = helmho_v + p_v*v_v
F(1,1) = (p_l - p_v)*1e-6_pr
F(2,1) = (g_l - g_v)*1e-3_pr
F(3,1) = (e_v - e)*1e-3_pr
!
ELSEIF (MODE .EQ. 5) THEN
! for a given input couple of values of specific internal energy and
! specific volume, the temperature, vapor specific volume, liquid specific
! volume, are evalued at sauration and the corresponding quality is
! computed. (TP region)
! (v,e)--> (Vv, Vl, T, x)
! F1 = pv = pl
! F2 = gv = gl
! F3 = e - x*ev - (1-x)*el
! x = (v-Vl)/(Vv - Vl)
!
v_l = XXX(1,1)
v_v = XXX(2,1)
T = XXX(3,1)
e = in_1
v = in_2
!
CALL pressure(T,v_l,p_l)
CALL pressure(T,v_v,p_v)
!
CALL helmho(T,v_l,helmho_l)
CALL helmho(T,v_v,helmho_v)
!
CALL inter_energy(T,v_v,e_v)
CALL inter_energy(T,v_l,e_l)
!
qual = (v - v_l)/(v_v-v_l)
g_l = helmho_l + p_l*v_l ! J / kg
g_v = helmho_v + p_v*v_v
!
F(1,1) = (p_l - p_v)*1e-6_pr
F(2,1) = (g_l - g_v)*1e-3_pr
F(3,1) = (e - qual*e_v - (1_pr - qual)*e_l)*1e-3_pr
!
ELSE
print*,'MODE of New_Rap3D unknown'
STOP
ENDIF
END SUBROUTINE F_New_Rap3D
!
!===============================================================================
SUBROUTINE New_Rap1Daxl(MODE, out_1, out_2, resnorm, Niter,&
& exitflag, GGG, X0, in_1, out3)
!===============================================================================
! 1D TWO-PHASE or SINGLE-PHASE
!-------------------------------------------------------------------
!
! Input :
! -------
! MODE = it will depend on the used function
! GGG = idem
! in_1 = idem
! in_2 = idem
! X0 = idem
!
!
! Output :
! -------
!
! out_1 = idem
! out_2 = idem
!
!-------------------------------------------------------------------
! M. De Lorenzo 03/2016
!-------------------------------------------------------------------
!
!
IMPLICIT NONE
!
INTEGER :: MAX_ITER
INTEGER, INTENT(IN) :: MODE
INTEGER, INTENT(OUT) :: exitflag, Niter
REAL(8), INTENT(IN) :: GGG, X0, in_1
REAL(8), INTENT(OUT) :: out_1, out_2, resnorm, out3
REAL(8) :: TOL_X, TOL_FUN, ALPHA, MIN_LAMBDA, MAX_LAMBDA, Jstar,&
& lambda, slope, fffold, lambda_min, fff, fff2, A_l, aa, bb, Dx_J,&
& discriminant, lambda2, lambda_OLD, XXX, F, delta, XXX_forw, J0,&
& XXX_back, F_forw, F_back, dF, TYPX, dx_star, dx, XXX_old, g, J
REAL(8), DIMENSION (2,2) :: B_l
REAL(8), DIMENSION (2,1) :: C_l, aabb
!
!
XXX = X0 ! Initial value
!
! Options
!
TOL_X = 1d-20 ! relative max step tolerance
TOL_FUN = 1d-12 ! function tolerance
MAX_ITER = 500 ! max number of iterations
ALPHA = 1d-4 ! criteria for decrease
MIN_LAMBDA = 1d-1 ! min lambda
MAX_LAMBDA = 5d-1
TYPX = abs(XXX) ! x scaling value, remove zeros
!
! ---- Function F definition
! F = GGG_input - GGG
!
call F_New_Rap1D(MODE, F, out_2, XXX, GGG, in_1, out3)
!
!
! --------- Jacobian estimation-----------------
!
Dx_J = 1d-6 ! finite difference delta
delta = Dx_J
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap1D(MODE, F_forw, out_2, XXX_forw, GGG, in_1, out3)
call F_New_Rap1D(MODE, F_back, out_2, XXX_back, GGG, in_1, out3)
dF = F_forw - F_back ! delta F
J = dF/Dx_J/2d0 ! derivatives dF/d_n
J0 = 1d0 / TYPX ! Jacobian scaling factor
Jstar = J/J0; ! scale Jacobian
resnorm = abs(F) ! Norm of residues
exitflag = 1
!
!
!--- SOLVER -------------------
!
Niter = 0
lambda = 1d0 ! backtracking
lambda_OLD = lambda
!
!
!
1 DO WHILE ( ((resnorm>TOL_FUN) .OR. (lambda<1d0)) .AND. &
& (exitflag>=0) .AND. (Niter<=MAX_ITER))
Niter = Niter + 1
!
!--------- Newton-Raphson solver
!
IF (lambda .eq. 1.0d0) THEN
dx_star = -F/Jstar ! calculate Newton step
dx = dx_star*TYPX
g = F*Jstar
slope = g*dx_star
fffold = F*F
XXX_old = XXX
lambda_min = TOL_X/(ABS(dx)/ABS(XXX_old))
ENDIF
!
!--------- Check about proximity of XXX and XXX_OLD
!
IF (lambda < lambda_min) THEN
exitflag = 2;
!print*,'XXX is too close to XXX_OLD'
!EXIT ! OUT NewRap
ENDIF
!
!--------- Eventually, backtracking of New-Rap step
!
XXX = XXX_old + dx*lambda
call F_New_Rap1D(MODE, F, out_2, XXX, GGG, in_1, out3)
!
!--------- Jacobian estimation
!
XXX_forw = XXX + delta
XXX_back = XXX - delta
call F_New_Rap1D(MODE, F_forw, out_2, XXX_forw, GGG, in_1,&
& out3)
call F_New_Rap1D(MODE, F_back, out_2, XXX_back, GGG, in_1,&
& out3)
dF = F_forw - F_back ! delta F
J = dF/Dx_J/2d0 ! derivatives dF/d_n
Jstar = J/J0 ! scale Jacobian
fff = F*F ! f to be minimized
!
!--------- Optimization technique (minimizing fff = SUM(F*F) )
!
IF (fff > fffold + ALPHA*lambda*slope) THEN
IF (lambda .eq. 1d0) THEN
lambda = -slope/2d0/(fff-fffold-slope) ! calculate lambda
ELSE
A_l = 1d0/(lambda_OLD - lambda2)
B_l(1,1) = 1d0/lambda_OLD/lambda_OLD
B_l(1,2) = -1d0/lambda2/lambda2
B_l(2,1) = -lambda2/lambda_OLD/lambda_OLD
B_l(2,2) = lambda_OLD/lambda2/lambda2
C_l(1,1) = fff-fffold-lambda_OLD*slope
C_l(2,1) = fff2-fffold-lambda2*slope
!
aabb = A_l* MATMUL(B_l,C_l)
aa = aabb(1,1)
bb = aabb(2,1)
IF (aa .EQ. 0.0d0) THEN
lambda = -slope/2d0/bb;
ELSE
discriminant = bb**2d0 - 3d0*aa*slope;
IF (discriminant < 0d0) THEN
lambda = MAX_LAMBDA*lambda_OLD;
ELSEIF (bb <= 0d0) THEN
lambda = (-bb + sqrt(discriminant))/3d0/aa
ELSE
lambda = -slope/(bb + sqrt(discriminant))
ENDIF
ENDIF
lambda = min(lambda,MAX_LAMBDA*lambda_OLD) ! minimum step length
ENDIF
ELSE
lambda = 1d0; ! fraction of Newton step
ENDIF
IF (lambda < 1d0) THEN
lambda2 = lambda_OLD;
fff2 = fff; ! save 2nd most previous value
lambda = max(lambda,MIN_LAMBDA*lambda_OLD) ! minimum step length
GO TO 1
ENDIF
!
resnorm = abs(F)
!
END DO
!
out_1 = XXX
!
END SUBROUTINE New_Rap1Daxl
!
!
!===============================================================================
SUBROUTINE F_New_Rap1D(MODE, F, out_2, XXX, GGG, in_1, out_3)
!===============================================================================
IMPLICIT NONE
INTEGER, INTENT(IN) :: MODE
REAL(8), INTENT(IN) :: in_1, XXX, GGG
REAL(8), INTENT(OUT) :: F, out_2,out_3
!
!LOCAL
INTEGER :: i,j
REAL(8) :: temp, e, v
REAL(8) :: delta_p,press,e_l,e_v,V_v,V_l,qual
IF (MODE .EQ. 1) THEN
! (v,e) --> T: v=in_1, e=GGG, T=XXX
v = in_1
temp = XXX
!
CALL inter_energy(temp, v, e)
!
F = (e - GGG) * 1e-3_pr
out_2 = 0.0_pr
out_3 = 0.0_pr
!
!
ELSEIF (MODE .EQ. 2) THEN
! In two-phase region, (e,v) --> (psat,x,Tsat)
!
press = XXX
v = in_1
e = GGG
! CALL satprop(3, press, temp, v_v, v_l, e_v, e_l)
! print*, 'iter process',v,v_l, v_v
out_3 = temp
qual = (v - v_l)/(v_v-v_l)
out_2 = qual
F = (e - qual*e_v - (1_pr - qual)*e_l) * 1e-3_pr
!
!
ELSEIF (MODE .EQ. 3) THEN
v = XXX
temp = in_1
CALL pressure(temp,v,press)
F = (press - GGG) * 1e-6_pr
ELSE
print*,'MODE of New_Rap1D unknown'
STOP
ENDIF
!
END SUBROUTINE F_New_Rap1D
!=======================================================================================
!
SUBROUTINE BrentRootsaxl(MODE, out_1, out_2, residue, Niter, GGG, lower, upper, in_1, in_2)
!
!=======================================================================================
!
IMPLICIT NONE
INTEGER, INTENT(IN) :: MODE
INTEGER, INTENT(OUT) :: Niter
REAL(8), INTENT(IN) :: lower, upper, GGG, in_1, in_2
REAL(8), INTENT(OUT) :: out_1, out_2, residue
INTEGER :: done
REAL(8), PARAMETER :: FPP = 1d-16 ! floating-point precision
REAL(8), PARAMETER :: MAX_ITER = 2d2 ! maximum allowed number of iterations
REAL(8), PARAMETER :: nearzero = 1d-16
REAL(8) :: TOL, AA, BB, CC, DD, EE, FA, FB, FC, xm, PP, SS, QQ, RR,&
& Tol1, Root, Minimum
TOL = 1d-12
done = 0
Niter = 0
!
AA = lower
BB = upper
CALL F_Brent(MODE, FA, out_2, AA, GGG, in_1, in_2)
IF (abs(FA) .LT. TOL) THEN
out_1 = AA
RETURN
ENDIF
!
CALL F_Brent(MODE, FB, out_2, BB, GGG, in_1, in_2)
IF (abs(FB) .LT. TOL) THEN
out_1 = BB
RETURN
ENDIF
!
! Check if root is bracketed
IF (((FA .GT. 0d0) .AND. (FB .GT. 0d0)) .OR. ((FA .LT. 0d0) .AND.&
& (FB .LT. 0d0))) THEN
print*, 'ERROR: Root is not bracketed in MODE', MODE
STOP
ELSE
FC = FB
DO WHILE ((done .EQ. 0) .AND. (Niter .LT. MAX_ITER))
Niter = Niter + 1
!
IF (((FC .GT. 0d0) .AND. (FB .GT. 0d0)) .OR.&
& ((FC .LT. 0d0) .AND. (FB .LT. 0d0))) THEN
! rename AA, BB, CC and adjust bounding interval DD
CC = AA
FC = FA
DD = BB - AA
EE = DD
ENDIF
IF (abs(FC) .LT. abs(FB)) THEN
AA = BB
BB = CC
CC = AA
FA = FB
FB = FC
FC = FA
ENDIF
Tol1 = 2d0 * FPP * abs(BB) + 0.5d0 * TOL ! convergence check
xm = 0.5d0 * (CC - BB)
IF((abs(xm) .LE.Tol1) .OR. (abs(FA) .LT.nearzero)) THEN
! a root has been found
Root = BB
done = 1
CALL F_Brent(MODE, residue, out_2,BB,GGG,in_1,in_2)
ELSE
IF ((abs(EE) .GE. Tol1) .AND.&
& (abs(FA) .GT. abs(FB))) THEN
SS = FB / FA ! attempt inverse quadratic function
IF (abs(AA - CC) .LT. nearzero) THEN
PP = 2d0 * xm * SS
QQ = 1d0 - SS
ELSE
QQ = FA / FC
RR = FB / FC
PP = SS * (2d0 * xm * QQ * (QQ - RR) -&
& (BB- AA) * (RR - 1d0))
QQ = (QQ - 1d0) * (RR - 1d0) * (SS - 1d0)
ENDIF
IF (PP .GT. nearzero) THEN
QQ = -QQ
ENDIF
PP = abs(PP)
! Evaluation of the minimum
IF ((3d0 * xm * QQ - abs(Tol1 * QQ)) .LT.&
& (abs(EE * QQ))) THEN
Minimum = 3d0 * xm * QQ - abs(Tol1 * QQ)
ELSE
Minimum = abs(EE * QQ)
ENDIF
!
IF ((2d0 * PP) .LT. Minimum) THEN
! accept interpolation
EE = DD
DD = PP / QQ
ELSE
! interpolation failed, use bisection
DD = xm
EE = DD
ENDIF
ELSE
! bounds decreasing too slowly, use bisection