1+ # %% [markdown]
2+ # # Standard diffraction: Na2Ca3Al2F14
3+ #
4+ # Standard diffraction analysis of Na2Ca3Al2F14 after the powder neutron
5+ # time-of-flight diffraction measurement from WISH at ISIS.
6+ #
7+ # Only a single dataset from detector banks 5 & 6 is used here.
8+
9+ # %% [markdown]
10+ # ## Import EasyDiffraction
11+
12+ # %%
13+ from easydiffraction import (
14+ Project ,
15+ SampleModel ,
16+ Experiment ,
17+ download_from_repository
18+ )
19+
20+ # %% [markdown]
21+ # ## Define Sample Model
22+ #
23+ # This section covers how to add sample models and modify their parameters.
24+ #
25+ # ### Create sample model object
26+
27+ # %%
28+ model = SampleModel ('ncaf' )
29+
30+ # %% [markdown]
31+ # ### Define space group
32+
33+ # %%
34+ model .space_group .name_h_m = 'I 21 3'
35+ model .space_group .it_coordinate_system_code = '1'
36+
37+ # %% [markdown]
38+ # ### Define unit cell
39+
40+ # %%
41+ model .cell .length_a = 10.250256
42+
43+ # %% [markdown]
44+ # ### Define atom sites
45+
46+ # %%
47+ model .atom_sites .add ('Ca' , 'Ca' , 0.4661 , 0.0 , 0.25 , wyckoff_letter = "b" , b_iso = 0.9 )
48+ model .atom_sites .add ('Al' , 'Al' , 0.25171 , 0.25171 , 0.25171 , wyckoff_letter = "a" , b_iso = 0.66 )
49+ model .atom_sites .add ('Na' , 'Na' , 0.08481 , 0.08481 , 0.08481 , wyckoff_letter = "a" , b_iso = 1.9 )
50+ model .atom_sites .add ('F1' , 'F' , 0.1375 , 0.3053 , 0.1195 , wyckoff_letter = "c" , b_iso = 0.9 )
51+ model .atom_sites .add ('F2' , 'F' , 0.3626 , 0.3634 , 0.1867 , wyckoff_letter = "c" , b_iso = 1.28 )
52+ model .atom_sites .add ('F3' , 'F' , 0.4612 , 0.4612 , 0.4612 , wyckoff_letter = "a" , b_iso = 0.79 )
53+
54+ # %% [markdown]
55+ # ## Define Experiment
56+ #
57+ # This section teaches how to add experiments, configure their parameters, and
58+ # link to them the sample models defined in the previous step.
59+ #
60+ # ### Download measured data
61+
62+ # %%
63+ download_from_repository ('wish_ncaf.xye' ,
64+ branch = 'docs' ,
65+ destination = 'data' )
66+
67+ # %% [markdown]
68+ # ### Create experiment object
69+
70+ # %%
71+ expt = Experiment ('wish' ,
72+ beam_mode = 'time-of-flight' ,
73+ data_path = 'data/wish_ncaf.xye' )
74+
75+ # %% [markdown]
76+ # ### Define instrument
77+
78+ # %%
79+ expt .instrument .setup_twotheta_bank = 152.827
80+ expt .instrument .calib_d_to_tof_offset = 0.0
81+ expt .instrument .calib_d_to_tof_linear = 20770
82+ expt .instrument .calib_d_to_tof_quad = - 1.08308
83+
84+ # %% [markdown]
85+ # ### Define peak profile
86+
87+ # %%
88+ expt .peak_profile_type = 'pseudo-voigt * ikeda-carpenter'
89+ expt .peak .broad_gauss_sigma_0 = 0.0
90+ expt .peak .broad_gauss_sigma_1 = 0.0
91+ expt .peak .broad_gauss_sigma_2 = 5.0
92+ expt .peak .broad_mix_beta_0 = 0.01
93+ expt .peak .broad_mix_beta_1 = 0.01
94+
95+ # %% [markdown]
96+ # ### Define peak asymmetry
97+
98+ # %%
99+ expt .peak .asym_alpha_0 = 0.0
100+ expt .peak .asym_alpha_1 = 0.1
101+
102+ # %% [markdown]
103+ # ### Define background
104+
105+ # %%
106+ expt .background_type = 'line-segment'
107+ for x , y in [
108+ (9162 , 465 ),
109+ (11136 , 593 ),
110+ (13313 , 497 ),
111+ (14906 , 546 ),
112+ (16454 , 533 ),
113+ (17352 , 496 ),
114+ (18743 , 428 ),
115+ (20179 , 452 ),
116+ (21368 , 397 ),
117+ (22176 , 468 ),
118+ (22827 , 477 ),
119+ (24644 , 380 ),
120+ (26439 , 381 ),
121+ (28257 , 378 ),
122+ (31196 , 343 ),
123+ (34034 , 328 ),
124+ (37265 , 310 ),
125+ (41214 , 323 ),
126+ (44827 , 283 ),
127+ (49830 , 273 ),
128+ (52905 , 257 ),
129+ (58204 , 260 ),
130+ (62916 , 261 ),
131+ (70186 , 262 ),
132+ (74204 , 262 ),
133+ (82103 , 268 ),
134+ (91958 , 268 ),
135+ (102712 , 262 )
136+ ]:
137+ expt .background .add (x , y )
138+
139+ # %% [markdown]
140+ # ### Select linked phase
141+
142+ # %%
143+ expt .linked_phases .add ('ncaf' , scale = 0.5 )
144+
145+ # %% [markdown]
146+ # ## Define Project
147+ #
148+ # The project object is used to manage the sample model, experiments, and
149+ # analysis
150+ #
151+ # ### Create project object
152+
153+ # %%
154+ project = Project ()
155+
156+ # %% [markdown]
157+ # ### Configure Plotting Engine
158+
159+ # %%
160+ project .plotter .engine = 'plotly'
161+
162+ # %% [markdown]
163+ # ### Add sample model
164+
165+ # %%
166+ project .sample_models .add (model )
167+
168+ # %% [markdown]
169+ # ### Add experiment
170+
171+ # %%
172+ project .experiments .add (expt )
173+
174+ # %% [markdown]
175+ # ## Analysis
176+ #
177+ # This section will guide you through the analysis process, including setting
178+ # up calculators and fitting models.
179+ #
180+ # ### Set calculation engine
181+
182+ # %%
183+ project .analysis .current_calculator = 'cryspy'
184+
185+ # %% [markdown]
186+ # ### Set fitting engine
187+
188+ # %%
189+ project .analysis .current_minimizer = 'lmfit (leastsq)'
190+
191+ # %% [markdown]
192+ # ### Show measured vs calculated
193+
194+ # %%
195+ project .plot_meas_vs_calc (expt_name = 'wish' ,
196+ show_residual = True )
197+ project .plot_meas_vs_calc (expt_name = 'wish' ,
198+ x_min = 37000 , x_max = 40000 ,
199+ show_residual = True )
200+
201+ # %% [markdown]
202+ # ### Perform Fit 1/5
203+ #
204+ # Set parameters to be fitted
205+
206+ # %%
207+ model .cell .length_a .free = True
208+
209+ expt .instrument .calib_d_to_tof_offset .free = True
210+ expt .linked_phases ['ncaf' ].scale .free = True
211+
212+ # %% [markdown]
213+ # Show free parameters after selection
214+
215+ # %%
216+ project .analysis .show_free_params ()
217+
218+ # %% [markdown]
219+ # #### Start fitting
220+
221+ # %%
222+ project .analysis .fit ()
223+
224+ # %% [markdown]
225+ # #### Show fitting results
226+
227+ # %%
228+ project .plot_meas_vs_calc (expt_name = 'wish' ,
229+ show_residual = True )
230+ project .plot_meas_vs_calc (expt_name = 'wish' ,
231+ x_min = 37000 , x_max = 40000 ,
232+ show_residual = True )
233+
234+ # %% [markdown]
235+ # ### Perform Fit 2/5
236+ #
237+ # Set parameters to be fitted
238+
239+ # %%
240+ expt .peak .broad_gauss_sigma_2 .free = True
241+ expt .peak .broad_mix_beta_0 .free = True
242+ expt .peak .broad_mix_beta_1 .free = True
243+ expt .peak .asym_alpha_0 .free = True
244+ expt .peak .asym_alpha_1 .free = True
245+
246+ # %% [markdown]
247+ # Show free parameters after selection
248+
249+ # %%
250+ project .analysis .show_free_params ()
251+
252+ # %% [markdown]
253+ # #### Start fitting
254+
255+ # %%
256+ project .analysis .fit ()
257+
258+ # %% [markdown]
259+ # #### Show fitting results
260+
261+ # %%
262+ project .plot_meas_vs_calc (expt_name = 'wish' ,
263+ show_residual = True )
264+ project .plot_meas_vs_calc (expt_name = 'wish' ,
265+ x_min = 37000 , x_max = 40000 ,
266+ show_residual = True )
267+
268+
269+ # %% [markdown]
270+ # ### Perform Fit 3/5
271+ #
272+ # Set parameters to be fitted
273+
274+ # %%
275+ model .atom_sites ['Ca' ].fract_x .free = True
276+ model .atom_sites ['Al' ].fract_x .free = True
277+ model .atom_sites ['Na' ].fract_x .free = True
278+ model .atom_sites ['F1' ].fract_x .free = True
279+ model .atom_sites ['F1' ].fract_y .free = True
280+ model .atom_sites ['F1' ].fract_z .free = True
281+ model .atom_sites ['F2' ].fract_x .free = True
282+ model .atom_sites ['F2' ].fract_y .free = True
283+ model .atom_sites ['F2' ].fract_z .free = True
284+ model .atom_sites ['F3' ].fract_x .free = True
285+
286+ # %% [markdown]
287+ # Show free parameters after selection
288+
289+ # %%
290+ project .analysis .show_free_params ()
291+
292+ # %% [markdown]
293+ # #### Start fitting
294+
295+ # %%
296+ project .analysis .fit ()
297+
298+ # %% [markdown]
299+ # #### Show fitting results
300+
301+ # %%
302+ project .plot_meas_vs_calc (expt_name = 'wish' ,
303+ show_residual = True )
304+ project .plot_meas_vs_calc (expt_name = 'wish' ,
305+ x_min = 37000 , x_max = 40000 ,
306+ show_residual = True )
307+
308+ # %% [markdown]
309+ # ### Perform Fit 4/5
310+ #
311+ # Set parameters to be fitted
312+
313+ # %%
314+ expt .instrument .calib_d_to_tof_linear .free = True
315+
316+ model .atom_sites ['Ca' ].b_iso .free = True
317+ model .atom_sites ['Al' ].b_iso .free = True
318+ model .atom_sites ['Na' ].b_iso .free = True
319+ model .atom_sites ['F1' ].b_iso .free = True
320+ model .atom_sites ['F2' ].b_iso .free = True
321+ model .atom_sites ['F3' ].b_iso .free = True
322+
323+ # %% [markdown]
324+ # Show free parameters after selection
325+
326+ # %%
327+ project .analysis .show_free_params ()
328+
329+ # %% [markdown]
330+ # #### Start fitting
331+
332+ # %%
333+ project .analysis .fit ()
334+
335+ # %% [markdown]
336+ # #### Show fitting results
337+
338+ # %%
339+ project .plot_meas_vs_calc (expt_name = 'wish' ,
340+ show_residual = True )
341+ project .plot_meas_vs_calc (expt_name = 'wish' ,
342+ x_min = 37000 , x_max = 40000 ,
343+ show_residual = True )
344+
345+ # %% [markdown]
346+ # ## Summary
347+ #
348+ # In this final section, you will learn how to review the results of the
349+ # analysis
350+
351+ # %% [markdown]
352+ # ### Show project summary report
353+
354+ # %%
355+ project .summary .show_report ()
0 commit comments