Created
January 15, 2026 17:08
-
-
Save bocklund/b8a1f9840959b9e56a42bbb5ee5927a7 to your computer and use it in GitHub Desktop.
Programmatically add FUNCTION to a Calphad database and add it to a PARAMETER
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from pycalphad import Database, variables as v | |
| from symengine import Symbol | |
| from tinydb import where | |
| dbf = Database("NiTi-92Sau.tdb") | |
| # Add FUNCTION called MYFUNC | |
| myfunc = 1 + 10 * v.T + 100 * v.T**2 | |
| dbf.symbols["MYFUNC"] = myfunc | |
| # Update parameters | |
| # Can list all parameters with | |
| # print(dbf._parameters.all()) | |
| # In this case, we want to build a query to select just one parameter: G(B2_BCC,NI:VA:VA;0) | |
| query = ( | |
| (where("phase_name") == "B2_BCC") & | |
| (where("constituent_array") == ((v.Species("NI"),), (v.Species("VA"),), (v.Species("VA"),))) | |
| ) | |
| # querying for the desired parameter | |
| param = dbf._parameters.search(query)[0] # search() returns a list of all matches, assume that there's only a single match here | |
| # create new parameter | |
| new_param = param.copy() | |
| # update the value of the parameter itself (the SymEngine expression), assuming that there's only one Piecewise step in temperature (the .args[0]) and add the MYFUNC Syjbol | |
| new_param["parameter"] = param["parameter"].args[0] + Symbol("MYFUNC") | |
| # delete old parameter from the database so there aren't duplicates | |
| dbf._parameters.remove(query) | |
| # add newly created parameter | |
| dbf._parameters.insert(new_param) | |
| # Write TDB to file and also print | |
| dbf.to_file("newdb.tdb", if_exists="overwrite") | |
| print(dbf.to_string(fmt="tdb")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment