Exemplo n.º 1
0
def matmul_3D(a, b):
    '''
    Finds the matrix multiplication of :math:`Q` pairs of matrices ``a`` and
    ``b``.

    Parameters
    ----------
    a : af.Array [M N Q 1]
        First set of :math:`Q` 2D arrays :math:`N \\neq 1` and :math:`M \\neq 1`.
    b : af.Array [N P Q 1]
        Second set of :math:`Q` 2D arrays :math:`P \\neq 1`.

    Returns
    -------
    matmul : af.Array [M P Q 1]
             Matrix multiplication of :math:`Q` sets of 2D arrays.
    '''
    shape_a = shape(a)
    shape_b = shape(b)

    P = shape_b[1]

    a = af.transpose(a)
    a = af.reorder(a, d0=0, d1=3, d2=2, d3=1)
    a = af.tile(a, d0=1, d1=P)
    b = af.tile(b, d0=1, d1=1, d2=1, d3=a.shape[3])

    matmul = af.sum(a * b, dim=0)
    matmul = af.reorder(matmul, d0=3, d1=1, d2=2, d3=0)

    return matmul
Exemplo n.º 2
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)

    # 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
Exemplo n.º 3
0
def test_dump_moments():
    test_obj = test()
    N_g      = test_obj.N_ghost

    dump_moments(test_obj, 'test_file')

    h5f          = h5py.File('test_file.h5', 'r')
    moments_read = h5f['moments'][:]
    h5f.close()

    moments_read = np.swapaxes(moments_read, 0, 1)

    print(moments_read.shape)
    print(compute_moments_imported(test_obj, 'density').shape)

    assert(af.sum(af.to_array(moments_read[:, :, 0]) - 
                  af.reorder(compute_moments_imported(test_obj, 'density'), 
                             1, 2, 0
                            )[N_g:-N_g, N_g:-N_g]
                 )==0
          )

    assert(af.sum(af.to_array(moments_read[:, :, 1]) - 
                  af.reorder(compute_moments_imported(test_obj, 'energy'),
                             1, 2, 0
                            )[N_g:-N_g, N_g:-N_g] 
                 )==0
          )
Exemplo n.º 4
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
Exemplo n.º 5
0
def polyval_2d(poly_2d, xi, eta):
    '''
    '''
    poly_2d_shape = poly_2d.shape
    poly_xy = af.tile(poly_2d, d0 = 1, d1 = 1, d2 = 1, d3 = xi.shape[0])
    poly_xy_shape = poly_xy.shape
    # print(poly_xy)

    xi_power = af.flip(af.range(poly_xy_shape[1], dtype = af.Dtype.u32))
    xi_power = af.tile(af.transpose(xi_power), d0 = poly_xy_shape[0])
    xi_power = af.tile(xi_power, d0 = 1, d1 = 1, d2 = xi.shape[0])

    eta_power = af.flip(af.range(poly_xy_shape[0], dtype = af.Dtype.u32))
    eta_power = af.tile(eta_power, d0 = 1, d1 = poly_xy_shape[1])
    eta_power = af.tile(eta_power, d0 = 1, d1 = 1, d2 = eta.shape[0])

    Xi = af.reorder(xi, d0 = 2, d1 = 1, d2 = 0)
    Xi = af.tile(Xi, d0 = poly_xy_shape[0], d1 = poly_xy_shape[1])
    Xi = af.pow(Xi, xi_power)
    Xi = af.reorder(Xi, d0 = 0, d1 = 1, d2 = 3, d3 = 2)
    # print(Xi)

    Eta = af.reorder(eta, d0 = 2, d1 = 1, d2 = 0)
    Eta = af.tile(Eta, d0 = poly_xy_shape[0], d1 = poly_xy_shape[1])
    Eta = af.pow(Eta, eta_power)
    Eta = af.reorder(Eta, d0 = 0, d1 = 1, d2 = 3, d3 = 2)
    # print(Eta)

    Xi_Eta = Xi * Eta

    poly_val = af.broadcast(multiply, poly_xy, Xi_Eta)
    poly_val = af.sum(af.sum(poly_val, dim = 1), dim = 0)
    poly_val = af.reorder(poly_val, d0 = 2, d1 = 3, d2 = 0, d3 = 1)

    return poly_val
