def compare(dynamics, dynamics_ij, dynamics_beta, c, arguments, N, sigma, low, high, transition_to_high):

    t = np.arange(0, 100, 0.01)
    num_col = int(np.sqrt(N))
    A = network_ensemble_grid(9, 3)
    xs_l, xs_h = stable_state(A, degree, dynamics, c, low, high, arguments) 
    A = network_ensemble_grid(N, num_col)
    if transition_to_high == 1:
        x_initial = np.mean(xs_l) * np.ones(N) 
    else:
        x_initial = np.mean(xs_h) * np.ones(N)
    index = np.where(A!=0)
    A_interaction = A[index].reshape(N, degree)
    local_state = np.random.RandomState(1) # avoid same random process.
    noise= local_state.normal(0, np.sqrt(dt), (np.size(t)-1, N)) * sigma
    dyn = main.sdesolver(main.close(dynamics, *(N, index, degree, A_interaction, c, arguments)), x_initial, t, dW = noise)
    transition_first = np.where(dyn > (xs_l[0] + xs_h[0])/2)[1][0]
    # transition_first = 8
    first_neighbor = index[1][transition_first * c : (transition_first+1) * c]
    noise_beta = np.zeros((np.size(t)-1, 2))
    noise_beta[:, 0] = noise[:, transition_first] 
    noise_beta[:, 1] = np.mean(np.take(noise, first_neighbor, axis=1), -1)
    # noise_beta[:, 2] = np.mean(noise, -1)

    dyn_ij = main.sdesolver(main.close(dynamics_ij, *(c,arguments[:-1])), x_initial[:2], t, dW = noise_beta)

    '''
    des = '../data/' + 'mutual' + str(4) + f'/size1/c{c}/' 
    xy_relation = np.array(pd.read_csv(des + 'xy_relation.csv', header=None).iloc[:, :])
    f = interp1d(xy_relation[0], xy_relation[1])
    xj_stable = np.zeros((np.size(dyn_ij[:, 0])))
    plt.plot(t, xj_stable)
    for xi, i in zip(dyn_ij[:, 0], range(np.size(dyn_ij[:, 0]))):
        if xi < xy_relation[0, 0]:
            xj = xy_relation[1, 0]
        elif xi > xy_relation[0, -1]:
            xj = xy_relation[1, -1]
        else:
            xj = f(xi)
        xj_stable[i] = xj
    dyn_beta = main.sdesolver(main.close(dynamics_beta, *(c,arguments[:-1]), f, xy_relation[0, 0], xy_relation[0, -1]), x_initial[:1], t, dW = noise_beta[:, 0].reshape(np.size(noise_beta[:,0]), 1))
    plt.plot(t, dyn_beta, label='beta')
    '''
    plt.plot(t, dyn[:, transition_first], label='first_original')
    plt.plot(t, np.mean(np.take(dyn, first_neighbor, axis=1), -1), label='neighbor_original')
    plt.plot(t, dyn_ij[:, 0], label='first_reduction')
    plt.plot(t, dyn_ij[:, 1], label='neighbor_reduction')
    
    #plt.plot(t, np.mean(dyn, -1))
    plt.xlabel('t', fontsize=fs)
    plt.ylabel('x', fontsize=fs)
    # plt.title(f'$c=${c}_$N=${N}_$\\sigma=${sigma}', fontsize=fs)
    plt.xticks(fontsize=15)
    plt.yticks(fontsize=15)
    plt.subplots_adjust(left=0.15, right=0.99, wspace=0.25, hspace=0.25, bottom=0.13, top=0.99)

    plt.legend(fontsize=14)
    plt.xlim(right=40)
    plt.show()
    return index, dyn, xs_l, xs_h
