forked from E3SM-Project/Ocean-BGC
-
Notifications
You must be signed in to change notification settings - Fork 0
/
co2calc.F90
1242 lines (1021 loc) · 45.6 KB
/
co2calc.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 co2calc
!-----------------------------------------------------------------------------
! based upon OCMIP2 co2calc
!
! CVS:$Id: co2calc.F90 941 2006-05-12 21:36:48Z klindsay $
! CVS:$Name$
!-----------------------------------------------------------------------------
use BGC_parms
#ifdef CCSMCOUPLED
!*** ccsm
USE shr_vmath_mod
#endif
IMPLICIT NONE
!-----------------------------------------------------------------------------
! public/private declarations
!-----------------------------------------------------------------------------
PRIVATE
PUBLIC :: co2calc_1point, comp_CO3terms, comp_co3_sat_vals
!-----------------------------------------------------------------------------
! module parameters
!-----------------------------------------------------------------------------
real (BGC_r8), parameter :: &
c0 = 0.0_BGC_r8, &
c1 = 1.0_BGC_r8, &
c2 = 2.0_BGC_r8, &
c3 = 3.0_BGC_r8, &
c10 = 10.0_BGC_r8, &
c1000 = 1000.0_BGC_r8, &
p5 = 0.5_BGC_r8, &
p001 = 0.001_BGC_r8
! these need to be passed in
real (BGC_r8), parameter :: &
! rho_sw = 4.1_BGC_r8/3.996_BGC_r8, & ! density of salt water (g/cm^3)
rho_sw = 1.026_BGC_r8, & ! density of salt water (g/cm^3) from SHR_CONST
T0_Kelvin = 273.15_BGC_r8 ! zero point for Celsius
!-----------------------------------------------------------------------------
! The current setting of xacc, a tolerance critera, will result in co2star
! being accurate to 3 significant figures (xx.y). Making xacc bigger will
! result in faster convergence also, but this is not recommended (xacc of
! 10**-9 drops precision to 2 significant figures).
!-----------------------------------------------------------------------------
REAL(KIND=BGC_r8), PARAMETER :: xacc = 1e-10_BGC_r8
INTEGER(KIND=BGC_i4), PARAMETER :: max_bracket_grow_it = 3
INTEGER(KIND=BGC_i4), PARAMETER :: maxit = 100
REAL(KIND=BGC_r8), PARAMETER :: salt_min = 0.1_BGC_r8
REAL(KIND=BGC_r8), PARAMETER :: dic_min = salt_min / 35.0_BGC_r8 * 1944.0_BGC_r8
REAL(KIND=BGC_r8), PARAMETER :: alk_min = salt_min / 35.0_BGC_r8 * 2225.0_BGC_r8
!-----------------------------------------------------------------------------
! declarations for function coefficients & species concentrations
!-----------------------------------------------------------------------------
REAL(KIND=BGC_r8), dimension(1) :: & ! need to be arrays to use shr_vmath
kw, kb, ks, kf, k1p, k2p, k3p, ksi, &
bt, st, ft, dic, ta, pt, sit
!*****************************************************************************
CONTAINS
!*****************************************************************************
SUBROUTINE co2calc_1point(depth, locmip_k1_k2_bug_fix, lcomp_co3_coeffs, &
temp, salt, dic_in, ta_in, pt_in, sit_in, phlo, phhi, ph, xco2_in, atmpres, &
co2star, dco2star, pCO2surf, dpco2)
!---------------------------------------------------------------------------
! SUBROUTINE co2calc_row
!
! PURPOSE : Calculate delta co2*, etc. from total alkalinity, total CO2,
! temp, salinity (s), etc.
!---------------------------------------------------------------------------
!---------------------------------------------------------------------------
! input arguments
!---------------------------------------------------------------------------
LOGICAL(KIND=BGC_log), INTENT(IN) :: locmip_k1_k2_bug_fix
LOGICAL(KIND=BGC_log), INTENT(IN) :: lcomp_co3_coeffs
REAL(KIND=BGC_r8), INTENT(IN) :: &
depth, & ! depth (meters)
temp, & ! temperature (degrees C)
salt, & ! salinity (PSU)
dic_in, & ! total inorganic carbon (nmol/cm^3)
ta_in, & ! total alkalinity (neq/cm^3)
pt_in, & ! inorganic phosphate (nmol/cm^3)
sit_in, & ! inorganic silicate (nmol/cm^3)
xco2_in, & ! atmospheric mole fraction CO2 in dry air (ppmv)
atmpres ! atmospheric pressure (atmosphere)
!---------------------------------------------------------------------------
! input/output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(INOUT) :: &
phlo, & ! lower limit of pH range
phhi ! upper limit of pH range
!---------------------------------------------------------------------------
! output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(OUT) :: &
ph, & ! computed ph values, for initial guess on next time step
co2star, & ! CO2*water (nmol/cm^3)
dco2star, & ! delta CO2 (nmol/cm^3)
pco2surf, & ! oceanic pCO2 (ppmv)
dpco2 ! Delta pCO2, i.e, pCO2ocn - pCO2atm (ppmv)
!---------------------------------------------------------------------------
! local variable declarations
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4) :: i
INTEGER(KIND=BGC_i4) :: k
REAL(KIND=BGC_r8) :: &
mass_to_vol, & ! (mol/kg) -> (mmol/m^3)
vol_to_mass, & ! (mmol/m^3) -> (mol/kg)
co2starair, & ! co2star saturation
htotal2
REAL(KIND=BGC_r8) :: &
press_bar, & ! pressure at z=depth (bars)
xco2, & ! atmospheric CO2 (atm)
htotal, & ! free concentration of H ion
k0,k1,k2, & ! equilibrium constants for CO2 species
ff ! fugacity of CO2
!---------------------------------------------------------------------------
! set unit conversion factors
!---------------------------------------------------------------------------
mass_to_vol = 1e6_BGC_r8 * rho_sw
vol_to_mass = c1 / mass_to_vol
k = 1
!---------------------------------------------------------------------------
! compute thermodynamic CO3 coefficients
!---------------------------------------------------------------------------
! below is from POP ref_pressure
press_bar = 0.059808_BGC_r8*(exp(-0.025_BGC_r8*depth) - c1) &
+ 0.100766_BGC_r8*depth + 2.28405e-7_BGC_r8*depth**2
IF (lcomp_co3_coeffs) THEN
CALL comp_co3_coeffs( k, press_bar, temp, salt, k0, k1, k2, ff, &
k1_k2_pH_tot=locmip_k1_k2_bug_fix)
END IF
!---------------------------------------------------------------------------
! compute htotal
!---------------------------------------------------------------------------
CALL comp_htotal(k, temp, dic_in, ta_in, pt_in, sit_in, &
k1, k2, phlo, phhi, htotal)
!---------------------------------------------------------------------------
! convert xco2 from uatm to atm
!---------------------------------------------------------------------------
xco2 = xco2_in * 1e-6_BGC_r8
!---------------------------------------------------------------------------
! Calculate [CO2*] as defined in DOE Methods Handbook 1994 Ver.2,
! ORNL/CDIAC-74, Dickson and Goyet, eds. (Ch 2 p 10, Eq A.49)
!
! Compute co2starair
!---------------------------------------------------------------------------
htotal2 = htotal ** 2
co2star = dic(1) * htotal2 / &
(htotal2 + k1 * htotal + k1 * k2)
co2starair = xco2 * ff * atmpres
dco2star = co2starair - co2star
ph = -LOG10(htotal)
!---------------------------------------------------------------------
! Add two output arguments for storing pCO2surf
! Should we be using K0 or ff for the solubility here?
!---------------------------------------------------------------------
pCO2surf = co2star / ff
dpCO2 = pCO2surf - xco2 * atmpres
!---------------------------------------------------------------------
! Convert units of output arguments
! Note: pCO2surf and dpCO2 are calculated in atm above.
!---------------------------------------------------------------------
co2star = co2star * mass_to_vol
dco2star = dco2star * mass_to_vol
pCO2surf = pCO2surf * 1e6_BGC_r8
dpCO2 = dpCO2 * 1e6_BGC_r8
END SUBROUTINE co2calc_1point
!*****************************************************************************
SUBROUTINE comp_CO3terms(k, depth, lcomp_co3_coeffs, temp, salt, &
dic_in, ta_in, pt_in, sit_in, phlo, phhi, ph, H2CO3, HCO3, CO3)
!---------------------------------------------------------------------------
! SUBROUTINE comp_CO3terms
!
! PURPOSE : Calculate H2CO3, HCO3, CO3 from
! total alkalinity, total CO2, temp, salinity (s), etc.
!---------------------------------------------------------------------------
!---------------------------------------------------------------------------
! input arguments
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4), INTENT(IN) :: k
LOGICAL(KIND=BGC_log), INTENT(IN) :: lcomp_co3_coeffs
REAL(KIND=BGC_r8), INTENT(IN) :: &
depth, & ! depth (meters)
temp, & ! temperature (degrees C)
salt, & ! salinity (PSU)
dic_in, & ! total inorganic carbon (nmol/cm^3)
ta_in, & ! total alkalinity (neq/cm^3)
pt_in, & ! inorganic phosphate (nmol/cm^3)
sit_in ! inorganic silicate (nmol/cm^3)
!---------------------------------------------------------------------------
! input/output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(INOUT) :: &
phlo, & ! lower limit of pH range
phhi ! upper limit of pH range
!---------------------------------------------------------------------------
! output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(OUT) :: &
pH, & ! computed ph values, for initial guess on next time step
H2CO3, & ! Carbonic Acid Concentration
HCO3, & ! Bicarbonate Ion Concentration
CO3 ! Carbonate Ion Concentration
!---------------------------------------------------------------------------
! local variable declarations
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4) :: i
REAL(KIND=BGC_r8) :: &
mass_to_vol, & ! (mol/kg) -> (mmol/m^3)
vol_to_mass, & ! (mmol/m^3) -> (mol/kg)
htotal2, denom
REAL(KIND=BGC_r8) :: &
htotal, & ! free concentration of H ion
k0,k1,k2, & ! equilibrium constants for CO2 species
ff ! fugacity of CO2
!---------------------------------------------------------------------------
! set unit conversion factors
!---------------------------------------------------------------------------
mass_to_vol = 1e6_BGC_r8 * rho_sw
vol_to_mass = c1 / mass_to_vol
!------------------------------------------------------------------------
! compute thermodynamic CO3 coefficients
!------------------------------------------------------------------------
IF (lcomp_co3_coeffs) THEN
CALL comp_co3_coeffs(k, depth, temp, salt, k0, k1, k2, ff, k1_k2_pH_tot=.true.)
END IF
!------------------------------------------------------------------------
! compute htotal
!------------------------------------------------------------------------
CALL comp_htotal(k, temp, dic_in, &
ta_in, pt_in, sit_in, k1, k2, &
phlo, phhi, htotal)
!------------------------------------------------------------------------
! Calculate [CO2*] as defined in DOE Methods Handbook 1994 Ver.2,
! ORNL/CDIAC-74, Dickson and Goyet, eds. (Ch 2 p 10, Eq A.49-51)
!------------------------------------------------------------------------
htotal2 = htotal ** 2
denom = c1 / (htotal2 + k1 * htotal + k1 * k2)
H2CO3 = dic(1) * htotal2 * denom
HCO3 = dic(1) * k1 * htotal * denom
CO3 = dic(1) * k1 * k2 * denom
ph = -LOG10(htotal)
!------------------------------------------------------------------
! Convert units of output arguments
!------------------------------------------------------------------
H2CO3 = H2CO3 * mass_to_vol
HCO3 = HCO3 * mass_to_vol
CO3 = CO3 * mass_to_vol
END SUBROUTINE comp_CO3terms
!*****************************************************************************
SUBROUTINE comp_co3_coeffs(k, depth, temp, salt, sk0, sk1, sk2, sff, k1_k2_pH_tot)
!---------------------------------------------------------------------------
! input arguments
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4), INTENT(IN) :: k
REAL(KIND=BGC_r8), INTENT(IN) :: &
depth, & ! depth (meters)
temp, & ! temperature (degrees C)
salt ! salinity (PSU)
LOGICAL(KIND=BGC_log), INTENT(IN) :: k1_k2_pH_tot
!---------------------------------------------------------------------------
! output arguments
!---------------------------------------------------------------------------
!maltrud these are scalar versions--need to copy from array(1) due to shr_vmath
REAL(KIND=BGC_r8), INTENT(OUT) :: &
sk0,sk1,sk2, & ! equilibrium constants for CO2 species
sff ! fugacity of CO2
!---------------------------------------------------------------------------
! local variable declarations
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), dimension(1) :: & ! need to be arrays for shr_vmath
k0,k1,k2, & ! equilibrium constants for CO2 species
ff ! fugacity of CO2
INTEGER(KIND=BGC_i4) :: i
REAL(KIND=BGC_r8) :: &
press_bar ! pressure at level k [bars]
REAL(KIND=BGC_r8), dimension(1) :: & ! need to be arrays to use shr_vmath
salt_lim, & ! bounded salt
tk, & ! temperature (K)
is, & ! ionic strength
scl, & ! chlorinity
tk100, tk1002, invtk, dlogtk, is2, sqrtis, &
s2, sqrts, s15, invRtk, arg, &
deltaV,Kappa,lnKfac,Kfac, & ! pressure correction terms
log_1_m_1p005em3_s, &
log_1_p_tot_sulfate_div_ks
!---------------------------------------------------------------------------
! press_bar = ref_pressure(k)
! below is from POP ref_pressure
press_bar = 0.059808_BGC_r8*(exp(-0.025_BGC_r8*depth) - c1) &
+ 0.100766_BGC_r8*depth + 2.28405e-7_BGC_r8*depth**2
!---------------------------------------------------------------------------
! Calculate all constants needed to convert between various
! measured carbon species. References for each equation are
! noted in the code. Once calculated, the constants are stored
! and passed in the common block "const". The original version
! of this code was based on the code by Dickson in Version 2 of
! "Handbook of Methods for the Analysis of the Various Parameters
! of the Carbon Dioxide System in Seawater", DOE, 1994 (SOP No. 3,
! p25-26).
! Derive simple terms used more than once
!---------------------------------------------------------------------------
salt_lim = max(salt,salt_min)
tk = T0_Kelvin + temp
tk100 = tk * 1e-2_BGC_r8
tk1002 = tk100 * tk100
invtk = c1 / tk
#ifdef CCSMCOUPLED
CALL shr_vmath_log(tk, dlogtk, 1)
#else
dlogtk = LOG(tk)
#endif
invRtk = (c1 / 83.1451_BGC_r8) * invtk
is = 19.924_BGC_r8 * salt_lim / (c1000 - 1.005_BGC_r8 * salt_lim)
is2 = is * is
#ifdef CCSMCOUPLED
CALL shr_vmath_sqrt(is, sqrtis, 1)
CALL shr_vmath_sqrt(salt_lim, sqrts, 1)
#else
sqrtis = SQRT(is)
sqrts = SQRT(salt_lim)
#endif
s2 = salt_lim * salt_lim
scl = salt_lim / 1.80655_BGC_r8
arg = c1 - 0.001005_BGC_r8 * salt_lim
#ifdef CCSMCOUPLED
CALL shr_vmath_log(arg, log_1_m_1p005em3_s, 1)
#else
log_1_m_1p005em3_s = LOG(arg)
#endif
!---------------------------------------------------------------------------
! f = k0(1-pH2O)*correction term for non-ideality
! Weiss & Price (1980, Mar. Chem., 8, 347-359;
! Eq 13 with table 6 values)
!---------------------------------------------------------------------------
arg = -162.8301_BGC_r8 + 218.2968_BGC_r8 / tk100 + &
90.9241_BGC_r8 * (dlogtk + LOG(1e-2_BGC_r8)) - 1.47696_BGC_r8 * tk1002 + &
salt_lim * (.025695_BGC_r8 - .025225_BGC_r8 * tk100 + 0.0049867_BGC_r8 * tk1002)
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, ff, 1)
#else
ff = EXP(arg)
#endif
sff = ff(1)
!---------------------------------------------------------------------------
! K0 from Weiss 1974
!---------------------------------------------------------------------------
arg = 93.4517_BGC_r8 / tk100 - 60.2409_BGC_r8 + 23.3585_BGC_r8 * (dlogtk + LOG(1e-2_BGC_r8)) + &
salt_lim * (.023517_BGC_r8 - 0.023656_BGC_r8 * tk100 + 0.0047036_BGC_r8 * tk1002)
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k0, 1)
#else
k0 = EXP(arg)
#endif
sk0 = k0(1)
!---------------------------------------------------------------------------
! k1 = [H][HCO3]/[H2CO3]
! k2 = [H][CO3]/[HCO3]
! if k1_k2_pH_tot == .true., then use
! Lueker, Dickson, Keeling (2000) using Mehrbach et al. data on total scale
! otherwise, use
! Millero p.664 (1995) using Mehrbach et al. data on seawater scale
! this is only present to be consistent w/ OCMIP2 code
! it should not be used for new runs
! the only reason to use it is to be compatible with prior
! long spun up runs that had used it
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------------
IF (k1_k2_pH_tot) THEN
! total pH scale
arg = 3633.86_BGC_r8 * invtk - 61.2172_BGC_r8 + &
9.67770_BGC_r8 * dlogtk - 0.011555_BGC_r8 * salt_lim + &
0.0001152_BGC_r8 * s2
ELSE
! seawater pH scale, see comment above
arg = 3670.7_BGC_r8 * invtk - 62.008_BGC_r8 + &
9.7944_BGC_r8 * dlogtk - 0.0118_BGC_r8 * salt_lim + &
0.000116_BGC_r8 * s2
END IF
arg = -LOG(c10) * arg
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k1, 1)
#else
k1 = EXP(arg)
#endif
sk1 = k1(1)
IF (k > 1) THEN
deltaV = -25.5_BGC_r8 + 0.1271_BGC_r8 * temp
Kappa = (-3.08_BGC_r8 + 0.0877_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
k1 = k1 * Kfac
END IF
IF (k1_k2_pH_tot) THEN
! total pH scale
arg = 471.78_BGC_r8 * invtk + 25.9290_BGC_r8 - &
3.16967_BGC_r8 * dlogtk - 0.01781_BGC_r8 * salt_lim + 0.0001122_BGC_r8 * s2
ELSE
! seawater pH scale, see comment above
arg = 1394.7_BGC_r8 * invtk + 4.777_BGC_r8 - &
0.0184_BGC_r8 * salt_lim + 0.000118_BGC_r8 * s2
END IF
arg = -LOG(c10) * arg
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k2, 1)
#else
k2 = EXP(arg)
#endif
sk2 = k2(1)
IF (k > 1) THEN
deltaV = -15.82_BGC_r8 - 0.0219_BGC_r8 * temp
Kappa = (1.13_BGC_r8 - 0.1475_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
k2 = k2 * Kfac
END IF
!---------------------------------------------------------------------------
! kb = [H][BO2]/[HBO2]
! Millero p.669 (1995) using data from Dickson (1990)
! CO2SYS states that this in on total pH scale
! pressure correction from Millero 1979, p. 1657
! omitting salinity contribution
!---------------------------------------------------------------------------
arg = (-8966.90_BGC_r8 - 2890.53_BGC_r8 * sqrts - &
77.942_BGC_r8 * salt_lim + 1.728_BGC_r8 * salt_lim * sqrts - &
0.0996_BGC_r8 * s2) * invtk + &
(148.0248_BGC_r8 + 137.1942_BGC_r8 * sqrts + 1.62142_BGC_r8 * salt_lim) + &
(-24.4344_BGC_r8 - 25.085_BGC_r8 * sqrts - 0.2474_BGC_r8 * salt_lim) * dlogtk + &
0.053105_BGC_r8 * sqrts * tk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, kb, 1)
#else
kb = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -29.48_BGC_r8 + (0.1622_BGC_r8 - 0.002608_BGC_r8 * temp) * temp
Kappa = -2.84_BGC_r8 * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
kb = kb * Kfac
END IF
!---------------------------------------------------------------------------
! k1p = [H][H2PO4]/[H3PO4]
! DOE(1994) eq 7.2.20 with footnote using data from Millero (1974)
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------------
arg = -4576.752_BGC_r8 * invtk + 115.525_BGC_r8 - &
18.453_BGC_r8 * dlogtk + &
(-106.736_BGC_r8 * invtk + 0.69171_BGC_r8) * sqrts + &
(-0.65643_BGC_r8 * invtk - 0.01844_BGC_r8) * salt_lim
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k1p, 1)
#else
k1p = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -14.51_BGC_r8 + (0.1211_BGC_r8 - 0.000321_BGC_r8 * temp) * temp
Kappa = (-2.67_BGC_r8 + 0.0427_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
k1p = k1p * Kfac
END IF
!---------------------------------------------------------------------------
! k2p = [H][HPO4]/[H2PO4]
! DOE(1994) eq 7.2.23 with footnote using data from Millero (1974))
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------------
arg = -8814.715_BGC_r8 * invtk + 172.0883_BGC_r8 - &
27.927_BGC_r8 * dlogtk + &
(-160.340_BGC_r8 * invtk + 1.3566_BGC_r8) * sqrts + &
(0.37335_BGC_r8 * invtk - 0.05778_BGC_r8) * salt_lim
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k2p, 1)
#else
k2p = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -23.12_BGC_r8 + (0.1758_BGC_r8 - 0.002647_BGC_r8 * temp) * temp
Kappa = (-5.15_BGC_r8 + 0.09_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
k2p = k2p * Kfac
END IF
!---------------------------------------------------------------------------
! k3p = [H][PO4]/[HPO4]
! DOE(1994) eq 7.2.26 with footnote using data from Millero (1974)
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------------
arg = -3070.75_BGC_r8 * invtk - 18.141_BGC_r8 + &
(17.27039_BGC_r8 * invtk + 2.81197_BGC_r8) * sqrts + &
(-44.99486_BGC_r8 * invtk - 0.09984_BGC_r8) * salt_lim
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, k3p, 1)
#else
k3p = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -26.57_BGC_r8 + (0.202_BGC_r8 - 0.003042_BGC_r8 * temp) * temp
Kappa = (-4.08_BGC_r8 + 0.0714_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
k3p = k3p * Kfac
END IF
!---------------------------------------------------------------------------
! ksi = [H][SiO(OH)3]/[Si(OH)4]
! Millero p.671 (1995) using data from Yao and Millero (1995)
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
! apply boric acid values
!---------------------------------------------------------------------------
arg = -8904.2_BGC_r8 * invtk + 117.385_BGC_r8 - &
19.334_BGC_r8 * dlogtk + &
(-458.79_BGC_r8 * invtk + 3.5913_BGC_r8) * sqrtis + &
(188.74_BGC_r8 * invtk - 1.5998_BGC_r8) * is + &
(-12.1652_BGC_r8 * invtk + 0.07871_BGC_r8) * is2 + &
log_1_m_1p005em3_s
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, ksi, 1)
#else
ksi = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -29.48_BGC_r8 + (0.1622_BGC_r8 - 0.002608_BGC_r8 * temp) * temp
Kappa = -2.84_BGC_r8 * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
ksi = ksi * Kfac
END IF
!---------------------------------------------------------------------------
! kw = [H][OH]
! Millero p.670 (1995) using composite data
! following DOE Handbook, 0.015 substracted from constant to
! approximately convert from SWS pH scale to total pH scale
! pressure correction from Millero 1983
! note that deltaV coeffs in Millero 1995 are those actually
! freshwater deltaV coeffs from Millero 1983
!---------------------------------------------------------------------------
arg = -13847.26_BGC_r8 * invtk + 148.9652_BGC_r8 - 23.6521_BGC_r8 * dlogtk + &
(118.67_BGC_r8 * invtk - 5.977_BGC_r8 + 1.0495_BGC_r8 * dlogtk) * sqrts - &
0.01615_BGC_r8 * salt_lim
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, kw, 1)
#else
kw = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -20.02_BGC_r8 + (0.1119_BGC_r8 - 0.001409_BGC_r8 * temp) * temp
Kappa = (-5.13_BGC_r8 + 0.0794_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
kw = kw * Kfac
END IF
!---------------------------------------------------------------------------
! ks = [H][SO4]/[HSO4], free pH scale
! Dickson (1990, J. chem. Thermodynamics 22, 113)
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------------
arg = -4276.1_BGC_r8 * invtk + 141.328_BGC_r8 - 23.093_BGC_r8 * dlogtk + &
(-13856.0_BGC_r8 * invtk + 324.57_BGC_r8 - 47.986_BGC_r8 * dlogtk) * sqrtis + &
(35474.0_BGC_r8 * invtk - 771.54_BGC_r8 + 114.723_BGC_r8 * dlogtk) * is - &
2698.0_BGC_r8 * invtk * is * sqrtis + &
1776.0_BGC_r8 * invtk * is2 + &
log_1_m_1p005em3_s
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, ks, 1)
#else
ks = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -18.03_BGC_r8 + (0.0466_BGC_r8 + 0.000316_BGC_r8 * temp) * temp
Kappa = (-4.53_BGC_r8 + 0.09_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
ks = ks * Kfac
END IF
!---------------------------------------------------------------------
! kf = [H][F]/[HF]
! Dickson and Riley (1979) -- change pH scale to total
! pressure correction from Millero 1995, p. 675
! w/ typo corrections from CO2SYS
!---------------------------------------------------------------------
arg = c1 + (0.1400_BGC_r8 / 96.062_BGC_r8) * (scl) / ks
#ifdef CCSMCOUPLED
CALL shr_vmath_log(arg, log_1_p_tot_sulfate_div_ks, 1)
#else
log_1_p_tot_sulfate_div_ks = LOG(arg)
#endif
arg = 1590.2_BGC_r8 * invtk - 12.641_BGC_r8 + 1.525_BGC_r8 * sqrtis + &
log_1_m_1p005em3_s + log_1_p_tot_sulfate_div_ks
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(arg, kf, 1)
#else
kf = EXP(arg)
#endif
IF (k > 1) THEN
deltaV = -9.78_BGC_r8 - (0.009_BGC_r8 + 0.000942_BGC_r8 * temp) * temp
Kappa = (-3.91_BGC_r8 + 0.054_BGC_r8 * temp) * p001
lnKfac = (-deltaV + p5 * Kappa * press_bar) * press_bar * invRtk
#ifdef CCSMCOUPLED
CALL shr_vmath_exp(lnKfac, Kfac, 1)
#else
Kfac = EXP(lnKfac)
#endif
kf = kf * Kfac
END IF
!---------------------------------------------------------------------
! Calculate concentrations for borate, sulfate, and fluoride
! bt : Uppstrom (1974)
! st : Morris & Riley (1966)
! ft : Riley (1965)
!---------------------------------------------------------------------
bt = 0.000232_BGC_r8 / 10.811_BGC_r8 * scl
st = 0.14_BGC_r8 / 96.062_BGC_r8 * scl
ft = 0.000067_BGC_r8 / 18.9984_BGC_r8 * scl
END SUBROUTINE comp_co3_coeffs
!*****************************************************************************
SUBROUTINE comp_htotal(k, temp, dic_in, ta_in, pt_in, sit_in, &
k1, k2, phlo, phhi, htotal)
!---------------------------------------------------------------------------
! SUBROUTINE comp_htotal
!
! PURPOSE : Calculate htotal from total alkalinity, total CO2,
! temp, salinity (s), etc.
!---------------------------------------------------------------------------
!---------------------------------------------------------------------------
! input arguments
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4), INTENT(IN) :: k
REAL(KIND=BGC_r8), INTENT(IN) :: &
temp, & ! temperature (degrees C)
dic_in, & ! total inorganic carbon (nmol/cm^3)
ta_in, & ! total alkalinity (neq/cm^3)
pt_in, & ! inorganic phosphate (nmol/cm^3)
sit_in, & ! inorganic silicate (nmol/cm^3)
k1,k2 ! equilibrium constants for CO2 species
!---------------------------------------------------------------------------
! input/output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(INOUT) :: &
phlo, & ! lower limit of pH range
phhi ! upper limit of pH range
!---------------------------------------------------------------------------
! output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(OUT) :: &
htotal ! free concentration of H ion
!---------------------------------------------------------------------------
! local variable declarations
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4) :: i
REAL(KIND=BGC_r8) :: &
mass_to_vol, & ! (mol/kg) -> (mmol/m^3)
vol_to_mass ! (mmol/m^3) -> (mol/kg)
REAL(KIND=BGC_r8) :: &
x1, x2 ! bounds on htotal for solver
!---------------------------------------------------------------------------
! set unit conversion factors
!---------------------------------------------------------------------------
mass_to_vol = 1e6_BGC_r8 * rho_sw
vol_to_mass = c1 / mass_to_vol
!---------------------------------------------------------------------------
! convert tracer units to per mass
!---------------------------------------------------------------------------
dic = max(dic_in,dic_min) * vol_to_mass
ta = max(ta_in,alk_min) * vol_to_mass
pt = max(pt_in,c0) * vol_to_mass
sit = max(sit_in,c0) * vol_to_mass
x1 = c10 ** (-phhi)
x2 = c10 ** (-phlo)
!---------------------------------------------------------------------------
! If DIC and TA are known then either a root finding or iterative
! method must be used to calculate htotal. In this case we use
! the Newton-Raphson "safe" method taken from "Numerical Recipes"
! (function "rtsafe.f" with error trapping removed).
!
! As currently set, this procedure iterates about 12 times. The
! x1 and x2 values set below will accomodate ANY oceanographic
! values. If an initial guess of the pH is known, then the
! number of iterations can be reduced to about 5 by narrowing
! the gap between x1 and x2. It is recommended that the first
! few time steps be run with x1 and x2 set as below. After that,
! set x1 and x2 to the previous value of the pH +/- ~0.5.
!---------------------------------------------------------------------------
CALL drtsafe_row( k, k1, k2, x1, x2, xacc, htotal)
END SUBROUTINE comp_htotal
!*****************************************************************************
SUBROUTINE drtsafe_row(k, k1, k2, x1, x2, xacc, soln)
!---------------------------------------------------------------------------
! Vectorized version of drtsafe, which was a modified version of
! Numerical Recipes algorithm.
! Keith Lindsay, Oct 1999
!
! Algorithm comment :
! Iteration from Newtons method is used unless it leaves
! bracketing interval or the dx is > 0.5 the previous dx.
! In that case, bisection method is used.
!---------------------------------------------------------------------------
!---------------------------------------------------------------------------
! input arguments
!---------------------------------------------------------------------------
INTEGER(KIND=BGC_i4), INTENT(IN) :: k
REAL(KIND=BGC_r8), INTENT(IN) :: k1, k2
REAL(KIND=BGC_r8), INTENT(IN) :: xacc
!---------------------------------------------------------------------------
! input/output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(INOUT) :: x1, x2
!---------------------------------------------------------------------------
! output arguments
!---------------------------------------------------------------------------
REAL(KIND=BGC_r8), INTENT(OUT) :: soln
!---------------------------------------------------------------------------
! local variable declarations
!---------------------------------------------------------------------------
LOGICAL(KIND=BGC_log) :: leave_bracket, dx_decrease, mask
INTEGER(KIND=BGC_i4) :: i, it
REAL(KIND=BGC_r8) :: temp
REAL(KIND=BGC_r8) :: xlo, xhi, flo, fhi, f, df, dxold, dx
!---------------------------------------------------------------------------
! bracket root at each location and set up first iteration
!---------------------------------------------------------------------------
it = 0
DO
CALL talk_row(k1, k2, x1, flo, df)
CALL talk_row(k1, k2, x2, fhi, df)
mask = (flo > c0 .AND. fhi > c0) .OR. &
(flo < c0 .AND. fhi < c0)
IF (.NOT. mask) EXIT
it = it + 1
IF (it > max_bracket_grow_it) THEN
! CALL shr_sys_abort('bounding bracket for pH solution not found')
END IF
dx = sqrt(x2 / x1)
x2 = x2 * dx
x1 = x1 / dx
END DO
IF (flo .LT. c0) THEN
xlo = x1
xhi = x2
ELSE
xlo = x2
xhi = x1
temp = flo
flo = fhi
fhi = temp
END IF
soln = p5 * (xlo + xhi)
dxold = ABS(xlo - xhi)
dx = dxold
CALL talk_row(k1, k2, soln, f, df)
!---------------------------------------------------------------------------
! perform iterations, zeroing mask when a location has converged
!---------------------------------------------------------------------------
mask = .true.
DO it = 1,maxit
leave_bracket = ((soln - xhi) * df - f) * &
((soln - xlo) * df - f) .GE. 0
dx_decrease = ABS(c2 * f) .LE. ABS(dxold * df)
IF (leave_bracket .OR. .NOT. dx_decrease) THEN
dxold = dx
dx = p5 * (xhi - xlo)
soln = xlo + dx
IF (xlo .EQ. soln) mask = .FALSE.
ELSE
dxold = dx
dx = -f / df
temp = soln
soln = soln + dx
IF (temp .EQ. soln) mask = .FALSE.
END IF
IF (ABS(dx) .LT. xacc) mask = .FALSE.
IF (.NOT. mask) RETURN
CALL talk_row(k1, k2, soln, f, df)
IF (f .LT. c0) THEN
xlo = soln
flo = f
ELSE
xhi = soln
fhi = f
END IF
END DO ! iteration loop
#ifdef CCSMCOUPLED
! CALL shr_sys_abort('lack of convergence in drtsafe_row')
#endif
END SUBROUTINE drtsafe_row
!*****************************************************************************