Exemple #1
0
def test_matmul_3D():
    '''
    '''
    M = 3
    N = 2
    P = 4
    Q = 2

    a = af.range(M * N * Q, dtype=af.Dtype.u32)
    b = af.range(N * P * Q, dtype=af.Dtype.u32)

    a = af.moddims(a, d0=M, d1=N, d2=Q)
    b = af.moddims(b, d0=N, d1=P, d2=Q)

    a_init = a
    b_init = b

    ref_a_0 = np.matmul(np.array(a_init[:, :, 0]), np.array(b_init[:, :, 0]))

    ref_a_1 = np.matmul(np.array(a_init[:, :, 1]), np.array(b_init[:, :, 1]))

    test_matmul = np.array(utils.matmul_3D(a, b))

    diff_mat_0 = np.abs(test_matmul[:, :, 0] - ref_a_0)
    diff_mat_1 = np.abs(test_matmul[:, :, 1] - ref_a_1)

    assert np.all(diff_mat_0 == 0) and np.all(diff_mat_1 == 0)
def A_matrix():
    '''

    Calculates A matrix whose elements :math:`A_{p i}` are given by
    :math:`A_{pi} = \\int^1_{-1} L_p(\\xi)L_i(\\xi) \\frac{dx}{d\\xi}`

    The integrals are computed using the integrate() function.
    Since elements are taken to be of equal size, :math:`\\frac {dx}{d\\xi}`
    is same everywhere
    
    Returns
    -------

    A_matrix : arrayfire.Array [N_LGL N_LGL 1 1]
               The value of integral of product of lagrange basis functions
               obtained by LGL points, using the integrate() function

    '''
    # Coefficients of Lagrange basis polynomials.
    lagrange_coeffs = params.lagrange_coeffs
    lagrange_coeffs = af.reorder(lagrange_coeffs, 1, 2, 0)

    # Coefficients of product of Lagrange basis polynomials.
    lag_prod_coeffs = af.convolve1(lagrange_coeffs,\
                                   af.reorder(lagrange_coeffs, 0, 2, 1),\
                                   conv_mode=af.CONV_MODE.EXPAND)
    lag_prod_coeffs = af.reorder(lag_prod_coeffs, 1, 2, 0)
    lag_prod_coeffs = af.moddims(lag_prod_coeffs, params.N_LGL**2,
                                 2 * params.N_LGL - 1)

    dx_dxi = params.dx_dxi
    A_matrix = dx_dxi * af.moddims(lagrange.integrate(lag_prod_coeffs),\
                                             params.N_LGL, params.N_LGL)

    return A_matrix
Exemple #3
0
def test_poly1d_prod():
    '''
    Checks the product of the polynomials of different degrees using the
    poly1d_product function and compares it to the analytically calculated
    product coefficients.
    '''

    N = 3

    N_a = 3
    poly_a = af.range(N * N_a, dtype=af.Dtype.u32)
    poly_a = af.moddims(poly_a, d0=N, d1=N_a)

    N_b = 2
    poly_b = af.range(N * N_b, dtype=af.Dtype.u32)
    poly_b = af.moddims(poly_b, d0=N, d1=N_b)

    ref_poly = af.np_to_af_array(
        np.array([[0., 0., 9., 18.], [1., 8., 23., 28.], [4., 20., 41., 40.]]))

    test_poly1d_prod = utils.poly1d_product(poly_a, poly_b)
    test_poly1d_prod_commutative = utils.poly1d_product(poly_b, poly_a)

    diff = af.abs(test_poly1d_prod - ref_poly)
    diff_commutative = af.abs(test_poly1d_prod_commutative - ref_poly)

    assert af.all_true(diff == 0.) and af.all_true(diff_commutative == 0.)
Exemple #4
0
def read_and_preprocess_notmnist_data():
    print('Reading and preprocessing notMNIST data...')

    with open('notMNIST.pickle', 'rb') as f:
        data = pickle.load(f)

    train_images = af.from_ndarray(data['train_dataset'])
    train_targets = af.from_ndarray(ints_to_onehots(data['train_labels'], 10))
    test_images = af.from_ndarray(data['test_dataset'])
    test_targets = af.from_ndarray(ints_to_onehots(data['test_labels'], 10))

    num_train = train_images.dims()[0]
    num_test = test_images.dims()[0]

    feature_length = int(train_images.elements() / num_train)
    train_feats = af.moddims(train_images, num_train, feature_length)
    test_feats = af.moddims(test_images, num_test, feature_length)

    X_train = train_feats.to_ndarray()
    y_train = train_targets.to_ndarray()
    X_test = test_feats.to_ndarray()
    y_test = test_targets.to_ndarray()

    y_train = onehots_to_ints(y_train).astype('uint32')
    y_test = onehots_to_ints(y_test).astype('uint32')

    print('Reading and preprocessing notMNIST data DONE')
    return (X_train, y_train, X_test, y_test)