def system_collect(store_index, N, index, degree, A_interaction, strength, x_initial, T_start, T_end, t, dt, des_evolution, des_ave, des_high, dynamics, c, arguments, transition_to_high, criteria, remove, initial_noise):
    """one realization to run sdeint and save dynamics

    """
    evolution_file = des_evolution + f'realization{store_index}_T_{T_start}_{T_end}'
    # local_state = np.random.RandomState(store_index + T_start * int(parallel_size_all/T_every) ) # avoid same random process.
    local_state = np.random.RandomState(store_index)
    if initial_noise == 'metastable' and T_start == 0:
        pre_t = np.arange(0, 50, dt)
        dynamics_pre = globals()[dynamics.__name__[: dynamics.__name__.find('_')] + '_preprocess']
        noise= local_state.normal(0, np.sqrt(dt), (np.size(pre_t)-1, N)) * strength
        x_initial = main.sdesolver(main.close(dynamics_pre, *(N, index, degree, A_interaction, c, arguments)), x_initial, pre_t, dW = noise)[-1] 


    for i in range(int(T_start/T_every)+1):
        noise= local_state.normal(0, np.sqrt(dt), (np.size(t)-1, N)) * strength
    if N == 1:
        dynamics = globals()[dynamics.__name__[: dynamics.__name__.find('_')] + '_1D']
        dyn_all = main.sdesolver(main.close(dynamics, *(c, arguments[:-1])), x_initial, t, dW=noise.reshape(np.size(noise), 1) ) 
        '''
        des = '../data/' + 'mutual' + str(4) + f'/size1/c{c}/' 
        xy_relation = np.array(pd.read_csv(des + 'xy_relation.csv', header=None).iloc[:, :])
        f = interp1d(xy_relation[0], xy_relation[1])

        dynamics = globals()[dynamics.__name__[: dynamics.__name__.find('_')] + '_beta']
        dyn_all = main.sdesolver(main.close(dynamics, *(c, arguments[:-1], f, xy_relation[0, 0], xy_relation[0, -1] )), x_initial, t, dW=noise.reshape(np.size(noise), 1) ) 
        '''
        dyn_ave = np.mean(dyn_all, -1)
        x_high = np.mean(dyn_all[-1])

    elif N == 2:
        dynamics = globals()[dynamics.__name__[: dynamics.__name__.find('_')] + '_ij']
        noise[:, 1] = noise[:, 1] / 2 
        dyn_all = main.sdesolver(main.close(dynamics, *(c, arguments)), x_initial, t, dW=noise )
        x_high = dyn_all[-1, 0]
        dyn_ave = dyn_all[:, 0]
    else:
        dyn_all = main.sdesolver(main.close(dynamics, *(N, index, degree, A_interaction, c, arguments)), x_initial, t, dW = noise)
        dyn_ave = np.mean(dyn_all, -1)
        x_high = np.mean(dyn_all[-1])
    x_high_df = pd.DataFrame(np.ones((1, 1)) * x_high)
    x_high_df.to_csv(des_high + f'realization{store_index}.csv', mode='a', index=False, header=False)
    if (transition_to_high == 1 and x_high > criteria) or (transition_to_high == 0 and x_high < criteria):
        ave_file = des_ave + f'realization{store_index}_T_{T_start}_{T_end}'
        np.save(ave_file, dyn_ave)
        if remove == 0:
            np.save(evolution_file, dyn_all)
    else:
        np.save(evolution_file, dyn_all)
    del noise, dyn_all, x_high, dyn_ave
    return None
示例#3
0
def system_collect(store_index, N, index, degree, A_interaction, strength,
                   x_initial, T_start, T_end, t, dt, des_evolution, des_ave,
                   des_high, c):
    """one realization to run sdeint and save dynamics

    """
    local_state = np.random.RandomState(store_index +
                                        T_start)  # avoid same random process.
    noise = local_state.normal(0, np.sqrt(dt), (np.size(t) - 1, N)) * strength
    dyn_all = main.sdesolver(main.close(harvest_lattice,
                                        *(N, index, degree, A_interaction, c)),
                             x_initial,
                             t,
                             dW=noise)
    evolution_file = des_evolution + f'realization{store_index}_T_{T_start}_{T_end}'
    np.save(evolution_file, dyn_all)
    x_high = np.mean(dyn_all[-1])
    x_high_df = pd.DataFrame(np.ones((1, 1)) * x_high)
    x_high_df.to_csv(des_high + f'realization{store_index}.csv',
                     mode='a',
                     index=False,
                     header=False)
    if x_high > K / 2:

        ave_file = des_ave + f'realization{store_index}_T_{T_start}_{T_end}'
        np.save(ave_file, np.mean(dyn_all, -1))

    return None
