## Assuming base pressure limited by water
m0 = 18.0 * constants.atomic_mass

mbead = bu.get_mbead(date)

rbead = bu.get_rbead(mbead)
Ibead = bu.get_Ibead(mbead)

print('Optical torque estimate: ', Ibead['val'] * 20.0e3 / 1500.0)

kappa = {}
kappa['val'] = 6.47e11
kappa['sterr'] = 0.06e11
kappa['syserr'] = 0.25e11

kappa_calc = bu.get_kappa(mbead)

#colors = bu.get_color_map(len(newpaths)*2 + 1, cmap='plasma')
colors = bu.get_color_map(len(newpaths), cmap='plasma')

two_point_times = []
two_point_estimates = []
two_point_errors = []
two_point_estimates_2 = []
two_point_errors_2 = []
linear_estimates = []
linear_errors = []

all_fits = []

for fileind, file in enumerate(newpaths):
# filename = os.path.join(base, 'libration_ringdown.p')
filename = os.path.join(base, 'sdeint_ringdown_manyp_3.p')

### load that data
results = pickle.load( open(filename, 'rb') )

### Subselect some results so the plot is comprehensible
# results_cut = results[:8:2]
results_cut = results

### Define some constants. Later simulations will have these saved with the
### data, but I forgot on the first few so....
mbead_dic = {'val': 84.3e-15, 'sterr': 1.0e-15, 'syserr': 1.5e-15}
Ibead = bu.get_Ibead(mbead=mbead_dic)['val']
kappa = bu.get_kappa(mbead=mbead_dic)['val']
m0 = 18.0 * constants.atomic_mass  # residual gas particle mass, in kg


### Define an exponential for fitting the rindown
def exponential(x, a, b, c):
    return a * np.exp(-1.0 * b * x) + c

# plt.figure(figsize=(10,8))

### Colors for plotting
colors = bu.get_color_map(len(results_cut), cmap='plasma')

