Skip to content

Commit 3a94ce1

Browse files
committed
make examples more robust
1 parent a1a4c29 commit 3a94ce1

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

examples/example_composite.py

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
compositional, thermodynamic and thermoelastic properties.
2828
* How to use the stoichiometric and reaction affinity methods to solve
2929
simple thermodynamic equilibrium problems.
30+
31+
Please see example_equilibrate.py for more advanced (and user-friendly)
32+
examples of equilibration of a composite material.
3033
"""
3134

3235
import numpy as np
@@ -35,6 +38,7 @@
3538

3639

3740
import burnman
41+
from burnman import equilibrate
3842
from burnman import minerals
3943

4044

@@ -213,19 +217,20 @@ def delta_affinity_two_binaries_P_x_Fe_wad(x, T):
213217

214218
# Now solve for the equilibrium state at a range of pressures
215219
# less than the univariant pressure
216-
def delta_affinity_two_binaries_x_Fe_ol_wad(x, P, T):
217-
x_fe_ol, x_fe_wad = x
218-
rock.set_state(P, T)
219-
ol.set_composition([1.0 - x_fe_ol, x_fe_ol])
220-
wad.set_composition([1.0 - x_fe_wad, x_fe_wad])
221-
return rock.reaction_affinities
222-
223220
pressures = np.linspace(10.0e9, fo_mwd.pressure, 21)
224-
x_fe_ols = np.zeros_like(pressures)
225-
x_fe_wads = np.zeros_like(pressures)
226-
for i, P in enumerate(pressures[:-1]):
227-
sol = fsolve(delta_affinity_two_binaries_x_Fe_ol_wad, [0.1, 0.1], args=(P, T))
228-
x_fe_ols[i], x_fe_wads[i] = sol
221+
free_compositional_vectors = [{"Mg": 1.0, "Fe": -1.0}]
222+
composition = {"Mg": 1.8, "Fe": 0.2, "Si": 1.0, "O": 4.0}
223+
equality_constraints = [["P", pressures], ["T", T], ["phase_fraction", [ol, 0.5]]]
224+
sol, prm = equilibrate(
225+
composition, rock, equality_constraints, free_compositional_vectors
226+
)
227+
228+
x_fe_ols = np.array(
229+
[s.assemblage.phases[0].molar_fractions[1] if s.success else 0 for s in sol]
230+
)
231+
x_fe_wads = np.array(
232+
[s.assemblage.phases[1].molar_fractions[1] if s.success else 0 for s in sol]
233+
)
229234

230235
# Pretty-print the results
231236
print(f"Olivine-wadsleyite equilibria at {T} K:")

examples/example_equilibrate.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -240,19 +240,19 @@
240240

241241
# Run the equilibration
242242
pressure = 1.0e5
243-
temperatures = np.linspace(300.0, 601.4, 41)
243+
temperatures = np.linspace(300.0, 601.0, 41)
244244
equality_constraints = [("P", pressure), ("T", temperatures)]
245245

246246
sols, prm = equilibrate(composition, assemblage, equality_constraints)
247247

248248
# Interrogate the stable assemblages for phase compositions.
249249
x1s = np.array(
250-
[sol.assemblage.phases[0].molar_fractions[1] for sol in sols if sol.code == 0]
250+
[sol.assemblage.phases[0].molar_fractions[1] for sol in sols if sol.success]
251251
)
252252
x2s = np.array(
253-
[sol.assemblage.phases[1].molar_fractions[1] for sol in sols if sol.code == 0]
253+
[sol.assemblage.phases[1].molar_fractions[1] for sol in sols if sol.success]
254254
)
255-
Ts = np.array([sol.assemblage.temperature for sol in sols if sol.code == 0])
255+
Ts = np.array([sol.assemblage.temperature for sol in sols if sol.success])
256256

257257
plt.plot(x1s, Ts)
258258
plt.plot(x2s, Ts)

0 commit comments

Comments
 (0)