Exemple #5
0
def read_and_preprocess_mnist_data():
    print('Reading and preprocessing MNIST data...')

    train_images = af.read_array('train_images.af', key='train_images')
    train_targets = af.read_array('train_targets.af', key='train_targets')
    test_images = af.read_array('test_images.af', key='test_images')
    test_targets = af.read_array('test_targets.af', key='test_targets')

    num_train = train_images.dims()[2]
    num_classes = train_targets.dims()[0]
    num_test = test_images.dims()[2]

    feature_length = int(train_images.elements() / num_train)
    train_feats = af.transpose(
        af.moddims(train_images, feature_length, num_train))
    test_feats = af.transpose(af.moddims(test_images, feature_length,
                                         num_test))

    train_targets = af.transpose(train_targets)
    test_targets = af.transpose(test_targets)

    X_train = train_feats.to_ndarray()
    y_train = train_targets.to_ndarray()
    X_test = test_feats.to_ndarray()
    y_test = test_targets.to_ndarray()

    y_train = onehots_to_ints(y_train)
    y_test = onehots_to_ints(y_test)

    y_train = y_train.astype('uint32')
    y_test = y_test.astype('uint32')

    print('Reading and preprocessing MNIST data DONE')
    return (X_train, y_train, X_test, y_test)
Exemple #6
0
def communicate_fields(self, on_fdtd_grid=False):
    """
    Used in communicating the values at the boundary zones for each of
    the local vectors among all procs.This routine is called to take care
    of communication(and periodic B.C's) procedures for the EM field
    arrays. The function is used for communicating the EM field values 
    on the cell centered grid  which is used by default. Additionally,it can
    also be used to communicate the values on the Yee-grid which is used by the FDTD solver.
    """
    if (self.performance_test_flag == True):
        tic = af.time()

    # Obtaining start coordinates for the local zone
    # Additionally, we also obtain the size of the local zone
    ((i_q1_start, i_q2_start), (N_q1_local,
                                N_q2_local)) = self._da_fields.getCorners()

    N_g = self.N_g

    # Assigning the values of the af.Array
    # fields quantities to the PETSc.Vec:

    if (on_fdtd_grid is True):
        flattened_global_EM_fields_array = \
            af.flat(self.yee_grid_EM_fields[:, :, N_g:-N_g, N_g:-N_g])
        flattened_global_EM_fields_array.to_ndarray(self._glob_fields_array)

    else:
        flattened_global_EM_fields_array = \
            af.flat(self.cell_centered_EM_fields[:, :, N_g:-N_g, N_g:-N_g])
        flattened_global_EM_fields_array.to_ndarray(self._glob_fields_array)

    # Takes care of boundary conditions and interzonal communications:
    self._da_fields.globalToLocal(self._glob_fields, self._local_fields)

    # Converting back to af.Array
    if (on_fdtd_grid is True):

        self.yee_grid_EM_fields = af.moddims(
            af.to_array(self._local_fields_array), 6, 1, N_q1_local + 2 * N_g,
            N_q2_local + 2 * N_g)

        af.eval(self.yee_grid_EM_fields)

    else:

        self.cell_centered_EM_fields = af.moddims(
            af.to_array(self._local_fields_array), 6, 1, N_q1_local + 2 * N_g,
            N_q2_local + 2 * N_g)

        af.eval(self.cell_centered_EM_fields)

    if (self.performance_test_flag == True):
        af.sync()
        toc = af.time()
        self.time_communicate_fields += toc - tic

    return
Exemple #7
0
def Get_Linear_Equation_Gpu(x,
                            weight,
                            bin_data_num,
                            bin_data_y,
                            r,
                            dtype='f4'):
    n, p = x.shape
    d = p - 1
    if dtype is 'f4':
        dtype = af.Dtype.f32
    elif dtype is 'f8':
        dtype = af.Dtype.f64
    xw = af.constant(0, n, p, dtype=dtype)
    for ii in af.ParallelRange(p):
        xw[:, ii] = x[:, ii] * weight
    s = af.constant(0, p, p, np.prod(bin_data_num.shape), dtype=dtype)
    t = af.constant(0, p, np.prod(bin_data_num.shape), dtype=dtype)
    ker_d = np.ones(4, dtype='int')
    ker_d[:d] = 2 * r + 1
    if d is 4:
        for i in range(p):
            for j in range(i, p):
                if i is 0:
                    kernel = af.moddims(xw[:, j], ker_d[0], ker_d[1], ker_d[2],
                                        ker_d[3])
                    t[j] = af.flat(
                        af.reorder(Convolve4(bin_data_y, kernel), 3, 2, 1, 0))
                else:
                    kernel = af.moddims(xw[:, i] * x[:, j], ker_d[0], ker_d[1],
                                        ker_d[2], ker_d[3])
                s[i, j] = af.flat(
                    af.reorder(Convolve4(bin_data_num, kernel), 3, 2, 1, 0))
                s[j, i] = s[i, j]
    elif d < 4:
        for i in range(p):
            for j in range(i, p):
                if i is 0:
                    kernel = af.moddims(xw[:, j], ker_d[0], ker_d[1], ker_d[2],
                                        ker_d[3])
                    if kernel.elements() is 1:
                        t[j] = af.flat((bin_data_y * kernel.to_list()[0]).T)
                    else:
                        t[j] = af.flat(af.fft_convolve(bin_data_y, kernel).T)
                else:
                    kernel = af.moddims(xw[:, i] * x[:, j], ker_d[0], ker_d[1],
                                        ker_d[2], ker_d[3])
                if kernel.elements() is 1:
                    s[i, j] = af.flat((bin_data_num * kernel.to_list()[0]).T)
                else:
                    s[i, j] = af.flat(af.fft_convolve(bin_data_num, kernel).T)
                s[j, i] = s[i, j]
    for i in range(1, p):
        s[i, i] += 1e-12
    return ([
        np.array(s).reshape(p**2, -1).T.reshape(-1, p, p),
        np.array(af.flat(t))
    ])
