def reset(self): self.post = gv_general.chiwedge( wedge = self.pars.get('wedge'), chi = self.pars.get('chi')).ravel() self.posti= np.dot(gv_general.wedgemat(self.pars.get('wedge')), gv_general.chimat(self.pars.get('chi'))).T.ravel() self.axis = np.array([0,0,-1],np.float)
def grain_x0(omega, wedge, chi, tx, ty, tz): """ Grain origin positions at Omega rotation """ co = np.cos(np.radians(omega)) so = np.sin(np.radians(omega)) x0 = np.zeros((3, omega.shape[0])) x0[0] = co * tx - so * ty x0[1] = so * tx + co * ty x0[2] = tz cw = gv_general.chiwedge(chi, wedge) x0 = np.dot(cw.T, x0) return x0
def g_to_k(g, omega, wedge, chi): """ Rotate g vector to k vector """ co = np.cos(np.radians(omega)) so = np.sin(np.radians(omega)) k = np.zeros((3, len(omega))) k[0, :] = co * g[0, :] - so * g[1, :] k[1, :] = so * g[0, :] + co * g[1, :] k[2, :] = g[2, :] cw = gv_general.chiwedge(chi, wedge) k = np.dot(cw, k) return k
def k_to_g(k, omega, wedge, chi): """ Rotate k vector to g vector """ #print "k_to_g",k,omega,wedge,chi om = np.radians(omega) g = np.zeros(k.shape) cw = gv_general.chiwedge(chi, wedge) cwk = np.dot(cw, k) co = np.cos(np.radians(omega)) # 1D so = np.sin(np.radians(omega)) # 1D g[0, :] = co * cwk[0, :] + so * cwk[1, :] g[1, :] = -so * cwk[0, :] + co * cwk[1, :] g[2, :] = cwk[2, :] return g
def compute_XLYLZL(self): self.peaks_xyz = transform.compute_xyz_lab( [self.colfile.sc, self.colfile.fc], **self.colfile.parameters.parameters) # These are the peak positions in the laboratory frame # # Now get the beam and peaks in the crystal frame wedge = float(self.colfile.parameters.get('wedge')) chi = float(self.colfile.parameters.get('chi')) self.chiwedge = gv_general.chiwedge(chi, wedge) omega = np.radians(self.colfile.omega) cosomega = np.cos(omega) sinomega = np.sin(omega) beam = np.zeros((3, len(omega)), float) beam[0, :] = 1. self.XB = k_to_g(beam, cosomega, sinomega, self.chiwedge) self.XS = k_to_g(self.peaks_xyz, cosomega, sinomega, self.chiwedge)
def testhklcalc_many(wedge, chi): # hklcalc_many # apply wedge, chi to axis if necessary axis = np.array( [0,0,-1], np.float ) UBI = np.eye(3).ravel() hkl = np.zeros( (len(omega), 3), np.float) XL = np.ones( (len(omega), 3), np.float)*1e3 T = np.array([100,200,300],np.float) wvln = 0.254 pre = np.eye(3).ravel() post = gv_general.chiwedge( wedge=wedge, chi=chi ).ravel() kc = XL.copy() wripaca.hklcalc_many( XL, axis, omega*np.pi/180, UBI, T, pre, post, hkl, wvln, kc) print "axis is",axis t, e = transform.compute_tth_eta_from_xyz( XL.T, t_x = T[0], t_y = T[1], t_z = T[2], omega = omega, wedge = wedge, chi = chi ) #print t,e,omega ori = transform.compute_grain_origins( omega, wedge=wedge, chi=chi, t_x = T[0], t_y = T[1], t_z = T[2] ) print "last origin",ori.T[-1],ori.T[0] print "last xl",XL[-1],XL[0] kve = transform.compute_k_vectors( t, e, wvln ) print "last k",kve.T[-1],kve.T[0] gve = transform.compute_g_vectors( t, e, omega, wvln, wedge=wedge, chi=chi ) print "last g",gve.T[-1],gve.T[0] htest = np.dot( UBI.reshape(3,3), gve ) for i in range(len(t)): # print gve[:,i] # print hkl[i] if ((gve[:,i]-hkl[i])**2).sum() > 1e-10: print "FAIL", print i,gve[:,i],hkl[i],((gve[:,i]-hkl[i])**2).sum() print wedge,chi sys.exit() print "OK for wedge",wedge,"chi",chi
def test_25_30(self): """ wedge, chi = 25,30 """ w, c = 25, 30 SANITY = False if SANITY: print "*" * 80 om = np.zeros(self.np, np.float) g_old = transform.compute_g_vectors(self.tth, self.eta, self.omega, self.wvln, wedge=w, chi=c) if SANITY: print g_old.shape, "\n", g_old[:, :3] k = transform.compute_k_vectors(self.tth, self.eta, self.wvln) if SANITY: print "k=", k[:, :3] post = gv_general.chiwedge(chi=c, wedge=w) g_new = gv_general.k_to_g(k, self.omega, axis=[0, 0, -1], post=post) if SANITY: print g_new.shape, g_new[:, :3] if SANITY: print "end routine" if SANITY: print "*" * 80 self.assertAlmostEqual(array_diff(g_new, g_old), 0, 6)
def uncompute_g_vectors(g, wavelength, wedge=0.0, chi=0.0): """ Given g-vectors compute tth,eta,omega assert uncompute_g_vectors(compute_g_vector(tth,eta,omega))==tth,eta,omega """ if wedge == chi == 0: post = None else: post = gv_general.wedgechi(wedge=wedge, chi=chi) omega1, omega2, valid = gv_general.g_to_k(g, wavelength, axis=[0, 0, -1], pre=None, post=post) # we know g, omega. Compute k as ... ? if post is None: pre = None else: pre = gv_general.chiwedge(wedge=wedge, chi=chi).T k_one = gv_general.k_to_g(g, omega1, axis=[0, 0, 1], pre=pre, post=None) k_two = gv_general.k_to_g(g, omega2, axis=[0, 0, 1], pre=pre, post=None) # # k[1,:] = -ds*c*sin(eta) # ------ ------------- .... tan(eta) = -k1/k2 # k[2,:] = ds*c*cos(eta) # eta_one = np.arctan2(-k_one[1, :], k_one[2, :]) eta_two = np.arctan2(-k_two[1, :], k_two[2, :]) # # ds = np.sqrt(np.sum(g * g, 0)) s = ds * wavelength / 2.0 # sin theta tth = np.degrees(np.arcsin(s) * 2.) * valid eta1 = np.degrees(eta_one) * valid eta2 = np.degrees(eta_two) * valid omega1 = omega1 * valid omega2 = omega2 * valid return tth, [eta1, eta2], [omega1, omega2]
def Dcalc(UB, hkls, wedge, chi, wavelength, etao, omega): """ Finds "C"alculated 2thetac, etac, omegac for a grain (Note that these do not depend on the grain position) UB = 3x3 UB matrix hkls = 3xn list of peaks (normally integers) wedge = tilt around y under rotation axis chi = tilt around beam under rotation axis wavelength = of the radiation, scales UB etao = observed azimuth values (angle on powder ring) omega = observed(?) sample rotation angles ??? ysign determines which solution (there are usually two) """ # computed g-vectors g = np.dot(UB, hkls) assert g.dtype == float if 0: print(g.shape) print(hkls.shape) for i in range(10): print(g[:, i], end=' ') print(hkls[:, i], end=' ') print(np.dot(np.linalg.inv(UB), g[:, i])) # wedge/chi matrix for axis orientation wc = gv_general.wedgechi(wedge=wedge, chi=chi) assert wc.dtype == float # computed omega angles for reflections omega1, omega2, valid = gv_general.g_to_k(g, wavelength, axis=[0, 0, -1], pre=None, post=wc) assert omega1.dtype == float # hum - isn't this wc-1 ? cwT = gv_general.chiwedge(wedge=wedge, chi=chi).T # k vectors, scattering in laboratory at angle omega1/2 k1 = gv_general.k_to_g(g, omega1, axis=[0, 0, 1], pre=cwT, post=None) k2 = gv_general.k_to_g(g, omega2, axis=[0, 0, 1], pre=cwT, post=None) assert k1.dtype == float # Computed azimuth angles # # k[1,:] = -ds*c*sin(eta) # ------ ------------- .... tan(eta) = -k1/k2 # k[2,:] = ds*c*cos(eta) # eta_one = np.degrees(np.arctan2(-k1[1, :], k1[2, :])) eta_two = np.degrees(np.arctan2(-k2[1, :], k2[2, :])) # # We select the one matching the observed data # FIXME - doesn't make sense to do this here # pick the solution which is closest to matching in eta/omega d1 = angmod(eta_one - etao) d2 = angmod(eta_two - etao) # Selecting which is the computed value for peaks etac = np.where(abs(d1) < abs(d2), eta_one, eta_two) omegac = np.where(abs(d1) < abs(d2), omega1, omega2) if TESTING > 2: err = abs(etac - etao) for i in range(len(err)): if err[i] < 1: continue print(i, eta_one[i], eta_two[i], omega1[i], omega2[i]) print("\t", etao[i], omega[i], etac[i], omegac[i]) # Two theta angles mod_g = np.sqrt((g * g).sum(axis=0)) tthc = np.degrees(2 * np.arcsin(wavelength * mod_g / 2)) # Return computed values (no derivatives here) return tthc, etac, omegac
testaxisang() def anglediff(a1, a2): return np.arctan2(np.sin(a1-a2), np.cos(a1-a2)) for wedge, chi in [(0.,0.),(-10.,0.), (11,-15), (0,6)]: print "Wedge",wedge,"chi",chi print "gve[0] gve[1] gve[2] om1py om2py om1af om2af omtrue" gve = transform.compute_g_vectors( tth, eta, omega, wvln, wedge, chi ) gve = gve.T.copy() post = gv_general.chiwedge( wedge=wedge, chi=chi ) posti = np.dot(gv_general.wedgemat(wedge), gv_general.chimat(chi)).T # don't try to match this as it is wrong!! # tth, eta, omega = transform.uncompute_g_vectors( hkl.T, wvln, # wedge=wedge, # chi = chi) ppre = np.reshape(pre,(3,3)) ppost = np.reshape(post,(3,3)) omg1, omg2, valid = gv_general.g_to_k( gve.T, wvln, axis=axis, pre=ppre, post=ppost ) #print pre #print posti.ravel() #print axis