コード例 #1
0
ファイル: fitting.py プロジェクト: jackey-qiu/DaFy
def fit_crystal(uc, refl_list):
    try:
        uc_system, uc_params = uc.get_lattice_params()
        start = uc_params
        lower = [
            0,
        ] * len(uc_params)
        upper, sigma = _get_uc_upper_limits(uc_system)
    except AttributeError:
        raise DiffcalcException(
            "UB matrix not initialised. Cannot run UB matrix fitting procedure."
        )
    try:
        from org.apache.commons.math3.optim.nonlinear.scalar.noderiv import CMAESOptimizer
        from org.apache.commons.math3.optim.nonlinear.scalar import GoalType
        from org.apache.commons.math3.optim import MaxEval, InitialGuess, SimpleBounds
        from org.apache.commons.math3.random import MersenneTwister

        optimizer = CMAESOptimizer(30000, 0, True, 10, 0, MersenneTwister(),
                                   True, None)
        opt = optimizer.optimize(
            (MaxEval(5000), get_crystal_target(refl_list,
                                               uc_system), GoalType.MINIMIZE,
             CMAESOptimizer.PopulationSize(15), CMAESOptimizer.Sigma(sigma),
             InitialGuess(start), SimpleBounds(lower, upper)))
        vals = opt.getPoint()
        #res = opt.getValue()
    except ImportError:
        from scipy.optimize import minimize

        ref_data = _get_refl_hkl(refl_list)
        bounds = zip(lower, upper)
        res = minimize(_func_crystal,
                       start,
                       args=(uc_system, ref_data),
                       method='SLSQP',
                       tol=1e-10,
                       options={
                           'disp': False,
                           'maxiter': 10000,
                           'eps': 1e-6,
                           'ftol': 1e-10
                       },
                       bounds=bounds)
        vals = res.x
    res_cr = CrystalUnderTest('trial', uc_system, 1, 1, 1, 90, 90, 90)
    res_cr._set_cell_for_system(uc_system, *vals)
    return res_cr
コード例 #2
0
ファイル: fitting.py プロジェクト: jackey-qiu/DaFy
def _func_crystal(vals, uc_system, ref_data):
    trial_cr = CrystalUnderTest('trial', uc_system, 1, 1, 1, 90, 90, 90)
    try:
        trial_cr._set_cell_for_system(uc_system, *vals)
    except Exception:
        return 1e6

    res = 0
    I = matrix('1 0 0; 0 1 0; 0 0 1')
    for (hkl_vals, pos_vals, en) in ref_data:
        wl = 12.3984 / en
        [_, DELTA, NU, _, _, _] = create_you_matrices(*pos_vals.totuple())
        q_pos = (NU * DELTA - I) * matrix([[0], [2 * pi / wl], [0]])
        q_hkl = trial_cr.B * hkl_vals
        res += (norm(q_pos) - norm(q_hkl))**2
    return res