Exemple #1
0
def main():
    args = sys.argv[1:]
    argc_min = 0
    argc = len(args)
    if(argc < argc_min):
        print('usage: ./cycle.py')
        sys.exit(1)
            
    n = [0.01, 0.02, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7]
    T = [0.01, 0.02, 0.04, 0.065, 0.1, 0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.4, 1.6, 2, 3.2, 6.4, 12.8, 25.6]
    
    n = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7]
    T = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2]    
    
    n = [0.2, 0.3]
    T = [1, 2]
    
    n = [0.01, 0.02, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7]
    T = [0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.4, 1.6, 2, 3.2]
    
    n = [0.02, 0.05, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5]
    #T = [0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.5, 1.75, 2, 2.5, 3, 4]       
    T = [0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.5, 1.75, 2]
    
    for ni in n:
        for Ti in T:
            new_name = 'N2048/n' + my.str_sgn_round(ni, 3) + '_Tmp' + my.str_sgn_round(Ti, 3)
            
            #os.system('./E.py ' + new_name + ' -')
            
            if(not my.run_it('./diffusion.py ' + new_name + ' -pics-percent')):
                return
Exemple #2
0
def main():
    args = sys.argv[1:]
    argc_ok = 0
    argc = len(args)
    if (argc != argc_ok):
        print('usage: ./cycle.py')
        sys.exit(1)

    n = [0.01, 0.02, 0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7]
    T = [
        0.01, 0.02, 0.04, 0.065, 0.1, 0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2,
        1.4, 1.6, 2, 3.2, 6.4, 12.8, 25.6
    ]

    n = [0.05, 0.1, 0.15, 0.2, 0.25, 0.3, 0.35, 0.4, 0.5, 0.6, 0.7]
    T = [0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1, 1.1, 1.2]

    n = [0.2, 0.3]
    T = [1, 2]

    n = [0.02, 0.05, 0.1, 0.2, 0.25, 0.3, 0.4, 0.5]
    T = [0.2, 0.4, 0.7, 0.8, 0.9, 1, 1.1, 1.2, 1.3, 1.5, 1.75, 2, 2.5, 3, 4]

    #base_path = os.path.join('/home', 'ypolyach', '\\!molecules', 'RES', 'DATA')
    base_path = os.path.join('~', '\\!molecules', 'RES', 'DATA')
    for ni in n:
        for Ti in T:
            new_name = 'N2048/n' + str(my.str_sgn_round(ni, 5)) + '_Tmp' + str(
                my.str_sgn_round(Ti, 5))

            main_path = os.path.join(base_path, new_name)
            if (not my.run_it('mkdir --parents ' +
                              os.path.join(main_path, 'graph'))):
                return
            if (not my.run_it('cp -av ' +
                              os.path.join('.', new_name, '*.dat') + ' ' +
                              main_path)):
                return
            if (not my.run_it('cp -av ' +
                              os.path.join('.', new_name, '*.log') + ' ' +
                              main_path)):
                return
            if (not my.run_it('cp -av ' +
                              os.path.join('.', new_name, '*.txt') + ' ' +
                              main_path)):
                return
            if (not my.run_it('cp -av ' +
                              os.path.join('.', new_name, 'graph/*') + ' ' +
                              os.path.join(main_path, 'graph'))):
                return
