What's the most efficient way to get the equilibrium phases and phase fractions for a set of alloy compositions? For context, I'm coming from Thermo-Calc/TC-Python where I would just loop through the alloy compositions and do a single equilbrium calculation. However, when I attempt to do this with pycalphad it seems quite slow. For example, here is what my code for the "one at a time" looping:
from pycalphad import Database, calculate, equilibrium, variables as v
dbf = Database("Cr-Fe-Ni.tdb")
components = ['FE','CR', 'NI', 'VA']
phases = ['LIQUID', 'BCC_A2', 'FCC_A1', 'SIGMA']
#Example set of alloy compositions, normally I would want to sample more finely but I'll keep it simple for demonstration purposes
compositions = array([[0. , 0. ],
[0. , 0.2],
[0. , 0.4],
[0. , 0.6],
[0. , 0.8],
[0.2, 0. ],
[0.2, 0.2],
[0.2, 0.4],
[0.2, 0.6],
[0.2, 0.8],
[0.4, 0. ],
[0.4, 0.2],
[0.4, 0.4],
[0.4, 0.6],
[0.6, 0. ],
[0.6, 0.2],
[0.6, 0.4],
[0.8, 0. ],
[0.8, 0.2]])
phase_data = []
for i in tqdm.tqdm(range(len(compositions)), total=len(compositions), position=0, leave=True, smoothing=0.3):
eq_result = equilibrium(dbf, components, phases, {v.X('CR'):compositions[i,0], v.X('NI'):compositions[i,1], v.T:1000, v.P:101325})
phase_data.append([*eq_result.NP.values.squeeze().tolist(), *eq_result.Phase.values.squeeze().tolist()])
This takes about 15 seconds, or about 1.2 alloys evaluated per second. This seems quite slow for a ternary, and I suspect it's because I'm calling the equilibrium function each time instead of just changing the composition.
I tried feeding the whole column at once (see code below), but this seemed to take the Cartesian product of the compositions, which is not what I want.
#database, phase, and composition setup is same as in above code
eq_result = equilibrium(dbf, components, phases, {v.X('CR'):compositions[:,0], v.X('NI'):compositions[:,1], v.T:1000, v.P:101325})
So, please let me know what the better approach is. Thanks!