def volume_integral(u, advec_var):
    '''
    Vectorize, p, q, moddims.
    '''
    dLp_xi_ij_Lq_eta_ij = advec_var.dLp_Lq
    dLq_eta_ij_Lp_xi_ij = advec_var.dLq_Lp
    dxi_dx = 10.
    deta_dy = 10.
    jacobian = 100.
    c_x = params.c_x
    c_y = params.c_y

    if (params.volume_integrand_scheme_2d == 'Lobatto'
            and params.N_LGL == params.N_quad):
        w_i = af.flat(
            af.transpose(
                af.tile(advec_var.lobatto_weights_quadrature, 1,
                        params.N_LGL)))
        w_j = af.tile(advec_var.lobatto_weights_quadrature, params.N_LGL)
        wi_wj_dLp_xi = af.broadcast(utils.multiply, w_i * w_j,
                                    advec_var.dLp_Lq)
        volume_integrand_ij_1_sp = c_x * dxi_dx * af.broadcast(utils.multiply,\
                                               wi_wj_dLp_xi, u) / jacobian
        wi_wj_dLq_eta = af.broadcast(utils.multiply, w_i * w_j,
                                     advec_var.dLq_Lp)
        volume_integrand_ij_2_sp = c_y * deta_dy * af.broadcast(utils.multiply,\
                                               wi_wj_dLq_eta, u) / jacobian

        volume_integral = af.reorder(
            af.sum(volume_integrand_ij_1_sp + volume_integrand_ij_2_sp, 0), 2,
            1, 0)

    else:
        volume_integrand_ij_1 = c_x * dxi_dx * af.broadcast(utils.multiply,\
                                        dLp_xi_ij_Lq_eta_ij,\
                                        u) / jacobian

        volume_integrand_ij_2 = c_y * deta_dy * af.broadcast(utils.multiply,\
                                        dLq_eta_ij_Lp_xi_ij,\
                                        u) / jacobian

        volume_integrand_ij = af.moddims(volume_integrand_ij_1 + volume_integrand_ij_2, params.N_LGL ** 2,\
                                         (params.N_LGL ** 2) * 100)

        lagrange_interpolation = af.moddims(
            wave_equation_2d.lag_interpolation_2d(volume_integrand_ij,
                                                  advec_var.Li_Lj_coeffs),
            params.N_LGL, params.N_LGL, params.N_LGL**2 * 100)

        volume_integrand_total = utils.integrate_2d_multivar_poly(lagrange_interpolation[:, :, :],\
                                                    params.N_quad,'gauss', advec_var)
        volume_integral = af.transpose(
            af.moddims(volume_integrand_total, 100, params.N_LGL**2))

    return volume_integral
Exemple #9
0
def calculate_dfdp_background(self):
    """
    Calculates the derivative of the background distribution 
    with respect to the variables p1, p2, p3. This is used to
    solve for the contribution from the fields
    """
    f_b = af.moddims(self.f_background, self.N_p1, self.N_p2, self.N_p3, self.N_species)

#    8th order central diff -- just in case
#    f_minus_4 = af.shift(f_b, 4); f_plus_4 = af.shift(f_b, -4)
#    f_minus_3 = af.shift(f_b, 3); f_plus_3 = af.shift(f_b, -3)
#    f_minus_2 = af.shift(f_b, 2); f_plus_2 = af.shift(f_b, -2)
#    f_minus_1 = af.shift(f_b, 1); f_plus_1 = af.shift(f_b, -1)
#
#    dfdp1_background = \
#    ( 1/280)*f_minus_4 + (-4/105)*f_minus_3 + ( 1/5)*f_minus_2 + (-4/5)*f_minus_1 \
#  + (-1/280)*f_plus_4  + ( 4/105)*f_plus_3  + (-1/5)*f_plus_2  + ( 4/5)*f_plus_1 
#
#    dfdp1_background = dfdp1_background/self.dp1

    # Using a 4th order central difference stencil:
    dfdp1_background = (-af.shift(f_b, -2) + 8 * af.shift(f_b, -1)
                        +af.shift(f_b,  2) - 8 * af.shift(f_b,  1)
                       ) / (12 * self.dp1)

    dfdp2_background = (-af.shift(f_b, 0, -2) + 8 * af.shift(f_b, 0, -1)
                        +af.shift(f_b, 0,  2) - 8 * af.shift(f_b, 0,  1)
                       ) / (12 * self.dp2)

    dfdp3_background = (-af.shift(f_b, 0, 0, -2) + 8 * af.shift(f_b, 0, 0, -1)
                        +af.shift(f_b, 0, 0,  2) - 8 * af.shift(f_b, 0, 0,  1)
                       ) / (12 * self.dp3)

    # Reshaping such that the variations in velocity are along axis 0:
    self.dfdp1_background = af.moddims(dfdp1_background, 
                                       self.N_p1 * self.N_p2 * self.N_p3,
                                       self.N_species
                                      )
    self.dfdp2_background = af.moddims(dfdp2_background,
                                       self.N_p1 * self.N_p2 * self.N_p3,
                                       self.N_species
                                      )
    self.dfdp3_background = af.moddims(dfdp3_background,
                                       self.N_p1 * self.N_p2 * self.N_p3,
                                       self.N_species
                                      )

    af.eval(self.dfdp1_background,
            self.dfdp2_background,
            self.dfdp3_background
           )

    return
