Exemplo n.º 1
0
def run_find_hpp_nominal_test_sig():
    """
    Do experiment to obtain nominal HPP data
    :return:
    """
    from calibration_processing import initial_compute_hpp
    from comparisons import compute_rmse, compute_mb
    from inverse_model_nominal import obtain_sp_vars_from_curve_min, compute_single_probe_forward_model_min
    # path = '../hpp-data/hpp-formatted-data/calibration/July-12-2017/'
    path = '../hpp-data/hpp-formatted-data/sand/July-20-2017/'
    # path = '../hpp-data/hpp-formatted-data/sand/July-21-2017/'
    # path = '../hpp-data/hpp-formatted-data/sand/July-26-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-26-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-27-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-30-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-31-2017/'
    start_string = 'sand-'
    # start_string = 'cal-'
    # start_string = 'sand-'
    # start_string = 'peat-'
    downsample = 'dp'
    filt = 'both'
    filt_cutoff = 10  # cutoff filter frequency (Hz)
    fs = 120  # sample rate of device (Hz)
    fs_down = 12  # downsample frequency (Hz)
    t_cold = 1  # time of cold sampling at beginning (s)
    additional_text = '-rep1'
    r0 = 6e-3  # starting radius

    # sand
    theta_o = 9.2e-3  # organic content
    theta_m = 0.59  # mineral content

    # Cm_set = 1.1e6
    # Co_set = 1.0e6
    Cm_set = None
    Co_set = None

    # peat
    # theta_o = 0.49
    # theta_m = 0.01

    #############################################################################

    q = 45
    t_heat = 8
    t_total = 3 * SECONDS_IN_MIN

    # q = 45
    # t_heat = 11
    # t_total = 3 * SECONDS_IN_MIN

    # q = 55
    # t_heat = 20
    # t_total = 3 * SECONDS_IN_MIN

    # q = 55
    # t_heat = 89
    # t_total = 3 * SECONDS_IN_MIN

    # q = 10
    # t_heat = 10 * SECONDS_IN_MIN
    # t_total = 12 * SECONDS_IN_MIN

    # q = 20
    # t_heat = 89
    # t_total = 3 * SECONDS_IN_MIN

    ################################################

    # LOAD IN THE COEFFICIENTS USED FOR CALIBRATION AND OBTAIN r_nominal
    # NOTE that the calibrated r0 is stored in mm, so we need to convert to m to be able to use the data
    from obtain_load_r0 import SaveLoadR0
    sr = SaveLoadR0()
    sr.load_r0(CONST_PATH + CAL_R0_FILENAME + SAVEZ_EXT)
    r_nominal = sr.get_r0(q) * 1.0e-3

    # We always use the assumed q and the step detector q
    use_assumed_q = False
    use_step_detector_q = True

    I, Rw, T1, T2, Vout, dV, dac_code, delta_T1, delta_T1_trim, delta_T1_trim_heating, \
    delta_T2, delta_T2_trim, num, qav, qprime, step_q_tfirst, step_q_tsecond, t1, t1_trim, \
    t1_trim_heating, t2, t2_trim, ypk, t, rmse_q_calc, mb_q_calc, pd_q_calc, t2_trim_heating, \
    delta_T2_trim_heating = \
        initial_compute_hpp(start_string, additional_text,
        downsample, filt, filt_cutoff, fs, fs_down, path, q, t_cold, t_heat, t_total, use_assumed_q,
        use_step_detector_q)

    dt_sp = t1_trim_heating[1] - t1_trim_heating[0]
    dt_dp = t2_trim_heating[1] - t2_trim_heating[0]

    from sp_model_signal import sp_model_signal_inv
    from curve_time_trim import curve_time_trim
    from sp_model_signal import sp_model_late_time_inv
    from inverse_model_nominal import obtain_k_alpha_from_dual_probe
    from inverse_model_dp_radius import InverseWarmupCurveDP
    from get_theta_rho import get_theta_rho

    # heating only for SP
    t1_trim_heating1, delta_T1_trim_heating1 = curve_time_trim(
        t1_trim_heating, delta_T1_trim_heating)
    # heating only for DP
    t2_trim_heating1, delta_T2_trim_heating1 = curve_time_trim(
        t2_trim_heating, delta_T2_trim_heating)
    # heating and cooling for DP
    t2_trim1, delta_T2_trim1 = curve_time_trim(t2_trim, delta_T2_trim)

    # SP SIGNAL PROCESSING INVERSE MODEL
    kdet_sig, bdet = sp_model_signal_inv(delta_T1_trim_heating1,
                                         t1_trim_heating1,
                                         dt_sp,
                                         q,
                                         output_model_array=False)

    th = t_heat
    kstart = 5
    Hstart = 80
    k_det, alpha_det = obtain_k_alpha_from_dual_probe(qav,
                                                      t2_trim1,
                                                      th,
                                                      delta_T2_trim1,
                                                      kstart,
                                                      Hstart,
                                                      r_nominal,
                                                      full=True)
    dT_synth = compute_delta_T_dual_heating_cooling(qav,
                                                    k_det,
                                                    alpha_det,
                                                    r_nominal,
                                                    t2_trim1,
                                                    th,
                                                    split=False)

    # from lmfit.models import SkewedGaussianModel
    # model = SkewedGaussianModel()
    # params = model.make_params(amplitude=10, center=0, sigma=1, gamma=0)
    # result = model.fit(delta_T2_trim1, params, x=t2_trim1)
    # y = result.best_fit

    from get_max import get_max, get_min
    idx, m = get_max(dT_synth)
    print('dt_dp = ', dt_dp)
    idx_time = dt_dp * idx
    print('idx = ', idx)
    print('idx_time = ', idx_time)

    from deriv_forward_inv import forward_first_derivative
    dT_deriv = forward_first_derivative(dT_synth, dt_dp)
    from numerical_derivative import second_derivative_3pt
    tt, dT_deriv2 = second_derivative_3pt(dT_synth, dt_dp, t2_trim1)

    # from find_plateau_curvature import obtain_curvature_1d
    # cc = obtain_curvature_1d(dT_synth, 5, 3, dt_dp)
    idx_peak, md_peak = get_max(dT_synth)
    idxd, md = get_min(dT_deriv2)
    # if idxd > idx_peak:
    #     idxd_found = idxd

    idxd_found = idx_peak
    idxd_time = dt_dp * idxd_found
    # print('idxd_time = ', idxd_time)
    # print('idxd_time_heat = ', idxd_time + th)

    # set the trim time at the end
    # end_trim_time = 40      # only for 21 July
    end_trim_time = 0
    end_cut_num = int(np.floor(fs_down * end_trim_time))

    # Cut the sequence at the beginning
    # cut_time = idxd_time
    # cut_time = idxd_time
    cut_time = idxd_time
    print('cut_time = ', cut_time)
    cut_num = int(np.floor(fs_down * cut_time))
    if end_cut_num > 0:
        t2_trim1_cut = t2_trim1[cut_num:-end_cut_num]
        delta_T2_trim1_cut = delta_T2_trim1[cut_num:-end_cut_num]
    else:
        t2_trim1_cut = t2_trim1[cut_num:]
        delta_T2_trim1_cut = delta_T2_trim1[cut_num:]

    block = False
    plt.figure()
    plt.plot(t2_trim1, delta_T2_trim1)
    plt.plot(t2_trim1, dT_synth)
    # plt.plot(t2_trim1, y)
    plt.plot(t2_trim1[1:], dT_deriv)
    plt.plot(tt, dT_deriv2)
    # plt.plot(t2_trim1, cc)
    plt.axvline(cut_time)
    plt.title('DP heating and cooling')
    plt.show(block)

    # obtain the radius
    from dp_model_recomp import dp_model_recomp_heating, dp_model_recomp_heating_cooling, \
        dp_model_recomp_heating_cooling_trim
    # r_t_heating = dp_model_recomp_heating(fs_down, q, kdet_sig, t2_trim_heating1_cut, delta_T2_trim_heating1_cut,
    #                                       r_nominal)
    r_t_heating_cooling, gamma4 = dp_model_recomp_heating_cooling_trim(
        fs_down,
        qav,
        kdet_sig,
        t2_trim1_cut,
        th,
        delta_T2_trim1_cut,
        r_nominal,
        get_gamma4=True)

    # tcut_heat = 7.0
    # cut_num_heat = int(np.floor(fs_down * tcut_heat))
    # r_t_heating, gamma4_heating = dp_model_recomp_heating(fs_down, qav, kdet_sig, t2_trim_heating1[1:],
    #                                                       delta_T2_trim_heating1[1:], r_nominal, get_gamma4=True)

    # compute alpha
    alpha = (r_t_heating_cooling**2) / (4.0 * gamma4)
    alpha_sig = np.mean(alpha)

    print('k_det = ', k_det)
    print('alpha_det = ', alpha_det)

    print('kdet_sig = ', kdet_sig)
    print('alpha_sig = ', alpha_sig)

    print('alpha = ', alpha_sig)

    theta_w_var, rho_nom_var = get_theta_rho(k_det, alpha_det, theta_o,
                                             theta_m, Cm_set, Co_set)
    theta_w_sig, rho_w_sig = get_theta_rho(kdet_sig, alpha_sig, theta_o,
                                           theta_m, Cm_set, Co_set)

    print('------------------------------')
    print('theta_w_var = ', theta_w_var)
    print('rho_nom_var = ', rho_nom_var)
    print('------------------------------')
    print('theta_w_sig = ', theta_w_sig)
    print('rho_w_sig = ', rho_w_sig)
    print('------------------------------')

    # TEST THE INVERSE
    print('testing the inverse')
    from recomp_theta_rho import recomp_theta_rho_additional
    theta_w_var, rho_nom_var, theta_w_sig, rho_w_sig, \
    t2_trim1, delta_T2_trim1, dT_synth_nom, dT_synth_sig, \
    cut_time, t2_trim1_cut, r_t_heating_cooling, \
    alpha_det, alpha_sig, k_det, kdet_sig, \
    t1_trim_heating1, delta_T1_trim_heating1, dT_synth_sp, \
    delta_T1_trim, t1_trim, qav = \
    recomp_theta_rho_additional(path, start_string, additional_text, q, t_heat, t_total, end_trim_time)
    print('done testing')

    print('------------------------------')
    print('theta_w_var = ', theta_w_var)
    print('rho_nom_var = ', rho_nom_var)
    print('------------------------------')
    print('theta_w_sig = ', theta_w_sig)
    print('rho_w_sig = ', rho_w_sig)
    print('------------------------------')
