-
Notifications
You must be signed in to change notification settings - Fork 2
/
mod_phsolvers.F90
1970 lines (1276 loc) · 54 KB
/
mod_phsolvers.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
! -*- f90 -*-
!
! Copyright 2013 Guy Munhoven
!
! This file is part of SolveSAPHE.
! SolveSAPHE is free software: you can redistribute it and/or modify
! it under the terms of the GNU Lesser General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! SolveSAPHE is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU Lesser General Public License for more details.
!
! You should have received a copy of the GNU Lesser General Public License
! along with SolveSAPHE. If not, see <http://www.gnu.org/licenses/>.
!
! **********************
! Precompiler directives
! **********************
! VARIANT_BACASTOWORIG:
! - if not defined, use secant iterations on [H+]
! - if defined, use secant iterations on X = SQRT(K_1 K_2)/[H+]
#undef VARIANT_BACASTOWORIG
! **************************
! End precompiler directives
! **************************
MODULE MOD_PHSOLVERS
USE MOD_PRECISION
USE MOD_COMMON, ONLY: zero, one, two, three, four, nine, ten, hundred
IMPLICIT NONE
! General parameters
REAL(KIND=wp), PARAMETER :: pp_rdel_ah_target = 1.E-8_wp
REAL(KIND=wp), PARAMETER :: pp_ln10 = 2.302585092994045684018_wp
! Maximum number of iterations for each method
INTEGER, PARAMETER :: jp_maxniter_atgen = 50
INTEGER, PARAMETER :: jp_maxniter_icacfp = 50
INTEGER, PARAMETER :: jp_maxniter_bacastow = 50
INTEGER, PARAMETER :: jp_maxniter_atsec = 50
INTEGER, PARAMETER :: jp_maxniter_ocmip = 50
INTEGER, PARAMETER :: jp_maxniter_atfast = 50
! Bookkeeping variables for each method
! - SOLVE_AT_GENERAL
INTEGER :: niter_atgen = jp_maxniter_atgen
! - SOLVE_AT_ICACFP
INTEGER :: niter_icacfp = jp_maxniter_icacfp
! - SOLVE_AT_BACASTOW
INTEGER :: niter_bacastow = jp_maxniter_bacastow
! - SOLVE_AT_GENERAL_SEC
INTEGER :: niter_atsec = jp_maxniter_atsec
! - SOLVE_AT_OCMIP
INTEGER :: niter_ocmip = jp_maxniter_ocmip
! - SOLVE_AT_FAST (variant of SOLVE_AT_GENERAL w/o bracketing
INTEGER :: niter_atfast = jp_maxniter_atfast
! Keep the following functions private to avoid conflicts with
! other modules that provide similar ones.
! PRIVATE AHINI_FOR_AT, SOLVE_AC
CONTAINS
!===============================================================================
SUBROUTINE ANW_INFSUP(p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, &
p_alknw_inf, p_alknw_sup)
!===============================================================================
! Subroutine returns the lower and upper bounds of "non-water-selfionization"
! contributions to total alkalinity (the infimum and the supremum), i.e
! inf(TA - [OH-] + [H+]) and sup(TA - [OH-] + [H+])
IMPLICIT NONE
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_dictot
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
REAL(KIND=wp), INTENT(OUT) :: p_alknw_inf
REAL(KIND=wp), INTENT(OUT) :: p_alknw_sup
!==============================================================================
! p_alknw_inf = -\Sum_i m_i Xtot_i
! p_alknw_inf =-p_dictot*zero & ! n = 2, m = 0
! -p_bortot*zero & ! n = 1, m = 0
! -p_po4tot*one & ! n = 3, m = 1
! -p_siltot*zero & ! n = 1, m = 0
! -p_nh4tot*zero & ! n = 1, m = 0
! -p_h2stot*zero & ! n = 1, m = 0
! -p_so4tot*one & ! n = 1, m = 1
! -p_flutot*one ! n = 1, m = 1
p_alknw_inf = -p_po4tot - p_so4tot - p_flutot
! p_alknw_sup = \Sum_i (n_i - m_i) Xtot_i
! p_alknw_sup = p_dictot*(two-zero) & ! n = 2, m = 0
! p_bortot*(one-zero) & ! n = 1, m = 0
! p_po4tot*(three-one) & ! n = 3, m = 1
! p_siltot*(one-zero) & ! n = 1, m = 0
! p_nh4tot*(one-zero) & ! n = 1, m = 0
! p_h2stot*(one-zero) & ! n = 1, m = 0
! p_so4tot*(one-one) & ! n = 1, m = 1
! p_flutot*(one-one) ! n = 1, m = 1
p_alknw_sup = p_dictot + p_dictot + p_bortot &
+ p_po4tot + p_po4tot + p_siltot &
+ p_nh4tot + p_h2stot
RETURN
!===============================================================================
END SUBROUTINE ANW_INFSUP
!===============================================================================
!===============================================================================
FUNCTION EQUATION_AT(p_alktot, p_h, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, &
p_deriveqn)
!===============================================================================
USE MOD_CHEMCONST
IMPLICIT NONE
REAL(KIND=wp) :: EQUATION_AT
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alktot
REAL(KIND=wp), INTENT(IN) :: p_h
REAL(KIND=wp), INTENT(IN) :: p_dictot
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
REAL(KIND=wp), INTENT(OUT), OPTIONAL :: p_deriveqn
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: znumer_dic, zdnumer_dic, zdenom_dic, zalk_dic, zdalk_dic
REAL(KIND=wp) :: znumer_bor, zdnumer_bor, zdenom_bor, zalk_bor, zdalk_bor
REAL(KIND=wp) :: znumer_po4, zdnumer_po4, zdenom_po4, zalk_po4, zdalk_po4
REAL(KIND=wp) :: znumer_sil, zdnumer_sil, zdenom_sil, zalk_sil, zdalk_sil
REAL(KIND=wp) :: znumer_nh4, zdnumer_nh4, zdenom_nh4, zalk_nh4, zdalk_nh4
REAL(KIND=wp) :: znumer_h2s, zdnumer_h2s, zdenom_h2s, zalk_h2s, zdalk_h2s
REAL(KIND=wp) :: znumer_so4, zdnumer_so4, zdenom_so4, zalk_so4, zdalk_so4
REAL(KIND=wp) :: znumer_flu, zdnumer_flu, zdenom_flu, zalk_flu, zdalk_flu
REAL(KIND=wp) :: zalk_wat, zdalk_wat
!==============================================================================
! H2CO3 - HCO3 - CO3 : n=2, m=0
znumer_dic = two*api2_dic + p_h* api1_dic
zdenom_dic = api2_dic + p_h*( api1_dic + p_h)
zalk_dic = p_dictot * (znumer_dic/zdenom_dic)
! B(OH)3 - B(OH)4 : n=1, m=0
znumer_bor = api1_bor
zdenom_bor = api1_bor + p_h
zalk_bor = p_bortot * (znumer_bor/zdenom_bor)
! H3PO4 - H2PO4 - HPO4 - PO4 : n=3, m=1
znumer_po4 = three*api3_po4 + p_h*(two*api2_po4 + p_h* api1_po4)
zdenom_po4 = api3_po4 + p_h*( api2_po4 + p_h*(api1_po4 + p_h))
zalk_po4 = p_po4tot * (znumer_po4/zdenom_po4 - one) ! Zero level of H3PO4 = 1
! H4SiO4 - H3SiO4 : n=1, m=0
znumer_sil = api1_sil
zdenom_sil = api1_sil + p_h
zalk_sil = p_siltot * (znumer_sil/zdenom_sil)
! NH4 - NH3 : n=1, m=0
znumer_nh4 = api1_nh4
zdenom_nh4 = api1_nh4 + p_h
zalk_nh4 = p_nh4tot * (znumer_nh4/zdenom_nh4)
! H2S - HS : n=1, m=0
znumer_h2s = api1_h2s
zdenom_h2s = api1_h2s + p_h
zalk_h2s = p_h2stot * (znumer_h2s/zdenom_h2s)
! HSO4 - SO4 : n=1, m=1
znumer_so4 = api1_so4
zdenom_so4 = api1_so4 + p_h
zalk_so4 = p_so4tot * (znumer_so4/zdenom_so4 - one)
! HF - F : n=1, m=1
znumer_flu = api1_flu
zdenom_flu = api1_flu + p_h
zalk_flu = p_flutot * (znumer_flu/zdenom_flu - one)
! H2O - OH
zalk_wat = api1_wat/p_h - p_h/aphscale
EQUATION_AT = zalk_dic + zalk_bor + zalk_po4 + zalk_sil &
+ zalk_nh4 + zalk_h2s + zalk_so4 + zalk_flu &
+ zalk_wat - p_alktot
IF(PRESENT(p_deriveqn)) THEN
! H2CO3 - HCO3 - CO3 : n=2
zdnumer_dic = api1_dic*api2_dic + p_h*(four*api2_dic &
+ p_h* api1_dic)
zdalk_dic = -p_dictot*(zdnumer_dic/zdenom_dic**2)
! B(OH)3 - B(OH)4 : n=1
zdnumer_bor = api1_bor
zdalk_bor = -p_bortot*(zdnumer_bor/zdenom_bor**2)
! H3PO4 - H2PO4 - HPO4 - PO4 : n=3
zdnumer_po4 = api2_po4*api3_po4 + p_h*(four*api1_po4*api3_po4 &
+ p_h*(nine*api3_po4 + api1_po4*api2_po4 &
+ p_h*(four*api2_po4 &
+ p_h* api1_po4)))
zdalk_po4 = -p_po4tot * (zdnumer_po4/zdenom_po4**2)
! H4SiO4 - H3SiO4 : n=1
zdnumer_sil = api1_sil
zdalk_sil = -p_siltot * (zdnumer_sil/zdenom_sil**2)
! NH4 - NH3 : n=1
zdnumer_nh4 = api1_nh4
zdalk_nh4 = -p_nh4tot * (zdnumer_nh4/zdenom_nh4**2)
! H2S - HS : n=1
zdnumer_h2s = api1_h2s
zdalk_h2s = -p_h2stot * (zdnumer_h2s/zdenom_h2s**2)
! HSO4 - SO4 : n=1
zdnumer_so4 = api1_so4
zdalk_so4 = -p_so4tot * (zdnumer_so4/zdenom_so4**2)
! HF - F : n=1
zdnumer_flu = api1_flu
zdalk_flu = -p_flutot * (zdnumer_flu/zdenom_flu**2)
p_deriveqn = zdalk_dic + zdalk_bor + zdalk_po4 + zdalk_sil &
+ zdalk_nh4 + zdalk_h2s + zdalk_so4 + zdalk_flu &
- api1_wat/p_h**2 - one/aphscale
ENDIF
RETURN
!===============================================================================
END FUNCTION EQUATION_AT
!===============================================================================
!===============================================================================
SUBROUTINE AHINI_FOR_AT(p_alkcb, p_dictot, p_bortot, p_hini)
!===============================================================================
! Subroutine returns the root for the 2nd order approximation of the
! DIC -- B_T -- A_CB equation for [H+] (reformulated as a cubic polynomial)
! around the local minimum, if it exists.
! Returns * 1E-03_wp if p_alkcb <= 0
! * 1E-10_wp if p_alkcb >= 2*p_dictot + p_bortot
! * 1E-07_wp if 0 < p_alkcb < 2*p_dictot + p_bortot
! and the 2nd order approximation does not have a solution
USE MOD_CHEMCONST, ONLY : api1_dic, api2_dic, api1_bor
IMPLICIT NONE
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alkcb, p_dictot, p_bortot
REAL(KIND=wp), INTENT(OUT) :: p_hini
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: zca, zba
REAL(KIND=wp) :: zd, zsqrtd, zhmin
REAL(KIND=wp) :: za2, za1, za0
!==============================================================================
IF (p_alkcb <= zero) THEN
p_hini = 1.e-3_wp
ELSEIF (p_alkcb >= (two*p_dictot + p_bortot)) THEN
p_hini = 1.e-10_wp
ELSE
zca = p_dictot/p_alkcb
zba = p_bortot/p_alkcb
! Coefficients of the cubic polynomial
za2 = api1_bor*(one - zba) + api1_dic*(one-zca)
za1 = api1_dic*api1_bor*(one - zba - zca) + api2_dic*(one - (zca+zca))
za0 = api2_dic*api1_bor*(one - zba - (zca+zca))
! Taylor expansion around the minimum
zd = za2*za2 - three*za1 ! Discriminant of the quadratic equation
! for the minimum close to the root
IF(zd > zero) THEN ! If the discriminant is positive
zsqrtd = SQRT(zd)
IF(za2 < 0) THEN
zhmin = (-za2 + zsqrtd)/three
ELSE
zhmin = -za1/(za2 + zsqrtd)
ENDIF
p_hini = zhmin + SQRT(-(za0 + zhmin*(za1 + zhmin*(za2 + zhmin)))/zsqrtd)
ELSE
p_hini = 1.e-7_wp
ENDIF
ENDIF
RETURN
!===============================================================================
END SUBROUTINE AHINI_FOR_AT
!===============================================================================
!===============================================================================
FUNCTION SOLVE_AT_GENERAL(p_alktot, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, p_hini, p_val)
!===============================================================================
! Universal pH solver that converges from any given initial value,
! determines upper an lower bounds for the solution if required
USE MOD_CHEMCONST, ONLY: api1_wat, aphscale
IMPLICIT NONE
REAL(KIND=wp) :: SOLVE_AT_GENERAL
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alktot
REAL(KIND=wp), INTENT(IN) :: p_dictot
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
REAL(KIND=wp), INTENT(IN), OPTIONAL :: p_hini
REAL(KIND=wp), INTENT(OUT), OPTIONAL :: p_val
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: zh_ini, zh, zh_prev, zh_lnfactor
REAL(KIND=wp) :: zalknw_inf, zalknw_sup
REAL(KIND=wp) :: zh_min, zh_max
REAL(KIND=wp) :: zdelta, zh_delta
REAL(KIND=wp) :: zeqn, zdeqndh, zeqn_absmin
LOGICAL :: l_exitnow
REAL(KIND=wp), PARAMETER :: pz_exp_threshold = 1.0_wp
!==============================================================================
IF(PRESENT(p_hini)) THEN
zh_ini = p_hini
ELSE
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_GENERAL] Calling AHINI_FOR_AT for h_ini'
#endif
CALL AHINI_FOR_AT(p_alktot, p_dictot, p_bortot, zh_ini)
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_GENERAL] h_ini :', zh_ini
#endif
ENDIF
CALL ANW_INFSUP(p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, &
zalknw_inf, zalknw_sup)
zdelta = (p_alktot-zalknw_inf)**2 + four*api1_wat/aphscale
IF(p_alktot >= zalknw_inf) THEN
zh_min = two*api1_wat /( p_alktot-zalknw_inf + SQRT(zdelta) )
ELSE
zh_min = aphscale*(-(p_alktot-zalknw_inf) + SQRT(zdelta) ) / two
ENDIF
zdelta = (p_alktot-zalknw_sup)**2 + four*api1_wat/aphscale
IF(p_alktot <= zalknw_sup) THEN
zh_max = aphscale*(-(p_alktot-zalknw_sup) + SQRT(zdelta) ) / two
ELSE
zh_max = two*api1_wat /( p_alktot-zalknw_sup + SQRT(zdelta) )
ENDIF
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_GENERAL] h_min :', zh_min
PRINT*, '[SOLVE_AT_GENERAL] h_max :', zh_max
#endif
zh = MAX(MIN(zh_max, zh_ini), zh_min)
!zh = SQRT(zh_max*zh_min) ! Uncomment this line for the
! "safe" initialisation test
niter_atgen = 0 ! Reset counters of iterations
zeqn_absmin = HUGE(1._wp)
DO
IF(niter_atgen >= jp_maxniter_atgen) THEN
zh = -one
EXIT
ENDIF
zh_prev = zh
zeqn = EQUATION_AT(p_alktot, zh, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, P_DERIVEQN = zdeqndh)
! Adapt bracketing interval
IF(zeqn > zero) THEN
zh_min = zh_prev
ELSEIF(zeqn < zero) THEN
zh_max = zh_prev
ELSE
! zh is the root; unlikely but, one never knows
EXIT
ENDIF
! Now determine the next iterate zh
niter_atgen = niter_atgen + 1
IF(ABS(zeqn) >= 0.5_wp*zeqn_absmin) THEN
! if the function evaluation at the current point is
! not decreasing faster than with a bisection step (at least linearly)
! in absolute value take one bisection step on [ph_min, ph_max]
! ph_new = (ph_min + ph_max)/2d0
! In terms of [H]_new:
! [H]_new = 10**(-ph_new)
! = 10**(-(ph_min + ph_max)/2d0)
! = SQRT(10**(-(ph_min + phmax)))
! = SQRT(zh_max * zh_min)
zh = SQRT(zh_max * zh_min)
zh_lnfactor = (zh - zh_prev)/zh_prev ! Required to test convergence below
ELSE
! dzeqn/dpH = dzeqn/d[H] * d[H]/dpH
! = -zdeqndh * LOG(10) * [H]
! \Delta pH = -zeqn/(zdeqndh*d[H]/dpH) = zeqn/(zdeqndh*[H]*LOG(10))
! pH_new = pH_old + \deltapH
! [H]_new = 10**(-pH_new)
! = 10**(-pH_old - \Delta pH)
! = [H]_old * 10**(-zeqn/(zdeqndh*[H]_old*LOG(10)))
! = [H]_old * EXP(-LOG(10)*zeqn/(zdeqndh*[H]_old*LOG(10)))
! = [H]_old * EXP(-zeqn/(zdeqndh*[H]_old))
zh_lnfactor = -zeqn/(zdeqndh*zh_prev)
IF(ABS(zh_lnfactor) > pz_exp_threshold) THEN
zh = zh_prev*EXP(zh_lnfactor)
ELSE
zh_delta = zh_lnfactor*zh_prev
zh = zh_prev + zh_delta
ENDIF
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_GENERAL] testing zh :', zh, zeqn, zh_lnfactor
#endif
IF( zh < zh_min ) THEN
! if [H]_new < [H]_min
! i.e., if ph_new > ph_max then
! take one bisection step on [ph_prev, ph_max]
! ph_new = (ph_prev + ph_max)/2d0
! In terms of [H]_new:
! [H]_new = 10**(-ph_new)
! = 10**(-(ph_prev + ph_max)/2d0)
! = SQRT(10**(-(ph_prev + phmax)))
! = SQRT([H]_old*10**(-ph_max))
! = SQRT([H]_old * zh_min)
zh = SQRT(zh_prev * zh_min)
zh_lnfactor = (zh - zh_prev)/zh_prev ! Required to test convergence below
ENDIF
IF( zh > zh_max ) THEN
! if [H]_new > [H]_max
! i.e., if ph_new < ph_min, then
! take one bisection step on [ph_min, ph_prev]
! ph_new = (ph_prev + ph_min)/2d0
! In terms of [H]_new:
! [H]_new = 10**(-ph_new)
! = 10**(-(ph_prev + ph_min)/2d0)
! = SQRT(10**(-(ph_prev + ph_min)))
! = SQRT([H]_old*10**(-ph_min))
! = SQRT([H]_old * zhmax)
zh = SQRT(zh_prev * zh_max)
zh_lnfactor = (zh - zh_prev)/zh_prev ! Required to test convergence below
ENDIF
ENDIF
zeqn_absmin = MIN( ABS(zeqn), zeqn_absmin)
! Stop iterations once |\delta{[H]}/[H]| < rdel
! <=> |(zh - zh_prev)/zh_prev| = |EXP(-zeqn/(zdeqndh*zh_prev)) -1| < rdel
! |EXP(-zeqn/(zdeqndh*zh_prev)) -1| ~ |zeqn/(zdeqndh*zh_prev)|
! Alternatively:
! |\Delta pH| = |zeqn/(zdeqndh*zh_prev*LOG(10))|
! ~ 1/LOG(10) * |\Delta [H]|/[H]
! < 1/LOG(10) * rdel
! Hence |zeqn/(zdeqndh*zh)| < rdel
! rdel <-- pp_rdel_ah_target
l_exitnow = (ABS(zh_lnfactor) < pp_rdel_ah_target)
IF(l_exitnow) EXIT
ENDDO
SOLVE_AT_GENERAL = zh
IF(PRESENT(p_val)) THEN
IF(zh > zero) THEN
p_val = EQUATION_AT(p_alktot, zh, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot)
ELSE
p_val = HUGE(1._wp)
ENDIF
ENDIF
RETURN
!===============================================================================
END FUNCTION SOLVE_AT_GENERAL
!===============================================================================
!===============================================================================
FUNCTION AC_FROM_AT(p_alktot, p_h, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot)
!===============================================================================
USE MOD_CHEMCONST
IMPLICIT NONE
REAL(KIND=wp) :: AC_FROM_AT
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alktot
REAL(KIND=wp), INTENT(IN) :: p_h
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: znumer_bor, zdenom_bor, zalk_bor
REAL(KIND=wp) :: znumer_po4, zdenom_po4, zalk_po4
REAL(KIND=wp) :: znumer_sil, zdenom_sil, zalk_sil
REAL(KIND=wp) :: znumer_nh4, zdenom_nh4, zalk_nh4
REAL(KIND=wp) :: znumer_h2s, zdenom_h2s, zalk_h2s
REAL(KIND=wp) :: znumer_so4, zdenom_so4, zalk_so4
REAL(KIND=wp) :: znumer_flu, zdenom_flu, zalk_flu
REAL(KIND=wp) :: zalk_wat
!==============================================================================
! B(OH)3 - B(OH)4 : n=1, m=0
znumer_bor = api1_bor
zdenom_bor = api1_bor + p_h
zalk_bor = p_bortot * (znumer_bor/zdenom_bor)
! H3PO4 - H2PO4 - HPO4 - PO4 : n=3, m=1
znumer_po4 = three*api3_po4 + p_h*(two*api2_po4 + p_h* api1_po4)
zdenom_po4 = api3_po4 + p_h*( api2_po4 + p_h*(api1_po4 + p_h))
zalk_po4 = p_po4tot * (znumer_po4/zdenom_po4 - one) ! Zero level of H3PO4 = 1
! H4SiO4 - H3SiO4 : n=1, m=0
znumer_sil = api1_sil
zdenom_sil = api1_sil + p_h
zalk_sil = p_siltot * (znumer_sil/zdenom_sil)
! NH4 - NH3 : n=1, m=0
znumer_nh4 = api1_nh4
zdenom_nh4 = api1_nh4 + p_h
zalk_nh4 = p_nh4tot * (znumer_nh4/zdenom_nh4)
! H2S - HS : n=1, m=0
znumer_h2s = api1_h2s
zdenom_h2s = api1_h2s + p_h
zalk_h2s = p_h2stot * (znumer_h2s/zdenom_h2s)
! HSO4 - SO4 : n=1, m=1
znumer_so4 = api1_so4
zdenom_so4 = api1_so4 + p_h
zalk_so4 = p_so4tot * (znumer_so4/zdenom_so4 - one)
! HF - F : n=1, m=1
znumer_flu = api1_flu
zdenom_flu = api1_flu + p_h
zalk_flu = p_flutot * (znumer_flu/zdenom_flu - one)
! H2O - OH
zalk_wat = api1_wat/p_h - p_h/aphscale
AC_FROM_AT = p_alktot &
- ( zalk_bor + zalk_po4 + zalk_sil &
+ zalk_nh4 + zalk_h2s + zalk_so4 + zalk_flu &
+ zalk_wat)
RETURN
!===============================================================================
END FUNCTION AC_FROM_AT
!===============================================================================
!===============================================================================
FUNCTION SOLVE_AC(p_alkc, p_dictot)
!===============================================================================
! Function returns the solution of the DIC - A_C equation for [H]
! Returns -1 if A_C <= 0 or A_C >= 2*DIC
USE MOD_CHEMCONST, ONLY : api1_dic, api2_dic
IMPLICIT NONE
REAL(KIND=wp) :: SOLVE_AC
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alkc, p_dictot
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: zca, zsqrtdelta, za1, za0
!==============================================================================
IF((p_alkc <= zero) .OR. (p_alkc >= (p_dictot+p_dictot))) THEN
SOLVE_AC = -one
ELSE
zca = p_dictot/p_alkc
za1 = api1_dic*(one - zca)
za0 = api2_dic*(one - zca - zca)
zsqrtdelta = SQRT(za1**2 - four*za0)
IF(za1 > zero) THEN
SOLVE_AC = -two*za0/( za1 + zsqrtdelta )
ELSE
SOLVE_AC = ( -za1 + zsqrtdelta )/two
ENDIF
ENDIF
RETURN
!===============================================================================
END FUNCTION SOLVE_AC
!===============================================================================
!===============================================================================
FUNCTION SOLVE_AT_ICACFP( p_alktot, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, p_hini, p_val)
!===============================================================================
! Function returns the solution of the Alk_T-pH equation derived by
! the iterated carbonate alkalinity correction method (fixed point iteration)
! Returns -1 if the iterations did not converge (divergence or number of
! iterations exceeded.
IMPLICIT NONE
REAL(KIND=wp) :: SOLVE_AT_ICACFP
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alktot
REAL(KIND=wp), INTENT(IN) :: p_dictot
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
REAL(KIND=wp), INTENT(IN), OPTIONAL :: p_hini
REAL(KIND=wp), INTENT(OUT), OPTIONAL :: p_val
!-----------------!
! Local variables !
!-----------------!
REAL(KIND=wp) :: zh_ini, zh, zh_prev, zalk_dic
REAL(KIND=wp) :: zalknw_inf, zalknw_sup
REAL(KIND=wp) :: zdelta
REAL(KIND=wp) :: zeqn, zdeqndh, zeqn_absmin
LOGICAL :: l_exitnow
!==============================================================================
IF(PRESENT(p_hini)) THEN
zh_ini = p_hini
ELSE
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_ICACFP] Calling AHINI_FOR_AT for h_ini'
#endif
CALL AHINI_FOR_AT(p_alktot, p_dictot, p_bortot, zh_ini)
#if defined(DEBUG_PHSOLVERS)
PRINT*, '[SOLVE_AT_ICACFP] h_ini :', zh_ini
#endif
ENDIF
zh = zh_ini
niter_icacfp = 0 ! Reset counters of iterations
DO
niter_icacfp = niter_icacfp + 1
IF(niter_icacfp > jp_maxniter_icacfp) THEN
zh = -one
EXIT
ENDIF
zh_prev = zh
zalk_dic = AC_FROM_AT(p_alktot, zh, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot)
zh = SOLVE_AC(zalk_dic, p_dictot)
IF(zh < zero) THEN
! IF zh < 0, the quadratic equation
! in DIC and ALK_C does not have any
! positive root.
l_exitnow = .TRUE.
ELSE
l_exitnow = (ABS((zh_prev - zh)/zh) < pp_rdel_ah_target)
ENDIF
IF(l_exitnow) EXIT
ENDDO
SOLVE_AT_ICACFP = zh
IF(PRESENT(p_val)) THEN
IF(zh > zero) THEN
p_val = EQUATION_AT(p_alktot, zh, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot)
ELSE
p_val = HUGE(1._wp)
ENDIF
ENDIF
RETURN
!===============================================================================
END FUNCTION SOLVE_AT_ICACFP
!===============================================================================
!===============================================================================
FUNCTION SOLVE_AT_BACASTOW(p_alktot, p_dictot, p_bortot, &
p_po4tot, p_siltot, p_nh4tot, p_h2stot, &
p_so4tot, p_flutot, p_hini, p_val)
!===============================================================================
#if defined(VARIANT_BACASTOWORIG)
USE MOD_CHEMCONST, ONLY : api2_dic
#endif
IMPLICIT NONE
! fixed point iteration
REAL(KIND=wp) :: SOLVE_AT_BACASTOW
!--------------------!
! Argument variables !
!--------------------!
REAL(KIND=wp), INTENT(IN) :: p_alktot
REAL(KIND=wp), INTENT(IN) :: p_dictot
REAL(KIND=wp), INTENT(IN) :: p_bortot
REAL(KIND=wp), INTENT(IN) :: p_po4tot
REAL(KIND=wp), INTENT(IN) :: p_siltot
REAL(KIND=wp), INTENT(IN) :: p_nh4tot
REAL(KIND=wp), INTENT(IN) :: p_h2stot
REAL(KIND=wp), INTENT(IN) :: p_so4tot
REAL(KIND=wp), INTENT(IN) :: p_flutot
REAL(KIND=wp), INTENT(IN), OPTIONAL :: p_hini
REAL(KIND=wp), INTENT(OUT), OPTIONAL :: p_val
!-----------------!
! Local variables !