Exemple #10
0
def pool(image, w, s):
    #TODO: Remove assumption of image.dims()[3] == 1
    d0, d1, d2 = image.dims()
    #TODO: remove reorder and moddims call
    image = af.moddims(af.reorder(image, 2, 0, 1), 1, d2, d0, d1)
    d0, d1, d2, d3 = image.dims()
    h_o = (d2 - w)/s + 1
    w_o = (d3 - w)/s + 1
    tiles = af.unwrap(af.reorder(image, 2, 3, 1, 0), w, w, s, s)
    
    return af.reorder(af.reorder(af.moddims(af.max(tiles, 0), d0, h_o, w_o,
                                            d1), 0, 3, 1, 2), 2, 3, 1, 0)
Exemple #11
0
def conv(weights, biases, image, wx, wy, sx = 1, sy = 1, px = 0, py = 0, groups = 1):
    image = __pad(image, px, py)

    batch     = util.num_input(image)
    n_filters = util.num_filters(weights)

    n_channel = util.num_channels(weights)

    w_i = image.dims()[0]
    h_i = image.dims()[1]
    w_o = (w_i - wx) / sx + 1
    h_o = (h_i - wy) / sy + 1

    tiles     = af.unwrap(image, wx, wy, sx, sy)

    weights   = af.moddims(weights, wx*wy, n_channel, n_filters)
    out       = af.constant(0, batch, n_filters, w_o, h_o)

    if groups > 1:

        out = af.constant(0, w_o, h_o, n_filters, batch)

        split_in = util.num_channels(image) / groups
        s_i = split_in
        split_out = n_filters / groups
        s_o = split_out

        for i in xrange(groups):
            weights_slice = af.moddims(weights[:,:,i*s_o:(i+1)*s_o],
                                       wx, wy, n_channel, split_out)
            biases_slice = biases[i*s_o:(i+1)*s_o]
            image_slice  = image[:,:,i*s_i:(i+1)*s_i]
            out[:,:,i*s_o:(i+1)*s_o] = conv(weights_slice,
                                        biases_slice,
                                        image_slice,
                                        wx, wy, sx, sy, 0, 0, 1)
            # out[:,i*s_o:(i+1)*s_o] = conv(weights_slice,
            #                               biases_slice,
            #                               image_slice,
            #                               wx, wy, sx, sy, 0, 0, 1)
        return out

    #TODO: Speedup this section
    for f in xrange(n_filters):
        for d in xrange(n_channel):
            tile_d = af.reorder(tiles[:,:,d],1,0)
            weight_d = weights[:,d,f]
            out[0,f] += af.moddims(af.matmul(tile_d, weight_d), 1,1, w_o, h_o)
        out[0,f] += biases[f].to_list()[0]

    return af.reorder(out, 2, 3, 1, 0)
Exemple #12
0
def lax_friedrichs_flux(u, gv):
    '''
    '''
    u = af.reorder(af.moddims(u, params.N_LGL**2, 10, 10), 2, 1, 0)

    diff_u_boundary = af.np_to_af_array(np.zeros([10, 10, params.N_LGL**2]))

    u_xi_minus1_boundary_right = u[:, :, :params.N_LGL]
    u_xi_minus1_boundary_left = af.shift(u[:, :, -params.N_LGL:], d0=0, d1=1)
    u[:, :, :params.N_LGL] = (u_xi_minus1_boundary_right +
                              u_xi_minus1_boundary_left) / 2

    diff_u_boundary[:, :, :params.N_LGL] = (u_xi_minus1_boundary_right -
                                            u_xi_minus1_boundary_left)

    u_xi_1_boundary_left = u[:, :, -params.N_LGL:]
    u_xi_1_boundary_right = af.shift(u[:, :, :params.N_LGL], d0=0, d1=-1)
    u[:, :, :params.N_LGL] = (u_xi_minus1_boundary_left +
                              u_xi_minus1_boundary_right) / 2

    diff_u_boundary[:, :, -params.N_LGL:] = (u_xi_minus1_boundary_right -
                                             u_xi_minus1_boundary_left)

    u_eta_minus1_boundary_down = af.shift(u[:, :, params.N_LGL -
                                            1:params.N_LGL**2:params.N_LGL],
                                          d0=-1)
    u_eta_minus1_boundary_up = u[:, :, 0:-params.N_LGL + 1:params.N_LGL]
    u[:, :, 0:-params.N_LGL + 1:params.N_LGL] = (u_eta_minus1_boundary_down\
                                               + u_eta_minus1_boundary_up) / 2
    diff_u_boundary[:, :, 0:-params.N_LGL + 1:params.N_LGL] = (u_eta_minus1_boundary_up\
                                                               -u_eta_minus1_boundary_down)

    u_eta_1_boundary_down = u[:, :,
                              params.N_LGL - 1:params.N_LGL**2:params.N_LGL]
    u_eta_1_boundary_up = af.shift(u[:, :, 0:-params.N_LGL + 1:params.N_LGL],
                                   d0=1)

    u[:, :, params.N_LGL - 1:params.N_LGL ** 2:params.N_LGL] = (u_eta_1_boundary_up\
                                                              +u_eta_1_boundary_down) / 2

    diff_u_boundary[:, :, params.N_LGL - 1:params.N_LGL ** 2:params.N_LGL] = (u_eta_1_boundary_up\
                                                                             -u_eta_1_boundary_down)

    u = af.moddims(af.reorder(u, 2, 1, 0), params.N_LGL**2, 100)
    diff_u_boundary = af.moddims(af.reorder(diff_u_boundary, 2, 1, 0),
                                 params.N_LGL**2, 100)
    F_xi_e_ij = F_xi(u, gv) - params.c_x * diff_u_boundary
    F_eta_e_ij = F_eta(u, gv) - params.c_y * diff_u_boundary

    return F_xi_e_ij, F_eta_e_ij