Exemple #3
0
def main():
    args = sys.argv[1:]
    argc_min = 1
    if len(args) < argc_min:
        print('usage:\npython E.py    model_name    [keys   N0    N1]')
        sys.exit(1)

    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

    #for i in range(N0, N1):
    #    for j in range(4):
    #        E[i,j] /= params['Ntot']
    E /= params['Ntot']

    fig_c = -1
    fig = []
    draw_on_screen = my.find_key(keys, 'keep')
    if (not draw_on_screen):
        plt.switch_backend('Agg')

    fig_c += 1
    # 0
    fig.append(plt.figure(fig_c))
    plt.plot(t[N0:N1], E[N0:N1, 2], '-', label='Etot')
    plt.plot(t[N0:N1], E[N0:N1, 0], '-', label='Ek')
    plt.plot(t[N0:N1], E[N0:N1, 1], '-', label='Ep')
    plt.xlabel('time')
    plt.ylabel('E')
    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):
        fig[fig_c].show()
    path = os.path.join(graph_dir, 'Energy3_' + time_gaps_str + '.png')
    fig[fig_c].savefig(path)

    # 1
    E0 = np.mean(E[N0:N1, 0]) + abs(np.mean(E[N0:N1, 1]))
    path = os.path.join(graph_dir, 'dE_norm_' + time_gaps_str + '.png')
    E_av = np.mean(E[N0:N1, 2])
    #y = [(e_el - E[N0,2])/E0 for e_el in E[N0:N1,2]]
    y = [(e_el / E_av - 1) for e_el in E[N0:N1, 2]]
    fig_c, fig = my.plot_error(fig_c,
                               fig,
                               t[N0:N1],
                               y,
                               y0=0,
                               y_lbl='E/<E>-1',
                               tit='E/<E> - 1 | std = ' +
                               my.str_sgn_round(np.std(y), 3),
                               pic_path=path,
                               show_key=draw_on_screen)

    # shadow error
    E0 = np.mean(E[N0:N1, 0]) + abs(np.mean(E[N0:N1, 1]))
    path = os.path.join(graph_dir, 'dH_norm_' + time_gaps_str + '.png')
    H_av = np.mean(E[N0:N1, 3])
    y = [(e_el / H_av - 1) for e_el in E[N0:N1, 3]]
    #print(E[N0:N1, :])
    fig_c, fig = my.plot_error(fig_c,
                               fig,
                               t[N0:N1],
                               y,
                               y0=0,
                               y_lbl='H/<H>-1',
                               tit='H/<H> - 1 | std = ' +
                               my.str_sgn_round(np.std(y), 3),
                               pic_path=path,
                               show_key=draw_on_screen)

    # shadow
    fig_c += 1
    fig.append(plt.figure(fig_c))
    plt.plot(t[N0:N1], E[N0:N1, 2], '-', label='Etot')
    plt.plot(t[N0:N1], E[N0:N1, 3], '-', label='d2H')
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
               loc=3,
               ncol=2,
               mode="expand",
               borderaxespad=0.)
    if (draw_on_screen):
        fig[fig_c].show()
    path = os.path.join(graph_dir, 'E_H_' + time_gaps_str + '.png')
    fig[fig_c].savefig(path)

    # shadow term
    fig_c += 1
    fig.append(plt.figure(fig_c))
    plt.plot(t[N0:N1], E[N0:N1, 3] - E[N0:N1, 2], '-', label='$d^2 H$')
    plt.legend(bbox_to_anchor=(0., 1.02, 1., .102),
               loc=3,
               mode="expand",
               borderaxespad=0.)
    if (draw_on_screen):
        fig[fig_c].show()
    path = os.path.join(graph_dir, 'd2H_' + time_gaps_str + '.png')
    fig[fig_c].savefig(path)

    # Pressure
    path = os.path.join(graph_dir, 'Pressure_' + time_gaps_str + '.png')
    P_th = params['n'] * params['Tmp']
    y = P[N0:N1]
    fig_c, fig = my.plot_error(
        fig_c,
        fig,
        t[N0:N1],
        y,
        y_th=[_x * P_th for _x in np.ones(len(y))],
        y0=np.mean(P[N0:N1]),
        y_lbl='P',
        tit='P(time) | std_rel = ' +
        my.str_sgn_round(np.std(P[N0:N1]) / np.mean(P[N0:N1]), 3),
        pic_path=path,
        show_key=draw_on_screen)

    # 2
    #path = os.path.join(graph_dir, 'Etot_' + time_gaps_str + '.png')
    #y = [_x*params['Ntot'] for _x in E[N0:N1, 2]]
    #fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=np.mean(y), y_lbl='Etot', tit='Etot | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)

    # 3
    #path = os.path.join(graph_dir, 'dE_normSQRT(N)_' + time_gaps_str + '.png')
    ##y = [(e_el - E[N0,2])*mth.sqrt(params['Ntot']) for e_el in E[N0:N1,2]]
    #y = [(e_el - E_av)*mth.sqrt(params['Ntot']) for e_el in E[N0:N1,2]]
    #fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=0, y_lbl='(E - <E>)/sqrt(N)', tit='Esqrt | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)

    # 4
    #path = os.path.join(graph_dir, 'dE_' + time_gaps_str + '.png')
    ##y = [(e_el - E[N0,2])*params['Ntot'] for e_el in E[N0:N1,2]]
    #y = [(e_el - E_av)*params['Ntot'] for e_el in E[N0:N1,2]]
    #fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=0, y_lbl='E - <E>', tit='E - <E> | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)

    # 5
    path = os.path.join(graph_dir, 'Tmp_' + time_gaps_str + '.png')
    y = Tmp
    fig_c, fig = my.plot_error(fig_c,
                               fig,
                               t[N0:N1],
                               Tmp,
                               y0=Tmp_av,
                               y_lbl='Tmp',
                               tit='Tmp | std_rel = ' +
                               my.str_sgn_round(np.std(Tmp) / Tmp_av, 3),
                               pic_path=path,
                               show_key=draw_on_screen)

    if (draw_on_screen):
        input()