Exemplo n.º 6
0
def calculate_q(q1_start, q2_start, N_q1, N_q2, N_g1, N_g2, dq1, dq2):

    i_q1 = np.arange(-N_g1, N_q1 + N_g1)
    i_q2 = np.arange(-N_g2, N_q2 + N_g2)

    q1_left_bot = q1_start + i_q1 * dq1
    q2_left_bot = q2_start + i_q2 * dq2

    q2_left_bot, q1_left_bot = np.meshgrid(q2_left_bot, q1_left_bot)
    q2_left_bot, q1_left_bot = af.to_array(q2_left_bot), af.to_array(
        q1_left_bot)

    # To bring the data structure to the default form:(N_p, N_s, N_q1, N_q2)
    q1_left_bot = af.reorder(q1_left_bot, 3, 2, 0, 1)
    q2_left_bot = af.reorder(q2_left_bot, 3, 2, 0, 1)

    q1_center_bot = q1_left_bot + 0.5 * dq1
    q2_center_bot = q2_left_bot

    q1_left_center = q1_left_bot
    q2_left_center = q2_left_bot + 0.5 * dq2

    q1_center = q1_left_bot + 0.5 * dq1
    q2_center = q2_left_bot + 0.5 * dq2

    ans = [[q1_left_bot, q2_left_bot], [q1_center_bot, q2_center_bot],
           [q1_left_center, q2_center_bot], [q1_center, q2_center]]

    return (ans)
Exemplo n.º 7
0
def test_polynomial_product_coeffs():
    '''
    '''
    threshold = 1e-12

    poly1 = af.reorder(
        af.transpose(
            af.np_to_af_array(np.array([[1, 2, 3., 4], [5, -2, -4.7211, 2]]))),
        0, 2, 1)

    poly2 = af.reorder(
        af.transpose(
            af.np_to_af_array(np.array([[-2, 4, 7., 9], [1, 0, -9.1124, 7]]))),
        0, 2, 1)

    numerical_product_coeffs = utils.polynomial_product_coeffs(poly1, poly2)
    analytical_product_coeffs_1 = af.np_to_af_array(
        np.array([[-2, -4, -6, -8], [4, 8, 12, 16], [7, 14, 21, 28],
                  [9, 18, 27, 36]]))

    analytical_product_coeffs_2 = af.np_to_af_array(
        np.array([[5, -2, -4.7211, 2], [0, 0, 0, 0],
                  [-45.562, 18.2248, 43.02055164, -18.2248],
                  [35, -14, -33.0477, 14]]))

    print(numerical_product_coeffs)
    assert af.max(af.abs(numerical_product_coeffs[:, :, 0] - analytical_product_coeffs_1 + \
                  numerical_product_coeffs[:, :, 1] - analytical_product_coeffs_2)) < threshold
Exemplo n.º 8
0
def initialize_f(q1, q2, p1, p2, p3, params):

    m = params.mass_particle
    k = params.boltzmann_constant

    rho = af.select(af.abs(q2) > 0.25, q1**0, 2)

    # Seeding the instability
    p1_bulk = af.reorder(p1_bulk, 1, 2, 0, 3)
    p1_bulk += af.to_array(
        np.random.rand(1, q1.shape[1], q1.shape[2]) *
        np.random.choice([-1, 1], size=(1, q1.shape[1], q1.shape[2]))) * 0.005

    p2_bulk = af.to_array(
        np.random.rand(1, q1.shape[1], q1.shape[2]) *
        np.random.choice([-1, 1], size=(1, q1.shape[1], q1.shape[2]))) * 0.005

    p1_bulk = af.reorder(p1_bulk, 2, 0, 1, 3)
    p2_bulk = af.reorder(p2_bulk, 2, 0, 1, 3)

    T = (2.5 / rho)

    f = rho * (m / (2 * np.pi * k * T))**(3 / 2) \
            * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T)) \
            * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T)) \
            * af.exp(-m * (p3)**2 / (2 * k * T))

    af.eval(f)
    return (f)