Exemplo n.º 2
0
def run_find_hpp_nominal():
    """
    Do experiment to obtain nominal HPP data
    :return:
    """
    from calibration_processing import initial_compute_hpp
    from comparisons import compute_rmse, compute_mb
    from inverse_model_nominal import obtain_sp_vars_from_curve_min, compute_single_probe_forward_model_min
    # path = '../hpp-data/hpp-formatted-data/calibration/July-12-2017/'
    path = '../hpp-data/hpp-formatted-data/sand/July-20-2017/'
    # path = '../hpp-data/hpp-formatted-data/sand/July-21-2017/'
    # path = '../hpp-data/hpp-formatted-data/sand/July-26-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-26-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-27-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-30-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-30-2017/'
    # path = '../hpp-data/hpp-formatted-data/peat/July-31-2017/'
    start_string = 'sand-'
    # start_string = 'cal-'
    # start_string = 'peat-'
    downsample = 'dp'
    filt = 'both'
    filt_cutoff = 10  # cutoff filter frequency (Hz)
    fs = 120  # sample rate of device (Hz)
    fs_down = 12  # downsample frequency (Hz)
    t_cold = 1  # time of cold sampling at beginning (s)
    additional_text = '-rep1'
    r0 = 6e-3  # starting radius (m)

    # Tikhonov regularization parameter
    epsilon = 0.0

    #############################################################################

    q = 45
    t_heat = 8
    t_total = 3 * SECONDS_IN_MIN

    # q = 10
    # t_heat = 10 * SECONDS_IN_MIN
    # t_total = 12 * SECONDS_IN_MIN

    ################################################

    # LOAD IN THE COEFFICIENTS USED FOR CALIBRATION AND OBTAIN r_nominal
    # NOTE that the calibrated r0 is stored in mm, so we need to convert to m to be able to use the data
    from obtain_load_r0 import SaveLoadR0
    sr = SaveLoadR0()
    sr.load_r0(CONST_PATH + CAL_R0_FILENAME + SAVEZ_EXT)
    r_nominal = sr.get_r0(q) * 1.0e-3
    # r_nominal = 6e-3

    # We always use assumed q and use the step detector q
    use_assumed_q = False
    use_step_detector_q = True

    I, Rw, T1, T2, Vout, dV, dac_code, delta_T1, delta_T1_trim, delta_T1_trim_heating, \
    delta_T2, delta_T2_trim, num, qav, qprime, step_q_tfirst, step_q_tsecond, t1, t1_trim, \
    t1_trim_heating, t2, t2_trim, ypk, t, rmse_q_calc, mb_q_calc, pd_q_calc, t2_trim_heating, \
    delta_T2_trim_heating = \
        initial_compute_hpp(start_string, additional_text,
        downsample, filt, filt_cutoff, fs, fs_down, path, q, t_cold, t_heat, t_total, use_assumed_q,
        use_step_detector_q)

    dt_sp = t1_trim_heating[1] - t1_trim_heating[0]
    dt_dp = t2_trim_heating[1] - t2_trim_heating[0]

    from sp_model_signal import sp_model_signal_inv
    from curve_time_trim import curve_time_trim
    from sp_model_signal import sp_model_late_time_inv
    from inverse_model_nominal import obtain_k_alpha_from_dual_probe
    from inverse_model_dp_radius import InverseWarmupCurveDP
    from get_theta_rho import get_theta_rho

    # heating only for SP
    t1_trim_heating1, delta_T1_trim_heating1 = curve_time_trim(
        t1_trim_heating, delta_T1_trim_heating)
    # heating only for DP
    t2_trim_heating1, delta_T2_trim_heating1 = curve_time_trim(
        t2_trim_heating, delta_T2_trim_heating)
    # heating and cooling for DP
    t2_trim1, delta_T2_trim1 = curve_time_trim(t2_trim, delta_T2_trim)

    # SP SIGNAL PROCESSING INVERSE MODEL
    kdet_sig, bdet = sp_model_signal_inv(delta_T1_trim_heating1,
                                         t1_trim_heating1,
                                         dt_sp,
                                         q,
                                         output_model_array=False)
    # SP inverse model to obtain k via a late-time model
    tlinear, kout_sp_late_time = sp_model_late_time_inv(
        delta_T1_trim_heating1, t1_trim_heating1, dt_sp, qav)

    # Calculate the time at which the heat pulse goes linear relative to the start of the experiment.
    # This also includes the cold time at the beginning of the experiment (used for graphing).
    tlinear_from_begin = t_cold + tlinear

    # SP FORWARD MODELS
    kd_signal, bd_signal, cd_signal, dd_signal = obtain_sp_vars_from_curve(
        qav, t1_trim_heating1, delta_T1_trim_heating1, kout_sp_late_time)
    kd_lt, bd_lt, cd_lt, dd_lt = obtain_sp_vars_from_curve(
        qav, t1_trim_heating1, delta_T1_trim_heating1, kout_sp_late_time)

    # SP SYNTHETIC MODELS
    delta_T_single_synth_signal = compute_single_probe_forward_model(
        qav, kd_signal, t1_trim_heating1, bd_signal, cd_signal, dd_signal)
    delta_T_single_synth_late_time = compute_single_probe_forward_model(
        qav, kd_lt, t1_trim_heating1, bd_lt, cd_lt, dd_lt)

    # OBTAIN {k, alpha} FROM THE NOMINAL DP MODEL (HEATING AND COOLING)
    # NOTE THAT THIS REQUIRES AN ESTIMATE OF THE PROBE SPACING RADIUS as r_nominal READ FROM A COEFFICIENT FILE
    kstart = kd_signal
    Hstart = 80
    k_nom_heat, alpha_nom_heat = obtain_k_alpha_from_dual_probe(
        q,
        t2_trim_heating1,
        t_heat,
        delta_T2_trim_heating1,
        kstart,
        Hstart,
        r_nominal,
        full=False)  # heat only
    k_nom_heat_and_cool, alpha_nom_heat_and_cool = obtain_k_alpha_from_dual_probe(
        qav,
        t2_trim1,
        t_heat,
        delta_T2_trim1,
        kstart,
        Hstart,
        r_nominal,
        full=True)  # heat and cool
    # OBTAIN {r(t), alpha} FROM THE SIGNAL PROCESSING MODEL
    # r(t) = r_t_sig as the time-variable radius
    InvDP = InverseWarmupCurveDP()
    typ = 'iterative'
    from scipy.interpolate import spline
    from scipy.interpolate import UnivariateSpline
    # s = spline(t2_trim_heating1, delta_T2_trim_heating1, t2_trim_heating1, order=5, kind='smoothest')
    spl = UnivariateSpline(t2_trim_heating1, delta_T2_trim_heating1)
    s = spl(t2_trim_heating1)  # spline (apply to inverse warmup curve)
    from deriv_forward_inv import forward_first_derivative
    sderiv = forward_first_derivative(s, 1 / fs_down)

    # run a zero crossing detector to determine when sderiv is zero and then cut at that point
    nn = length(sderiv)
    ncut = 0
    for k in range(nn):
        if sderiv[k] > 0:
            ncut = k
            break
    ncut += 1
    print('ncut = ', ncut)
    tt_ncut = t2_trim_heating1[ncut + 1:]
    sderiv_ncut = sderiv[ncut:]
    tt_cut = tt_ncut[0]

    # block = True
    # plt.figure()
    # # plt.plot(tt_ncut, sderiv_ncut)
    # plt.plot(t2_trim_heating1[1:], sderiv)
    # plt.axvline(x=tt_cut)
    # plt.show(block)

    scut = s[ncut:]
    tcut = t2_trim_heating1[ncut:]

    # scut = s
    # tcut = t2_trim_heating1
    # compute from the inverse warmup curve
    r0 = 6e-3
    alpha_sig, r_t_sig = InvDP.inverse_warmup_curve_dp(kdet_sig,
                                                       scut,
                                                       qav,
                                                       tcut,
                                                       r_nominal,
                                                       typ,
                                                       lowpass=False,
                                                       epsilon=0.0)

    # average over r_t_sig
    from integrate_average import integrate_average
    r_av = integrate_average(r_t_sig, tcut, 'simpson')
    # r_av = np.mean(r_t_sig)
    print('r_av = ', r_av)

    #############################################################################
    # OBTAIN {k, alpha} FROM THE NOMINAL DP MODEL (HEATING AND COOLING)
    # USE THE r_av FROM SIGNAL PROCESSING
    kstart = kd_signal
    Hstart = 80
    # r_av = r_nominal
    print('r_nominal = ', r_nominal)
    k_sig_heat, alpha_sig_heat = obtain_k_alpha_from_dual_probe(
        q,
        t2_trim_heating1,
        t_heat,
        delta_T2_trim_heating1,
        kstart,
        Hstart,
        r_av,
        full=False)  # heat only
    k_sig_heat_and_cool, alpha_sig_heat_and_cool = obtain_k_alpha_from_dual_probe(
        qav, t2_trim1, t_heat, delta_T2_trim1, kstart, Hstart, r_av,
        full=True)  # heat and cool
    #############################################################################

    # DP SYNTHETICS NOMINAL
    deltaT_dual_synth_nominal_heat_cool = compute_delta_T_dual_heating_cooling(
        qav, k_nom_heat, alpha_nom_heat, r_nominal, t2_trim1, t_heat)
    deltaT_dual_synth_nominal_heating = compute_delta_T_dual_infinite_line_heating(
        qav, k_nom_heat_and_cool, alpha_nom_heat_and_cool, r_nominal,
        t2_trim_heating1)

    # DP SYNTHETIC WITH RADIUS DETERMINED FROM SIGNAL PROCESSING
    deltaT_dual_syth_variable_radius_heating = compute_delta_T_dual_infinite_line_heating(
        qav, kdet_sig, alpha_sig, r_av, t2_trim_heating1)
    # set the organic content
    theta_o = 9.2e-3

    # COMPUTATIONS
    # {theta_w, rho} for DP (heating)
    theta_w_heat, rho_heat = get_theta_rho(k_nom_heat, alpha_nom_heat, theta_o)
    # {theta_w, rho} for DP (heating and cooling)
    theta_w_heat_cool, rho_heat_cool = get_theta_rho(k_nom_heat_and_cool,
                                                     alpha_nom_heat_and_cool,
                                                     theta_o)

    # SIGNAL PROCESSING (HEAT AND COOL)
    theta_w_heat_sig, rho_heat_sig = get_theta_rho(kdet_sig, alpha_sig,
                                                   theta_o)
    theta_w_heat_cool_sig, rho_heat_cool_sig = get_theta_rho(
        kdet_sig, alpha_sig, theta_o)

    # DUAL PROBE
    # RMSE, MB asnd PD for DP model actual vs synthetic (heating)
    # RMSE, MB and PD for DP model actual vs synthetic (heating and cooling)
    # RMSE, MB and PD for DP model actual vs synthetic variable radius (heating only) SIGNAL PROCESSING
    # SINGLE PROBE
    # RMSE, MB and PD for SP model actual vs synthetic (late-time)
    # RMSE, MB and PD for SP model actual vs synthetic (signal processing)

    # from butterworth_low import butter_lowpass_filter
    # y = butter_lowpass_filter(delta_T2_trim_heating1, 1, fs, order=5, both=True)

    # block = False
    # plt.figure()
    # plt.plot(t2_trim_heating1, y)
    # plt.xlabel('Time (s)')
    # plt.ylabel('delta T2 heating')
    # plt.legend()
    # plt.show(block)

    print('OUTPUTS:')
    print('kdet_sig (signal processing) = ', kdet_sig)
    print('tlinear (late-time) = ', tlinear)
    print('kout_sp_late_time (late-time) = ', kout_sp_late_time)
    print('')
    print('k_nom_heat = ', k_nom_heat)
    print('alpha_nom_heat = ', alpha_nom_heat)
    print('')
    print('k_nom_heat_and_cool = ', k_nom_heat_and_cool)
    print('alpha_nom_heat_and_cool = ', alpha_nom_heat_and_cool)
    print('')
    print('alpha_sig (from signal processing with variable radius) = ',
          alpha_sig)
    print('THETA AND RHO')
    print('-----')
    print('theta_w_heat = ', theta_w_heat)
    print('rho_heat = ', rho_heat)
    print('-----')
    print('theta_w_heat_cool = ', theta_w_heat_cool)
    print('rho_heat_cool = ', rho_heat_cool)

    print('----SIGNAL PROCESSING----')
    print('theta_w_heat_sig = ', theta_w_heat_sig)
    print('rho_heat_sig = ', rho_heat_sig)
    print('-----')
    print('---HEAT AND COOL:---')
    print('theta_w_heat_cool_sig = ', theta_w_heat_cool_sig)
    print('rho_heat_cool_sig = ', rho_heat_cool_sig)
    print('--------------------------')

    block = False
    plt.figure()
    plt.plot(t1_trim_heating1, delta_T1_trim_heating1, label='data')
    plt.plot(t1_trim_heating1, delta_T_single_synth_signal, label='signal')
    plt.plot(t1_trim_heating1,
             delta_T_single_synth_late_time,
             label='late time')
    plt.xlabel('Time (s)')
    plt.ylabel('delta T1 heating SP')
    plt.legend()
    plt.show(block)

    # plt.plot(t2_trim1, delta_T2_trim1, label='data heating and cooling')
    # plt.plot(t2_trim_heating1, deltaT_dual_synth_nominal_heating, label='heating')
    # plt.plot(t2_trim1, deltaT_dual_synth_nominal_heat_cool, label='heating and cooling')
    block = False
    plt.figure()
    plt.plot(t2_trim_heating1, delta_T2_trim_heating1, label='data heating')
    # plt.plot(t2_trim_heating1, deltaT_dual_syth_variable_radius_heating, label='heating (variable radius)')
    plt.plot(t2_trim_heating1, s, label='heating (variable radius) smooth')
    plt.legend()
    plt.xlabel('Time (s)')
    plt.ylabel('delta T2 heating DP')
    plt.show(block)

    block = True
    plt.figure()
    plt.plot(tcut, r_t_sig, label='r(t)')
    plt.legend()
    plt.xlabel('Time (s)')
    plt.ylabel('r(t)')
    plt.show(block)
