Joint Refinement: Si, Bragg + PDF¶
This example demonstrates a joint refinement of the Si crystal structure combining Bragg diffraction and pair distribution function (PDF) analysis. The Bragg experiment uses time-of-flight neutron powder diffraction data from SEPD at Argonne, while the PDF experiment uses data from NOMAD at SNS. A single shared Si structure is refined simultaneously against both datasets.
Import Library¶
In [2]:
Copied!
from easydiffraction import ExperimentFactory
from easydiffraction import Project
from easydiffraction import StructureFactory
from easydiffraction import download_data
from easydiffraction import ExperimentFactory
from easydiffraction import Project
from easydiffraction import StructureFactory
from easydiffraction import download_data
In [3]:
Copied!
structure = StructureFactory.from_scratch(name='si')
structure = StructureFactory.from_scratch(name='si')
Set Space Group¶
In [4]:
Copied!
structure.space_group.name_h_m = 'F d -3 m'
structure.space_group.it_coordinate_system_code = '1'
structure.space_group.name_h_m = 'F d -3 m'
structure.space_group.it_coordinate_system_code = '1'
Set Unit Cell¶
In [5]:
Copied!
structure.cell.length_a = 5.42
structure.cell.length_a = 5.42
Set Atom Sites¶
In [6]:
Copied!
structure.atom_sites.create(
label='Si',
type_symbol='Si',
fract_x=0,
fract_y=0,
fract_z=0,
wyckoff_letter='a',
b_iso=0.2,
)
structure.atom_sites.create(
label='Si',
type_symbol='Si',
fract_x=0,
fract_y=0,
fract_z=0,
wyckoff_letter='a',
b_iso=0.2,
)
In [7]:
Copied!
bragg_data_path = download_data(id=7, destination='data')
bragg_data_path = download_data(id=7, destination='data')
Getting data...
Data #7: Si, SEPD (Argonne)
✅ Data #7 downloaded to 'data/ed-7.xye'
Create Experiment¶
In [8]:
Copied!
bragg_expt = ExperimentFactory.from_data_path(
name='sepd', data_path=bragg_data_path, beam_mode='time-of-flight'
)
bragg_expt = ExperimentFactory.from_data_path(
name='sepd', data_path=bragg_data_path, beam_mode='time-of-flight'
)
Data loaded successfully
Experiment 🔬 'sepd'. Number of data points: 5600.
Set Instrument¶
In [9]:
Copied!
bragg_expt.instrument.setup_twotheta_bank = 144.845
bragg_expt.instrument.calib_d_to_tof_offset = -9.2
bragg_expt.instrument.calib_d_to_tof_linear = 7476.91
bragg_expt.instrument.calib_d_to_tof_quad = -1.54
bragg_expt.instrument.setup_twotheta_bank = 144.845
bragg_expt.instrument.calib_d_to_tof_offset = -9.2
bragg_expt.instrument.calib_d_to_tof_linear = 7476.91
bragg_expt.instrument.calib_d_to_tof_quad = -1.54
Set Peak Profile¶
In [10]:
Copied!
bragg_expt.peak_profile_type = 'pseudo-voigt * ikeda-carpenter'
bragg_expt.peak.broad_gauss_sigma_0 = 5.0
bragg_expt.peak.broad_gauss_sigma_1 = 45.0
bragg_expt.peak.broad_gauss_sigma_2 = 1.0
bragg_expt.peak.broad_mix_beta_0 = 0.04221
bragg_expt.peak.broad_mix_beta_1 = 0.00946
bragg_expt.peak.asym_alpha_0 = 0.0
bragg_expt.peak.asym_alpha_1 = 0.5971
bragg_expt.peak_profile_type = 'pseudo-voigt * ikeda-carpenter'
bragg_expt.peak.broad_gauss_sigma_0 = 5.0
bragg_expt.peak.broad_gauss_sigma_1 = 45.0
bragg_expt.peak.broad_gauss_sigma_2 = 1.0
bragg_expt.peak.broad_mix_beta_0 = 0.04221
bragg_expt.peak.broad_mix_beta_1 = 0.00946
bragg_expt.peak.asym_alpha_0 = 0.0
bragg_expt.peak.asym_alpha_1 = 0.5971
⚠️ Switching peak profile type discards existing peak parameters.
Peak profile type for experiment 'sepd' changed to
pseudo-voigt * ikeda-carpenter
Set Background¶
In [11]:
Copied!
bragg_expt.background_type = 'line-segment'
for x in range(0, 35000, 5000):
bragg_expt.background.create(id=str(x), x=x, y=200)
bragg_expt.background_type = 'line-segment'
for x in range(0, 35000, 5000):
bragg_expt.background.create(id=str(x), x=x, y=200)
Background type for experiment 'sepd' already set to
line-segment
Set Linked Phases¶
In [12]:
Copied!
bragg_expt.linked_phases.create(id='si', scale=13.0)
bragg_expt.linked_phases.create(id='si', scale=13.0)
In [13]:
Copied!
pdf_data_path = download_data(id=5, destination='data')
pdf_data_path = download_data(id=5, destination='data')
Getting data...
Data #5: NOM_9999_Si_640g_PAC_50_ff_ftfrgr_up-to-50.gr
✅ Data #5 already present at 'data/ed-5.gr'. Keeping existing file.
Create Experiment¶
In [14]:
Copied!
pdf_expt = ExperimentFactory.from_data_path(
name='nomad',
data_path=pdf_data_path,
beam_mode='time-of-flight',
scattering_type='total',
)
pdf_expt = ExperimentFactory.from_data_path(
name='nomad',
data_path=pdf_data_path,
beam_mode='time-of-flight',
scattering_type='total',
)
Warning: No uncertainty (sy) column provided. Defaulting to 0.03.
Data loaded successfully
Experiment 🔬 'nomad'. Number of data points: 5033.
Set Peak Profile (PDF Parameters)¶
In [15]:
Copied!
pdf_expt.peak.damp_q = 0.02
pdf_expt.peak.broad_q = 0.02
pdf_expt.peak.cutoff_q = 35.0
pdf_expt.peak.sharp_delta_1 = 0.001
pdf_expt.peak.sharp_delta_2 = 4.0
pdf_expt.peak.damp_particle_diameter = 0
pdf_expt.peak.damp_q = 0.02
pdf_expt.peak.broad_q = 0.02
pdf_expt.peak.cutoff_q = 35.0
pdf_expt.peak.sharp_delta_1 = 0.001
pdf_expt.peak.sharp_delta_2 = 4.0
pdf_expt.peak.damp_particle_diameter = 0
Set Linked Phases¶
In [16]:
Copied!
pdf_expt.linked_phases.create(id='si', scale=1.0)
pdf_expt.linked_phases.create(id='si', scale=1.0)
In [17]:
Copied!
project = Project()
project = Project()
Add Structure¶
In [18]:
Copied!
project.structures.add(structure)
project.structures.add(structure)
Add Experiments¶
In [19]:
Copied!
project.experiments.add(bragg_expt)
project.experiments.add(pdf_expt)
project.experiments.add(bragg_expt)
project.experiments.add(pdf_expt)
In [20]:
Copied!
project.analysis.fit_mode.mode = 'joint'
project.analysis.joint_fit_experiments.create(id='sepd', weight=0.7)
project.analysis.joint_fit_experiments.create(id='nomad', weight=0.3)
project.analysis.fit_mode.mode = 'joint'
project.analysis.joint_fit_experiments.create(id='sepd', weight=0.7)
project.analysis.joint_fit_experiments.create(id='nomad', weight=0.3)
Set Minimizer¶
In [21]:
Copied!
project.analysis.current_minimizer = 'lmfit'
project.analysis.current_minimizer = 'lmfit'
Current minimizer changed to
lmfit
Plot Measured vs Calculated (Before Fit)¶
In [22]:
Copied!
project.plot_meas_vs_calc(expt_name='sepd', show_residual=False)
project.plot_meas_vs_calc(expt_name='sepd', show_residual=False)
In [23]:
Copied!
project.plot_meas_vs_calc(expt_name='nomad', show_residual=False)
project.plot_meas_vs_calc(expt_name='nomad', show_residual=False)
Set Fitting Parameters¶
Shared structural parameters are refined against both datasets simultaneously.
In [24]:
Copied!
structure.cell.length_a.free = True
structure.atom_sites['Si'].b_iso.free = True
structure.cell.length_a.free = True
structure.atom_sites['Si'].b_iso.free = True
Bragg experiment parameters.
In [25]:
Copied!
bragg_expt.linked_phases['si'].scale.free = True
bragg_expt.instrument.calib_d_to_tof_offset.free = True
bragg_expt.peak.broad_gauss_sigma_0.free = True
bragg_expt.peak.broad_gauss_sigma_1.free = True
bragg_expt.peak.broad_gauss_sigma_2.free = True
for point in bragg_expt.background:
point.y.free = True
bragg_expt.linked_phases['si'].scale.free = True
bragg_expt.instrument.calib_d_to_tof_offset.free = True
bragg_expt.peak.broad_gauss_sigma_0.free = True
bragg_expt.peak.broad_gauss_sigma_1.free = True
bragg_expt.peak.broad_gauss_sigma_2.free = True
for point in bragg_expt.background:
point.y.free = True
PDF experiment parameters.
In [26]:
Copied!
pdf_expt.linked_phases['si'].scale.free = True
pdf_expt.peak.damp_q.free = True
pdf_expt.peak.broad_q.free = True
pdf_expt.peak.sharp_delta_1.free = True
pdf_expt.peak.sharp_delta_2.free = True
pdf_expt.linked_phases['si'].scale.free = True
pdf_expt.peak.damp_q.free = True
pdf_expt.peak.broad_q.free = True
pdf_expt.peak.sharp_delta_1.free = True
pdf_expt.peak.sharp_delta_2.free = True
Show Free Parameters¶
In [27]:
Copied!
project.analysis.show_free_params()
project.analysis.show_free_params()
Free parameters for both structures (🧩 data blocks) and experiments (🔬 data blocks)
| datablock | category | entry | parameter | value | uncertainty | min | max | units | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | si | cell | length_a | 5.42000 | -inf | inf | Å | ||
| 2 | si | atom_site | Si | b_iso | 0.20000 | -inf | inf | Ų | |
| 3 | sepd | linked_phases | si | scale | 13.00000 | -inf | inf | ||
| 4 | sepd | peak | gauss_sigma_0 | 5.00000 | -inf | inf | µs² | ||
| 5 | sepd | peak | gauss_sigma_1 | 45.00000 | -inf | inf | µs/Å | ||
| 6 | sepd | peak | gauss_sigma_2 | 1.00000 | -inf | inf | µs²/Ų | ||
| 7 | sepd | instrument | d_to_tof_offset | -9.20000 | -inf | inf | µs | ||
| 8 | sepd | background | 0 | y | 200.00000 | -inf | inf | ||
| 9 | sepd | background | 5000 | y | 200.00000 | -inf | inf | ||
| 10 | sepd | background | 10000 | y | 200.00000 | -inf | inf | ||
| 11 | sepd | background | 15000 | y | 200.00000 | -inf | inf | ||
| 12 | sepd | background | 20000 | y | 200.00000 | -inf | inf | ||
| 13 | sepd | background | 25000 | y | 200.00000 | -inf | inf | ||
| 14 | sepd | background | 30000 | y | 200.00000 | -inf | inf | ||
| 15 | nomad | linked_phases | si | scale | 1.00000 | -inf | inf | ||
| 16 | nomad | peak | damp_q | 0.02000 | -inf | inf | Å⁻¹ | ||
| 17 | nomad | peak | broad_q | 0.02000 | -inf | inf | Å⁻² | ||
| 18 | nomad | peak | sharp_delta_1 | 0.00100 | -inf | inf | Å | ||
| 19 | nomad | peak | sharp_delta_2 | 4.00000 | -inf | inf | Ų |
Run Fitting¶
In [28]:
Copied!
project.analysis.fit()
project.analysis.show_fit_results()
project.analysis.fit()
project.analysis.show_fit_results()
Using all experiments 🔬 ['sepd', 'nomad'] for 'joint' fitting
🚀 Starting fit process with 'lmfit (leastsq)'...
📈 Goodness-of-fit (reduced χ²) change:
| iteration | χ² | improvement [%] | |
|---|---|---|---|
| 1 | 1 | 3378.13 | |
| 2 | 23 | 844.33 | 75.0% ↓ |
| 3 | 43 | 267.01 | 68.4% ↓ |
| 4 | 63 | 59.98 | 77.5% ↓ |
| 5 | 83 | 52.58 | 12.3% ↓ |
| 6 | 103 | 51.89 | 1.3% ↓ |
| 7 | 170 | 51.87 |
🏆 Best goodness-of-fit (reduced χ²) is 51.87 at iteration 153
✅ Fitting complete.
Fit results
✅ Success: True
⏱️ Fitting time: 91.78 seconds
📏 Goodness-of-fit (reduced χ²): 51.87
📏 R-factor (Rf): 10.50%
📏 R-factor squared (Rf²): 9.32%
📏 Weighted R-factor (wR): 8.30%
📈 Fitted parameters:
| datablock | category | entry | parameter | start | fitted | uncertainty | units | change | |
|---|---|---|---|---|---|---|---|---|---|
| 1 | si | cell | length_a | 5.4200 | 5.4306 | 0.0000 | Å | 0.20 % ↑ | |
| 2 | si | atom_site | Si | b_iso | 0.2000 | 0.7041 | 0.0037 | Ų | 252.05 % ↑ |
| 3 | sepd | linked_phases | si | scale | 13.0000 | 16.0569 | 0.1008 | 23.51 % ↑ | |
| 4 | sepd | peak | gauss_sigma_0 | 5.0000 | -1.9630 | 1.2495 | µs² | 139.26 % ↓ | |
| 5 | sepd | peak | gauss_sigma_1 | 45.0000 | 50.3913 | 2.4601 | µs/Å | 11.98 % ↑ | |
| 6 | sepd | peak | gauss_sigma_2 | 1.0000 | 0.2155 | 0.4974 | µs²/Ų | 78.45 % ↓ | |
| 7 | sepd | instrument | d_to_tof_offset | -9.2000 | -8.2424 | 0.0950 | µs | 10.41 % ↓ | |
| 8 | sepd | background | 0 | y | 200.0000 | 280.6064 | 3.2291 | 40.30 % ↑ | |
| 9 | sepd | background | 5000 | y | 200.0000 | 148.7236 | 1.3525 | 25.64 % ↓ | |
| 10 | sepd | background | 10000 | y | 200.0000 | 118.2995 | 1.4191 | 40.85 % ↓ | |
| 11 | sepd | background | 15000 | y | 200.0000 | 135.8529 | 2.7036 | 32.07 % ↓ | |
| 12 | sepd | background | 20000 | y | 200.0000 | 132.6849 | 4.7369 | 33.66 % ↓ | |
| 13 | sepd | background | 25000 | y | 200.0000 | 174.9850 | 9.5201 | 12.51 % ↓ | |
| 14 | sepd | background | 30000 | y | 200.0000 | 180.5821 | 19.3648 | 9.71 % ↓ | |
| 15 | nomad | linked_phases | si | scale | 1.0000 | 1.2713 | 0.0010 | 27.13 % ↑ | |
| 16 | nomad | peak | damp_q | 0.0200 | 0.0250 | 0.0001 | Å⁻¹ | 25.16 % ↑ | |
| 17 | nomad | peak | broad_q | 0.0200 | 0.0188 | 0.0002 | Å⁻² | 5.92 % ↓ | |
| 18 | nomad | peak | sharp_delta_1 | 0.0010 | 2.4399 | 0.0382 | Å | 243887.89 % ↑ | |
| 19 | nomad | peak | sharp_delta_2 | 4.0000 | -1.5282 | 0.0898 | Ų | 138.21 % ↓ |
Plot Measured vs Calculated (After Fit)¶
In [29]:
Copied!
project.plot_meas_vs_calc(expt_name='sepd', show_residual=False)
project.plot_meas_vs_calc(expt_name='sepd', show_residual=False)
In [30]:
Copied!
project.plot_meas_vs_calc(expt_name='nomad', show_residual=False)
project.plot_meas_vs_calc(expt_name='nomad', show_residual=False)
In [ ]:
Copied!