# fig = plt.figure(figsize=(8,5))
### Loop over the simulation results and fit each one
for ind, result in enumerate(results_cut):
示例#3
0
def fit_kappa(proc_outdat_dict, mbead, plot_chi2=False):

    rbead = bu.get_rbead(mbead)

    mass_vec = proc_outdat_dict['mass_vec']
    mass_err_vec = proc_outdat_dict['mass_err_vec']
    pmax_norm_vec = proc_outdat_dict['pmax_norm_vec']
    pmax_norm_err_vec = proc_outdat_dict['pmax_norm_sterr_vec']

    pmax_norm_vec_upper = pmax_norm_vec + proc_outdat_dict['pmax_norm_syserr_vec']
    pmax_norm_vec_lower = pmax_norm_vec - proc_outdat_dict['pmax_norm_syserr_vec']

    popt3, pcov3 = opti.curve_fit(inverse_sqrt, mass_vec, pmax_norm_vec, [0.1 / 100, 0])

    def cost(param):
        resid = np.abs(inverse_sqrt(mass_vec, param, 0) - pmax_norm_vec)
        norm = 1. / (len(mass_vec) - 1)
        tot_var = pmax_norm_err_vec**2 + pmax_norm_vec**2 * (mass_err_vec / mass_vec)**2
        return norm * np.sum( resid**2 / tot_var)

    def cost_upper(param):
        resid = np.abs(inverse_sqrt(mass_vec, param, 0) - pmax_norm_vec_upper)
        norm = 1. / (len(mass_vec) - 1)
        tot_var = pmax_norm_err_vec**2 + pmax_norm_vec_upper**2 * (mass_err_vec / mass_vec)**2
        return norm * np.sum( resid**2 / tot_var)

    def cost_lower(param):
        resid = np.abs(inverse_sqrt(mass_vec, param, 0) - pmax_norm_vec_lower)
        norm = 1. / (len(mass_vec) - 1)
        tot_var = pmax_norm_err_vec**2 + pmax_norm_vec_lower**2 * (mass_err_vec / mass_vec)**2
        return norm * np.sum( resid**2 / tot_var)

    #param_arr = np.linspace(0.98*popt3[0], 1.02*popt3[0], 200)
    #param, param_err, min_chi = bu.minimize_nll(cost, param_arr, plot=plot_chi2)

    m=Minuit(cost,
             param = popt3[0], # set start parameter
             #fix_param = "True", # you can also fix it
             #limit_param = (0.0, 10000.0),
             errordef = 1,
             print_level = 0, 
             pedantic=False)
    m.migrad(ncall=500000)
    minos = m.minos()

    m_upper=Minuit(cost_upper,
                   param = popt3[0], # set start parameter
                   #fix_param = "True", # you can also fix it
                   #limit_param = (0.0, 10000.0),
                   errordef = 1,
                   print_level = 0, 
                   pedantic=False)
    m_upper.migrad(ncall=500000)

    m_lower=Minuit(cost_lower,
                   param = popt3[0], # set start parameter
                   #fix_param = "True", # you can also fix it
                   #limit_param = (0.0, 10000.0),
                   errordef = 1,
                   print_level = 0, 
                   pedantic=False)
    m_lower.migrad(ncall=500000)

    param = {}
    param['val'] = minos['param']['min']
    param['sterr'] = np.mean(np.abs([minos['param']['upper'], minos['param']['lower']]))

    param['syserr'] = np.mean(np.abs(m.values['param'] - \
                                np.array([m_upper.values['param'], m_lower.values['param']])))

    min_chi = m.fval

    # convert from units of mbar * amu^1/2 / (e * um) to Pa * kg^1/2 / (C * m)
    conv_fac = (100.0) * np.sqrt(1.6605e-27) * (1.0 / dipole_units)
    param_si = param['val'] * conv_fac
    param_si_sterr = param['sterr'] * conv_fac
    param_si_syserr = param['syserr'] * conv_fac

    rot_freq = np.mean(proc_outdat_dict['rot_freq_vec'])
    rot_amp =  np.mean(proc_outdat_dict['rot_amp_vec'])

    kappa = {}
    kappa['val'] = param_si * 2.0 * np.pi * rot_freq / rot_amp

    #print mass_vec
    #print rot_freq_err_vec

    #print param_si_err / param_si
    #print np.median(rot_freq_err_vec) / rot_freq
    #print np.median(rot_amp_err_vec) / rot_amp

    kappa['sterr'] = kappa['val'] * np.sqrt( (np.max(proc_outdat_dict['rot_freq_err_vec']) / rot_freq)**2 + \
                                 (np.max(proc_outdat_dict['rot_amp_err_vec']) / rot_amp)**2 + \
                                 (param_si_sterr / param_si)**2 )
    kappa['syserr'] = kappa['val'] * np.sqrt( (param_si_syserr / param_si)**2 )


    sigma = {}
    sigma['val'] = (1.0 / (kappa['val'] * rbead['val']**4)) * \
                        np.sqrt( (9.0 * kb * T) / (32.0 * np.pi) )
    sigma['sterr'] = sigma['val'] * np.sqrt( (kappa['sterr']/kappa['val'])**2 + \
                                    16.0 * (rbead['sterr']/rbead['val'])**2 )
    sigma['syserr'] = sigma['val'] * np.sqrt( (kappa['syserr']/kappa['val'])**2 + \
                                    16.0 * (rbead['syserr']/rbead['val'])**2 )

    kappa_calc = bu.get_kappa(mbead, verbose=True)

    sigma2 = {}
    sigma2['val'] = kappa_calc['val'] / kappa['val']
    sigma2['sterr'] = sigma2['val'] * np.sqrt( (kappa['sterr']/kappa['val'])**2 + \
                                    (kappa_calc['sterr']/kappa_calc['val'])**2 )
    sigma2['syserr'] = sigma2['val'] * np.sqrt( (kappa['syserr']/kappa['val'])**2 + \
                                    (kappa_calc['syserr']/kappa_calc['val'])**2 )



    print()
    print('Kappa (meas) : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(kappa['val'], kappa['sterr'], kappa['syserr']))
    print('Kappa (calc) : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(kappa_calc['val'], kappa_calc['sterr'], kappa_calc['syserr']))
    print('Rbead        : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(rbead['val'], rbead['sterr'], rbead['syserr']))
    print('mbead        : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(mbead['val'], mbead['sterr'], mbead['syserr']))
    print('sigma        : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(sigma['val'], sigma['sterr'], sigma['syserr']))
    print('sigma2       : {:0.4g} +- {:0.4g} (st) +- {:0.4g} (sys)'\
                        .format(sigma2['val'], sigma2['sterr'], sigma2['syserr']))
    print('min chi: {:0.2g}'.format(min_chi))

    return {'kappa': kappa, \
            'kappa_calc': kappa_calc, \
            'sigma': sigma, \
            'param': param}