def test_contraction_conical_Miller_coefficients(): from fluids.fittings import contraction_conical_Miller_tck path = os.path.join(fluids_data_dir, 'Miller 2E 1990 conical contractions K part 1.csv') Kds, l_ratios, A_ratios = Engauge_2d_parser(open(path).readlines()) path = os.path.join(fluids_data_dir, 'Miller 2E 1990 conical contractions K part 2.csv') Kd2, l_ratio2, A_ratio2 = Engauge_2d_parser(open(path).readlines()) Kds.extend(Kd2) l_ratios.extend(l_ratio2) A_ratios.extend(A_ratio2) A_ratios = [[i + 1.0 for i in j] for j in A_ratios] # # The second set of data obviously looks terirble when plotted # # Normally the data should be smoothed, but, well, the smoothing # # function also requires smooth functions. # for K, ls, As in zip(Kds, l_ratios, A_ratios): # plt.loglog(ls, np.array(As)-1, label=str(K)) # plt.legend() # plt.show() all_zs = [] all_xs = [] all_ys = [] for z, xs, ys in zip(Kds, l_ratios, A_ratios): for x, y in zip(xs, ys): all_zs.append(z) all_xs.append(x) all_ys.append(y) tck = bisplrep(np.log(all_xs), np.log(all_ys), all_zs, kx=3, ky=1, s=.0001) [ assert_allclose(i, j) for i, j in zip(contraction_conical_Miller_tck, tck) ]
def test_bend_rounded_Miller_K_coefficients(): from fluids import fluids_data_dir from fluids.core import Engauge_2d_parser from fluids.fittings import tck_bend_rounded_Miller Kb_curve_path = os.path.join(fluids_data_dir, 'Miller 2E 1990 smooth bends Kb.csv') lines = open(Kb_curve_path).readlines() all_zs, all_xs, all_ys = Engauge_2d_parser(lines, flat=True) tck_recalc = bisplrep(all_xs, all_ys, all_zs, kx=3, ky=3, s=.001)
def test_bend_miter_Miller_coefficients(): from fluids.optional.pychebfun import chebfun, chebfun_to_poly curve_path = os.path.join(fluids_data_dir, 'Miller 2E 1990 Kb mitre bend.csv') text = open(curve_path).readlines() zs, x_lists, y_lists = Engauge_2d_parser(text) x_raw, y_raw = x_lists[0], y_lists[0] univar = UnivariateSpline(x_raw, y_raw, s=1e-4) fun = chebfun(f=univar, domain=[0,120], N=15) # 15 max for many coeffs recalc_coeffs = chebfun_to_poly(fun) from fluids.fittings import bend_miter_Miller_coeffs assert_allclose(bend_miter_Miller_coeffs, recalc_coeffs)
def test_diffuser_conical_Miller_coefficients(): from fluids.fittings import tck_diffuser_conical_Miller path = os.path.join(fluids_data_dir, 'Miller 2E 1990 conical diffuser Kd.csv') Kds, l_ratios, A_ratios = Engauge_2d_parser(open(path).readlines()) # Fixup stupidity A_ratios = [[i + 1.0 for i in j] for j in A_ratios] # for K, ls, As in zip(Kds, l_ratios, A_ratios): # plt.loglog(ls, np.array(As)-1) # plt.show() interp_objs = [] for K, ls, As in zip(Kds, l_ratios, A_ratios): univar = UnivariateSpline(np.log10(ls), np.log10(As), s=4e-5) interp_objs.append(univar) # Extrapolation to the left and right looks bad # Extrapolation upwards looks bad too ls_full = np.logspace(np.log10(0.1), np.log10(20)) ls_stored = [] As_stored = [] for i, (K, ls, As) in enumerate(zip(Kds, l_ratios, A_ratios)): # plt.loglog(ls, As) univar = interp_objs[i] As_full = 10**univar(np.log10(ls_full)) # plt.loglog(ls_full, As_full) # print(len(univar.get_coeffs()), len(univar.get_knots())) ls_smoothed = np.logspace(np.log10(ls[0]), np.log10(ls[-1]), 100) As_smoothed = 10**univar(np.log10(ls_smoothed)) # plt.loglog(ls_smoothed, As_smoothed) ls_stored.append(ls_smoothed) As_stored.append(As_smoothed) # plt.show() all_zs = [] all_xs = [] all_ys = [] for z, xs, ys in zip(Kds, ls_stored, As_stored): for x, y in zip(xs, ys): all_zs.append(z) all_xs.append(x) all_ys.append(y) tck_recalc = bisplrep(np.log(all_xs), np.log(all_ys), all_zs, s=.002) [ assert_allclose(i, j, rtol=1e-2) for i, j in zip(tck_diffuser_conical_Miller, tck_recalc) ]
def test_contraction_abrupt_Miller_coefficients(): from fluids.fittings import tck_contraction_abrupt_Miller curve_path = os.path.join(fluids_data_dir, 'Miller 2E 1990 abrupt contraction K.csv') text = open(curve_path).readlines() zs, x_lists, y_lists = Engauge_2d_parser(text) for xs, values in zip(x_lists, y_lists): values[-1] = 0 low = 1e-8 for i in range(2): low = low / 10 values.insert(-1, low) xs.insert(-1, 1 - low) xs[-1] = 1 inter_objs = [] for rd, As, Ks in zip(zs, x_lists, y_lists): univar = UnivariateSpline(As, Ks, s=1e-5) inter_objs.append(univar) # make a rectangular grid As = np.linspace(0, 1, 1000) Ks_stored = [] for obj in inter_objs: Ks_smoothed = obj(As) Ks_smoothed[Ks_smoothed < 0] = 0 # Avoid zeros Ks_stored.append(Ks_smoothed) # Flatten the data to the form used in creating the spline all_zs = [] all_xs = [] all_ys = [] for z, x, ys in zip(zs, As, Ks_stored): for x, y in zip(As, ys): all_zs.append(z) all_xs.append(x) all_ys.append(y) tck_recalc = bisplrep(all_xs, all_zs, all_ys, s=5e-4) [ assert_allclose(i, j, rtol=1e-2) for i, j in zip(tck_contraction_abrupt_Miller, tck_recalc) ]
def test_entrance_rounded_Miller_coefficients(): from fluids.fittings import entrance_rounded_Miller_coeffs path = os.path.join(fluids_data_dir, 'Miller 2E 1990 entrances rounded beveled K.csv') lines = open(path).readlines() _, ratios, Ks = Engauge_2d_parser(lines) ratios_45, ratios_30, ratios_round = ratios Ks_45, Ks_30, Ks_round = Ks # plt.plot(ratios_round, Ks_round) t_ds2 = np.linspace(ratios_round[0], ratios_round[1], 1000) # Ks_Rennels = [entrance_rounded(Di=1, rc=t) for t in t_ds2] # plt.plot(t_ds2, Ks_Rennels) obj = UnivariateSpline(ratios_round, Ks_round, s=6e-5) # plt.plot(t_ds2, obj(t_ds2)) fun = chebfun(f=obj, domain=[0,.3], N=8) # plt.plot(t_ds2, fun(t_ds2), '--') # plt.show() coeffs = chebfun_to_poly(fun) assert_allclose(coeffs, entrance_rounded_Miller_coeffs)
def test_bend_rounded_Miller_outlet_tangent_correction(): from fluids.fittings import tck_bend_rounded_Miller_C_Re Re_curve_path = os.path.join( fluids_data_dir, 'Miller 2E 1990 smooth bends outlet tangent length correction.csv') text = open(Re_curve_path).readlines() Kbs, length_ratio_lists, Co_lists = Engauge_2d_parser(text) def BioScience_GeneralizedSubstrateDepletion_model(x_in): '''Fit created using zunzun.com, comparing the non-linear, non-logarithmic plot values with pixel positions on the graph. 0 0.00 1 311 2 493 4 721 6 872 10 1074 20 1365 30 1641 40 1661 ''' temp = 0.0 a = 1.0796070184265327E+03 b = 2.7557612059844967E+00 c = -2.1529870432577212E+01 d = 4.1229208061974096E-03 temp = (a * x_in) / (b + x_in) - (c * x_in) - d return temp def fix(y): # Reverse the plot # Convert input "y" to between 0 and 1661 y = y / 30 # 0-1 linear y *= 1641 # to max err = lambda x: BioScience_GeneralizedSubstrateDepletion_model(x) - y return float(fsolve(err, 1)) for values in length_ratio_lists: for i in range(len(values)): x = min(values[i], 30) # Do not allow values over 30 values[i] = fix(x) # Plotting code # inter_objs = [] # for Kb, lrs, Cos in zip(Kbs, length_ratio_lists, Co_lists): # univar = UnivariateSpline(lrs, Cos, s=4e-4) # Default smoothing is great! # inter_objs.append(univar) # for i, (Kb, lrs, Cos) in enumerate(zip(Kbs, length_ratio_lists, Co_lists)): # plt.semilogx(lrs, Cos, 'x') # univar = inter_objs[i] # Cs_smoothed = univar(lrs) # plt.semilogx(lrs, Cs_smoothed) # plt.ylim([0.3, 3]) # plt.xlim([0.1, 30]) # plt.show() # Code to literally write the code min_vals = [] tcks = [] for Kb, lrs, Cos in zip(Kbs, length_ratio_lists, Co_lists): univar = splrep(lrs, Cos, s=4e-4) # Default smoothing is great! s = ('tck_bend_rounded_Miller_C_o_%s = ' % str(Kb).replace('.', '_')) template = 'np.array(%s),\n' t1 = template % str(univar[0].tolist()) t2 = template % str(univar[1].tolist()) s = s + '[%s%s3]' % (t1, t2) # print(s) min_vals.append(float(splev(0.01, univar))) tcks.append(univar) # Check the fixed constants above the function from fluids.fittings import tck_bend_rounded_Miller_C_os for tck, tck_recalc in zip(tck_bend_rounded_Miller_C_os, tcks): [assert_allclose(i, j) for i, j in zip(tck, tck_recalc)] from fluids.fittings import bend_rounded_Miller_C_o_limit_0_01 assert_allclose(min_vals, bend_rounded_Miller_C_o_limit_0_01) from fluids.fittings import bend_rounded_Miller_C_o_limits max_ratios = [i[-1] for i in length_ratio_lists] assert_allclose(max_ratios, bend_rounded_Miller_C_o_limits)
def test_bend_rounded_Miller_Re_correction(): from fluids import fluids_data_dir from fluids.core import Engauge_2d_parser from fluids.fittings import tck_bend_rounded_Miller_C_Re Re_curve_path = os.path.join( fluids_data_dir, 'Miller 2E 1990 smooth bends Re correction.csv') text = open(Re_curve_path).readlines() rds, Re_lists, C_lists = Engauge_2d_parser(text) inter_objs = [] for rd, Res, Cs in zip(rds, Re_lists, C_lists): univar = UnivariateSpline(np.log10(Res), Cs) # Default smoothing is great! inter_objs.append(univar) for i, (rd, Res, Cs) in enumerate(zip(rds, Re_lists, C_lists)): # plt.semilogx(Res, Cs) univar = inter_objs[i] Cs_smoothed = univar(np.log10(Res)) # plt.semilogx(Res, Cs_smoothed) # print(univar.get_coeffs(), univar.get_knots()) # plt.show() # make a rectangular grid Res = np.logspace(np.log10(1E4), np.log10(1E8), 100) Cs_stored = [] for obj in inter_objs: Cs_smoothed = obj(np.log10(Res)) # plt.semilogx(Res, Cs_smoothed) Cs_stored.append(Cs_smoothed) # plt.show() # Flatten the data to the form used in creating the spline all_zs = [] all_xs = [] all_ys = [] for z, x, ys in zip(rds, Res, Cs_stored): for x, y in zip(Res, ys): all_zs.append(z) all_xs.append(x) all_ys.append(y) tck_recalc = bisplrep(np.log10(all_xs), all_zs, all_ys) [ assert_allclose(i, j) for i, j in zip(tck_bend_rounded_Miller_C_Re, tck_recalc) ] spline_obj = lambda Re, r_D: bisplev(np.log10(Re), r_D, tck_recalc) Res = np.logspace(np.log10(1E4), np.log10(1E8), 100) for obj, r_d in zip(inter_objs, rds): Cs_smoothed = obj(np.log10(Res)) # plt.semilogx(Res, Cs_smoothed) # Cs_spline = spline_obj(Res, r_d) # plt.semilogx(Res, Cs_spline, '--') for r in np.linspace(1, 2, 10): Cs_spline = spline_obj(Res, r) # plt.semilogx(Res, Cs_spline, '-') # plt.show() from fluids.fittings import bend_rounded_Miller_C_Re_limit_1 from fluids.fittings import bend_rounded_Miller_C_Re ps = np.linspace(1, 2) qs = [ newton(lambda x: bend_rounded_Miller_C_Re(x, i) - 1, 2e5) for i in ps ] rs = np.polyfit(ps, qs, 4).tolist() assert_allclose(rs, bend_rounded_Miller_C_Re_limit_1)