Exemple #4
0
def main():
    args = sys.argv[1:]
    argc_min = 2
    if len(args) < argc_min:
        print(
            'usage: ./maxwell.py    model_name    N_V_steps    [keys,    N0,    N1]'
        )
        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, 2, 3, 4)
    # 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

    NV = int(args[1])
    draw_on_screen = my.find_key(keys, 'keep')
    if (not draw_on_screen):
        plt.switch_backend('Agg')

    if (len(args) < 4):
        if (draw_on_screen):
            print("N0 isn't set, N0 = ", stabTind, ' will be used')
        N0 = stabTind
        Nfrm = N1 - N0

    Z_sum = math.pow(params['mu'] / 2 / np.pi / Tmp_av, 1.5)
    k0 = 2 * np.pi * Z_sum
    v_prob = math.sqrt(2 * Tmp_av / params['mu'])
    v2_prob = Tmp_av / params['mu']
    total_v2 = np.zeros(NV - 1)
    total_v = np.zeros(NV - 1)
    for n in range(N0, N1):
        x, v, m = my.read_frame(model_name, n)

        v2 = my.arrDot(v, v)
        v_abs = my.arrFnc(v2, math.sqrt)
        if (n == N0):
            #v2max = max(v2)
            #v2min = min(v2)
            v2max = 3 * Tmp_av / params['mu'] * 4
            v2min = 3 * Tmp_av / params['mu'] / 10
            k_v2 = 1 / (params['Ntot'] * (v2max - v2min) / NV * Nfrm)
            k_v = 1 / (params['Ntot'] *
                       (math.sqrt(v2max) - math.sqrt(v2min)) / NV * Nfrm)
            v2steps = np.linspace(v2min, v2max, num=NV)
            vsteps = np.linspace(math.sqrt(v2min), math.sqrt(v2max), num=NV)
            x_v2 = [(v2steps[i] + v2steps[i + 1]) / 2 for i in range(NV - 1)]
            x_v = [(vsteps[i] + vsteps[i + 1]) / 2 for i in range(NV - 1)]
            y0_v2 = [
                k0 * math.sqrt(_x) * math.exp(-params['mu'] * _x / 2 / Tmp_av)
                for _x in x_v2
            ]
            y0_v = [
                2 * k0 * _x * _x *
                math.exp(-params['mu'] * _x * _x / 2 / Tmp_av) for _x in x_v
            ]
            #y0_ln = [-params['mu']*_x/2/Tmp_av for _x in x_v2]
            y0_ln = []
            for _x in x_v2:
                _a = params['mu'] * _x / 2 / Tmp_av
                #y0_ln.append(math.log(1-2*_a/3/params['Ntot']) - _a)
                y0_ln.append(-_a)

        peaks, bin_edges = np.histogram(v2, bins=v2steps)
        for i in range(NV - 1):
            total_v2[i] += peaks[i]

        peaks, bin_edges = np.histogram(v_abs, bins=vsteps)
        for i in range(NV - 1):
            total_v[i] += peaks[i]

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

    y_v2 = [total_v2[i] * k_v2 for i in range(NV - 1)]
    y_v = [total_v[i] * k_v for i in range(NV - 1)]
    y_ln = []
    for i in range(NV - 1):
        if (y_v2[i] > 0):
            y_ln.append(math.log(y_v2[i] / k0 / math.sqrt(x_v2[i])))
        else:
            y_ln.append(0)
    dy_v = [y_v[i] / y0_v[i] - 1 for i in range(len(x_v))]
    dy_v2 = [y_v2[i] / y0_v2[i] - 1 for i in range(len(x_v2))]
    dy_ln = [y_ln[i] / y0_ln[i] - 1 for i in range(len(x_v2))]

    p_lnp = np.poly1d(np.polyfit(x_v2, y_ln, 1))
    k_exp = -p_lnp.c[0]
    k_th = params['mu'] / 2 / Tmp_av
    #print('k_exp = ', k_exp)
    #print('k_th = ', k_th)
    s = 0
    for i in range(NV - 1):
        s += (p_lnp(x_v2[i]) - y_ln[i])**2
    s /= (NV - 2)
    if (draw_on_screen):
        print('S = ', s)

    fig_c = -1
    figs = []
    time_gaps_str += ('_NV_' + str(NV))

    path = os.path.join(graph_dir, 'p(v)_' + time_gaps_str + '.png')
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v,
                                y_v,
                                y_th=y0_v,
                                x0=v_prob,
                                x_lbl='v',
                                y_lbl='p',
                                pic_path=path,
                                show_key=draw_on_screen)

    path = os.path.join(graph_dir, 'p(v2)_' + time_gaps_str + '.png')
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v2,
                                y_v2,
                                y_th=y0_v2,
                                x0=v2_prob,
                                x_lbl='v^2',
                                y_lbl='p',
                                pic_path=path,
                                show_key=draw_on_screen)

    path = os.path.join(graph_dir, 'ln_p(v2)_' + time_gaps_str + '.png')
    #fig_c, figs = my.plot_error(fig_c, figs, x_v2, y_ln, y_th=y0_ln, x_lbl='v^2', y_lbl='ln(p/v^2)', tit='(ln(p/v^2))(v^2) | k_th = ' + my.str_sgn_round(k_th,3) + ',  k_exp = ' + my.str_sgn_round(k_exp,3) + ' | std = ' + my.str_sgn_round(s,3), pic_path=path, show_key=draw_on_screen)
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v2,
                                y_ln,
                                y_th=y0_ln,
                                x_lbl='v^2',
                                y_lbl='ln(p/v^2)',
                                tit='k_th = ' + my.str_sgn_round(k_th, 3) +
                                ',  k_exp = ' + my.str_sgn_round(k_exp, 3) +
                                ' | std = ' + my.str_sgn_round(s, 3) +
                                ', p0 = ' + my.str_sgn_round(p_lnp.c[1], 3),
                                pic_path=path,
                                show_key=draw_on_screen)

    path = os.path.join(graph_dir, 'd_p(v)_' + time_gaps_str + '.png')
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v,
                                dy_v,
                                x0=v_prob,
                                y0=0,
                                x_lbl='v',
                                y_lbl='p/p0-1',
                                pic_path=path,
                                show_key=draw_on_screen)

    path = os.path.join(graph_dir, 'd_p(v2)_' + time_gaps_str + '.png')
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v2,
                                dy_v2,
                                x0=v2_prob,
                                y0=0,
                                x_lbl='v^2',
                                y_lbl='p/p0-1',
                                pic_path=path,
                                show_key=draw_on_screen)

    path = os.path.join(graph_dir, 'd_ln_p(v2)_' + time_gaps_str + '.png')
    fig_c, figs = my.plot_error(fig_c,
                                figs,
                                x_v2,
                                dy_ln,
                                y0=0,
                                x_lbl='v^2',
                                y_lbl='dln(p)',
                                pic_path=path,
                                show_key=draw_on_screen)

    if (draw_on_screen):
        input()