Exemplo n.º 3
0
def recomp_theta_rho_additional(path,
                                start_string,
                                additional_text,
                                q,
                                t_heat,
                                t_total,
                                end_trim_time,
                                trim_sp_begin=0,
                                r_nominal=None,
                                theta_o=None,
                                theta_m=None,
                                add_time_after_max=None):
    """
    Open a file and recompose theta, rho and other inputs
    :param path:                        as the path where to do the processing
    :param start_string:                as the starting string of the file
    :param additional_text:             as the additional text at the end
    :param q:                           as the heat input into the soil
    :param t_heat:                      as the time of heating
    :param t_total:                     as the total time
    :param end_trim_time:               as the time to trim at the beginning of the sequence
    :param trim_sp_begin:               as the number of points to trim at the beginning of the SP sequence
                                        for inverse model
    :param r_nominal:                   as the nominal radius (to override the calibrated radius)
    :param theta_o:                     fraction of organic content in the soil
    :param theta_m:                     fraction of mineral content in the soil
    :param: add_time_after_max:         time to cut the curve after the maximum
    :return:
    """
    downsample = 'dp'
    filt = 'both'
    filt_cutoff = DP_LOWPASS_FILTER_CUT
    fs = FS_SAMPLE
    fs_down = FS_DOWN_DP
    t_cold = T_COLD_BEGIN

    if add_time_after_max is None:
        add_time_after_max = NOM_TIME_ADD_AFTER_MAX

    sr = SaveLoadR0()
    sr.load_r0(CONST_PATH + CAL_R0_FILENAME + SAVEZ_EXT)
    if r_nominal is None:
        r_nominal = sr.get_r0(q) * 1.0e-3

    # set whether we are dealing with sand or peat
    if 'sand' in start_string:
        Cm_set = None
        Co_set = None
        if theta_o is None:
            theta_o = theta_o_sand
        if theta_m is None:
            theta_m = theta_m_sand
    elif 'peat' in start_string:
        Cm_set = Cm_peat
        Co_set = Co_peat
        if theta_o is None:
            theta_o = theta_o_peat
        if theta_m is None:
            theta_m = theta_m_peat
    else:
        raise ValueError(
            'recomp_theta_rho_additional: start_string must contain sand or peat'
        )

    use_assumed_q = False
    use_step_detector_q = True

    I, Rw, T1, T2, Vout, dV, dac_code, delta_T1, delta_T1_trim, delta_T1_trim_heating, \
    delta_T2, delta_T2_trim, num, qav, qprime, step_q_tfirst, step_q_tsecond, t1, t1_trim, \
    t1_trim_heating, t2, t2_trim, ypk, t, rmse_q_calc, mb_q_calc, pd_q_calc, t2_trim_heating, \
    delta_T2_trim_heating = \
        initial_compute_hpp(start_string, additional_text,
        downsample, filt, filt_cutoff, fs, fs_down, path, q, t_cold, t_heat, t_total, use_assumed_q,
        use_step_detector_q, return_q_full=False)

    dt_sp = t1_trim_heating[1] - t1_trim_heating[0]
    dt_dp = t2_trim_heating[1] - t2_trim_heating[0]

    t1_trim_heating1, delta_T1_trim_heating1 = curve_time_trim(
        t1_trim_heating, delta_T1_trim_heating)
    t2_trim_heating1, delta_T2_trim_heating1 = curve_time_trim(
        t2_trim_heating, delta_T2_trim_heating)
    t2_trim1, delta_T2_trim1 = curve_time_trim(t2_trim, delta_T2_trim)

    kdet_sig, _bdet = sp_model_signal_inv(
        delta_T1_trim_heating1[trim_sp_begin:],
        t1_trim_heating1[trim_sp_begin:],
        dt_sp,
        qav,
        output_model_array=False)

    tlinear, kdet_linear = sp_model_late_time_inv(delta_T1_trim_heating1,
                                                  t1_trim_heating1,
                                                  dt_sp,
                                                  qav,
                                                  entire_heating_time=False,
                                                  return_all=False)

    th = t_heat
    kstart = 5
    Hstart = 80
    k_det, alpha_det = obtain_k_alpha_from_dual_probe(qav,
                                                      t2_trim1,
                                                      th,
                                                      delta_T2_trim1,
                                                      kstart,
                                                      Hstart,
                                                      r_nominal,
                                                      full=True)
    dT_synth_nom = compute_delta_T_dual_heating_cooling(qav,
                                                        k_det,
                                                        alpha_det,
                                                        r_nominal,
                                                        t2_trim1,
                                                        th,
                                                        split=False)

    k_out_sp, b_out_sp, c_out_sp, d_out_sp = obtain_sp_vars_from_curve(
        qav, t1_trim_heating1, delta_T1_trim_heating1, kdet_sig)
    dT_synth_sp = compute_single_probe_forward_model(qav,
                                                     k_out_sp,
                                                     t1_trim_heating1,
                                                     b_out_sp,
                                                     c_out_sp,
                                                     d_out_sp,
                                                     constrain=True)

    idx_peak, md_peak = get_max(dT_synth_nom)
    idxd_found = idx_peak
    idxd_time = dt_dp * idxd_found
    end_cut_num = int(np.floor(fs_down * end_trim_time))

    cut_time = idxd_time + add_time_after_max  # time when the curve needs to be cut (can also be negative)
    cut_num = int(np.floor(fs_down * cut_time))
    if end_cut_num > 0:
        t2_trim1_cut = t2_trim1[cut_num:-end_cut_num]
        delta_T2_trim1_cut = delta_T2_trim1[cut_num:-end_cut_num]
    else:
        t2_trim1_cut = t2_trim1[cut_num:]
        delta_T2_trim1_cut = delta_T2_trim1[cut_num:]

    r_t_heating_cooling, gamma4 = dp_model_recomp_heating_cooling_trim(
        fs_down,
        qav,
        kdet_sig,
        t2_trim1_cut,
        th,
        delta_T2_trim1_cut,
        r_nominal,
        get_gamma4=True)
    alpha_vec = (r_t_heating_cooling**2) / (4.0 * gamma4)
    alpha_sig = np.mean(np.abs(alpha_vec))

    theta_w_nom, rho_nom = get_theta_rho(k_det, alpha_det, theta_o, theta_m,
                                         Cm_set, Co_set)
    theta_w_sig, rho_sig = get_theta_rho(kdet_sig, alpha_sig, theta_o, theta_m,
                                         Cm_set, Co_set)

    dT_synth_sig = compute_delta_T_dual_heating_cooling(qav,
                                                        kdet_sig,
                                                        alpha_sig,
                                                        r_nominal,
                                                        t2_trim1,
                                                        th,
                                                        split=False)
    """
    theta_w_nom                 water content from nominal heating and cooling curve-fitting
    rho_nom                     density from nominal heating and cooling curve-fitting
    theta_w_sig                 water content from signal processing 
    rho_w_sig                   density from signal processing
        
    t2_trim1                    time vector for DP experiment
    delta_T2_trim1              temperature difference vector for DP experiment
    dT_synth_nom                synthetic curve for DP using nominal method
    dT_synth_sig                synthetic curve for DP using signal processing
    
    cut_time                    time when sequence is cut
    t2_trim1_cut                cut time sequence for heating and cooling
    r_t_heating_cooling         r(t) for heating and cooling determined over cut time
    
    alpha_det                   alpha from DP curve-fitting
    alpha_sig                   alpha from signal processing
        
    k_det                       thermal conductivity from curve-fitting
    kdet_sig                    thermal conductivity from signal processing
    
    t1_trim_heating1            trimmed SP time vector
    delta_T1_trim_heating1      trimmed SP time heating vector
    dT_synth_sp                 synthetic model from single probe
    
    delta_T1_trim               change in temperature of SP
    t1_trim                     time vector associated with SP     
    
    tlinear                     time at which curve becomes linear
    kdet_linear                 k detected using linear algorithm                   
    """
    return theta_w_nom, rho_nom, theta_w_sig, rho_sig, \
           t2_trim1, delta_T2_trim1, dT_synth_nom, dT_synth_sig, \
           cut_time, t2_trim1_cut, r_t_heating_cooling, \
           alpha_det, alpha_sig, k_det, kdet_sig, \
           t1_trim_heating1, delta_T1_trim_heating1, dT_synth_sp, \
           delta_T1_trim, t1_trim, \
           tlinear, kdet_linear, qav