Exemple #13
0
def f_left(q1, q2, p1, p2, p3, params):
    f    = q1**0 * np.sqrt(1 / (4 * np.pi)) * af.exp(-p1**2 / 4)
    f[:] = 0
    
    f           = af.moddims(f, 49*49, 4, 4)
    f_activated = f.copy()
    
    f_activated[:, 3, 2] = 1
    
    f           = af.moddims(f, 49, 49, 4*4)
    f_activated = af.moddims(f_activated, 49, 49, 4*4)
    
    f[:, 19:28] = f_activated[:, 19:28]
    return(f)
def add_boundary_conditions(u, v):

    u_bc = af.np_to_af_array(np.zeros([params.n, params.n]))
    v_bc = af.np_to_af_array(np.zeros([params.n, params.n]))

    u = af.moddims(u, params.n - 2, params.n - 2)
    v = af.moddims(v, params.n - 2, params.n - 2)

    u_bc[1:-1, 1:-1] = u
    v_bc[1:-1, 1:-1] = v

    u_bc[:, 0] += params.u_i_0

    return u_bc, v_bc
Exemple #15
0
def f_left(q1, q2, p1, p2, p3, params):
    f = q1**0 * np.sqrt(1 / (4 * np.pi)) * af.exp(-p1**2 / 4)
    f[:] = 0

    f = af.moddims(f, 1038 * 178, 4, 4)
    f_activated = f.copy()

    f_activated[:, 3, 2] = 1
    f_activated[:, 3, 1] = 1

    f = af.moddims(f, 1038, 178, 4 * 4)
    f_activated = af.moddims(f_activated, 1038, 178, 4 * 4)

    f[:, 71:105] = f_activated[:, 71:105]
    return (f)
Exemple #16
0
def initialize_f(q1, q2, p1, p2, p3, params):
    rho = af.exp(-200 * (q1 - 0.5)**2 - 200 * (q2 - 0.5)**2)
    f = p1 * rho

    f[:] = 0

    f = af.moddims(f, N_p1, N_p2, N_p3, q1.shape[2] * q1.shape[3])
    rho = af.moddims(rho, 1, 1, 1, q1.shape[2] * q1.shape[3])

    f[2, 2, 0] = rho

    f = af.moddims(f, N_p1 * N_p2 * N_p3, 1, q1.shape[2], q1.shape[3])

    af.eval(f)
    return (f)
def grad_q(q, u, v):
    '''
    done - Matching qx qnd qy
    '''
    q = af.moddims(q, params.n, params.n)

    q_x = af.np_to_af_array(np.zeros([params.n, params.n]))
    q_y = af.np_to_af_array(np.zeros([params.n, params.n]))

    q_x[1:-1, 1:-1] = (q[:-2, 1:-1] - q[2:, 1:-1]) * (params.n - 1) / 2.0
    q_y[1:-1, 1:-1] = (q[1:-1, :-2] - q[1:-1, 2:]) * (params.n - 1) / 2.0

    # Horizontal boundary conditions, qx = 0
    q_y[0, 1:-1] = (q[0, :-2] - q[0, 2:]) * (params.n - 1) / 2.0
    q_y[-1, 1:-1] = (q[-1, :-2] - q[-1, 2:]) * (params.n - 1) / 2.0

    # Vertical boundary conditions, qy = 0
    q_x[1:-1, 0] = (q[:-2, 0] - q[2:, 0]) * (params.n - 1) / 2.0
    q_x[1:-1, -1] = (q[:-2, -1] - q[2:, -1]) * (params.n - 1) / 2.0
    #UNEXPLAINED SWITCHING in the second part of numerator in octave

    q_x = af.flat(q_x)
    q_y = af.flat(q_y)

    return q_x, q_y
Exemple #18
0
def load_distribution_function(self, file_name):
    """
    This function is used to load the distribution function from the
    dump file that was created by dump_distribution_function.

    Parameters
    ----------

    file_name : The distribution_function array will be loaded from this
                provided file name.

    Examples
    --------
    
    >> solver.load_distribution_function('distribution_function')
    
    The above statemant will load the distribution function data stored in the file
    distribution_function.h5 into self.f
    """
    viewer = PETSc.Viewer().createHDF5(file_name + '.h5', 
                                       PETSc.Viewer.Mode.READ, 
                                       comm=self._comm
                                      )
    self._glob_f.load(viewer)

    N_g = self.N_ghost
    self.f[:, N_g:-N_g, N_g:-N_g] = af.moddims(af.to_array(self._glob_f_array),
                                               self.N_p1 * self.N_p2 * self.N_p3,
                                               self.N_q1, self.N_q2
                                              )

    return
