def alpha_constrain_err(err, d0, d1, d2, d3, valid=True, debug=False): N = len(d0) zeros = np.zeros(N) d0 = np.min([d0, zeros], axis=0) del_0 = np.abs(d0*100) d1 = np.max([d1, zeros], axis=0) del_1 = np.abs(d1*1000) d2 = np.min([d2, zeros], axis=0) del_2 = np.abs(d2*5000) d3 = np.max([d3, zeros], axis=0) del_3 = np.abs(d3*500000000) tot = np.sum(del_0) + np.sum(del_1) + np.sum(del_2) + np.sum(del_3) if tot == 0 and not valid: # if not Twu91_check_params(params): del_0 = 1e5*np.ones(N) if debug: tot = np.sum(err) + tot print(tot, err, del_0, del_1, del_2, del_3) err += del_0 err += del_1 err += del_2 err += del_3 return err
def alpha_poly_objfc(params, Trs, alphas_over_a, domain=None, debug=False): try: coeffs = [i.value for i in params.values()] # already sorted except: coeffs = params v, d1, d2, d3 = horner_and_der3(coeffs, Trs) err = np.abs(v - alphas_over_a) valid = poly_check_params(coeffs, domain=domain) err = alpha_constrain_err(err, v, d1, d2, d3, valid=valid, debug=debug) return err
def alpha_Twu91_objfc(params, Trs, alphas_over_a, debug=False): try: c0, c1, c2 = params['c0'].value, params['c1'].value, params['c2'].value except: c0, c1, c2 = params x0 = c1 - 1.0 x1 = c2*x0 x2 = c1*c2 x3 = Trs**x2 x4 = Trs**x1*np.exp(-c0*(x3 - 1.0)) x5 = c0*x3 x6 = c1*x5 x7 = c2*x4 x8 = x1 - 1.0 x9 = x6*(x2*x5 - x2 + 1.0) x10 = 3.0*x1 x11 = c2*c2 x12 = 3.0*x2 x13 = c1*c1*x11 Trs_inv = 1.0/Trs Trs_inv2 = Trs_inv*Trs_inv Trs_inv3 = Trs_inv*Trs_inv2 alphas_calc = d0 = x4 d1 = x7*(x0 - x6)*Trs_inv d2 = x7*(x0*x8 - 2.0*x1*x6 + x9)*Trs_inv2 d3 = (-x7*(-x0*(x0*x0*x11 - x10 + 2.0) + x10*x6*x8 - x10*x9 + x6*(Trs**(2.0*x2)*c0*c0*x13 + x12*x5 - x12 - 3.0*x13*x5 + x13 + 2.0) )*Trs_inv3) err = np.abs(alphas_calc - alphas_over_a) valid = Twu91_check_params(params) err = alpha_constrain_err(err, d0, d1, d2, d3, valid=valid, debug=debug) return err
def postproc_lmfit(result): result.aard = np.abs(result.residual).sum()/result.ndata return result