def run_sp_model_example(q, k, t, b, c, d, show_plot=False):
    """
    Run the SP model example and the inverse to show how a synthetic curve can be obtained
    :param q:
    :param k:
    :param t:
    :param b:
    :param c:
    :param d:
    :return:
    """
    dT = sp_model_nominal(q, k, t, b, c, d)
    dT_synth0, t0 = cut_to_zero(dT, t)
    dt = t0[1] - t0[0]
    kdet, bdet, tth, dT_sig_pro = sp_model_signal_inv(dT_synth0, t0, dt, q, output_model_array=True,
                                                      cutoff=0.5, filter=False)
    dT_sig_pro_comp = dT_sp_model_signal(q, kdet, tth, bdet)
    dT_sig_pro_comp_diff = dT_sig_pro - dT_sig_pro_comp
    kd, bd, cd, dd = obtain_sp_vars_from_curve(q, t0, dT_synth0, kdet)
    dT_comp = sp_model_nominal(q, kd, t0, bd, cd, dd)
    dT_comp_diff = dT_synth0 - dT_comp

    fig = plt.figure(num=None, figsize=SQUARE_FIGSIZE)

    # (a)
    ax = fig.add_subplot(2, 2, 1)
    # ax.set_xlabel('Time (s)')
    ticklabels_off_x()
    ax.set_ylabel(create_label('$\Delta T$', 'K'))
    ax.plot(t0, dT_synth0, color=MODELLED_COLOR, linewidth=4, label='Forward')
    ax.plot(t0, dT_comp, color='grey', ls='dashed', label='Inverse')
    ax.legend()
    ax.set_title('(a)', loc='center')

    # (b)
    ax = fig.add_subplot(2, 2, 2)
    # ax.set_xlabel('Time (s)')
    ticklabels_off_x()
    ax.set_ylabel(create_label('$\Delta T \hspace{1}$ Forward - Inverse', 'K'))
    ax.plot(t0, dT_comp_diff, color=GREY_COLOR, label='Difference', ls='solid')
    ax.legend()
    ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
    ax.set_title('(b)', loc='center')

    # (c)
    ax = fig.add_subplot(2, 2, 3)
    ax.set_xlabel('Time (s)')
    ax.set_ylabel(create_label('$\Delta \Gamma _{5} \hspace{0.3} (\hspace{0.3} t \hspace{0.3})$', ''))
    ax.plot(tth, dT_sig_pro_comp, color=MODELLED_COLOR, linewidth=4, label='Forward (Processed)')
    ax.plot(tth, dT_sig_pro, color='grey', ls='dashed', label='Theoretical')
    ax.set_title('(c)', loc='center')
    ax.legend()

    # (d)
    ax = fig.add_subplot(2, 2, 4)
    ax.set_xlabel('Time (s)')
    ax.set_ylabel(create_label('$\Delta \Gamma _{5} \hspace{0.3} (\hspace{0.3} t \hspace{0.3}) \hspace{1}$ '
                               'Forward - Inverse', 'K'))
    ax.plot(tth, dT_sig_pro_comp_diff, color=GREY_COLOR, ls='solid')
    ax.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
    ax.set_title('(d)', loc='center')

    plt.tight_layout()
    plt.savefig(FIG_PATH + SP_SYNTH_FILENAME_EXAMPLE + PLOT_EXTENSION)
    if show_plot:
        plt.show()