Exemple #5
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()
Exemple #6
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()
Exemple #7
0
def main():
    args = sys.argv[1:]
    argc_min = 1
    if len(args) < argc_min:
        print('usage: ./E.py    model_name    [keys   N0    N1]')
        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
    
    for i in range(N0, N1):
        for j in range(3):
            E[i,j] /= params['Ntot']
        
    fig_c = -1
    fig = []
    draw_on_screen = my.find_key(keys, 'keep')
    if(not draw_on_screen):
        plt.switch_backend('Agg') 
    
    fig_c += 1
    # 0
    fig.append(plt.figure(fig_c))
    plt.plot(t[N0:N1], E[N0:N1,2], '-', label = 'Etot')    
    plt.plot(t[N0:N1], E[N0:N1,0], '-', label = 'Ek')
    plt.plot(t[N0:N1], E[N0:N1,1], '-', label = 'Ep')
    plt.xlabel('time')
    plt.ylabel('E')
    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):
        fig[fig_c].show()
    path = os.path.join(graph_dir, 'Energy3_' + time_gaps_str + '.png')
    fig[fig_c].savefig(path)    
    
    # 1    
    E0 = np.mean(E[N0:N1,0]) + abs(np.mean(E[N0:N1,1]))
    path = os.path.join(graph_dir, 'dE_norm_' + time_gaps_str + '.png')
    E_av = np.mean(E[:,2])
    #y = [(e_el - E[N0,2])/E0 for e_el in E[N0:N1,2]]
    y = [(e_el - E_av)/E0 for e_el in E[N0:N1,2]]
    fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=0, y_lbl='E/<E>-1', tit='E/<E> - 1 | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)
    
    # 2
    path = os.path.join(graph_dir, 'Etot_' + time_gaps_str + '.png')
    y = [_x*params['Ntot'] for _x in E[N0:N1, 2]]
    fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=np.mean(y), y_lbl='Etot', tit='Etot | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)
       
    # 3
    path = os.path.join(graph_dir, 'dE_normSQRT(N)_' + time_gaps_str + '.png')
    #y = [(e_el - E[N0,2])*mth.sqrt(params['Ntot']) for e_el in E[N0:N1,2]]
    y = [(e_el - E_av)*mth.sqrt(params['Ntot']) for e_el in E[N0:N1,2]]
    fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=0, y_lbl='(E - <E>)/sqrt(N)', tit='Esqrt | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)
    
    # 4
    path = os.path.join(graph_dir, 'dE_' + time_gaps_str + '.png')
    #y = [(e_el - E[N0,2])*params['Ntot'] for e_el in E[N0:N1,2]]
    y = [(e_el - E_av)*params['Ntot'] for e_el in E[N0:N1,2]]
    fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], y, y0=0, y_lbl='E - <E>', tit='E - <E> | std = ' + my.str_sgn_round(np.std(y),3), pic_path=path, show_key=draw_on_screen)
       
    # 5
    path = os.path.join(graph_dir, 'Tmp_' + time_gaps_str + '.png')
    fig_c, fig = my.plot_error(fig_c, fig, t[N0:N1], Tmp, y0=Tmp_av, y_lbl='Tmp', tit='Tmp | std/Tmp = ' + my.str_sgn_round(np.std(Tmp)/Tmp_av,3), pic_path=path, show_key=draw_on_screen)
    
    if(draw_on_screen):
        input()
Exemple #8
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()