-
Notifications
You must be signed in to change notification settings - Fork 6
/
aed_gcsolver.F90
4765 lines (3730 loc) · 178 KB
/
aed_gcsolver.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
!###############################################################################
!# #
!# aed_gcsolver.F90 #
!# #
!# Calculate the forward timestep values for geochemical species, including #
!# metals, cations and anions #
!# #
!# ----------------------------------------------------------------------- #
!# #
!# Developed by : #
!# AquaticEcoDynamics (AED) Group #
!# School of Agriculture and Environment #
!# The University of Western Australia #
!# #
!# https://aquatic.science.uwa.edu.au/ #
!# #
!# Copyright 2012 - 2024 - The University of Western Australia #
!# #
!# AED 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. #
!# #
!# AED 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 <https://www.gnu.org/licenses/>. #
!# #
!# ----------------------------------------------------------------------- #
!# #
!# Created March 2012 #
!# #
!###############################################################################
#include "aed.h"
#include "aed_debug.h"
#define DP 8
MODULE aed_gcsolver
!##############################################################################!
USE aed_core ! temporarily
! USE DataTypes
USE aed_gctypes
USE aed_gclib,only : antiLOG,NumOfComps,NumOfSpecies,GetSpeciesName, &
GetCompCompName, GetCompInfo, GetPhaseInfo, GetSpeciesInfo
IMPLICIT NONE
PRIVATE
PUBLIC :: ConfigEquilibriumSolver, &
InitialiseGCProperties, &
UpdateEquilibration, &
reportGeochemConfig, &
GetListOfGeochemDiagnostics, &
returnGCDerivedVector, &
allComponents, nComponents, &
PUREPHASE, &
nFixedDICHMVars, &
simManganRedox, &
simArsenicRedox, &
simSulfurRedox, &
simCarbonRedox, &
simIronRedox, &
FEII, FEIII, MNII, MNIV, ALIII, NiII, &
ASIII, ASV, CDII, ZNII, PBII, SO4, H2S, &
chargeBalCol, CO3, CH4
!------------------------------------------------------------------------------!
! Module level declarations !
!------------------------------------------------------------------------------!
!------------------------------------------------------------------------------!
! CAB - my quick haque additions
!
INTEGER, PARAMETER :: MAX_LABEL_LENGTH = 32
INTEGER, PARAMETER :: idl=10 ! Character length of keywords
REAL, PARAMETER :: WaterFreezingInKelvins = 273.15 ! !##get ref
REAL, PARAMETER :: GasConstant = 8.3143 ! Drever, 1997, pg19
LOGICAL :: simC_DIC ! Simulate Inorganic Carbon in C Cycle
!------------------------------------------------------------------------------!
!-- Component Subset required for this simulation
TYPE (gcUnknowns), DIMENSION(:), ALLOCATABLE, TARGET :: allComponents
TYPE (gcSpecies), DIMENSION(:), ALLOCATABLE, TARGET :: allSpecies
!-- Some Shortcut Pointers
TYPE (gcUnknowns), POINTER :: ionStrength
TYPE (gcUnknowns), POINTER :: activityWater
TYPE (gcUnknowns), POINTER :: hydrogenIon
TYPE (gcUnknowns), POINTER :: massWater
TYPE (gcUnknowns), POINTER :: massHydrogen
!-- Module Constants
INTEGER, PARAMETER :: nCompulsoryUnknowns = 5 ! MU,AH2O,CB,MH,MH2O
!-- Component Types
INTEGER, PARAMETER :: PUREPHASE = 1
INTEGER, PARAMETER :: MOLEBLNCE = 2
INTEGER, PARAMETER :: IONSTNGTH = 3
INTEGER, PARAMETER :: CHARGEBAL = 4
INTEGER, PARAMETER :: ACTIVYH2O = 5
INTEGER, PARAMETER :: MASSOXYGN = 6
INTEGER, PARAMETER :: MASSHYDGN = 7
!-- Species Gamma Calculation Method
INTEGER, PARAMETER :: UNCHGD = 0
INTEGER, PARAMETER :: DAVIES = 1
INTEGER, PARAMETER :: WATEQDH = 2
!-- Input concentration mode
INTEGER, PARAMETER :: MMOLPERL = 0
INTEGER, PARAMETER :: MMOLPERM3 = 2
INTEGER, PARAMETER :: MGPERL = 1
!-- Output Verbosity
INTEGER, PARAMETER :: baseVerb = 0
INTEGER :: verbosity = 0
!-- General
DOUBLETYPE, PARAMETER :: gc_zero = 0.0_GCHP
DOUBLETYPE, PARAMETER :: gc_one = 1.0_GCHP
DOUBLETYPE, PARAMETER :: minLogActivity = -30.0
!-- Module Variables
DOUBLETYPE, DIMENSION(:,:), ALLOCATABLE :: inequalityArray
DOUBLETYPE :: Waq = gc_one
DOUBLETYPE :: molWgtH2O = 0.0180160000
DOUBLETYPE :: cellTemp = 20.00
DOUBLETYPE :: cellSal = 0.00
INTEGER :: residColumn = 0
INTEGER :: chargeBalCol = 0
INTEGER :: nComponents = 0
INTEGER :: nSpecies = 0
INTEGER :: nOPTEqs = 0
INTEGER :: nEQLEqs = 0
INTEGER :: nINQEqs = 0
INTEGER :: nMBEqs = 0
INTEGER :: nPPEqs = 0
INTEGER :: nFixedDICHMVars = 0
LOGICAL :: pHisFixed = .TRUE.
LOGICAL :: peisFixed = .FALSE.
LOGICAL :: SolveWithPP = .FALSE.
LOGICAL :: simManganRedox = .FALSE.
LOGICAL :: simArsenicRedox = .FALSE.
LOGICAL :: simIronRedox = .FALSE.
LOGICAL :: simSulfurRedox = .FALSE.
LOGICAL :: simCarbonRedox = .FALSE.
INTEGER :: FEII, FEIII, MNII, MNVII, ALIII, MNIV, NiII
INTEGER :: ZNII, ASIII, ASV, PBII, CDII, H2S, SO4, CH4, CO3
LOGICAL :: solvePPtoEquilibrium
!-- Derived Variables
INTEGER :: nGCDerivedVars = 0
INTEGER, DIMENSION(:),ALLOCATABLE :: derivedGCList
DOUBLETYPE, DIMENSION(:), ALLOCATABLE :: derivedGCVals
!##############################################################################!
CONTAINS
!##############################################################################!
! ConfigureGeochem: !
! !
! Main setup routine for geochemistry solver. !
!------------------------------------------------------------------------------!
SUBROUTINE ConfigEquilibriumSolver( nUserReqComponents, nUserReqPurePhases, &
listofCompNames, listofPPNames, &
nDissTransportables, nPartTransportables, &
listDissTransVars, listPartTransVars) !
!-- Incoming !
INTEGER, INTENT(IN) :: nUserReqComponents !
INTEGER, INTENT(IN) :: nUserReqPurePhases !
CHARACTER(LEN=64), DIMENSION(:), INTENT(IN) :: listofCompNames !
CHARACTER(LEN=64), DIMENSION(:), INTENT(IN) :: listofPPNames !
!-- Outgoing !
INTEGER, INTENT(INOUT) :: nDissTransportables !
INTEGER, INTENT(INOUT) :: nPartTransportables !
CHARACTER(LEN=64), ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: listDissTransVars !
CHARACTER(LEN=64), ALLOCATABLE, DIMENSION(:), INTENT(OUT) :: listPartTransVars !
!-- Local !
! INTEGER :: nDissTransportables, nPartTransportables !
! CHARACTER(LEN=idl), ALLOCATABLE :: listDissTransVars(:) !
! CHARACTER(LEN=idl), ALLOCATABLE :: listPartTransVars(:) !
CHARACTER(*), PARAMETER :: THIS_PROC = "ConfigEquilibriumSolver" !
TYPE(gcUnknowns) :: dummyComp !
INTEGER :: status !
! INTEGER :: speciesIndex !
INTEGER :: unknownsThatAreAlreadyVars !
INTEGER :: i, cIndex, wqColCounter !
!
!---------------------------------------------------------------------------!
!-- Lets begin the setup
nFixedDICHMVars = 0 !nDissTransportables
!-------
!-- Calculate the total number of geochemical components
!-- This is not the size of DICHM - it is the number of
!-- Components to be solved for in this module,
!-- i.e. nComponents
!-------
nComponents = nUserReqComponents &
+ nUserReqPurePhases &
+ nCompulsoryUnknowns
!-- Allocate space component objects
ALLOCATE(allComponents(nComponents),STAT=status)
CheckAllocStatus(status,THIS_PROC,"allComponents")
!-------
!-- In the WQ array, DICHM MUST have the *essential* inorganic
!-- nutrients: DO, PO4, NH4, NO3 and SiO2
!-- If these are also to be included in the speciation calcs,
!-- then we must make sure we dont count them twice
!-------
unknownsThatAreAlreadyVars = 0
!DO i = 1,nUserReqComponents
! IF(TRIM(listofCompNames(i)) == "DO" .OR. &
! TRIM(listofCompNames(i)) == "PO4" .OR. &
! TRIM(listofCompNames(i)) == "NH4" .OR. &
! TRIM(listofCompNames(i)) == "NO3" .OR. &
! TRIM(listofCompNames(i)) == "SiO2") THEN
!
! unknownsThatAreAlreadyVars = unknownsThatAreAlreadyVars + 1
! END IF
!END DO
!-------
!-- Check with the database to see the components requested
!-- by the user (in listofCompNames read in from *.con) are
!-- valid and present in the chem database (*.chm). If they
!-- are present, then populate the local Component space.
!-------
wqColCounter = 0! nDissTransportables
DO i = 1,nUserReqComponents
allComponents(i)%EltName = TRIM(listofCompNames(i))
CALL GetCompInfo(allComponents(i)) !!##GEOCH_MOD
allComponents(i)%wqIndex = GetDissChemIndex(allComponents(i)%EltName, &
wqColCounter)
allComponents(i)%CompIndex = i
allComponents(i)%CompType = MOLEBLNCE
! DIC is a special case: If this has been selected
! then we must perform some other
IF(TRIM(allComponents(i)%EltName) == "DIC") THEN
!DICColNum = allComponents(i)%wqIndex
simC_DIC = .TRUE.
END IF
END DO
!-------
!-- The geochem routine requires some compulsory unknowns
!-- to be included - these are added after the user requested
!-- unknowns.
!-------
!-- Compulsory Unknown 1: Ionic Strength
allComponents(nUserReqComponents+1)%CompType = IONSTNGTH
allComponents(nUserReqComponents+1)%CompName = "IONSTNGTH"
allComponents(nUserReqComponents+1)%EltName = "IONSTNGTH"
allComponents(nUserReqComponents+1)%CompIndex = nUserReqComponents+1
allComponents(nUserReqComponents+1)%wqIndex = 0 ! NOT TRANSPORTABLE
ionStrength => allComponents(nUserReqComponents+1)
!-- Compulsory Unknown 2: Activity of Water
allComponents(nUserReqComponents+2)%CompType = ACTIVYH2O
allComponents(nUserReqComponents+2)%CompName = "H2O"
allComponents(nUserReqComponents+2)%EltName = "ACTIVYH2O"
allComponents(nUserReqComponents+2)%CompIndex = nUserReqComponents+2
allComponents(nUserReqComponents+2)%wqIndex = 0 ! NOT TRANSPORTABLE
activityWater => allComponents(nUserReqComponents+2)
!-- Compulsory Unknown 3: Charge Balance (lnaH+)
allComponents(nUserReqComponents+3)%CompType = CHARGEBAL
allComponents(nUserReqComponents+3)%CompName = "H+"
allComponents(nUserReqComponents+3)%EltName = "pH"
allComponents(nUserReqComponents+3)%CompIndex = nUserReqComponents+3
allComponents(nUserReqComponents+3)%wqIndex = &
GetDissChemIndex(allComponents(nUserReqComponents+3)%EltName, &
wqColCounter)
hydrogenIon => allComponents(nUserReqComponents+3)
!-- Compulsory Unknown 4: Mass Hydrogen (lnae-)
allComponents(nUserReqComponents+4)%CompType = MASSHYDGN
allComponents(nUserReqComponents+4)%CompName = "e-"
allComponents(nUserReqComponents+4)%EltName = "pe"
allComponents(nUserReqComponents+4)%CompIndex = nUserReqComponents+4
allComponents(nUserReqComponents+4)%wqIndex = &
GetDissChemIndex(allComponents(nUserReqComponents+4)%EltName, &
wqColCounter)
massHydrogen => allComponents(nUserReqComponents+4)
!-- Compulsory Unknown 5: Mass Water
allComponents(nUserReqComponents+5)%CompType = MASSOXYGN
allComponents(nUserReqComponents+5)%CompName = "MASSOXYGN"
allComponents(nUserReqComponents+5)%EltName = "MASSOXYGN"
allComponents(nUserReqComponents+5)%CompIndex = nUserReqComponents+5
allComponents(nUserReqComponents+5)%wqIndex = 0 ! NOT TRANSPORTABLE
massWater => allComponents(nUserReqComponents+5)
!-- Special transportable so we can remember the conc of unbalaned charge
chargeBalCol = GetDissChemIndex("ubalchg ",wqColCounter)
!-------
!-- OK, now we can calculate the size of DICHM - the dissolved
!-- transported components. This is not the same as
!-- nComponents! Now that we know what is being simulated
!-- we can return to WQ_config() the list of names to be
!-- included in WQ3D%a3d
!-------
nDissTransportables = wqColCounter
!-- Allocate and populate list of DICHM vars
ALLOCATE(listDissTransVars(nDissTransportables),STAT=status)
listDissTransVars = ""
listDissTransVars = GetDissChemNames(nDissTransportables, &
allComponents(1:nUserReqComponents+nCompulsoryUnknowns))
DO i = 1,nDissTransportables
!PRINT *,' Diss Names:',i,listDissTransVars(i)
END DO
!-------
!-- Now we know what the components to be included in the simulation
!-- are, we can sort through the species database and find the
!-- ones that should be simulated. The simulated subset is
!-- placed in allSpecies(:), and all relevant pointers are setup
!-------
CALL createSpeciesSubSetforSim( &
allComponents(1:nUserReqComponents+nCompulsoryUnknowns))
!-------
!-- The pure phase unknowns must now be taken care of
!-- and PICHM setup (similar to above). There are currently
!-- no compulsory PICHM vars, so this is easy.
!-------
ALLOCATE(dummyComp%PPData,STAT=status)
CheckAllocStatus(status,THIS_PROC,"dummyComp%PPData")
ALLOCATE(dummyComp%PPData%stoich(NumOfComps()),STAT=status)
CheckAllocStatus(status,THIS_PROC,"dummyComp%PPData%Stoich")
dummyComp%PPData%stoich = gc_zero
wqColCounter = 0 !nPartTransportables
DO i = 1,nUserReqPurePhases
cIndex = nUserReqComponents + nCompulsoryUnknowns + i
dummyComp%EltName = TRIM(listOfPPNames(i))
CALL GetPhaseInfo(dummyComp)
dummyComp%CompIndex = cIndex
allComponents(cIndex) = setupPhaseStoich(dummyComp, &
allComponents(1:nUserReqComponents+nCompulsoryUnknowns))
allComponents(cIndex)%wqIndex = GetPartChemIndex(wqColCounter)
allComponents(cIndex)%CompType = PUREPHASE
END DO
!-------
!-- Allocate and populate list of PICHM vars
!-------
nPartTransportables = wqColCounter
ALLOCATE(listPartTransVars(nPartTransportables),STAT=status)
listPartTransVars = ""
listPartTransVars = GetPPChemNames(nPartTransportables, &
allComponents( (nUserReqComponents+nCompulsoryUnknowns+1) : &
nComponents) )
!DO i = 1,nPartTransportables
! PRINT *,' Part Names:',i,listPartTransVars(i)
!END DO
!solvePPtoEquilibrium = .TRUE.
solvePPtoEquilibrium = .FALSE.
!-------
!-- Update species stoich array with relevant pure phase
!-- data.
!-------
!-------
!-- Reset special CompName's for internal use
!-------
allComponents(nUserReqComponents+2)%CompName = "ACTIVYH2O"
allComponents(nUserReqComponents+3)%CompName = "CHARGEBAL"
allComponents(nUserReqComponents+4)%CompName = "MASSHYDGN"
!-------
!-- Now define the dimensions of the reaction set
!-------
nMBEqs = nUserReqComponents
nPPEqs = nUserReqPurePhases
Waq = gc_one
molWgtH2O = 0.0180160000
IF(verbosity > 0) THEN
print *,' Aqueous Species List: ----------'
DO i = 1,nSpecies
PRINT *,' Species:',i, TRIM(allSpecies(i)%Name),': ', &
INT(allSpecies(i)%Stoich)
END DO
print *,'--------------------------------'
IF(verbosity > 2) THEN
CALL outputSpeciesData(allSpecies)
END IF
END IF
!-------
!-- Now find out if we have any redox pairs
!-------
CALL setupWCRedoxConfiguration( allComponents(1:nUserReqComponents) )
END SUBROUTINE ConfigEquilibriumSolver !
!------------------------------------------------------------------------------!
!------------------------------------------------------------------------------!
! InitialiseGCProperties: !
! !
! Based on user input initial conditions for geochemical variables, this !
! routine will solve for equilibrium conditions. It assumes pH is fixed !
! (ie. given based on measurement), and solves for the charge imbalance, which !
! is stored in the transportable variable CHGBAL. !
! No mineral phases are included. !
!------------------------------------------------------------------------------!
SUBROUTINE InitialiseGCProperties(dissConcs, partConcs, concMode, inTemp) !
!-- Incoming !
! made AED_REAL to enable build in single precision - CAB
AED_REAL,DIMENSION(:) :: partConcs, dissConcs !
INTEGER, INTENT(IN) :: concMode ! Conc units !
REAL,INTENT(IN), OPTIONAL :: inTemp ! Temperature !
!-- Local !
CHARACTER(*), PARAMETER :: THIS_PROC = "InitialiseGCProperties"
INTEGER, DIMENSION(:), ALLOCATABLE :: componentList !
! DOUBLETYPE, DIMENSION(:) :: partConcs, dissConcs !
DOUBLETYPE :: fff, weight !
LOGICAL :: REPEAT, FAIL,failed !
INTEGER :: cellIndex, &
componentIndex, &
nComps2Process, &
iter !
!
!---------------------------------------------------------------------------!
WRITE(*,"(11X,'Initialise geochemical properties...')")
verbosity = 0
IF(PRESENT(inTemp)) THEN
cellTemp = REAL(inTemp,GCHP)
ELSE
cellTemp = 20.0
END IF
!---------------
!-- This initial round requires that we hold pH const and
!-- ignore pure phase so that we can deterimine the charge
!-- balance and remember it for future calculations
pHisFixed = .TRUE.
peisFixed = .TRUE.
SolveWithPP = .FALSE.
nComps2Process = nMBEqs + 2 ! Mole-Balance Eqs + ionicStrength + aH2O
ALLOCATE(componentList(nComps2Process))
! Need to set component Eq Indicies
CALL seteqIndiciesforSpeciation(allComponents, nComps2Process,componentList)
nOPTEqs = 1 ! Number of Rows in A matrix
nEQLEqs = nComps2Process - nOPTEqs ! Number of Rows in C matrix
nINQEqs = 0 ! Number of Rows in E matrix
!-- 1. Set start/total moles of components to be the WQ3F value and
!-- change units as necessary
CALL setComponentTotalConc(dissConcs,partConcs,allComponents, concMode)
ionStrength%Total = initialIonicStrength(allComponents)
CALL updateLogK(allSpecies,allComponents,cellTemp)
CALL updateGammas(allSpecies,ionStrength%Total)
REPEAT = .TRUE.
FAIL = .FALSE.
iter = 0
DO WHILE (REPEAT)
iter = iter + 1
IF (verbosity > 10) THEN
PRINT *,'Beginning set iteration: ',iter
END IF
IF (iter == 10) THEN
PRINT *, 'Did not converge in set iteration',iter
fail = .TRUE.
END IF
CALL updateSpeciesMoles(allSpecies, allComponents)
CALL moleBalanceSums(allSpecies, allComponents)
REPEAT = .FALSE.
DO componentIndex = 1, nComps2Process
IF(allComponents(componentList(componentIndex))%CompType == MOLEBLNCE) THEN
IF (ABS(allComponents(componentList(componentIndex))%Total) < 1e-30) THEN
allComponents(componentList(componentIndex))%Total = gc_zero
END IF
fff = ABS(allComponents(componentList(componentIndex))%Value)
IF (fff == gc_zero .AND. allComponents(componentList(componentIndex))%Total == gc_zero) THEN
allComponents(componentList(componentIndex))%Master%logActivity = minLogActivity
CYCLE
ELSE IF (fff == gc_zero) THEN
REPEAT = .TRUE.
allComponents(componentList(componentIndex))%Master%logActivity = &
allComponents(componentList(componentIndex))%Master%logActivity +5.0
ELSE IF (fail .AND. fff < 1.5 * ABS(allComponents(componentList(componentIndex))%Total)) THEN
CYCLE
ELSE IF (fff > 1.5 * ABS(allComponents(componentList(componentIndex))%Total) .OR. &
fff < 1e-5 * ABS(allComponents(componentList(componentIndex))%Total) ) THEN
IF (fff < (1e-5 * ABS(allComponents(componentList(componentIndex))%Total))) THEN
weight = 0.3
ELSE
weight = 1.0
END IF
IF (allComponents(componentList(componentIndex))%Total <= gc_zero) THEN
allComponents(componentList(componentIndex))%Master%logActivity = minLogActivity
ELSE
REPEAT = .TRUE.
allComponents(componentList(componentIndex))%Master%logActivity = &
allComponents(componentList(componentIndex))%Master%logActivity + &
weight * LOG10(ABS(allComponents(componentList(componentIndex))%Total / &
allComponents(componentList(componentIndex))%Value))
END IF
END IF
END IF
END DO
END DO
ionStrength%Total = updateIonicStrength(allSpecies)
CALL updateGammas(allSpecies,ionStrength%Total)
!-- 2. Equilibrate water column cells
CALL solveEquilibriumReactions( allSpecies, allComponents, &
componentList,nComps2Process, failed )
IF(verbosity > 2) THEN
DO componentIndex = 1,nComps2Process
print *,'After Aq Stage',allcomponents(componentIndex)%CompName, &
allcomponents(componentIndex)%Total,allcomponents(componentIndex)%Value
END DO
END IF
DEALLOCATE(componentList)
!---------------
!-- 3. Update state variable arrays with new component estimates
! and revert to original units
CALL updateWQArray(dissConcs,partConcs,allComponents, concMode)
!WQ(cellIndex,DICHM(:)) = REAL(dissConcs, r_wq)
!WQ(cellIndex,PICHM(:)) = REAL(partConcs, r_wq)
!-- Charge Balance Condition
dissConcs(chargeBalCol) = hydrogenIon%Value
!-- 4. Now update derived variable arrays
IF(concMode /= MMOLPERL) THEN
!-- Not CANDI calling, so update away
DO iter = 1, nGCDerivedVars
IF(derivedGCList(iter) > 0) THEN
derivedGCVals(iter) = allSpecies(derivedGCList(iter))%Moles
ELSE IF (derivedGCList(iter) == -999) THEN
derivedGCVals(iter) = calcpCO2(allSpecies, cellTemp, cellSal)
END IF
END DO
END IF
verbosity = baseVerb
END SUBROUTINE InitialiseGCProperties
!------------------------------------------------------------------------------!
!------------------------------------------------------------------------------!
! UpdateEquilibration: !
! !
! Main run-time routine to conduct equilibration on supplied cells !
! Incoming cells may be from water column or sediment, this rotuine is generic !
! !
!------------------------------------------------------------------------------!
SUBROUTINE UpdateEquilibration(dissConcs, partConcs, concMode, inTemp,inSalt, stoEq, IAP, upDerv)
! DOUBLETYPE, DIMENSION(:) :: partConcs, dissConcs !
AED_REAL, DIMENSION(:) :: partConcs, dissConcs !
INTEGER, INTENT(IN) :: concMode ! g/m3 or mol/L !
REAL, INTENT(IN), OPTIONAL :: inTemp ! Temperature !
REAL, INTENT(IN), OPTIONAL :: inSalt ! Salinity !
LOGICAL, INTENT(IN), OPTIONAL :: stoEq ! solvePPtoEquil !
REAL, DIMENSION(:), INTENT(INOUT), OPTIONAL :: IAP !
LOGICAL, INTENT(IN), OPTIONAL :: upDerv ! Update derived !
!-- Local !
CHARACTER(*), PARAMETER :: THIS_PROC = "Equilibrate" !
INTEGER, DIMENSION(:), ALLOCATABLE :: componentList !
DOUBLETYPE :: fff, weight, TotalH, TotalO !
LOGICAL :: REPEAT, FAIL, failed ,updateDerivedVars !
INTEGER :: cellIndex, &
componentIndex, &
nComps2Process, &
sIndex, &
iter, &
nFailedCells !
!
!---------------------------------------------------------------------------!
! WRITE(*,"(6X,'Chemistry equilibration')")
IF(PRESENT(stoEq)) THEN
solvePPtoEquilibrium = stoEq
END IF
IF(PRESENT(upDerv)) THEN
updateDerivedVars = upDerv
ELSE
updateDerivedVars = .FALSE.
END IF
nFailedCells = 0
!verbosity = baseVerb
IF(PRESENT(inTemp)) THEN
cellTemp = REAL(inTemp,GCHP)
ELSE
cellTemp = 20.0
END IF
IF(PRESENT(inSalt)) THEN
cellSal = REAL(inSalt,GCHP)
ELSE
cellSal = 0.0
END IF
!---------------
!-- We have already held pH const and calc'd the charge bal
!-- in initialiseGeochem. So now we must estimate the pH whilst
!-- keeping the charge balance const. Lets do this without
!-- pure phases first.
pHisFixed = .FALSE.
peisFixed = .TRUE.
SolveWithPP = .FALSE.
!-- Before pure phases come in, we just include the mole balances
!-- ionic strength, activity of H2O and pH
nComps2Process = nMBEqs + 3
ALLOCATE(componentList(nComps2Process))
! Need to set component Eq Indicies
CALL seteqIndiciesforSpeciation(allComponents,nComps2Process,componentList)
nOPTEqs = 1 ! Number of Rows in A matrix
nEQLEqs = nComps2Process - nOPTEqs ! Number of Rows in C matrix
nINQEqs = 0 ! Number of Rows in E matrix
!-- 1. Set start/total moles of components to be the WQ3F value and
!-- change units as necessary
CALL setComponentTotalConc(dissConcs,partConcs,allComponents,concMode)
ionStrength%Total = initialIonicStrength(allComponents)
hydrogenIon%Total = dissConcs(chargeBalCol)
CALL updateLogK(allSpecies,allComponents,cellTemp)
CALL updateGammas(allSpecies,ionStrength%Total)
REPEAT = .TRUE.
FAIL = .FALSE.
iter = 0
DO WHILE (REPEAT)
iter = iter + 1
IF (verbosity > 10) THEN
print *,'Beginning set iteration: ',iter
END IF
IF (iter == 10) THEN
print *, 'Did not converge in set iteration',iter
fail = .TRUE.
END IF
CALL updateSpeciesMoles(allSpecies, allComponents)
CALL moleBalanceSums(allSpecies, allComponents)
REPEAT = .FALSE.
DO componentIndex = 1, nComps2Process
IF(allComponents(componentList(componentIndex))%CompType==MOLEBLNCE) THEN
IF(ABS(allComponents(componentList(componentIndex))%Total) <1e-30) THEN
allComponents(componentList(componentIndex))%Total = gc_zero
END IF
fff = ABS(allComponents(componentList(componentIndex))%Value)
IF (fff == gc_zero .AND. &
allComponents(componentList(componentIndex))%Total==gc_zero) THEN
allComponents(componentList(componentIndex))%Master%logActivity &
= minLogActivity
CYCLE
ELSE IF (fff == gc_zero) THEN
REPEAT = .TRUE.
allComponents(componentList(componentIndex))%Master%logActivity = &
allComponents(componentList(componentIndex))%Master%logActivity+5.0
ELSE IF (fail .AND. fff < 1.5 * ABS(allComponents(componentList(componentIndex))%Total)) THEN
CYCLE
ELSE IF (fff > 1.5 * ABS(allComponents(componentList(componentIndex))%Total) .OR. &
fff < 1e-5 * ABS(allComponents(componentList(componentIndex))%Total) ) THEN
IF (fff < (1e-5 * ABS(allComponents(componentList(componentIndex))%Total))) THEN
weight = 0.3
ELSE
weight = 1.0
END IF
IF (allComponents(componentList(componentIndex))%Total <= gc_zero) THEN
allComponents(componentList(componentIndex))%Master%logActivity = minLogActivity
ELSE
REPEAT = .TRUE.
allComponents(componentList(componentIndex))%Master%logActivity = &
allComponents(componentList(componentIndex))%Master%logActivity + &
weight * LOG10(ABS(allComponents(componentList(componentIndex))%Total / &
allComponents(componentList(componentIndex))%Value))
END IF
END IF
END IF
END DO
END DO
ionStrength%Total = updateIonicStrength(allSpecies)
CALL updateGammas(allSpecies,ionStrength%Total)
!-- 2. Equilibrate water column cells
CALL solveEquilibriumReactions(allSpecies,allComponents,componentList, &
nComps2Process,failed)
IF(verbosity > 2) THEN
DO componentIndex = 1,nComps2Process
print *,' After Aq Stage',allcomponents(componentIndex)%CompName, &
allcomponents(componentIndex)%Total,allcomponents(componentIndex)%Value
END DO
END IF
DEALLOCATE(componentList)
IF(nPPEqs > 0 .AND. solvePPtoEquilibrium) THEN
IF(verbosity > 1) THEN
PRINT *,'Starting Batch Rxn:'
END IF
!---------------
! -- Add pure phases and perform full solution
! --
!-- Update switches
pHisFixed = .FALSE.
peisFixed = .FALSE.
SolveWithPP = .TRUE.
!-- Count unknown equations for inequality solver
nOPTEqs = nPPEqs ! Number of Rows in A matrix
nEQLEqs = nMBEqs + nCompulsoryUnknowns ! Number of Rows in C matrix
nINQEqs = nPPEqs ! Number of Rows in E matrix
! n = PP + MB's + MU + aH2O + CB + MH + MH2O
nComps2Process = nPPEqs + nMBEqs + nCompulsoryUnknowns
!-- Update component Eqn Indicies and populate componentList
ALLOCATE(componentList(nComps2Process))
CALL seteqIndiciesforBatchRxn(allComponents,nComps2Process,componentList)
activityWater%Total = (antiLOG(activityWater%master%logActivity,10.0_GCHP) - 1.0)
massWater%Total = massWater%Value
hydrogenIon%Total = hydrogenIon%Value
molWgtH2O = 1.0/massWater%Total
ionStrength%Total = updateIonicStrength(allSpecies)
TotalH = gc_zero
TotalO = gc_zero
DO sIndex = 1,nSpecies
TotalH = TotalH+ allSpecies(sIndex)%Hstoich *allSpecies(sIndex)%Moles
TotalO = TotalO+ allSpecies(sIndex)%H2Ostoich*allSpecies(sIndex)%Moles
END DO
TotalH = TotalH + 2*massWater%Total
massHydrogen%Total = TotalH-2*TotalO
!-- 2. Equilibrate water column cells
CALL solveEquilibriumReactions(allSpecies,allComponents,componentList, &
nComps2Process,failed)
DEALLOCATE(componentList)
END IF
!---------------
!-- 3. Update state variable arrays with new component estimates
! and revert to original units
IF(.NOT. failed) THEN
CALL updateWQArray(dissConcs,partConcs,allComponents,concMode)
WHERE(partConcs<gc_zero)partConcs=gc_zero
! WQ3F(cellIndex,DICHM(:)) = REAL(dissConcs, r_wq)
! WQ3F(cellIndex,PICHM(:)) = REAL(partConcs, r_wq)
!-- 4. Now update derived variable arrays
IF(updateDerivedVars) THEN
DO iter = 1, nGCDerivedVars
IF(derivedGCList(iter) > 0) THEN
! diagnostic is a species
derivedGCVals(iter) = allSpecies(derivedGCList(iter))%Moles
ELSE IF (derivedGCList(iter) <= -1 .AND. derivedGCList(iter) >= -5 ) THEN
! diagnostic is saturation index
derivedGCVals(iter) = calcSIforPP(-derivedGCList(iter))
ELSE IF (derivedGCList(iter) == -998) THEN
! diagnostic is convergence flag
derivedGCVals(iter) = gc_zero
ELSE IF (derivedGCList(iter) == -999) THEN
! diagnostic is pCO2
derivedGCVals(iter) = calcpCO2(allSpecies, cellTemp, cellSal)
END IF
END DO
END IF
ELSE
! Non convergances
nFailedCells = nFailedCells + 1
IF(updateDerivedVars) THEN
DO iter = 1, nGCDerivedVars
IF (derivedGCList(iter) == -998) THEN
! diagnostic is convergence flag
derivedGCVals(iter) = 1.0
END IF
END DO
END IF
END IF
!-- 5. If pure phases are simulated an optional array
! to return the IAP to mediate the kinetic reactions is possible
IF(nPPEqs > 0 .AND. PRESENT(IAP)) THEN
DO iter = 1,nPPEqs
IAP(iter) = CalcIAPforPP( nComponents - nPPEqs + iter )
! Note: this assumes PP's are the last in the component array
END DO
END IF
! IF(nFailedCells > 0) THEN
! PRINT *,' >---'
! PRINT *,' Number of cells that failed converging: ',nFailedCells,' (of ',SIZE(WQ3F,1),')'
! PRINT *,' Cell molalities are being set to the previous time step.'
! PRINT *,' ---<'
! nFailedCells = 0
! END IF
END SUBROUTINE UpdateEquilibration !
!------------------------------------------------------------------------------!
!------------------------------------------------------------------------------!
! SolveEquilibriumReactions !
! !
! Numerical solution to reaction set defined in the above routines. Solution !
! is based on a iterative Newton-Rhapson scheme and the Simplex matrix solver !
! !
!------------------------------------------------------------------------------!
SUBROUTINE solveEquilibriumReactions(species, components, &
cList, nComps2Process, failed) !
!-- Incoming !
TYPE (gcSpecies), DIMENSION(:) :: species !
TYPE (gcUnknowns), DIMENSION(:) :: components !
INTEGER, INTENT(IN) :: nComps2Process !
INTEGER, DIMENSION(:), INTENT(IN) :: cList !
LOGICAL, INTENT(OUT) :: failed !
!-- Local !
CHARACTER(*), PARAMETER :: THIS_PROC = "solveEquilibriumReactions"!
INTEGER :: Status ! memory allocation status !
INTEGER :: componentIndex, miscIndex !
INTEGER :: nItern, ineqCntr !
INTEGER :: inKODE, outKODE, KODE !
INTEGER :: inITER, outITER, ITER !
INTEGER :: ITMAX = 150 !
LOGICAL :: converged !
DOUBLETYPE :: ERROR !
DOUBLETYPE :: max, min !
DOUBLETYPE, PARAMETER :: convTolerance = 1e-12 ! Convg toler !
DOUBLETYPE, PARAMETER :: min_value = 1e-10 ! For Scaling !
DOUBLETYPE, PARAMETER :: TOLER = 1e-14 ! For Simplex !
DOUBLETYPE, DIMENSION(nOPTEqs+nEQLEqs+nINQEqs) :: RES !
DOUBLETYPE, DIMENSION(nComps2Process+2) :: deltaConc !
DOUBLETYPE, DIMENSION(nComps2Process) :: normal !
!---------------------------------------------------------------------------!
!-- Initial items
IF(nPPEqs > 1) THEN
ITMAX = 500
ELSE
ITMAX = 150
END IF
!-- Allocate space for the inequality solver array
!-- (This includes jacobian sums + residuals + inequality constraints)
ALLOCATE(inequalityArray(nOPTEqs+nEQLeqs+nINQEqs+2,nComps2Process+2), &
STAT=Status)
CheckAllocStatus(Status,THIS_PROC,"inequalityArray")
inequalityArray = gc_zero
residColumn = nComps2Process+1
nItern = 0
converged = .FALSE.
inKODE = 1
outKODE = 0
KODE = 0
inITER = 80