def tg_from_one_transition(dynamics, c, arguments, N, sigma, low, high, transition_to_high):
    """TODO: Docstring for tg_from_one_transition.

    :N: TODO
    :sigma: TODO
    :returns: TODO

    """
    t = np.arange(0, 100, 0.01)
    num_col = int(np.sqrt(N))
    A = network_ensemble_grid(9, 3)
    xs_l, xs_h = stable_state(A, degree, dynamics, c, low, high, arguments) 
    A = network_ensemble_grid(N, num_col)
    if transition_to_high == 1:
        x_initial = np.mean(xs_l) * np.ones(N) 
    else:
        x_initial = np.mean(xs_h) * np.ones(N)
    x_half = (np.mean(xs_l) + np.mean(xs_h))/2
    x_initial[int(num_col/2)] = np.mean(xs_h)
    index = np.where(A!=0)
    A_interaction = A[index].reshape(N, degree)
    local_state = np.random.RandomState(5) # avoid same random process.
    noise= np.random.normal(0, np.sqrt(dt), (np.size(t)-1, N)) * sigma
    dyn = main.sdesolver(main.close(dynamics, *(N, index, degree, A_interaction, c, arguments)), x_initial, t, dW = noise)
    rho = np.mean(dyn, -1)
    tg = next(x for x, y in enumerate(rho) if y > x_half) * 0.01
    return tg
示例#5
0
def example():

    N = 100
    num_col = int(np.sqrt(N))
    x_initial = np.ones(N) * 1
    A = network_ensemble_grid(N, num_col)
    N = np.size(A, -1)
    index = np.where(A != 0)
    A_interaction = A[index].reshape(N, degree)
    local_state = np.random.RandomState(0)  # avoid same random process.
    noise = local_state.normal(0, np.sqrt(dt), (np.size(t) - 1, N)) * strength
    dyn = main.sdesolver(main.close(harvest_lattice,
                                    *(N, index, degree, A_interaction, c)),
                         x_initial,
                         t,
                         dW=noise)
def example(dynamics, c, arguments, N, sigma, low, high, transition_to_high):

    t = np.arange(0, 1000, 0.01)
    num_col = int(np.sqrt(N))
    A = network_ensemble_grid(9, 3)
    xs_l, xs_h = stable_state(A, degree, dynamics, c, low, high, arguments) 
    A = network_ensemble_grid(N, num_col)
    if transition_to_high == 1:
        x_initial = np.mean(xs_l) * np.ones(N) 
    else:
        x_initial = np.mean(xs_h) * np.ones(N)
    index = np.where(A!=0)
    A_interaction = A[index].reshape(N, degree)
    local_state = np.random.RandomState(5) # avoid same random process.
    noise= local_state.normal(0, np.sqrt(dt), (np.size(t)-1, N)) * sigma
    dyn = main.sdesolver(main.close(dynamics, *(N, index, degree, A_interaction, c, arguments)), x_initial, t, dW = noise)
    #plt.plot(t, np.mean(dyn, -1))
    plt.plot(t, dyn)
    plt.xlabel('t', fontsize=fs)
    plt.ylabel('x', fontsize=fs)
    plt.title(f'$c=${c}_$N=${N}_$\\sigma=${sigma}', fontsize=fs)
    plt.show()
    return index, dyn, xs_l, xs_h