Exemple #19
0
    def _convert_to_p_expanded(self, array):
        """
        Since we are limited to use 4D arrays due to
        the bound from ArrayFire, we define 2 forms
        which can be used such that the computations may
        carried out along all dimensions necessary:

        q_expanded form:(N_p1 * N_p2 * N_p3, N_s, N_q1, N_q2)
        p_expanded form:(N_p1, N_p2, N_p3, N_s * N_q1 * N_q2)
        
        This function converts the input array from
        q_expanded to p_expanded form.
        """
        # Obtaining start coordinates for the local zone
        # Additionally, we also obtain the size of the local zone
        ((i_q1_start, i_q2_start), (N_q1_local,
                                    N_q2_local)) = self._da_f.getCorners()

        array = af.moddims(
            array, self.N_p1 + 2 * self.N_ghost_p,
            self.N_p2 + 2 * self.N_ghost_p, self.N_p3 + 2 * self.N_ghost_p,
            self.N_species * (N_q1_local + 2 * self.N_ghost_q) *
            (N_q2_local + 2 * self.N_ghost_q))

        af.eval(array)
        return (array)
def calculate_dfdp_background(self):
    """
    Calculates the derivative of the background distribution 
    with respect to the variables p1, p2, p3. This is used to
    solve for the contribution from the fields
    """
    f_b = af.moddims(self.f_background, self.N_p1, self.N_p2, self.N_p3)

    # Using a 4th order central difference stencil:
    dfdp1_background = (-af.shift(f_b, -2) + 8 * af.shift(f_b, -1) + af.shift(
        f_b, 2) - 8 * af.shift(f_b, 1)) / (12 * self.dp1)

    dfdp2_background = (-af.shift(f_b, 0, -2) + 8 * af.shift(f_b, 0, -1) +
                        af.shift(f_b, 0, 2) -
                        8 * af.shift(f_b, 0, 1)) / (12 * self.dp2)

    dfdp3_background = (-af.shift(f_b, 0, 0, -2) +
                        8 * af.shift(f_b, 0, 0, -1) + af.shift(f_b, 0, 0, 2) -
                        8 * af.shift(f_b, 0, 0, 1)) / (12 * self.dp3)

    # Reordering such that the variations in velocity are along axis 2
    self.dfdp1_background = af.reorder(af.flat(dfdp1_background), 2, 3, 0, 1)
    self.dfdp2_background = af.reorder(af.flat(dfdp2_background), 2, 3, 0, 1)
    self.dfdp3_background = af.reorder(af.flat(dfdp3_background), 2, 3, 0, 1)

    af.eval(self.dfdp1_background, self.dfdp2_background,
            self.dfdp3_background)

    return
Exemple #21
0
def fc(weights, biases, image):
    #TODO: fix shape of fc weights
    #TODO: fix handling 1d vs 3d image
    #TODO: remove assumption of only 1 input
    s = image.dims()[0]
    if len(image.dims()) is 3:
        in0, in1, in2 = image.dims()
        image = af.reorder(image, 1, 0, 2)
        s = in0*in1*in2

    image = af.moddims(image, 1, s)
    d0, d1, d2, d3 = weights.dims()
    weights = af.moddims(weights, d2, d3)
    out = af.moddims(af.matmul(image, weights), d3) + biases

    return out
Exemple #22
0
def volume_integral(u, gv):
    '''
    Vectorize, p, q, moddims.
    '''
    dLp_xi_ij_Lq_eta_ij = gv.dLp_Lq
    dLq_eta_ij_Lp_xi_ij = gv.dLq_Lp

    if (params.volume_integrand_scheme_2d == 'Lobatto'
            and params.N_LGL == params.N_quad):
        w_i = af.flat(
            af.transpose(
                af.tile(gv.lobatto_weights_quadrature, 1, params.N_LGL)))
        w_j = af.tile(gv.lobatto_weights_quadrature, params.N_LGL)
        wi_wj_dLp_xi = af.broadcast(utils.multiply, w_i * w_j, gv.dLp_Lq)
        volume_integrand_ij_1_sp = af.broadcast(utils.multiply,\
                                               wi_wj_dLp_xi, F_xi(u, gv) * gv.sqrt_g)
        wi_wj_dLq_eta = af.broadcast(utils.multiply, w_i * w_j, gv.dLq_Lp)
        volume_integrand_ij_2_sp = af.broadcast(utils.multiply,\
                                               wi_wj_dLq_eta, F_eta(u, gv) * gv.sqrt_g)

        volume_integral = af.reorder(
            af.sum(volume_integrand_ij_1_sp + volume_integrand_ij_2_sp, 0), 2,
            1, 0)

    else:  # NEEDS TO BE CHANGED
        volume_integrand_ij_1 = af.broadcast(utils.multiply,\
                                        dLp_xi_ij_Lq_eta_ij,\
                                        F_xi(u, gv))

        volume_integrand_ij_2 = af.broadcast(utils.multiply,\
                                             dLq_eta_ij_Lp_xi_ij,\
                                             F_eta(u, gv))

        volume_integrand_ij = af.moddims((volume_integrand_ij_1 + volume_integrand_ij_2)\
                                        * np.mean(gv.sqrt_det_g), params.N_LGL ** 2,\
                                         (params.N_LGL ** 2) * 100)

        lagrange_interpolation = af.moddims(
            lag_interpolation_2d(volume_integrand_ij, gv.Li_Lj_coeffs),
            params.N_LGL, params.N_LGL, params.N_LGL**2 * 100)

        volume_integrand_total = utils.integrate_2d_multivar_poly(lagrange_interpolation[:, :, :],\
                                                    params.N_quad,'gauss', gv)
        volume_integral = af.transpose(
            af.moddims(volume_integrand_total, 100, params.N_LGL**2))

    return volume_integral
