Skip to content

Commit 73dd932

Browse files
Update lattice strain fitting example
1 parent abfbe74 commit 73dd932

File tree

2 files changed

+24
-19
lines changed

2 files changed

+24
-19
lines changed

docs/source/gallery/examples/geochem/mineral_lattice.py

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
#
5454
fontsize = 8
5555
fig, ax = plt.subplots(1)
56-
56+
ax.set(ylabel="$D_X$", xlabel="Radii ($\AA$)", yscale="log")
5757
site2labels = ["Na", "Ca", "Eu", "Sr"]
5858
# get the Shannon ionic radii for the elements in the 2+ site
5959
site2radii = [
@@ -103,7 +103,6 @@
103103
ax.annotate(
104104
l, xy=(r, d), xycoords="data", ha="right", va="bottom", fontsize=fontsize
105105
)
106-
ax.set(ylabel="$D_X$", xlabel="Radii ($\AA$)", yscale="log")
107106
fig
108107
########################################################################################
109108
# As europium is commonly present as a mixture of both :math:`Eu^{2+}`
@@ -136,14 +135,17 @@
136135
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137136
# Given the lattice strain model and a partitioning profile (for e.g. REE data),
138137
# we can also fit a model to a given curve; here we fit to our REE data above,
139-
# for which we have some known parameters to compare to:
138+
# for which we have some known parameters to compare to. Note that this uses
139+
# the youngs modulus approximation behind the scenes where it isn't provided:
140140
#
141-
from pyrolite.mineral.lattice import fit_lattice_strain, _lattice_opt_function
141+
from pyrolite.mineral.lattice import fit_lattice_strain, youngs_modulus_approximation
142142

143-
_ri, _tk, _D = fit_lattice_strain(np.array(site3radii), site3Ds, z=3)
143+
t0 = 273.15 + 700 # estimated temperature
144+
_ri, _tk, _D = fit_lattice_strain(site3radii, site3Ds, z=3, t0=t0)
144145
########################################################################################
145146
# We can compare the results of this fit to our source parameters - the ionic radius of
146-
# La, 900°C and estimated :math:`D_{La}`:
147+
# La, 900°C and estimated :math:`D_{La}` - note that the temperature isn't being fit
148+
# well here, but the other parameters are recovered reasonably well:
147149
#
148150
import pandas as pd
149151

@@ -154,27 +156,26 @@
154156
).T
155157

156158
########################################################################################
157-
# We can also compare the curves visually:
159+
# From this point, we could We can also compare the curves visually:
158160
#
159161
from pyrolite.plot.spider import REE_v_radii
160162

161163
ax = REE_v_radii(index="radii")
162164
ax.set(ylabel="$D_X$", xlabel="Radii ($\AA$)")
165+
163166
ax.plot(site3radii, site3Ds, label="True", color="k")
164-
ax.plot(
165-
site3radii,
166-
_lattice_opt_function(site3radii, site3radii.mean(), 298 + 1000, 1, z=3),
167-
label="Starting Guess",
168-
ls="--",
169-
color="0.5",
167+
168+
r0 = site3radii.mean()
169+
starting_guess = strain_coefficient(
170+
r0, site3radii, r0=r0, z=3, T=t0, E=youngs_modulus_approximation(3, r0)
170171
)
172+
ax.plot(site3radii, starting_guess, label="Starting Guess", ls="--", color="0.5")
171173

172-
ax.plot(
173-
site3radii,
174-
_lattice_opt_function(site3radii, _ri, _tk, _D, z=3),
175-
label="Fit",
176-
color="r",
174+
fit_curve = _D * strain_coefficient(
175+
_ri, site3radii, r0=_ri, T=_tk, z=3, E=youngs_modulus_approximation(3, _ri)
177176
)
177+
ax.plot(site3radii, fit_curve, color="r", ls="--", label="Fit")
178+
178179
ax.legend()
179180
ax.figure
180181
########################################################################################
@@ -202,4 +203,3 @@
202203
# American Journal of Science 314, 1319–1372.
203204
# doi: `10.2475/09.2014.04 <https://doi.org/10.2475/09.2014.04>`__
204205
#
205-

pyrolite/mineral/lattice.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ def fit_lattice_strain(
227227
ri, tk, D : :class:`float`
228228
Radius, temperature and partition coefficeint describing the
229229
lattice strain fit.
230+
231+
Notes
232+
-----
233+
* Uses the youngs modulus approximation where `E` not provided.
234+
* Passes keyword arguments to :func:`scipy.optimize_curve_fit`.
230235
"""
231236

232237
popt, pcov = curve_fit(

0 commit comments

Comments
 (0)