forked from kanzakiy/caco3
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcaco3.py
More file actions
executable file
·2725 lines (2682 loc) · 129 KB
/
Copy pathcaco3.py
File metadata and controls
executable file
·2725 lines (2682 loc) · 129 KB
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
# -*- coding: utf-8 -*-
"""
signal tracking diagenesis model
"""
import numpy as np
import os
from scipy.sparse import lil_matrix
from scipy.sparse.linalg import spsolve
#import scipy.linalg as la
import scipy.linalg.lapack as lapack
if os.path.exists('./mocsy.dll'):import mocsy
np.set_printoptions(formatter={'float': '{:.2e}'.format})
def calceq1(tmp,sal,dep):
tmp_k=np.float64(tmp+273.15)
pres=np.float64(dep*100.)
coeff=np.zeros(6,dtype=np.float64)
coeff[0]=13.4191e0
coeff[1]=0.0331e0
coeff[2]=-5.33e-5
coeff[3]=-530.1228e0
coeff[4]=-6.103e0
coeff[5]=-2.06950e0
calceq1=-126.34048e0+6320.813e0/tmp_k+19.568224*np.log(tmp_k)
calceq1+=coeff[0]*sal**0.5+coeff[1]*sal+coeff[2]*sal**2. \
+(coeff[3]*sal**0.5+coeff[4]*sal)/tmp_k \
+coeff[5]*sal**0.5*np.log(tmp_k)
calceq1=10.**(-calceq1)
# calceq1*=np.exp((-(-15.82e0-0.0219e0*tmp)*pres
# +0.5e0*(1.13e-3-0.1475e-3*tmp)*pres*pres)/83.131e0/tmp_k)
calceq1*=np.exp((-(-25.50e0+0.1271e0*tmp)*pres
+0.5e0*(-3.08e-3+0.0877e-3*tmp)*pres*pres)/83.131e0/tmp_k)
return calceq1
def calceq2(tmp,sal,dep):
tmp_k=np.float64(tmp+273.15)
pres=np.float64(dep*100.)
coeff=np.zeros(6,dtype=np.float64)
coeff[0]=21.0894e0
coeff[1]=0.1248e0
coeff[2]=-0.0003687e0
coeff[3]=-772.483e0
coeff[4]=-20.051e0
coeff[5]=-3.32254e0
calceq2=-90.18333e0+5143.692e0/tmp_k+14.613358*np.log(tmp_k)
calceq2+=coeff[0]*sal**0.5+coeff[1]*sal+coeff[2]*sal**2. \
+(coeff[3]*sal**0.5+coeff[4]*sal)/tmp_k \
+coeff[5]*sal**0.5*np.log(tmp_k)
calceq2=10.**(-calceq2)
# calceq2*=np.exp((-(-25.50e0+0.1271e0*tmp)*pres
# +0.5e0*(-3.08e-3+0.0877e-3*tmp)*pres*pres)/83.131e0/tmp_k)
calceq2*=np.exp((-(-15.82e0-0.0219e0*tmp)*pres
+0.5e0*(1.13e-3-0.1475e-3*tmp)*pres*pres)/83.131e0/tmp_k)
return calceq2
def calceqcc(tmp,sal,dep):
tmp_k=np.float64(tmp+273.15e0)
pres = np.float64(dep*100e0)
calceqcc = -171.9065e0 - 0.077993e0*tmp_k + 2839.319e0/tmp_k \
+71.595e0*np.log10(tmp_k) \
+(-0.77712e0+0.0028426e0*tmp_k+178.34e0/tmp_k)*sal**0.5e0 \
-0.07711e0*sal+0.0041249e0*sal**1.5e0
calceqcc = 10e0**calceqcc
calceqcc*=np.exp((-(-48.76e0+0.5304e0*tmp)*pres
+0.5e0*(-11.76e-3+0.3692e-3*tmp)*pres*pres)/83.131e0/tmp_k)
return calceqcc
def calceqw(tmp,sal,dep):
tmp_k=np.float64(tmp+273.15e0)
pres = np.float64(dep*100e0)
calceqw= 148.96502e0 -13847.26e0/tmp_k -23.6521e0*np.log(tmp_k) \
+ (118.67e0/tmp_k -5.977e0 + 1.0495e0*np.log(tmp_k))*sal**0.5e0 - 0.01615e0*sal
calceqw=np.exp(calceqw)
calceqw*=np.exp((-(-25.60e0+0.2324e0*tmp-3.6246e-3*tmp*tmp)*pres+0.5e0*(-5.13e-3+0.0794e-3*tmp)*pres*pres)/83.131e0/tmp_k)
return calceqw
def chk_caco3_therm():
temp = 2e0
sal = 35e0
n = 100
for i in range(n):
dep=6e0*(i+1)/np.float64(n)
keqcc=calceqcc(temp,sal,dep)
print dep,keqcc
return
def chk_caco3_therm_sbrtns():
nz = 100
temp = 2e0 # temperature in Celsius
sal = 35e0 # salinity wt o/oo
dep = 4.0e0 # depth in km
dic = np.ones(nz,dtype=np.float64)*2211e0*1e-6/1e3 # 2211 uM converted to mol/cm3; all the same value at different grids
alk = np.ones(nz,dtype=np.float64)*2285e0*1e-6/1e3
# calling subroutine to calculate all aqueous co2 species and pH
co2,hco3,co3,ph,info = calcspecies(temp,sal,dep,dic,alk,nz)
if info!=0: # if ph cannot be calculated in the subroutine, info=1 is returned
print 'error in calcspecies'
# calling subroutine to calculate derivatives of co3 conc. wrt dic and alk (defined as dco3_ddic and dco3_dalk, respectively)
dco3_ddic,dco3_dalk,info = calcdevs(temp,sal,dep,dic,alk,nz)
if info!=0: # if ph cannot be calculated in the subroutine, info=1 is returned
print 'error in calcdevs'
# printing results on screen; if you want check with MATLAB version by copy and paste
for iz in range(nz):
print dic[iz],alk[iz],co2[iz],hco3[iz],co3[iz],ph[iz],dco3_ddic[iz],dco3_dalk[iz]
# note that the above concentrations (alk,dic,co2,hco3,co3) are all in mol/cm3
# ph is actually H+ concentration in mol/L
# dco3_dalk and dco3_ddic should be dimensionless
return
def calcspecies(tmp,sal,dep,dic,alk,nz):
info = 0
k1=calceq1(tmp,sal,dep)
k2=calceq2(tmp,sal,dep)
a=np.zeros(nz,dtype=np.float64)
b=np.zeros(nz,dtype=np.float64)
c=np.zeros(nz,dtype=np.float64)
pro=np.zeros(nz,dtype=np.float64)
co2=np.zeros(nz,dtype=np.float64)
hco3=np.zeros(nz,dtype=np.float64)
co3=np.zeros(nz,dtype=np.float64)
a[:]=1e0
b[:]=(1.-dic[:]/alk[:])*k1
c[:]=(1.-2.*dic[:]/alk[:])*k1*k2
pro[:]=(-b[:]+(b[:]*b[:]-4.*a*c[:])**0.5)*0.5
if any(pro<0.):
print '... unable to calculate ph'
# print pro
# print dic
# print alk
info = 1
## for iz in range(nz):
## p=np.poly1d([a[iz],b[iz],c[iz]])
## for ip in range(p.roots[np.isreal(p.roots)].shape[0]):
## if p.roots[np.isreal(p.roots)][ip]>0:
## pro[iz]=p.roots[np.isreal(p.roots)][ip]
## break
co2[:]=alk[:]/(k1/pro[:]+2.*k1*k2/pro[:]/pro[:])
hco3[:]=alk[:]/(1.+2.*k2/pro[:])
co3[:]=alk[:]/(pro[:]/k2+2.)
db_dalk=np.zeros(nz,dtype=np.float64)
dc_dalk=np.zeros(nz,dtype=np.float64)
db_ddic=np.zeros(nz,dtype=np.float64)
dc_ddic=np.zeros(nz,dtype=np.float64)
dph_dalk=np.zeros(nz,dtype=np.float64)
dph_ddic=np.zeros(nz,dtype=np.float64)
dco3_dalk=np.zeros(nz,dtype=np.float64)
dco3_ddic=np.zeros(nz,dtype=np.float64)
db_dalk[:] = k1*(-1.)*dic[:]*(-1.)/alk[:]/alk[:]
dc_dalk[:] = k1*k2*(-2.)*dic[:]*(-1.)/alk[:]/alk[:]
db_ddic[:] = k1*(-1./alk[:])
dc_ddic[:] = k1*k2*(-2./alk[:])
dph_dalk[:] = -0.5*db_dalk[:] + 0.5*0.5*(b[:]*b[:]-4.*c[:])**(-0.5)\
*(2.*b[:]*db_dalk[:] - 4.*dc_dalk[:])
dph_ddic[:] = -0.5*db_ddic[:] + 0.5*0.5*(b[:]*b[:]-4.*c[:])**(-0.5)\
*(2.*b[:]*db_ddic[:] - 4.*dc_ddic[:])
dco3_dalk[:] = 1./(pro[:]/k2+2.) + alk[:]*(-1.)/((pro[:]/k2+2.)**2.)\
*(1./k2)*dph_dalk[:]
dco3_ddic[:] = 0./(pro[:]/k2+2.) + alk[:]*(-1.)/((pro[:]/k2+2.)**2.)\
*(1./k2)*dph_ddic[:]
return co2,hco3,co3,pro,dco3_ddic,dco3_dalk,info
def calcco2h2o(tmp,sal,dep,dic,alk,nz):
info=0
k1=calceq1(tmp,sal,dep)
k2=calceq2(tmp,sal,dep)
kw=calceqw(tmp,sal,dep)
a=np.zeros(nz,dtype=np.float64)
b=np.zeros(nz,dtype=np.float64)
c=np.zeros(nz,dtype=np.float64)
d=np.zeros(nz,dtype=np.float64)
e=np.zeros(nz,dtype=np.float64)
ph=np.zeros(nz,dtype=np.float64)
a[:]=1e0
b[:]=alk[:]+k1
c[:]=-(dic[:]-alk[:])*k1-kw+k1*k2
d[:]=-(2e0*dic[:]-alk[:])*k1*k2-k1*kw
e[:]=-k1*k2*kw
for iz in range(nz):
p=np.poly1d([a[iz],b[iz],c[iz],d[iz],e[iz]])
for ip in range(p.roots[np.isreal(p.roots)].shape[0]):
if p.roots[np.isreal(p.roots)][ip]>0:
ph[iz]=p.roots[np.isreal(p.roots)][ip]
break
co2=np.zeros(nz,dtype=np.float64)
hco3=np.zeros(nz,dtype=np.float64)
co3=np.zeros(nz,dtype=np.float64)
co2[:] = dic[:]/(1e0+k1/ph[:]+k1*k2/ph[:]/ph[:])
hco3[:] = dic[:]/(ph[:]/k1+1e0+k2/ph[:])
co3[:] = dic[:]/(ph[:]*ph[:]/k1/k2+ph[:]/k2+1e0)
da_dalk=np.zeros(nz,dtype=np.float64)
db_dalk=np.zeros(nz,dtype=np.float64)
dc_dalk=np.zeros(nz,dtype=np.float64)
dd_dalk=np.zeros(nz,dtype=np.float64)
de_dalk=np.zeros(nz,dtype=np.float64)
da_ddic=np.zeros(nz,dtype=np.float64)
db_ddic=np.zeros(nz,dtype=np.float64)
dc_ddic=np.zeros(nz,dtype=np.float64)
dd_ddic=np.zeros(nz,dtype=np.float64)
de_ddic=np.zeros(nz,dtype=np.float64)
da_dalk[:]=0e0
db_dalk[:]=1e0
dc_dalk[:]=k1
dd_dalk[:]=k1*k2
de_dalk[:]=0e0
da_ddic[:]=0e0
db_ddic[:]=0e0
dc_ddic[:]=-k1
dd_ddic[:]=-2e0*k1*k2
de_ddic[:]=0e0
dph_dalk=np.zeros(nz,dtype=np.float64)
dph_ddic=np.zeros(nz,dtype=np.float64)
dco3_dalk=np.zeros(nz,dtype=np.float64)
dco3_ddic=np.zeros(nz,dtype=np.float64)
dph_dalk[:] = -(da_dalk[:]*ph[:]**4e0+db_dalk[:]*ph[:]**3e0+dc_dalk[:]*ph[:]**2e0+dd_dalk[:]*ph[:]+de_dalk[:]) \
/(a[:]*4e0*ph[:]**3e0+b[:]*3e0*ph[:]**2e0+c[:]*2e0*ph[:]+d[:])
dph_ddic[:] = -(da_ddic[:]*ph[:]**4e0+db_ddic[:]*ph[:]**3e0+dc_ddic[:]*ph[:]**2e0+dd_ddic[:]*ph[:]+de_ddic[:]) \
/(a[:]*4e0*ph[:]**3e0+b[:]*3e0*ph[:]**2e0+c[:]*2e0*ph[:]+d[:])
dco3_dalk[:] = 0e0/(ph[:]*ph[:]/k1/k2+ph[:]/k2+1e0) \
+ dic[:]*(-1e0)/((ph[:]*ph[:]/k1/k2+ph[:]/k2+1e0)**2e0)*(2e0*ph[:]/k1/k2+1e0/k2)*dph_dalk[:]
dco3_ddic[:] = 1e0/(ph[:]*ph[:]/k1/k2+ph[:]/k2+1e0) \
+ dic[:]*(-1e0)/((ph[:]*ph[:]/k1/k2+ph[:]/k2+1e0)**2e0)*(2e0*ph[:]/k1/k2+1e0/k2)*dph_ddic[:]
return co2,hco3,co3,ph,dco3_ddic,dco3_dalk,info
def test_co2h2o():
nz = 100
dic=np.zeros(nz,dtype=np.float64)
alk=np.zeros(nz,dtype=np.float64)
dep = 4.
sal = 35.
temp = 2.
dic[:] = 2285e0#*1e-6
alk[:] = 2211e0#*1e-6
co2,hco3,co3,ph,dco3_ddic,dco3_dalk,info = calcco2h2o(temp,sal,dep,dic,alk,nz)
print co2
print ''
print hco3
print ''
print co3
print ''
print ph
print ''
print dco3_ddic
print ''
print dco3_dalk
print ''
def calcdevs(tmp,sal,dep,dic,alk,nz):
info = 0
k1=np.float64(calceq1(tmp,sal,dep))
k2=np.float64(calceq2(tmp,sal,dep))
a=np.float64(1.)
b=np.zeros(nz,dtype=np.float64)
c=np.zeros(nz,dtype=np.float64)
pro=np.zeros(nz,dtype=np.float64)
co2=np.zeros(nz,dtype=np.float64)
hco3=np.zeros(nz,dtype=np.float64)
co3=np.zeros(nz,dtype=np.float64)
b[:]=(1.-dic[:]/alk[:])*k1
c[:]=(1.-2.*dic[:]/alk[:])*k1*k2
pro[:]=(-b[:]+(b[:]*b[:]-4.*a*c[:])**0.5)*0.5
if any(pro<0.):
print '... unable to calculate ph'
info = 1
co2 = np.zeros(nz,dtype=np.float64)
hco3 = np.zeros(nz,dtype=np.float64)
co3 = np.zeros(nz,dtype=np.float64)
co2[:]=alk[:]/(k1/pro[:]+2.*k1*k2/pro[:]/pro[:])
hco3[:]=alk[:]/(1.+2.*k2/pro[:])
co3[:]=alk[:]/(pro[:]/k2+2.)
db_dalk=np.zeros(nz,dtype=np.float64)
dc_dalk=np.zeros(nz,dtype=np.float64)
db_ddic=np.zeros(nz,dtype=np.float64)
dc_ddic=np.zeros(nz,dtype=np.float64)
dph_dalk=np.zeros(nz,dtype=np.float64)
dph_ddic=np.zeros(nz,dtype=np.float64)
dco3_dalk=np.zeros(nz,dtype=np.float64)
dco3_ddic=np.zeros(nz,dtype=np.float64)
db_dalk[:] = k1*(-1.)*dic[:]*(-1.)/alk[:]/alk[:]
dc_dalk[:] = k1*k2*(-2.)*dic[:]*(-1.)/alk[:]/alk[:]
db_ddic[:] = k1*(-1./alk[:])
dc_ddic[:] = k1*k2*(-2./alk[:])
dph_dalk[:] = -0.5*db_dalk[:] + 0.5*0.5*(b[:]*b[:]-4.*c[:])**(-0.5)\
*(2.*b[:]*db_dalk[:] - 4.*dc_dalk[:])
dph_ddic[:] = -0.5*db_ddic[:] + 0.5*0.5*(b[:]*b[:]-4.*c[:])**(-0.5)\
*(2.*b[:]*db_ddic[:] - 4.*dc_ddic[:])
dco3_dalk[:] = 1./(pro[:]/k2+2.) + alk[:]*(-1.)/((pro[:]/k2+2.)**2.)\
*(1./k2)*dph_dalk[:]
dco3_ddic[:] = 0./(pro[:]/k2+2.) + alk[:]*(-1.)/((pro[:]/k2+2.)**2.)\
*(1./k2)*dph_ddic[:]
return dco3_ddic,dco3_dalk,info
def co2sys_mocsy(nz,alk,dic,tempi,depi,sali):
# following is copied and pasted from test_mocsy.py and then modified
# Define input data (typical values at depth from 0 to 5000 meters)
temp = np.repeat(tempi, nz).astype('float32')
depth = np.repeat (depi, nz).astype('float32')
sal = np.repeat(sali, nz).astype('float32')
sil = phos = np.repeat(0.0, nz).astype('float32')
Patm = np.repeat(1.0, nz).astype('float32')
# optK1K2 = 'l'
optcon = 'mol/m3' # input concentrations are in MOL/m3
optt = 'Tinsitu' # input temperature, variable 'temp' is actually IN SITU temp [°C]
optp = 'm' # input variable 'depth' is in 'DECIBARS'
optb = 'l10'
optk1k2 = 'l'
optkf = 'dg'
# Create output arrays
# --------------------
# computed variables at 6 input points
lat = np.zeros((nz,)).astype('float32')
ph = np.zeros((nz,)).astype('float32')
pco2 = np.zeros((nz,)).astype('float32')
fco2 = np.zeros((nz,)).astype('float32')
co2 = np.zeros((nz,)).astype('float32')
hco3 = np.zeros((nz,)).astype('float32')
co3 = np.zeros((nz,)).astype('float32')
OmegaA = np.zeros((nz,)).astype('float32')
OmegaC = np.zeros((nz,)).astype('float32')
BetaD = np.zeros((nz,)).astype('float32')
rhoSW = np.zeros((nz,)).astype('float32')
p = np.zeros((nz,)).astype('float32')
tempis = np.zeros((nz,)).astype('float32')
# values of derivatives w/ respect to 6 input variables and at 6 input points
ph_deriv = np.zeros((6*nz,)).astype('float32')
pco2_deriv = np.zeros((6*nz,)).astype('float32')
OmegaA_deriv = np.zeros((6*nz,)).astype('float32')
ph_deriv = ph_deriv.reshape ((6,nz), order='F')
pco2_deriv = pco2_deriv.reshape ((6,nz), order='F')
OmegaA_deriv = OmegaA_deriv.reshape ((6,nz), order='F')
# Run mocsy.vars()
# Notice that option names are all lowercase
ph, pco2, fco2, co2, hco3, co3, OmegaA, OmegaC, BetaD, rhoSW, p, tempis = \
mocsy.mvars (temp, sal, alk, dic, sil, phos, Patm, depth, lat
,optcon=optcon, optt=optt, optp=optp, optb=optb, optk1k2=optk1k2, optkf=optkf)
# print mocsy results
# -------------------
# print "pH pCO2 fCO2 CO2* HCO3- CO32- OmegaA OmegaC R Density Press Temperature"
# for i in range (0, 3):
# print ph[i], pco2[i], fco2[i], co2[i], hco3[i], co3[i], OmegaA[i], OmegaC[i], BetaD[i], rhoSW[i], p[i], tempis[i]
# Compute automatic derivatives (using automatic differentiation)
ph_deriv, pco2_deriv, fco2_deriv, co2_deriv, hco3_deriv, co3_deriv, OmegaA_deriv, OmegaC_deriv = \
mocsy.mderivauto(temp, sal, alk, dic, sil, phos, Patm, depth, lat, # INPUT
optcon=optcon, optt=optt, optp=optp, optb=optb, optk1k2=optk1k2, optkf=optkf) # INPUT OPTIONS
# Compute buffer factors from Egleston
# pco2_deriv[2,:] are derivatives of pCO2 w/ respect to DIC
# pco2_deriv[1,:] are ... w/ respect to Alk
gamma_DIC = pco2 / pco2_deriv[1,:]
gamma_Alk = pco2 / pco2_deriv[0,:]
beta_DIC = -1. / (np.log(10.) * ph_deriv[1,:])
beta_Alk = -1. / (np.log(10.) * ph_deriv[0,:])
# Here, we use Omega of Aragonite (use of Calcite would have been equaly valid)
omega_DIC = OmegaA / OmegaA_deriv[1,:]
omega_Alk = OmegaA / OmegaA_deriv[0,:]
# print ""
# print "gamma_DIC gamma_Alk beta_DIC beta_Alk omega_DIC omega_Alk"
# for i in range (0, 3):
# print gamma_DIC[i], gamma_Alk[i], beta_DIC[i], beta_Alk[i], omega_DIC[i], omega_Alk[i]
# Print derivatives of pH with respect to phosphate, silicate, temperature and salinity
# print ""
# print "dpH/dPhos dpH/dSil dpH/dT dpH/dS"
# print "pH/µMol pH/µMol pH/°C pH/psu"
# for i in range (0, 3):
# print ph_deriv[2,i], ph_deriv[3,i], ph_deriv[4,i], ph_deriv[5,i]
return co2,hco3,co3,10.**-ph,OmegaC,OmegaC_deriv[1,:],OmegaC_deriv[0,:]
def calcupwindscheme(w,nz):
# ------------ determine calculation scheme for advection
up=np.zeros(nz,dtype=np.float64)
dwn=np.zeros(nz,dtype=np.float64)
cnr=np.zeros(nz,dtype=np.float64)
adf=np.ones(nz,dtype=np.float64)
up[:] = 0
dwn[:]=0
cnr[:] =0
adf[:]=1e0
for iz in range(nz ):
if iz==0:
if w[iz]>=0e0 and w[iz+1]>=0e0:up[iz] = 1
elif w[iz]<=0e0 and w[iz+1]<=0e0:dwn[iz] = 1
else: # where burial sign changes
if not w[iz]*w[iz+1] <=0e0:
print 'error'
input()
cnr[iz] = 1
elif iz==nz-1:
if w[iz]>=0e0 and w[iz-1]>=0e0:up[iz] = 1
elif w[iz]<=0e0 and w[iz-1]<=0e0:dwn[iz] = 1
else:
if not w[iz]*w[iz-1] <=0e0:
print 'error'
input()
cnr[iz] = 1
else :
if w[iz] >=0e0:
if w[iz+1]>=0e0 and w[iz-1]>=0e0:up[iz] = 1
else:cnr[iz] = 1
else:
if w[iz+1]<=0e0 and w[iz-1]<=0e0:dwn[iz] = 1
else:cnr[iz] = 1
if np.sum(up[:]+dwn[:]+cnr[:])!=nz:
print 'error',np.sum(up),np.sum(dwn),np.sum(cnr)
input()
for iz in range(nz-1):
if cnr[iz]==1 and cnr[iz+1]==1:
if w[iz]>=0e0 and w[iz+1] < 0e0:
corrf = np.float64(5e0) # This assignment of central advection term helps conversion especially when assuming turbo2 mixing
cnr[iz+1]=abs(w[iz]**corrf)/(abs(w[iz+1]**corrf)+abs(w[iz]**corrf))
cnr[iz]=abs(w[iz+1]**corrf)/(abs(w[iz+1]**corrf)+abs(w[iz]**corrf))
dwn[iz+1]=1e0-cnr[iz+1]
up[iz]=1e0-cnr[iz]
if cnr[iz]==1 and cnr[iz+1]==1:
if w[iz]< 0e0 and w[iz+1] >= 0e0:
cnr[iz+1]=0
cnr[iz]=0
up[iz+1]=1
dwn[iz]=1
adf[iz]=abs(w[iz+1])/(abs(w[iz+1])+abs(w[iz]))
adf[iz+1]=abs(w[iz])/(abs(w[iz+1])+abs(w[iz]))
return up,dwn,cnr,adf
def makegrid(beta,ztot,nz):
z = np.zeros(nz,dtype=np.float64)
dz = np.zeros(nz,dtype=np.float64)
eta = np.linspace(ztot/nz,ztot,nz)
for iz in range(nz):
if iz==0:dz[iz]=ztot*np.log((beta+(eta[iz]/ztot)**2)
/(beta-(eta[iz]/ztot)**2)) \
/np.log((beta+1.)/(beta-1.))
else:dz[iz] = ztot*np.log((beta+(eta[iz]/ztot)**2)
/(beta-(eta[iz]/ztot)**2)) \
/np.log((beta+1.)/(beta-1.)) - np.sum(dz[:iz])
for iz in range(nz):
if iz==0: z[iz]=dz[iz]*0.5
else: z[iz] = z[iz-1]+dz[iz-1]*0.5 + 0.5*dz[iz]
return dz,z
def getporosity(z,nz):
poro = np.zeros(nz,dtype=np.float64)
calgg = 0.0
pore_max = 1.- ( 0.483 + 0.45 * calgg) / 2.5 # porosity at the bottom
exp_pore = 0.25*calgg + 3.0 *(1.-calgg)# scale depth of e-fold porosity decrease
poro[:] = np.exp(-z[:]/exp_pore) * (1.0-pore_max) + pore_max
return poro
def dep2age(z,dz,nz,w):
age = np.zeros(nz,dtype=np.float64)
dage = np.zeros(nz,dtype=np.float64)
dage[:] = dz[:]/w[:]
for iz in range(nz):
if iz==0: age[iz]=dage[iz]*0.5
else: age[iz]=age[iz-1]+dage[iz-1]*0.5+0.5*dage[iz]
return age
def coefs(cai,temp,nz,nspcc,poro,komi,kcci,size,sal,dep):
dif_dic = np.zeros(nz,dtype=np.float64)
dif_alk = np.zeros(nz,dtype=np.float64)
dif_o2 = np.zeros(nz,dtype=np.float64)
kom = np.zeros(nz,dtype=np.float64)
kcc = np.zeros((nz,nspcc),dtype=np.float64)
co3sat = 0.
ff = np.zeros(nz,dtype=np.float64)
ff[:] = poro[:]*poro[:]
dif_dic0 = (151.69 + 7.93*temp) # cm2/yr at 2 oC (Huelse et al. 2018)
dif_alk0 = (151.69 + 7.93*temp) # cm2/yr (Huelse et al. 2018)
dif_o20 = (348.62 + 14.09*temp)
dif_dic[:] = dif_dic0*ff[:] # reflecting tortuosity factor
dif_alk[:] = dif_alk0*ff[:]
dif_o2[:] = dif_o20*ff[:]
kom[:] = komi # assume reference values for all reaction terms
kcc[:,:] = kcci
if size:
kcc[:,0:4]=kcci*10.
# keq1 = calceq1(temp,sal,dep)
# keq2 = calceq2(temp,sal,dep)
keqcc = calceqcc(temp,sal,dep)
co3sat = keqcc/cai
return dif_dic,dif_alk,dif_o2,kom,kcc,co3sat
def calc_zox(
oxic,anoxic,nz,o2x,o2th,komi,ztot,z,o2i,dz # input
):
"""calculation of zox"""
tol = 1e-6
zox = 0e0
o2_penetrated = True
for iz in range(nz):
if o2x[iz]<=0e0:
o2_penetrated = False
break
if o2_penetrated: # oxygen never gets less than 0
zox = ztot # zox is the bottom depth
else:
if iz==0: # calculating zox interpolating at z=0 with SWI conc. and at z=z(iz) with conc. o2x(iz)
zox = (z[iz]*o2i*1e-6/1e3 + 0e0*np.abs(o2x[iz]))/(o2i*1e-6/1e3+np.abs(o2x[iz]))
elif iz==1:
zox = z[iz-1] - o2x[iz-1]/((o2i*1e-6/1e3 - o2x[iz-1])/(0e0-z[iz-1]))
else: # calculating zox interpolating at z=z(iz-1) with o2x(iz-1) and at z=z(iz) with conc. o2x(iz)
zox = z[iz-1] - o2x[iz-1]/((o2x[iz-2] - o2x[iz-1])/(z[iz-2]-z[iz-1]))
# calculation of kom
kom = np.zeros(nz,dtype=np.float64)
kom_ox = np.zeros(nz,dtype=np.float64)
kom_an = np.zeros(nz,dtype=np.float64)
izox = -100
if anoxic:
kom[:] = komi
for iz in range(nz):
if z[iz]+0.5e0*dz[iz]<=zox:
kom_ox[iz]=komi
if iz> izox: izox = iz
elif z[iz]+0.5e0*dz[iz]>zox and z[iz]-0.5e0*dz[iz]< zox:
kom_ox[iz]=komi* (1e0- ( (z[iz]+0.5e0*dz[iz]) - zox)/dz[iz])
kom_an[iz]=komi* (( (z[iz]+0.5e0*dz[iz]) - zox)/dz[iz])
if iz> izox: izox = iz
elif z[iz]-0.5e0*dz[iz]>=zox:
kom_an[iz]=komi
if not (abs(kom_ox[:]+kom_an[:]-kom[:])/komi<tol).all():
print 'error: calc kom',kom_ox,kom_an,kom
input()
else:
for iz in range(nz):
if z[iz]+0.5e0*dz[iz]<=zox:
kom_ox[iz]=komi
if iz> izox: izox = iz
elif z[iz]+0.5e0*dz[iz]>zox and z[iz]-0.5e0*dz[iz]< zox:
kom_ox[iz]=komi* (1e0- ( (z[iz]+0.5e0*dz[iz]) - zox)/dz[iz])
if iz> izox :izox = iz
elif z[iz]-0.5e0*dz[iz]>=zox :
continue
kom[:] = kom_ox[:]
return izox,kom,zox,kom_ox,kom_an
def omcalc(
kom,omx # in&output
,oxic,anoxic,o2x,om,nz,sporo,sporoi,sporof,o2th,komi # input
,w,wi,dt,up,dwn,cnr,adf,trans,nspcc,labs,turbo2,nonlocal,omflx,poro,dz # input
):
nsp = 1
nmx=nz*nsp
amx=np.zeros((nmx,nmx),dtype=np.float64)
ymx=np.zeros((nmx),dtype=np.float64)
for iz in range(nz):
row = (iz)*nsp
if iz==0:
ymx[row] = \
+ sporo[iz]*(-om[iz])/dt \
- omflx/dz[iz]
amx[row,row] = (
# time change term
+ sporo[iz]*(1e0)/dt
# advection terms
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*1e0-sporoi*wi*0e0)/dz[iz]
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*0e0
-sporo[iz]*w[iz]*1e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*0e0
-sporoi*wi*0e0)/dz[iz]
# rxn term
+ sporo[iz]*kom[iz]
)
# matrix filling at grid iz but for unknwon at grid iz + 1
# (here only advection terms)
amx[row,row+nsp] = (
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*1e0-sporo[iz]*w[iz]*0e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*1e0-sporoi*wi*0e0)/dz[iz]
)
elif iz == nz-1:# : ! need to reflect lower boundary; none; but must care that iz + 1 does not exist
ymx[row] = 0e0 \
+ sporo[iz]*(-om[iz])/dt
amx[row,row] = (
# time change term
+ sporo[iz]*(1e0)/dt
# advection terms
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*1e0-sporo[iz-1]*w[iz-1]*0e0)/dz[iz]
+ adf[iz]*dwn[iz]*(sporof*w[iz]*1e0-sporo[iz]*w[iz]*1e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporof*w[iz]*1e0-sporo[iz-1]*w[iz-1]*0e0)/dz[iz]
# rxn term
+ sporo[iz]*kom[iz]
)
# filling matrix at grid iz but for unknown at grid iz-1 (only advection terms)
amx[row,row-nsp] = (
+ adf[iz]*up[iz]*(sporof*w[iz]*0e0-sporo[iz-1]*w[iz-1]*1e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporof*w[iz]*0e0-sporo[iz-1]*w[iz-1]*1e0)/dz[iz]
)
else:# # do not have to care about boundaries
ymx[row] = 0e0 \
+ sporo[iz]*(0e0-om[iz])/dt
amx[row,row] = (
# time change term
+ sporo[iz]*(1e0)/dt
# advection terms
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*1e0-sporo[iz-1]*w[iz-1]*0e0)/dz[iz]
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*0e0-sporo[iz]*w[iz]*1e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*0e0-sporo[iz-1]*w[iz-1]*0e0)/dz[iz]
# rxn term
+ sporo[iz]*kom[iz]
)
# filling matrix at grid iz but for unknown at grid iz+1 (only advection terms)
amx[row,row+nsp] = (
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*1e0-sporo[iz]*w[iz]*0e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*1e0-sporo[iz-1]*w[iz-1]*0e0)/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz-1 (only advection terms)
amx[row,row-nsp] = (
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*0e0-sporo[iz-1]*w[iz-1]*1e0)/dz[iz]
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*0e0-sporo[iz-1]*w[iz-1]*1e0)/dz[iz]
)
# diffusion terms are reflected with transition matrices
if turbo2[0] or labs[0]:
for iiz in range( nz):
col = (iiz)*nsp
if trans[iiz,iz,0]==0e0: continue
amx[row,col] += -trans[iiz,iz,0]/dz[iz]*dz[iiz]*(1e0-poro[iiz])
else :
for iiz in range( nz):
col = (iiz)*nsp
if trans[iiz,iz,0]==0e0: continue
amx[row,col] += -trans[iiz,iz,0]/dz[iz]
ymx = - ymx
# kai = np.linalg.solve(amx, ymx)
# kai = la.solve(amx, ymx)
lu, piv, kai, info = lapack.dgesv(amx, ymx)
ymx[:]=kai[:].copy()
omx[:] = ymx[:].copy() # now passing the solution to unknowns omx
return omx
def calcflxom(
sporo,om,omx,dt,w,dz,z,nz,turbo2,labs,nonlocal,poro
,up,dwn,cnr,adf,rho,mom,trans,kom,sporof,sporoi,wi,nspcc,omflx,workdir # input
):
""" calculating the fluxes relevant to om diagenesis (and checking the calculation satisfies the difference equations) """
omadv = np.float64(0e0)
omdec = np.float64(0e0)
omdif = np.float64(0e0)
omrain = np.float64(0e0)
omtflx = np.float64(0e0)
for iz in range(nz ):
if iz == 0:
omtflx += sporo[iz]*(omx[iz]-om[iz])/dt*dz[iz]
omadv += adf[iz]*up[iz]*(sporo[iz]*w[iz]*omx[iz]-0e0)/dz[iz]*dz[iz] \
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*omx[iz+1]-sporo[iz]*w[iz]*omx[iz])/dz[iz]*dz[iz] \
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*omx[iz+1]-0e0)/dz[iz]*dz[iz]
omdec += sporo[iz]*kom[iz]*omx[iz]*dz[iz]
omrain += - omflx/dz[iz]*dz[iz]
elif iz == nz-1:
omtflx = omtflx + sporo[iz]*(omx[iz]-om[iz])/dt*dz[iz]
omadv = omadv + adf[iz]*up[iz]*(sporo[iz]*w[iz]*omx[iz]-sporo[iz-1]*w[iz-1]*omx[iz-1])/dz[iz]*dz[iz] \
+ adf[iz]*dwn[iz]*(sporof*w[iz]*omx[iz]-sporo[iz]*w[iz]*omx[iz])/dz[iz]*dz[iz] \
+ adf[iz]*cnr[iz]*(sporof*w[iz]*omx[iz]-sporo[iz-1]*w[iz-1]*omx[iz-1])/dz[iz]*dz[iz]
omdec = omdec + sporo[iz]*kom[iz]*omx[iz]*dz[iz]
else :
omtflx = omtflx + sporo[iz]*(omx[iz]-om[iz])/dt*dz[iz]
omadv = omadv + adf[iz]*up[iz]*(sporo[iz]*w[iz]*omx[iz]-sporo[iz-1]*w[iz-1]*omx[iz-1])/dz[iz]*dz[iz] \
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*omx[iz+1]-sporo[iz]*w[iz]*omx[iz])/dz[iz]*dz[iz] \
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*omx[iz+1]-sporo[iz-1]*w[iz-1]*omx[iz-1])/dz[iz]*dz[iz]
omdec = omdec + sporo[iz]*kom[iz]*omx[iz]*dz[iz]
if turbo2[0] or labs[0]:
for iiz in range( nz):
if trans[iiz,iz,0]==0e0: continue
omdif = omdif -trans[iiz,iz,0]/dz[iz]*dz[iiz]*(1e0-poro[iiz])*dz[iz]*omx[iiz]
else:
for iiz in range(nz):
if trans[iiz,iz,0]==0e0: continue
omdif = omdif -trans[iiz,iz,0]/dz[iz]*dz[iz]*omx[iiz] # check previous versions
omres = omadv + omdec + omdif + omrain + omtflx # this is residual flux should be zero equations are exactly satisfied
flg_restart = False
if any(omx<0e0): # if negative om conc. is detected, need to stop
print 'negative om, stop'
file_tmp=open(workdir+'NEGATIVE_OM.txt','w')
for iz in range(nz):
print >> file_tmp, z[iz],omx[iz]*mom/rho[iz]*100e0,w[iz],up[iz],dwn[iz],cnr[iz],adf[iz]
file_tmp.close()
flg_restart = True
# input()
if any(np.isnan(omx)) : # if NAN, ... the same ... stop
print 'nan om, stop'
print omx
flg_restart = True
# input()
return omadv,omdec,omdif,omrain,omres,omtflx,flg_restart
def o2calc_ox(
izox,nz,poro,o2,kom,omx,sporo,dif_o2,dz,dt,o2i,ox2om,o2x # input
):
nsp = 1
nmx=nsp*nz
amx=np.zeros((nmx,nmx),dtype=np.float64)
ymx=np.zeros((nmx),dtype=np.float64)
amx[:] = 0e0
ymx[:] = 0e0
for iz in range(nz):
row = (iz)*nsp
if iz == 0 : # be careful about upper boundary
ymx[row] = (
+ poro[iz]*(0e0-o2[iz])/dt
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(0e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- poro[iz]*dif_o2[iz]*(0e0-o2i*1e-6/1e3)/dz[iz])/dz[iz]
# rxn term
+ sporo[iz]*ox2om*kom[iz]*omx[iz]
)
amx[row,row] = (
+ poro[iz]*(1e0)/dt
# diffusion term
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(-1e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- poro[iz]*dif_o2[iz]*(1e0)/dz[iz])/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz+1 (only diffusion term)
amx[row,row+nsp] = (
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(1e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- 0e0)/dz[iz]
)
elif iz == nz-1 : # be careful about lower boundary
ymx[row] = (0e0
# time change term
+ poro[iz]*(0e0-o2[iz])/dt
# diffusion term
- (0e0 - 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(0e0)/(0.5e0*(dz[iz-1]+dz[iz])))/dz[iz]
# rxn term
+ sporo[iz]*ox2om*kom[iz]*omx[iz]
)
amx[row,row] = (
# time change term
+ poro[iz]*(1e0)/dt
# diffusion term
- (0e0 - 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(1e0)/(0.5e0*(dz[iz-1]+dz[iz])))/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz-1 (only diffusion term)
amx[row,row-nsp] = (
- (0e0 - 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(-1e0)/(0.5e0*(dz[iz-1]+dz[iz])))/dz[iz]
)
else :
ymx[row] = ( 0e0
# time change term
+ poro[iz]*(0e0-o2[iz])/dt
# diffusion term
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(0e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(0e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
# rxn term
+ sporo[iz]*ox2om*kom[iz]*omx[iz]
)
amx[row,row] = (
# time change term
+ poro[iz]*(1e0)/dt
# diffusion term
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(-1e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(1e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz+1 (only diffusion term)
amx[row,row+nsp] = (
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(1e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0e0)/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz-1 (only diffusion term)
amx[row,row-nsp]= (
- (0e0
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(-1e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
)
ymx = - ymx # sign change; see above for the case of om
# ymx = np.linalg.solve(amx, ymx)
# kai = la.solve(amx, ymx)
lu, piv, kai, info = lapack.dgesv(amx, ymx)
ymx[:]=kai[:].copy()
o2x[:] = ymx[:].copy() # passing solutions to unknowns
return o2x
def calcflxo2_ox(
nz,sporo,kom,omx,dz,poro,dif_o2,dt,o2,o2x,ox2om,o2i # input
):
o2dec = np.float64(0e0)
o2dif = np.float64(0e0)
o2tflx = np.float64(0e0)
for iz in range(nz):
if iz == 0 :
o2dec = o2dec + sporo[iz]*ox2om*kom[iz]*omx[iz]*dz[iz]
o2tflx = o2tflx + (o2x[iz]-o2[iz])/dt*dz[iz]*poro[iz]
o2dif = o2dif - ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(o2x[iz+1]-o2x[iz])/(0.5e0*(dz[iz]+dz[iz+1])) \
- poro[iz]*dif_o2[iz]*(o2x[iz]-o2i*1e-6/1e3)/dz[iz])/dz[iz] *dz[iz]
elif iz == nz-1 :
o2dec = o2dec + (1e0-poro[iz])*ox2om*kom[iz]*omx[iz]/poro[iz]*dz[iz]*poro[iz]
o2tflx = o2tflx + (o2x[iz]-o2[iz])/dt*dz[iz]*poro[iz]
o2dif = o2dif \
- (0e0 - 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(o2x[iz]-o2x[iz-1])/(0.5e0*(dz[iz-1]+dz[iz])))/dz[iz] \
*dz[iz]
else :
o2dec = o2dec + (1e0-poro[iz])*ox2om*kom[iz]*omx[iz]/poro[iz]*dz[iz]*poro[iz]
o2tflx = o2tflx + (o2x[iz]-o2[iz])/dt*dz[iz]*poro[iz]
o2dif = o2dif \
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(o2x[iz+1]-o2x[iz])/(0.5e0*(dz[iz+1]+dz[iz])) \
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(o2x[iz]-o2x[iz-1])/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz] \
*dz[iz]
o2res = o2dec + o2dif + o2tflx # residual flux
return o2dec,o2dif,o2tflx,o2res
def o2calc_sbox(
izox,nz,poro,o2,kom,omx,sporo,dif_o2,dz,dt,o2i,ox2om,o2x # input
):
# print izox
# if izox!=nz-1:iz_zero = izox # modification made only for Python
# else:iz_zero = izox-1 # modification made only for Python
iz_zero = izox
nsp = 1
nmx=nsp*nz
amx=np.zeros((nmx,nmx),dtype=np.float64)
ymx=np.zeros((nmx),dtype=np.float64)
amx[:] = 0e0
ymx[:] = 0e0
for iz in range(nz ):
row = (iz)*nsp
if iz == 0 :
ymx[row] = (
# time change
+ poro[iz]*(0e0-o2[iz])/dt
# diffusion
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(0e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- poro[iz]*dif_o2[iz]*(0e0-o2i*1e-6/1e3)/dz[iz])/dz[iz]
# rxn
+ sporo[iz]*ox2om*kom[iz]*omx[iz]
)
amx[row,row] = (
# time change
+ poro[iz]*(1e0)/dt
# diffusion
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(-1e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- poro[iz]*dif_o2[iz]*(1e0)/dz[iz])/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz+1 (only diffusion term)
amx[row,row+nsp] = (
- ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(1e0)/(0.5e0*(dz[iz]+dz[iz+1]))
- 0e0)/dz[iz]
)
elif iz>0 and iz<= iz_zero :
ymx[row] = ( 0e0
# time change
+ poro[iz]*(0e0-o2[iz])/dt
# diffusion
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(0e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(0e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
# rxn
+ sporo[iz]*ox2om*kom[iz]*omx[iz]
)
amx[row,row] = (
# time change
+ poro[iz]*(1e0)/dt
# diffusion
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(-1e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(1e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz+1 (only diffusion term)
amx[row,row+nsp] = (
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(1e0)/(0.5e0*(dz[iz+1]+dz[iz]))
- 0e0)/dz[iz]
)
# filling matrix at grid iz but for unknown at grid iz-1 (only diffusion term)
amx[row,row-nsp] = (
- (0e0
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(-1e0)/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz]
)
elif iz> iz_zero : # at lower than zox; zero conc. is forced
ymx[row] = ( 0e0
)
amx[row,row] = (
+ 1e0
)
ymx = - ymx # change signs
# kai = np.linalg.solve(amx, ymx) # solving
# kai = la.solve(amx, ymx) # solving
lu, piv, kai, info = lapack.dgesv(amx, ymx)
ymx[:]=kai[:].copy()
o2x[:] = ymx[:].copy() # passing solution to variable
return o2x
def calcflxo2_sbox(
nz,sporo,kom,omx,dz,poro,dif_o2,dt,o2,o2x,izox,ox2om,o2i # input
):
# if izox!=nz-1:iz_zero = izox # modification made only for Python
# else:iz_zero = izox-1 # modification made only for Python
iz_zero = izox
# fluxes relevant to oxygen
o2dec = np.float64(0e0)
o2dif = np.float64(0e0)
o2tflx = np.float64(0e0)
for iz in range(nz ):
if iz == 0:
o2dec = o2dec + sporo[iz]*ox2om*kom[iz]*omx[iz]*dz[iz]
o2tflx = o2tflx + (o2x[iz]-o2[iz])/dt*dz[iz]*poro[iz]
o2dif = o2dif - ((poro[iz]*dif_o2[iz]+poro[iz+1]*dif_o2[iz+1])*0.5e0*(o2x[iz+1]-o2x[iz])/(0.5e0*(dz[iz]+dz[iz+1]))
- poro[iz]*dif_o2[iz]*(o2x[iz]-o2i*1e-6/1e3)/dz[iz])/dz[iz] *dz[iz]
elif iz>0 and iz<=iz_zero :
o2dec = o2dec + (1e0-poro[iz])*ox2om*kom[iz]*omx[iz]/poro[iz]*dz[iz]*poro[iz]
o2tflx = o2tflx + (o2x[iz]-o2[iz])/dt*dz[iz]*poro[iz]
o2dif = o2dif \
- (0.5e0*(poro[iz+1]*dif_o2[iz+1]+poro[iz]*dif_o2[iz])*(o2x[iz+1]-o2x[iz])/(0.5e0*(dz[iz+1]+dz[iz]))
- 0.5e0*(poro[iz]*dif_o2[iz]+poro[iz-1]*dif_o2[iz-1])*(o2x[iz]-o2x[iz-1])/(0.5e0*(dz[iz]+dz[iz-1])))/dz[iz] \
*dz[iz]
o2res = o2dec + o2dif + o2tflx # residual flux
return o2dec,o2dif,o2tflx,o2res
def calccaco3sys( #
ccx,dicx,alkx,rcc,dt # in&output
,nspcc,dic,alk,dep,sal,temp,labs,turbo2,nonlocal,sporo,sporoi,sporof,poro,dif_alk,dif_dic # input
,w,up,dwn,cnr,adf,dz,trans,cc,oxco2,anco2,co3sat,kcc,ccflx,ncc,omega,nz,tol,sparse,fact # input
,dici,alki,ccx_th,showiter,w_pre,co2chem,mcc,rho,workdir
):
drcc_dco3=np.zeros((nz,nspcc),dtype=np.float64)
drcc_dcc=np.zeros((nz,nspcc),dtype=np.float64)
drcc_ddic=np.zeros((nz,nspcc),dtype=np.float64)
drcc_dalk=np.zeros((nz,nspcc),dtype=np.float64)
dco3_dalk=np.zeros((nz),dtype=np.float64)
dco3_ddic=np.zeros((nz),dtype=np.float64)
error = 1e4
itr = 0
nsp = 2 + nspcc # now considered species are dic, alk and nspcc of caco3
nmx = nz*nsp # col (and row) of matrix; the same number of unknowns
flg_restart = False
while error > tol:
if flg_restart: break # exit from loop for caco3 system iteration
if sparse:amx=lil_matrix((nmx,nmx),dtype=np.float64)
else:amx=np.zeros((nmx,nmx),dtype=np.float64)
amx[:,:]=0e0
ymx=np.zeros(nmx,dtype=np.float64)
# calling subroutine from caco3_therm.f90 to calculate aqueous co2 species
if co2chem != 'mocsy':
if co2chem == 'co2':
co2x,hco3x,co3x,prox,dco3_ddic,dco3_dalk,info = calcspecies(temp,sal,dep,dicx,alkx,nz)
# print info
# calcspecies(temp,sal,dep,dicx,alkx)
if info==1: # which means error in calculation
dt=dt/10e0
dicx[:]=dic[:].copy()
alkx[:]=alk[:].copy()
ccx[:,:]=cc[:,:].copy()
w[:]=w_pre[:].copy()
# upwind(w)
up,dwn,cnr,adf = calcupwindscheme(w,nz)
print 'location 1'
flg_restart = True
continue
print 'stop'
input()
elif co2chem=='co2h2o':
co2x,hco3x,co3x,prox,dco3_ddic,dco3_dalk,info = calcco2h2o(temp,sal,dep,dicx,alkx,nz)
if info==1: # which means error in calculation
dt=dt/10e0
dicx[:]=dic[:].copy()
alkx[:]=alk[:].copy()
ccx[:,:]=cc[:,:].copy()
w[:]=w_pre[:].copy()
# upwind(w)
up,dwn,cnr,adf = calcupwindscheme(w,nz)
print 'location 2'
flg_restart = True
continue
print 'stop'
input()
for isp in range(nspcc):
# calculation of dissolution rate for individual species
rcc[:,isp] = kcc[:,isp]*ccx[:,isp]*abs(1e0-co3x[:]*1e3/co3sat)**ncc*((1e0-co3x[:]*1e3/co3sat)>0e0).astype(float)
# calculation of derivatives of dissolution rate wrt conc. of caco3 species, dic and alk
drcc_dcc[:,isp] = kcc[:,isp]*abs(1e0-co3x[:]*1e3/co3sat)**ncc*((1e0-co3x[:]*1e3/co3sat)>0e0).astype(float)
drcc_dco3[:,isp] = kcc[:,isp]*ccx[:,isp]*ncc*abs(1e0-co3x[:]*1e3/co3sat)**(ncc-1e0) \
*((1e0-co3x[:]*1e3/co3sat)>0e0).astype(float)*(-1e3/co3sat)
drcc_ddic[:,isp] = drcc_dco3[:,isp]*dco3_ddic[:]
drcc_dalk[:,isp] = drcc_dco3[:,isp]*dco3_dalk[:]
elif co2chem == 'mocsy':
co2x,hco3x,co3x,prox,ohmega,dohmega_ddic,dohmega_dalk = co2sys_mocsy(nz,alkx*1e6,dicx*1e6,temp,dep*1e3,sal)
co2x = co2x/1e6
hco3x = hco3x/1e6
co3x = co3x/1e6
dohmega_ddic = dohmega_ddic*1e6
dohmega_dalk = dohmega_dalk*1e6
drcc_dohmega = np.zeros((nz,nspcc),dtype=np.float64)
for isp in range(nspcc):
# calculation of dissolution rate for individual species
rcc[:,isp] = kcc[:,isp]*ccx[:,isp]*abs(1e0-ohmega[:])**ncc*((1e0-ohmega[:])>0e0).astype(float)
# calculation of derivatives of dissolution rate wrt conc. of caco3 species, dic and alk
drcc_dcc[:,isp] = kcc[:,isp]*abs(1e0-ohmega[:])**ncc*((1e0-ohmega[:])>0e0).astype(float)
drcc_dohmega[:,isp] = kcc[:,isp]*ccx[:,isp]*ncc*abs(1e0-ohmega[:])**(ncc-1e0) \
*((1e0-ohmega[:])>0e0).astype(float)*(-1e0)
drcc_ddic[:,isp] = drcc_dohmega[:,isp]*dohmega_ddic[:]
drcc_dalk[:,isp] = drcc_dohmega[:,isp]*dohmega_dalk[:]
for iz in range(nz):
row =(iz)*nsp
if iz == 0: # when upper condition must be taken account; *** comments for matrix filling are given only in this case
for isp in range(nspcc): # multiple caco3 species
# put f(x) for isp caco3 species
ymx[row+isp] = \
+ sporo[iz]*(ccx[iz,isp]-cc[iz,isp])/dt \
- ccflx[isp]/dz[iz] \
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*ccx[iz,isp]-0e0)/dz[iz] \
+ adf[iz]*dwn[iz]*(sporo[iz+1]*w[iz+1]*ccx[iz+1,isp]-sporo[iz]*w[iz]*ccx[iz,isp])/dz[iz] \
+ adf[iz]*cnr[iz]*(sporo[iz+1]*w[iz+1]*ccx[iz+1,isp]-0e0)/dz[iz] \
+ sporo[iz]*rcc[iz,isp]
# derivative of f(x) wrt isp caco3 conc. at grid iz in ln
amx[row+isp,row+isp] = (
+ sporo[iz]*(1e0)/dt
+ adf[iz]*up[iz]*(sporo[iz]*w[iz]*1e0-0e0)/dz[iz]
+ adf[iz]*dwn[iz]*(0e0-sporo[iz]*w[iz]*1e0)/dz[iz]
+ sporo[iz]* drcc_dcc[iz,isp]
)* ccx[iz,isp]
# derivative of f(x) wrt isp caco3 conc. at grid iz+1 in ln
amx[row+isp,row+isp+nsp] = (