In [1]:
from pycalphad import Database, Model, variables as v
from pycalphad import equilibrium, calculate
import numpy as np
from pycalphad.tests.datasets import *
dbf = Database('alzn_mey.tdb')
In [2]:
from pycalphad.core.solver import InteriorPointSolver
class ProblemSaver(InteriorPointSolver):
saved_problem = [None]
def solve(self, prob):
self.saved_problem[0] = prob
self.verbose = True
return super(ProblemSaver, self).solve(prob)
v.T.default_value = 930
eq2 = equilibrium(dbf, ['AL', 'ZN', 'VA'], ['FCC_A1', 'LIQUID', 'HCP_A3'],
{v.X('ZN'): 1e-4, v.NP('LIQUID'): 1e-4,
v.P: 1e5}, verbose=False, solver=ProblemSaver())
print(eq2)
In [3]:
eq2.GM - ((eq2.NP.isel(vertex=0) * eq2.MU * eq2.X.isel(vertex=0)).sum() +
(eq2.NP.isel(vertex=1) * (eq2.MU * eq2.X.isel(vertex=1)).sum()) +
(eq2.NP.isel(vertex=2) * (eq2.MU * eq2.X.isel(vertex=2)).sum()))
Out[3]:
In [4]:
eq2.X
Out[4]:
In [5]:
%matplotlib inline
import matplotlib.pyplot as plt
print(eq2['X_ZN'].values.flatten())
print(eq2['T'].values.flatten())
plt.scatter(eq2['X_ZN'].values.flatten(), eq2['T'].values.flatten())
plt.xlim((0,1))
plt.xlabel('X(ZN)')
plt.ylabel('T')
Out[5]:
In [6]:
fcc = calculate(dbf, ['AL', 'ZN', 'VA'], 'FCC_A1',
T=float(eq2['T']), P=1e5)
hcp = calculate(dbf, ['AL', 'ZN', 'VA'], 'HCP_A3',
T=float(eq2['T']), P=1e5)
liq = calculate(dbf, ['AL', 'ZN', 'VA'], 'LIQUID',
T=float(eq2['T']), P=1e5)
In [7]:
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
plt.figure(figsize=(12,12))
plt.scatter(fcc.X.sel(component='ZN').values.flatten(),
fcc.GM.values.flatten(), c='g')
plt.scatter(hcp.X.sel(component='ZN').values.flatten(),
hcp.GM.values.flatten(), c='y')
plt.scatter(liq.X.sel(component='ZN').values.flatten(),
liq.GM.values.flatten(), c='b')
plt.plot([0,1], eq2.MU.values.flatten())
plt.scatter([1e-4], [float(eq2.GM)])
plt.grid(True)
In [8]:
eq2.Y
Out[8]:
In [9]:
eq2.MU
Out[9]:
In [10]:
eq2.GM
Out[10]:
In [11]:
import numpy as np
def constraint_calculator(temps):
# Should be zero at T=933
x_in = [1.00000000e+00, 1.00000000e+05, 3.00000000e+02, 9.97257972e-01,
2.74202827e-03, 9.98058935e-01, 1.94106495e-03, 2.62233490e-03,
9.97377665e-01, 5.86390285e-01, 4.13608725e-01, 1.00000000e-06]
desired_chempots = [-37840.74636944, -110371.97774282]
cons = []
total_chempots = []
fixed_chempots = []
free_chempots_1 = []
free_chempots_2 = []
for temp in temps:
x_in[2] = temp
cons.append(ProblemSaver.saved_problem[0].constraints(x_in))
total_chempots.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in))
fixed_chempots.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, selected_phase=1))
free_chempots_1.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, selected_phase=0))
free_chempots_2.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, free_only=True))
cons = np.array(cons - np.array(ProblemSaver.saved_problem[0].cl))
total_chempots = np.array(total_chempots) - np.array(desired_chempots)
fixed_chempots = np.array(fixed_chempots) - np.array(desired_chempots)
free_chempots_1 = np.array(free_chempots_1) - np.array(desired_chempots)
free_chempots_2 = np.array(free_chempots_2) - np.array(desired_chempots)
return cons, total_chempots, fixed_chempots, free_chempots_1, free_chempots_2
def constraint_calculator_2(xfccs):
# Should be zero at T=933
x_in = [1.00000000e+00, 1.00000000e+05, 3.00000000e+02, 9.97257972e-01,
2.74202827e-03, 9.98058935e-01, 1.94106495e-03, 2.62233490e-03,
9.97377665e-01, 5.86390285e-01, 4.13608725e-01, 1.00000000e-06]
desired_chempots = [-37840.74636944, -110371.97774282]
cons = []
total_chempots = []
fixed_chempots = []
free_chempots_1 = []
free_chempots_2 = []
for xfcc in xfccs:
x_in[2] = 933
x_in[3] = xfcc
x_in[4] - 1-xfcc
cons.append(ProblemSaver.saved_problem[0].constraints(x_in))
total_chempots.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in))
fixed_chempots.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, selected_phase=1))
free_chempots_1.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, selected_phase=0))
free_chempots_2.append(ProblemSaver.saved_problem[0].chemical_potentials(x_in, free_only=True))
cons = np.array(cons - np.array(ProblemSaver.saved_problem[0].cl))
total_chempots = np.array(total_chempots) - np.array(desired_chempots)
fixed_chempots = np.array(fixed_chempots) - np.array(desired_chempots)
free_chempots_1 = np.array(free_chempots_1) - np.array(desired_chempots)
free_chempots_2 = np.array(free_chempots_2) - np.array(desired_chempots)
return cons, total_chempots, fixed_chempots, free_chempots_1, free_chempots_2
temps = np.arange(300., 2000., step=100)
xfccs = np.linspace(1e-6,1-1e-6, num=100)
results = constraint_calculator(temps)
results_2 = constraint_calculator_2(xfccs)
%matplotlib inline
import matplotlib.pyplot as plt
plt.plot(temps, results[0][:, -2], label='cons 1')
plt.plot(temps, results[0][:, -1], label='cons 2')
plt.plot(temps, results[1][:, 0], label='total')
plt.plot(temps, results[2][:, 0], label='fixed')
plt.plot(temps, results[3][:, 0], label='free 1')
plt.plot(temps, results[4][:, 0], label='free 2')
#plt.ylim((-1000, 1000))
#plt.xlim((700, 900))
#plt.plot(xfccs, results_2[0][:, -2], label='cons 1')
#plt.plot(xfccs, results_2[0][:, -1], label='cons 2')
#plt.xlim((0,0.1))
plt.legend(loc='best')
Out[11]:
In [12]:
np.argmin(np.abs(results_2[0][:, -1]))
Out[12]:
In [13]:
xfccs[1]
Out[13]:
In [14]:
results[0][:, -2]
Out[14]:
In [ ]: