Beispiel #1
0
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
print "Time for CAPI detectorXYToGvec: %f"%(elapsed2)

# maxDiff_tTh = np.linalg.norm(tTh_d1-tTh_d2,np.inf)
# print "Maximum disagreement in tTh:  %f"%maxDiff_tTh
# maxDiff_eta = np.linalg.norm(eta_d1-eta_d2,np.inf)
## print "Maximum disagreement in eta:  %f"%maxDiff_eta
# maxDiff_gVec = np.linalg.norm(np.sqrt(np.sum(np.asarray(gVec_l1.T-gVec_l2)**2,1)),np.inf)
# print "Maximum disagreement in gVec: %f"%maxDiff_gVec



gVec_c1 = np.dot(rMat_c.T,np.dot(rMat_s.T,gVec_l1))
gVec_c2 = np.ascontiguousarray(np.dot(rMat_c.T,np.dot(rMat_s.T,gVec_l2.T)).T)

start3 = timer()                      # time this
xy1 = xf.gvecToDetectorXY(gVec_c1,rMat_d,rMat_s,rMat_c,tVec_d,tVec_s,tVec_c,beamVec=bVec_ref)
elapsed3 = (timer() - start3)
print "Time for Python gvecToDetectorXY: %f"%(elapsed3)

start4 = timer()                      # time this
xy2 = xfcapi.gvecToDetectorXY(gVec_c2,rMat_d,rMat_s,rMat_c,tVec_d,tVec_s,tVec_c,beamVec=bVec_ref)
elapsed4 = (timer() - start4)
print "Time for CAPI gvecToDetectorXY: %f"%(elapsed4)


print 'cudadevice: ', numba.cuda.get_current_device().name


# setup or numba version
# should be able to run in nopython mode
bHat_l = np.zeros(3)
Beispiel #3
0
def objFuncFitGrain(gFit, gFull, gFlag, 
                    detectorParams, 
                    xyo_det, hkls_idx, bMat, wavelength, 
                    bVec, eVec, 
                    dFunc, dParams, 
                    omePeriod, 
                    simOnly=False):
    """
    gFull[0]  = expMap_c[0]
    gFull[1]  = expMap_c[1]
    gFull[2]  = expMap_c[2]
    gFull[3]  = tVec_c[0]
    gFull[4]  = tVec_c[1]
    gFull[5]  = tVec_c[2]
    gFull[6]  = vInv_MV[0]
    gFull[7]  = vInv_MV[1]
    gFull[8]  = vInv_MV[2]
    gFull[9]  = vInv_MV[3]
    gFull[10] = vInv_MV[4]
    gFull[11] = vInv_MV[5]
    
    detectorParams[0]  = tiltAngles[0]
    detectorParams[1]  = tiltAngles[1]
    detectorParams[2]  = tiltAngles[2]
    detectorParams[3]  = tVec_d[0]
    detectorParams[4]  = tVec_d[1]
    detectorParams[5]  = tVec_d[2]
    detectorParams[6]  = chi
    detectorParams[7]  = tVec_s[0]
    detectorParams[8]  = tVec_s[1]
    detectorParams[9]  = tVec_s[2]
    """
    npts   = len(xyo_det)

    gFull[gFlag] = gFit
    
    xy_unwarped = dFunc(xyo_det[:, :2], dParams)
    
    rMat_d = xfcapi.makeDetectorRotMat(detectorParams[:3])
    tVec_d = detectorParams[3:6].reshape(3, 1)
    chi    = detectorParams[6]
    tVec_s = detectorParams[7:10].reshape(3, 1)
    
    rMat_c = xfcapi.makeRotMatOfExpMap(gFull[:3])
    tVec_c = gFull[3:6].reshape(3, 1)
    vInv_s = gFull[6:]
    vMat_s = mutil.vecMVToSymm(vInv_s)              # NOTE: Inverse of V from F = V * R

    gVec_c = np.dot(bMat, hkls_idx)                 # gVecs with magnitudes in CRYSTAL frame
    gVec_s = np.dot(vMat_s, np.dot(rMat_c, gVec_c)) # stretched gVecs in SAMPLE frame
    gHat_c = mutil.unitVector(
        np.dot(rMat_c.T, gVec_s)) # unit reciprocal lattice vectors in CRYSTAL frame
    
    match_omes, calc_omes = matchOmegas(xyo_det, hkls_idx, chi, rMat_c, bMat, wavelength, 
                                        vInv=vInv_s, beamVec=bVec, etaVec=eVec, 
                                        omePeriod=omePeriod)
    
    xy_det = np.zeros((npts, 2))
    for i in range(npts):
        rMat_s = xfcapi.makeOscillRotMat(np.r_[chi, calc_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"
    
    # 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
eta_d2   = dangs2[0][1]
gVec_l2  = dangs2[1]
elapsed2 = (time.clock() - start2)
print "Time for CAPI detectorXYToGvec: %f"%(elapsed2)

maxDiff_tTh = np.linalg.norm(tTh_d1-tTh_d2,np.inf)
print "Maximum disagreement in tTh:  %f"%maxDiff_tTh
maxDiff_eta = np.linalg.norm(eta_d1-eta_d2,np.inf)
print "Maximum disagreement in eta:  %f"%maxDiff_eta
maxDiff_gVec = np.linalg.norm(np.sqrt(np.sum(np.asarray(gVec_l1.T-gVec_l2)**2,1)),np.inf)
print "Maximum disagreement in gVec: %f"%maxDiff_gVec

gVec_c1 = np.dot(rMat_c.T,np.dot(rMat_s.T,gVec_l1))
gVec_c2 = np.ascontiguousarray(np.dot(rMat_c.T,np.dot(rMat_s.T,gVec_l2.T)).T)

start3 = time.clock()                      # time this
xy1 = xf.gvecToDetectorXY(gVec_c1,rMat_d,rMat_s,rMat_c,tVec_d,tVec_s,tVec_c,beamVec=bVec_ref)
elapsed3 = (time.clock() - start3)
print "Time for Python gvecToDetectorXY: %f"%(elapsed3)

maxDiff_xy = np.linalg.norm(np.sqrt(np.sum(np.asarray(XY-xy1)**2,1)),np.inf)
print "Maximum disagreement in gVec: %f"%maxDiff_xy

start4 = time.clock()                      # time this
xy2 = xfcapi.gvecToDetectorXY(gVec_c2,rMat_d,rMat_s,rMat_c,tVec_d,tVec_s,tVec_c,beamVec=bVec_ref)
elapsed4 = (time.clock() - start4)
print "Time for CAPI gvecToDetectorXY: %f"%(elapsed4)

maxDiff_xy = np.linalg.norm(np.sqrt(np.sum(np.asarray(XY-xy2)**2,1)),np.inf)
print "Maximum disagreement in gVec: %f"%maxDiff_xy