forked from pycalphad/pycalphad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
constraints.py
50 lines (44 loc) · 1.59 KB
/
constraints.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
"""
The constraints module contains definitions for equilibrium constraints and
their Jacobian.
"""
import pycalphad.variables as v
from sympy import S, Add
def mole_fraction(phase, active_comps, species):
"""
Return a SymPy object representing the mole fraction as a function of
site fractions.
Parameters
----------
phase : Phase
Phase object corresponding to the phase of interest.
active_comps : list of str
Names of components to consider.
species : str
Names of species to consider.
Returns
-------
SymPy object representing the mole fraction.
Examples
--------
>>> dbf = Database('alfe_sei.TDB')
>>> mole_fraction(dbf.phases['FCC_A1'], ['AL', 'FE', 'VA'], 'AL')
"""
result = S.Zero
site_ratio_normalization = S.Zero
# Calculate normalization factor
for idx, sublattice in enumerate(phase.constituents):
active = set(sublattice).intersection(set(active_comps))
if 'VA' in active:
site_ratio_normalization += phase.sublattices[idx] * \
(1.0 - v.SiteFraction(phase.name, idx, 'VA'))
else:
site_ratio_normalization += phase.sublattices[idx]
site_ratios = [c/site_ratio_normalization for c in phase.sublattices]
# Sum up site fraction contributions from each sublattice
for idx, sublattice in enumerate(phase.constituents):
active = set(sublattice).intersection(set(active_comps))
if species in active:
result += site_ratios[idx] * \
v.SiteFraction(phase.name, idx, species)
return result