-
Notifications
You must be signed in to change notification settings - Fork 0
/
easy_chem.f90
2046 lines (1782 loc) · 70.9 KB
/
easy_chem.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
!!!!---------------------------
!!!! EASY CHEM, A CEA CLONE
!!!!---------------------------
!!!! (C) BY PAUL MOLLIERE 2015
!!!!----
!!!! SOME GLOBAL DATA STORAGE STUFF
!!!!----
module thermo_data_block
implicit none
CHARACTER*80, parameter, public :: fpath = '~/ARCiS/Data/thermo_easy_chem_simp_own.inp'
INTEGER, parameter, public :: N_coeffs = 10, N_temps = 10, N_reac_save = 1000
INTEGER, public :: thermo_data_n_coeffs(N_temps,N_reac_save), thermo_data_n_intervs(N_reac_save)
DOUBLE PRECISION, public :: thermo_data(N_coeffs,N_temps,N_reac_save), mol_weight(N_reac_save)
DOUBLE PRECISION, public :: thermo_data_temps(2,N_temps,N_reac_save), reac_stoich(5,N_reac_save), &
form_heat_Jmol_298_15_K(N_reac_save), thermo_data_T_exps(8,N_temps,N_reac_save), &
H_0_298_15_K_m_H_0_0_K(N_temps,N_reac_save)
DOUBLE PRECISION, parameter, public :: R = 8.3144598d0
CHARACTER*2, public :: reac_atoms_names(5,N_reac_save)
LOGICAL, public :: reac_condensed(N_reac_save), reac_ion(N_reac_save)
LOGICAL, public :: verbose, ions, quick, remove_ions
INTEGER, public :: iter_max
INTEGER, public :: N_gas, N_ions
CHARACTER*40 :: names_reactants_reorg(N_reac_save)
DOUBLE PRECISION, public, parameter :: amu = 1.660538921d-24, kB=1.3806488d-16
DOUBLE PRECISION, public, parameter :: mol = 6.02214129d23
INTEGER, public, parameter :: N_atoms_save = 104
! The atomic mass data shown below was taken from http://www.science.co.il/PTelements.asp
CHARACTER*2, public, parameter :: names_atoms_save(N_atoms_save) = &
(/ 'E ','H ','He','Li','Be','B ','C ','N ','O ','F ','Ne','Na', &
'Mg','Al','Si','P ','S ','Cl','Ar','K ','Ca','Sc','Ti','V ','Cr','Mn', &
'Fe','Co','Ni','Cu','Zn','Ga','Ge','As','Se','Br','Kr','Rb','Sr', &
'Y ','Zr','Nb','Mo','Tc','Ru','Rh','Pd','Ag','Cd','In','Sn','Sb', &
'Te','I ','Xe','Cs','Ba','La','Ce','Pr','Nd','Pm','Sm','Eu','Gd', &
'Tb','Dy','Ho','Er','Tm','Yb','Lu','Hf','Ta','W ','Re','Os','Ir', &
'Pt','Au','Hg','Tl','Pb','Bi','Po','At','Rn','Fr','Ra','Ac','Th', &
'Pa','U ','Np','Pu','Am','Cm','Bk','Cf','Es','Fm','Md','No','Lr' /)
DOUBLE PRECISION, public, parameter :: masses_atoms_save(N_atoms_save) = &
amu*(/ 0.000548579909d0, 1.0079d0,4.0026d0,6.941d0,9.0122d0,10.811d0,12.0107d0,14.0067d0 &
,15.9994d0,18.9984d0,20.1797d0,22.9897d0,24.305d0,26.9815d0,28.0855d0,30.9738d0,32.065d0 &
,35.453d0,39.948d0,39.0983d0,40.078d0,44.9559d0,47.867d0,50.9415d0,51.9961d0,54.938d0,55.845d0 &
,58.9332d0,58.6934d0,63.546d0,65.39d0,69.723d0,72.64d0,74.9216d0,78.96d0,79.904d0,83.8d0,85.4678d0 &
,87.62d0,88.9059d0,91.224d0,92.9064d0,95.94d0,98d0,101.07d0,102.9055d0,106.42d0,107.8682d0 &
,112.411d0,114.818d0,118.71d0,121.76d0,127.6d0,126.9045d0,131.293d0,132.9055d0,137.327d0,138.9055d0 &
,140.116d0,140.9077d0,144.24d0,145d0,150.36d0,151.964d0,157.25d0,158.9253d0,162.5d0,164.9303d0 &
,167.259d0,168.9342d0,173.04d0,174.967d0,178.49d0,180.9479d0,183.84d0,186.207d0,190.23d0,192.217d0 &
,195.078d0,196.9665d0,200.59d0,204.3833d0,207.2d0,208.9804d0,209d0,210d0,222d0,223d0,226d0,227d0,232.0381d0 &
,231.0359d0,238.0289d0,237d0,244d0,243d0,247d0,247d0,251d0,252d0,257d0,258d0,259d0,262d0/)
end module thermo_data_block
!!!!----
!!!! MAIN SUBROUTINE
!!!!----
subroutine EASY_CHEM(N_atoms,N_reactants,names_atoms,names_reactants,molfracs_atoms, &
molfracs_reactants,massfracs_reactants,temp,press,ini,nabla_ad,gamma2,MMW,rho)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_atoms, N_reactants
CHARACTER*40 :: names_atoms(N_atoms), names_reactants(N_reactants), &
names_reactants_orig(N_reactants)
DOUBLE PRECISION :: molfracs_atoms(N_atoms), molfracs_reactants(N_reactants), &
massfracs_reactants(N_reactants)
DOUBLE PRECISION :: temp, press
DOUBLE PRECISION :: thermo_quants,nabla_ad,gamma2,MMW,rho
LOGICAL :: ini
!! Internal:
DOUBLE PRECISION :: C_P_0(N_reactants), H_0(N_reactants), S_0(N_reactants), &
molfracs_atoms_ions(N_atoms+1)
INTEGER :: i_reac, N_atoms_use
CHARACTER*40 :: names_atoms_ions(N_atoms+1)
verbose = .FALSE.
quick = .FALSE.
remove_ions = .FALSE.
! Contains the original order of reactant names
names_reactants_orig = names_reactants
call init_random_seed()
! If EASY_CHEM is called the first time it needs to read in the thermodynamic data for all
! the considered reactant species:
if (ini) then
call ec_READ_ALL_DATA(N_reactants,names_reactants)
! Reordered such that all condensed species are at the end
names_reactants_reorg(1:N_reactants) = names_reactants
else
! ec_READ_ALL_DATA is only called when ini == .TRUE.
! (must be done at least once!).
! For ini == .FALSE. restore the ordering where the
! condensed species are at the end, because this
! is how the thermodynamic data is stored
names_reactants = names_reactants_reorg(1:N_reactants)
end if
names_atoms_ions(1:N_atoms) = names_atoms
molfracs_atoms_ions(1:N_atoms) = molfracs_atoms
names_atoms_ions(N_atoms+1) = 'E'
molfracs_atoms_ions(N_atoms+1) = 0d0
IF (ions .AND. temp > 750d0) THEN
N_atoms_use = N_atoms+1
ELSE IF (ions .AND. temp <= 750d0) THEN
remove_ions = .TRUE.
N_atoms_use = N_atoms
ELSE
N_atoms_use = N_atoms
END IF
call ec_CALC_THERMO_QUANTS(N_reactants,names_reactants,temp,C_P_0, H_0, S_0)
call ec_CALC_EQU_CHEM(N_atoms_use,N_reactants,names_atoms_ions(1:N_atoms_use), &
names_reactants,molfracs_atoms_ions(1:N_atoms_use), &
molfracs_reactants,massfracs_reactants,temp,press,C_P_0, H_0, S_0, &
names_reactants_orig,nabla_ad,gamma2,MMW,rho)
! Restore names etc. to original, i.e. user input order.
names_reactants = names_reactants_orig
end subroutine EASY_CHEM
!!!!----
!!!! "SUB"-SUBROUTINES
!!!!----
!#####################################################
subroutine ec_READ_ALL_DATA(N_reactants,names_reactants)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_reactants
CHARACTER*40 :: names_reactants(N_reactants)
!! FILE I/O:
CHARACTER*80 :: file_line
!! INTERNAL
INTEGER :: i_reac, n_interv, i_interv, &
i_stoich, stoich_start, reac_found
reac_found = 0
thermo_data_n_intervs = -1
N_gas = 0
N_ions = 0
reac_ion = .FALSE.
ions = .FALSE.
OPEN(unit=17,file=fpath)
DO WHILE (1>0)
READ(17,'(A80)',end=122) file_line
DO i_reac = 1, N_reactants
IF (TRIM(ADJUSTL(file_line(1:18))) .EQ. &
TRIM(ADJUSTL(names_reactants(i_reac)))) THEN
reac_found = reac_found+1
READ(17,'(A80)',end=122) file_line
READ(file_line(1:3),'(I2)') n_interv
thermo_data_n_intervs(i_reac) = n_interv
IF (file_line(52:52) == '0') THEN
reac_condensed(i_reac) = .FALSE.
N_gas = N_gas + 1
ELSE
reac_condensed(i_reac) = .TRUE.
END IF
END IF
END DO
END DO
122 close(17)
IF (reac_found .NE. N_reactants) THEN
WRITE(*,*)
WRITE(*,*) 'EASY CHEM ERROR! For the following species no thermodynamical data was found:'
DO i_reac = 1, N_reactants
IF (thermo_data_n_intervs(i_reac) .EQ. -1) THEN
WRITE(*,*) trim(adjustl(names_reactants(i_reac)))
END IF
END DO
STOP
END IF
! Put condensed species at the end of the reactants array
call reorder_specs(N_reactants,names_reactants)
! BASED ON THE ~RIGHT DESCRIPTION GIVEN IN GORDON 1996, page 73
! AND THE APPEARANCE OF THERMO.INP.
OPEN(unit=17,file=fpath)
DO WHILE (1>0)
READ(17,'(A80)',end=123) file_line
DO i_reac = 1, N_reactants
IF (TRIM(ADJUSTL(file_line(1:18))) .EQ. &
TRIM(ADJUSTL(names_reactants(i_reac)))) THEN
READ(17,'(A80)',end=123) file_line
READ(file_line(1:3),'(I2)') n_interv
thermo_data_n_intervs(i_reac) = n_interv
stoich_start = 11
DO i_stoich = 1, 5
reac_atoms_names(i_stoich,i_reac) = file_line(stoich_start:stoich_start+1)
! Are there ions to be treated?
IF (trim(adjustl(reac_atoms_names(i_stoich,i_reac))) .EQ. 'E') THEN
ions = .TRUE.
reac_ion(i_reac) = .TRUE.
N_ions = N_ions + 1
END IF
READ(file_line(stoich_start+2:stoich_start+7),'(F6.2)') reac_stoich(i_stoich,i_reac)
stoich_start = stoich_start+8
END DO
IF (file_line(52:52) == '0') THEN
reac_condensed(i_reac) = .FALSE.
ELSE
reac_condensed(i_reac) = .TRUE.
END IF
READ(file_line(53:65),'(F13.5)') mol_weight(i_reac)
READ(file_line(66:80),'(F13.5)') form_heat_Jmol_298_15_K(i_reac)
DO i_interv = 1, 3*n_interv
READ(17,'(A80)',end=123) file_line
IF (MOD(i_interv,3) .EQ. 1) THEN
READ(file_line(2:22),'(F10.3,X,F10.3)') thermo_data_temps(1,i_interv/3+1,i_reac), &
thermo_data_temps(2,i_interv/3+1,i_reac)
READ(file_line(23:23),'(I1)') thermo_data_n_coeffs(i_interv/3+1,i_reac)
READ(file_line(24:63),'(8F5.1)') thermo_data_T_exps(1,i_interv/3+1,i_reac), &
thermo_data_T_exps(2,i_interv/3+1,i_reac), thermo_data_T_exps(3,i_interv/3+1,i_reac), &
thermo_data_T_exps(4,i_interv/3+1,i_reac), thermo_data_T_exps(5,i_interv/3+1,i_reac), &
thermo_data_T_exps(6,i_interv/3+1,i_reac), thermo_data_T_exps(7,i_interv/3+1,i_reac), &
thermo_data_T_exps(8,i_interv/3+1,i_reac)
READ(file_line(66:80),'(F15.3)') H_0_298_15_K_m_H_0_0_K(i_interv/3+1,i_reac)
END IF
IF (MOD(i_interv,3) .EQ. 2) THEN
READ(file_line(1:80),'(5D16.8)') thermo_data(1,i_interv/3+1,i_reac), &
thermo_data(2,i_interv/3+1,i_reac), thermo_data(3,i_interv/3+1,i_reac) , &
thermo_data(4,i_interv/3+1,i_reac), thermo_data(5,i_interv/3+1,i_reac)
END IF
IF (MOD(i_interv,3) .EQ. 0) THEN
READ(file_line(1:80),'(5D16.8)') thermo_data(6,i_interv/3,i_reac), &
thermo_data(7,i_interv/3,i_reac), thermo_data(8,i_interv/3,i_reac) , &
thermo_data(9,i_interv/3,i_reac), thermo_data(10,i_interv/3,i_reac)
END IF
END DO
END IF
END DO
END DO
123 CLOSE(17)
end subroutine ec_READ_ALL_DATA
!#####################################################
subroutine reorder_specs(N_reactants,names_reactants)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_reactants
CHARACTER*40 :: names_reactants(N_reactants), &
names_reactants_buff(N_reactants)
!! Internal:
INTEGER :: i_reac, gas_offset, cond_offset
names_reactants_buff = names_reactants
gas_offset = 1
cond_offset = 1
DO i_reac = 1, N_reactants
IF (reac_condensed(i_reac)) THEN
names_reactants(N_gas+cond_offset) = names_reactants_buff(i_reac)
cond_offset = cond_offset + 1
ELSE
names_reactants(gas_offset) = names_reactants_buff(i_reac)
gas_offset = gas_offset + 1
END IF
END DO
end subroutine reorder_specs
!#####################################################
subroutine ec_CALC_THERMO_QUANTS(N_reactants,names_reactants,temp,C_P_0, H_0, S_0)
use thermo_data_block
implicit none
!! I/O
INTEGER :: N_reactants
CHARACTER*40 :: names_reactants(N_reactants)
DOUBLE PRECISION :: temp
DOUBLE PRECISION :: C_P_0(N_reactants), H_0(N_reactants), &
S_0(N_reactants)
!! internal
INTEGER :: i_reac, i_tempinv, tempinv_ind
DO i_reac = 1, N_reactants
! Get temperature interpolation range
IF (temp < thermo_data_temps(1,1,i_reac)) THEN
tempinv_ind = 1
ELSE IF (temp >= thermo_data_temps(2,thermo_data_n_intervs(i_reac),i_reac)) THEN
tempinv_ind = thermo_data_n_intervs(i_reac)
ELSE
DO i_tempinv = 1, thermo_data_n_intervs(i_reac)
IF ((temp >= thermo_data_temps(1,i_tempinv,i_reac)) .AND. &
(temp < thermo_data_temps(2,i_tempinv,i_reac))) THEN
tempinv_ind = i_tempinv
EXIT
END IF
END DO
END IF
! Calculate thermodynamic quantities as explained in Gordon 1996, page 74
C_P_0(i_reac) = (thermo_data(1,tempinv_ind,i_reac)*temp**(-2)+ &
thermo_data(2,tempinv_ind,i_reac)*temp**(-1)+ &
thermo_data(3,tempinv_ind,i_reac)+thermo_data(4,tempinv_ind,i_reac)* &
temp**(1)+thermo_data(5,tempinv_ind,i_reac)*temp**(2)+ &
thermo_data(6,tempinv_ind,i_reac)*temp**(3)+thermo_data(7,tempinv_ind,i_reac)* &
temp**(4))*R
H_0(i_reac) = (-thermo_data(1,tempinv_ind,i_reac)*temp**(-2)+ &
thermo_data(2,tempinv_ind,i_reac)*temp**(-1)*log(temp)+ &
thermo_data(3,tempinv_ind,i_reac)+thermo_data(4,tempinv_ind,i_reac)*temp**(1)/2d0+ &
thermo_data(5,tempinv_ind,i_reac)*temp**(2)/3d0+ &
thermo_data(6,tempinv_ind,i_reac)*temp**(3)/4d0+thermo_data(7,tempinv_ind,i_reac)* &
temp**(4)/5d0+thermo_data(9,tempinv_ind,i_reac)/temp)* &
R*temp
S_0(i_reac) = (-thermo_data(1,tempinv_ind,i_reac)*temp**(-2)/2d0- &
thermo_data(2,tempinv_ind,i_reac)*temp**(-1)+ &
thermo_data(3,tempinv_ind,i_reac)*log(temp)+ &
thermo_data(4,tempinv_ind,i_reac)*temp**(1)+ &
thermo_data(5,tempinv_ind,i_reac)*temp**(2)/2d0+ &
thermo_data(6,tempinv_ind,i_reac)*temp**(3)/3d0+thermo_data(7,tempinv_ind,i_reac)* &
temp**(4)/4d0+thermo_data(10,tempinv_ind,i_reac))*R
END DO
end subroutine ec_CALC_THERMO_QUANTS
!#####################################################
recursive subroutine ec_CALC_EQU_CHEM(N_atoms,N_reactants,names_atoms,names_reactants,molfracs_atoms, &
molfracs_reactants,massfracs_reactants,temp,press,C_P_0, H_0, S_0,names_reactants_orig, &
nabla_ad,gamma2,MMW,rho)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_atoms, N_reactants
CHARACTER*40 :: names_atoms(N_atoms), names_reactants(N_reactants), &
names_reactants_orig(N_reactants)
DOUBLE PRECISION :: molfracs_atoms(N_atoms), molfracs_reactants(N_reactants), &
massfracs_reactants(N_reactants)
DOUBLE PRECISION :: temp, press
DOUBLE PRECISION :: C_P_0(N_reactants), H_0(N_reactants), S_0(N_reactants)
!! CEA McBride 1994 style variables:
DOUBLE PRECISION :: n ! Moles of gas particles per total mass of mixture in kg
DOUBLE PRECISION :: n_spec(N_reactants) ! Moles of species per total mass of mixture in kg
DOUBLE PRECISION :: pi_atom(N_atoms) ! Lagrangian multipliers for the atomic species divided
! by (R*T)
DOUBLE PRECISION :: matrix(N_reactants+N_atoms+1,N_reactants+N_atoms+1)
! So the solution vector will contain the delta log(n_j) for gas, the delta n_j for
! condensed species, the pis and the delta log(n)
DOUBLE PRECISION :: vector(N_reactants+N_atoms+1), &
solution_vector(N_reactants+N_atoms+1), nabla_ad,gamma2,MMW,rho
!! Internal:
INTEGER :: i_iter, i_reac, j_reac, inc_next, &
current_solids_number, N_spec_eff, buffer_ind, i_atom
LOGICAL :: converged, remove_cond, slowed
LOGICAL, allocatable :: solid_inc(:), neg_cond(:)
DOUBLE PRECISION, allocatable :: dgdnj(:)
INTEGER, allocatable :: solid_indices(:), solid_indices_buff(:)
DOUBLE PRECISION :: nsum, mu_gas(N_gas), a_gas(N_gas,N_atoms), mass_species, atom_mass, &
msum
converged = .FALSE.
slowed = .FALSE.
call ec_MAKE_ini_vals(N_reactants,N_atoms,n,n_spec,pi_atom)
iter_max = 50000 + N_reactants/2
current_solids_number = 0
MMW = 0d0
! FIRST: DO GAS ONLY!
DO i_iter = 1, iter_max
IF (quick) THEN
call ec_MAKE_MATRIX_short(N_atoms,names_atoms,molfracs_atoms,N_gas,press,temp,&
C_P_0,H_0,S_0,n,n_spec,pi_atom,matrix(1:N_atoms+1,1:N_atoms+1),vector(1:N_atoms+1),(/1,1,1,1,1/),&
names_reactants, N_reactants, 5, mu_gas,a_gas)
call ec_INV_MATRIX_short(N_atoms+1, &
matrix(1:N_atoms+1,1:N_atoms+1),vector(1:N_atoms+1), &
solution_vector(1:N_atoms+1))
call ec_CHANGE_ABUNDS_short(N_atoms,N_gas,solution_vector(1:N_atoms+1),n_spec,pi_atom,n,converged,&
(/1,1,1,1,1/),5,mu_gas,a_gas,temp,names_atoms,molfracs_atoms)
ELSE
call ec_MAKE_MATRIX_long(N_atoms,names_atoms,molfracs_atoms,N_gas,&
press,temp,C_P_0,H_0,S_0,n,n_spec,pi_atom, &
matrix(1:N_gas+N_atoms+1,1:N_gas+N_atoms+1),vector(1:N_gas+N_atoms+1),&
(/1,1,1,1,1/),names_reactants,N_reactants,5)
call ec_INV_MATRIX_long(N_atoms+N_gas+1, &
matrix(1:N_gas+N_atoms+1,1:N_gas+N_atoms+1),vector(1:N_gas+N_atoms+1), &
solution_vector(1:N_gas+N_atoms+1))
call ec_CHANGE_ABUNDS_long(N_atoms,N_gas,solution_vector(1:N_gas+N_atoms+1), &
n_spec,pi_atom,n,converged,(/1,1,1,1,1/),5,names_atoms,molfracs_atoms)
END IF
IF (verbose) THEN
write(*,*)
write(*,*)
write(*,*) i_iter
DO i_reac = 1, N_reactants
write(*,*) names_reactants(i_reac), n_spec(i_reac)/SUM(n_spec)
END DO
END IF
IF (converged) THEN
EXIT
END IF
END DO
IF (.NOT. converged) THEN
WRITE(*,*)
! STOP 'EASY CHEM: Terminated without convergence!'
print*,'EASY CHEM: Terminated without convergence!'
END IF
converged = .FALSE.
remove_cond = .FALSE.
IF (N_gas .EQ. N_reactants) THEN
call ec_CALC_ADIABATIC_GRADIENT(N_atoms,N_gas,N_reactants,n_spec, &
n,H_0,C_P_0,(/ 1,1,1,1,1 /),5,temp,names_atoms,nabla_ad,gamma2)
END IF
! THEN: INCLUDE CONDENSATES!
IF (N_gas < N_reactants) THEN
allocate(solid_inc(N_reactants-N_gas))
allocate(dgdnj(N_reactants-N_gas))
allocate(solid_indices(N_reactants-N_gas))
allocate(solid_indices_buff(N_reactants-N_gas))
allocate(neg_cond(N_reactants-N_gas))
solid_inc = .FALSE.
inc_next = 0
neg_cond = .FALSE.
N_spec_eff = N_gas
DO WHILE (.NOT. (inc_next .EQ. -1))
call ec_INCLUDE_SOLIDS_QUESTIONMARK(N_reactants,N_atoms,pi_atom,names_atoms,H_0,S_0,dgdnj,temp, &
solid_inc,inc_next,neg_cond)
IF (.NOT. (inc_next .EQ. 0)) THEN
DO i_reac = N_gas+1, N_reactants
IF (n_spec(i_reac) < 0d0) THEN
n_spec(i_reac) = 0d0
inc_next = i_reac
remove_cond = .TRUE.
END IF
END DO
END IF
IF (.NOT. (inc_next .EQ. -1)) THEN
IF (remove_cond) THEN
current_solids_number = current_solids_number - 1
solid_indices_buff = 0
buffer_ind = 1
DO i_reac = 1, N_reactants-N_gas
IF (solid_indices(i_reac) .NE. inc_next) THEN
solid_indices_buff(buffer_ind) = solid_indices(i_reac)
buffer_ind = buffer_ind + 1
END IF
END DO
solid_indices = solid_indices_buff
solid_inc(inc_next-N_gas) = .FALSE.
neg_cond(inc_next-N_gas) = .TRUE.
ELSE
current_solids_number = current_solids_number + 1
solid_indices(current_solids_number) = inc_next
solid_inc(inc_next-N_gas) = .TRUE.
END IF
N_spec_eff = N_gas+current_solids_number
DO i_iter = 1, iter_max
IF (quick) THEN
call ec_MAKE_MATRIX_short(N_atoms,names_atoms,molfracs_atoms,N_spec_eff,press,temp,&
C_P_0,H_0,S_0,n,n_spec,pi_atom, &
matrix(1:N_atoms+1+N_spec_eff-N_gas,1:N_atoms+1+N_spec_eff-N_gas), &
vector(1:N_atoms+1+N_spec_eff-N_gas),solid_indices,&
names_reactants, N_reactants, N_spec_eff-N_gas, mu_gas,a_gas)
call ec_INV_MATRIX_short(N_atoms+1+N_spec_eff-N_gas, &
matrix(1:N_atoms+1+N_spec_eff-N_gas,1:N_atoms+1+N_spec_eff-N_gas), &
vector(1:N_atoms+1+N_spec_eff-N_gas), &
solution_vector(1:N_atoms+1+N_spec_eff-N_gas))
call ec_CHANGE_ABUNDS_short(N_atoms,N_spec_eff,solution_vector(1:N_atoms+1+N_spec_eff-N_gas), &
n_spec,pi_atom,n,converged,&
solid_indices,N_spec_eff-N_gas,mu_gas,a_gas,temp,names_atoms,molfracs_atoms)
ELSE
call ec_MAKE_MATRIX_long(N_atoms,names_atoms,molfracs_atoms,N_spec_eff,&
press,temp,C_P_0,H_0,S_0,n,n_spec,pi_atom, &
matrix(1:N_spec_eff+N_atoms+1,1:N_spec_eff+N_atoms+1),vector(1:N_spec_eff+N_atoms+1),&
solid_indices,names_reactants,N_reactants,N_spec_eff-N_gas)
call ec_INV_MATRIX_long(N_atoms+N_spec_eff+1, &
matrix(1:N_spec_eff+N_atoms+1,1:N_spec_eff+N_atoms+1),vector(1:N_spec_eff+N_atoms+1), &
solution_vector(1:N_spec_eff+N_atoms+1))
call ec_CHANGE_ABUNDS_long(N_atoms,N_spec_eff,solution_vector(1:N_spec_eff+N_atoms+1), &
n_spec,pi_atom,n,converged,&
solid_indices,N_spec_eff-N_gas,names_atoms,molfracs_atoms)
END IF
IF (verbose) THEN
write(*,*)
write(*,*)
write(*,*) i_iter
DO i_reac = 1, N_reactants
write(*,*) names_reactants(i_reac), n_spec(i_reac)/SUM(n_spec)
END DO
END IF
DO i_reac = N_gas+1, N_reactants
IF ((n_spec(i_reac) < 0d0) .AND. (i_iter > 30)) THEN
converged = .TRUE.
EXIT
END IF
END DO
IF (converged) THEN
EXIT
END IF
END DO
IF (.NOT. converged) THEN
WRITE(*,*)
IF (quick) THEN
quick = .FALSE.
call ec_CALC_EQU_CHEM(N_atoms,N_reactants,names_atoms, &
names_reactants,molfracs_atoms, &
molfracs_reactants,massfracs_reactants, &
temp,press,C_P_0, H_0, S_0,names_reactants_orig, &
nabla_ad,gamma2,MMW,rho)
quick = .TRUE.
slowed = .TRUE.
if (verbose) THEN
WRITE(*,*)
write(*,*) 'SLOW!'
END IF
EXIT
ELSE
WRITE(*,*)
! STOP 'EASY CHEM: Terminated without convergence!'
print*,'EASY CHEM: Terminated without convergence!'
END IF
END IF
converged = .FALSE.
END IF
remove_cond = .FALSE.
END DO
! Calc. nabla_ad
IF (.NOT. slowed) THEN
call ec_CALC_ADIABATIC_GRADIENT(N_atoms,N_spec_eff,N_reactants,n_spec, &
n,H_0,C_P_0,solid_indices,N_spec_eff-N_gas,temp,names_atoms,nabla_ad,gamma2)
END IF
deallocate(solid_inc)
deallocate(dgdnj)
deallocate(solid_indices)
deallocate(solid_indices_buff)
deallocate(neg_cond)
END IF
! PREPARE FINAL OUTPUT
IF (.NOT. slowed) THEN
nsum = SUM(n_spec)
DO i_reac = 1, N_reactants
IF (n_spec(i_reac)/nsum < 1d-50) THEN
n_spec(i_reac) = 0d0
END IF
END DO
nsum = SUM(n_spec)
DO i_reac = 1, N_reactants
DO j_reac = 1, N_reactants
IF (names_reactants(j_reac) .EQ. names_reactants_orig(i_reac)) THEN
molfracs_reactants(i_reac) = n_spec(j_reac)/nsum
EXIT
END IF
END DO
END DO
DO i_reac = 1, N_reactants
mass_species = 0d0
DO i_atom = 1, 5
if (trim(adjustl(reac_atoms_names(i_atom,i_reac))) .NE. '') THEN
call ec_ATOM_MASS(reac_atoms_names(i_atom,i_reac),atom_mass)
mass_species = mass_species+atom_mass*DBLE(reac_stoich(i_atom,i_reac))
END IF
END DO
DO j_reac = 1, N_reactants
IF (names_reactants(i_reac) .EQ. names_reactants_orig(j_reac)) THEN
massfracs_reactants(j_reac) = n_spec(i_reac)*mass_species
MMW = MMW + massfracs_reactants(j_reac)/mass_species
EXIT
END IF
END DO
END DO
msum = SUM(massfracs_reactants)
massfracs_reactants = massfracs_reactants / msum
MMW = MMW/msum
MMW = 1d0/MMW
rho = (press*1d6)/kB/temp*MMW
MMW = MMW/amu
END IF
end subroutine ec_CALC_EQU_CHEM
!#####################################################
subroutine ec_MAKE_ini_vals(N_reactants,N_atoms,n,n_spec,pi_atom)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_atoms, N_reactants
DOUBLE PRECISION :: n ! Moles of gas particles per total mass of mixture in kg
DOUBLE PRECISION :: n_spec(N_reactants) ! Moles of species per total mass of mixture in kg
DOUBLE PRECISION :: pi_atom(N_atoms) ! Lagrangian multipliers for the atomic species divided
! by (R*T)
!! Internal:
INTEGER :: i_reac
n = 0.1d0
n_spec = 0d0
pi_atom = 0d0
DO i_reac = 1, N_gas
n_spec(i_reac) = n/DBLE(N_gas)
IF (remove_ions) THEN
IF(reac_ion(i_reac)) THEN
n_spec(i_reac) = 0d0
END IF
END IF
END DO
end subroutine ec_MAKE_ini_vals
!#####################################################
subroutine ec_MAKE_MATRIX_long(N_atoms,names_atoms,molfracs_atoms,N_reactants,press,temp,&
C_P_0,H_0,S_0,n,n_spec,pi_atom,matrix,vector,solid_indices,&
names_reactants, N_reactants2, N_solids)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_atoms, N_reactants, N_reactants2, N_solids
INTEGER :: solid_indices(N_solids)
CHARACTER*40 :: names_atoms(N_atoms), names_reactants(N_reactants2)
DOUBLE PRECISION :: molfracs_atoms(N_atoms), press, temp
DOUBLE PRECISION :: C_P_0(N_reactants2), H_0(N_reactants2), S_0(N_reactants2)
DOUBLE PRECISION :: n ! Moles of gas particles per total mass of mixture in kg
DOUBLE PRECISION :: n_spec(N_reactants) ! Moles of species per total mass of mixture in kg
DOUBLE PRECISION :: pi_atom(N_atoms) ! Lagrangian multipliers for the atomic species divided
! by (R*T)
DOUBLE PRECISION :: matrix(N_reactants+N_atoms+1,N_reactants+N_atoms+1)
! So the solution vector will contain the delta log(n_j) for gas, the delta n_j for
! condensed species, the pis and the delta log(n)
DOUBLE PRECISION :: vector(N_reactants+N_atoms+1)
!! Internal:
DOUBLE PRECISION :: b_0(N_atoms), b_0_norm, mass_atom, b(N_atoms)
DOUBLE PRECISION :: a(N_reactants,N_atoms), mu(N_reactants)
INTEGER :: i_atom, i_reac, i_ratom
CHARACTER*40 :: upper_atom_name
CHARACTER*2 :: upper_ratom_name
! Set up b0
b_0_norm = 0d0
DO i_atom = 1, N_atoms
call ec_ATOM_MASS(names_atoms(i_atom),mass_atom)
b_0_norm = b_0_norm + mass_atom*molfracs_atoms(i_atom)
END DO
b_0 = molfracs_atoms/b_0_norm
! Set up a_ij
a = 0d0
DO i_atom = 1, N_atoms
call To_upper(names_atoms(i_atom),upper_atom_name)
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
a(i_reac,1:N_atoms) = 0d0
CYCLE
END IF
END IF
DO i_ratom = 1, 5
call To_upper(reac_atoms_names(i_ratom,i_reac),upper_ratom_name)
IF (trim(adjustl(upper_atom_name)) .EQ. &
trim(adjustl(upper_ratom_name))) THEN
a(i_reac,i_atom) = reac_stoich(i_ratom,i_reac)*mol
END IF
END DO
END DO
DO i_reac = N_gas+1, N_reactants
DO i_ratom = 1, 5
call To_upper(reac_atoms_names(i_ratom,solid_indices(i_reac-N_gas)),upper_ratom_name)
IF (trim(adjustl(upper_atom_name)) .EQ. &
trim(adjustl(upper_ratom_name))) THEN
a(i_reac,i_atom) = reac_stoich(i_ratom,solid_indices(i_reac-N_gas))*mol
END IF
END DO
END DO
END DO
! Set up mu_j
DO i_reac = 1, N_reactants
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
mu(i_reac) = 0d0
CYCLE
END IF
END IF
! Taken from Venot et al. (2012), in comparison with McBride 1996.
IF (i_reac <= N_gas) THEN
mu(i_reac) = H_0(i_reac) - temp*S_0(i_reac)
IF (n_spec(i_reac) > 1d-290) THEN
mu(i_reac) = mu(i_reac) + R*temp*log(n_spec(i_reac)/n)+R*temp*log(press)
ELSE
IF (verbose) THEN
write(*,*) 'n_spec(i_reac) == 0 for '//trim(adjustl(names_reactants(i_reac)))// &
' set to 1d-13 and try again.'
END IF
call RANDOM_NUMBER(n_spec(i_reac))
n_spec(i_reac) = n_spec(i_reac)*1d-13
mu(i_reac) = mu(i_reac) + R*temp*log(n_spec(i_reac)/n)+R*temp*log(press)
END IF
ELSE
mu(i_reac) = H_0(solid_indices(i_reac-N_gas)) - temp*S_0(solid_indices(i_reac-N_gas))
END IF
END DO
! MATRIX SETUP
matrix = 0d0
! Set up the matrix for the N_gas equations (Eq. 2.18)
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(i_reac,i_reac) = 1d0
DO i_atom = 1, N_atoms
matrix(i_reac,N_reactants+i_atom) = -a(i_reac,i_atom)
END DO
matrix(i_reac,N_reactants+N_atoms+1) = -1d0
END DO
! Set up the matrix for the N_reactants-N_gas equations (Eq. 2.19)
IF (N_gas < N_reactants) THEN
DO i_reac = N_gas+1, N_reactants
DO i_atom = 1, N_atoms
matrix(i_reac,N_reactants+i_atom) = -a(i_reac,i_atom)
END DO
END DO
END IF
! Set up the matrix for the N_atom equations (Eq. 2.20)
DO i_atom = 1, N_atoms
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(N_reactants+i_atom,i_reac) = a(i_reac,i_atom)*n_spec(i_reac)
END DO
IF (N_gas < N_reactants) THEN
DO i_reac = N_gas+1, N_reactants
matrix(N_reactants+i_atom,i_reac) = a(i_reac,i_atom)
END DO
END IF
END DO
! Set up the matrix for the last equation (Eq. 2.21)
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(N_reactants+N_atoms+1,i_reac) = n_spec(i_reac)
END DO
matrix(N_reactants+N_atoms+1,N_reactants+N_atoms+1) = -n
! VECTOR SETUP
!vector(N_reactants+N_atoms+1)
vector = 0d0
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
vector(i_reac) = -mu(i_reac)/R/temp ! (Eq. 2.18)
END DO
IF (N_gas < N_reactants) THEN
vector(N_gas+1:N_reactants) = -mu(N_gas+1:N_reactants)/R/temp ! (Eq. 2.19)
END IF
b = 0d0
DO i_atom = 1, N_atoms
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
b(i_atom) = b(i_atom) + a(i_reac,i_atom)*n_spec(i_reac)
END DO
DO i_reac = N_gas+1, N_reactants
b(i_atom) = b(i_atom) + a(i_reac,i_atom)*n_spec(solid_indices(i_reac-N_gas))
END DO
END DO
vector(N_reactants+1:N_reactants+N_atoms) = b_0 - b ! (Eq. 2.20)
vector(N_reactants+N_atoms+1) = n - SUM(n_spec(1:N_gas)) ! (Eq. 2.21)
end subroutine ec_MAKE_MATRIX_long
!#####################################################
subroutine ec_MAKE_MATRIX_short(N_atoms,names_atoms,molfracs_atoms,N_reactants,press,temp,&
C_P_0,H_0,S_0,n,n_spec,pi_atom,matrix,vector,solid_indices,&
names_reactants, N_reactants2, N_solids, mu_gas,a_gas)
use thermo_data_block
implicit none
!! I/O:
INTEGER :: N_atoms, N_reactants, N_reactants2, N_solids
INTEGER :: solid_indices(N_solids)
CHARACTER*40 :: names_atoms(N_atoms), names_reactants(N_reactants2)
DOUBLE PRECISION :: molfracs_atoms(N_atoms), press, temp
DOUBLE PRECISION :: C_P_0(N_reactants), H_0(N_reactants), S_0(N_reactants)
DOUBLE PRECISION :: n ! Moles of gas particles per total mass of mixture in kg
DOUBLE PRECISION :: n_spec(N_reactants) ! Moles of species per total mass of mixture in kg
DOUBLE PRECISION :: pi_atom(N_atoms) ! Lagrangian multipliers for the atomic species divided
! by (R*T)
DOUBLE PRECISION :: matrix(N_atoms+1+(N_reactants-N_gas),N_atoms+1+(N_reactants-N_gas))
! So the solution vector will contain the delta log(n_j) for gas, the delta n_j for
! condensed species, the pis and the delta log(n)
DOUBLE PRECISION :: vector(N_atoms+1+(N_reactants-N_gas))
DOUBLE PRECISION :: mu_gas(N_gas), a_gas(N_gas,N_atoms)
!! Internal:
DOUBLE PRECISION :: b_0(N_atoms), b_0_norm, mass_atom, b(N_atoms)
DOUBLE PRECISION :: a(N_reactants,N_atoms), mu(N_reactants)
INTEGER :: i_atom, i_reac, i_ratom, i_atom2
CHARACTER*40 :: upper_atom_name
CHARACTER*2 :: upper_ratom_name
! Set up b0
b_0_norm = 0d0
DO i_atom = 1, N_atoms
call ec_ATOM_MASS(names_atoms(i_atom),mass_atom)
b_0_norm = b_0_norm + mass_atom*molfracs_atoms(i_atom)
END DO
b_0 = molfracs_atoms/b_0_norm
! Set up a_ij
a = 0d0
DO i_atom = 1, N_atoms
call To_upper(names_atoms(i_atom),upper_atom_name)
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
a(i_reac,1:N_atoms) = 0d0
CYCLE
END IF
END IF
DO i_ratom = 1, 5
call To_upper(reac_atoms_names(i_ratom,i_reac),upper_ratom_name)
IF (trim(adjustl(upper_atom_name)) .EQ. &
trim(adjustl(upper_ratom_name))) THEN
a(i_reac,i_atom) = reac_stoich(i_ratom,i_reac)*mol
END IF
END DO
END DO
DO i_reac = N_gas+1, N_reactants
DO i_ratom = 1, 5
call To_upper(reac_atoms_names(i_ratom,solid_indices(i_reac-N_gas)),upper_ratom_name)
IF (trim(adjustl(upper_atom_name)) .EQ. &
trim(adjustl(upper_ratom_name))) THEN
a(i_reac,i_atom) = reac_stoich(i_ratom,solid_indices(i_reac-N_gas))*mol
END IF
END DO
END DO
END DO
! Set up mu_j
DO i_reac = 1, N_reactants
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
mu(i_reac) = 0d0
CYCLE
END IF
END IF
! Taken from Venot et al. (2012), in comparison with McBride 1996.
IF (i_reac <= N_gas) THEN
mu(i_reac) = H_0(i_reac) - temp*S_0(i_reac)
IF (n_spec(i_reac) > 1d-290) THEN
mu(i_reac) = mu(i_reac) + R*temp*log(n_spec(i_reac)/n)+R*temp*log(press)
ELSE
IF (verbose) THEN
write(*,*) 'n_spec(i_reac) == 0 for '//trim(adjustl(names_reactants(i_reac)))// &
' set to 1d-13 and try again.'
END IF
call RANDOM_NUMBER(n_spec(i_reac))
n_spec(i_reac) = n_spec(i_reac)*1d-13
mu(i_reac) = mu(i_reac) + R*temp*log(n_spec(i_reac)/n)+R*temp*log(press)
END IF
ELSE
mu(i_reac) = H_0(solid_indices(i_reac-N_gas)) - temp*S_0(solid_indices(i_reac-N_gas))
END IF
END DO
a_gas = a(1:N_gas,1:N_atoms)
mu_gas = mu(1:N_gas)
! MATRIX SETUP
matrix = 0d0
! Set up the matrix for the N_atoms equations (Eq. 2.24)
DO i_atom = 1, N_atoms
DO i_atom2 = 1, N_atoms
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(i_atom,i_atom2) = matrix(i_atom,i_atom2) + &
a(i_reac,i_atom)*a(i_reac,i_atom2)*n_spec(i_reac)
END DO
END DO
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(i_atom,N_atoms+1) = matrix(i_atom,N_atoms+1) + &
a(i_reac,i_atom)*n_spec(i_reac)
END DO
IF (N_gas < N_reactants) THEN
DO i_reac = N_gas+1, N_reactants
matrix(i_atom,N_atoms+1+i_reac-N_gas) = a(i_reac,i_atom)
END DO
END IF
END DO
! Set up the matrix for the equation (Eq. 2.26)
DO i_atom = 1, N_atoms
DO i_reac = 1, N_gas
IF (remove_ions) THEN
IF (reac_ion(i_reac)) THEN
CYCLE
END IF
END IF
matrix(N_atoms+1,i_atom) = matrix(N_atoms+1,i_atom) + &
a(i_reac,i_atom)*n_spec(i_reac)
END DO
END DO
DO i_reac = 1, N_gas
IF (remove_ions) THEN