|
| 1 | +from pycalphad import Database, equilibrium, variables as v |
| 2 | +from pycalphad.core.utils import instantiate_models, filter_phases, unpack_components |
| 3 | +from pycalphad.codegen.callables import build_phase_records |
| 4 | +import pandas as pd |
| 5 | +import math |
| 6 | + |
| 7 | +# Problem Specific setup for our 9-element space exploration. Make sure that the elements |
| 8 | +# are in the same order as the composition vector that will be passed to the equilibrium_callable. |
| 9 | +dbf = Database("ammap/databases/TDB") #GLOBAL from yaml |
| 10 | +T = 'TEMPERATURE' #GLOBAL from yaml |
| 11 | +elementalSpaceComponents = 'ELEMENTS' #GLOBAL from yaml |
| 12 | + |
| 13 | +# Setup the pycalphad models |
| 14 | +phases = list(set(dbf.phases.keys())) |
| 15 | +comps = [s.upper() for s in elementalSpaceComponents]+['VA'] |
| 16 | +phases_filtered = filter_phases(dbf, unpack_components(dbf, comps), phases) |
| 17 | +models = instantiate_models(dbf, comps, phases_filtered) |
| 18 | +phase_records = build_phase_records(dbf, comps, phases_filtered, {v.N, v.P, v.T}, models=models) |
| 19 | +expected_conds=[v.T]+[v.X(el) for el in comps[:-2]] |
| 20 | +default_conds={v.P: 101325, v.N: 1.0} |
| 21 | + |
| 22 | +# A neat callable for the equilibrium calculation that we will pass to the parallel graph exploration |
| 23 | +def equilibrium_callable(elP): |
| 24 | + # Round to 6 decimal places, but make sure that 0.0 is not rounded to 0.0. pyCalphad will not like |
| 25 | + # if the components are exactly 0 or sum to exactly 1 in the equilibrium calculation for reasons |
| 26 | + # that are beyond the scope of this tutorial. |
| 27 | + elP_round = [round(v-0.000001, 6) if v>0.000001 else 0.0000001 for v in elP] |
| 28 | + conds = {**default_conds, **dict(zip(expected_conds, [T] + elP_round))} |
| 29 | + eq_res = equilibrium( |
| 30 | + dbf, comps, phases_filtered, |
| 31 | + conds, model=models, phase_records=phase_records, calc_opts=dict(pdens=2000)) |
| 32 | + # Process the result into what we want -> a list of phases present |
| 33 | + nPhases = eq_res.Phase.data.shape[-1] |
| 34 | + phaseList = list(eq_res.Phase.data.reshape([nPhases])) |
| 35 | + phasePresentList = [pn for pn in phaseList if pn!=''] |
| 36 | + if len(phasePresentList)==0: |
| 37 | + # Decide on what to do if the equilibrium calculation failed to converge. By default, we will |
| 38 | + # just pass and let the graph exploration scheme decide what to do about it. |
| 39 | + # print(f"Point: {elP_round} failed to converge.") |
| 40 | + pass |
| 41 | + allphase = list(eq_res.Phase.data) |
| 42 | + pFrac=eq_res.NP.data.shape[-1] |
| 43 | + pFracList=list(eq_res.NP.data.reshape([nPhases])) |
| 44 | + pFracPresent= [pn for pn in pFracList if not math.isnan(pn)] |
| 45 | + return{ |
| 46 | + 'Phases':phasePresentList, |
| 47 | +# 'allPhase': allphase, |
| 48 | + 'PhaseFraction':pFracPresent |
| 49 | + } |
| 50 | + |
| 51 | +# Some extra code for the future tutorial on Scheil solidification :) |
| 52 | +from scheil import simulate_scheil_solidification |
| 53 | +# Meta Settings |
| 54 | +scheil_start_temperature = 'startTemperature' #GLOBAL from yaml |
| 55 | +liquid_phase_name = 'LIQUIDPHASE' #GLOBAL from yaml |
| 56 | +def scheil_callable(elP): |
| 57 | + elP_round = [round(v-0.000001, 6) if v>0.000001 else 0.0000001 for v in elP] |
| 58 | + initial_composition = {**dict(zip([v.X(el) for el in comps[:-2]], elP_round))} |
| 59 | + |
| 60 | + sol_res = simulate_scheil_solidification( |
| 61 | + dbf, comps, phases_filtered, |
| 62 | + initial_composition, scheil_start_temperature, step_temperature=1.0) |
| 63 | + |
| 64 | + phaseFractions = {} |
| 65 | + for phase, ammounts in sol_res.cum_phase_amounts.items(): |
| 66 | + finalAmmount = round(ammounts[-1], 6) |
| 67 | + if finalAmmount > 0: |
| 68 | + phaseFractions[phase] = finalAmmount |
| 69 | + test = sol_res.cum_phase_amounts |
| 70 | + Lfrac = sol_res.fraction_liquid |
| 71 | + Sfrac = sol_res.fraction_solid |
| 72 | + scheilT = sol_res.temperatures |
| 73 | + solT = sol_res.temperatures[len(scheilT) - 1] |
| 74 | + ddict = sol_res.x_phases |
| 75 | + keys_to_remove_from_ddict = [] |
| 76 | + for ddict_key, dict_value in ddict.items(): |
| 77 | + keys_to_remove_from_dict = [key for key, value in dict_value.items() if pd.isna(value).all()] |
| 78 | + # Remove marked keys from the dictionary |
| 79 | + for key in keys_to_remove_from_dict: |
| 80 | + del dict_value[key] |
| 81 | + # If the dictionary is empty, mark the key in the defaultdict for removal |
| 82 | + if not dict_value: |
| 83 | + keys_to_remove_from_ddict.append(ddict_key) |
| 84 | +# Remove the marked keys from the defaultdict |
| 85 | + for key in keys_to_remove_from_ddict: |
| 86 | + del ddict[key] |
| 87 | + yPhase = sol_res.Y_phases |
| 88 | + for i, value in enumerate(Lfrac): |
| 89 | + if value < 1: |
| 90 | + break |
| 91 | + liqT = sol_res.temperatures[i-1] |
| 92 | + return { |
| 93 | + 'scheilT': scheilT, |
| 94 | + 'finalPhase': phaseFractions, |
| 95 | + 'Lfrac': Lfrac, |
| 96 | + 'Sfrac': Sfrac, |
| 97 | + 'solT': solT, |
| 98 | + 'liqT': liqT, |
| 99 | + 'phaseFractions': test, |
| 100 | + 'xPhase': ddict#, |
| 101 | + #'yPhase': yPhase |
| 102 | + } |
| 103 | + |
| 104 | +if __name__ == "main": |
| 105 | + pass |
0 commit comments