示例#1
0
def getGx(x_data, i0, params, dr):
    N_slice = math.ceil(params['R']/dr)
    y = np.zeros(N_slice)
    for i in range(params['Ntot']):
        if(i != i0):
            slice_i = math.floor(my.length(my.shiftCrd(x_data[i], params['R']))/dr)
            if(slice_i < N_slice):
                y[slice_i] += 1
            
    k = 1/(params['Ntot']/math.pow(2*params['R'], 3)*4*math.pi*dr)
    return [y[_i]*k/((_i+1)*dr) for _i in range(N_slice)]
示例#2
0
def main():
    args = sys.argv[1:]
    argc_min = 1
    if len(args) < argc_min:
        print('usage: ./diffusion.py model_name [keys, N0, N1, Nprt]')
        sys.exit(1)

    # --------------------------------- std start ------------------------------------------
    model_name, keys, graph_dir, time_gaps_str, N0, N1, Nfrm, E, Tmp, Tmp_av, t, stabTind, params = my.std_start(
        args, 0, 1, 2, 3)
    # std_start(args, model_i, N0_i, N1_i):
    # model_name, keys, graph_dir, time_gaps_str, N0, N1, Nfrm, E, Tmp, Tmp_av, t, stabTind, params

    # --------------------------------- input handle ------------------------------------------
    if (Nfrm < 10):
        print('Too few grames (Nfrm < 10)')
        return
    if (len(args) < 4):
        Nprt = params['Ntot']
    else:
        Nprt = int(args[3])

    draw_on_screen = my.find_key(keys, 'keep')
    pnrt_progress = (my.find_key(keys, 'percent')
                     or my.find_key(keys, 'prcnt'))
    if (not draw_on_screen):
        plt.switch_backend('Agg')
    make_pics = (my.find_key(keys, 'pics') or draw_on_screen)

    # --------------------------------- theory prediction ------------------------------------------
    n0 = params['Ntot'] / math.pow(2 * params['R'], 3)
    r_th0 = 1
    lmd_th0 = 1 / (n0 * math.pi * r_th0**2 * math.sqrt(2))

    r_th1 = r_th0 * my.r_th1(Tmp_av, n0)
    lmd_th1 = lmd_th0 * (r_th0 / r_th1)**2

    #r_th2 = pow(2, 1/6) * math.sqrt(1 + 2/(3*Tmp_av))
    r_th2 = r_th0 * my.r_th2(Tmp_av)
    lmd_th2 = lmd_th0 * (r_th0 / r_th2)**2

    k_th = 4 * lmd_th1 * math.sqrt(2 * Tmp_av / (math.pi * params['mu']))
    c_th = params['mu'] / (6 * Tmp_av)

    # --------------------------------- experimant data ------------------------------------------
    r2 = []
    dr = [0, 0, 0]
    cubeSize = 2 * params['R']
    cubeSzEff = params['R']
    x_cm = []
    r_cm = []
    v_cm = []
    for n in range(N0, N1):
        x, v, m = my.read_frame(model_name, n)

        if (my.find_key(keys, 'vcm')):
            v_cm.append(my.find_cm(v, m))
        else:
            if (n == N0):
                # set start conditions
                x0 = cp.deepcopy(x)
                x_real = cp.deepcopy(x)
                x_prev = cp.deepcopy(x)
                r2.append(0)

                v_start_av = np.mean([my.length(_x) for _x in v])
            else:
                r2_sum = 0
                for i in range(Nprt):  # find <r^2> considering coords's shifts
                    for j in range(3):
                        dx = x[i][j] - x_prev[i][j]
                        if (dx > cubeSzEff):  # re-shift coords
                            dx -= cubeSize
                        elif (dx < -cubeSzEff):
                            dx += cubeSize
                        x_real[i][j] += dx
                        # find real displacement
                        dr[j] = x_real[i][j] - x0[i][j]
                    r2_sum += my.dot(dr, dr)
                r2.append(r2_sum / Nprt)
                x_prev = cp.deepcopy(x)
            x_cm.append(my.find_cm(x_real, m))
            r_cm.append(my.length(x_cm[-1]))

        if (pnrt_progress):
            print('diffusion progress: ' + str(
                (n + 1 - N0) / Nfrm * 100) + '%                     \r',
                  end='')

    if (not my.find_key(keys, 'vcm')):
        # --------------------------------- experimant analisys ------------------------------------------
        # find proper tg
        r1 = my.arrFnc(r2, math.sqrt)
        big_time = 3
        small_time = 0.5
        approx_r2 = np.poly1d(np.polyfit(t[N0:N1], r2, 1))
        k_exp = approx_r2.c[0]
        tau = c_th * k_exp
        c2 = int(tau * params['dumpDT'] * big_time)
        if (c2 > Nfrm - 10):
            c2 = Nfrm - 10
        if (c2 < 10):
            c2 = int(Nfrm / 2)
        approx_r2 = np.poly1d(np.polyfit(t[(N0 + c2):N1], r2[c2:], 1))
        k_exp = abs(approx_r2.c[0])
        tau = c_th * k_exp
        c2 = int(tau * params['dumpDT'] * big_time)
        if (c2 > Nfrm - 5):
            c2 = Nfrm - 5
        if (c2 < 10):
            c2 = int(Nfrm / 2)
        c1 = int(tau * params['dumpDT'] * small_time)
        if (c1 < 3):
            c1 = 3

        # in case of strange emergencies
        bad_k_exp = (k_exp < my.myeps)
        if (bad_k_exp):
            if (k_exp < 0):
                k_exp = -k_exp
            if (k_exp < my.myeps):
                k_exp = my.myeps

        xa0 = abs(approx_r2(0))
        #tau = xa0/k_exp

        c_exp = xa0 / k_exp**2

        x = my.arrFnc(t[(N0 + c2):N1], math.log)
        y = my.arrFnc(r2[c2:], math.log)
        logEnd_approx = np.poly1d(np.polyfit(x, y, 1))
        x = my.arrFnc(t[N0 + 1:c1], math.log)
        y = my.arrFnc(r2[1:c1], math.log)
        logBeg_approx = np.poly1d(np.polyfit(x, y, 1))
        x = t[N0 + 1:c1]
        y = my.arrFnc(r2[1:c1], math.sqrt)
        approx_r1 = np.poly1d(np.polyfit(x, y, 1))

        #lmd_1 = math.sqrt(3*math.pi*xa0)/4
        lmd_1 = my.lmd_1(xa0)
        #lmd_2 = k_exp/4 * math.sqrt(math.pi*params['mu'] / (Tmp_av*2))
        lmd_2 = my.lmd_2(k_exp, params['mu'], Tmp_av)

        lmd_3 = my.lmd_3(logBeg_approx.c[0], logBeg_approx.c[1],
                         logEnd_approx.c[0], logEnd_approx.c[1], params['mu'],
                         Tmp_av)  # tau -> lmd
        lmd_4 = my.lmd_4(logBeg_approx.c[0], logBeg_approx.c[1],
                         logEnd_approx.c[0],
                         logEnd_approx.c[1])  # straight lmd

    if (make_pics):
        fig_c = -1
        figs = []

        if (not my.find_key(keys, 'vcm')):
            # -------------------------------- r_cm(t) -----------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            cm = []
            std_cm = []
            x = t[N0:N1]
            t**s = ['x', 'y', 'z']
            for _j in range(3):
                cm.append([x_cm[_i][_j] - x_cm[0][_j] for _i in range(Nfrm)])
                std_cm.append(np.std(cm[_j]))
                plt.plot(
                    x,
                    cm[_j],
                    '-',
                    label=t**s[_j] + '; std = ' +
                    str(my.str_sgn_round(std_cm[_j], 3)))  # experiment data
            x = [t[N0], t[N1 - 1]]
            plt.plot(x, [0, 0], '--')
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
            plt.xlabel('t')
            plt.ylabel('xyz_cm')
            plt.grid(True)
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                       loc=3,
                       ncol=3,
                       mode="expand",
                       borderaxespad=0.)
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir,
                                'xyz_cm(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- r2(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            plt.plot(t[N0:N1], r2, '-')  # experiment data

            x = [t[N0 + 1], t[N1 - 1]]
            y = [approx_r2(_x) for _x in x]
            plt.plot(x, y, '--')  # line approximation

            x = t[N0:N1]
            #y = [xa0*(_x/tau - 1 + math.exp(- _x/tau)) for _x in x]
            r2_th = [
                k_exp * (_x - tau * (1 - math.exp(-_x / tau))) for _x in x
            ]
            plt.plot(x, r2_th, '--')  # my approximation

            plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
            plt.xlabel('t')
            plt.ylabel('<r^2>')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('r^2(t)')
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r2(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- r2_err(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))

            x = t[N0:N1]
            y = [r2[_i] - r2_th[_i] for _i in range(len(x))]
            plt.plot(x, y, '-')  # experiment data
            plt.plot([t[N0 + 1], t[N1 - 1]], [0, 0], '--')

            plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
            plt.xlabel('t')
            plt.ylabel('<r^2> - <r^2>_th')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('<r^2> error')
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir,
                                'r2_err(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- r2_err_rel(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))

            x = t[N0:N1]
            y = [my.rel_err(r2[_i], r2_th[_i]) for _i in range(len(x))]
            plt.plot(x, y, '-')  # experiment data
            plt.plot([t[N0 + 1], t[N1 - 1]], [0, 0], '--')

            plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
            plt.xlabel('t')
            plt.ylabel('err')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('|<r^2> / <r^2>_th - 1|')
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir,
                                'r2_err_rel(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- log(r2)(log(t)) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            plt.plot(t[N0:N1], r2, '-')  # experiment data

            x = [t[N0 + 1], t[N0 + c2]]
            y = [math.exp(logBeg_approx(math.log(_x))) for _x in x]
            plt.plot(x, y, '--')  # beginning approximation

            x = [t[N0 + c1], t[N1 - 1]]
            y = [math.exp(logEnd_approx(math.log(_x))) for _x in x]
            plt.plot(x, y, '--')  # ending approximation

            x = t[N0:N1]
            plt.plot(x, r2_th, '--')  # my approximation

            plt.xlabel('t')
            plt.ylabel('<r^2>')
            plt.grid(True)
            plt.xscale('log')
            plt.yscale('log')
            plt.title('log(r2) | k_begin = ' +
                      str(my.str_sgn_round(logBeg_approx.c[0], 3)) +
                      '; k_end = ' +
                      str(my.str_sgn_round(logEnd_approx.c[0], 3)))
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir,
                                'r2(t)_loglog_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- r(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            plt.plot(t[N0:N1], r1, '-')  # experiment data

            x = [t[N0 + 1], t[c2]]
            y = [approx_r1(_x) for _x in x]
            plt.plot(x, y, '--')  # beginning approximation

            x = t[N0:N1]
            y = my.arrFnc(r2_th, math.sqrt)
            plt.plot(x, y, '--')  # my approximation

            plt.xlabel('t')
            plt.ylabel('sqrt(<r^2>)')
            plt.grid(True)
            #plt.title('sqrt(r2) | l_th = ' + str(my.str_sgn_round(lmd_th1,3)) + '; l_3 = ' + str(my.str_sgn_round(lmd_3,3)) + '; l_2 = ' + str(my.str_sgn_round(lmd_2,3)))
            plt.title('sqrt(<r^2>)')
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r1(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

        # -------------------------------- v_cm(t) -----------------------------------------
        fig_c += 1
        figs.append(plt.figure(fig_c))
        x = t[N0:N1]
        dv_cm = []
        std_dvcm = []
        t**s = ['vx', 'vy', 'vz']
        for _j in range(3):
            v_av = np.mean(v_cm[:][_j])
            dv_cm.append([v_cm[_i][_j] - v_av for _i in range(Nfrm)])
            std_dvcm.append(np.std(dv_cm[_j]))
            plt.plot(x,
                     dv_cm[_j],
                     '-',
                     label=t**s[_j] + '; std = ' +
                     str(my.str_sgn_round(std_dvcm[_j], 3)))  # experiment data
        x = [t[N0], t[N1 - 1]]
        plt.plot(x, [0, 0], '--')
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
        plt.xlabel('t')
        plt.ylabel('v_cm')
        plt.grid(True)
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                   loc=3,
                   ncol=3,
                   mode="expand",
                   borderaxespad=0.)
        if (draw_on_screen):
            figs[fig_c].show()
        path = os.path.join(graph_dir, 'v_cm(t)_' + time_gaps_str + '.png')
        figs[fig_c].savefig(path)

    if (not my.find_key(keys, 'vcm')):
        print(n0, Tmp_av, lmd_th0, lmd_th1, lmd_th2, lmd_1, lmd_2, lmd_3,
              lmd_4)

    if (draw_on_screen):
        #print(c_th, c_exp, my.rel_err(c_th, c_exp))
        input()