Exemplo n.º 5
0
def test_calibration_k(path, start_string, additional_text, downsample, filt,
                       filt_cutoff, fs, fs_down, q, t_cold, t_heat, t_total,
                       k_assumed, Hstart):
    """
    Obtain the k from a series of files and test to see how the k can be determined
    :param path:
    :param start_string:
    :param additional_text:
    :param downsample:
    :param filt:
    :param filt_cutoff:
    :param fs:
    :param fs_down:
    :param q:
    :param t_cold:
    :param t_heat:
    :param t_total:
    :param k_assumed:
    :param Hstart:
    :return:
    """
    use_assumed_q = False
    use_step_detector_q = True
    I, Rw, T1, T2, Vout, dV, dac_code, delta_T1, delta_T1_trim, delta_T1_trim_heating, delta_T2, \
    delta_T2_trim, num, qav, qprime, step_q_tfirst, step_q_tsecond, t1, t1_trim, t1_trim_heating, t2, t2_trim, \
    ypk, t, rmse_q_calc, mb_q_calc, pd_q_calc, t2_trim_heating, delta_T2_trim_heating = \
        initial_compute_hpp(start_string, additional_text,
                            downsample, filt, filt_cutoff, fs, fs_down, path, q, t_cold, t_heat, t_total,
                            use_assumed_q,
                            use_step_detector_q)

    # use the average q determined from the curve
    qq = qav
    # starting k for inverse models
    kstart = k_assumed

    # CURVE-FITTING HEATING DP
    t2_trim_heating1, delta_T2_trim_heating1 = curve_time_trim(
        t2_trim_heating, delta_T2_trim_heating)
    t_heating = t_heat
    full = False
    kout_dp_heating, _hout = obtain_k_H_from_dual_probe(
        qq, t2_trim_heating1, t_heating, delta_T2_trim_heating1, kstart,
        Hstart, full)

    # CURVE-FITTING HEATING AND COOLING
    t2_trim1, delta_T2_trim1 = curve_time_trim(t2_trim, delta_T2_trim)
    full = True
    kout_dp_heating_cooling, _hout = obtain_k_H_from_dual_probe(
        q, t2_trim1, t_heating, delta_T2_trim1, kstart, Hstart, full)

    # LINEAR LATE-TIME MODEL
    t1_trim_heating1, delta_T1_trim_heating1 = curve_time_trim(
        t1_trim_heating, delta_T1_trim_heating)
    dt = t1_trim_heating[1] - t1_trim_heating[0]
    tlinear, kout_sp_late_time = sp_model_late_time_inv(
        delta_T1_trim_heating1, t1_trim_heating1, dt, qav)
    # NOTE that tlinear is with respect to the cut curve

    # LINEAR LATE-TIME MODEL (ALL DATA)
    entire = True
    _tlinear, kout_sp_all = sp_model_late_time_inv(delta_T1_trim_heating1,
                                                   t1_trim_heating1, dt, qav,
                                                   entire)

    # SIGNAL PROCESSING TO OBTAIN K
    kout_signal, bdet_signal = sp_model_signal_inv(delta_T1_trim_heating1,
                                                   t1_trim_heating1, dt, qav)

    # CURVE-FITTING AFTER SIGNAL PROCESSING
    kout_signal_curve_fit, _bd, _cd, _dd = obtain_sp_vars_from_curve(
        q, t1_trim_heating1, delta_T1_trim_heating1, kout_signal)
    """
    OUTPUTS: 
    kout_dp_heating             = curve-fit to dual probe heating
    kout_dp_heating_cooling     = curve-fit to dual probe heating and cooling
    kout_sp_late_time           = single probe late-time model
    tlinear                     = time detected when the curve becomes linear
    kout_sp_all                 = single probe late-time model (all heating data)
    kout_signal                 = signal processing to obtain k 
    bdet_signal                 = b that is detected from the model
    kout_signal_curve_fit       = signal processing and then fitting to the full SP model
    """
    return kout_dp_heating, kout_dp_heating_cooling, kout_sp_late_time, tlinear, kout_sp_all, \
            kout_signal, bdet_signal, kout_signal_curve_fit