Exemple #23
0
def simple_data(verbose=False):
    display_func = _util.display_func(verbose)

    display_func(af.constant(100, 3, 3, dtype=af.Dtype.f32))
    display_func(af.constant(25, 3, 3, dtype=af.Dtype.c32))
    display_func(af.constant(2**50, 3, 3, dtype=af.Dtype.s64))
    display_func(af.constant(2+3j, 3, 3))
    display_func(af.constant(3+5j, 3, 3, dtype=af.Dtype.c32))

    display_func(af.range(3, 3))
    display_func(af.iota(3, 3, tile_dims=(2, 2)))

    display_func(af.identity(3, 3, 1, 2, af.Dtype.b8))
    display_func(af.identity(3, 3, dtype=af.Dtype.c32))

    a = af.randu(3, 4)
    b = af.diag(a, extract=True)
    c = af.diag(a, 1, extract=True)

    display_func(a)
    display_func(b)
    display_func(c)

    display_func(af.diag(b, extract=False))
    display_func(af.diag(c, 1, extract=False))

    display_func(af.join(0, a, a))
    display_func(af.join(1, a, a, a))

    display_func(af.tile(a, 2, 2))

    display_func(af.reorder(a, 1, 0))

    display_func(af.shift(a, -1, 1))

    display_func(af.moddims(a, 6, 2))

    display_func(af.flat(a))

    display_func(af.flip(a, 0))
    display_func(af.flip(a, 1))

    display_func(af.lower(a, False))
    display_func(af.lower(a, True))

    display_func(af.upper(a, False))
    display_func(af.upper(a, True))

    a = af.randu(5, 5)
    display_func(af.transpose(a))
    af.transpose_inplace(a)
    display_func(a)

    display_func(af.select(a > 0.3, a, -0.3))

    af.replace(a, a > 0.3, -0.3)
    display_func(a)

    display_func(af.pad(a, (1, 1, 0, 0), (2, 2, 0, 0)))
