1+ # %% [markdown]
2+ # # Standard diffraction: HS
3+ #
4+ # Standard diffraction analysis of HS after the powder neutron constant wavelength
5+ # diffraction measurement from HRPT at PSI.
6+
7+ # %% [markdown]
8+ # ## Import EasyDiffraction
9+
10+ # %%
11+ from easydiffraction import (
12+ Project ,
13+ SampleModel ,
14+ Experiment ,
15+ download_from_repository
16+ )
17+
18+ # %% [markdown]
19+ # ## Define Sample Model
20+ #
21+ # This section covers how to add sample models and modify their parameters.
22+ #
23+ # ### Create sample model object
24+
25+ # %%
26+ model = SampleModel ('hs' )
27+
28+ # %% [markdown]
29+ # ### Define space group
30+
31+ # %%
32+ model .space_group .name_h_m = 'R -3 m'
33+ model .space_group .it_coordinate_system_code = 'h'
34+
35+ # %% [markdown]
36+ # ### Define unit cell
37+
38+ # %%
39+ model .cell .length_a = 6.9
40+ model .cell .length_c = 14.1
41+
42+ # %% [markdown]
43+ # ### Define atom sites
44+
45+ # %%
46+ model .atom_sites .add ('Zn' , 'Zn' , 0 , 0 , 0.5 , wyckoff_letter = 'b' , b_iso = 0.5 )
47+ model .atom_sites .add ('Cu' , 'Cu' , 0.5 , 0 , 0 , wyckoff_letter = 'e' , b_iso = 0.5 )
48+ model .atom_sites .add ('O' , 'O' , 0.21 , - 0.21 , 0.06 , wyckoff_letter = 'h' , b_iso = 0.5 )
49+ model .atom_sites .add ('Cl' , 'Cl' , 0 , 0 , 0.197 , wyckoff_letter = 'c' , b_iso = 0.5 )
50+ model .atom_sites .add ('H' , '2H' , 0.13 , - 0.13 , 0.08 , wyckoff_letter = 'h' , b_iso = 0.5 )
51+
52+ # %% [markdown]
53+ # ### Symmetry constraints
54+ #
55+ # Model as CIF before applying symmetry constraints
56+
57+ # %%
58+ model .show_as_cif ()
59+
60+ # %% [markdown]
61+ # Apply symmetry constraints
62+
63+ # %%
64+ model .apply_symmetry_constraints ()
65+
66+ # Model as CIF after applying symmetry constraints
67+
68+ # %%
69+ model .show_as_cif ()
70+
71+ # %% [markdown]
72+ # ## Define Experiment
73+ #
74+ # This section teaches how to add experiments, configure their parameters, and
75+ # link to them the sample models defined in the previous step.
76+ #
77+ # ### Download measured data
78+
79+ # %%
80+ download_from_repository ('hrpt_hs.xye' ,
81+ branch = 'docs' ,
82+ destination = 'data' )
83+
84+ # %% [markdown]
85+ # ### Create experiment object
86+
87+ # %%
88+ expt = Experiment (name = 'hrpt' ,
89+ data_path = 'data/hrpt_hs.xye' )
90+
91+ # %% [markdown]
92+ # ### Define instrument
93+
94+ # %%
95+ expt .instrument .setup_wavelength = 1.89
96+ expt .instrument .calib_twotheta_offset = 0.0
97+
98+ # %% [markdown]
99+ # ### Define peak profile
100+
101+ # %%
102+ expt .peak .broad_gauss_u = 0.1
103+ expt .peak .broad_gauss_v = - 0.2
104+ expt .peak .broad_gauss_w = 0.2
105+ expt .peak .broad_lorentz_x = 0.0
106+ expt .peak .broad_lorentz_y = 0
107+
108+ # %% [markdown]
109+ # ### Define background
110+
111+ # %%
112+ expt .background .add (x = 4.4196 , y = 500 )
113+ expt .background .add (x = 6.6207 , y = 500 )
114+ expt .background .add (x = 10.4918 , y = 500 )
115+ expt .background .add (x = 15.4634 , y = 500 )
116+ expt .background .add (x = 45.6041 , y = 500 )
117+ expt .background .add (x = 74.6844 , y = 500 )
118+ expt .background .add (x = 103.4187 , y = 500 )
119+ expt .background .add (x = 121.6311 , y = 500 )
120+ expt .background .add (x = 159.4116 , y = 500 )
121+
122+ # %% [markdown]
123+ # ### Select linked phase
124+
125+ # %%
126+ expt .linked_phases .add ('hs' , scale = 0.5 )
127+
128+ # %% [markdown]
129+ # ## Define Project
130+ #
131+ # The project object is used to manage the sample model, experiments, and
132+ # analysis
133+ #
134+ # ### Create project object
135+
136+ # %%
137+ project = Project ()
138+
139+ # %% [markdown]
140+ # ### Configure Plotting Engine
141+
142+ # %%
143+ project .plotter .engine = 'plotly'
144+
145+ # %% [markdown]
146+ # ### Add sample model
147+
148+ # %%
149+ project .sample_models .add (model )
150+
151+ # %% [markdown]
152+ # ### Add experiment
153+
154+ # %%
155+ project .experiments .add (expt )
156+
157+ # %% [markdown]
158+ # ## Analysis
159+ #
160+ # This section will guide you through the analysis process, including setting
161+ # up calculators and fitting models.
162+ #
163+ # ### Set calculation engine
164+
165+ # %%
166+ project .analysis .current_calculator = 'cryspy'
167+
168+ # %% [markdown]
169+ # ### Set fitting engine
170+
171+ # %%
172+ project .analysis .current_minimizer = 'lmfit (leastsq)'
173+
174+ # %% [markdown]
175+ # ### Show measured vs calculated
176+
177+ # %%
178+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
179+ show_residual = True )
180+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
181+ x_min = 48 , x_max = 51 ,
182+ show_residual = True )
183+
184+ # %% [markdown]
185+ # ### Perform Fit 1/5
186+ #
187+ # Set parameters to be fitted
188+
189+ # %%
190+ model .cell .length_a .free = True
191+ model .cell .length_c .free = True
192+
193+ expt .linked_phases ['hs' ].scale .free = True
194+ expt .instrument .calib_twotheta_offset .free = True
195+
196+ # %% [markdown]
197+ # Show free parameters after selection
198+
199+ # %%
200+ project .analysis .show_free_params ()
201+
202+ # %% [markdown]
203+ # #### Start fitting
204+
205+ # %%
206+ project .analysis .fit ()
207+
208+ # %% [markdown]
209+ # #### Show fitting results
210+
211+ # %%
212+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
213+ show_residual = True )
214+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
215+ x_min = 48 , x_max = 51 ,
216+ show_residual = True )
217+
218+ # %% [markdown]
219+ # ### Perform Fit 2/5
220+ #
221+ # Set parameters to be fitted
222+
223+ # %%
224+ expt .peak .broad_gauss_u .free = True
225+ expt .peak .broad_gauss_v .free = True
226+ expt .peak .broad_gauss_w .free = True
227+ expt .peak .broad_lorentz_x .free = True
228+
229+ for point in expt .background :
230+ point .y .free = True
231+
232+ # %% [markdown]
233+ # Show free parameters after selection
234+
235+ # %%
236+ project .analysis .show_free_params ()
237+
238+ # %% [markdown]
239+ # #### Start fitting
240+
241+ # %%
242+ project .analysis .fit ()
243+
244+ # %% [markdown]
245+ # #### Show fitting results
246+
247+ # %%
248+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
249+ show_residual = True )
250+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
251+ x_min = 48 , x_max = 51 ,
252+ show_residual = True )
253+
254+ # %% [markdown]
255+ # ### Perform Fit 3/5
256+ #
257+ # Set parameters to be fitted
258+
259+ # %%
260+ model .atom_sites ['O' ].fract_x .free = True
261+ model .atom_sites ['O' ].fract_z .free = True
262+ model .atom_sites ['Cl' ].fract_z .free = True
263+ model .atom_sites ['H' ].fract_x .free = True
264+ model .atom_sites ['H' ].fract_z .free = True
265+
266+ # %% [markdown]
267+ # Show free parameters after selection
268+
269+ # %%
270+ project .analysis .show_free_params ()
271+
272+ # %% [markdown]
273+ # #### Start fitting
274+
275+ # %%
276+ project .analysis .fit ()
277+
278+ # %% [markdown]
279+ # #### Show fitting results
280+
281+ # %%
282+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
283+ show_residual = True )
284+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
285+ x_min = 48 , x_max = 51 ,
286+ show_residual = True )
287+
288+ # %% [markdown]
289+ # ### Perform Fit 4/5
290+ #
291+ # Set parameters to be fitted
292+
293+ # %%
294+ model .atom_sites ['Zn' ].b_iso .free = True
295+ model .atom_sites ['Cu' ].b_iso .free = True
296+ model .atom_sites ['O' ].b_iso .free = True
297+ model .atom_sites ['Cl' ].b_iso .free = True
298+ model .atom_sites ['H' ].b_iso .free = True
299+
300+ # %% [markdown]
301+ # Show free parameters after selection
302+
303+ # %%
304+ project .analysis .show_free_params ()
305+
306+ # %% [markdown]
307+ # #### Start fitting
308+
309+ # %%
310+ project .analysis .fit ()
311+
312+ # %% [markdown]
313+ # #### Show fitting results
314+
315+ # %%
316+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
317+ show_residual = True )
318+ project .plot_meas_vs_calc (expt_name = 'hrpt' ,
319+ x_min = 48 , x_max = 51 ,
320+ show_residual = True )
321+
322+ # %% [markdown]
323+ # ## Summary
324+ #
325+ # In this final section, you will learn how to review the results of the
326+ # analysis
327+
328+ # %% [markdown]
329+ # ### Show project summary report
330+
331+ # %%
332+ project .summary .show_report ()
0 commit comments