def objFuncSX(pFit, pFull, pFlag, dFunc, dFlag, xyo_det, hkls_idx, bMat, vInv, wavelength, bVec, eVec, omePeriod, simOnly=False): npts = len(xyo_det) refineFlag = np.hstack([pFlag, dFlag]) # pFull[refineFlag] = pFit/scl[refineFlag] pFull[refineFlag] = pFit dParams = pFull[-len(dFlag):] xy_unwarped = dFunc(xyo_det[:, :2], dParams) # detector quantities rMat_d = xf.makeDetectorRotMat(pFull[:3]) tVec_d = pFull[3:6].reshape(3, 1) # sample quantities chi = pFull[6] tVec_s = pFull[7:10].reshape(3, 1) # crystal quantities rMat_c = xf.makeRotMatOfExpMap(pFull[10:13]) tVec_c = pFull[13:16].reshape(3, 1) gVec_c = np.dot(bMat, hkls_idx) vMat_s = mutil.vecMVToSymm(vInv) # stretch tensor comp matrix from MV notation in SAMPLE frame gVec_s = np.dot(vMat_s, np.dot(rMat_c, gVec_c)) # reciprocal lattice vectors in SAMPLE frame gHat_s = mutil.unitVector(gVec_s) # unit reciprocal lattice vectors in SAMPLE frame gHat_c = np.dot(rMat_c.T, gHat_s) # unit reciprocal lattice vectors in CRYSTAL frame match_omes, calc_omes = matchOmegas(xyo_det, hkls_idx, chi, rMat_c, bMat, wavelength, vInv=vInv, beamVec=bVec, etaVec=eVec, omePeriod=omePeriod) xy_det = np.zeros((npts, 2)) for i in range(npts): rMat_s = xf.makeOscillRotMat([chi, calc_omes[i]]) # rMat_s = xf.makeOscillRotMat([chi, meas_omes[i]]) xy_det[i, :] = xf.gvecToDetectorXY(gHat_c[:, i].reshape(3, 1), rMat_d, rMat_s, rMat_c, tVec_d, tVec_s, tVec_c, beamVec=bVec).flatten() pass if np.any(np.isnan(xy_det)): print "infeasible pFull: may want to scale back finite difference step size" # return values # retval = np.sum((xy_det - xy_unwarped[:, :2])**2) if simOnly: retval = np.hstack([xy_det, calc_omes.reshape(npts, 1)]) else: # retval = np.sum( (xy_det - xy_unwarped[:, :2])**2, axis=1) retval = np.sum( (np.hstack([xy_det, calc_omes.reshape(npts, 1)]) - np.hstack([xy_unwarped[:, :2], xyo_det[:, 2].reshape(npts, 1)]) )**2, axis=1) return retval
[ -3.235616], [-1050.74026 ] ] ) chi = -0.0011591608938627839 tVec_s = np.array([ [-0.15354144], [ 0. ], [-0.23294777] ] ) rMat_c = xf.makeRotMatOfExpMap(np.array( [ [ 0.66931818], [-0.98578066], [ 0.73593251] ] ) ) tVec_c = np.array( [ [ 0.07547626], [ 0.08827523], [-0.02131205] ] ) rMat_s = xf.makeOscillRotMat([chi, 0.]) # ###################################################################### # Calculate pixel coordinates # pvec = 204.8 * np.linspace(-1, 1, 512) #pvec = 204.8 * np.linspace(-1, 1, 2048) #pvec = 204.8 * np.linspace(-1, 1, 4096) #pvec = 204.8 * np.linspace(-1, 1, 8192) #pvec = 204.8 * np.linspace(-1, 1, 16384) dcrds = np.meshgrid(pvec, pvec) XY = np.ascontiguousarray(np.vstack([dcrds[0].flatten(), dcrds[1].flatten()]).T) # Check the timings
from hexrd.xrd import transforms_CAPI as xfcapi epsf = 2.2e-16 vec = np.array([[random.uniform(-np.pi,np.pi),random.uniform(-np.pi,np.pi),random.uniform(-np.pi,np.pi)]]) vHat1 = xf.unitVector(vec.T) vHat2 = xfcapi.unitRowVector(vec) print "unitVector results match: ",np.linalg.norm(vHat1.T-vHat2)/np.linalg.norm(vHat1) < epsf tAng = np.array([0.0011546340766314521,-0.0040527538387122993,-0.0026221336905160211]) rMat1 = xf.makeDetectorRotMat(tAng) rMat2 = xfcapi.makeDetectorRotMat(tAng) print "makeDetectorRotMat results match: ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf oAng = np.array([-0.0011591608938627839,0.0011546340766314521]) rMat1 = xf.makeOscillRotMat(oAng) rMat2 = xfcapi.makeOscillRotMat(oAng) print "makeOscillRotMat results match: ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf eMap = np.array([ 0.66931818,-0.98578066,0.73593251]) rMat1 = xf.makeRotMatOfExpMap(eMap) rMat2 = xfcapi.makeRotMatOfExpMap(eMap) print "makeRotMatOfExpMap results match: ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf axis = np.array([ 0.66931818,-0.98578066,0.73593251]) rMat1 = xf.makeBinaryRotMat(axis) rMat2 = xfcapi.makeBinaryRotMat(axis) print "makeBinaryRotMat results match: ",np.linalg.norm(rMat1-rMat2)/np.linalg.norm(rMat1) < epsf bHat = np.array([0.0,0.0,-1.0]) eta = np.array([1.0,0.0,0.0])
# input parameters bVec_ref = xf.bVec_ref tilt_angles = (0.0011546340766314521, -0.0040527538387122993, -0.0026221336905160211) rMat_d = xf.makeDetectorRotMat(tilt_angles) tVec_d = np.array([[-1.44904], [-3.235616], [-1050.74026]]) chi = -0.0011591608938627839 tVec_s = np.array([[-0.15354144], [0.], [-0.23294777]]) rMat_c = xf.makeRotMatOfExpMap( np.array([[0.66931818], [-0.98578066], [0.73593251]])) tVec_c = np.array([[0.07547626], [0.08827523], [-0.02131205]]) rMat_s = xf.makeOscillRotMat([chi, 0.]) # ###################################################################### # Calculate pixel coordinates # pvec = 204.8 * np.linspace(-1, 1, 2048) dcrds = np.meshgrid(pvec, pvec) XY = np.ascontiguousarray( np.vstack([dcrds[0].flatten(), dcrds[1].flatten()]).T) # Check the timings start1 = time.clock() # time this dangs1 = xf.detectorXYToGvec(XY, rMat_d, rMat_s, tVec_d,
random.uniform(-np.pi, np.pi) ]]) vHat1 = xf.unitVector(vec.T) vHat2 = xfcapi.unitRowVector(vec) print "unitVector results match: ", np.linalg.norm( vHat1.T - vHat2) / np.linalg.norm(vHat1) < epsf tAng = np.array( [0.0011546340766314521, -0.0040527538387122993, -0.0026221336905160211]) rMat1 = xf.makeDetectorRotMat(tAng) rMat2 = xfcapi.makeDetectorRotMat(tAng) print "makeDetectorRotMat results match: ", np.linalg.norm( rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf oAng = np.array([-0.0011591608938627839, 0.0011546340766314521]) rMat1 = xf.makeOscillRotMat(oAng) rMat2 = xfcapi.makeOscillRotMat(oAng) print "makeOscillRotMat results match: ", np.linalg.norm( rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf eMap = np.array([0.66931818, -0.98578066, 0.73593251]) rMat1 = xf.makeRotMatOfExpMap(eMap) rMat2 = xfcapi.makeRotMatOfExpMap(eMap) print "makeRotMatOfExpMap results match: ", np.linalg.norm( rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf axis = np.array([0.66931818, -0.98578066, 0.73593251]) rMat1 = xf.makeBinaryRotMat(axis) rMat2 = xfcapi.makeBinaryRotMat(axis) print "makeBinaryRotMat results match: ", np.linalg.norm( rMat1 - rMat2) / np.linalg.norm(rMat1) < epsf