Exemple #24
0
def simple_data(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    display_func(af.constant(100, 3,3, dtype=af.Dtype.f32))
    display_func(af.constant(25, 3,3, dtype=af.Dtype.c32))
    display_func(af.constant(2**50, 3,3, dtype=af.Dtype.s64))
    display_func(af.constant(2+3j, 3,3))
    display_func(af.constant(3+5j, 3,3, dtype=af.Dtype.c32))

    display_func(af.range(3, 3))
    display_func(af.iota(3, 3, tile_dims=(2,2)))

    display_func(af.identity(3, 3, 1, 2, af.Dtype.b8))
    display_func(af.identity(3, 3, dtype=af.Dtype.c32))

    a = af.randu(3, 4)
    b = af.diag(a, extract=True)
    c = af.diag(a, 1, extract=True)

    display_func(a)
    display_func(b)
    display_func(c)

    display_func(af.diag(b, extract = False))
    display_func(af.diag(c, 1, extract = False))

    display_func(af.join(0, a, a))
    display_func(af.join(1, a, a, a))

    display_func(af.tile(a, 2, 2))


    display_func(af.reorder(a, 1, 0))

    display_func(af.shift(a, -1, 1))

    display_func(af.moddims(a, 6, 2))

    display_func(af.flat(a))

    display_func(af.flip(a, 0))
    display_func(af.flip(a, 1))

    display_func(af.lower(a, False))
    display_func(af.lower(a, True))

    display_func(af.upper(a, False))
    display_func(af.upper(a, True))

    a = af.randu(5,5)
    display_func(af.transpose(a))
    af.transpose_inplace(a)
    display_func(a)

    display_func(af.select(a > 0.3, a, -0.3))

    af.replace(a, a > 0.3, -0.3)
    display_func(a)
Exemple #25
0
def _soft_l_value_demapper_af(rx_symbs, M, snr, bits_map):
    num_bits = int(np.log2(M))
    N = rx_symbs.shape[0]
    k = bits_map.shape[1]
    sig = af.np_to_af_array(rx_symbs)
    bit_mtx = af.moddims(af.np_to_af_array(bits_map), 1, num_bits, k, 2)
    tmp = af.sum(af.broadcast(lambda x,y: af.exp(-snr*af.abs(x-y)**2), bit_mtx, sig), dim=2)
    lvl = af.log(tmp[:,:,:,1]) - af.log(tmp[:,:,:,0])
    return np.array(lvl)
Exemple #26
0
def __pad(image, px, py):
    if px == 0 and py == 0:
        return image

    d = image.dims()
    pad_x = d[0]+2*px
    pad_y = d[1]+2*py
    return af.moddims(af.unwrap(image, pad_x, pad_y, pad_x, pad_y, px, py),
                      pad_x, pad_y, d[2])
def projection_step(u, v, A_projection):

    N_squared = params.n**2
    u_boundary_condition, v_boundary_condition = add_boundary_conditions(u, v)
    q = projection_step_poisson(u_boundary_condition, v_boundary_condition,
                                A_projection)
    gradient_q = grad_q(q, u, v)

    u_boundary_condition = af.flat(u_boundary_condition)
    v_boundary_condition = af.flat(v_boundary_condition)

    u4 = u_boundary_condition - gradient_q[0]
    v4 = v_boundary_condition - gradient_q[1]

    u4 = af.flat(af.moddims(u4, params.n, params.n)[1:-1, 1:-1])
    v4 = af.flat(af.moddims(v4, params.n, params.n)[1:-1, 1:-1])

    return u4, v4
Exemple #28
0
 def predict_proba1(self, X):
     near_locs, near_dists = af.vision.nearest_neighbour(X, self._data, self._dim, \
                                                         self._num_nearest, self._match_type)
     weights = self._get_neighbor_weights(near_dists)
     top_labels = af.moddims(self._labels[near_locs], \
                             get_dims(near_locs)[0], get_dims(near_locs)[1])
     accum_weights = af.scan_by_key(
         top_labels, weights)  # reduce by key would be more ideal
     probs, _ = af.imax(accum_weights, dim=0)
     return probs.T
Exemple #29
0
def contour_2d(u, index):
    '''
    '''
    color_levels = np.linspace(-1.1, 1.1, 100)
    u_plot = af.flip(af.moddims(u, params.N_LGL, params.N_LGL, 10, 10), 0)
    x_plot = af.flip(af.moddims(x_e_ij, params.N_LGL, params.N_LGL, 10, 10), 0)
    y_plot = af.flip(af.moddims(y_e_ij, params.N_LGL, params.N_LGL, 10, 10), 0)

    x_contour = af.np_to_af_array(
        np.zeros([params.N_LGL * 10, params.N_LGL * 10]))
    y_contour = af.np_to_af_array(
        np.zeros([params.N_LGL * 10, params.N_LGL * 10]))
    u_contour = af.np_to_af_array(
        np.zeros([params.N_LGL * 10, params.N_LGL * 10]))
    fig = pl.figure()
    #
    for i in range(100):
        p = int(i / 10)
        q = i - p * 10
        x_contour[p * params.N_LGL:params.N_LGL * (p + 1),\
                  q * params.N_LGL:params.N_LGL * (q + 1)] = x_plot[:, :, q, p]

        y_contour[p * params.N_LGL:params.N_LGL * (p + 1),\
                  q * params.N_LGL:params.N_LGL * (q + 1)] = y_plot[:, :, q, p]

        u_contour[p * params.N_LGL:params.N_LGL * (p + 1),\
                  q * params.N_LGL:params.N_LGL * (q + 1)] = u_plot[:, :, q, p]

    x_contour = np.array(x_contour)
    y_contour = np.array(y_contour)
    u_contour = np.array(u_contour)
    pl.contourf(x_contour,
                y_contour,
                u_contour,
                200,
                levels=color_levels,
                cmap='jet')
    pl.gca().set_aspect('equal')
    pl.colorbar()
    pl.title('Time = %.2f' % (index * 10 * delta_t_2d))
    fig.savefig('results/2D_Wave_images/%04d' % (index) + '.png')
    pl.close('all')
    return
Exemple #30
0
def lrn(image, size, alpha, beta):
    #TODO: Remove assumption of image.dims()[3] == 1
    d0, d1, d2 = image.dims()
    #TODO: remove reorder and moddims call
    image = af.moddims(af.reorder(image, 2, 0, 1), 1, d2, d0, d1)
    d0, d1, d2, d3 = image.dims()

    pad = (size - 1) / 2
    padded_size = d1+2*pad
    out = af.constant(0,d1, d2, d3)
    image = af.moddims(image, d1, d2, d3)
    padded = af.moddims(af.unwrap(image, padded_size, d2, padded_size, d2, pad,0), padded_size, d2, d2)
    padded = padded*padded
    for i in xrange(size):
        out += padded[i:i+d1]
    out = 1 + (float(alpha)/size) * out
    out = af.pow(out,beta)
    #TODO: remove reorder call
    return af.reorder(af.moddims(image/out, d0,d1,d2,d3), 2, 3, 1, 0)
Exemple #31
0
def initialize_f(q1, q2, p1, p2, p3, params):

    x = q1 * af.cos(q2)
    y = q1 * af.sin(q2)

    # Calculating the perturbed density:
    rho = af.exp(-(x**2 + (y - 1.5)**2)/0.25**2)
    f   = p1 * rho

    f[:] = 0

    f   = af.moddims(f, 32, 32, 70*70)
    rho = af.moddims(rho, 1, 1, 70*70)

    f[20, 20] = rho
    
    f = af.moddims(f, 32*32, 70, 70)

    af.eval(f)
    return (f)
a = af.randu(3, 4)
b = af.diag(a, extract=True)
c = af.diag(a, 1, extract=True)

af.display(a)
af.display(b)
af.display(c)

af.display(af.diag(b, extract = False))
af.display(af.diag(c, 1, extract = False))

af.display(af.tile(a, 2, 2))

af.display(af.reorder(a, 1, 0))

af.display(af.shift(a, -1, 1))

af.display(af.moddims(a, 6, 2))

af.display(af.flat(a))

af.display(af.flip(a, 0))
af.display(af.flip(a, 1))

af.display(af.lower(a, False))
af.display(af.lower(a, True))

af.display(af.upper(a, False))
af.display(af.upper(a, True))
a = af.randu(3, 4)
b = af.diag(a, extract=True)
c = af.diag(a, 1, extract=True)

af.print_array(a)
af.print_array(b)
af.print_array(c)

af.print_array(af.diag(b, extract = False))
af.print_array(af.diag(c, 1, extract = False))

af.print_array(af.tile(a, 2, 2))

af.print_array(af.reorder(a, 1, 0))

af.print_array(af.shift(a, -1, 1))

af.print_array(af.moddims(a, 6, 2))

af.print_array(af.flat(a))

af.print_array(af.flip(a, 0))
af.print_array(af.flip(a, 1))

af.print_array(af.lower(a, False))
af.print_array(af.lower(a, True))

af.print_array(af.upper(a, False))
af.print_array(af.upper(a, True))