Exemplo n.º 9
0
    def _calculate_p_center(self):
        """
        Initializes the cannonical variables p1, p2 and p3 using a centered
        formulation. The size, and resolution are the same as declared
        under domain of the physical system object.
        """
        p1_center = \
            self.p1_start + (0.5 + np.arange(0, self.N_p1, 1)) * self.dp1
        
        p2_center = \
            self.p2_start + (0.5 + np.arange(0, self.N_p2, 1)) * self.dp2
        
        p3_center = \
            self.p3_start + (0.5 + np.arange(0, self.N_p3, 1)) * self.dp3

        p2_center, p1_center, p3_center = np.meshgrid(p2_center,
                                                      p1_center,
                                                      p3_center
                                                     )

        # Flattening the obtained arrays:
        p1_center = af.flat(af.to_array(p1_center))
        p2_center = af.flat(af.to_array(p2_center))
        p3_center = af.flat(af.to_array(p3_center))

        # Reordering such that variation in velocity is along axis 2:
        # This is done to be consistent with the positionsExpanded form:
        p1_center = af.reorder(p1_center, 2, 3, 0, 1)
        p2_center = af.reorder(p2_center, 2, 3, 0, 1)
        p3_center = af.reorder(p3_center, 2, 3, 0, 1)

        af.eval(p1_center, p2_center, p3_center)
        return(p1_center, p2_center, p3_center)
Exemplo n.º 10
0
def lagrange_interpolation(fn_i):
    '''
    Finds the general interpolation of a function.
    
    Parameters
    ----------
    fn_i : af.Array [N N_LGL 1 1]
           Value of :math:`N` functions at the LGL points.
    
    Returns
    -------
    lagrange_interpolation : af.Array [N N_LGL 1 1]
                             :math:`N` interpolated polynomials for
                             :math:`N` functions.
    '''

    fn_i = af.transpose(af.reorder(fn_i, d0=2, d1=1, d2=0))
    lagrange_interpolation = af.broadcast(utils.multiply,
                                          params.lagrange_coeffs, fn_i)
    lagrange_interpolation = af.reorder(af.sum(lagrange_interpolation, dim=0),
                                        d0=2,
                                        d1=1,
                                        d2=0)

    return lagrange_interpolation
Exemplo n.º 11
0
    def __init__(self):
        
        self.q1_start = np.random.randint(0, 5)
        self.q2_start = np.random.randint(0, 5)

        self.q1_end = np.random.randint(5, 10)
        self.q2_end = np.random.randint(5, 10)

        self.N_q1 = np.random.randint(16, 32)
        self.N_q2 = np.random.randint(16, 32)

        self.dq1 = (self.q1_end - self.q1_start) / self.N_q1
        self.dq2 = (self.q2_end - self.q2_start) / self.N_q2

        N_g = self.N_ghost = np.random.randint(1, 5)

        self.q1 = self.q1_start \
                  * (0.5 + np.arange(-self.N_ghost,
                                     self.N_q1 + self.N_ghost
                                    )
                    ) * self.dq1

        self.q2 = self.q2_start \
                  * (0.5 + np.arange(-self.N_ghost,
                                      self.N_q2 + self.N_ghost
                                    )
                    ) * self.dq2

        self.q2, self.q1 = np.meshgrid(self.q2, self.q1)
        self.q1, self.q2 = af.to_array(self.q1), af.to_array(self.q2)

        self.q1 = af.reorder(self.q1, 2, 0, 1)
        self.q2 = af.reorder(self.q2, 2, 0, 1)

        self.q1 = af.tile(self.q1, 6)
        self.q2 = af.tile(self.q2, 6)

        self._da_fields = PETSc.DMDA().create([self.N_q1, self.N_q2],
                                              dof=6,
                                              stencil_width=self.N_ghost,
                                              boundary_type=('periodic',
                                                             'periodic'),
                                              stencil_type=1,
                                             )

        self._glob_fields  = self._da_fields.createGlobalVec()
        self._local_fields = self._da_fields.createLocalVec()

        self._glob_fields_array  = self._glob_fields.getArray()
        self._local_fields_array = self._local_fields.getArray()

        self.cell_centered_EM_fields = af.constant(0, 6, self.q1.shape[1], 
                                                   self.q1.shape[2],
                                                   dtype=af.Dtype.f64
                                                  )

        self.cell_centered_EM_fields[:, N_g:-N_g, N_g:-N_g] = \
            af.sin(2 * np.pi * self.q1 + 4 * np.pi * self.q2)[:, N_g:-N_g,N_g:-N_g]
        
        self.performance_test_flag = False