Exemplo n.º 6
0
def run_k_q_example(start_string, additional_text, downsample, filt, filt_cutoff, fs, fs_down,
                    path, q, t_cold, t_heat, t_total, show, fn):
    use_assumed_q = False
    use_step_detector_q = True
    I, Rw, T1, T2, Vout, dV, dac_code, delta_T1, delta_T1_trim, delta_T1_trim_heating, \
    delta_T2, delta_T2_trim, num, qav, qprime, step_q_tfirst, step_q_tsecond, t1, t1_trim, \
    t1_trim_heating, t2, t2_trim, ypk, t, rmse_q_calc, mb_q_calc, pd_q_calc, t2_trim_heating, \
    delta_T2_trim_heating = \
                            initial_compute_hpp(start_string, additional_text,
                            downsample, filt, filt_cutoff, fs, fs_down, path, q, t_cold, t_heat, t_total,
                            use_assumed_q,
                            use_step_detector_q)
    t1_trim_heating1, delta_T1_trim_heating1 = curve_time_trim(t1_trim_heating, delta_T1_trim_heating)
    dt = t1_trim_heating1[1] - t1_trim_heating1[0]

    # late-time model
    entire = False
    return_all = True
    tlinear, kdet, bdet, t_tlinear0, dT_tlinear0 = sp_model_late_time_inv(delta_T1_trim_heating1,
                                                                    t1_trim_heating1, dt, qav, entire, return_all)
    tlog = np.log(t1_trim_heating1)
    dT_sp_synth = sp_model_late_time(qav, kdet, t_tlinear0, bdet)
    rmse_sp = compute_rmse(dT_tlinear0, dT_sp_synth)
    mb_sp = compute_mb(dT_tlinear0, dT_sp_synth)
    pd_sp = compute_percentage_diff(dT_tlinear0, dT_sp_synth)

    # signal processing model
    kdet_sig, bdet_sig, tth, dT2_h = sp_model_signal_inv(delta_T1_trim_heating1, t1_trim_heating1, dt, q,
                                                         output_model_array=True)
    bout, cout, dout = sp_model_nominal_get_b_c_d(delta_T1_trim_heating1, t1_trim_heating1, q, kdet_sig)
    print('SP output:')
    print('kdet = ', kdet)
    print('bout = ', bout)
    print('cout = ', cout)
    print('dout = ', dout)

    # synthetic signal processing for comparison
    dT_model_sig_synth = dT_sp_model_signal(qav, kdet_sig, tth, bdet_sig)

    rmse_sp_sig = compute_rmse(dT2_h, dT_model_sig_synth)
    mb_sp_sig = compute_mb(dT2_h, dT_model_sig_synth)
    pd_sp_sig = compute_percentage_diff(dT2_h, dT_model_sig_synth)

    fig = plt.figure(num=None, figsize=HSQUARE_FIGSIZE)
    is_scientific = True
    use_pd = True

    # (a)
    ax = fig.add_subplot(1, 2, 1)
    ax.plot(tlog, delta_T1_trim_heating1, color=MEASURED_COLOR, label='Measured')
    ax.plot(np.log(t_tlinear0), dT_sp_synth, color=MODELLED_COLOR, label='Modelled')
    ax.axvline(x=np.log(tlinear), linestyle=':', color=MODELLED_COLOR, label='Linear\nSection')
    place_rmse_mb_pd_on_plot(rmse_sp, mb_sp, pd_sp, 'K', 1, 'center left',
                             is_scientific,
                             use_pd)
    ax.legend(loc='best')
    ax.set_xlabel(r'$\mathrm{log}(t)$')
    ax.set_ylabel(create_label('$\Delta T$', 'K'))
    ylim = list(ax.get_ylim())
    ylim[1] += 0.40*(ylim[1]-ylim[0])
    ax.set_ylim(ylim)
    ax.set_title('(a)', loc='left')

    # (b)
    ax = fig.add_subplot(1, 2, 2)
    ax.plot(tth, dT2_h, color=MEASURED_COLOR, label='signal')
    ax.plot(tth, dT_model_sig_synth, color=MODELLED_COLOR, label='model')
    ax.set_xlabel('Time (s)')
    ax.set_ylabel(create_label('$\Delta \Gamma_{5} \hspace{0.3} (\hspace{0.3}t\hspace{0.3})$', ''))
    place_rmse_mb_pd_on_plot(rmse_sp_sig, mb_sp_sig, pd_sp_sig, 'K', 1, 'lower right',
                             is_scientific,
                             use_pd)
    ax.set_title('(b)', loc='left')

    plt.tight_layout()
    plt.savefig(FIG_PATH + fn + PLOT_EXTENSION)

    if show:
        plt.show()