-
Notifications
You must be signed in to change notification settings - Fork 0
/
phases.py
6675 lines (5496 loc) · 229 KB
/
phases.py
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
"""
This module provides Python wrappers to individual and collections of phases.
"""
from thermoengine import core
from thermoengine import chem
import numpy as np
import pandas as pd
from os import path
from collections import OrderedDict
from abc import ABCMeta, abstractmethod
from importlib import import_module, invalidate_caches
import sys
import deprecation
from functools import wraps
# specialized numerical imports
from scipy import optimize as optim
from typing import Type, List
# __all__ = ['Rxn','Assemblage','PurePhase','SolutionPhase','get_phaselist']
__all__ = ['Rxn','Assemblage','Phase','PurePhase','SolutionPhase','get_phaselist']
DATADIR = 'data/phases'
PURE_PHASE_FILE = 'PurePhaseList.csv'
SOLUTION_PHASE_FILE = 'SolutionPhaseList.csv'
# inputs
#
# * P=1, T=1, mol=1
# * P=N_PTX, T=1, mol=1
# * P=1, T=N_PTX, mol=1
# * P=1, T=1, mol = N_PTX
# * P=N_PTX, T=N_PTX, mol=N_PTX
#
# qty
#
# * N_endmem (deriv_type)
# * N_rxn
# * N_phase
#
# outputs
#
# * N_PTX,
# * N_PTX, N_endmem
# * N_PTX, N_endmem, N_endmem
#
# * N_phase,
# * N_rxn, N_phase
# * N_rxn, N_phase, N_PTX
#
# * reduce dimensions if N_PTX=1
#===================================================
def get_phase_info():
"""
Get acceptable table of basic phase info
(e.g., name, abbrev, formula, members, etc.).
Returns
-------
phase_table : dict
Dictionary containing phase information.
- 'purephases' : pandas dataframe
- 'purefilenm' : str defining source file for pure phase info
"""
phase_info = {}
phase_info['pure'] = _read_phase_info(PURE_PHASE_FILE)
phase_info['solution'] = _read_phase_info(SOLUTION_PHASE_FILE)
info_files = {}
info_files['pure'] = PURE_PHASE_FILE
info_files['solution'] = SOLUTION_PHASE_FILE
return phase_info, info_files
def _read_phase_info(filenm):
"""
Read phase info tables
Internal method to read phase abbreviations and names from csv files.
"""
parentpath = path.dirname(__file__)
pathname = path.join(parentpath,DATADIR,filenm)
try:
phases_info_df = pd.read_csv(pathname)
except:
assert False,'The '+pathname+' file cannot be found. '\
'It is needed to define the standard phase abbreviations.'
return phases_info_df
#===================================================
class FixedRxnSet:
def __init__(self, phase_symbols, endmember_ids, rxn_coefs,
phases_dict, T, P, mols,):
self._init_rxn_set(phase_symbols, endmember_ids, rxn_coefs,
phases_dict)
self._init_exp_cond(T, P, mols)
def _init_rxn_set(self, phase_symbols, endmember_ids, rxn_coefs,
phases_dict):
assert np.all(
[sym in phases_dict.keys() for sym in phase_symbols ]), (
'phase symbols must appear in phase_obj_dict'
)
self._phase_symbols = phase_symbols
self._endmember_ids = endmember_ids
self._rxn_coefs = rxn_coefs
self._phases_dict = phases_dict
self._rxn_num = rxn_coefs.shape[0]
self._endmember_num = rxn_coefs.shape[1]
def _init_exp_cond(self, T, P, mols):
phase_symbols = self._phase_symbols
# validate mols
self._validate_mol_input(mols)
assert np.all(
[sym in phase_symbols for sym in mols.keys()]),(
'molar composition must be defines for every phase'
)
self._T = T
self._P = P
self._mols = mols
def _validate_mol_input(self, mols):
mols = {} if mols is None else mols
phases = self.phases
phase_symbols = self.phase_symbols
for symbol in phase_symbols:
if symbol not in mols:
mols[symbol] = None
assert np.all(np.array(
[symbol in phase_symbols for symbol in mols.keys()])), (
'Invalid phase symbol(s) used to define phase '
'composition in mols. Only use valid phase symbols '
'found in rxn.phase_symbols.'
)
return mols
def affinity(self):
rxn_coefs = self._rxn_coefs
phase_chem_potentials = self.phase_chem_potentials()
chem_potentials = self.endmem_chem_potentials(
phase_chem_potentials)
affinities = np.dot(rxn_coefs, chem_potentials)
return affinities
def endmem_chem_potentials(self, phase_chem_potentials):
endmember_num = self._endmember_num
phase_symbols = self._phase_symbols
endmember_ids = self._endmember_ids
chem_potentials = zeros(endmember_num)
for ind, (phs_sym, endmem_id) in enumerate(
zip(phase_symbols, endmember_ids)):
chem_potentials[ind] = phase_chem_potentials[phs_sym][endmem_id]
return chem_potentials
def phase_chem_potentials(self):
phases_dict = self._phases_dict
T = self._T
P = self._P
mols = self._mols
phase_chem_potentials = {}
for phs_sym in phases_dict:
phs = phases_dict[phs_sym]
phase_chem_potentials[phs_sym] = phs.chem_pot(T, P, mols=mols)
return phase_chem_potentials
#===================================================
class Rxn:
"""
Class that defines identity/properties of a specific phase reaction.
Reactions occur between phases (either pure or solution) and are
defined in terms of the participating endmembers, indicating which atoms
are exchanged between phases during the reaction.
Parameters
----------
phase_objs : array of Phase Objects
Defines which phases participate in the reaction.
endmember_ids : int array
Indicates the endmember of each phase that participates in the reaction.
This array must have the same order as the phase array (phase_objs).
rxn_coefs : double array
Defines the stoichiometric rxn coefficient, where negative values
are reactants and positive values are products. The reaction must be
balanced (obeying mass conservation).
This array must have the same order as the phase array (phase_objs).
Attributes
----------
endmember_ids
endmember_names
phase_num
phase_symbols
phases
product_phases
reactant_phases
rxn_coefs
Notes
-----
* The phases themselves may be pure or have realistic
intermediate compositions (if they are solution phases).
* The reaction is defined in terms of the exchange of endmembers between
the participating phases.
* Reaction coefficients correspond to a balanced stoichiometric reaction.
"""
def __init__(self, phase_objs, endmember_ids, rxn_coefs,
coefs_per_atom=False):
self._validate_inputs(phase_objs, endmember_ids, rxn_coefs)
self._init_rxn(phase_objs, endmember_ids, rxn_coefs,
coefs_per_atom)
# TK
# self._validate_rxn_balance
pass
def _validate_inputs(self, phase_objs, endmember_ids, rxn_coefs):
Nphase = len(phase_objs)
rxn_coefs = np.array(rxn_coefs)
assert len(endmember_ids) == Nphase, (
'endmember_ids must provide endmember index for '
'every reaction phase. '
)
assert len(rxn_coefs) == Nphase, (
'rxn_coefs must provide stoichiometric '
'coefficients for every reaction phase.'
)
assert np.any(rxn_coefs<0), (
'rxn_coefs must indicate reactants with negative '
'sign and products with positive sign. Currently, '
'none of the stoichiometric coefficients are negative.'
)
assert np.any(rxn_coefs>0), (
'rxn_coefs must indicate reactants with negative '
'sign and products with positive sign. Currently, '
'none of the stoichiometric coefficients are positive.'
)
def _init_rxn(self, phase_objs, endmember_ids, rxn_coefs, coefs_per_atom):
phase_objs, endmember_ids, rxn_coefs = (
self._trim_absent_rxn_phases(
phase_objs, endmember_ids, rxn_coefs))
phase_objs = np.array(phase_objs)
endmember_ids = np.array(endmember_ids)
Nphase = len(phase_objs)
rxn_coefs = np.array(rxn_coefs)
all_atom_nums = []
for phs, endmember_id in zip(phase_objs, endmember_ids):
endmember_id = int(endmember_id)
iatom_num = phs.props['atom_num'][endmember_id]
all_atom_nums.append(iatom_num)
all_atom_nums = np.array(all_atom_nums)
if coefs_per_atom:
rxn_coefs = rxn_coefs/all_atom_nums
phase_symbols = []
endmember_names = []
for phase_obj, iendmember_id in zip(phase_objs, endmember_ids):
phase_symbols.append(
phase_obj.props['abbrev'])
endmember_names.append(
phase_obj.endmember_names[int(iendmember_id)])
phase_symbols = np.array(phase_symbols)
# indsort = np.argsort(phase_symbols)
# self._set_phase_assemblage(
# phase_objs[indsort], phase_symbols[indsort],
# obj_is_classnm=obj_is_classnm)
self._phase_num = Nphase
self._phase_symbols = phase_symbols
self._phases = phase_objs
self._endmember_ids = endmember_ids
self._endmember_names = endmember_names
self._rxn_coefs = rxn_coefs
def _trim_absent_rxn_phases(self, phase_objs, endmember_ids, rxn_coefs):
remove_set, = np.where(rxn_coefs == 0)
# re-create lists for items not in remove set
rxn_coefs = [v for i, v in enumerate(rxn_coefs)
if i not in remove_set]
phase_objs = [v for i, v in enumerate(phase_objs)
if i not in remove_set]
endmember_ids = [v for i, v in enumerate(endmember_ids)
if i not in remove_set]
return phase_objs, endmember_ids, rxn_coefs
@property
def phase_num(self):
"""
Number of phases
Returns
-------
Number of phases (int)
"""
return self._phase_num
@property
def phases(self):
"""
Phase objects used in the reaction
Returns
-------
Array of phase objects used in the reaction
"""
return self._phases
@property
def reactant_phases(self):
"""
Reactant phases
Returns
-------
Array of reactant phase objects
"""
return self.phases[self.rxn_coefs<0]
@property
def product_phases(self):
"""
Product phases
Returns
-------
Array of product phase objects
"""
return self.phases[self.rxn_coefs>0]
@property
def phase_symbols(self):
"""
Phase symbols
Returns
-------
Array of phase symbols used in the reaction (str)
"""
return self._phase_symbols
@property
def endmember_ids(self):
"""
ID number of each endmember in phase
Returns
-------
Array of ids, [int,...]
"""
return self._endmember_ids
@property
def endmember_names(self):
"""
Name of each endmember
Returns
-------
List of endmember names for this solution phase, [str,...]
"""
return self._endmember_names
@property
def rxn_coefs(self):
"""
Reaction coefficients
Returns
-------
Array of reaction coefficients (double)
"""
return self._rxn_coefs
def _validate_mol_input(self, mols):
mols = {} if mols is None else mols
phases = self.phases
phase_symbols = self.phase_symbols
for symbol in phase_symbols:
if symbol not in mols:
mols[symbol] = None
assert np.all(np.array(
[symbol in phase_symbols for symbol in mols.keys()])), (
'Invalid phase symbol(s) used to define phase '
'composition in mols. Only use valid phase symbols '
'found in rxn.phase_symbols.'
)
return mols
def affinity(self, T, P, mols=None):
"""
Calculate reaction affinity
Parameters
----------
T : array-like
Temperature in Kelvin
P : array-like
Pressure in bars
mol : dict of arrays, optional
Composition of each phase in terms of mols of endmembers
(unneeded for pure phases)
Returns
-------
value : array-like
Reaction affinity in J
"""
affinity = -self.chem_potential(T, P, mols=mols)
return affinity
def chem_potential(self, T, P, mols=None):
"""
Calculate net chemical potential change of the reaction
Parameters
----------
T : array-like
Temperature in Kelvin
P : array-like
Pressure in bars
mol : dict of arrays, optional
Composition of each phase in terms of mols of endmembers
(unneeded for pure phases)
Returns
-------
value : array-like
Chemical potential in J for the net change of the reaction
"""
chem_potential = self._calc_net_rxn_values(
'chem_potential', T, P, mols=mols, use_endmember=True)
return chem_potential
def volume(self, T, P, mols=None, peratom=False):
"""
Calculate net volume change of the reaction
Parameters
----------
T : array-like
Temperature in Kelvin
P : array-like
Pressure in bars
mol : dict of arrays, optional
Composition of each phase in terms of mols of endmembers
(unneeded for pure phases)
Returns
-------
value : array-like
Volume in J/bar for the net change of the reaction
"""
rxn_volume = self._calc_net_rxn_values('volume', T, P, mols=mols,
peratom=peratom)
return rxn_volume
def entropy(self, T, P, mols=None, peratom=False):
"""
Calculate net entropy change of the reaction
Parameters
----------
T : array-like
Temperature in Kelvin
P : array-like
Pressure in bars
mol : dict of arrays, optional
Composition of each phase in terms of mols of endmembers
(unneeded for pure phases).
Returns
-------
value : array-like
Entropy in J/K for the net change of the reaction.
"""
rxn_entropy = self._calc_net_rxn_values('entropy', T, P, mols=mols,
peratom=peratom)
return rxn_entropy
def boundary(self, T=None, P=None, mols=None, init_guess=None):
Tstd = 300.0
Pstd = 1.0
assert (T is not None) or (P is not None), \
'Both T and P are None; Must define either temperature or pressure.'
if T is not None:
if init_guess is None:
init_guess = Pstd
Afun = lambda P, T=T: self.affinity(T, P, mols=mols)
dAfun = lambda P, T=T: -self.volume(T, P, mols=mols, peratom=True)
else:
if init_guess is None:
init_guess = Tstd
Afun = lambda T, P=P: self.affinity(T, P, mols=mols)
dAfun = lambda T, P=P: +self.entropy(T, P, mols=mols, peratom=True)
value_bound = optim.newton(Afun, init_guess, fprime=dAfun)
return value_bound
def clapeyron_slope(self, T, P, mols=None, peratom=False):
dV_rxn = self.volume(T, P, mols=mols, peratom=peratom)
dS_rxn = self.entropy(T, P, mols=mols, peratom=peratom)
dTdP = dV_rxn / dS_rxn
return dTdP
def simultaneous_rxn_cond(self, other_rxn, Pinit=1.0, TOL=1e-5):
P = Pinit
while True:
Tbnd1 = self.boundary(P=P)
Tbnd2 = other_rxn.boundary(P=P)
dTdP1 = self.clapeyron_slope(Tbnd1,P)
dTdP2 = other_rxn.clapeyron_slope(Tbnd2,Pinit)
if np.abs(np.log(Tbnd1/Tbnd2)) < TOL:
T = 0.5*(Tbnd1+Tbnd2)
break
dP = - (Tbnd1 - Tbnd2)/(dTdP1-dTdP2)
P += dP
T = float(T)
P = float(P)
return T, P
def trace_boundary(self, Tlims=None, Plims=None, init_guess=None,
Nsamp=30):
assert (Tlims is not None) or (Plims is not None), \
'Both T and P are None; Must define either temperature or pressure.'
if Tlims is not None:
xvals = np.linspace(Tlims[0], Tlims[1], Nsamp)
bound_fun = lambda T, init_guess: self.boundary(
T=T, init_guess=init_guess)
dAdx_fun = lambda x, y: +self.entropy(x, y)
dAdy_fun = lambda x, y: -self.volume(x, y)
else:
xvals = np.linspace(Plims[0], Plims[1], Nsamp)
bound_fun = lambda P, init_guess: self.boundary(
P=P, init_guess=init_guess)
dAdx_fun = lambda x, y: -self.volume(y, x)
dAdy_fun = lambda x, y: +self.entropy(y, x)
dx = xvals[1]-xvals[0]
yvals = np.zeros(xvals.shape)
for ind, x in enumerate(xvals):
y = bound_fun(x, init_guess)
dy_guess = dx*dAdx_fun(x=x, y=y)/dAdy_fun(x=x, y=y)
yvals[ind] = y
init_guess = y + dy_guess
if Tlims is not None:
T_bnds, P_bnds = xvals, yvals
else:
T_bnds, P_bnds = yvals, xvals
return T_bnds, P_bnds
def competing_rxn(self, T_bounds, P_bounds, other_rxn, TOL=1e-6):
competing = False
A_rxn_other = other_rxn.affinity(T_bounds, P_bounds)
is_competing = np.tile(False, len(T_bounds))
if np.all(other_rxn.reactant_phases in self.phases):
is_competing[A_rxn_other>TOL] = True
if np.all(other_rxn.product_phases in self.phases):
is_competing[A_rxn_other<-TOL] = True
return is_competing
def stability(self, T_bounds, P_bounds, other_rxns, TOL=1e-6):
# NOTE: other_rxns must be a list of reactions of the same composition
peratom=True
peratom=False
# self._reac_assemblage
# A_rxn_vals = self.affinity(T_bounds, P_bounds, peratom=peratom)
A_rxn_vals = self.affinity(T_bounds, P_bounds)
assert np.all(np.abs(A_rxn_vals)<TOL), \
'Rxn affinity must be equal to zero to within TOL.'
N_TP = A_rxn_vals.size
N_rxn = len(other_rxns)
# A_rxn_others = np.zeros((N_rxn, N_TP))
# Calculate gibbs energy of rxn for all possible reactions
stable = np.tile(True, N_TP)
for ind, irxn in enumerate(other_rxns):
is_competing = self.competing_rxn(T_bounds, P_bounds, irxn)
stable[is_competing] = False
# iA_rxn_other = irxn.affinity(T_bounds, P_bounds, peratom=peratom )
# # if other rxn phase assemblage is a subset of the current
# # assemblage, then it is a valid competing reaction
# if self.competing_rxn(irxn):
# if (rxn._reac_assemblage.issubset(self._reac_assemblage) | \
# rxn._reac_assemblage.issubset(self._prod_assemblage) ):
# G_rxn_other_a[ind] = iG_rxn_other_a
# elif (rxn._prod_assemblage.issubset(self._reac_assemblage) | \
# rxn._prod_assemblage.issubset(self._prod_assemblage) ):
# # Store negative energy as reverse reaction energy change
# G_rxn_other_a[ind] = -iG_rxn_other_a
# else:
# G_rxn_other_a[ind] = 0.0
# print(G_rxn_other_a)
# If any rxn is energetically favored, then current reaction is not
# stable
# stable_a = ~np.any(G_rxn_other_a < -TOL, axis=0)
return stable
def _validate_state_input(self, T, P, mols):
N_T = np.asarray(T).size
N_P = np.asarray(P).size
N_solution_phases = len(mols)
N_X = np.ones(N_solution_phases, dtype=int)
for ind, iphs in enumerate(mols):
imol = mols[iphs]
if imol is None:
N_X[ind] = 0
else:
imol = np.asarray(imol)
if imol.ndim==1:
N_X[ind] = 1
else:
N_X[ind] = imol.shape[0]
assert np.all(N_X<=1), (
'Multiple compositions not currently supported.'
)
N_all = np.hstack((N_T, N_P, N_X))
N_PTX = np.max(N_all)
assert np.all(np.isin(N_all, [0, 1, N_PTX])),(
'The number of state points for T, P, mol must be compatible.'
)
state = {}
state['N_T'] = N_T
state['N_P'] = N_P
state['N_X'] = N_X
state['N_PTX'] = N_PTX
# from IPython import embed;embed();import ipdb as pdb;pdb.set_trace()
T = np.tile(np.asarray(T), N_PTX) if N_T < N_PTX else T
P = np.tile(np.asarray(P), N_PTX) if N_P < N_PTX else P
state['T'] = T
state['P'] = P
state['mols'] = mols
return state
def _calc_net_rxn_values(self, method_name, T, P, mols=None,
peratom=False, use_endmember=False):
mols = self._validate_mol_input(mols)
phase_num = self.phase_num
endmember_ids = self.endmember_ids
rxn_coefs = self.rxn_coefs
state = self._validate_state_input(T, P, mols)
T, P, mols, N_PTX = (state.get(key) for
key in ['T', 'P', 'mols', 'N_PTX'])
values = np.zeros((phase_num, N_PTX))
for i, (iphs, iendmember) in enumerate(zip(
self.phases, endmember_ids)):
iphs_abbrev = iphs.abbrev
imol = mols[iphs_abbrev]
iphs_method = getattr(iphs, method_name)
if use_endmember:
ival = iphs_method(T, P, mol=imol, endmember=iendmember)
else:
ival = iphs_method(T, P, mol=imol)
# if peratom:
# ival /= self.rxn_atomnum
# from IPython import embed;embed();import ipdb as pdb;pdb.set_trace()
values[i] = ival
net_rxn_values = np.dot(rxn_coefs, values)
#from IPython import embed;embed();import pdb as pdb;pdb.set_trace()
return net_rxn_values
######################
# NOT UPDATED #
######################
def gibbs_energy( self, T_a, P_a, peratom=False ):
dG_rxn_a = self._calc_rxn_change('gibbs_energy_all', T_a, P_a,
peratom=peratom)
return dG_rxn_a
def enthalpy( self, T_a, P_a, peratom=False ):
dH_rxn_a = self._calc_rxn_change('enthalpy_all', T_a, P_a,
peratom=peratom )
return dH_rxn_a
def _calc_reac_value( self, method_name, T_a, P_a, peratom=False ):
reac_method = getattr(self.reac_assemblage, method_name)
val_phs_a = reac_method( T_a, P_a )
val_a = np.dot(self.reac_rxn_coef_a, val_phs_a)
# if peratom:
# val_a /= self.rxn_atomnum
return val_a
def _calc_prod_value( self, method_name, T_a, P_a, peratom=False ):
#prod_method = getattr(self.prod_assemblage, method_name)
prod_method = getattr(self.product_phases, method_name)
val_phs_a = prod_method( T_a, P_a )
val_a = np.dot(self.prod_rxn_coef_a, val_phs_a)
#
# if peratom:
# #
# val_a /= self.rxn_atomnum
return val_a
def _calc_rxn_change( self, method_name, T_a, P_a, peratom=False ):
val_prod_a = self._calc_prod_value( method_name, T_a, P_a, peratom=peratom )
val_reac_a = self._calc_reac_value( method_name, T_a, P_a, peratom=peratom )
val_rxn_a = val_prod_a-val_reac_a
return val_rxn_a
#===================================================
class Assemblage:
def __init__(self, phase_objs, obj_is_classnm=False):
# Get phase symbol list
phase_symbols = []
for phase_obj in phase_objs:
phase_symbols.append(
phase_obj.props['abbrev'])
phase_objs = np.array(phase_objs)
phase_symbols = np.array(phase_symbols)
indsort = np.argsort(phase_symbols)
self._set_phase_assemblage(
phase_objs[indsort], phase_symbols[indsort],
obj_is_classnm=obj_is_classnm)
pass
def __eq__(self, other):
return self._phases == other._phases
def __lt__(self, other):
if len(self._phases) < len(other._phases):
return True
return self._phases[0] < other._phases[0]
def __gt__(self, other):
if len(self._phases) > len(other._phases):
return True
return self._phases[0] > other._phases[0]
def issubset(self, other):
is_member = [phase in other._phases for phase in self._phases]
return np.all(is_member)
def _set_phase_assemblage(self, phase_objs,
phase_symbols,
obj_is_classnm=False):
if obj_is_classnm:
phase_classnms = phase_objs
phase_objs = [
PurePhase(phase_classnm, phasesym)
for (phase_classnm, phasesym) in
zip(phase_classnms, phase_symbols)]
self._phase_symbols = phase_symbols
self._phases = phase_objs
props = {}
props['formula_all'] = [phs.props['formula']
for phs in phase_objs]
props['name_all'] = [phs.props['phase_name']
for phs in phase_objs]
props['abbrev_all'] = [phs.props['abbrev']
for phs in phase_objs ]
props['molwt_all'] = np.array([
phs.props['molwt'] for phs in phase_objs])
props['element_comp_all'] = np.vstack([
phs.props['element_comp'] for phs
in phase_objs])
# NOTE replace symbols?
# props['element_symbols_all'] = phase_objs[0].props['element_symbols']
self._props = props
pass
def _validate_mol_input(self, mols):
phases = self.phases
phase_symbols = self.phase_symbols
for symbol in phase_symbols:
if symbol not in mols:
mols[symbol] = None
# else:
# imol = mols[symbol]
# iphase = phases[symbol]
# iprops = iphase.props
# iendmember_names = iprops['endmember_name']
# iendmember_num = len(iendmember_names)
return mols
@property
def phase_symbols(self):
return self._phase_symbols
@property
def phases(self):
return self._phases
@property
def props(self):
return self._props
def get_endmember_comp_matrix(self):
oxide_num = chem.oxide_props['oxide_num']
all_mol_oxide_comp = np.zeros((0,oxide_num))
all_phase_name = np.zeros((0))
all_endmember_name = np.zeros((0))
all_endmember_ind = np.zeros((0))
all_phase_ind = np.zeros((0))
for iphase in self.phases:
iphs_props = iphase.props
imol_oxide_comp = iphs_props['mol_oxide_comp']
iendmember_name = iphs_props['endmember_name']
iendmember_num = len(iendmember_name)
iendmember_ind = np.arange(iendmember_num)
iphase_name_tile = np.tile(np.array([iphs_props['phase_name']]), iendmember_num)
all_phase_ind = np.hstack((all_phase_ind, np.tile(ind_phs,(iendmember_num))))
all_mol_oxide_comp= np.vstack((all_mol_oxide_comp, iall_mol_oxide_comp))
all_phase_name = np.hstack((all_phase_name, iphase_name_tile))
all_endmember_name = np.hstack((all_endmember_name, iendmember_name))
all_endmember_ind = np.hstack((all_endmember_ind, iendmember_ind))
return all_mol_oxide_comp
def gibbs_energy_all(self, T, P, mols=None):
mols = self._validate_mol_input(mols)
gibbs_energy_all = []
for iphs in self.phases:
iphs_abbrev = iphs.abbrev
imol = mols[iphs_abbrev]
igibbs_energy = iphs.gibbs_energy(
T, P, mol=imol)
gibbs_energy_all.append(igibbs_energy)
gibbs_energy_all = np.vstack(gibbs_energy_all)
return gibbs_energy_all
def chem_potential_all(self, T, P, mols=None):
mols = self._validate_mol_input(mols)
chem_potential_all = []
for iphs in self.phases:
iphs_abbrev = iphs.abbrev
imol = mols[iphs_abbrev]
ichem_potential = iphs.chem_potential(
T, P, mol=imol)
chem_potential_all.append(ichem_potential)
chem_potential_all = np.vstack(chem_potential_all)
return chem_potential_all
def enthalpy_all(self, T, P):
return np.vstack([phs.enthalpy(T, P)
for phs in self.phases])
def entropy_all(self, T, P):
return np.vstack([phs.entropy(T, P)
for phs in self.phases])
def heat_capacity_all(self, T, P):
return np.vstack([phs.heat_capacity(T, P)
for phs in self.phases])
def dCp_dT_all(self, T, P):
return np.vstack([phs.dCp_dT(T, P)
for phs in self.phases])
def volume_all(self, T, P):
return np.vstack([phs.volume(T, P)
for phs in self.phases])
def dV_dT_all(self, T, P):
return np.vstack([phs.dV_dT(T, P)
for phs in self.phases])