Skip to content

Instantly share code, notes, and snippets.

@bocklund
Created August 22, 2022 22:38
Show Gist options
  • Select an option

  • Save bocklund/edfca20d646a4ff14f0de0f8e510735b to your computer and use it in GitHub Desktop.

Select an option

Save bocklund/edfca20d646a4ff14f0de0f8e510735b to your computer and use it in GitHub Desktop.
PyCalphad: Al-Zn finding eutectic point with fixed phases
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"from pycalphad import Database, variables as v\n",
"from pycalphad.core.solver import Solver\n",
"from pycalphad.core.composition_set import CompositionSet\n",
"from pycalphad.codegen.callables import build_phase_records\n",
"from pycalphad.core.utils import instantiate_models, unpack_components"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"dbf = Database(\"alzn_mey.tdb\")"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"comps = [\"AL\", \"ZN\", \"VA\"]\n",
"phases = list(dbf.phases.keys())\n",
"\n",
"# Build phase records for the specified set of components/phases\n",
"models = instantiate_models(dbf, unpack_components(dbf, comps), phases)\n",
"phase_records = build_phase_records(dbf, comps, phases, {v.N, v.T, v.P}, models)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"# to find a three phase equilibrium, we need to fix two phases to amount zero\n",
"# in this case, we'll try to find the fcc+hcp+liquid equilibrium, fixing hcp\n",
"# fcc to amount zero.\n",
"compset_fcc = CompositionSet(phase_records[\"FCC_A1\"])\n",
"compset_hcp = CompositionSet(phase_records[\"HCP_A3\"])\n",
"compset_liq = CompositionSet(phase_records[\"LIQUID\"])\n",
"# Fix the composition sets, we'll set their amounts in the next step.\n",
"# \"fixed\" in this context means that the amount of the phase cannot change,\n",
"# but the internal degrees of freedom and state variables can change.\n",
"compset_fcc.fixed = True\n",
"compset_hcp.fixed = True\n",
"\n",
"# For each composition set, we need to provide a starting point guess for the\n",
"# state variables (N, P, T) and internal degrees of freedom for each phase.\n",
"# In a \"normal\" calculation, PyCalphad does this for you, but this is an advanced use case.\n",
"statevars_guess = np.array([1.0, 101325.0, 600.0]) # N, P, T\n",
"\n",
"# the \"update\" method needs an array of site fractions, the phase amount, and an array of state variables\n",
"# remember to give vacancy site fractions for phases with vacancies, etc.\n",
"# in this case, none of the phases have any extra internal degrees of freedom\n",
"compset_fcc.update(np.array([0.5, 0.5]), 0.0, statevars_guess) # Al-50Zn, 0.0 phase fraction\n",
"compset_hcp.update(np.array([0.1, 0.9]), 0.0, statevars_guess) # Al-90Zn, 0.0 phase fraction\n",
"compset_liq.update(np.array([0.2, 0.8]), 1.0, statevars_guess) # Al-80Zn, 1.0 phase fraction"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Converged: True\n",
"Chemical potentials [-24574.80913274 -31540.23660633]\n",
"Expecting T = 654 K...\n",
"N, P, T = [1.00000000e+00 1.01325000e+05 6.54008527e+02]\n",
"CompositionSet(FCC_A1, [0.32689249 0.67310751], NP=0.0, GM=-29263.290654659533)\n",
"CompositionSet(HCP_A3, [0.0308997 0.9691003], NP=0.0, GM=-31325.006977980065)\n",
"CompositionSet(LIQUID, [0.11646005 0.88353995], NP=1.0, GM=-30729.04259799266)\n"
]
}
],
"source": [
"# we have c+2 = 2+2 = 4 conditions to specify in a binary system and we\n",
"# are replacing two conditions with fixed phases, we have 2 conditions left to\n",
"# specify\n",
"conditions = {v.P: 101325, v.N: 1.0} # all conditions in this API must be scalar\n",
"str_conditions = {str(cond_var): float(cond_val) for cond_var, cond_val in conditions.items()}\n",
"composition_sets = [compset_fcc, compset_hcp, compset_liq]\n",
"\n",
"# create a solver object and use it to solve based on our initial guess\n",
"# the composition sets are updated in-place\n",
"solver = Solver()\n",
"solver_result = solver.solve(composition_sets, str_conditions)\n",
"\n",
"# State variables:\n",
"print(\"Converged:\", solver_result.converged)\n",
"print(\"Chemical potentials\", np.asarray(solver_result.chemical_potentials))\n",
"print(\"Expecting T = 654 K...\")\n",
"print(\"N, P, T = \", np.asarray(solver_result.x[:3]))\n",
"for compset in composition_sets:\n",
" print(compset)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "calphad-dev",
"language": "python",
"name": "calphad-dev"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "59cea594589ac34d40b79982e4878a95bbbbc70c652d363d31f41a15b933cebb"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
$ ALZN
$
$ TDB-file for the thermodynamic assessment of the Al-ZN system
$
$-------------------------------------------------------------------------------
$ 2011.11.9
$
$ TDB file created by T.Abe, K.Hashimoto and Y.sawada
$
$ Particle Simulation and Thermodynamics Group, National Institute for
$ Materials Science. 1-2-1 Sengen, Tsukuba, Ibaraki 305-0047, Japan
$ e-mail: [email protected]
$ Copyright (C) NIMS 2009
$
$ ------------------------------------------------------------------------------
$ PARAMETERS ARE TAKEN FROM
$ Reevaluation of the Al-Zn System,
$ Sabine an Mey, Z.Metallkd., 84 (1993) 451-455.
$
$ ------------------------------------------------------------------------------
ELEMENT /- ELECTRON_GAS 0.0000E+00 0.0000E+00 0.0000E+00!
ELEMENT VA VACUUM 0.0000E+00 0.0000E+00 0.0000E+00!
ELEMENT AL FCC_A1 2.6982E+01 4.5773E+03 2.8322E+01!
ELEMENT ZN HCP_ZN 6.5390E+01 5.6568E+03 4.1631E+01!
$-------------------------------------------------------------------------------
$ FUNCTIONS FOR PURE AND OTHERS
$-------------------------------------------------------------------------------
FUNCTION GHSERAL 298.0
-7976.15+137.0715*T-24.36720*T*LN(T)-1.884662E-3*T**2-0.877664E-6*T**3
+74092*T**(-1); 700.00 Y
-11276.24+223.0269*T-38.58443*T*LN(T)+18.531982E-3*T**2-5.764227E-6*T**3
+74092*T**(-1); 933.6 Y
-11277.68+188.6620*T-31.74819*T*LN(T)-1234.26E25*T**(-9); 2900.00 N !
FUNCTION GALLIQ 298.0
+3029.403+125.2307*T-24.36720*T*LN(T)-1.884662E-3*T**2-0.877664E-6*T**3
+74092*T**(-1)+79.401E-21*T**7; 700.00 Y
-270.6860+211.1861*T-38.58443*T*LN(T)+18.53198E-3*T**2-5.764227E-6*T**3
+74092*T**(-1)+79.401E-21*T**7; 933.6 Y
-795.7090+177.4100*T-31.74819*T*LN(T); 2900.00 N !
FUNCTION GALHCP 298.0 +5481-1.8*T+GHSERAL#; 6000 N !
FUNCTION GHSERZN 298.0 -7285.787+118.4693*T-23.70131*T*LN(T)
-.001712034*T**2-1.264963E-06*T**3; 692.7 Y
-11070.60+172.3449*T-31.38*T*LN(T)+4.70657E+26*T**(-9); 1700 N !
$FUNCTION GZNLIQ 298.0 -1.285170+108.1769*T-23.70131*T*LN(T)
$ -.001712034*T**2-1.264963E-06*T**3-3.585652E-19*T**7; 692.7 Y
$ -11070.60+172.3449*T-31.38*T*LN(T)+4.70657E+26*T**(-9); 1700 N !
FUNCTION GZNLIQ 298.14 +7157.213-10.29299*T-3.5896E-19*T**7+GHSERZN#;
692.7 Y
+7450.168-10.737066*T-4.7051E+26*T**(-9)+GHSERZN#; 1700 N !
FUNCTION GZNFCC 298.15 +2969.82-1.56968*T+GHSERZN#; 1700 N !
$-------------------------------------------------------------------------------
TYPE_DEFINITION % SEQ *!
DEFINE_SYSTEM_DEFAULT ELEMENT 2 !
DEFAULT_COMMAND DEF_SYS_ELEMENT VA /- !
$-------------------------------------------------------------------------------
$ PARAMETERS FOR LIQUID PHASE
$-------------------------------------------------------------------------------
PHASE LIQUID % 1 1.0 !
CONSTITUENT LIQUID :AL,ZN : !
PARAMETER G(LIQUID,AL;0) 298.15 +GALLIQ#; 2900 N !
PARAMETER G(LIQUID,ZN;0) 298.15 +GZNLIQ#; 1700 N !
PARAMETER G(LIQUID,AL,ZN;0) 298.15 +10465.5-3.39259*T; 6000 N !
$-------------------------------------------------------------------------------
$ FUNCTIONS FOR FCC_A1
$-------------------------------------------------------------------------------
PHASE FCC_A1 % 1 1.0 !
CONSTITUENT FCC_A1 :AL,ZN : !
PARAMETER G(FCC_A1,AL;0) 298.15 +GHSERAL#; 2900 N !
PARAMETER G(FCC_A1,ZN;0) 298.15 +GZNFCC#; 1700 N !
PARAMETER G(FCC_A1,AL,ZN;0) 298.15 +7297.5+0.47512*T; 6000 N !
PARAMETER G(FCC_A1,AL,ZN;1) 298.15 +6612.9-4.5911*T; 6000 N !
PARAMETER G(FCC_A1,AL,ZN;2) 298.15 -3097.2+3.30635*T; 6000 N !
$-------------------------------------------------------------------------------
$ FUNCTIONS FOR HCP_A3
$-------------------------------------------------------------------------------
PHASE HCP_A3 % 1 1.0 !
CONSTITUENT HCP_A3 :AL,ZN : !
PARAMETER G(HCP_A3,AL;0) 298.15 +GALHCP#; 2900 N !
PARAMETER G(HCP_A3,ZN;0) 298.15 +GHSERZN#; 1700 N !
PARAMETER G(HCP_A3,AL,ZN;0) 298.15 +18821.0-8.95255*T; 6000 N !
PARAMETER G(HCP_A3,AL,ZN;3) 298.15 -702.8; 6000 N !
$
$------------------------------------------------------------------- END OF LINE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment