Beispiel #1
0
def objFunc(x, grainList, scl):
    """
    """
    x = x / scl  # remove scaling
    xc = x[0]  # beam x-center
    zTilt = x[1]  # zTilt --> inclination of oscill. axis on detector
    chiTilt = x[2]  # zTilt --> inclination of oscill. axis on detector

    for i in range(len(grainList)):
        grainList[i].detectorGeom.xc = xc
        grainList[i].detectorGeom.zTilt = zTilt
        grainList[i].detectorGeom.chiTilt = chiTilt
    # need a fresh detector object to hand to spots
    # use first grain by default (any will do)
    tmpDG = grainList[0].detectorGeom.makeNew()
    tmpDG.pVec = None

    # reset the detector used by all spots
    # each grain currently carried th
    # ...PRIME CANDIDATE FOR OPTIMIZATION/CLEANUP/GENERALIZATION...
    # ...perhaps loop over only the spots used by the grains in grainList?...
    # grainList[0].spots.resetDetectorGeom(tmpDG)

    # strip out quantities to hand off to the fit objective fuction to get residual contribution
    resd = []
    for i in range(len(grainList)):
        spotIDs = grainList[i].grainSpots['iRefl']
        spotIDs = spotIDs[spotIDs >= 0]
        for j in range(len(spotIDs)):
            spot = grainList[i].spots._Spots__spots[spotIDs[j]]
            spot.setDetectorGeom(tmpDG, clobber=True)
            angCOM = spot.angCOM()
            grainList[i].spots._Spots__spotAngCoords[spotIDs[j]] = angCOM
            grainList[i].spots._Spots__spotXYOCoords[
                spotIDs[j]] = grainList[i].spots.detectorGeom.angToXYO(*angCOM)
            pass
        # refit grains to new detector -- mainly for fixing pVecs
        grainList[i].updateGVecs()
        grainList[i].fit(display=False)

        angAxs = ROT.angleAxisOfRotMat(grainList[i].rMat)
        biotT = matrixutil.symmToVecMV(grainList[i].vMat - numpy.eye(3))
        pVec = grainList[i].detectorGeom.pVec

        x = numpy.vstack(
            [angAxs[0] * angAxs[1],
             biotT.reshape(6, 1),
             pVec.reshape(3, 1)])
        resd.append(grainList[i]._fitF_objFunc(x))
        pass
    resd = numpy.hstack(resd).flatten()
    return sum(resd**2)
Beispiel #2
0
def objFunc(x, grainList, scl):
    """
    """
    x = x / scl                         # remove scaling
    xc      = x[0]                        # beam x-center
    zTilt   = x[1]                        # zTilt --> inclination of oscill. axis on detector
    chiTilt = x[2]                        # zTilt --> inclination of oscill. axis on detector

    for i in range(len(grainList)):
        grainList[i].detectorGeom.xc      = xc
        grainList[i].detectorGeom.zTilt   = zTilt
        grainList[i].detectorGeom.chiTilt = chiTilt
    # need a fresh detector object to hand to spots
    # use first grain by default (any will do)
    tmpDG      = grainList[0].detectorGeom.makeNew()
    tmpDG.pVec = None

    # reset the detector used by all spots
    # each grain currently carried th
    # ...PRIME CANDIDATE FOR OPTIMIZATION/CLEANUP/GENERALIZATION...
    # ...perhaps loop over only the spots used by the grains in grainList?...
    # grainList[0].spots.resetDetectorGeom(tmpDG)

    # strip out quantities to hand off to the fit objective fuction to get residual contribution
    resd = []
    for i in range(len(grainList)):
        spotIDs = grainList[i].grainSpots['iRefl']
        spotIDs = spotIDs[spotIDs >= 0]
        for j in range(len(spotIDs)):
            spot = grainList[i].spots._Spots__spots[spotIDs[j]]
            spot.setDetectorGeom(tmpDG, clobber=True)
            angCOM = spot.angCOM()
            grainList[i].spots._Spots__spotAngCoords[spotIDs[j]] = angCOM
            grainList[i].spots._Spots__spotXYOCoords[spotIDs[j]] = grainList[i].spots.detectorGeom.angToXYO( *angCOM )
            pass
        # refit grains to new detector -- mainly for fixing pVecs
        grainList[i].updateGVecs()
        grainList[i].fit(display=False)

        angAxs = ROT.angleAxisOfRotMat(grainList[i].rMat)
        biotT  = matrixutil.symmToVecMV(grainList[i].vMat - numpy.eye(3))
        pVec   = grainList[i].detectorGeom.pVec

        x = numpy.vstack([angAxs[0]*angAxs[1], biotT.reshape(6, 1), pVec.reshape(3, 1)])
        resd.append(grainList[i]._fitF_objFunc(x))
        pass
    resd = numpy.hstack(resd).flatten()
    return sum(resd**2)
