Skip to content

IsolatedPhase fix and composition Jansson derivative default change - #595

Merged
bocklund merged 8 commits into
pycalphad:developfrom
cjkunselman18:develop
May 6, 2025
Merged

bocklund merged 8 commits into
pycalphad:developfrom
cjkunselman18:develop

Conversation

@cjkunselman18

Copy link
Copy Markdown
Contributor

IsolatedPhase was not constraining chemical potentials to the compset, but was allowing phase separation in miscibility gaps.

Currently, the default for composition Jansson derivatives is to define n-1 dependent components rather than just 1. Defining one dependent component seems more intuitive and applicable.

One dependent component for Jansson derivatives wrt composition
IsolatedPhase chemical potentials now computed properly.
@bocklund

Copy link
Copy Markdown
Collaborator

I'm not sure what's going on with the Windows tests, but that code seems to be orthogonal to the code here and also occurring in #594

@bocklund

bocklund commented Mar 10, 2025 •

Copy link
Copy Markdown
Collaborator

@cjkunselman18 is there any test you can write to verify the new behavior? The test should fail under the current develop branch that's incorrect and pass after applying the changes in this PR.

@bocklund

Copy link
Copy Markdown
Collaborator

I came up with test for this:

from importlib.resources import files
import pycalphad.tests.databases
from pycalphad import Database, Workspace, calculate, variables as v
from pycalphad.property_framework import IsolatedPhase
import numpy as np
import matplotlib.pyplot as plt

temperature = 100 # K
dbf = Database(files(pycalphad.tests.databases).joinpath("pbsn.tdb"))
wks = Workspace(dbf, phases=["BCT_A5"], components=["PB", "SN", "VA"], conditions={v.T: temperature, v.P:101325, v.X("SN"): 0.5})
calc_res = calculate(dbf, ["PB", "SN", "VA"], ["BCT_A5"], T=temperature, pdens=1000)

global_GM = wks.get("GM")
global_chempots = np.asarray(wks.get("MU(*)"))
global_sitefracs = wks.get("Y(BCT_A5,0,*)")
isolated_GM = wks.get(IsolatedPhase("BCT_A5",wks=wks)("GM"))
isolated_chempots = np.asarray(wks.get(IsolatedPhase("BCT_A5",wks=wks)("MU(*)")))
isolated_chempots_manually_extracted = np.asarray([wks.get(IsolatedPhase("BCT_A5",wks=wks)(x)) for x in ["MU(PB)", "MU(SN)"]])
# isolated_sitefracs = wks.get(IsolatedPhase("BCT_A5",wks=wks)("Y(BCT_A5,0,*)"))  # currently fails
print("Global")
print(global_GM)
print(global_chempots)
print(global_sitefracs)
print("Isolated")
print(isolated_GM)
print(isolated_chempots)  # TODO: this is a bug, we should bet an array out
print(isolated_chempots_manually_extracted)  # TODO: this is also a bug, it"s the same as the global min hyperplane these look like chempots in the MG
# print(isolated_sitefracs)  # currently fails

plt.scatter(calc_res.X.sel(component="SN").squeeze(), calc_res.GM.squeeze(), s=2, label="GM", c='blue')
plt.scatter(wks.get(IsolatedPhase("BCT_A5",wks=wks)("X(SN)")), wks.get(IsolatedPhase("BCT_A5",wks=wks)("GM")), c='k', label="Isolated phase GM")
plt.scatter(wks.get("X(SN)"), wks.get("GM"), c='red', label="Global GM")
plt.plot([0, 1], np.asarray([wks.get(IsolatedPhase("BCT_A5",wks=wks)(x)) for x in ["MU(PB)", "MU(SN)"]]), c='k', ls='-', label="Isolated phase MU")
plt.plot([0, 1], np.asarray(wks.get("MU(*)")), c='red', label="MU", ls='--')
plt.xlabel("X(SN)")
plt.ylabel("GM")
plt.title(f"Pb-Sn T = {temperature} K")
plt.legend(loc="center left", bbox_to_anchor=(1.01, 0.5))
plt.savefig(f"Pb-Sn-GM@{temperature}K.png", dpi=150, bbox_inches="tight")