示例#3
0
def main():
    args = sys.argv[1:]
    argc_min = 1
    if len(args) < argc_min:
        print('usage:\npython diffusion.py model_name [keys, N0, N1, Nprt]')
        sys.exit(1)
        
    # --------------------------------- std start ------------------------------------------
    model_name, keys, model_dir, graph_dir, time_gaps_str, N0, N1, Nfrm, E, P, Tmp, Tmp_av, t, stabTind, params = my.std_start(args, 0, 1, 2, 3)
    # std_start(args, model_i, N0_i, N1_i):
    # model_name, keys, graph_dir, time_gaps_str, N0, N1, Nfrm, E, Tmp, Tmp_av, t, stabTind, params
            
    # --------------------------------- input handle ------------------------------------------
    if(Nfrm < 10):
        print('Too few grames (Nfrm < 10)')
        return
    if(len(args) < 5):
        Nprt = params['Ntot']
    else:
        Nprt = int(args[4])
                
    draw_on_screen = my.find_key(keys, 'keep')
    pnrt_progress = (my.find_key(keys, 'percent') or my.find_key(keys, 'prcnt'))
    if(not draw_on_screen):
        plt.switch_backend('Agg') 
    make_pics = (my.find_key(keys, 'pics') or draw_on_screen)
    
    # --------------------------------- theory prediction ------------------------------------------
    n0 = params['n']
    r_th0 = 1
    lmd_th0 = 1/(n0 * math.pi * r_th0**2 * math.sqrt(2))
            
    r_th1 = r_th0 * my.r_th1(Tmp_av, n0)
    lmd_th1 = lmd_th0 * (r_th0/r_th1)**2
    
    #r_th2 = pow(2, 1/6) * math.sqrt(1 + 2/(3*Tmp_av))
    r_th2 = r_th0 * my.r_th2(Tmp_av)
    lmd_th2 = lmd_th0 * (r_th0/r_th2)**2
    
    k_th = 4*lmd_th1 * math.sqrt(2*Tmp_av / math.pi)
    c_th = 1 / (6*Tmp_av)
    
    # --------------------------------- experimant data ------------------------------------------
    diff_file_path = os.path.join(model_dir, my.diff_filename)
    if(os.path.isfile(diff_file_path) and (not my.find_key(keys, 'recomp'))):
        data = np.loadtxt(diff_file_path)
        #data = np.delete(data, (0), axis=0)
        #data = np.delete(data, (0), axis=1)
        r2 = cp.deepcopy(data[0])
        l = len(r2);
        x_cm = np.zeros((l, 3))
        v_cm = np.zeros((l, 3))
        for i in range(l):
            for j in range(3):
                x_cm[i][j] = data[j+1][i]
                v_cm[i][j] = data[j+4][i]
        data = None;
    else:
        r2 = []
        dr = [0,0,0]
        cubeSize = 2*params['R']
        cubeSzEff = params['R']
        x_cm = []
        #r_cm = []
        v_cm = []
        for n in range(N0,N1):
            x,v = my.read_frame(model_name, n)
            m = np.ones(params['Ntot'])
            
            if(n == N0):
                # set start conditions
                x0 = cp.deepcopy(x)
                x_real = cp.deepcopy(x)
                r2.append(0)
                
                v_start_av = np.mean([my.length(_x) for _x in v])
            else:
                r2_sum = 0
                for i in range(Nprt): # find <r^2> considering coords's shifts
                    for j in range(3): 
                        dr[j] = x[i][j]-x0[i][j]
                    r2_sum += my.dot(dr,dr)
                r2.append(r2_sum/Nprt)
            x_cm.append(my.find_cm(x, m))
            v_cm.append(my.find_cm(v,m))
                                
            if(pnrt_progress):
                print('diffusion progress: ' + str((n + 1 - N0) / Nfrm * 100) + '%                     \r', end='')
                
        xx = [_x[0] for _x in x_cm]
        xy = [_x[1] for _x in x_cm]
        xz = [_x[2] for _x in x_cm]
        vx = [_x[0] for _x in v_cm]
        vy = [_x[1] for _x in v_cm]
        vz = [_x[2] for _x in v_cm]        
        np.savetxt(diff_file_path, (r2, xx, xy, xz, vx, vy, vz))
                
    # --------------------------------- experimant analisys ------------------------------------------        
    # find proper tg
    r1 = my.arrFnc(r2, math.sqrt)
    big_time = 3
    small_time = 0.5
    approx_r2 = np.poly1d(np.polyfit(t[N0:N1], r2, 1))
    k_exp = approx_r2.c[0]
    tau = c_th*k_exp
    c2 = int(tau*params['dumpDT'] * big_time)
    if(c2 > Nfrm-10):
        c2 = Nfrm-10
    if(c2 < 10):
        c2 = int(Nfrm/2)
    approx_r2 = np.poly1d(np.polyfit(t[(N0+c2):N1], r2[c2:], 1))
    k_exp = abs(approx_r2.c[0])
    tau = c_th * k_exp
    c2 = int(tau*params['dumpDT'] * big_time)
    if(c2 > Nfrm - 5):
        c2 = Nfrm - 5
    if(c2 < 10):
        c2 = int(Nfrm/2)
    c1 = int(tau*params['dumpDT'] * small_time)
    if(c1 < 3):
        c1 = 3
                
    # in case of strange emergencies
    bad_k_exp = (k_exp < my.myeps)
    if(bad_k_exp):
        if(k_exp < 0):
            k_exp = -k_exp
        if(k_exp < my.myeps):
            k_exp = my.myeps
            
    xa0 = abs(approx_r2(0))
    tau = xa0/k_exp
            
    c_exp = xa0 / k_exp**2
    B = (1 - r2[-2] / (k_exp * t[N1-1])) * math.sqrt(t[N1-1])
    subdiff_c = int(round(B*B*params['dumpDT'] + 1.01))
        
    x = my.arrFnc(t[(N0+c2):N1], math.log)
    y = my.arrFnc(r2[c2:], math.log)
    logEnd_approx = np.poly1d(np.polyfit(x, y, 1))
    x = my.arrFnc(t[N0:c1], math.log)
    y = my.arrFnc(r2[1:c1], math.log)
    logBeg_approx = np.poly1d(np.polyfit(x, y, 1))
    x = t[N0:c1]
    y = my.arrFnc(r2[1:c1], math.sqrt)
    approx_r1 = np.poly1d(np.polyfit(x, y, 1))
    
    #lmd_1 = math.sqrt(3*math.pi*xa0)/4
    lmd_1 = my.lmd_1(xa0)
    #lmd_2 = k_exp/4 * math.sqrt(math.pi / (Tmp_av*2))
    lmd_2 = my.lmd_2(k_exp, 1, Tmp_av)
    D_Einst = lmd_2 * math.sqrt(8 * Tmp_av / math.pi) / 3
    
    selfconsist = my.rel_err(lmd_1, lmd_2)
    
    lmd_3 = my.lmd_3(logBeg_approx.c[0], logBeg_approx.c[1], logEnd_approx.c[0], logEnd_approx.c[1], 1, Tmp_av) # tau -> lmd
    lmd_4 = my.lmd_4(logBeg_approx.c[0], logBeg_approx.c[1], logEnd_approx.c[0], logEnd_approx.c[1]) # straight lmd

    if(make_pics):
        fig_c = -1
        figs = []
        
        if(my.find_key(keys, 'r_cm')):
            # -------------------------------- r_cm(t) -----------------------------------------        
            fig_c += 1
            figs.append(plt.figure(fig_c))
            cm = []
            std_cm = []
            x = t[N0:N1]
            t**s = ['x', 'y', 'z']
            for _j in range(3):
                cm.append([x_cm[_i][_j]-x_cm[0][_j] for _i in range(Nfrm)])
                std_cm.append(np.std(cm[_j]))
                plt.plot(x, cm[_j], '-', label = t**s[_j] + '; std = ' + str(my.str_sgn_round(std_cm[_j],3))) # experiment data
            x = [t[N0], t[N1-1]]
            plt.plot(x, [0,0], '--')
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
            plt.xlabel('t')
            plt.ylabel('xyz_cm')
            plt.grid(True)
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                       ncol=3, mode="expand", borderaxespad=0.)        
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'xyz_cm(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)    
                            
        if(my.find_key(keys, 'r2')):
            # -------------------------------- r2(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            
            #tau = 1
            x = t[N0:N1]
            x_draw = [_x/tau for _x in x]
            plt.plot(x_draw, r2, '-', label="exp") # experiment data
            
            x = t[N0:N1]
            r2_th = [k_exp*tau*(_x/tau - 1 + math.exp(- _x/tau)) for _x in x]
            x_draw = [_x/tau for _x in x]
            plt.plot(x_draw, r2_th, '--', label="my_th")  # my approximation            
            
            if(my.find_key(keys, 'subdiff')):
                x = t[(N0 + subdiff_c):N1]
                subdiff_app = [k_exp*_x*(1 - B/math.sqrt(_x)) for _x in x]
                x_draw = [_x/tau for _x in x]
                plt.plot(x_draw, subdiff_app, '-.', label="subdiff") # subdiff approximation
                                    
            x = [t[N0], t[N1-1]]
            y = [approx_r2(_x) for _x in x]
            x_draw = [_x/tau for _x in x]
            plt.plot(x_draw, y, '--', label="line asimp") # line approximation            
            
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
            plt.xlabel('t/tau')
            plt.ylabel('<r^2>')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('r^2(t) | tau = ' + my.str_sgn_round(tau,3) + ' | B = ' + my.str_sgn_round(B,3))
            plt.legend()
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r2(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)
            
            # -------------------------------- log(r2)(log(t)) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            
            x = t[N0:N1]
            x_draw = [_x/tau for _x in x]
            plt.plot(x_draw, r2, '-', label="exp") # experiment data
                        
            x = [_x/tau for _x in t[N0:N1]]
            plt.plot(x, r2_th, '--', label="my_th") # my approximation         
            
            if(my.find_key(keys, 'subdiff')):
                x = t[(N0 + subdiff_c):N1]
                x_draw = [_x/tau for _x in x]            
                plt.plot(x_draw, subdiff_app, '--', label="subdiff") # subdiff approximation
            
            x = [t[N0], t[N0+c2]]
            y = [math.exp(logBeg_approx(math.log(_x))) for _x in x]
            x_draw = [_x/tau for _x in x]
            plt.plot(x_draw, y, '--', label="ballistic") # beginning approximation        
            
            x = [t[N0+c1], t[N1-1]]
            y = [math.exp(logEnd_approx(math.log(_x))) for _x in x]
            x_draw = [_x/tau for _x in x]            
            plt.plot(x_draw, y, '--', label="linear") # ending approximation            
                    
            plt.xlabel('t/tau')
            plt.ylabel('<r^2>')
            plt.grid(True)
            plt.xscale('log')
            plt.yscale('log')
            plt.title('log(r2) | k_begin = ' + str(my.str_sgn_round(logBeg_approx.c[0],3)) + '; k_end = ' + str(my.str_sgn_round(logEnd_approx.c[0],3)))
            plt.legend()
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r2(t)_loglog_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)
            
            # -------------------------------- r(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))        
            plt.plot(t[N0:N1], r1, '-', label="exp") # experiment data
                    
            x = t[N0:N1]
            y = my.arrFnc(r2_th, math.sqrt)
            plt.plot(x, y, '--', label="my_th") # my approximation 
            
            if(my.find_key(keys, 'subdiff')):
                x = t[(N0 + subdiff_c):N1]
                y = my.arrFnc(subdiff_app, math.sqrt)
                plt.plot(x, y, '--', label="subdiff") # sudiff approximation             
            
            x = [t[N0], t[c2]]
            y = [approx_r1(_x) for _x in x]
            plt.plot(x, y, '--', label="ballistic") # beginning approximation             
                
            plt.xlabel('t')
            plt.ylabel('sqrt(<r^2>)')
            plt.grid(True)
            #plt.title('sqrt(r2) | l_th = ' + str(my.str_sgn_round(lmd_th1,3)) + '; l_3 = ' + str(my.str_sgn_round(lmd_3,3)) + '; l_2 = ' + str(my.str_sgn_round(lmd_2,3)))
            plt.title('sqrt(<r^2>)')
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r1(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)    
                            
        if(my.find_key(keys, 'r2_err')):
            # -------------------------------- r2_err(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            
            x = t[N0:N1]
            y = [r2[_i] - r2_th[_i] for _i in range(len(r2_th))]
            plt.plot(x, y, '-') # my approximation            

            if(my.find_key(keys, 'subdiff')):
                x = t[(N0 + subdiff_c):N1]
                y = [r2[_i + subdiff_c] - subdiff_app[_i] for _i in range(0, N1 - N0 - subdiff_c)]
                plt.plot(x, y, '--') # subdiff approximation                        
            
            plt.plot([t[N0+1], t[N1-1]], [0, 0], '--')
            
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
            plt.xlabel('t')
            plt.ylabel('<r^2> - <r^2>_th')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('<r^2> error')
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r2_err(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)        
            
            # -------------------------------- r2_err_rel(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            
            tmp = int(round(tau*5*params['dumpDT']))
            x = t[(N0 + subdiff_c + tmp):N1]
            y = [my.rel_err(r2[_i], r2_th[_i]) for _i in range(tmp, N1 - N0 - subdiff_c)]
            plt.plot(x, y, '-') # my approximation
            
            if(my.find_key(keys, 'subdiff')):
                x = t[(N0 + subdiff_c + tmp):N1]
                y = [my.rel_err(r2[_i + subdiff_c],subdiff_app[_i]) for _i in range(tmp, N1 - N0 - subdiff_c)]
                plt.plot(x, y, '-.') # subdiff approximation                        
            
            plt.plot([t[N0+1], t[N1-1]], [0, 0], '--')
            
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
            plt.xlabel('t')
            plt.ylabel('err')
            plt.grid(True)
            #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
            plt.title('|<r^2> / <r^2>_th - 1|')
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r2_err_rel(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)                
    
            
        if(my.find_key(keys, 'v_cm')):
            # -------------------------------- v_cm(t) -----------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            x = t[N0:N1]
            dv_cm = []
            std_dvcm = []
            t**s = ['vx', 'vy', 'vz']        
            for _j in range(3):
                v_av = np.mean(v_cm[:][_j])
                dv_cm.append([v_cm[_i][_j] - v_av for _i in range(Nfrm)])
                std_dvcm.append(np.std(dv_cm[_j]))
                plt.plot(x, dv_cm[_j], '-', label = t**s[_j] + '; std = ' + str(my.str_sgn_round(std_dvcm[_j],3))) # experiment data
            x = [t[N0], t[N1-1]]
            plt.plot(x, [0,0], '--')
            plt.ticklabel_format(style='sci', axis='y', scilimits=(0,0))
            plt.ticklabel_format(style='sci', axis='x', scilimits=(0,0))
            plt.xlabel('t')
            plt.ylabel('v_cm')
            plt.grid(True)
            plt.legend(bbox_to_anchor=(0., 1.02, 1., .102), loc=3,
                    ncol=3, mode="expand", borderaxespad=0.)        
            if(draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'v_cm(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)    
            
        if(my.find_key(keys, 'human')):
            print('n = ', n0, '; T = ', Tmp_av)
            print('lmd_0 = ', lmd_th0, '; lmd_T<1 = ', lmd_th1, '; lmd_T>1 = ', lmd_th2)
            print('lmd_a = ', lmd_1, '; lmd_tg = ', lmd_2, '; lmd_exp_intercept = ', lmd_3, '; lmd_tau_intercept = ', lmd_4)
            print('inconsistency = ', selfconsist)
        else:
            print(n0, Tmp_av, lmd_th0, lmd_th1, lmd_th2, lmd_1, lmd_2, lmd_3, lmd_4)
        #print('D_Einst = ' + str(D_Einst))
        
    if(draw_on_screen):
        #print(c_th, c_exp, my.rel_err(c_th, c_exp))
        input()
示例#4
0
def main():
    args = sys.argv[1:]
    argc_min = 1
    if len(args) < argc_min:
        print('usage: ./diffusion.py model_name [keys, N0, N1, Nprt]')
        sys.exit(1)

    model_name, keys, graph_dir, time_gaps_str, N0, N1, Nfrm, E, Tmp, Tmp_av, t, stabTind, params = my.std_start(
        args, 0, 1, 2, 3)
    # std_start(args, model_i, N0_i, N1_i):
    # model_name, keys, graph_dir, time_gaps_str, N0, N1, Nfrm, E, Tmp, Tmp_av, t, stabTind, params

    if (len(args) < 4):
        Nprt = params['Ntot']
    else:
        Nprt = int(args[3])

    if (len(args) == 1):
        full_mode = 1
    else:
        full_mode = (my.find_key(keys, 'full_mode')
                     or my.find_key(keys, 'full'))
    if (full_mode and (Nfrm < int(Nfrm / 2) + 5)):
        print('too few frames for full_mode')
        sys.exit(1)

    draw_on_screen = my.find_key(keys, 'keep')
    if (not draw_on_screen):
        plt.switch_backend('Agg')
    make_pics = (my.find_key(keys, 'pics') or draw_on_screen)

    n0 = params['Ntot'] / math.pow(2 * params['R'], 3)
    sgm_th0 = 1
    lmd_th0 = 1 / (n0 * math.pi * math.sqrt(2))

    sgm_th2 = pow(2, 1 / 6) * math.sqrt(1 + 2 / (3 * Tmp_av))
    lmd_th2 = lmd_th0 / (sgm_th2 * sgm_th2)

    sgm_th1 = math.pow(
        (1 + math.sqrt(1 + 1.5 * Tmp_av + ((2 * n0)**2) * (n0**2 - 1))) / 2,
        -1 / 6)
    lmd_th1 = lmd_th0 / (sgm_th1 * sgm_th1)
    k_th = 4 * lmd_th1 * math.sqrt(2 * Tmp_av / (math.pi * params['mu']))

    r2 = []
    dr = [0, 0, 0]
    cubeSize = 2 * params['R']
    cubeSzEff = params['R']
    x_cm = []
    r_cm = []
    for n in range(N0, N1):
        x, v, m = my.read_frame(model_name, n)

        if (n == N0):
            # set start conditions
            x0 = cp.deepcopy(x)
            x_real = cp.deepcopy(x)
            x_prev = cp.deepcopy(x)
            r2.append(0)

            v_start_av = np.mean([my.length(_x) for _x in v])
        else:
            r2_sum = 0
            for i in range(Nprt):  # find <r^2> considering coords's shifts
                for j in range(3):
                    dx = x[i][j] - x_prev[i][j]
                    if (dx > cubeSzEff):  # re-shift coords
                        dx -= cubeSize
                    elif (dx < -cubeSzEff):
                        dx += cubeSize
                    x_real[i][j] += dx
                    # find real displacement
                    dr[j] = x_real[i][j] - x0[i][j]
                r2_sum += my.dot(dr, dr)
            r2.append(r2_sum / Nprt)
            x_prev = cp.deepcopy(x)
        x_cm.append(my.find_cm(x_real, m))
        r_cm.append(my.length(x_cm[-1]))

        if (draw_on_screen):
            print('diffusion progress: ' + str(
                (n + 1 - N0) / Nfrm * 100) + '%                     \r',
                  end='')

    fig_c = -1
    figs = []

    # -------------------------------- r_cm(t) ------------------------------------------
    if (make_pics):
        fig_c += 1
        figs.append(plt.figure(fig_c))
        cm = []
        std_cm = []
        x = t[N0:N1]
        t**s = ['x', 'y', 'z']
        for _j in range(3):
            cm.append([x_cm[_i][_j] - x_cm[0][_j] for _i in range(Nfrm)])
            std_cm.append(np.std(cm[_j]))
            plt.plot(x,
                     cm[_j],
                     '-',
                     label=t**s[_j] + '; std = ' +
                     str(my.str_sgn_round(std_cm[_j], 3)))  # experiment data
        x = [t[N0], t[N1 - 1]]
        plt.plot(x, [0, 0], '--')
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
        plt.xlabel('t')
        plt.ylabel('xyz_cm')
        plt.grid(True)
        plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
                   loc=3,
                   ncol=3,
                   mode="expand",
                   borderaxespad=0.)
        if (draw_on_screen):
            figs[fig_c].show()
        path = os.path.join(graph_dir, 'xyz_cm(t)_' + time_gaps_str + '.png')
        figs[fig_c].savefig(path)

    c2 = int(Nfrm / 2)
    if (full_mode):
        c1 = int(Nfrm / 4)
        c3 = int(Nfrm / 100)
        c4 = int(Nfrm / 8)
        x = my.arrFnc(t[(N0 + c2):(N1 - c4)], math.log)
        y = my.arrFnc(r2[c2:-c4], math.log)
        logEnd_approx = np.poly1d(np.polyfit(x, y, 1))
        x = my.arrFnc(t[N0 + 1:c3], math.log)
        y = my.arrFnc(r2[1:c3], math.log)
        logBeg_approx = np.poly1d(np.polyfit(x, y, 1))
        x = t[N0 + 1:c3]
        y = my.arrFnc(r2[1:c3], math.sqrt)
        approx_r1 = np.poly1d(np.polyfit(x, y, 1))
    else:
        c1 = 0
    approx_r2 = np.poly1d(np.polyfit(t[(N0 + c1):N1], r2[c1:], 1))
    k_exp = approx_r2.c[0]
    bad_k_exp = (k_exp < my.myeps)
    if (bad_k_exp):
        if (k_exp < 0):
            k_exp = -k_exp
        if (k_exp < my.myeps):
            k_exp = my.myeps

    # -------------------------------- r2(t) ------------------------------------------
    if (make_pics):
        fig_c += 1
        figs.append(plt.figure(fig_c))
        plt.plot(t[N0:N1], r2, '-')  # experiment data
        x = [t[N0], t[N1 - 1]]
        plt.plot(x, [approx_r2(_x) for _x in x],
                 '--')  # line experiment approximation

        tau = abs(approx_r2(0)) / k_exp
        x = t[N0:N1]
        y = [k_exp * (_x - tau + tau * pow(math.e, -_x / tau)) for _x in x]
        #plt.plot(x, [k_exp*(_x - tau + tau*math.exp(-_x/tau)) for _x in x], '--') # non-line experiment approximation
        plt.plot(x, y, '--')  # non-line experiment approximation

        #plt.plot([t[c2], t[c2]], [min(r2), max(r2)], '--') # last part boundary
        plt.ticklabel_format(style='sci', axis='y', scilimits=(0, 0))
        plt.ticklabel_format(style='sci', axis='x', scilimits=(0, 0))
        plt.xlabel('t')
        plt.ylabel('<r^2>')
        plt.grid(True)
        #plt.title('r^2(t) | k_exp = ' + str(my.str_sgn_round(k_exp,3)) + ', k_th = ' + str(my.str_sgn_round(k_th,3)))
        plt.title('r^2(t)')
        if (draw_on_screen):
            figs[fig_c].show()
        path = os.path.join(graph_dir, 'r2(t)_' + time_gaps_str + '.png')
        figs[fig_c].savefig(path)

    lmd_1 = k_exp / 4 * math.sqrt(math.pi * params['mu'] / (Tmp_av * 2))
    sgm_1 = 1 / math.sqrt(n0 * math.pi * lmd_1 * math.sqrt(2))
    lmd_4 = math.sqrt(abs(approx_r2(0)) * math.pi * 3) / 4

    if (draw_on_screen):
        print('Tmp_av = ', Tmp_av)
        print('sgm_th1 = ', sgm_th1)
        print('lmd_th1 = ', lmd_th1)
        print('sgm_1 = ', sgm_1)
        print('lmd_1 = ', lmd_1)
        print('lmd_4 = ', lmd_4)
    if (full_mode):
        i_log = 2
        while (1):
            tg = math.log(r2[i_log + 10] / r2[i_log]) / math.log(
                t[N0 + i_log + 10] / t[N0 + i_log])
            if (abs(2 - tg) > 0.2):
                break
            i_log += 1
            if (i_log >= N1 - N0 - 10):
                break
        stab_time_log = t[i_log]

        i_r1 = 2
        r1 = my.arrFnc(r2, math.sqrt)
        while (1):
            tg = (r1[i_r1 + 10] - r1[i_r1]) / (t[N0 + i_r1 + 10] -
                                               t[N0 + i_r1]) / v_start_av
            if (abs(tg - 1) > 0.18):
                break
            i_r1 += 1
            if (i_r1 >= N1 - N0 - 10):
                break
        stab_time_r1 = t[i_r1]

        #lmd_2 = 2 * stab_time * math.sqrt(2*Tmp_av / (math.pi*params['mu']))
        # it's incorrect to calculate lmd this way here, because in the beggining V doesn't have maxwell distribution
        lmd_2 = stab_time_log * v_start_av
        lmd_3 = stab_time_r1 * v_start_av
        sgm_2 = 1 / math.sqrt(n0 * math.pi * lmd_2 * math.sqrt(2))
        sgm_3 = 1 / math.sqrt(n0 * math.pi * lmd_3 * math.sqrt(2))
        stab_time_th = lmd_th1 / 2 * math.sqrt(math.pi * params['mu'] /
                                               (2 * Tmp_av))
        if (draw_on_screen):
            print('stab_time_th = ', stab_time_th)
            print('stab_time_log = ', stab_time_log)
            print('stab_time_r1 = ', stab_time_r1)
            print('sgm_2 = ', sgm_2)
            print('lmd_2 = ', lmd_2)
            print('r2 = ', math.sqrt(r2[i_log]))
            print('sgm_3 = ', sgm_3)
            print('lmd_3 = ', lmd_3)
            print('r1 = ', r1[i_r1])
        else:
            print(n0, Tmp_av, lmd_th0, lmd_th1, lmd_th2, lmd_1, lmd_2, lmd_3,
                  lmd_4)

        if (make_pics):
            # -------------------------------- log(r2)(log(t)) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            plt.plot(t[N0:N1], r2, '-')  # experiment data
            x = [t[N0 + 1], t[N1 - 1]]
            plt.plot(x, [math.exp(logBeg_approx(math.log(_x))) for _x in x],
                     '--')  # beginning approximation
            plt.plot(x, [math.exp(logEnd_approx(math.log(_x))) for _x in x],
                     '--')  # ending approximation
            #plt.plot(x, [lmd_th1**2, lmd_th1**2], '--')
            #plt.plot(x, [lmd_1**2, lmd_1**2], '--')
            #plt.plot([stab_time_th, stab_time_th], [min(r2), max(r2)], '--')
            #plt.plot([t[c2], t[c2]], [min(r2), max(r2)], '--')

            plt.xlabel('t')
            plt.ylabel('<r^2>')
            plt.grid(True)
            plt.xscale('log')
            plt.yscale('log')
            plt.title('log(r2) | k_begin = ' +
                      str(my.str_sgn_round(logBeg_approx.c[0], 3)) +
                      '; k_end = ' +
                      str(my.str_sgn_round(logEnd_approx.c[0], 3)))
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir,
                                'r2(t)_loglog_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

            # -------------------------------- r(t) ------------------------------------------
            fig_c += 1
            figs.append(plt.figure(fig_c))
            plt.plot(t[N0:N1], r1, '-')  # experiment data
            x = [t[N0 + 1], t[c4]]
            plt.plot(x, [approx_r1(_x) for _x in x],
                     '--')  # beginning approximation
            #plt.plot(x, [lmd_th1, lmd_th1], '--')
            #plt.plot([stab_time_th, stab_time_th], [min(r1), max(r1)], '--')
            #plt.plot([stab_time_r1, stab_time_r1], [min(r1), max(r1)], '--')
            #plt.plot([t[c2], t[c2]], [min(r1), max(r1)], '--')

            plt.xlabel('t')
            plt.ylabel('sqrt(<r^2>)')
            plt.grid(True)
            #plt.title('sqrt(r2) | l_th = ' + str(my.str_sgn_round(lmd_th1,3)) + '; l_3 = ' + str(my.str_sgn_round(lmd_3,3)) + '; l_2 = ' + str(my.str_sgn_round(lmd_2,3)))
            plt.title('sqrt(<r^2>)')
            if (draw_on_screen):
                figs[fig_c].show()
            path = os.path.join(graph_dir, 'r1(t)_' + time_gaps_str + '.png')
            figs[fig_c].savefig(path)

    if (draw_on_screen):
        input()