Beispiel #3
0
    def dump_grain(self, grain_id, completeness, chisq, grain_params):
        assert len(grain_params) == 12, \
            "len(grain_params) must be 12, not %d" % len(grain_params)

        # extract strain
        emat = logm(np.linalg.inv(mutil.vecMVToSymm(grain_params[6:])))
        evec = mutil.symmToVecMV(emat, scale=False)

        res = [int(grain_id), completeness, chisq] \
            + grain_params.tolist() \
            + evec.tolist()
        output_str = self._delim.join([
            self._delim.join(['{:<12d}', '{:<12f}',
                              '{:<12e}']).format(*res[:3]),
            self._delim.join(np.tile('{:<23.16e}',
                                     len(res) - 3)).format(*res[3:])
        ])
        print(output_str, file=self.fid)
        return output_str
Beispiel #4
0
    def dump_grain(self, grain_id, completeness, chisq,
                   grain_params):
        assert len(grain_params) == 12, \
            "len(grain_params) must be 12, not %d" % len(grain_params)

        # extract strain
        emat = logm(np.linalg.inv(mutil.vecMVToSymm(grain_params[6:])))
        evec = mutil.symmToVecMV(emat, scale=False)

        res = [int(grain_id), completeness, chisq] \
            + grain_params.tolist() \
            + evec.tolist()
        output_str = self._delim.join(
            [self._delim.join(
                ['{:<12d}', '{:<12f}', '{:<12e}']
             ).format(*res[:3]),
             self._delim.join(
                np.tile('{:<23.16e}', len(res) - 3)
             ).format(*res[3:])]
        )
        print(output_str, file=self.fid)
        return output_str
 def inverse_stretch(self):
     m = np.array(self.stretch_matrix).reshape(3, 3)
     return matrixutil.symmToVecMV(np.linalg.inv(m), scale=True)
Beispiel #6
0
def compare_grain_fits(fit_grain_params,
                       ref_grain_params,
                       mtol=1.e-4,
                       ctol=1.e-3,
                       vtol=1.e-4):
    """
    Executes comparison between reference and fit grain parameters for ff-HEDM
    for the same initial parameters.

    Parameters
    ----------
    fit_grain_params : array_like, (n, 12)
        The fit grain parameters to be tested.
    ref_grain_params : array_like, (n, 12)
        The reference grain parameters (see Notes below).

    Returns
    -------
    bool
        True is successful comparison

    Notes
    -----
    The fitgrains action currently returns
        grain_id, completeness, chisq, grain_params.
    We will have to assume that the grain_ids are in the *same order* as the
    reference, which can be enforces by running the comparison using the
    reference orientation list.
    """
    fit_grain_params = np.atleast_2d(fit_grain_params)
    ref_grain_params = np.atleast_2d(ref_grain_params)
    cresult = False
    ii = 0
    for fg, rg in zip(fit_grain_params, ref_grain_params):
        # test_orientation
        quats = rot.quatOfExpMap(np.vstack([fg[:3], rg[:3]]).T)
        ang, mis = rot.misorientation(quats[:, 0].reshape(4, 1),
                                      quats[:, 1].reshape(4, 1))
        if ang <= mtol:
            cresult = True
        else:
            print("orientations for grain %d do not agree." % ii)
            return cresult

        # test position
        if np.linalg.norm(fg[3:6] - rg[3:6]) > ctol:
            print("centroidal coordinates for grain %d do not agree." % ii)
            return False

        # test strain
        vmat_fit = mutil.symmToVecMV(np.linalg.inv(mutil.vecMVToSymm(fg[6:])),
                                     scale=False)
        vmat_ref = mutil.symmToVecMV(np.linalg.inv(mutil.vecMVToSymm(rg[6:])),
                                     scale=False)
        if np.linalg.norm(vmat_fit - vmat_ref, ord=1) > vtol:
            print("stretch components for grain %d do not agree." % ii)
            return False

        # index grain id
        ii += 1
    return cresult