forked from pycalphad/pycalphad
-
Notifications
You must be signed in to change notification settings - Fork 2
/
variables.py
118 lines (110 loc) · 4.09 KB
/
variables.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
#pylint: disable=C0103,R0903,W0232
"""
Classes and constants for representing thermodynamic variables.
"""
from sympy import Float, Symbol
class StateVariable(Symbol):
"""
State variables are symbols with built-in assumptions of being real.
"""
def __new__(cls, name, **assumptions):
return Symbol.__new__(cls, name.upper(), real=True, **assumptions)
class SiteFraction(StateVariable):
"""
Site fractions are symbols with built-in assumptions of being real
and nonnegative. The constructor handles formatting of the name.
"""
def __new__(cls, phase_name, subl_index, species): #pylint: disable=W0221
varname = phase_name + str(subl_index) + species
#pylint: disable=E1121
new_self = StateVariable.__new__(cls, varname, nonnegative=True)
new_self.phase_name = phase_name.upper()
new_self.sublattice_index = subl_index
new_self.species = species.upper()
return new_self
def _latex(self):
"LaTeX representation."
#pylint: disable=E1101
return 'y^{'+self.phase_name.replace('_', '-') + \
'}_{'+str(self.subl_index)+'},_{'+self.species+'}'
def __str__(self):
"String representation."
#pylint: disable=E1101
return 'Y(%s,%d,%s)' % \
(self.phase_name, self.sublattice_index, self.species)
class PhaseFraction(StateVariable):
"""
Phase fractions are symbols with built-in assumptions of being real
and nonnegative. The constructor handles formatting of the name.
"""
def __new__(cls, phase_name, multiplicity): #pylint: disable=W0221
varname = phase_name + str(multiplicity)
#pylint: disable=E1121
new_self = StateVariable.__new__(cls, varname, nonnegative=True)
new_self.phase_name = phase_name.upper()
new_self.multiplicity = multiplicity
return new_self
def _latex(self):
"LaTeX representation."
#pylint: disable=E1101
return 'f^{'+self.phase_name.replace('_', '-') + \
'}_{'+str(self.multiplicity)+'}'
class Composition(StateVariable):
"""
Compositions are symbols with built-in assumptions of being real
and nonnegative.
"""
def __new__(cls, *args): #pylint: disable=W0221
new_self = None
varname = None
phase_name = None
species = None
if len(args) == 1:
# this is an overall composition variable
species = args[0].upper()
varname = 'X_' + species
elif len(args) == 2:
# this is a phase-specific composition variable
phase_name = args[0].upper()
species = args[1].upper()
varname = 'X_' + phase_name + '_' + species
else:
# not defined
raise ValueError('Composition not defined for args: '+args)
#pylint: disable=E1121
new_self = StateVariable.__new__(cls, varname, nonnegative=True)
new_self.phase_name = phase_name
new_self.species = species
return new_self
def _latex(self):
"LaTeX representation."
#pylint: disable=E1101
if self.phase_name:
return 'x^{'+self.phase_name.replace('_', '-') + \
'}_{'+self.species+'}'
else:
return 'x_{'+self.species+'}'
class ChemicalPotential(StateVariable):
"""
Chemical potentials are symbols with built-in assumptions of being real.
"""
def __new__(cls, species, **assumptions):
varname = 'MU_' + species.upper()
new_self = StateVariable.__new__(cls, varname, **assumptions)
new_self.species = species
return new_self
def _latex(self):
"LaTeX representation."
return '\mu_{'+self.species+'}'
def __str__(self):
"String representation."
return 'MU(%s)' % self.species
temperature = T = StateVariable('T')
entropy = S = StateVariable('S')
pressure = P = StateVariable('P')
volume = V = StateVariable('V')
moles = N = StateVariable('N')
site_fraction = Y = SiteFraction
X = Composition
MU = ChemicalPotential
si_gas_constant = R = Float(8.3145) # ideal gas constant