Exemplo n.º 12
0
def test_calculate_p():
    obj = test()

    p1, p2, p3 = calculate_p(obj)

    p1_expected = obj.p1_start + (0.5 + np.arange(obj.N_p1)) * obj.dp1
    p2_expected = obj.p2_start + (0.5 + np.arange(obj.N_p2)) * obj.dp2
    p3_expected = obj.p3_start + (0.5 + np.arange(obj.N_p3)) * obj.dp3

    p2_expected, p1_expected, p3_expected = np.meshgrid(p2_expected,
                                                        p1_expected,
                                                        p3_expected
                                                       )
    
    p1_expected = af.reorder(af.flat(af.to_array(p1_expected)),
                             2, 3, 0, 1
                            )
                          
    p2_expected = af.reorder(af.flat(af.to_array(p2_expected)),
                             2, 3, 0, 1
                            )
                         
    p3_expected = af.reorder(af.flat(af.to_array(p3_expected)),
                             2, 3, 0, 1
                            )

    assert(af.sum(af.abs(p1_expected - p1)) == 0)
    assert(af.sum(af.abs(p2_expected - p2)) == 0)
    assert(af.sum(af.abs(p3_expected - p3)) == 0)
Exemplo n.º 13
0
    def _calculate_q_center(self):
        """
        Initializes the cannonical variables q1, q2 using a centered
        formulation. The size, and resolution are the same as declared
        under domain of the physical system object.

        Returns in q_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()

        i_q1_center = i_q1_start + 0.5
        i_q2_center = i_q2_start + 0.5

        i_q1 = (i_q1_center +
                np.arange(-self.N_ghost_q, N_q1_local + self.N_ghost_q))

        i_q2 = (i_q2_center +
                np.arange(-self.N_ghost_q, N_q2_local + self.N_ghost_q))

        q1_center = self.q1_start + i_q1 * self.dq1
        q2_center = self.q2_start + i_q2 * self.dq2

        q2_center, q1_center = np.meshgrid(q2_center, q1_center)
        q1_center, q2_center = af.to_array(q1_center), af.to_array(q2_center)

        # To bring the data structure to the default form:(N_p, N_s, N_q1, N_q2)
        q1_center = af.reorder(q1_center, 3, 2, 0, 1)
        q2_center = af.reorder(q2_center, 3, 2, 0, 1)

        af.eval(q1_center, q2_center)
        return (q1_center, q2_center)
Exemplo n.º 14
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))
    ])
Exemplo n.º 15
0
def fft2(array):
    # Reorder from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s) 
    array = af.reorder(array, 2, 3, 0, 1)
    array = af.fft2(array)
    # Reorder back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2) 
    array = af.reorder(array, 2, 3, 0, 1)

    af.eval(array)
    return(array)
Exemplo n.º 16
0
def ifft2(array):
    # Reorder from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s) 
    array = af.reorder(array, 2, 3, 0, 1)
    array = af.ifft2(array, scale=1)/(array.shape[0] * array.shape[1]) # fix for https://github.com/arrayfire/arrayfire/issues/2050
    # Reorder back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2) 
    array = af.reorder(array, 2, 3, 0, 1)

    af.eval(array)
    return(array)
Exemplo n.º 17
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)
Exemplo n.º 18
0
    def __init__(self, N):
        self.q1_start = 0
        self.q2_start = 0

        self.q1_end = 1
        self.q2_end = 1

        self.N_q1 = N
        self.N_q2 = N

        self.dq1 = (self.q1_end - self.q1_start) / self.N_q1
        self.dq2 = (self.q2_end - self.q2_start) / self.N_q2

        self.N_ghost = np.random.randint(3, 5)

        self.q1 = self.q1_start \
            + (0.5 + np.arange(-self.N_ghost,self.N_q1 + self.N_ghost)) * self.dq1

        self.q2 = self.q2_start \
            + (0.5 + np.arange(-self.N_ghost,self.N_q2 + self.N_ghost)) * self.dq2

        self.q2, self.q1 = np.meshgrid(self.q2, self.q1)
        self.q2, self.q1 = af.to_array(self.q2), af.to_array(self.q1)

        self.q1 = af.reorder(self.q1, 2, 0, 1)
        self.q2 = af.reorder(self.q2, 2, 0, 1)

        self.yee_grid_EM_fields = af.constant(0,
                                              6,
                                              self.q1.shape[1],
                                              self.q1.shape[2],
                                              dtype=af.Dtype.f64)

        self._da_fields = PETSc.DMDA().create(
            [self.N_q1, self.N_q2],
            dof=6,
            stencil_width=self.N_ghost,
            boundary_type=('periodic', 'periodic'),
            stencil_type=1,
        )

        self._glob_fields = self._da_fields.createGlobalVec()
        self._local_fields = self._da_fields.createLocalVec()

        self._glob_fields_array = self._glob_fields.getArray()
        self._local_fields_array = self._local_fields.getArray()

        self.boundary_conditions = type('obj', (object, ), {
            'in_q1': 'periodic',
            'in_q2': 'periodic'
        })

        self.performance_test_flag = False
Exemplo n.º 19
0
    def __init__(self):
        self.physical_system = type('obj', (object, ), {
            'moment_exponents': moment_exponents,
            'moment_coeffs': moment_coeffs
        })
        self.p1_start = -10
        self.p2_start = -10
        self.p3_start = -10

        self.N_p1 = 32
        self.N_p2 = 32
        self.N_p3 = 32

        self.dp1 = (-2 * self.p1_start) / self.N_p1
        self.dp2 = (-2 * self.p2_start) / self.N_p2
        self.dp3 = (-2 * self.p3_start) / self.N_p3

        self.N_q1 = 16
        self.N_q2 = 16

        self.N_ghost = 3

        self.p1 = self.p1_start + (0.5 + np.arange(self.N_p1)) * self.dp1
        self.p2 = self.p2_start + (0.5 + np.arange(self.N_p2)) * self.dp2
        self.p3 = self.p3_start + (0.5 + np.arange(self.N_p3)) * self.dp3

        self.p2, self.p1, self.p3 = np.meshgrid(self.p2, self.p1, self.p3)

        self.p1 = af.flat(af.to_array(self.p1))
        self.p2 = af.flat(af.to_array(self.p2))
        self.p3 = af.flat(af.to_array(self.p3))

        self.q1 = (-self.N_ghost + 0.5 +
                   np.arange(self.N_q1 + 2 * self.N_ghost)) / self.N_q1

        self.q2 = (-self.N_ghost + 0.5 +
                   np.arange(self.N_q2 + 2 * self.N_ghost)) / self.N_q2

        self.q2, self.q1 = np.meshgrid(self.q2, self.q1)

        self.q1 = af.reorder(af.to_array(self.q1), 2, 0, 1)
        self.q2 = af.reorder(af.to_array(self.q2), 2, 0, 1)

        rho = (1 + 0.01 * af.sin(2 * np.pi * self.q1 + 4 * np.pi * self.q2))
        T = (1 + 0.01 * af.cos(2 * np.pi * self.q1 + 4 * np.pi * self.q2))

        p1_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2)
        p2_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2)
        p3_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2)

        self.f = maxwell_boltzmann(rho, T, p1_b, p2_b, p3_b, self.p1, self.p2,
                                   self.p3)
Exemplo n.º 20
0
def fft_poisson(self, f=None):
    """
    Solves the Poisson Equation using the FFTs:

    Used as a backup solver in case of low resolution runs
    (ie. used on a single node) with periodic boundary
    conditions.
    """
    if (self.performance_test_flag == True):
        tic = af.time()

    if (self._comm.size != 1):
        raise Exception('FFT solver can only be used when run in serial')

    else:
        N_g = self.N_ghost
        rho = af.reorder(  self.physical_system.params.charge_electron \
                         * self.compute_moments('density', f)[:, N_g:-N_g, N_g:-N_g],
                         1, 2, 0
                        )

        k_q1 = fftfreq(rho.shape[0], self.dq1)
        k_q2 = fftfreq(rho.shape[1], self.dq2)

        k_q2, k_q1 = np.meshgrid(k_q2, k_q1)

        k_q1 = af.to_array(k_q1)
        k_q2 = af.to_array(k_q2)

        rho_hat = af.fft2(rho)

        potential_hat = rho_hat / (4 * np.pi**2 * (k_q1**2 + k_q2**2))
        potential_hat[0, 0] = 0

        E1_hat = -1j * 2 * np.pi * k_q1 * potential_hat
        E2_hat = -1j * 2 * np.pi * k_q2 * potential_hat

        # Non-inclusive of ghost-zones:
        E1_physical = af.reorder(af.real(af.ifft2(E1_hat)), 2, 0, 1)
        E2_physical = af.reorder(af.real(af.ifft2(E2_hat)), 2, 0, 1)

        self.cell_centered_EM_fields[0, N_g:-N_g, N_g:-N_g] = E1_physical
        self.cell_centered_EM_fields[1, N_g:-N_g, N_g:-N_g] = E2_physical

        af.eval(self.cell_centered_EM_fields)

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

    return
Exemplo n.º 21
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)
Exemplo n.º 22
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
Exemplo n.º 23
0
def Li_Lj_coeffs(N_LGL):
    '''
    '''
    xi_LGL = lagrange.LGL_points(N_LGL)
    lagrange_coeffs = af.np_to_af_array(
        lagrange.lagrange_polynomials(xi_LGL)[1])

    Li_xi = af.moddims(af.tile(af.reorder(lagrange_coeffs, 1, 2, 0), 1, N_LGL),
                       N_LGL, 1, N_LGL**2)

    Lj_eta = af.tile(af.reorder(lagrange_coeffs, 1, 2, 0), 1, 1, N_LGL)

    Li_Lj_coeffs = utils.polynomial_product_coeffs(Li_xi, Lj_eta)

    return Li_Lj_coeffs
Exemplo n.º 24
0
def lagrange_interpolation_u(u, gv):
    '''
    Calculates the coefficients of the Lagrange interpolation using
    the value of u at the mapped LGL points in the domain.
    The interpolation using the Lagrange basis polynomials is given by
    :math:`L_i(\\xi) u_i(\\xi)`
    Where L_i are the Lagrange basis polynomials and u_i is the value
    of u at the LGL points.
    
    Parameters
    ----------
    u : arrayfire.Array [N_LGL N_Elements 1 1]
        The value of u at the mapped LGL points.
        
    Returns
    -------
    lagrange_interpolated_coeffs : arrayfire.Array[1 N_LGL N_Elements 1]
                                   The coefficients of the polynomials obtained
                                   by Lagrange interpolation. Each polynomial
                                   is of order N_LGL - 1.
    '''
    lagrange_coeffs_tile = af.tile(gv.lagrange_coeffs, 1, 1,\
                                               params.N_Elements)
    reordered_u = af.reorder(u, 0, 2, 1)

    lagrange_interpolated_coeffs = af.sum(af.broadcast(utils.multiply,\
                                             reordered_u, lagrange_coeffs_tile), 0)

    return lagrange_interpolated_coeffs
Exemplo n.º 25
0
def test_calculate_q():
    obj = test()
    q1, q2 = calculate_q_center(obj)

    q1_expected = obj.q1_start + \
        (0.5 + np.arange(-obj.N_ghost, obj.N_q1 + obj.N_ghost)) * obj.dq1
    q2_expected = obj.q2_start + \
        (0.5 + np.arange(-obj.N_ghost, obj.N_q2 + obj.N_ghost)) * obj.dq2

    q2_expected, q1_expected = np.meshgrid(q2_expected, q1_expected)

    q1_expected = af.reorder(af.to_array(q1_expected), 2, 0, 1)
    q2_expected = af.reorder(af.to_array(q2_expected), 2, 0, 1)

    assert (af.sum(af.abs(q1_expected - q1)) == 0)
    assert (af.sum(af.abs(q2_expected - q2)) == 0)
Exemplo n.º 26
0
    def __init__(self):
        self.physical_system = type('obj', (object, ),
                                    {'params': type('obj', (object, ),
                                        {'charge_electron': -1})
                                     })

        self.N_q1 = 32
        self.N_q2 = 64

        self.single_mode_evolution = False

        self.N_p1 = 2
        self.N_p2 = 3
        self.N_p3 = 4

        self.k_q1 = 2 * np.pi * fftfreq(self.N_q1, 1 / self.N_q1)
        self.k_q2 = 2 * np.pi * fftfreq(self.N_q2, 1 / self.N_q2)

        self.k_q2, self.k_q1 = np.meshgrid(self.k_q2, self.k_q1)
        self.k_q2, self.k_q1 = af.to_array(self.k_q2), af.to_array(self.k_q1)

        self.q1 = af.to_array((0.5 + np.arange(self.N_q1)) * (1 / self.N_q1))
        self.q2 = af.to_array((0.5 + np.arange(self.N_q2)) * (1 / self.N_q2))

        self.q1 = af.tile(self.q1, 1, self.N_q2)
        self.q2 = af.tile(af.reorder(self.q2), self.N_q1, 1)
Exemplo n.º 27
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)))
Exemplo n.º 28
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)
Exemplo n.º 29
0
def test_1V():

    obj = test()

    obj.single_mode_evolution = False

    f_generalized = MB_dist(obj.q1_center, obj.q2_center, obj.p1, obj.p2,
                            obj.p3, 1)

    C_f_hat_generalized = 2 * af.fft2(
        collision_operator.BGK(
            f_generalized, obj.q1_center, obj.q2_center, obj.p1, obj.p2,
            obj.p3, obj.compute_moments,
            obj.physical_system.params)) / (obj.N_q2 * obj.N_q1)

    # Background
    C_f_hat_generalized[0, 0, :] = 0

    # Finding the indices of the mode excited:
    i_q1_max = np.unravel_index(
        af.imax(af.abs(C_f_hat_generalized))[1],
        (obj.N_q1, obj.N_q2, obj.N_p1 * obj.N_p2 * obj.N_p3),
        order='F')[0]

    i_q2_max = np.unravel_index(
        af.imax(af.abs(C_f_hat_generalized))[1],
        (obj.N_q1, obj.N_q2, obj.N_p1 * obj.N_p2 * obj.N_p3),
        order='F')[1]

    obj.p1 = np.array(af.reorder(obj.p1, 1, 2, 3, 0))
    obj.p2 = np.array(af.reorder(obj.p2, 1, 2, 3, 0))
    obj.p3 = np.array(af.reorder(obj.p3, 1, 2, 3, 0))

    delta_f_hat = 0.01 * (1 / (2 * np.pi))**(1 / 2) \
                       * np.exp(-0.5 * obj.p1**2)

    obj.single_mode_evolution = True

    C_f_hat_single_mode = collision_operator.linearized_BGK(
        delta_f_hat, obj.p1, obj.p2, obj.p3, obj.compute_moments,
        obj.physical_system.params)

    assert (af.mean(
        af.abs(
            af.flat(C_f_hat_generalized[i_q1_max, i_q2_max]) -
            af.to_array(C_f_hat_single_mode.flatten()))) < 1e-14)
Exemplo n.º 30
0
def calculate_k(N_q1, N_q2, dq1, dq2):
    """
    Initializes the wave numbers k_q1 and k_q2 which will be 
    used when solving in fourier space.
    """
    k_q1 = 2 * np.pi * np.fft.fftfreq(N_q1, dq1)
    k_q2 = 2 * np.pi * np.fft.fftfreq(N_q2, dq2)

    k_q2, k_q1 = np.meshgrid(k_q2, k_q1)

    k_q1 = af.to_array(k_q1)
    k_q2 = af.to_array(k_q2)

    k_q1 = af.reorder(k_q1, 2, 3, 0, 1)
    k_q2 = af.reorder(k_q2, 2, 3, 0, 1)

    af.eval(k_q1, k_q2)
    return (k_q1, k_q2)
Exemplo n.º 31
0
def reorder(a: ndarray, new_order: tp.Tuple[int, ...]):
    if len(new_order) > 4:
        raise ValueError(
            f'Cocos does not support arrays with more than 4 axes.')

    d0, d1, d2, d3 = _pad_shape_tuple_axis(new_order)
    # print(f'd0={d0}, d1={d1}, d2={d2}, d3={d3}')

    output_array = af.reorder(a._af_array, d0, d1, d2, d3)
    return ndarray(output_array)
Exemplo n.º 32
0
    def transpose(self, *axes):
        if(self.ndim == 1):
            return self
        if len(axes) == 0 and self.ndim == 2:
            s = arrayfire.transpose(self.d_array)
        else:
            order = [0,1,2,3]
            if len(axes) == 0 or axes[0] is None:
                order[:self.ndim] = order[:self.ndim][::-1]
            else:
                if isinstance(axes[0], collections.Iterable):
                    axes = axes[0]
                for i,ax in enumerate(axes):
                    order[i] = pu.c2f(self.shape, ax)
                # We have to do this gymnastic due to the fact that arrayfire
                # uses Fortran order
                order[:len(axes)] = order[:len(axes)][::-1]

            #print order
            s = arrayfire.reorder(self.d_array, order[0],order[1],order[2],order[3])
        return ndarray(pu.af_shape(s), dtype=self.dtype, af_array=s)
Exemplo n.º 33
0
af.display(af.identity(3, 3, dtype=af.c32))

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))
Exemplo n.º 34
0
af.print_array(af.identity(3, 3, dtype=af.c32))

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))