forked from AquaticEcoDynamics/libaed2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaed2_carbon.F90
1234 lines (995 loc) · 49.2 KB
/
aed2_carbon.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
!###############################################################################
!# #
!# aed2_carbon.F90 #
!# #
!# Developed by : #
!# AquaticEcoDynamics (AED) Group #
!# School of Agriculture and Environment #
!# The University of Western Australia #
!# #
!# http://aquatic.science.uwa.edu.au/ #
!# #
!# Copyright 2013 - 2020 - The University of Western Australia #
!# #
!# GLM is free software: you can redistribute it and/or modify #
!# it under the terms of the GNU General Public License as published by #
!# the Free Software Foundation, either version 3 of the License, or #
!# (at your option) any later version. #
!# #
!# GLM 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 General Public License for more details. #
!# #
!# You should have received a copy of the GNU General Public License #
!# along with this program. If not, see <http://www.gnu.org/licenses/>. #
!# #
!# ----------------------------------------------------------------------- #
!# #
!# Created March 2012 #
!# #
!###############################################################################
! !
! .----------------. .----------------. .----------------. !
! | .--------------. || .--------------. || .--------------. | !
! | | ______ | || | __ | || | _______ | | !
! | | .' ___ | | || | / \ | || | |_ __ \ | | !
! | | / .' \_| | || | / /\ \ | || | | |__) | | | !
! | | | | | || | / ____ \ | || | | __ / | | !
! | | \ `.___.'\ | || | _/ / \ \_ | || | _| | \ \_ | | !
! | | `._____.' | || ||____| |____|| || | |____| |___| | | !
! | | | || | | || | | | !
! | '--------------' || '--------------' || '--------------' | !
! '----------------' '----------------' '----------------' !
! !
!###############################################################################
#include "aed2.h"
!
MODULE aed2_carbon
!-------------------------------------------------------------------------------
! aed2_carbon --- carbon biogeochemical model
!
! The AED module carbon contains equations that describe exchange of
! dissolved inorganic carbon across the air/water interface and sediment flux,
! and simulation of methane.
!-------------------------------------------------------------------------------
USE aed2_core
USE aed2_util, ONLY: aed2_gas_piston_velocity
IMPLICIT NONE
PRIVATE
!
PUBLIC aed2_carbon_data_t
!
TYPE,extends(aed2_model_data_t) :: aed2_carbon_data_t
!# Variable identifiers
INTEGER :: id_dic, id_pH, id_ch4, id_oxy, id_talk, id_ch4_bub
INTEGER :: id_Fsed_dic, id_Fsed_ch4
INTEGER :: id_temp, id_salt
INTEGER :: id_wind, id_vel, id_depth
INTEGER :: id_ch4ox, id_pco2
INTEGER :: id_sed_dic, id_sed_ch4, id_sed_ch4_ebb, id_sed_ch4_ebb_3d
INTEGER :: id_atm_co2, id_atm_ch4, id_atm_ch4_ebb, id_ch4_ebb_df
INTEGER :: id_par, id_extc, id_dz, id_tau, id_Fsed_ch4_ebb
!# Model parameters
AED_REAL :: Fsed_dic, Ksed_dic, theta_sed_dic
AED_REAL :: Fsed_ch4, Ksed_ch4, theta_sed_ch4, Fsed_ch4_ebb, ch4_bub_tau0
AED_REAL :: Rch4ox, Kch4ox, vTch4ox, atm_co2, atm_ch4, ionic
AED_REAL :: maxMPBProdn, IkMPB
AED_REAL :: ch4_bub_aLL, ch4_bub_cLL, ch4_bub_kLL, ch4_bub_ws
AED_REAL :: ch4_bub_disf1, ch4_bub_disf2, ch4_bub_disdp
!# Model options
LOGICAL :: use_oxy, use_sed_model_dic, use_sed_model_ch4, use_sed_model_ebb
LOGICAL :: simDIC, simCH4, simCH4ebb
INTEGER :: alk_mode, co2_model, co2_piston_model, ch4_piston_model
CONTAINS
PROCEDURE :: define => aed2_define_carbon
PROCEDURE :: calculate_surface => aed2_calculate_surface_carbon
PROCEDURE :: calculate => aed2_calculate_carbon
PROCEDURE :: calculate_benthic => aed2_calculate_benthic_carbon
PROCEDURE :: equilibrate => aed2_equilibrate_carbon
! PROCEDURE :: mobility => aed2_mobility_carbon
! PROCEDURE :: light_extinction => aed2_light_extinction_carbon
! PROCEDURE :: delete => aed2_delete_carbon
END TYPE
! MODULE GLOBALS
INTEGER :: diag_level = 10
!===============================================================================
CONTAINS
!###############################################################################
SUBROUTINE aed2_define_carbon(data, namlst)
!-------------------------------------------------------------------------------
! Initialise the AED model
!
! Here, the aed namelist is read and te variables exported
! by the model are registered with AED2.
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_carbon_data_t),INTENT(inout) :: data
INTEGER,INTENT(in) :: namlst
!
!LOCALS
INTEGER :: status
! %% NAMELIST VARS
AED_REAL :: dic_initial = 1000.0
AED_REAL :: pH_initial = 7.5
AED_REAL :: ch4_initial = 4.5
AED_REAL :: ionic = 0.0
AED_REAL :: Fsed_dic = 0.0
AED_REAL :: Ksed_dic = 30.0
AED_REAL :: theta_sed_dic = 1.0
CHARACTER(len=64) :: Fsed_dic_variable=''
AED_REAL :: Fsed_ch4 = 0.0
AED_REAL :: Ksed_ch4 = 30.0
AED_REAL :: theta_sed_ch4 = 1.0
CHARACTER(len=64) :: Fsed_ch4_variable=''
AED_REAL :: atm_co2 = 367e-6
AED_REAL :: atm_ch4 = 1.76e-6
AED_REAL :: Rch4ox = 0.01
AED_REAL :: Kch4ox = 0.01
AED_REAL :: vTch4ox = 1.05
CHARACTER(len=64) :: methane_reactant_variable=''
! CHARACTER(len=64) :: carbon_pco2_link = 'CAR_pCO2' ! added by PHuang
! ! removed by CAB because it's not used
AED_REAL :: maxMPBProdn = 40.0 ! mmolC/m2/day
AED_REAL :: IkMPB = 180.0 ! Light sensitivity of MPB
INTEGER :: co2_model = 1
INTEGER :: alk_mode = 1
INTEGER :: co2_piston_model = 1
INTEGER :: ch4_piston_model = 1
LOGICAL :: simCH4ebb
AED_REAL :: Fsed_ch4_ebb = zero_
AED_REAL :: ch4_bub_tau0 = one_
CHARACTER(len=64) :: Fsed_ebb_variable=''
AED_REAL :: ch4_bub_aLL = 42.9512677
AED_REAL :: ch4_bub_cLL = 0.634
AED_REAL :: ch4_bub_kLL = -0.8247
AED_REAL :: ch4_bub_disf1 = 0.07
AED_REAL :: ch4_bub_disf2 = 0.33
AED_REAL :: ch4_bub_disdp = 20.0
AED_REAL :: ch4_bub_ws = zero_
! %% END NAMELIST VARS
NAMELIST /aed2_carbon/ dic_initial,pH_initial,ch4_initial,ionic, &
Fsed_dic,Ksed_dic,theta_sed_dic,Fsed_dic_variable, &
Fsed_ch4,Ksed_ch4,theta_sed_ch4,Fsed_ch4_variable, &
atm_co2,atm_ch4,Rch4ox,Kch4ox,vTch4ox, &
methane_reactant_variable, &
maxMPBProdn, IkMPB, &
co2_model, alk_mode, &
co2_piston_model, ch4_piston_model, &
simCH4ebb, Fsed_ch4_ebb, Fsed_ebb_variable, &
ch4_bub_aLL,ch4_bub_cLL, ch4_bub_kLL, &
ch4_bub_disf1, ch4_bub_disf2, ch4_bub_disdp, &
ch4_bub_ws, ch4_bub_tau0
!-------------------------------------------------------------------------------
!BEGIN
print *," aed2_carbon initialization"
!# Set defaults
data%simCH4ebb = .false.
data%simDIC = .false.
data%simCH4 = .false.
!# Read the namelist
read(namlst,nml=aed2_carbon,iostat=status)
IF (status /= 0) THEN
print *,'Error reading namelist aed2_carbon'
STOP
ENDIF
!# Store parameter values in our own derived type
! NB: all rates must be provided in values per day,
! and are converted here to values per second.
data%Fsed_dic = Fsed_dic/secs_per_day
data%Ksed_dic = Ksed_dic
data%theta_sed_dic = theta_sed_dic
data%ionic = ionic
data%co2_model = co2_model
data%alk_mode = alk_mode
data%atm_co2 = atm_co2
data%co2_piston_model = co2_piston_model
data%Fsed_ch4 = Fsed_ch4/secs_per_day
data%Ksed_ch4 = Ksed_ch4
data%theta_sed_ch4 = theta_sed_ch4
data%Rch4ox = Rch4ox/secs_per_day
data%Kch4ox = Kch4ox
data%vTch4ox = vTch4ox
data%atm_ch4 = atm_ch4
data%ch4_piston_model = ch4_piston_model
data%simCH4ebb = simCH4ebb
data%Fsed_ch4_ebb = Fsed_ch4_ebb
data%ch4_bub_tau0 = ch4_bub_tau0
data%ch4_bub_aLL = ch4_bub_aLL
data%ch4_bub_cLL = ch4_bub_cLL
data%ch4_bub_kLL = ch4_bub_kLL
data%ch4_bub_disf1 = ch4_bub_disf1
data%ch4_bub_disf2 = ch4_bub_disf2
data%ch4_bub_disdp = ch4_bub_disdp
data%ch4_bub_ws = ch4_bub_ws
data%maxMPBProdn = maxMPBProdn
data%IkMPB = IkMPB
!# Register state variables
IF (dic_initial>MISVAL) THEN
data%id_dic = aed2_define_variable('dic','mmol/m**3','dissolved inorganic carbon', &
dic_initial,minimum=zero_)
data%simDIC = .true.
data%id_pH = aed2_define_variable('pH','-','pH', &
pH_initial,minimum=zero_)
ENDIF
IF (ch4_initial>MISVAL) THEN
data%simCH4 = .true.
data%id_ch4 = aed2_define_variable('ch4','mmol/m**3','methane', &
ch4_initial,minimum=zero_)
IF( data%simCH4ebb ) THEN
data%id_ch4_bub = aed2_define_variable('ch4_bub','mmol/m**3', &
'methane bubbles',zero_,minimum=zero_)
ENDIF
ENDIF
!# Register external state variable dependencies
data%use_oxy = methane_reactant_variable .NE. '' !This means oxygen module switched on
IF (data%use_oxy) THEN
data%id_oxy = aed2_locate_variable(methane_reactant_variable)
ENDIF
data%use_sed_model_dic = Fsed_dic_variable .NE. ''
IF (data%use_sed_model_dic) &
data%id_Fsed_dic = aed2_locate_global_sheet(Fsed_dic_variable)
data%use_sed_model_ch4 = Fsed_ch4_variable .NE. ''
IF (data%use_sed_model_ch4) &
data%id_Fsed_ch4 = aed2_locate_global_sheet(Fsed_ch4_variable)
data%use_sed_model_ebb = Fsed_ebb_variable .NE. ''
IF (data%use_sed_model_ebb) &
data%id_Fsed_ch4_ebb = aed2_locate_global_sheet(Fsed_ebb_variable)
!# Register diagnostic variables
data%id_pco2 = aed2_define_diag_variable('pCO2','atm', 'pCO2')
data%id_sed_dic = aed2_define_sheet_diag_variable('sed_dic','mmol/m**2/d', &
'CO2 exchange across sed/water interface')
data%id_atm_co2 = aed2_define_sheet_diag_variable('atm_co2_flux', &
'mmol/m**2/d', 'CO2 exchange across atm/water interface')
IF( data%simCH4 ) THEN
data%id_ch4ox = aed2_define_diag_variable('ch4ox','mmol/m**3/d', 'methane oxidation rate')
data%id_sed_ch4 = aed2_define_sheet_diag_variable('sed_ch4','mmol/m**2/d', &
'CH4 exchange across sed/water interface')
data%id_atm_ch4 = aed2_define_sheet_diag_variable('atm_ch4_flux', &
'mmol/m**2/d', 'CH4 exchange across atm/water interface')
IF( data%simCH4ebb ) THEN
data%id_sed_ch4_ebb_3d = aed2_define_diag_variable('sed_ch4_ebb_3d','mmol/m**3/d', &
'CH4 ebullition release rate')
data%id_ch4_ebb_df = aed2_define_diag_variable('ch4_ebb_df','mmol/m**3/d', &
'CH4 bubble dissolution rate')
data%id_sed_ch4_ebb = aed2_define_sheet_diag_variable('sed_ch4_ebb','mmol/m**2/d', &
'CH4 ebullition across sed/water interface')
data%id_atm_ch4_ebb = aed2_define_sheet_diag_variable('atm_ch4_ebb_flux', &
'mmol/m**2/d', 'CH4 ebullition across atm/water interface')
ENDIF
ENDIF
!# Register environmental dependencies
data%id_temp = aed2_locate_global('temperature')
data%id_salt = aed2_locate_global('salinity')
data%id_extc = aed2_locate_global('extc_coef')
data%id_par = aed2_locate_global('par')
data%id_dz = aed2_locate_global('layer_ht')
data%id_vel = aed2_locate_global('cell_vel') ! needed for k600
data%id_depth= aed2_locate_global('depth')
! data%id_depth= aed2_locate_global('layer_ht')
data%id_wind = aed2_locate_global_sheet('wind_speed')
IF( data%simCH4ebb ) data%id_tau = aed2_locate_global_sheet('taub')
END SUBROUTINE aed2_define_carbon
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_calculate_carbon(data,column,layer_idx)
!-------------------------------------------------------------------------------
! Right hand sides of aed2_carbon model
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_carbon_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
AED_REAL :: dic,ch4,oxy,temp
AED_REAL :: ch4oxidation
!
!-------------------------------------------------------------------------------
!BEGIN
IF(data%simDIC .AND. data%simCH4) THEN
! Retrieve current (local) state variable values.
dic = _STATE_VAR_(data%id_dic) ! DIC
ch4 = _STATE_VAR_(data%id_ch4) ! CH4
!# Retrieve current dependent state variable values.
IF (data%use_oxy) THEN
oxy = _STATE_VAR_(data%id_oxy) ! O2
ELSE
oxy = 0.0
ENDIF
!# Retrieve current environmental conditions.
temp = _STATE_VAR_(data%id_temp) ! temperature
!# Compute rates of change (mmol C/m3/day)
ch4oxidation = aed2_carbon_fch4ox(data%use_oxy,data%Rch4ox,data%Kch4ox,data%vTch4ox,oxy,temp)
!# Set temporal derivatives
_FLUX_VAR_(data%id_dic) = _FLUX_VAR_(data%id_dic) + (ch4*ch4oxidation)
_FLUX_VAR_(data%id_ch4) = _FLUX_VAR_(data%id_ch4) + (-ch4*ch4oxidation)
!# If a linked oxygen pool is present, take oxidation from it
IF (data%use_oxy) THEN
_FLUX_VAR_(data%id_oxy) = _FLUX_VAR_(data%id_oxy) + (-(32./12.)*ch4*ch4oxidation)
ENDIF
!# Export diagnostic variables
_DIAG_VAR_(data%id_ch4ox) = ch4*ch4oxidation*secs_per_day
ENDIF
END SUBROUTINE aed2_calculate_carbon
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_calculate_surface_carbon(data,column,layer_idx)
!-------------------------------------------------------------------------------
! Air-sea exchange for the aed carbon model
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_carbon_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
! Environment
AED_REAL :: temp, salt, wind, S, T, depth, vel
! State
AED_REAL :: dic,pH,ch4,talk = 0.,TCO2 = 0.
! Temporary variables
AED_REAL :: pCO2 = 0.,FCO2,FCH4,henry
AED_REAL :: Ko,kCH4,KCO2, CH4solub
AED_REAL :: Tabs,windHt,atm
AED_REAL :: A1,A2,A3,A4,B1,B2,B3,logC
AED_REAL :: a,b,c,dcf
AED_REAL :: ca, bc, cb, carba, bicarb, carb, om_cal, om_arg
AED_REAL :: p00,p10,p01,p20,p11,p02
INTEGER :: ii
!-------------------------------------------------------------------------------
!BEGIN
IF(.NOT.data%simDIC .AND. .NOT.data%simCH4) RETURN
!----------------------------------------------------------------------------
!# Get dependent state variables from physical driver
windHt = 10.
wind = _STATE_VAR_S_(data%id_wind) ! Wind speed at 10 m above surface (m/s)
temp = _STATE_VAR_(data%id_temp) ! Temperature (degrees Celsius)
salt = _STATE_VAR_(data%id_salt) ! Salinity (psu)
depth = MAX( _STATE_VAR_(data%id_depth), one_ )
IF (data%id_vel > 0 ) THEN
vel = _STATE_VAR_(data%id_vel)
ELSE
! vel = SQRT(_STATE_VAR_(data%id_E_tau)/_STATE_VAR_(data%id_E_dens))
! vel = vel/0.41 * log(depth/0.01)
vel = 0.0001
ENDIF
Tabs = temp + 273.15
!# Solubility, Ko (mol/L/atm)
Ko = -58.0931+90.5069*(100.0/Tabs) + 22.294*log(Tabs/100.0) &
+ 0.027766*salt - 0.025888*salt*(Tabs/100.0)
Ko = Ko + 0.0050578*salt*(Tabs/100.0)*(Tabs/100.0)
Ko = exp(Ko)
!----------------------------------------------------------------------------
!# CO2 concentration in the surface layer, depends on DIC, TA
IF(data%simDIC) THEN
!# Retrieve current (local) state variable values.
dic = _STATE_VAR_(data%id_dic)! Concentration of carbon in surface layer
pH = _STATE_VAR_(data%id_pH) ! Concentration of carbon in surface layer
IF( data%co2_model == 1 ) THEN
!# Use the Haltafall CO2 code for computing pCO2 & pH
S=salt; T=temp
IF( data%alk_mode == 1 ) THEN
! talk = 520.1 + 51.24*S ! Atlantic (Millero 1998) from fabm, not suitable for estuaries
! talk = 1136.1 + 1.2*S*S + 2.8*S !Chesapeake Bay (George et al., 2013)
talk = 1627.4 + 22.176*S !regression from Naomi's data on Caboolture
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
! next, use the CO2 module (same to CDIAC module) to calc pCO2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 2 ) THEN
p00 = 1063
p10 = 1.751
p01 = -0.05369
p20 = 0.2266
p11 = -0.001252
p02 = 0.0002546
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 3 ) THEN
p00 = -258.8
p10 = 34.59
p01 = 0.9923
p20 = 0.8186
p11 = -0.03101
p02 = 0.0001045
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 4 ) THEN
p00 = -47.51
p10 = -17.21
p01 = 1.32
p20 = 0.1439
p11 = 0.01224
p02 = -0.0002055
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 5 ) THEN
p00 = 157.7
p10 = 4.298
p01 = 0.6448
p20 = 0.2107
p11 = -0.002072
p02 = 0.0001239
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ENDIF
CALL CO2SYS(T,S,talk,TCO2,pCO2,pH)
! Adjust outputs back to units used in the parent model code (e.g. mmol/m3) if appropriate
! note the output pCO2 is in unit of ATM
! pCO2 = pCO2*1.0D6 ! partial pressure of co2 in water
! _STATE_VAR_(data%id_talk) = talk*(1.0D6) ! total alkalinity (umol/kg)
_DIAG_VAR_(data%id_pco2) = pCO2
ELSEIF ( data%co2_model == 2 ) THEN
!# Use the Butler CO2 code for computing pCO2 & pH
pCO2 = aed2_carbon_co2(data%ionic,temp,dic,ph)*1e-6 / Ko !(=atm), use Yanti's script for pCO2
_DIAG_VAR_(data%id_pco2) = pCO2
ELSEIF ( data%co2_model == 0 ) THEN
!# Use the aed2_geochem module for computing pCO2 & pH
pCO2 = _DIAG_VAR_(data%id_pco2) ! this diagnostic is getting set in aed2_geocehmistry
ENDIF
!# Now compute piston velocity, k
kCO2 = aed2_gas_piston_velocity(windHt,wind,temp,salt, &
vel=vel,depth=depth,schmidt_model=2,piston_model=data%co2_piston_model)
!# Now compute the CO2 flux
! FCO2 = kCO2 * Ko * (pCO2 - PCO2a)
! pCO2a = 367e-6 atm (Keeling & Wharf, 1999)
! mmol/m2/s = m/s * mmol/m3/atm * atm
! FCO2 = - kCO2 * Ko*1e6 * ((pCO2 * 1e-6) - data%atm_co2) ! dCO2/dt
FCO2 = kCO2 * (1e6*Ko) * (pCO2 - data%atm_co2)
!--------------------------------------------------------------------------
!# Transfer surface exchange value to AED2 (mmmol/m2/s) converted by driver
_FLUX_VAR_T_(data%id_dic) = -FCO2
!# Also store co2 flux across the atm/water interface as a
! diagnostic variable (mmmol/m2/d)
_DIAG_VAR_S_(data%id_atm_co2) = FCO2*secs_per_day
END IF
!----------------------------------------------------------------------------
!# CH4 flux
IF(data%simCH4) THEN
! Algorithm from Arianto Santoso <abs11@students.waikato.ac.nz>
! Concentration of methane in surface layer
ch4 = _STATE_VAR_(data%id_ch4)
! Piston velocity for CH4
kCH4 = aed2_gas_piston_velocity(windHt,wind,temp,salt, &
vel=vel,depth=depth,schmidt_model=4,piston_model=data%ch4_piston_model)
! Solubility, Ko (mol/L/atm)
atm = data%atm_ch4 ! 1.76 * 1e-6 !## current atmospheric CH4 data from NOAA (in ppm? atm)
A1 = -415.2807
A2 = 596.8104
A3 = 379.2599
A4 = -62.0757
B1 = -0.05916
B2 = 0.032174
B3 = -0.0048198
logC = (log(atm)) + A1 &
+ (A2 * (100./Tabs)) + (A3 * log (Tabs/100.)) + (A4 * (Tabs/100.)) &
+ salt * (B1 + (B2 * (Tabs/100.)) + (B3 * (Tabs/100.)*(Tabs/100.)))
CH4solub = exp(logC) * 1e-3
!# Now compute methane atm flux (mmol/m2/s = m/s * mmol/m3)
FCH4 = kCH4 * (ch4 - CH4solub)
!----------------------------------------------------------------------------
!# Transfer surface exchange value to AED2 (mmmol/m2) converted by driver.
_FLUX_VAR_T_(data%id_ch4) = -FCH4
!# Also store CH4 flux across the atm/water interface as
! diagnostic variable (mmmol/m2/d)
_DIAG_VAR_S_(data%id_atm_ch4) = FCH4*secs_per_day
END IF
!----------------------------------------------------------------------------
END SUBROUTINE aed2_calculate_surface_carbon
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_calculate_benthic_carbon(data,column,layer_idx)
!-------------------------------------------------------------------------------
! Calculate pelagic bottom fluxes and benthic sink and source terms of AED carbon.
! Everything in units per surface area (not volume!) per time.
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_carbon_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
! Environment
AED_REAL :: temp, par, extc, dz, depth
! State
AED_REAL :: dic, oxy, mpb, ph
! Temporary variables
AED_REAL :: dic_flux, ch4_flux, Fsed_dic, Fsed_ch4, ebb_flux, Fsed_ch4_ebb, ch4_bub_disf
!AED_REAL, PARAMETER :: maxMPBProdn = 40. ! mmolC/m2/day !
!AED_REAL, PARAMETER :: IkMPB = 180.0 ! Light sensitivity of MPB !
!-------------------------------------------------------------------------------
!BEGIN
IF(.NOT.data%simDIC) RETURN
! Retrieve current environmental conditions for the bottom pelagic layer.
temp = _STATE_VAR_(data%id_temp) ! local temperature
par = _STATE_VAR_(data%id_par) ! local par
dz = _STATE_VAR_(data%id_dz) ! local layer thickness
depth= _STATE_VAR_(data%id_depth)! local layer depth
extc = _STATE_VAR_(data%id_extc) ! local extinction
! Retrieve current (local) state variable values.
dic = _STATE_VAR_(data%id_dic)! carbon
pH = _STATE_VAR_(data%id_pH)! pH
IF ( data%use_sed_model_dic ) THEN
Fsed_dic = _STATE_VAR_S_(data%id_Fsed_dic)
ELSE
Fsed_dic = data%Fsed_dic
ENDIF
IF ( data%use_sed_model_ch4 ) THEN
Fsed_ch4 = _STATE_VAR_S_(data%id_Fsed_ch4)
IF( data%simCH4ebb ) Fsed_ch4_ebb = _STATE_VAR_S_(data%id_Fsed_ch4_ebb)
ELSE
Fsed_ch4 = data%Fsed_ch4
IF( data%simCH4ebb ) Fsed_ch4_ebb = data%Fsed_ch4_ebb
ENDIF
IF (data%use_oxy) THEN
! Sediment flux dependent on oxygen and temperature
oxy = _STATE_VAR_(data%id_oxy)
dic_flux = Fsed_dic * oxy/(data%Ksed_dic+oxy) * (data%theta_sed_dic**(temp-20.0))
ch4_flux = Fsed_ch4 * data%Ksed_ch4/(data%Ksed_ch4+oxy) * (data%theta_sed_ch4**(temp-20.0))
IF( data%simCH4ebb ) ebb_flux = Fsed_ch4_ebb * (data%theta_sed_ch4**(temp-20.0))
ELSE
! Sediment flux dependent on temperature only.
dic_flux = Fsed_dic * (data%theta_sed_dic**(temp-20.0))
ch4_flux = Fsed_ch4 * (data%theta_sed_ch4**(temp-20.0))
IF( data%simCH4ebb ) THEN
ebb_flux = Fsed_ch4_ebb * (data%theta_sed_ch4**(temp-20.0))
! Kinneret special lake level equations
ebb_flux = ebb_flux * data%ch4_bub_cLL * exp(data%ch4_bub_kLL*(data%ch4_bub_aLL-depth))
ENDIF
ENDIF
!! Allow photosynthetic production of CO2 in the benthos due to MPB if light and suitable pH
!par = par * (exp(-extc*dz))
! IF( par > 50. .AND. pH > 5.5 .AND. pH < 9.6 ) THEN
! mpb = (data%maxMPBProdn/secs_per_day)*(1.0-exp(-par/data%IkMPB)) * (data%theta_sed_dic**(temp-20.0))
! dic_flux = Fsed_dic - mpb
! IF (data%use_oxy) THEN
! _FLUX_VAR_(data%id_oxy) = _FLUX_VAR_(data%id_oxy) + mpb
! ENDIF
! ENDIF
! Set bottom fluxes for the pelagic (flux per surface area, per second)
! Increment sediment flux value into derivative of water column variable
_FLUX_VAR_(data%id_dic) = _FLUX_VAR_(data%id_dic) + (dic_flux)
IF( data%simCH4) _FLUX_VAR_(data%id_ch4) = _FLUX_VAR_(data%id_ch4) + (ch4_flux)
! Store dissolved sediment fluxes as diagnostic variables (flux per surface area, per day)
_DIAG_VAR_S_(data%id_sed_dic) = dic_flux * secs_per_day
IF( data%simCH4) _DIAG_VAR_S_(data%id_sed_ch4) = ch4_flux * secs_per_day
! Re-distribute bubbles to the water or atmosphere, or dissolve
IF( data%simCH4ebb ) THEN
! Add bubbles to layer
!_FLUX_VAR_(data%id_ch4_bub) = _FLUX_VAR_(data%id_ch4_bub) + (ebb_flux)
! Dissolve bubbles in this bottom layer, depending on depth
ch4_bub_disf = data%ch4_bub_disf1
IF( depth > data%ch4_bub_disdp) ch4_bub_disf = data%ch4_bub_disf2
IF( data%simCH4) _FLUX_VAR_(data%id_ch4) = _FLUX_VAR_(data%id_ch4) + ebb_flux*ch4_bub_disf
_DIAG_VAR_(data%id_ch4_ebb_df) = ebb_flux*ch4_bub_disf * secs_per_day !/ dz. MH Something wrong with dz here?
! Release the remainder to the atmosphere (mmol/m2/day)
_DIAG_VAR_S_(data%id_atm_ch4_ebb) = ebb_flux * (1-ch4_bub_disf) * secs_per_day
! Note the bubble flux, as the zone sees it (mmol/m2/day)
_DIAG_VAR_S_(data%id_sed_ch4_ebb) = ebb_flux * secs_per_day
! Note the bubble flux, as the water sees it (mmol/m3/day)
_DIAG_VAR_(data%id_sed_ch4_ebb_3d) = ebb_flux * secs_per_day / dz
ENDIF
! Set sink and source terms for the benthos (change per surface area per second)
! Note that this must include the fluxes to and from the pelagic.
!_FLUX_VAR_B_(data%id_ben_dic) = _FLUX_VAR_B_(data%id_ben_dic) + (-dic_flux/secs_per_day)
END SUBROUTINE aed2_calculate_benthic_carbon
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
SUBROUTINE aed2_equilibrate_carbon(data,column,layer_idx)
!-------------------------------------------------------------------------------
! Update pH after kinetic transformations are applied
!-------------------------------------------------------------------------------
!ARGUMENTS
CLASS (aed2_carbon_data_t),INTENT(in) :: data
TYPE (aed2_column_t),INTENT(inout) :: column(:)
INTEGER,INTENT(in) :: layer_idx
!
!LOCALS
! State
AED_REAL :: dic, pH, pCO2, temp, salt
AED_REAL :: S,T,a,b,c,dcf,talk = 0.,TCO2 = 0.,ca,bc,cb,HENRY
AED_REAL :: p00,p10,p01,p20,p11,p02
!-------------------------------------------------------------------------------
!BEGIN
IF(.NOT.data%simDIC) RETURN
pCO2 = zero_
pH = _STATE_VAR_(data%id_pH) ! pH = zero_
!# Retrieve current (local) state variable values.
dic = _STATE_VAR_(data%id_dic) ! Concentration of DIC in the cell
salt = _STATE_VAR_(data%id_salt) ! Concentration of DIC in the cell
temp = _STATE_VAR_(data%id_temp) ! Concentration of DIC in the cell
IF ( data%co2_model == 1 ) THEN
!# Use the Haltafall CO2 code for computing pCO2 & pH
S=salt; T=temp
IF( data%alk_mode == 1 ) THEN
S=salt; T=temp
! talk = 520.1 + 51.24*S ! Atlantic (Millero 1998) from fabm, not suitable for estuaries
! talk = 1136.1 + 1.2*S*S + 2.8*S !Chesapeake Bay (George et al., 2013)
talk = 1627.4 + 22.176*S !regression from Naomi's data on Caboolture
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
! next, use the CO2 module (same to CDIAC module) to calc pCO2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 2 ) THEN
p00 = 1063
p10 = 1.751
p01 = -0.05369
p20 = 0.2266
p11 = -0.001252
p02 = 0.0002546
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 3 ) THEN
p00 = -258.8
p10 = 34.59
p01 = 0.9923
p20 = 0.8186
p11 = -0.03101
p02 = 0.0001045
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 4 ) THEN
p00 = -47.51
p10 = -17.21
p01 = 1.32
p20 = 0.1439
p11 = 0.01224
p02 = -0.0002055
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ELSEIF( data%alk_mode == 5 ) THEN
p00 = 157.7
p10 = 4.298
p01 = 0.6448
p20 = 0.2107
p11 = -0.002072
p02 = 0.0001239
a = 8.24493d-1 - 4.0899d-3*T + 7.6438d-5*T**2 - 8.2467d-7*T**3 + 5.3875d-9*T**4
b = -5.72466d-3 + 1.0227d-4*T - 1.6546d-6*T**2
c = 4.8314d-4
dcf = (999.842594 + 6.793952d-2*T- 9.095290d-3*T**2 + 1.001685d-4*T**3 &
- 1.120083d-6*T**4 + 6.536332d-9*T**5+a*S+b*S**1.5+c*S**2)/1.0D3
talk = p00 + p10*S + p01*dic + p20*S**2 + p11*dic*S + p02*dic**2
talk = talk / 1.0D6 ! change unit to mol/kgSW
TCO2 = dic / (1.0D6*dcf) ! change unit to mol/kgSW
ENDIF
!CALL CO2DYN ( TCO2, talk, T, S, pCO2, pH, HENRY, ca, bc, cb)
CALL CO2SYS(T,S,talk,TCO2,pCO2,pH)
ENDIF
!# SET PCO2 & pH as returned
_DIAG_VAR_(data%id_pco2) = pCO2
_STATE_VAR_(data%id_pH) = pH
END SUBROUTINE aed2_equilibrate_carbon
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
PURE AED_REAL FUNCTION aed2_carbon_fch4ox(use_oxy,Rch4ox,Kch4ox,vTch4ox,oxy,temp)
!-------------------------------------------------------------------------------
! Michaelis-Menten formulation for methane oxidation
!
! Here, the classical Michaelis-Menten formulation for nitrification
! is formulated.
!-------------------------------------------------------------------------------
!ARGUMENTS
LOGICAL,INTENT(in) :: use_oxy
AED_REAL,INTENT(in) :: Rch4ox,Kch4ox,vTch4ox,oxy,temp
!
!-------------------------------------------------------------------------------
!BEGIN
IF (use_oxy) THEN
aed2_carbon_fch4ox = Rch4ox * oxy/(Kch4ox+oxy) * (vTch4ox**(temp-20.0))
ELSE
aed2_carbon_fch4ox = Rch4ox * (vTch4ox**(temp-20.0))
ENDIF
END FUNCTION aed2_carbon_fch4ox
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
!###############################################################################
PURE AED_REAL FUNCTION aed2_carbon_co2(ionic, temp, dic, pH)
!-------------------------------------------------------------------------------
! CO2 concentration of DIC at fixed T, from Butler
!-------------------------------------------------------------------------------
!ARGUMENTS
AED_REAL, INTENT(IN) :: ionic, dic, temp, pH
!
!LOCALS
! Temporary variables
AED_REAL :: K_h, Kw, Ka1, Ka2, i_f
AED_REAL :: H, CO2, HCO3, CO3, TA
!-------------------------------------------------------------------------------
!BEGIN
! Acidity constants temperature dependence
! pKh = -0.000075324675x2 + 0.016279653680x + 1.110424242424
! pKa1 = 0.000142121212x2 - 0.012648181818x + 6.577539393939
! pKa2 = 0.000113679654x2 - 0.014687186147x + 10.625769696970
! pKw = 0.000201991342x2 - 0.043419653680x + 14.949709090909
K_h = -0.000075324675*temp*temp + 0.016279653680*temp + 1.110424242424
Ka1 = 0.000142121212*temp*temp - 0.012648181818*temp + 6.577539393939
Ka2 = 0.000113679654*temp*temp - 0.014687186147*temp + 10.625769696970
Kw = 0.000201991342*temp*temp - 0.043419653680*temp + 14.949709090909
! Ionic strength dependence
! 1st calculate function f
i_f = (((SQRT(ionic)) / (1+SQRT(ionic))) -0.20*ionic) * &
(298.0/(temp+273.))**0.666667
! pKh = pKh(0) + bI
! b = 0.105 (Butler, 1982)
K_h = K_h + 0.105*ionic
! pKw = pKw(0) - f
Kw = Kw - i_f
! pKa1 = pKa1(0) - f - bI
Ka1 = Ka1 - i_f - 0.105*ionic
!pKa2 = pKa2(0) - 2f
Ka2 = Ka2 + 2.0*i_f
! Convert from pK etc to Kh, Kw, Ka1, Ka2
K_h = 10.**(-K_h)
Ka1 = 10.**(-Ka1)
Ka2 = 10.**(-Ka2)
Kw = 10.**(-Kw)
! Calculate the speciation to know the molar mass of DIC !
H = 10.**(-pH)
CO3 = (Ka1*Ka2)/(H*H + Ka1*H + Ka1*Ka2)
HCO3 = (Ka1*H)/(H*H + Ka1*H + Ka1*Ka2)
CO2 = (H*H)/(H*H + Ka1*H + Ka1*Ka2)
! and update speciation (mol C/L)
CO3 = dic*CO3
HCO3 = dic*HCO3
CO2 = dic*CO2
! calculate TA for the previous timestep
TA = dic * (Ka1*H + 2.0*Ka1*Ka2) / (H*H + Ka1*H + Ka1*Ka2)
TA = TA + (Kw/H) - H
aed2_carbon_co2 = CO2
END FUNCTION aed2_carbon_co2
!+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
SUBROUTINE CO2SYS(TEM,Sal,TA0,TC0,fCO2xx,pH00)
REAL, INTENT(IN) :: Tem, Sal
REAL, INTENT(IN) :: TC0, TA0
REAL, INTENT(OUT) :: fCO2xx,pH00
! LOCAL
REAL :: PRE, K0, KS, kF, fH, KB, KW, KP1, KP2, KP3, KSi = 0., K1, K2, TB, TP, TS, TF, TSi, TC, TA
!===========Initialize the conditions =========================!
TB = 0.
TP = 0./1.e6
TS = 0.
TF = 0.
TSi = 0./1.e6
TC = TC0 !/1.e6
TA = TA0 !/1.e6
PRE = 0.
Call Cal_constants(Tem, Sal, PRE, K0, KS, kF, fH, KB, KW, KP1, KP2, KP3, &
& KSi, K1, K2, TB, TP, TS, TF)
Call Cal_pHfromTATC(TA, TC, pH00, K1, K2, TB, KB, KW, KP1, KP2, KP3,&
& TP, TSi, TS, KS, KSi, TF, KF)
Call Cal_fCO2fromTCpH(TC,pH00,fCO2xx,K1,K2,K0)
END SUBROUTINE CO2SYS