openmaterials.ai / codes / pycalphad

pycalphad: CALPHAD Gibbs energy models and equilibria in Python

A Python library that builds symbolic Gibbs energy models per phase from a TDB database and minimizes the multicomponent, multiphase Gibbs energy, with models swapped at run time.

What it is and how it works

Symbolic phase models

Each phase's molar Gibbs energy is a SymPy expression built from database parameters and decoupled from the solver. A custom Model subclass replaces it without touching the equilibrium code (Otis 2017).

Database

Database('example.tdb') parses the TDB format into phases, sublattice models and parameters, searchable with search() and written back with to_file() (Otis 2017).

Model

The library Model carries the compound energy formalism, Redlich-Kister polynomials with the Muggianu extension and the Inden-Hillert-Jarl magnetic model (Otis 2017).

calculate() and equilibrium()

calculate() gives a property at set T, P and site fractions. equilibrium() minimizes the Gibbs energy for conditions like {v.X('AL'): (0, 1, 0.01), v.T: 600} (Otis 2017).

xarray results

Results are xarray Datasets with dimensions for conditions, tie-simplex vertex and internal degrees of freedom. eq.MU.sel(component='AL', T=600) picks one (Otis 2017).

Compiled derivatives

SymPy code generation compiles each model and its exact first and second derivatives to machine code on demand (Otis 2017).

What people do with it

  • 3000lines of Python, roughly, at version 0.4.2Otis 2017
  • 80%of the code, by lines, covered by the CI testsOtis 2017
  • 170 x 100temperatures x compositions in a 2-D mappingOtis 2017

Cu-Mg database fitted with ESPEI

ESPEI fits Cu-Mg with pycalphad as engine. 15 degrees of freedom for five phases go through 500 MCMC iterations. The largest liquid Gibbs energy uncertainty is ~1 kJ/mol (Bocklund 2019). Bocklund 2019

Al-Fe phase diagram from COST 507

In the pycalphad paper, the COST 507 Al-Fe diagram, with lines of constant B2 ordering and the Curie temperature, shows a second-order bcc ordering transition (Otis 2017). Otis 2017

How it is used

  1. Load a TDB

    dbf = Database('alfe.tdb'), then list(dbf.phases.keys()) for the phase names.

  2. Pick components, phases

    Choose comps and phases, and optionally a custom Model for all phases or one of them.

  3. Set conditions

    conditions = {v.X('AL'): (0, 1, 0.01), v.T: 600}, with T in K and P in Pa.

  4. Compute

    eq = equilibrium(dbf, comps, phases, conditions, output='HM'), or calculate() per phase.

  5. Read and plot

    Slice eq.MU, eq.NP and eq.Phase with xarray, or map the phase diagram and plot it.