def plot_ineq_check(): """ Check the second part of the inequality :return: """ from forward_model_nominal import compute_delta_T_dual_heating_cooling from get_volumetric_heat_capacity import get_volumetric_heat_capacity from gen_time_vec import gen_time_vec from get_alpha import get_alpha_k_C from numpy import pi from ei_inv_cooling import ei_inv_cooling_array r = 6e-3 q = 50.0 k = 5.0 theta_o = 0.01 theta_w = 0.40 theta_m = 1 - theta_o - theta_w c = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) alpha = get_alpha_k_C(k, c) t_heating = 8 th = t_heating T = 30 fs = 120 t = gen_time_vec(fs, T, tstart=0) t = t[1:] dT_hc = compute_delta_T_dual_heating_cooling(q, k, alpha, r, t, t_heating) nn = int(np.ceil(fs * t_heating)) dT_cool = dT_hc[nn:] t_cool = t[nn:] term = (4 * pi * k) / q dT_cool0 = term * dT_cool # this is the input to find the inverse h = (r**2) / (4 * alpha) x = h / (t_cool - th) y = h / t z = np.sqrt((t_cool - th) / t_cool) high = (t_cool - th) * (((1 + z) / z) * (1.0 / dT_cool0)) print('h = ', h) aout = ei_inv_cooling_array(dT_cool0, t_cool, th) print('done inverse') block = False plt.figure() plt.plot(t, dT_hc) plt.plot(t_cool, dT_cool) plt.axvline(x=t_heating) plt.show(block) block = False plt.figure() plt.plot(t_cool, dT_cool0) plt.plot(t_cool, high) plt.show(block) block = True plt.figure() plt.plot(t_cool, aout) plt.show(block)
def test_full_inverse_heating(): q = 50 k = 4 r0 = 5.7e-3 r1 = 15e-3 theta_o = 1.0e-3 theta_w = 0.49 theta_m = 1 - theta_o - theta_w C = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) fs = 12 dt = 1 / fs T = 30 t = gen_time_vec(fs, T) t = t[1:] # cannot start with zero n = length(t) r = np.linspace(r0, r1, n) alpha = get_alpha_k_C(k, C) t_heating = 8 th = t_heating # dT = compute_delta_T_dual_infinite_line_heating(q, k, alpha, r, t) # obtain the forward model # r0_found, r_t = recomp_radius(fs, q, k, t, dT) # print('r0 = ', r0) # print('r0_found = ', r0_found) gamma4 = (r**2) / (4 * alpha) gamma5 = np.sqrt(gamma4) gamma5_comp = r / (2 * np.sqrt(alpha)) gamma6 = np.log(gamma5) gamma7 = forward_first_derivative(gamma6, dt) gamma7_comp = forward_first_derivative(np.log(r), dt) r0_in = r0 gamma8 = np.exp(inverse_first_derivative(gamma7, dt, np.log(r0_in))) gamma8 = gamma8 - gamma8[0] print('delta_r = ', r[2] - r[1]) print('delta_r = ', gamma8[2] - gamma8[1]) plt.figure() plt.plot(gamma8) plt.plot(r) plt.show()
def test_small_argument(): from ei import ei from get_alpha import get_alpha_k_C from get_volumetric_heat_capacity import get_volumetric_heat_capacity from gen_time_vec import gen_time_vec q = 50 k = 4 r0 = 5.0e-3 r1 = 50e-3 theta_o = 1.0e-3 theta_w = 0.49 theta_m = 1 - theta_o - theta_w C = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) fs = 120 dt = 1 / fs T = 30 t = gen_time_vec(fs, T) t = t[1:] # cannot start with zero n = length(t) r = np.linspace(r0, r1, n) alpha = get_alpha_k_C(k, C) print('alpha = ', alpha) gamma4 = r**2 / (4 * alpha) gamma5 = np.log(np.sqrt(gamma4)) gamma5_comp = np.log(r) - np.log(2 * np.sqrt(alpha)) gamma5_diff = gamma5 - gamma5_comp # take the average gamma6 = np.average(gamma5) N = length(gamma5) gamma6_comp = (1.0 / N) * np.sum(np.log(r)) - np.log(2 * np.sqrt(alpha)) print('gamma6 = ', gamma6) print('gamma6_comp = ', gamma6_comp) print('diff = ', gamma6 - gamma6_comp) # subtract from original gamma7 = gamma5 - gamma6 gamma7_comp = np.log(r) - (1 / N) * (np.sum(np.log(r))) gamma7_diff = gamma7 - gamma7_comp
def run_dp_model_inv_test(show_plot=False, run_full=False): """ Run the SP model inverse test to show an example of the reconstruction :param show_plot: True to show the plot :param run_full: True to run the reconstruction over the heating and cooling curve False to run the reconstruction over only the heating curve :return: """ ################################################ # MAIN ################################################ q = 45 k = 5.2 theta_m = 0.59 theta_o = 9.2e-3 theta_w = 0.40 C = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) alpha = get_alpha_k_C(k, C) r0 = 6e-3 fs = 12 t0 = 0 max_delta_r = 5.0e-3 timestep = 1 / fs t_heat = 8 # 8 seconds for heating (when working with the full curve) if run_full: t1 = 60*3 # 3 minutes for heating and cooling else: t1 = 8 # 8 seconds for heating # create the time vector t = np.arange(t0, t1, timestep) # required since t0 = 0 and this cannot be used since Ei(x=0) is not defined rstep = length(t)-1 ########################################################### # LINEAR CHANGE (INCREASE) ########################################################### print('Computing for linear increase in r(t)...') r = r0 + np.linspace(0, max_delta_r, rstep) dT_dual, dT_dual_fixed, r_det_mm, r_mm, r_t_det_diff, tt = \ run_plot_output(alpha, fs, k, q, r0, run_full, t_heat, t, r) ########################################################### # LINEAR CHANGE (DECREASE) ########################################################### print('Computing for linear decrease in r(t)...') rdec = r[::-1] r0_dec = rdec[0] dT_dual_dec, dT_dual_fixed_dec, r_det_mm_dec, r_mm_dec, r_t_det_diff_dec, tt = \ run_plot_output(alpha, fs, k, q, r0_dec, run_full, t_heat, t, rdec) ########################################################### # RANDOM WALK ########################################################### print('Computing for Brownian random walk in r(t)...') nr = length(r) rb = brownian_noise_norm(nr, r0, r0 + max_delta_r) rb0 = rb[0] dT_dual_bn, dT_dual_fixed_bn, r_det_mm_bn, r_mm_bn, r_t_det_diff_bn, tt = \ run_plot_output(alpha, fs, k, q, rb0, run_full, t_heat, t, rb) ########################################################### # FIGURES ########################################################### print('DONE COMPUTING, RUNNING FIGURES') fig = plt.figure(num=None, figsize=LARGE_SQUARE_FIGSIZE) # INCREASE # (a) ax = fig.add_subplot(3, 3, 1) ax.plot(tt, dT_dual_fixed, label='DP Fixed Radius', color='orange') ax.plot(tt, dT_dual, label='DP Variable Radius', color="cornflowerblue") # ax.set_xlabel('Time (s)') ticklabels_off_x() ax.set_ylabel(create_label('$\Delta T \hspace{1}$', 'K')) ax.legend() ax.set_title('(a)', loc='center') # (b) ax = fig.add_subplot(3, 3, 2) ax.plot(tt, r_mm, color=MODELLED_COLOR, linewidth=4, label='Forward') ax.plot(tt, r_det_mm, color='grey', ls='dashed', label='Inverse') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})$', 'mm')) # ax.set_xlabel('Time (s)') ticklabels_off_x() ax.legend() ax.set_title('(b)', loc='center') # (c) ax = fig.add_subplot(3, 3, 3) ax.plot(tt, r_t_det_diff, color='grey', label='difference') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})\hspace{1}$ Forward -\n Inverse', 'mm')) # ax.set_xlabel('Time (s)') ticklabels_off_x() ax.set_title('(c)', loc='center') # DECREASE # (d) ax = fig.add_subplot(3, 3, 4) ax.plot(tt, dT_dual_fixed_dec, label='DP Fixed Radius', color='orange') ax.plot(tt, dT_dual_dec, label='DP Variable Radius', color="cornflowerblue") ticklabels_off_x() # ax.set_xlabel('Time (s)') ax.set_ylabel(create_label('$\Delta T \hspace{1}$', 'K')) # ax.legend() ax.set_title('(d)', loc='center') # (e) ax = fig.add_subplot(3, 3, 5) ax.plot(tt, r_mm_dec, color=MODELLED_COLOR, linewidth=4, label='Forward') ax.plot(tt, r_det_mm_dec, color='grey', ls='dashed', label='Inverse') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})$', 'mm')) ticklabels_off_x() # ax.set_xlabel('Time (s)') # ax.legend() ax.set_title('(e)', loc='center') # (f) ax = fig.add_subplot(3, 3, 6) ax.plot(tt, r_t_det_diff_dec, color='grey', label='difference') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})\hspace{1}$ Forward -\n Inverse', 'mm')) ticklabels_off_x() # ax.set_xlabel('Time (s)') ax.set_title('(f)', loc='center') # RANDOM WALK # (g) ax = fig.add_subplot(3, 3, 7) ax.plot(tt, dT_dual_fixed_bn, label='DP Fixed Radius', color='orange') ax.plot(tt, dT_dual_bn, label='DP Variable Radius', color="cornflowerblue") ax.set_xlabel('Time (s)') ax.set_ylabel(create_label('$\Delta T \hspace{1}$', 'K')) # ax.legend() ax.set_title('(g)', loc='center') # (h) ax = fig.add_subplot(3, 3, 8) ax.plot(tt, r_mm_bn, color=MODELLED_COLOR, linewidth=4, label='Forward') ax.plot(tt, r_det_mm_bn, color='grey', ls='dashed', label='Inverse') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})$', 'mm')) ax.set_xlabel('Time (s)') ylim = list(ax.get_ylim()) ylim[1] += 0.20*(ylim[1]-ylim[0]) ax.set_ylim(ylim) ax.legend() ax.set_title('(h)', loc='center') # (i) ax = fig.add_subplot(3, 3, 9) ax.plot(tt, r_t_det_diff_bn, color='grey', label='difference') ax.set_ylabel(create_label('$r\hspace{0.3}(\hspace{0.3}t\hspace{0.3})\hspace{1}$ Forward -\n Inverse', 'mm')) ax.set_xlabel('Time (s)') ax.set_title('(i)', loc='center') # SHOW AND SAVE FIGURE plt.tight_layout() plt.savefig(FIG_PATH + DP_SYNTH_FILENAME_EXAMPLE + PLOT_EXTENSION) if show_plot: plt.show()
def plot_ei_test(): from ei import ei from get_alpha import get_alpha_k_C from get_volumetric_heat_capacity import get_volumetric_heat_capacity from gen_time_vec import gen_time_vec q = 50 k = 4 r0 = 6.7e-3 r1 = 15e-3 theta_o = 1.0e-3 theta_w = 0.49 theta_m = 1 - theta_o - theta_w C = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) fs = 12 dt = 1 / fs T = 30 t = gen_time_vec(fs, T) t = t[1:] # cannot start with zero n = length(t) r = np.linspace(r0, r1, n) alpha = get_alpha_k_C(k, C) print('alpha = ', alpha) t_heating = 8 th = t_heating dT, heating, cooling, first, second, tvec_heat, tvec_cool, rheat, rcool, ei_term3, ei_term2 = \ compute_delta_T_dual_heating_cooling(q, k, alpha, r, t, t_heating, split=True) # check the recomposition from dp_model_recomp import dp_model_recomp_heating_cooling, dp_model_recomp_heating r_t = dp_model_recomp_heating_cooling(fs, q, k, t, th, dT, r0) r_t_heat = dp_model_recomp_heating(fs, q, k, tvec_heat, heating, r0) block = True plt.figure() plt.plot(t, r_t) plt.plot(t, r) plt.plot(tvec_heat, r_t_heat) plt.show(block) # dT_known = dT # from numpy import pi # from dp_model_recomp import ei_inv_neg_array, ei_inv_cooling # from deriv_forward_inv import forward_first_derivative, inverse_first_derivative # out = np.zeros(n) # term = -(4*pi*k)/q # dt = 1 / fs # nn = int(np.ceil(fs*th)) # tvec_heat = t[:nn] # tvec_cool = t[nn:] # dT_known_heat = dT_known[:nn] # dT_known_cool = dT_known[nn:] # # HEATING # gamma2_heat = term*dT_known_heat # gamma3_heat = ei_inv_neg_array(gamma2_heat) * tvec_heat # gamma4_heat = -gamma3_heat # # COOLING # dT_cool_strip = -term*dT_known_cool # gamma4_cool = ei_inv_cooling(dT_cool_strip, th, tvec_cool) # gamma4 = np.concatenate((gamma4_heat, gamma4_cool)) # AFTER HERE # gamma5 = np.sqrt(gamma4) # gamma6 = np.log(gamma5) # gamma7 = forward_first_derivative(gamma6, dt) # # gamma7_comp = forward_first_derivative(np.log(r), dt) # gamma7_diff = gamma7 - gamma7_comp # # # check the inverse # r0_wanted = 6e-3 # log_r_inv = inverse_first_derivative(gamma7, dt, np.log(r0_wanted)) # r_inv = np.exp(log_r_inv) # # # block = False # plt.figure() # plt.plot(t[1:], gamma7) # plt.plot(t[1:], gamma7_comp) # plt.show(block) # # block = False # plt.figure() # plt.plot(t[1:], gamma7_diff) # plt.show(block) # block = True # plt.figure() # plt.plot(t[1:], linear_test_vec_log_deriv) # plt.plot(t[1:], gamma7) # plt.show(block) # block = True # plt.figure() # plt.plot(t, r_inv) # plt.show(block) from dp_model_recomp import dp_model_recomp_heating_cooling
def test_full_inverse_heating_determine_r(): q = 50 k = 4 r0 = 5.7e-3 r1 = 15e-3 theta_o = 1.0e-3 theta_w = 0.49 theta_m = 1 - theta_o - theta_w C = get_volumetric_heat_capacity(theta_m, theta_o, theta_w) fs = 12 dt = 1 / fs T = 30 t = gen_time_vec(fs, T) t = t[1:] # cannot start with zero n = length(t) r = np.linspace(r0, r1, n) alpha = get_alpha_k_C(k, C) t_heating = 8 th = t_heating # this is the known curve print('Running for different starting r0 values') dT = compute_delta_T_dual_infinite_line_heating(q, k, alpha, r, t) r0v = [4e-3, 5e-3, 5.7e-3, 6e-3] dT_inv_vec = [] for r0_elem in r0v: print('r0_elem = ', r0_elem) r_t_det = dp_model_recomp_heating(fs, q, k, t, dT, r0_elem, get_gamma4=False) dT_inv = compute_delta_T_dual_infinite_line_heating( q, k, alpha, r_t_det, t) dT_inv_vec.append(dT_inv) print('Starting optimization to find the known r') rstart = 1.0e-3 # def _f_obtain_r_from_curve(x, *args): # # UNKNOWN # r0_in = x[0] # # KNOWN # q_in = args[0] # k_in = args[1] # alpha_in = args[2] # t_in = args[3] # fs_in = args[4] # known_in = args[5] # r_t_det = dp_model_recomp_heating(fs, q_in, k_in, t_in, known_in, r0_in, get_gamma4=False) # dT_inv = compute_delta_T_dual_infinite_line_heating(q_in, k_in, alpha_in, r_t_det, t_in) # diff = (known_in - dT_inv)**2 # dsum = np.sum(diff) # return dsum # # use optimization for the starting r # xstart = (rstart,) # args = (q, k, alpha, t, fs, dT) # res = minimize(_f_obtain_r_from_curve, xstart, args, method='Nelder-Mead') # rout = res.x[0] # print('rout = ', rout) # print('r0 = ', r0) # block = True plt.figure() plt.plot(t, dT, linewidth=8) for dT_i in dT_inv_vec: plt.plot(t, dT_i) plt.show(block)