The test actually demonstrates two apparent bugs:

  1. Getting MU() for IsolatedPhase gives the global min MU.
  2. We either get errors or meaningless results for using wildcards in isolated phase: wks.get(IsolatedPhase()("MU(*)")) returns 0.0 and wks.get(IsolatedPhase("BCT_A5",wks=wks)("Y(BCT_A5,0,*)")) raises ValueError: Y(BCT_A5,0,*) is not in list

On current develop you get this:
Pb-Sn-GM@100K-broken

After apply changes so far up to 0911aef you get this (which is correct):
Pb-Sn-GM@100K

I committed a test to this branch for the chemical potentials fix. The second bug is still unaddressed and we also still need a test for the change to fixed_component_differential.

@bocklund

bocklund commented Apr 19, 2025 •

Copy link
Copy Markdown
Collaborator

I've played a little bit with the isolated phase wildcard expansion, but I'm not very happy with the internal hacks I had to do to make it work, so I think we should treat that as out of scope for the purpose of this PR. I opened #604 for that.

@cjkunselman18 can you give an example where the Jansson derivative fix is comes into play? I can help write the test if needed and then this is ready for review and merge.

@cjkunselman18

cjkunselman18 commented Apr 19, 2025 •

Copy link
Copy Markdown
Contributor Author

I would love help writing a test. For any system with three or more components, the fix ensures that Jansson derivatives with respect to an independent composition variable only define one dependent component. A simple way to test for this is ensuring that dG/dX(A) = mu(A) - mu(B) where B is the dependent component. Prior to the fix, for system A-B-C, dG/dX(A) = mu(A) - mu(B)/2 - mu(C)/2. Below is an example for Hf-Nb-Ti-V:

from pycalphad import Workspace, Database, variables as v
from pycalphad.property_framework import IsolatedPhase

wks = Workspace(database=Database('HF_NB_TI_V.TDB'), components=['HF', 'NB', 'TI', 'V'], phases=['BCC_A2'],
conditions={v.X('HF'): 0.25,v.X('NB'): 0.25, v.X('TI'): 0.25, v.T: (300,3000,50), v.P: 101325})
ip = IsolatedPhase('BCC_A2', wks=wks)
dG_dx_HF_option1 = wks.get(ip('GM.X(HF)'))
dG_dx_HF_option2 = wks.get(ip('MU(HF)')) - wks.get(ip('MU(V)'))

import matplotlib.pyplot as plt
import numpy as np

plt.figure(0)
plt.plot(np.arange(300,3000,50), dG_dx_HF_option1)
plt.plot(np.arange(300,3000,50), dG_dx_HF_option2, linestyle = 'dashed')

For current version, you get this:
image

After applying changes, you get this:
image

@bocklund bocklund added this to the 0.11.1 milestone Apr 23, 2025
@codecov

codecov Bot commented Apr 24, 2025 •

Copy link
Copy Markdown

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.96%. Comparing base (5ab3023) to head (9b64c32).
Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop     #595   +/-   ##
========================================
  Coverage    91.95%   91.96%           
========================================
  Files           77       77           
  Lines        12338    12357   +19     
========================================
+ Hits         11346    11364   +18     
- Misses         992      993    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bocklund
bocklund requested a review from richardotis April 24, 2025 04:35
Comment thread pycalphad/core/minimizer.pyx Resolved
@richardotis
richardotis self-requested a review May 4, 2025 01:13
@bocklund
bocklund merged commit a8de09f into pycalphad:develop May 6, 2025
@bocklund bocklund modified the milestones: 0.11.1-old, 0.11.2 Oct 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants