1 | #GSASII cell indexing program: variation on that of A. Coehlo |
---|
2 | # includes cell refinement from peak positions (not zero as yet) |
---|
3 | ########### SVN repository information ################### |
---|
4 | # $Date: 2011-06-27 17:11:26 +0000 (Mon, 27 Jun 2011) $ |
---|
5 | # $Author: vondreele $ |
---|
6 | # $Revision: 312 $ |
---|
7 | # $URL: trunk/GSASIIindex.py $ |
---|
8 | # $Id: GSASIIindex.py 312 2011-06-27 17:11:26Z vondreele $ |
---|
9 | ########### SVN repository information ################### |
---|
10 | import math |
---|
11 | import wx |
---|
12 | import time |
---|
13 | import numpy as np |
---|
14 | import numpy.linalg as nl |
---|
15 | import GSASIIpath |
---|
16 | import GSASIIlattice as G2lat |
---|
17 | import scipy.optimize as so |
---|
18 | |
---|
19 | # trig functions in degrees |
---|
20 | sind = lambda x: math.sin(x*math.pi/180.) |
---|
21 | asind = lambda x: 180.*math.asin(x)/math.pi |
---|
22 | tand = lambda x: math.tan(x*math.pi/180.) |
---|
23 | atand = lambda x: 180.*math.atan(x)/math.pi |
---|
24 | atan2d = lambda y,x: 180.*math.atan2(y,x)/math.pi |
---|
25 | cosd = lambda x: math.cos(x*math.pi/180.) |
---|
26 | acosd = lambda x: 180.*math.acos(x)/math.pi |
---|
27 | rdsq2d = lambda x,p: round(1.0/math.sqrt(x),p) |
---|
28 | #numpy versions |
---|
29 | npsind = lambda x: np.sin(x*np.pi/180.) |
---|
30 | npasind = lambda x: 180.*np.arcsin(x)/math.pi |
---|
31 | npcosd = lambda x: np.cos(x*math.pi/180.) |
---|
32 | nptand = lambda x: np.tan(x*math.pi/180.) |
---|
33 | npatand = lambda x: 180.*np.arctan(x)/np.pi |
---|
34 | npatan2d = lambda y,x: 180.*np.arctan2(y,x)/np.pi |
---|
35 | |
---|
36 | def scaleAbyV(A,V): |
---|
37 | v = G2lat.calc_V(A) |
---|
38 | scale = math.exp(math.log(v/V)/3.)**2 |
---|
39 | for i in range(6): |
---|
40 | A[i] *= scale |
---|
41 | |
---|
42 | def ranaxis(dmin,dmax): |
---|
43 | import random as rand |
---|
44 | return rand.random()*(dmax-dmin)+dmin |
---|
45 | |
---|
46 | def ran2axis(k,N): |
---|
47 | import random as rand |
---|
48 | T = 1.5+0.49*k/N |
---|
49 | # B = 0.99-0.49*k/N |
---|
50 | # B = 0.99-0.049*k/N |
---|
51 | B = 0.99-0.149*k/N |
---|
52 | R = (T-B)*rand.random()+B |
---|
53 | return R |
---|
54 | |
---|
55 | #def ranNaxis(k,N): |
---|
56 | # import random as rand |
---|
57 | # T = 1.0+1.0*k/N |
---|
58 | # B = 1.0-1.0*k/N |
---|
59 | # R = (T-B)*rand.random()+B |
---|
60 | # return R |
---|
61 | |
---|
62 | def ranAbyV(Bravais,dmin,dmax,V): |
---|
63 | cell = [0,0,0,0,0,0] |
---|
64 | bad = True |
---|
65 | while bad: |
---|
66 | bad = False |
---|
67 | cell = rancell(Bravais,dmin,dmax) |
---|
68 | G,g = G2lat.cell2Gmat(cell) |
---|
69 | A = G2lat.Gmat2A(G) |
---|
70 | if G2lat.calc_rVsq(A) < 1: |
---|
71 | scaleAbyV(A,V) |
---|
72 | cell = G2lat.A2cell(A) |
---|
73 | for i in range(3): |
---|
74 | bad |= cell[i] < dmin |
---|
75 | return A |
---|
76 | |
---|
77 | def ranAbyR(Bravais,A,k,N,ranFunc): |
---|
78 | R = ranFunc(k,N) |
---|
79 | if Bravais in [0,1,2]: #cubic - not used |
---|
80 | A[0] = A[1] = A[2] = A[0]*R |
---|
81 | A[3] = A[4] = A[5] = 0. |
---|
82 | elif Bravais in [3,4]: #hexagonal/trigonal |
---|
83 | A[0] = A[1] = A[3] = A[0]*R |
---|
84 | A[2] *= R |
---|
85 | A[4] = A[5] = 0. |
---|
86 | elif Bravais in [5,6]: #tetragonal |
---|
87 | A[0] = A[1] = A[0]*R |
---|
88 | A[2] *= R |
---|
89 | A[3] = A[4] = A[5] = 0. |
---|
90 | elif Bravais in [7,8,9,10]: #orthorhombic |
---|
91 | A[0] *= R |
---|
92 | A[1] *= R |
---|
93 | A[2] *= R |
---|
94 | A[3] = A[4] = A[5] = 0. |
---|
95 | elif Bravais in [11,12]: #monoclinic |
---|
96 | A[0] *= R |
---|
97 | A[1] *= R |
---|
98 | A[2] *= R |
---|
99 | A[4] *= R |
---|
100 | A[3] = A[5] = 0. |
---|
101 | else: #triclinic |
---|
102 | A[0] *= R |
---|
103 | A[1] *= R |
---|
104 | A[2] *= R |
---|
105 | A[3] *= R |
---|
106 | A[4] *= R |
---|
107 | A[5] *= R |
---|
108 | return A |
---|
109 | |
---|
110 | def rancell(Bravais,dmin,dmax): |
---|
111 | if Bravais in [0,1,2]: #cubic |
---|
112 | a = b = c = ranaxis(dmin,dmax) |
---|
113 | alp = bet = gam = 90 |
---|
114 | elif Bravais in [3,4]: #hexagonal/trigonal |
---|
115 | a = b = ranaxis(dmin,dmax) |
---|
116 | c = ranaxis(dmin,dmax) |
---|
117 | alp = bet = 90 |
---|
118 | gam = 120 |
---|
119 | elif Bravais in [5,6]: #tetragonal |
---|
120 | a = b = ranaxis(dmin,dmax) |
---|
121 | c = ranaxis(dmin,dmax) |
---|
122 | alp = bet = gam = 90 |
---|
123 | elif Bravais in [7,8,9,10]: #orthorhombic - F,I,C,P - a<b<c convention |
---|
124 | abc = [ranaxis(dmin,dmax),ranaxis(dmin,dmax),ranaxis(dmin,dmax)] |
---|
125 | abc.sort() |
---|
126 | a = abc[0] |
---|
127 | b = abc[1] |
---|
128 | c = abc[2] |
---|
129 | alp = bet = gam = 90 |
---|
130 | elif Bravais in [11,12]: #monoclinic - C,P - a<c convention |
---|
131 | ac = [ranaxis(dmin,dmax),ranaxis(dmin,dmax)] |
---|
132 | ac.sort() |
---|
133 | a = ac[0] |
---|
134 | b = ranaxis(dmin,dmax) |
---|
135 | c = ac[1] |
---|
136 | alp = gam = 90 |
---|
137 | bet = ranaxis(90.,130.) |
---|
138 | else: #triclinic - a<b<c convention |
---|
139 | abc = [ranaxis(dmin,dmax),ranaxis(dmin,dmax),ranaxis(dmin,dmax)] |
---|
140 | abc.sort() |
---|
141 | a = abc[0] |
---|
142 | b = abc[1] |
---|
143 | c = abc[2] |
---|
144 | r = 0.5*b/c |
---|
145 | alp = ranaxis(acosd(r),acosd(-r)) |
---|
146 | r = 0.5*a/c |
---|
147 | bet = ranaxis(acosd(r),acosd(-r)) |
---|
148 | r = 0.5*a/b |
---|
149 | gam = ranaxis(acosd(r),acosd(-r)) |
---|
150 | return [a,b,c,alp,bet,gam] |
---|
151 | |
---|
152 | def calc_M20(peaks,HKL): |
---|
153 | diff = 0 |
---|
154 | X20 = 0 |
---|
155 | for Nobs20,peak in enumerate(peaks): |
---|
156 | if peak[3]: |
---|
157 | Qobs = 1.0/peak[7]**2 |
---|
158 | Qcalc = 1.0/peak[8]**2 |
---|
159 | diff += abs(Qobs-Qcalc) |
---|
160 | elif peak[2]: |
---|
161 | X20 += 1 |
---|
162 | if Nobs20 == 19: |
---|
163 | d20 = peak[7] |
---|
164 | break |
---|
165 | else: |
---|
166 | d20 = peak[7] |
---|
167 | Nobs20 = len(peaks) |
---|
168 | for N20,hkl in enumerate(HKL): |
---|
169 | if hkl[3] < d20: |
---|
170 | break |
---|
171 | eta = diff/Nobs20 |
---|
172 | Q20 = 1.0/d20**2 |
---|
173 | if diff: |
---|
174 | M20 = Q20/(2.0*diff) |
---|
175 | else: |
---|
176 | M20 = 0 |
---|
177 | M20 /= (1.+X20) |
---|
178 | return M20,X20 |
---|
179 | |
---|
180 | def sortM20(cells): |
---|
181 | #cells is M20,X20,Bravais,a,b,c,alp,bet,gam |
---|
182 | #sort highest M20 1st |
---|
183 | T = [] |
---|
184 | for i,M in enumerate(cells): |
---|
185 | T.append((M[0],i)) |
---|
186 | D = dict(zip(T,cells)) |
---|
187 | T.sort() |
---|
188 | T.reverse() |
---|
189 | X = [] |
---|
190 | for key in T: |
---|
191 | X.append(D[key]) |
---|
192 | return X |
---|
193 | |
---|
194 | def IndexPeaks(peaks,HKL): |
---|
195 | import bisect |
---|
196 | N = len(HKL) |
---|
197 | if N == 0: return False |
---|
198 | hklds = list(np.array(HKL).T[3])+[1000.0,0.0,] |
---|
199 | hklds.sort() # ascending sort - upper bound at end |
---|
200 | hklmax = [0,0,0] |
---|
201 | for ipk,peak in enumerate(peaks): |
---|
202 | if peak[2]: |
---|
203 | i = bisect.bisect_right(hklds,peak[7]) # find peak position in hkl list |
---|
204 | dm = peak[7]-hklds[i-1] # peak to neighbor hkls in list |
---|
205 | dp = hklds[i]-peak[7] |
---|
206 | pos = N-i # reverse the order |
---|
207 | if dp > dm: pos += 1 # closer to upper than lower |
---|
208 | hkl = HKL[pos] # put in hkl |
---|
209 | if hkl[4] >= 0: # peak already assigned - test if this one better |
---|
210 | opeak = peaks[hkl[4]] |
---|
211 | dold = abs(opeak[7]-hkl[3]) |
---|
212 | dnew = min(dm,dp) |
---|
213 | if dold > dnew: # new better - zero out old |
---|
214 | opeak[4:7] = [0,0,0] |
---|
215 | opeak[8] = 0. |
---|
216 | else: # old better - do nothing |
---|
217 | continue |
---|
218 | hkl[4] = ipk |
---|
219 | peak[4:7] = hkl[:3] |
---|
220 | peak[8] = hkl[3] # fill in d-calc |
---|
221 | for peak in peaks: |
---|
222 | peak[3] = False |
---|
223 | if peak[2]: |
---|
224 | if peak[8] > 0.: |
---|
225 | for j in range(3): |
---|
226 | if abs(peak[j+4]) > hklmax[j]: hklmax[j] = abs(peak[j+4]) |
---|
227 | peak[3] = True |
---|
228 | if hklmax[0]*hklmax[1]*hklmax[2] > 0: |
---|
229 | return True |
---|
230 | else: |
---|
231 | return False |
---|
232 | |
---|
233 | def Values2A(ibrav,values): |
---|
234 | if ibrav in [0,1,2]: |
---|
235 | return [values[0],values[0],values[0],0,0,0] |
---|
236 | elif ibrav in [3,4]: |
---|
237 | return [values[0],values[0],values[1],values[0],0,0] |
---|
238 | elif ibrav in [5,6]: |
---|
239 | return [values[0],values[0],values[1],0,0,0] |
---|
240 | elif ibrav in [7,8,9,10]: |
---|
241 | return [values[0],values[1],values[2],0,0,0] |
---|
242 | elif ibrav in [11,12]: |
---|
243 | return [values[0],values[1],values[2],0,values[3],0] |
---|
244 | else: |
---|
245 | return list(values) |
---|
246 | |
---|
247 | def A2values(ibrav,A): |
---|
248 | if ibrav in [0,1,2]: |
---|
249 | return [A[0],] |
---|
250 | elif ibrav in [3,4,5,6]: |
---|
251 | return [A[0],A[2]] |
---|
252 | elif ibrav in [7,8,9,10]: |
---|
253 | return [A[0],A[1],A[2]] |
---|
254 | elif ibrav in [11,12]: |
---|
255 | return [A[0],A[1],A[2],A[4]] |
---|
256 | else: |
---|
257 | return A |
---|
258 | |
---|
259 | def FitHKL(ibrav,peaks,A,Pwr): |
---|
260 | |
---|
261 | def errFit(values,ibrav,d,H,Pwr): |
---|
262 | A = Values2A(ibrav,values) |
---|
263 | Qo = 1./d**2 |
---|
264 | Qc = G2lat.calc_rDsq(H,A) |
---|
265 | return (Qo-Qc)*d**Pwr |
---|
266 | |
---|
267 | Peaks = np.array(peaks).T |
---|
268 | |
---|
269 | values = A2values(ibrav,A) |
---|
270 | result = so.leastsq(errFit,values,args=(ibrav,Peaks[7],Peaks[4:7],Pwr), |
---|
271 | full_output=True,factor=0.1) |
---|
272 | A = Values2A(ibrav,result[0]) |
---|
273 | return True,np.sum(errFit(result[0],ibrav,Peaks[7],Peaks[4:7],Pwr)**2),A,result |
---|
274 | |
---|
275 | def FitHKLZ(wave,ibrav,peaks,A,Z,Pwr): |
---|
276 | |
---|
277 | def errFit(values,ibrav,d,H,tth,wave,Pwr): |
---|
278 | A = Values2A(ibrav,values[:-1]) |
---|
279 | Z = values[-1] |
---|
280 | Qo = 1./d**2 |
---|
281 | Qc = G2lat.calc_rDsqZ(H,A,Z,tth,wave) |
---|
282 | return (Qo-Qc)*d**Pwr |
---|
283 | |
---|
284 | Peaks = np.array(peaks).T |
---|
285 | |
---|
286 | values = A2values(ibrav,A) |
---|
287 | values.append(Z) |
---|
288 | result = so.leastsq(errFit,values,args=(ibrav,Peaks[7],Peaks[4:7],Peaks[0],wave,Pwr), |
---|
289 | full_output=True) |
---|
290 | A = Values2A(ibrav,result[0][:-1]) |
---|
291 | Z = result[0][-1] |
---|
292 | |
---|
293 | return True,np.sum(errFit(result[0],ibrav,Peaks[7],Peaks[4:7],Peaks[0],wave,Pwr)**2),A,Z,result |
---|
294 | |
---|
295 | def rotOrthoA(A): |
---|
296 | return [A[1],A[2],A[0],0,0,0] |
---|
297 | |
---|
298 | def swapMonoA(A): |
---|
299 | return [A[2],A[1],A[0],0,A[4],0] |
---|
300 | |
---|
301 | def oddPeak(indx,peaks): |
---|
302 | noOdd = True |
---|
303 | for peak in peaks: |
---|
304 | H = peak[4:7] |
---|
305 | if H[indx] % 2: |
---|
306 | noOdd = False |
---|
307 | return noOdd |
---|
308 | |
---|
309 | def halfCell(ibrav,A,peaks): |
---|
310 | if ibrav in [0,1,2]: |
---|
311 | if oddPeak(0,peaks): |
---|
312 | A[0] *= 2 |
---|
313 | A[1] = A[2] = A[0] |
---|
314 | elif ibrav in [3,4,5,6]: |
---|
315 | if oddPeak(0,peaks): |
---|
316 | A[0] *= 2 |
---|
317 | A[1] = A[0] |
---|
318 | if oddPeak(2,peaks): |
---|
319 | A[2] *=2 |
---|
320 | else: |
---|
321 | if oddPeak(0,peaks): |
---|
322 | A[0] *=2 |
---|
323 | if oddPeak(1,peaks): |
---|
324 | A[1] *=2 |
---|
325 | if oddPeak(2,peaks): |
---|
326 | A[2] *=2 |
---|
327 | return A |
---|
328 | |
---|
329 | def getDmin(peaks): |
---|
330 | return peaks[-1][7] |
---|
331 | |
---|
332 | def getDmax(peaks): |
---|
333 | return peaks[0][7] |
---|
334 | |
---|
335 | def refinePeaksZ(peaks,wave,ibrav,A,Zero): |
---|
336 | dmin = getDmin(peaks) |
---|
337 | OK,smin,Aref,Z,result = FitHKLZ(wave,ibrav,peaks,A,Zero,2) |
---|
338 | Peaks = np.array(peaks).T |
---|
339 | H = Peaks[4:7] |
---|
340 | Peaks[8] = 1./np.sqrt(G2lat.calc_rDsqZ(H,Aref,Z,Peaks[0],wave)) |
---|
341 | peaks = Peaks.T |
---|
342 | |
---|
343 | HKL = G2lat.GenHBravais(dmin,ibrav,Aref) |
---|
344 | M20,X20 = calc_M20(peaks,HKL) |
---|
345 | return len(HKL),M20,X20,Aref,Z |
---|
346 | |
---|
347 | def refinePeaks(peaks,ibrav,A): |
---|
348 | dmin = getDmin(peaks) |
---|
349 | smin = 1.0e10 |
---|
350 | pwr = 4 |
---|
351 | maxTries = 10 |
---|
352 | OK = False |
---|
353 | tries = 0 |
---|
354 | HKL = G2lat.GenHBravais(dmin,ibrav,A) |
---|
355 | while len(HKL) > 2 and IndexPeaks(peaks,HKL): |
---|
356 | Pwr = pwr - (tries % 2) |
---|
357 | HKL = [] |
---|
358 | tries += 1 |
---|
359 | osmin = smin |
---|
360 | oldA = A[:] |
---|
361 | Vold = G2lat.calc_V(oldA) |
---|
362 | OK,smin,A,result = FitHKL(ibrav,peaks,A,Pwr) |
---|
363 | Vnew = G2lat.calc_V(A) |
---|
364 | if Vnew > 2.0*Vold or Vnew < 2.: |
---|
365 | A = ranAbyR(ibrav,oldA,tries+1,maxTries,ran2axis) |
---|
366 | OK = False |
---|
367 | continue |
---|
368 | try: |
---|
369 | HKL = G2lat.GenHBravais(dmin,ibrav,A) |
---|
370 | except FloatingPointError: |
---|
371 | A = oldA |
---|
372 | OK = False |
---|
373 | break |
---|
374 | if len(HKL) == 0: break #absurd cell obtained! |
---|
375 | rat = (osmin-smin)/smin |
---|
376 | if abs(rat) < 1.0e-5 or not OK: break |
---|
377 | if tries > maxTries: break |
---|
378 | if OK: |
---|
379 | OK,smin,A,result = FitHKL(ibrav,peaks,A,2) |
---|
380 | Peaks = np.array(peaks).T |
---|
381 | H = Peaks[4:7] |
---|
382 | Peaks[8] = 1./np.sqrt(G2lat.calc_rDsq(H,A)) |
---|
383 | peaks = Peaks.T |
---|
384 | |
---|
385 | M20,X20 = calc_M20(peaks,HKL) |
---|
386 | return len(HKL),M20,X20,A |
---|
387 | |
---|
388 | def findBestCell(dlg,ncMax,A,Ntries,ibrav,peaks,V1): |
---|
389 | # dlg & ncMax are used for wx progress bar |
---|
390 | # A != 0 find the best A near input A, |
---|
391 | # A = 0 for random cell, volume normalized to V1; |
---|
392 | # returns number of generated hkls, M20, X20 & A for best found |
---|
393 | mHKL = [3,3,3, 5,5, 5,5, 7,7,7,7, 9,9, 10] |
---|
394 | dmin = getDmin(peaks)-0.05 |
---|
395 | amin = 2.5 |
---|
396 | amax = 5.*getDmax(peaks) |
---|
397 | Asave = [] |
---|
398 | GoOn = True |
---|
399 | if A: |
---|
400 | HKL = G2lat.GenHBravais(dmin,ibrav,A[:]) |
---|
401 | if len(HKL) > mHKL[ibrav]: |
---|
402 | IndexPeaks(peaks,HKL) |
---|
403 | Asave.append([calc_M20(peaks,HKL),A[:]]) |
---|
404 | tries = 0 |
---|
405 | while tries < Ntries: |
---|
406 | if A: |
---|
407 | Abeg = ranAbyR(ibrav,A,tries+1,Ntries,ran2axis) |
---|
408 | if ibrav in [11,12,13]: #monoclinic & triclinic |
---|
409 | Abeg = ranAbyR(ibrav,A,tries/10+1,Ntries,ran2axis) |
---|
410 | else: |
---|
411 | Abeg = ranAbyV(ibrav,amin,amax,V1) |
---|
412 | HKL = G2lat.GenHBravais(dmin,ibrav,Abeg) |
---|
413 | |
---|
414 | if IndexPeaks(peaks,HKL) and len(HKL) > mHKL[ibrav]: |
---|
415 | Lhkl,M20,X20,Aref = refinePeaks(peaks,ibrav,Abeg) |
---|
416 | Asave.append([calc_M20(peaks,HKL),Aref[:]]) |
---|
417 | if ibrav == 9: #C-centered orthorhombic |
---|
418 | for i in range(2): |
---|
419 | Abeg = rotOrthoA(Abeg[:]) |
---|
420 | Lhkl,M20,X20,Aref = refinePeaks(peaks,ibrav,Abeg) |
---|
421 | HKL = G2lat.GenHBravais(dmin,ibrav,Aref) |
---|
422 | IndexPeaks(peaks,HKL) |
---|
423 | Asave.append([calc_M20(peaks,HKL),Aref[:]]) |
---|
424 | elif ibrav == 11: #C-centered monoclinic |
---|
425 | Abeg = swapMonoA(Abeg[:]) |
---|
426 | Lhkl,M20,X20,Aref = refinePeaks(peaks,ibrav,Abeg) |
---|
427 | HKL = G2lat.GenHBravais(dmin,ibrav,Aref) |
---|
428 | IndexPeaks(peaks,HKL) |
---|
429 | Asave.append([calc_M20(peaks,HKL),Aref[:]]) |
---|
430 | else: |
---|
431 | break |
---|
432 | Nc = len(HKL) |
---|
433 | if Nc >= ncMax: |
---|
434 | GoOn = False |
---|
435 | elif dlg: |
---|
436 | GoOn = dlg.Update(Nc)[0] |
---|
437 | if not GoOn: |
---|
438 | break |
---|
439 | tries += 1 |
---|
440 | X = sortM20(Asave) |
---|
441 | if X: |
---|
442 | Lhkl,M20,X20,A = refinePeaks(peaks,ibrav,X[0][1]) |
---|
443 | return GoOn,Lhkl,M20,X20,A |
---|
444 | |
---|
445 | else: |
---|
446 | return GoOn,0,0,0,0 |
---|
447 | |
---|
448 | def monoCellReduce(ibrav,A): |
---|
449 | a,b,c,alp,bet,gam = G2lat.A2cell(A) |
---|
450 | G,g = G2lat.A2Gmat(A) |
---|
451 | if ibrav in [11]: |
---|
452 | u = [0,0,-1] |
---|
453 | v = [1,0,2] |
---|
454 | anew = math.sqrt(np.dot(np.dot(v,g),v)) |
---|
455 | if anew < a: |
---|
456 | cang = np.dot(np.dot(u,g),v)/(anew*c) |
---|
457 | beta = acosd(-abs(cang)) |
---|
458 | A = G2lat.cell2A([anew,b,c,90,beta,90]) |
---|
459 | else: |
---|
460 | u = [-1,0,0] |
---|
461 | v = [1,0,1] |
---|
462 | cnew = math.sqrt(np.dot(np.dot(v,g),v)) |
---|
463 | if cnew < c: |
---|
464 | cang = np.dot(np.dot(u,g),v)/(a*cnew) |
---|
465 | beta = acosd(-abs(cang)) |
---|
466 | A = G2lat.cell2A([a,b,cnew,90,beta,90]) |
---|
467 | return A |
---|
468 | |
---|
469 | def DoIndexPeaks(peaks,inst,controls,bravais): |
---|
470 | |
---|
471 | delt = 0.005 #lowest d-spacing cushion - can be fixed? |
---|
472 | amin = 2.5 |
---|
473 | amax = 5.0*getDmax(peaks) |
---|
474 | dmin = getDmin(peaks)-delt |
---|
475 | bravaisNames = ['Cubic-F','Cubic-I','Cubic-P','Trigonal-R','Trigonal/Hexagonal-P', |
---|
476 | 'Tetragonal-I','Tetragonal-P','Orthorhombic-F','Orthorhombic-I','Orthorhombic-C', |
---|
477 | 'Orthorhombic-P','Monoclinic-C','Monoclinic-P','Triclinic'] |
---|
478 | tries = ['1st','2nd','3rd','4th','5th','6th','7th','8th','9th','10th'] |
---|
479 | N1s = [1,1,1, 5,5, 5,5, 50,50,50,50, 50,50, 200] |
---|
480 | N2s = [1,1,1, 2,2, 2,2, 2,2,2,2, 2,2, 4] |
---|
481 | Nm = [1,1,1, 1,1, 1,1, 1,1,1,1, 2,2, 4] |
---|
482 | Nobs = len(peaks) |
---|
483 | wave = inst[1] |
---|
484 | if len(inst) > 10: |
---|
485 | zero = inst[3] |
---|
486 | else: |
---|
487 | zero = inst[2] |
---|
488 | print "%s %8.5f %6.3f" % ('wavelength, zero =',wave,zero) |
---|
489 | print "%s %8.3f %8.3f" % ('lattice parameter range = ',amin,amax) |
---|
490 | ifzero,maxzero,ncno = controls[:3] |
---|
491 | ncMax = Nobs*ncno |
---|
492 | print "%s %d %s %d %s %d" % ('change zero =',ifzero,'Nc/No max =',ncno,' Max Nc =',ncno*Nobs) |
---|
493 | cells = [] |
---|
494 | for ibrav in range(14): |
---|
495 | begin = time.time() |
---|
496 | if bravais[ibrav]: |
---|
497 | print 'cell search for ',bravaisNames[ibrav] |
---|
498 | print ' M20 X20 Nc a b c alpha beta gamma volume V-test' |
---|
499 | V1 = controls[3] |
---|
500 | bestM20 = 0 |
---|
501 | topM20 = 0 |
---|
502 | cycle = 0 |
---|
503 | while cycle < 5: |
---|
504 | dlg = wx.ProgressDialog("Generated reflections",tries[cycle]+" cell search for "+bravaisNames[ibrav],ncMax, |
---|
505 | style = wx.PD_ELAPSED_TIME|wx.PD_AUTO_HIDE|wx.PD_REMAINING_TIME|wx.PD_CAN_ABORT) |
---|
506 | screenSize = wx.ClientDisplayRect() |
---|
507 | Size = dlg.GetSize() |
---|
508 | dlg.SetPosition(wx.Point(screenSize[2]-Size[0]-305,screenSize[1]+5)) |
---|
509 | try: |
---|
510 | GoOn = True |
---|
511 | while GoOn: #Loop over increment of volume |
---|
512 | N2 = 0 |
---|
513 | while N2 < N2s[ibrav]: #Table 2 step (iii) |
---|
514 | if ibrav > 2: |
---|
515 | if not N2: |
---|
516 | A = [] |
---|
517 | GoOn,Nc,M20,X20,A = findBestCell(dlg,ncMax,A,Nm[ibrav]*N1s[ibrav],ibrav,peaks,V1) |
---|
518 | if A: |
---|
519 | GoOn,Nc,M20,X20,A = findBestCell(dlg,ncMax,A[:],N1s[ibrav],ibrav,peaks,0) |
---|
520 | else: |
---|
521 | GoOn,Nc,M20,X20,A = findBestCell(dlg,ncMax,0,Nm[ibrav]*N1s[ibrav],ibrav,peaks,V1) |
---|
522 | if Nc >= ncMax: |
---|
523 | GoOn = False |
---|
524 | break |
---|
525 | elif 3*Nc < Nobs: |
---|
526 | N2 = 10 |
---|
527 | break |
---|
528 | else: |
---|
529 | if not GoOn: |
---|
530 | break |
---|
531 | if M20 > 1.0: |
---|
532 | bestM20 = max(bestM20,M20) |
---|
533 | A = halfCell(ibrav,A[:],peaks) |
---|
534 | if ibrav in [12]: |
---|
535 | A = monoCellReduce(ibrav,A[:]) |
---|
536 | HKL = G2lat.GenHBravais(dmin,ibrav,A) |
---|
537 | IndexPeaks(peaks,HKL) |
---|
538 | a,b,c,alp,bet,gam = G2lat.A2cell(A) |
---|
539 | V = G2lat.calc_V(A) |
---|
540 | print "%10.3f %3d %3d %10.5f %10.5f %10.5f %10.3f %10.3f %10.3f %10.2f %10.2f" % (M20,X20,Nc,a,b,c,alp,bet,gam,V,V1) |
---|
541 | if M20 >= 2.0: |
---|
542 | cells.append([M20,X20,ibrav,a,b,c,alp,bet,gam,V,False]) |
---|
543 | if not GoOn: |
---|
544 | break |
---|
545 | N2 += 1 |
---|
546 | if ibrav < 11: |
---|
547 | V1 *= 1.1 |
---|
548 | elif ibrav in range(11,14): |
---|
549 | V1 *= 1.05 |
---|
550 | if not GoOn: |
---|
551 | if bestM20 > topM20: |
---|
552 | topM20 = bestM20 |
---|
553 | if cells: |
---|
554 | V1 = cells[0][9] |
---|
555 | else: |
---|
556 | V1 = 25 |
---|
557 | ncMax += Nobs |
---|
558 | cycle += 1 |
---|
559 | print 'Restart search, new Max Nc = ',ncMax |
---|
560 | else: |
---|
561 | cycle = 10 |
---|
562 | finally: |
---|
563 | dlg.Destroy() |
---|
564 | print '%s%s%s%s'%('finished cell search for ',bravaisNames[ibrav], \ |
---|
565 | ', elapsed time = ',G2lat.sec2HMS(time.time()-begin)) |
---|
566 | |
---|
567 | if cells: |
---|
568 | cells = sortM20(cells) |
---|
569 | cells[0][-1] = True |
---|
570 | return True,dmin,cells |
---|
571 | else: |
---|
572 | return False,0,0 |
---|
573 | |
---|
574 | |
---|
575 | NeedTestData = True |
---|
576 | def TestData(): |
---|
577 | array = np.array |
---|
578 | global NeedTestData |
---|
579 | NeedTestData = False |
---|
580 | global TestData |
---|
581 | TestData = [12, [7.,8.70,10.86,90.,102.95,90.], [7.76006,8.706215,10.865679,90.,102.947,90.],3, |
---|
582 | [[2.176562137832974, 761.60902227696033, True, True, 0, 0, 1, 10.591300714328161, 10.589436], |
---|
583 | [3.0477561489789498, 4087.2956049071572, True, True, 1, 0, 0, 7.564238997554908, 7.562777], |
---|
584 | [3.3254921120068524, 1707.0253890991009, True, True, 1, 0, -1, 6.932650301411212, 6.932718], |
---|
585 | [3.428121546163426, 2777.5082170150563, True, True, 0, 1, 1, 6.725163158013632, 6.725106], |
---|
586 | [4.0379791325512118, 1598.4321673135987, True, True, 1, 1, 0, 5.709789097440156, 5.70946], |
---|
587 | [4.2511182350743937, 473.10955149057577, True, True, 1, 1, -1, 5.423637972781876, 5.42333], |
---|
588 | [4.354684330373451, 569.88528280256071, True, True, 0, 0, 2, 5.2947091882172534, 5.294718], |
---|
589 | [4.723324574319177, 342.73882372499997, True, True, 1, 0, -2, 4.881681587039431, 4.881592], |
---|
590 | [4.9014773581253994, 5886.3516356615492, True, True, 1, 1, 1, 4.704350709093183, 4.70413], |
---|
591 | [5.0970774474587275, 3459.7541692903033, True, True, 0, 1, 2, 4.523933797797693, 4.523829], |
---|
592 | [5.2971997607389518, 1290.0229964239879, True, True, 0, 2, 0, 4.353139557169142, 4.353108], |
---|
593 | [5.4161306205553847, 1472.5726977257755, True, True, 1, 1, -2, 4.257619398422479, 4.257944], |
---|
594 | [5.7277364698554161, 1577.8791668322888, True, True, 0, 2, 1, 4.026169751907777, 4.026193], |
---|
595 | [5.8500213058834163, 420.74210142657131, True, True, 1, 0, 2, 3.9420803081518443, 3.942219], |
---|
596 | [6.0986764166731708, 163.02160537058708, True, True, 2, 0, 0, 3.7814965150452537, 3.781389], |
---|
597 | [6.1126665157702753, 943.25461245706833, True, True, 1, 2, 0, 3.772849962062199, 3.772764], |
---|
598 | [6.2559260555056957, 250.55355015505376, True, True, 1, 2, -1, 3.6865353266375283, 3.686602], |
---|
599 | [6.4226243128279892, 5388.5560141098349, True, True, 1, 1, 2, 3.5909481979190283, 3.591214], |
---|
600 | [6.5346132446561134, 1951.6070344509026, True, True, 0, 0, 3, 3.5294722429440584, 3.529812], |
---|
601 | [6.5586952135236443, 259.65938178131034, True, True, 2, 1, -1, 3.516526936765838, 3.516784], |
---|
602 | [6.6509216222783722, 93.265376597376573, True, True, 2, 1, 0, 3.4678179073694952, 3.468369], |
---|
603 | [6.7152737044107722, 289.39386813803162, True, True, 1, 2, 1, 3.4346235125812807, 3.434648], |
---|
604 | [6.8594130457361899, 603.54959764648322, True, True, 0, 2, 2, 3.362534044860622, 3.362553], |
---|
605 | [7.0511627728884454, 126.43246447656593, True, True, 0, 1, 3, 3.2712038721790675, 3.271181], |
---|
606 | [7.077700845503319, 125.49742760019636, True, True, 1, 1, -3, 3.2589538988480626, 3.259037], |
---|
607 | [7.099393757363675, 416.55444885434633, True, True, 1, 2, -2, 3.2490085228959193, 3.248951], |
---|
608 | [7.1623933278642742, 369.27397110921817, True, True, 2, 1, -2, 3.2204673608202383, 3.220487], |
---|
609 | [7.4121734953058924, 482.84120827021826, True, True, 2, 1, 1, 3.1120858221599876, 3.112308]] |
---|
610 | ] |
---|
611 | global TestData2 |
---|
612 | TestData2 = [12, [0.15336547830008007, 0.017345499139401827, 0.008122368657493792, 0, 0.02893538955687591, 0], 3, |
---|
613 | [[2.176562137832974, 761.6090222769603, True, True, 0, 0, 1, 10.591300714328161, 11.095801], |
---|
614 | [3.0477561489789498, 4087.295604907157, True, True, 0, 1, 0, 7.564238997554908, 7.592881], |
---|
615 | [3.3254921120068524, 1707.025389099101, True, False, 0, 0, 0, 6.932650301411212, 0.0], |
---|
616 | [3.428121546163426, 2777.5082170150563, True, True, 0, 1, 1, 6.725163158013632, 6.266192], |
---|
617 | [4.037979132551212, 1598.4321673135987, True, False, 0, 0, 0, 5.709789097440156, 0.0], |
---|
618 | [4.251118235074394, 473.10955149057577, True, True, 0, 0, 2, 5.423637972781876, 5.5479], |
---|
619 | [4.354684330373451, 569.8852828025607, True, True, 0, 0, 2, 5.2947091882172534, 5.199754], |
---|
620 | [4.723324574319177, 342.738823725, True, False, 0, 0, 0, 4.881681587039431, 0.0], |
---|
621 | [4.901477358125399, 5886.351635661549, True, False, 0, 0, 0, 4.704350709093183, 0.0], |
---|
622 | [5.0970774474587275, 3459.7541692903033, True, True, 0, 1, 2, 4.523933797797693, 4.479534], |
---|
623 | [5.297199760738952, 1290.022996423988, True, True, 0, 1, 0, 4.353139557169142, 4.345087], |
---|
624 | [5.416130620555385, 1472.5726977257755, True, False, 0, 0, 0, 4.257619398422479, 0.0], |
---|
625 | [5.727736469855416, 1577.8791668322888, True, False, 0, 0, 0, 4.026169751907777, 0.0], |
---|
626 | [5.850021305883416, 420.7421014265713, True, False, 0, 0, 0, 3.9420803081518443, 0.0], |
---|
627 | [6.098676416673171, 163.02160537058708, True, True, 0, 2, 0, 3.7814965150452537, 3.796441], |
---|
628 | [6.112666515770275, 943.2546124570683, True, False, 0, 0, 0, 3.772849962062199, 0.0], |
---|
629 | [6.255926055505696, 250.55355015505376, True, True, 0, 0, 3, 3.6865353266375283, 3.6986], |
---|
630 | [6.422624312827989, 5388.556014109835, True, True, 0, 2, 1, 3.5909481979190283, 3.592005], |
---|
631 | [6.534613244656113, 191.6070344509026, True, True, 1, 0, -1, 3.5294722429440584, 3.546166], |
---|
632 | [6.558695213523644, 259.65938178131034, True, True, 0, 0, 3, 3.516526936765838, 3.495428], |
---|
633 | [6.650921622278372, 93.26537659737657, True, True, 0, 0, 3, 3.4678179073694952, 3.466503], |
---|
634 | [6.715273704410772, 289.3938681380316, True, False, 0, 0, 0, 3.4346235125812807, 0.0], |
---|
635 | [6.85941304573619, 603.5495976464832, True, True, 0, 1, 3, 3.362534044860622, 3.32509], |
---|
636 | [7.051162772888445, 126.43246447656593, True, True, 0, 1, 2, 3.2712038721790675, 3.352121], |
---|
637 | [7.077700845503319, 125.49742760019636, True, False, 0, 0, 0, 3.2589538988480626, 0.0], |
---|
638 | [7.099393757363675, 416.55444885434633, True, False, 0, 0, 0, 3.2490085228959193, 0.0], |
---|
639 | [7.162393327864274, 369.27397110921817, True, False, 0, 0, 0, 3.2204673608202383, 0.0], |
---|
640 | [7.412173495305892, 482.84120827021826, True, True, 0, 2, 2, 3.112085822159976, 3.133096]] |
---|
641 | ] |
---|
642 | # |
---|
643 | def test0(): |
---|
644 | if NeedTestData: TestData() |
---|
645 | msg = 'test FitHKL' |
---|
646 | ibrav,cell,bestcell,Pwr,peaks = TestData |
---|
647 | print 'best cell:',bestcell |
---|
648 | print 'old cell:',cell |
---|
649 | Peaks = np.array(peaks) |
---|
650 | HKL = Peaks[4:7] |
---|
651 | print calc_M20(peaks,HKL) |
---|
652 | A = G2lat.cell2A(cell) |
---|
653 | OK,smin,A,result = FitHKL(ibrav,peaks,A,Pwr) |
---|
654 | print 'new cell:',G2lat.A2cell(A) |
---|
655 | print 'x:',result[0] |
---|
656 | print 'cov_x:',result[1] |
---|
657 | print 'infodict:' |
---|
658 | for item in result[2]: |
---|
659 | print item,result[2][item] |
---|
660 | print 'msg:',result[3] |
---|
661 | print 'ier:',result[4] |
---|
662 | result = refinePeaks(peaks,ibrav,A) |
---|
663 | N,M20,X20,A = result |
---|
664 | print 'refinePeaks:',N,M20,X20,G2lat.A2cell(A) |
---|
665 | print 'compare bestcell:',bestcell |
---|
666 | # |
---|
667 | def test1(): |
---|
668 | if NeedTestData: TestData() |
---|
669 | msg = 'test FitHKL' |
---|
670 | ibrav,A,Pwr,peaks = TestData2 |
---|
671 | print 'bad cell:',G2lat.A2cell(A) |
---|
672 | print 'FitHKL' |
---|
673 | OK,smin,A,result = FitHKL(ibrav,peaks,A,Pwr) |
---|
674 | result = refinePeaks(peaks,ibrav,A) |
---|
675 | N,M20,X20,A = result |
---|
676 | print 'refinePeaks:',N,M20,X20,A |
---|
677 | # Peaks = np.array(peaks) |
---|
678 | # HKL = Peaks[4:7] |
---|
679 | # print calc_M20(peaks,HKL) |
---|
680 | # OK,smin,A,result = FitHKL(ibrav,peaks,A,Pwr) |
---|
681 | # print 'new cell:',G2lat.A2cell(A) |
---|
682 | # print 'x:',result[0] |
---|
683 | # print 'cov_x:',result[1] |
---|
684 | # print 'infodict:' |
---|
685 | # for item in result[2]: |
---|
686 | # print item,result[2][item] |
---|
687 | # print 'msg:',result[3] |
---|
688 | # print 'ier:',result[4] |
---|
689 | |
---|
690 | # |
---|
691 | if __name__ == '__main__': |
---|
692 | test0() |
---|
693 | test1() |
---|
694 | # test2() |
---|
695 | # test3() |
---|
696 | # test4() |
---|
697 | # test5() |
---|
698 | # test6() |
---|
699 | # test7() |
---|
700 | # test8() |
---|
701 | print "OK" |
---|