-
Notifications
You must be signed in to change notification settings - Fork 1
/
hetp_mod.F90
10232 lines (10023 loc) · 341 KB
/
hetp_mod.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 hetp_mod
IMPLICIT NONE
PRIVATE
PUBLIC :: mach_hetp_main_15cases
PRIVATE :: mach_hetp_calca2
PRIVATE :: mach_hetp_calcb4
PRIVATE :: mach_hetp_calcc2
PRIVATE :: mach_hetp_calcd3
PRIVATE :: mach_hetp_calce4
PRIVATE :: mach_hetp_calcf2
PRIVATE :: mach_hetp_calcg5
PRIVATE :: mach_hetp_calch6
PRIVATE :: mach_hetp_calci6
PRIVATE :: mach_hetp_calcj3
PRIVATE :: mach_hetp_calco7
PRIVATE :: mach_hetp_calcm8
PRIVATE :: mach_hetp_calcp13
PRIVATE :: mach_hetp_calcl9
PRIVATE :: mach_hetp_calck4
PRIVATE :: mach_hetp_calcact1
PRIVATE :: mach_hetp_calcact1b
PRIVATE :: mach_hetp_calcact2
PRIVATE :: mach_hetp_calcact2b
PRIVATE :: mach_hetp_calcact3
PRIVATE :: mach_hetp_calcact3b
PRIVATE :: mach_hetp_calcact4
PRIVATE :: mach_hetp_calcact4b
PRIVATE :: mach_hetp_poly
PRIVATE :: mach_hetp_adjust
PRIVATE :: mach_hetp_calcph
PRIVATE :: mach_hetp_calchno3
PRIVATE :: mach_hetp_calchso4
PRIVATE :: mach_calc_hclhno3
PRIVATE :: mach_hetp_calcnh3
CONTAINS
!###################################################################################
! ## HETP Code
! ## Branch/subcase determination for metastable state
! AEROSOL THERMODYNAMIC EQUILIBRIUM OF THE NH4-SO4-NO3-Na-Cl-Ca-Mg-K system
!
! ## Copyright 2023, Environment and Climate Change Canada (ECCC)
! ## Written by Stefan Miller
!###################################################################################
subroutine mach_hetp_main_15cases(TS, TA, TN, TNa, TCl, TCa, TK, TMg, temp, rh, &
so4, hso4, caso4, nh4, nh3, no3, hno3, cl, hcl, &
na, ca, k, mg, h, oh, lwc, frna, frca, frk, frmg,&
frso4, case_number)
!
use mach_hetp_mod
implicit none
!
! INPUT VARIABLES (total gas + aerosol, input as mol/m3 air)
real(dp), intent(in) :: TS ! Total sulfate
real(dp), intent(in) :: TA ! Total ammonium
real(dp), intent(in) :: TN ! Total nitrate
real(dp), intent(in) :: TNa ! Total sodium
real(dp), intent(in) :: TCl ! Total chloride
real(dp), intent(in) :: TCa ! Total calcium
real(dp), intent(in) :: TK ! Total potassium
real(dp), intent(in) :: TMg ! Total magnesium
real(dp), intent(in) :: temp ! Air temperature (K)
real(dp), intent(in) :: rh ! Relative humidity (0-1 scale; zero allowed)
!
! OUTPUT VARIABLES (output as mol/m3 air)
real(dp), intent(out) :: so4 ! SO4-- (aq)
real(dp), intent(out) :: hso4 ! HSO4- (aq)
real(dp), intent(out) :: caso4 ! CaSO4 (s)
real(dp), intent(out) :: nh4 ! NH4+ (aq)
real(dp), intent(out) :: nh3 ! NH3 (g)
real(dp), intent(out) :: no3 ! NO3- (aq)
real(dp), intent(out) :: hno3 ! HNO3 (g)
real(dp), intent(out) :: cl ! Cl- (aq)
real(dp), intent(out) :: hcl ! HCl (g)
real(dp), intent(out) :: na ! Na+ (aq)
real(dp), intent(out) :: ca ! Ca2+ (aq)
real(dp), intent(out) :: k ! K+ (aq)
real(dp), intent(out) :: mg ! Mg2+ (aq)
real(dp), intent(out) :: h ! H+ (aq)
real(dp), intent(out) :: oh ! OH- (aq)
real(dp), intent(out) :: lwc ! Aerosol liquid water content
real(dp), intent(out) :: frna ! Free Na
real(dp), intent(out) :: frca ! Free Ca
real(dp), intent(out) :: frk ! Free K
real(dp), intent(out) :: frmg ! Free Mg
real(dp), intent(out) :: frso4 ! Free SO4
real(dp), intent(out) :: case_number
!
! ## Equilibrium reactions
! #1. HSO4(aq) <==> H+(aq) + SO4=(aq)
! #2. NH3(g) <==> NH3(aq)
! #3. NH3(aq) + H2O(aq) <==> NH4+(aq) + OH-(aq)
! #4. H2O(aq) <==> H+(aq) + OH-(aq)
! #5. HNO3(g) <==> H+(aq) + NO3-(aq)
! #6. HCl(g) <==> H+(aq) + Cl-(aq)
! #7. NH4NO3(s) <==> NH3(g) + HNO3(g)
integer, parameter :: nr = 7
! #1 #2 #3 #4
real(dp), dimension(nr) :: k0 = (/1.015e-2_dp, 57.639_dp, 1.805e-05_dp, 1.010e-14_dp, &
! #5 #6 #7
2.511e6_dp, 1.971e6_dp, 5.746e-17_dp/)
!
! #1 #2 #3 #4
real(dp), dimension(nr) :: p1 = (/ 8.85_dp, 13.79_dp, -1.50_dp, -22.52_dp, &
! #5 #6 #7
29.17_dp, 30.20_dp, -74.38_dp/)
!
! #1 #2 #3 #4
real(dp), dimension(nr) :: p2 = (/25.140_dp, -5.393_dp, 26.920_dp, 26.920_dp, &
! #5 #6 #7
16.830_dp, 19.910_dp, 6.120_dp/)
!
! ## Local variables ##
real(dp) :: sulrat, sodrat, so4rat, crnarat, crrat, rest
real(dp) :: ccaso4i, cafri, ccano32i, cnacli, cmgno32i, ccacl2i, rest1
real(dp) :: cna2so4i, frso4i, nafri, cnano3i, no3fr, rest2, cmgso4i
real(dp) :: frmgi, no3fri, cmgcl2i, clfri, rest3
real(dp) :: frnh4, frno3, frcl, frca_init, frna_init, frk_init, frmg_init
!
!
! On input, set output aqueous phase species to the total gas + aerosol concentration;
! the output speciation will be partitioned and these variables reused
so4 = TS
nh4 = TA
no3 = TN
na = TNa
cl = TCl
ca = TCa
k = TK
mg = TMg
!
! Set other output species to 0.0_dp
hso4 = 0.0_dp
caso4 = 0.0_dp
nh3 = 0.0_dp
hno3 = 0.0_dp
hcl = 0.0_dp
h = 0.0_dp
oh = 0.0_dp
lwc = 0.0_dp
frna = 0.0_dp
frca = 0.0_dp
frk = 0.0_dp
frmg = 0.0_dp
!
frso4 = 0.0_dp
frnh4 = 0.0_dp
frno3 = 0.0_dp
frcl = 0.0_dp
frca_init = 0.0_dp
frna_init = 0.0_dp
frk_init = 0.0_dp
frmg_init = 0.0_dp
!
! Sulfate ratios; initialize to 0.0_dp
sulrat = 0.0_dp
sodrat = 0.0_dp
so4rat = 0.0_dp
crnarat = 0.0_dp
crrat = 0.0_dp
!
! #######################
! ### SORTING SECTION ###
! #######################
!
! ### Branch 0 ###
! ### No cases, all species < tiny: do nothing
if (TS + TA + TN + TNa + TCl + TCa + TK + TMg <= tiny) then
case_number = 0.0_dp
!
! ### Branch 1 ###
! ISRP1F: Only sulfate and ammonium
else if (TN + TNa + TCl + TCa + TK + TMg <= tiny) then
so4 = max(so4, tiny)
nh4 = max(nh4, tiny)
no3 = 0.0_dp
na = 0.0_dp
cl = 0.0_dp
ca = 0.0_dp
k = 0.0_dp
mg = 0.0_dp
!
! ## Calculate sulfate ratio
sulrat = nh4 / so4
!
! ## 1. Sulfate poor (case: calca2)
if (2.0_dp <= sulrat) then
case_number = 1.0_dp
call mach_hetp_calca2(so4, nh4, nh3, hso4, h, oh, &
lwc, rh, temp, k0, p1, p2, nr)
!
! ## 2. Sulfate rich, no acid (case: calcb4)
else if (1.0_dp <= sulrat .and. sulrat < 2.0_dp) then
case_number = 2.0_dp
call mach_hetp_calcb4(so4, nh4, nh3, hso4, h, lwc, &
rh, temp, k0, p1, p2, nr)
!
! ## 3. Sulfate rich, free acid (case: calcc2)
else if (sulrat < 1.0_dp) then
case_number = 3.0_dp
call mach_hetp_calcc2(so4, nh4, nh3, hso4, h, lwc, &
rh, temp, k0, p1, p2, nr)
end if
!
! ### Branch 2 ###
! ISRP2F: Only sulfate, ammonium and nitrate
else if (TNa + TCl + TCa + TK + TMg <= tiny) then
so4 = max(so4, tiny)
nh4 = max(nh4, tiny)
no3 = max(no3, tiny)
na = 0.0_dp
cl = 0.0_dp
ca = 0.0_dp
k = 0.0_dp
mg = 0.0_dp
!
! ## Calculate sulfate ratio
sulrat = nh4 / so4
!
! ## 4. Sulfate poor (case: calcd3)
if (2.0_dp <= sulrat) then
case_number = 4.0_dp
call mach_hetp_calcd3(so4, nh4, hno3, nh3, hso4, h, no3, &
lwc, rh, temp, k0, p1, p2, nr)
!
! ## 5. Sulfate rich, no acid (case: calce4)
else if (1.0_dp <= sulrat .and. sulrat < 2.0_dp) then
case_number = 5.0_dp
call mach_hetp_calce4(so4, nh4, hno3, hso4, h, no3, &
lwc, rh, temp, k0, p1, p2, nr)
!
! ## 6. Sulfate rich, free acid (case: calcf2)
elseif (sulrat < 1.0_dp) then
case_number = 6.0_dp
call mach_hetp_calcf2(so4, nh4, hno3, hso4, h, no3, &
lwc, rh, temp, k0, p1, p2, nr)
end if
!
! ### Branch 3 ###
! ISRP3F: Only sulfate, ammonium, nitrate, chloride and calcium
else if (TCa + TK + TMg <= tiny) then
! ## Adjust for too little ammonium and chloride
nh4 = max(nh4, tiny) ! In ISORROPIA II nh4 and cl are set to 1.0e-1_dp0 (not tiny)
cl = max(cl, tiny) ! creating 'excess mass', HETP uses tiny for mass conservation
!
! ## Adjust for too little sodium, sulfate and nitrate combined
if (na + so4 + no3 <= tiny) then
na = tiny ! In ISORROPIA II na and so4 are set to 1.0e-1_dp0 (not tiny)
so4 = tiny ! creating 'excess mass', HETP uses tiny for mass conservation
end if
!
na = max(na, tiny)
so4 = max(so4, tiny)
no3 = max(no3, tiny)
ca = 0.0_dp
k = 0.0_dp
mg = 0.0_dp
!
! ## Check if too much sodium, if too much sodium adjust
! Keep track of free amount that results after adjustment
if (na > 2.0_dp*so4 + no3 + cl) then
frna_init = na - (1.0_dp - 1.0e-6_dp)*(2.0_dp*so4 + no3 + cl)
na = (1.0_dp - 1.0e-6_dp)*(2.0_dp*so4 + no3 + cl)
! write(*,*), 'Warning error: Na adjusted'
end if
!
! ## Calculate sulfate ratios
sulrat = (na + nh4) / so4
sodrat = na / so4
!
! ## 7. Sulfate poor and sodium poor (case: calcg5)
if (2.0_dp <= sulrat .and. sodrat < 2.0_dp) then
case_number = 7.0_dp
call mach_hetp_calcg5(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, rh, temp, k0, &
p1, p2, nr)
!
! ## 8. Sulfate poor and sodium rich (case: calch6)
else if (sulrat >= 2.0_dp .and. sodrat >= 2.0_dp) then
case_number = 8.0_dp
call mach_hetp_calch6(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, frna, rh, &
temp, k0, p1, p2, nr)
!
! ## 9. Sulfate rich, no acid (case: calci6)
else if (1.0_dp <= sulrat .and. sulrat < 2.0_dp) then
case_number = 9.0_dp
call mach_hetp_calci6(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, frna, rh, &
temp, k0, p1, p2, nr)
!
! ## 10. Sulfate rich, free acid (case: calcj3)
else if (sulrat < 1.0_dp) then
case_number = 10.0_dp
call mach_hetp_calcj3(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, rh, temp, k0, &
p1, p2, nr)
end if
!
! ### Branch 4 ###
! ISRP4F: All species are possibly present, at least one crustal species
! (Ca, K, Mg) is present; Na and Cl are not necessarily present
else
! ######In HETP; do not perform the below mass adjustments############
! ####################################################################
! ## Adjust for too little ammonium and chloride
! nh4 = max(nh4, 1.0e-10_dp)
! cl = max(cl, 1.0e-10_dp)
!
! ## Adjust for too little sodium, sulfate and nitrate combined
! if (na + so4 + no3 <= 1.0e-10_dp) then
! na = 1.0e-10_dp
! so4 = 1.0e-10_dp
! end if
! ####################################################################
!
na = max(na, tiny)
so4 = max(so4, tiny)
nh4 = max(nh4, tiny)
no3 = max(no3, tiny)
cl = max(cl, tiny)
ca = max(ca, tiny)
k = max(k, tiny)
mg = max(mg, tiny)
!
! ## Check if too much sodium + crustals; if too much adjust
rest = 2.0_dp*so4 + no3 + cl
if (na + ca + k + mg > rest) then
ccaso4i = min(so4, ca)
frso4i = max(so4 - ccaso4i, 0.0_dp)
cafri = max(ca - ccaso4i, 0.0_dp)
ccano32i = min(cafri, 0.5_dp*no3)
cafri = max(cafri - ccano32i, 0.0_dp)
no3fri = max(no3 - 2.0_dp*ccano32i, 0.0_dp)
ccacl2i = min(cafri, 0.5_dp*cl)
clfri = max(cl - 2.0_dp*ccacl2i, 0.0_dp)
rest1 = 2.0_dp*frso4i + no3fri + clfri
!
cna2so4i = min(frso4i, 0.5_dp*na)
frso4i = max(frso4i - cna2so4i, 0.0_dp)
nafri = max(na - 2.0_dp*cna2so4i, 0.0_dp)
cnacli = min(nafri, clfri)
nafri = max(nafri - cnacli, 0.0_dp)
clfri = max(clfri - cnacli, 0.0_dp)
cnano3i = min(nafri, no3fri)
no3fr = max(no3fri - cnano3i, 0.0_dp)
rest2 = 2.0_dp*frso4i + no3fri + clfri
!
cmgso4i = min(frso4i, mg)
frmgi = max(mg - cmgso4i, 0.0_dp)
frso4i = max(frso4i - cmgso4i, 0.0_dp)
cmgno32i = min(frmgi, 0.5_dp*no3fri)
frmgi = max(frmgi - cmgno32i, 0.0_dp)
no3fri = max(no3fri - 2.0_dp*cmgno32i, 0.0_dp)
cmgcl2i = min(frmgi, 0.5_dp*clfri)
clfri = max(clfri - 2.0_dp*cmgcl2i, 0.0_dp)
rest3 = 2.0_dp*frso4i + no3fri + clfri
!
if (ca > rest) then ! Ca > 2*SO4 + Cl + NO3?
frca_init = max(ca - (1.0_dp - 1.0e-6_dp)*rest, 0.0_dp)
frna_init = na
frk_init = k
frmg_init = mg
ca = (1.0_dp - 1.0e-6_dp)*rest
na = 0.0_dp
k = 0.0_dp
mg = 0.0_dp
! write(*,*), 'Warning error: Ca, Na, K, Mg in excess'
!
else if (na > rest1) then ! Na > 2*frso4 + frcl + frno3?
frna_init = max(na - (1.0_dp - 1.0e-6_dp)*rest1, 0.0_dp)
frk_init = k
frmg_init = mg
na = (1.0_dp - 1.0e-6_dp)*rest1
k = 0.0_dp
mg = 0.0_dp
! write(*,*), 'Warning error: Na, K, Mg in excess'
!
else if (mg > rest2) then ! Mg > 2*frso4 + frcl + frno3?
frk_init = k
frmg_init = max(mg - (1.0_dp - 1.0e-6_dp)*rest2, 0.0_dp)
mg = (1.0_dp - 1.0e-6_dp)*rest2
k = 0.0_dp
! write(*,*), 'Warning error: K, Mg in excess'
!
else if (k > rest3) then ! K > 2*frso4 + frcl + frno3?
frk_init = max(k - (1.0_dp - 1.0e-6_dp)*rest3, 0.0_dp)
k = (1.0_dp - 1.0e-6_dp)*rest3
! write(*,*), 'Warning error: K in excess'
end if
end if
!
! ## Calculate sulfate ratios
so4rat = (na + nh4 + ca + k + mg) / so4
crnarat = (na + ca + k + mg) / so4
crrat = (ca + k + mg) / so4
!
! ## 11. Sulfate poor and dust + sodium poor
if (2.0_dp <= so4rat .and. crnarat < 2.0_dp) then
case_number = 11.0_dp
call mach_hetp_calco7(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, ca, k, mg, &
caso4, frmg, frna, frca, frk, rh, &
temp, k0, p1, p2, nr)
!
! ## 12-13. Sulfate poor and dust + sodium rich
else if (so4rat >= 2.0_dp .and. crnarat >= 2.0_dp) then
if (crrat <= 2.0_dp) then
case_number = 12.0_dp
call mach_hetp_calcm8(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, ca, k, mg, &
caso4, frca, frmg, frk, frna, &
rh, temp, k0, p1, p2, nr)
!
else if (crrat > 2.0_dp) then
case_number = 13.0_dp
call mach_hetp_calcp13(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, ca, k, mg, &
caso4, frso4, frmg, frk, frca, &
frna, rh, temp, k0, p1, p2, nr)
end if
!
! ## 14. Sulfate rich (no acid)
else if (1.0_dp <= so4rat .and. so4rat < 2.0_dp) then
case_number = 14.0_dp
call mach_hetp_calcl9(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, ca, k, mg, &
caso4, frmg, frk, frca, frna, &
frso4, rh, temp, k0, p1, p2, nr)
!
! ## 15. Sulfate super rich (free acid)
else if (so4rat < 1.0_dp) then
case_number = 15.0_dp
call mach_hetp_calck4(so4, nh4, nh3, hno3, hcl, hso4, &
na, cl, no3, h, lwc, ca, k, mg, &
caso4, rh, temp, k0, p1, p2, nr)
end if
end if
!
! ### Return output ###
! Add up free amounts after chemical partitioning at thermodynamic equilibrium
frca = frca + frca_init
frna = frna + frna_init
frk = frk + frk_init
frmg = frmg + frmg_init
lwc = lwc * 1.0e3_dp / 18.0_dp ! kg/m3 air -> mol/m3 air
!
return
end subroutine mach_hetp_main_15cases
!############################################################################
! ## HETP Code
! ## Subcase: A2; Sulfate poor
!
! ## Copyright 2023, Environment and Climate Change Canada (ECCC)
! ## Written by Stefan Miller
!
! ## Code is based on ISORROPIA II, obtained from the CMAQ air-quality
! ## model (https://github.com/USEPA/CMAQ/tree/main/CCTM/src/aero/aero6)
!############################################################################
subroutine mach_hetp_calca2(so4_i, nh4_i, nh3g_i, hso4_i, h_i, oh_i, &
lwn_i, rh, temp, k0, p1, p2, nr)
!
use mach_hetp_mod
implicit none
!
integer, intent (in) :: nr
real(dp), intent (in) :: k0 (nr)
real(dp), intent (in) :: p1 (nr)
real(dp), intent (in) :: p2 (nr)
real(dp), intent(inout) :: so4_i
real(dp), intent(inout) :: nh4_i
real(dp), intent(inout) :: hso4_i
real(dp), intent(inout) :: nh3g_i
real(dp), intent(inout) :: h_i
real(dp), intent(inout) :: oh_i
real(dp), intent(inout) :: lwn_i
real(dp), intent (in) :: rh
real(dp), intent (in) :: temp
!
! ## Local variables
real(dp) :: so4, nh4, hso4, gnh3, h, oh, lwn, t, aw, so4_t, nh4_t
real(dp) :: khso4, knh3, kh2o, errin, errouloc
real(dp) :: omebe, omehi, y1, y2, y3, x3, c1, k1, k2, k3, k4, ya, yb, xa, xb
real(dp) :: dx, tt1, tt2, lwnsq, gmax, loccondition
real(dp) :: nh, sigma, xt, xf, xh, delta, rr, gx, gx2, u1
integer :: j, k, rooteval, irh, nmax
logical :: condition, noroot, earlyexit, soln, frst, calain, calou
real(dp), dimension(13) :: gama, gamin, gamou
!
so4 = so4_i
nh4 = nh4_i
aw = rh
t = temp
hso4 = 0.0_dp
gnh3 = 0.0_dp
h = 0.0_dp
oh = 0.0_dp
lwn = 0.0_dp
so4_t = 0.0_dp
nh4_t = nh4
noroot =.false.
earlyexit = .false.
soln = .false.
frst = .true.
calain = .true.
calou = .true.
loccondition = 0.0_dp
gama = 0.1e0_dp
gamou = 0.1e0_dp
gamin = 1.0e10_dp
!
! ### Calculate equilibrium constants and other static variables ###
! ## Set RH to a range between 0.5% and 99.5%: RH = 0.00_dp will
! ## cause division by zero, aborting the code
aw = max(aw, 0.005_dp)
aw = min(aw, 0.995_dp)
!
tt1 = tstd / t - 1.0_dp
tt2 = 1.0_dp + log(tstd / t) - tstd / t
!
! ## 1. HSO4(aq) <==> H+(aq) + SO4=(aq) (xk1)
khso4 = k0(1) * exp(p1(1)*tt1 + p2(1)*tt2)
!
! ## 2. k2 = NH3(g) <==> NH3(aq) (xk21)
! ## 3. k3 = NH3(aq) + H2O(aq) <==> NH4+(aq) + OH-(aq) (xk22)
! ## Net NH3: k2*k3 (xk2)
knh3 = (k0(2) * exp(p1(2)*tt1 + p2(2)*tt2))* &
(k0(3) * exp(p1(3)*tt1 + p2(3)*tt2))
!
! ## 4. H2O(aq) <==> H+(aq) + OH-(aq) (xkw)
kh2o = k0(4) * exp(p1(4)*tt1 + p2(4)*tt2)
!
! ## Calculate ZSR position parameter
irh = max(min(int(aw*100+0.5), 100), 1)
!
! ## Aerosol liquid water content
so4_t = so4
lwn = max(so4_t/awas(irh), tiny)
!
! ## Constant values
lwnsq = aw*lwn*lwn
c1 = r*t
k1 = kh2o*lwnsq
k2 = knh3*c1
k3 = khso4*lwn
k4 = k2/kh2o
!
!
! ### STAGE 1: Root tracking ###
! ## Find a subinterval [xa,xb] on the larger interval [I1,I2] where a sign change occurs
rooteval = 0
condition = .true.
do while (rooteval < 2 .or. (condition .and. rooteval < ndiv+1))
rooteval = rooteval + 1
!
! ## Set low limit and high limit
if (rooteval == 1) then
omehi = 2.0_dp*so4 ! high limit: from NH4+ -> NH3(g) + H+(aq)
y1 = 1.0_dp
end if
!
! ## Begin search on subinterval
if (rooteval == 2) then
soln = .false.
if (abs(y2) <= eps) then
earlyexit = .true.
end if
y1 = y2
!
if (earlyexit) then
dx = 0.0_dp
else
dx = (omehi - tiny)/float(ndiv)
end if
!
omehi = max(omehi - dx, tiny)
end if
!
! ## Continue search
if (rooteval > 2) then
if (loccondition < 0.0_dp) then
! ## 1. Root has been found on the subinterval; save x values for ITP search
y1 = y1
omebe = omebe
omehi = omehi
else
! ## 2. No root has been found; continue searching in the next subinterval
y1 = y2
omebe = omehi
omehi = max(omehi - dx, tiny)
end if
end if
!
! ## Solve the system of equations
frst = .true.
calain = .true.
!
if (.not. soln) then
h = omehi
so4_t = max(so4 - (so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/omehi + 1.0_dp)), tiny)
nh4_t = max(nh4/(1.0_dp/(k4*(gama(8)/gama(9))**2.0)/omehi + 1.0_dp), 2.0_dp*so4_t)
hso4 = so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/omehi + 1.0_dp)
gnh3 = max(nh4 - nh4_t, tiny)
oh = k1/omehi
end if
!
! ## Iterate until convergence of activity coefficients
errin = 1.0_dp
k = 0
do while ( k < nsweep-1 .and. errin >= epsact)
k = k + 1
! ## Reset gamou
if ((.not. soln) .and. frst) then
gamou(4) = gama(4)
gamou(7) = gama(7)
gamou(8) = gama(8)
gamou(9) = gama(9)
gamou(13) = gama(13)
end if
! ## Reset gamin
if (.not. soln) then
gamin(4) = gama(4)
gamin(7) = gama(7)
gamin(8) = gama(8)
gamin(9) = gama(9)
gamin(13) = gama(13)
end if
!
call mach_hetp_calcact1b(h, nh4_t, so4_t, hso4, lwn, gama, t, soln, &
frst, calain, calou)
!
if (frst) then
errouloc = 0.0_dp
errouloc = max(errouloc, abs(gamou(7) - gama(7)) / gamou(7))
errouloc = max(errouloc, abs(gamou(8) - gama(8)) / gamou(8))
errouloc = max(errouloc, abs(gamou(9) - gama(9)) / gamou(9))
errouloc = max(errouloc, abs(gamou(4) - gama(4)) / gamou(4))
errouloc = max(errouloc, abs(gamou(13) - gama(13)) / gamou(13))
calou = errouloc .ge. epsact
frst = .false.
end if
!
errin = 0.0_dp
! ## Test for convergence of activity coefficients
errin = max(errin, 0.0_dp)
errin = max(errin, abs((gamin(7) - gama(7)) / gamin(7)))
errin = max(errin, abs((gamin(8) - gama(8)) / gamin(8)))
errin = max(errin, abs((gamin(9) - gama(9)) / gamin(9)))
errin = max(errin, abs((gamin(4) - gama(4)) / gamin(4)))
errin = max(errin, abs((gamin(13)- gama(13)) / gamin(13)))
calain = errin .ge. epsact
!
! ## Solve system of equations, using new activity coefficients
if (.not. soln) then
h = omehi
so4_t = max(so4 - (so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/omehi + 1.0_dp)), tiny)
nh4_t = max(nh4/(1.0_dp/(k4*(gama(8)/gama(9))**2.0)/omehi + 1.0_dp), 2.0_dp*so4_t)
hso4 = so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/omehi + 1.0_dp)
gnh3 = max(nh4 - nh4_t, tiny)
oh = k1/omehi
end if
end do
!
y2 = (nh4_t/(2.0_dp*so4_t + hso4) - 1.0_dp) + h/(2.0_dp*so4_t + hso4)
!
! ## Check for criteria to exit root tracking
loccondition = sign(1.0_dp,y1)*sign(1.0_dp,y2)
!
! ## AFTER iterating through ALL ndiv subdivided intervals
if (rooteval == ndiv + 1) then
! ## (1) No solution; reset x-value to 'tiny' and use to solve system
if (loccondition > 0.0_dp .and. abs(y2) > eps .and. (.not. earlyexit)) then
noroot = .true.
! write(*,*), 'Warning in CALCA2: no solution found; no interval with sign change'
omehi = tiny ! Reset to tiny
omebe = omehi
else if (loccondition > 0.0_dp .and. abs(y2) <= eps .and. (.not. earlyexit)) then
! ## (2) Solution is assumed and ITP is not required
noroot = .true.
soln = .true.
else if (earlyexit) then
! ## (3) Solution is assumed and ITP is not required
noroot = .true.
omehi = tiny ! Reset to tiny
omebe = omehi
end if
end if
!
! ## Test for criteria to exit root tracking
condition = .false.
if (loccondition > 0.0_dp .and. (.not. noroot)) then
condition = .true.
else
soln = .true.
end if
end do
!
! ## The root tracking did not iterate until ndiv
! ## Check if an x-value of tiny was a solution, if so reset x-value to tiny
if (rooteval < ndiv + 1 .and. earlyexit) then
! Solution has been assumed
noroot = .true.
omehi = tiny ! Reset to tiny
omebe = omehi
soln = .true.
end if
!
if ((.not. earlyexit) .and. (.not. noroot)) then
soln = .false.
end if
!
!
! ### STAGE 2: modified bisection search (using ITP algorithm) ###
! ## Initialize static ITP variables
if (.not. noroot) then
yb = y1
ya = y2
!
xa = omehi
xb = omebe
x3 = omebe
!
if (xa == xb) then
noroot = .true.
gx = tiny
else
gx = xb - xa
end if
!
gx2 = (xa+xb)*0.5_dp
u1 = 0.2_dp/gx
nh = log10(abs(gx/(2.0_dp*eps*gx2))) / log10of2
nmax = int(nh) + 2
else
x3 = omehi
soln = .true.
end if
!
! ## Start search
j = 0
condition = .true.
do while (j < maxit .and. condition)
! ## Set dynamic ITP variables
if ((.not. noroot) .and. (.not. soln)) then
gx = xb - xa
xh = 0.5_dp*(xa + xb)
rr = max(gx2*eps*2.0_dp**(real(nmax - j)) - 0.5_dp*gx, 0.0_dp)
delta= u1*(max(gx, 0.0_dp))**2.0_dp
xf = max((yb*xa - ya*xb) / (yb - ya), 0.0_dp)
!
sigma = sign(1.0_dp, xh - xf)
if (delta <= abs(xh - xf)) then
xt = xf + sigma*delta
else
xt = xh
end if
!
if (abs(xt - xh) <= rr) then
x3 = xt
else
x3 = xh - sigma*rr
end if
end if
!
j = j + 1
!
if (.not. soln) then
gmax = 0.1_dp
gmax = max(gmax, gama(7))
gmax = max(gmax, gama(8))
gmax = max(gmax, gama(9))
gmax = max(gmax, gama(4))
gmax = max(gmax, gama(13))
end if
!
! ## Reinitialize activity coefficients if gmax > 100.0_dp
if (gmax > 100.0_dp .and. (.not. soln)) then
gama(7) = 0.1_dp
gama(8) = 0.1_dp
gama(9) = 0.1_dp
gama(4) = 0.1_dp
gama(13) = 0.1_dp
gamin(7) = 1.0e10_dp
gamin(8) = 1.0e10_dp
gamin(9) = 1.0e10_dp
gamin(4) = 1.0e10_dp
gamin(13)= 1.0e10_dp
calou = .true.
frst = .true.
end if
!
! ## Solve system of equations
frst = .true.
calain = .true.
if (.not. soln) then
h = x3
so4_t = max(so4 - (so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/x3 + 1.0_dp)), tiny)
nh4_t = max(nh4/(1.0_dp/(k4*(gama(8)/gama(9))**2.0)/x3 + 1.0_dp), 2.0_dp*so4_t)
hso4 = so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/x3 + 1.0_dp)
gnh3 = max(nh4 - nh4_t, tiny)
oh = k1/x3
end if
!
! ## Iterate until convergence of activity coefficients
k = 0
errin = 1.0_dp
do while ( k < nsweep-1 .and. errin >= epsact)
k = k + 1
!
! ## Reset gamou
if ((.not. soln) .and. frst) then
gamou(4) = gama(4)
gamou(7) = gama(7)
gamou(8) = gama(8)
gamou(9) = gama(9)
gamou(13) = gama(13)
end if
!
! ## Reset gamin
if (.not. soln) then
gamin(4) = gama(4)
gamin(7) = gama(7)
gamin(8) = gama(8)
gamin(9) = gama(9)
gamin(13) = gama(13)
end if
!
call mach_hetp_calcact1b(h, nh4_t, so4_t, hso4, lwn, gama, t, soln, &
frst, calain, calou)
!
if (frst) then
errouloc = 0.0_dp
errouloc = max(errouloc, abs(gamou(7) - gama(7)) / gamou(7))
errouloc = max(errouloc, abs(gamou(8) - gama(8)) / gamou(8))
errouloc = max(errouloc, abs(gamou(9) - gama(9)) / gamou(9))
errouloc = max(errouloc, abs(gamou(4) - gama(4)) / gamou(4))
errouloc = max(errouloc, abs(gamou(13) - gama(13)) / gamou(13))
calou = errouloc .ge. epsact
frst = .false.
end if
!
errin = 0.0_dp
! ## Test for convergence of activity coefficients
errin = max(errin, 0.0_dp)
errin = max(errin, abs((gamin(7) - gama(7)) / gamin(7)))
errin = max(errin, abs((gamin(8) - gama(8)) / gamin(8)))
errin = max(errin, abs((gamin(9) - gama(9)) / gamin(9)))
errin = max(errin, abs((gamin(4) - gama(4)) / gamin(4)))
errin = max(errin, abs((gamin(13)- gama(13)) / gamin(13)))
calain = errin .ge. epsact
!
! ## Solve system of equations, with new activity coefficients
if (.not. soln) then
h = x3
so4_t = max(so4 - (so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/x3 + 1.0_dp)), tiny)
nh4_t = max(nh4/(1.0_dp/(k4*(gama(8)/gama(9))**2.0)/x3 + 1.0_dp), 2.0_dp*so4_t)
hso4 = so4/((k3/gama(7)*(gama(8)/gama(7))**2.0)/x3 + 1.0_dp)
gnh3 = max(nh4 - nh4_t, tiny)
oh = k1/x3
end if
end do
!
y3 = (nh4_t/(2.0_dp*so4_t + hso4) - 1.0_dp) + h/(2.0_dp*so4_t + hso4)
!
condition = .false.
if (noroot) then
! ## If no root on interval then do not perform ITP
xa = x3
xb = x3
else if (y3 > 0.0_dp .and. (.not. soln)) then
xb = x3
yb = y3
else if (y3 < 0.0_dp .and. (.not. soln)) then
xa = x3
ya = y3
else if (.not. soln) then
xa = x3
xb = x3
end if
!
! ## Check for convergence criteria to exit ITP:
if (abs(xb - xa) > abs(xa*eps) .and. (.not. noroot)) then
condition = .true.
soln = .false.
else
soln = .true.
end if
end do
!
!
! ### Save result and return ###
so4_i = so4_t
nh4_i = nh4_t
hso4_i = hso4
h_i = h
nh3g_i = gnh3
lwn_i = lwn
oh_i = oh
!
return
end subroutine mach_hetp_calca2
!############################################################################
! ## HETP Code
! ## Subcase: B4; Sulfate rich, no free acid
!
! ## Copyright 2023, Environment and Climate Change Canada (ECCC)
! ## Written by Stefan Miller
!
! ## Code is based on ISORROPIA II, obtained from the CMAQ air-quality
! ## model (https://github.com/USEPA/CMAQ/tree/main/CCTM/src/aero/aero6)
!############################################################################
subroutine mach_hetp_calcb4(so4_i, nh4_i, nh3g_i, hso4_i, h_i, lwn_i, &
rh, temp, k0, p1, p2, nr)
!
use mach_hetp_mod
implicit none
!
integer, intent (in) :: nr
real(dp), intent (in) :: k0 (nr)
real(dp), intent (in) :: p1 (nr)
real(dp), intent (in) :: p2 (nr)
real(dp), intent(inout) :: so4_i
real(dp), intent(inout) :: nh4_i
real(dp), intent(inout) :: hso4_i
real(dp), intent(inout) :: nh3g_i
real(dp), intent(inout) :: h_i
real(dp), intent(inout) :: lwn_i
real(dp), intent (in) :: rh
real(dp), intent (in) :: temp
!
! ## Local variables:
real(dp) :: so4, nh4, hso4, gnh3, h, lwn, t, aw, tt1, tt2, c1, so4_t, nh4_t
real(dp) :: khso4, knh3, kh2o, bb, cc, dd, hh, v, ak1, errin, tt0
real(dp) :: m4, m9, m13, c2, c3, c4, gama5, gama10
integer :: j, irh
logical :: gg
real(dp), dimension(13) :: gama, gamin
!
! ### Initialize variables ###
so4 = so4_i
nh4 = nh4_i
aw = rh
t = temp
hso4 = 0.0_dp
gnh3 = 0.0_dp
h = 0.0_dp
lwn = 0.0_dp
so4_t = 0.0_dp
nh4_t = nh4
gama = 0.1_dp
gamin = 1.0e10_dp
!
! ### Calculate equilibrium constants and other static variables ###
! ## Set RH to a range between 0.5% and 99.5%: RH = 0.00_dp will
! ## cause division by zero, aborting the code
aw = max(aw, 0.005_dp)
aw = min(aw, 0.995_dp)
!
tt0 = tstd / t
tt1 = tt0 - 1.0_dp
tt2 = 1.0_dp + log(tt0) - tt0
!
! ## 1. HSO4(aq) <==> H+(aq) + SO4=(aq) (xk1)
khso4 = k0(1) * exp(p1(1)*tt1 + p2(1)*tt2)
!
! ## 2. k2 = NH3(g) <==> NH3(aq) (xk21)
! ## 3. k3 = NH3(aq) + H2O(aq) <==> NH4+(aq) + OH-(aq) (xk22)
! ## Net NH3: k2*k3 (xk2)
knh3 = (k0(2) * exp(p1(2)*tt1 + p2(2)*tt2))* &
(k0(3) * exp(p1(3)*tt1 + p2(3)*tt2))
!
! ## 4. H2O(aq) <==> H+(aq) + OH-(aq) (xkw)
kh2o = k0(4) * exp(p1(4)*tt1 + p2(4)*tt2)
!
! ## Calculate ZSR position parameter
irh = max(min(int(aw*100+0.5), 100), 1)
!
! ## Calculate dry composition; used to calculate initial aerosol liquid water
gg = 2.0_dp*so4 - nh4 <= nh4 - so4
!
if (gg) then
! NH4HSO4 >= (NH4)2SO4
m4 = 2.0_dp*nh4 - 3.0_dp*so4 ! clc
m9 = 0.0_dp ! nh4hs4
m13 = 2.0_dp*so4 - nh4 ! nh42s4
else
! NH4HSO4 < (NH4)2SO4
m4 = 0.0_dp ! clc