Пример #1
0
def f_interp_2d(self, dt):
    
    if(self.performance_test_flag == True):
        tic = af.time()

    # Defining a lambda function to perform broadcasting operations
    # This is done using af.broadcast, which allows us to perform 
    # batched operations when operating on arrays of different sizes
    addition = lambda a, b:a + b

    # af.broadcast(function, *args) performs batched operations on
    # function(*args)
    q1_center_new = af.broadcast(addition, self.q1_center, - self._A_q1 * dt)
    q2_center_new = af.broadcast(addition, self.q2_center, - self._A_q2 * dt)

    # Reordering from (dof, N_q1, N_q2) --> (N_q1, N_q2, dof)
    self.f = af.approx2(af.reorder(self.f, 1, 2, 0),
                        af.reorder(q1_center_new, 1, 2, 0),
                        af.reorder(q2_center_new, 1, 2, 0),
                        af.INTERP.BICUBIC_SPLINE, 
                        xp = af.reorder(self.q1_center, 1, 2, 0),
                        yp = af.reorder(self.q2_center, 1, 2, 0)
                       )

    # Reordering from (N_q1, N_q2, dof) --> (dof, N_q1, N_q2)
    self.f = af.reorder(self.f, 2, 0, 1)

    af.eval(self.f)

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

    return
Пример #2
0
def f_interp_2d(self, dt):
    """
    Performs 2D interpolation in the q-space to solve for the equation:
    
    df/dt + A_q1 df/dq1 + A_q2 df/dq2 = 0

    This is done by backtracing the characteristic curves
    and interpolating at the origin of the characteristics.

    Parameters
    ----------

    dt : double
         Time-step size to evolve the system
    """
    if(self.performance_test_flag == True):
        tic = af.time()

    A_q1, A_q2 = af.broadcast(self._A_q, self.f, self.time_elapsed, 
                              self.q1_center, self.q2_center,
                              self.p1_center, self.p2_center, self.p3_center,
                              self.physical_system.params
                             )

    # Using the add method wrapped with af.broadcast
    q1_center_new = add(self.q1_center, - A_q1 * dt)
    q2_center_new = add(self.q2_center, - A_q2 * dt)

    # Reordering from (dof, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_s, dof)
    # NOTE: To be changed after the implementation of axes specific 
    # interpolation operators gets completed from ArrayFire's end.
    # Ref:https://github.com/arrayfire/arrayfire/issues/1955
    self.f = af.approx2(af.reorder(self.f, 2, 3, 1, 0),
                        af.reorder(q1_center_new, 2, 3, 1, 0),
                        af.reorder(q2_center_new, 2, 3, 1, 0),
                        af.INTERP.BICUBIC_SPLINE, 
                        xp = af.reorder(self.q1_center, 2, 3, 1, 0),
                        yp = af.reorder(self.q2_center, 2, 3, 1, 0)
                       )

    # Reordering from (N_q1, N_q2, N_s, dof) --> (dof, N_s, N_q1, N_q2)
    self.f = af.reorder(self.f, 3, 2, 0, 1)

    af.eval(self.f)

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

    return
Пример #3
0
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af

a = af.randu(10, 1)
pos0 = af.randu(10) * 10
af.display(af.approx1(a, pos0))

a = af.randu(3, 3)
pos0 = af.randu(3, 3) * 10
pos1 = af.randu(3, 3) * 10

af.display(af.approx2(a, pos0, pos1))

a = af.randu(8, 1)
af.display(a)

af.display(af.fft(a))
af.display(af.dft(a))
af.display(af.real(af.ifft(af.fft(a))))
af.display(af.real(af.idft(af.dft(a))))

a = af.randu(4, 4)
af.display(a)

af.display(af.fft2(a))
af.display(af.dft(a))
af.display(af.real(af.ifft2(af.fft2(a))))
Пример #4
0
def simple_signal(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    a = af.randu(10, 1)
    pos0 = af.randu(10) * 10
    display_func(af.approx1(a, pos0))

    a = af.randu(3, 3)
    pos0 = af.randu(3, 3) * 10
    pos1 = af.randu(3, 3) * 10

    display_func(af.approx2(a, pos0, pos1))

    a = af.randu(8, 1)
    display_func(a)

    display_func(af.fft(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft(af.fft(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(4, 4)
    display_func(a)

    display_func(af.fft2(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft2(af.fft2(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(4, 4, 2)
    display_func(a)

    display_func(af.fft3(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft3(af.fft3(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(10, 1)
    b = af.randu(3, 1)
    display_func(af.convolve1(a, b))
    display_func(af.fft_convolve1(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5)
    b = af.randu(3, 3)
    display_func(af.convolve2(a, b))
    display_func(af.fft_convolve2(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5, 3)
    b = af.randu(3, 3, 2)
    display_func(af.convolve3(a, b))
    display_func(af.fft_convolve3(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))


    b = af.randu(3, 1)
    x = af.randu(10, 1)
    a = af.randu(2, 1)
    display_func(af.fir(b, x))
    display_func(af.iir(b, a, x))
Пример #5
0
def apply_shearing_box_bcs_fields(self, boundary, on_fdtd_grid):
    """
    Applies the shearing box boundary conditions along boundary specified 
    for the EM fields
    
    Parameters
    ----------
    boundary: str
              Boundary along which the boundary condition is to be applied.
    
    on_fdtd_grid: bool
                  Flag which dictates if boundary conditions are to be applied to the 
                  fields on the Yee grid or on the cell centered grid.
    """

    N_g_q = self.N_ghost_q
    q = self.physical_system.params.q
    omega = self.physical_system.params.omega

    L_q1 = self.q1_end - self.q1_start
    L_q2 = self.q2_end - self.q2_start

    if (boundary == 'left'):
        sheared_coordinates = self.q2_center[:, :, :
                                             N_g_q] - q * omega * L_q1 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q2_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q2_end,
                                            sheared_coordinates - L_q2,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q2_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q2_start,
                sheared_coordinates + L_q2, sheared_coordinates)
        if (on_fdtd_grid == True):
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.yee_grid_EM_fields[:, :, :N_g_q] = \
                af.reorder(af.approx2(af.reorder(self.yee_grid_EM_fields[:, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(self.q1_center[:, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :N_g_q], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :N_g_q], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

        else:
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.cell_centered_EM_fields[:, :, :N_g_q] = \
                af.reorder(af.approx2(af.reorder(self.cell_centered_EM_fields[:, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(self.q1_center[:, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :N_g_q], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :N_g_q], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

    elif (boundary == 'right'):
        sheared_coordinates = self.q2_center[:, :,
                                             -N_g_q:] + q * omega * L_q1 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q2_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q2_end,
                                            sheared_coordinates - L_q2,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q2_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q2_start,
                sheared_coordinates + L_q2, sheared_coordinates)

        if (on_fdtd_grid == True):
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.yee_grid_EM_fields[:, :, -N_g_q:] = \
                af.reorder(af.approx2(af.reorder(self.yee_grid_EM_fields[:, :, -N_g_q:], 2, 3, 0, 1),
                                      af.reorder(self.q1_center[:, :, -N_g_q:], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, -N_g_q:], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, -N_g_q:], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

        else:
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.cell_centered_EM_fields[:, :, -N_g_q:] = \
                af.reorder(af.approx2(af.reorder(self.cell_centered_EM_fields[:, :, -N_g_q:],2, 3, 0, 1),
                                      af.reorder(self.q1_center[:, :, -N_g_q:], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, -N_g_q:], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, -N_g_q:], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

    elif (boundary == 'bottom'):

        sheared_coordinates = self.q1_center[:, :, :, :
                                             N_g_q] - q * omega * L_q2 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q1_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q1_end,
                                            sheared_coordinates - L_q1,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q1_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q1_start,
                sheared_coordinates + L_q1, sheared_coordinates)

        if (on_fdtd_grid == True):
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.yee_grid_EM_fields[:, :, :, :N_g_q] = \
                af.reorder(af.approx2(af.reorder(self.yee_grid_EM_fields[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.reorder(self.q2_center[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :, :N_g_q], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

        else:
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.cell_centered_EM_fields[:, :, :, :N_g_q] = \
                af.reorder(af.approx2(af.reorder(self.cell_centered_EM_fields[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.reorder(self.q2_center[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :, :N_g_q], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :, :N_g_q], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

    elif (boundary == 'top'):

        sheared_coordinates = self.q1_center[:, :, :,
                                             -N_g_q:] + q * omega * L_q2 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q1_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q1_end,
                                            sheared_coordinates - L_q1,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q1_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q1_start,
                sheared_coordinates + L_q1, sheared_coordinates)

        if (on_fdtd_grid == True):
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.yee_grid_EM_fields[:, :, :, -N_g_q:] = \
                af.reorder(af.approx2(af.reorder(self.yee_grid_EM_fields[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.reorder(self.q2_center[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :, -N_g_q:], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

        else:
            # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
            # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)
            self.cell_centered_EM_fields[:, :, :, -N_g_q:] = \
                af.reorder(af.approx2(af.reorder(self.cell_centered_EM_fields[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      af.reorder(sheared_coordinates, 2, 3, 0, 1),
                                      af.reorder(self.q2_center[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      af.INTERP.BICUBIC_SPLINE,
                                      xp = af.reorder(self.q1_center[:, :, :, -N_g_q:], 2, 3, 0, 1),
                                      yp = af.reorder(self.q2_center[:, :, :, -N_g_q:], 2, 3, 0, 1)
                                     ),
                           2, 3, 0, 1
                          )

    else:
        raise Exception('Invalid choice for boundary')

    return
Пример #6
0
def simple_signal(verbose=False):
    display_func = _util.display_func(verbose)
    print_func = _util.print_func(verbose)

    a = af.randu(10, 1)
    pos0 = af.randu(10) * 10
    display_func(af.approx1(a, pos0))

    a = af.randu(3, 3)
    pos0 = af.randu(3, 3) * 10
    pos1 = af.randu(3, 3) * 10

    display_func(af.approx2(a, pos0, pos1))

    a = af.randu(8, 1)
    display_func(a)

    display_func(af.fft(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft(af.fft(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(4, 4)
    display_func(a)

    display_func(af.fft2(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft2(af.fft2(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(4, 4, 2)
    display_func(a)

    display_func(af.fft3(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft3(af.fft3(a))))
    display_func(af.real(af.idft(af.dft(a))))

    a = af.randu(10, 1)
    b = af.randu(3, 1)
    display_func(af.convolve1(a, b))
    display_func(af.fft_convolve1(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5)
    b = af.randu(3, 3)
    display_func(af.convolve2(a, b))
    display_func(af.fft_convolve2(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5, 3)
    b = af.randu(3, 3, 2)
    display_func(af.convolve3(a, b))
    display_func(af.fft_convolve3(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    b = af.randu(3, 1)
    x = af.randu(10, 1)
    a = af.randu(2, 1)
    display_func(af.fir(b, x))
    display_func(af.iir(b, a, x))
Пример #7
0
def f_interp_p_3d(self, dt):
    """
    Since the interpolation function are being performed in velocity space,
    the arrays used in the computation need to be in p_expanded form.
    Hence we will need to convert the same:
    """
    # Following Strang Splitting:
    # af.broadcast, allows us to perform batched operations 
    # when operating on arrays of different sizes
    # af.broadcast(function, *args) performs batched operations on
    # function(*args)

    if(self.performance_test_flag == True):
        tic = af.time()
    
    E1 = self.cell_centered_EM_fields_at_n[0]
    E2 = self.cell_centered_EM_fields_at_n[1]
    E3 = self.cell_centered_EM_fields_at_n[2]

    B1 = self.cell_centered_EM_fields_at_n[3]
    B2 = self.cell_centered_EM_fields_at_n[4]
    B3 = self.cell_centered_EM_fields_at_n[5]

    (A_p1, A_p2, A_p3) = af.broadcast(self._A_p, self.q1_center, self.q2_center,
                                      self.p1, self.p2, self.p3,
                                      E1, E2, E3, B1, B2, B3,
                                      self.physical_system.params
                                     )
    
    # Defining a lambda function to perform broadcasting operations
    # This is done using af.broadcast, which allows us to perform 
    # batched operations when operating on arrays of different sizes
    addition = lambda a,b:a + b
    
    # af.broadcast(function, *args) performs batched operations on
    # function(*args)
    p1_new = af.broadcast(addition, self.p1, - dt * A_p1)
    p2_new = af.broadcast(addition, self.p2, - dt * A_p2)

    p1_new = self._convert_to_p_expanded(p1_new)
    p2_new = self._convert_to_p_expanded(p2_new)

    # Transforming interpolant to go from [0, N_p - 1]:
    p1_lower_boundary = self.p1_start + 0.5 * self.dp1
    p2_lower_boundary = self.p2_start + 0.5 * self.dp2

    p1_interpolant = (p1_new - p1_lower_boundary) / self.dp1
    p2_interpolant = (p2_new - p2_lower_boundary) / self.dp2

    if(self.physical_system.params.p_dim == 3):        
        
        p3_new = af.broadcast(addition, self.p3, - 0.5 * dt * A_p3)
        p3_new = self._convert_to_p_expanded(p3_new)
        p3_lower_boundary = self.p3_start + 0.5 * self.dp3
        # Reordering p3_interpolant to bring variation in p3 to the 0-th axis:
        p3_interpolant = af.reorder((p3_new - p3_lower_boundary) / self.dp3, 2, 0, 1)

    # We perform the 3d interpolation by performing
    # individual 1d + 2d interpolations. Reordering to bring the
    # variation in values along axis 0 and axis 1

    self.f = self._convert_to_p_expanded(self.f)

    if(self.physical_system.params.p_dim == 3):

        
        self.f = af.approx1(af.reorder(self.f, 2, 0, 1),
                            p3_interpolant, 
                            af.INTERP.CUBIC_SPLINE
                           )

        self.f = af.reorder(self.f, 1, 2, 0)

    self.f = af.approx2(self.f,
                        p1_interpolant,
                        p2_interpolant,
                        af.INTERP.BICUBIC_SPLINE
                       )

    if(self.physical_system.params.p_dim == 3):
        
        self.f = af.approx1(af.reorder(self.f, 2, 0, 1),
                            p3_interpolant, 
                            af.INTERP.CUBIC_SPLINE
                           )

        self.f = af.reorder(self.f, 1, 2, 0)

    self.f = self._convert_to_q_expanded(self.f)
    af.eval(self.f)

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

    return
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################

import arrayfire as af

a = af.randu(10, 1)
pos0 = af.randu(10) * 10
af.display(af.approx1(a, pos0))

a = af.randu(3, 3)
pos0 = af.randu(3, 3) * 10
pos1 = af.randu(3, 3) * 10

af.display(af.approx2(a, pos0, pos1))

a = af.randu(8, 1)
af.display(a)

af.display(af.fft(a))
af.display(af.dft(a))
af.display(af.real(af.ifft(af.fft(a))))
af.display(af.real(af.idft(af.dft(a))))

a = af.randu(4, 4)
af.display(a)

af.display(af.fft2(a))
af.display(af.dft(a))
af.display(af.real(af.ifft2(af.fft2(a))))
Пример #9
0
def simple_signal(verbose=False):
    display_func = _util.display_func(verbose)
    print_func   = _util.print_func(verbose)

    signal = af.randu(10)
    x_new  = af.randu(10)
    x_orig = af.randu(10)
    display_func(af.approx1(signal, x_new, xp = x_orig))

    signal = af.randu(3, 3)
    x_new  = af.randu(3, 3)
    x_orig = af.randu(3, 3)
    y_new  = af.randu(3, 3)
    y_orig = af.randu(3, 3)

    display_func(af.approx2(signal, x_new, y_new, xp = x_orig, yp = y_orig))

    a = af.randu(8, 1)
    display_func(a)

    display_func(af.fft(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft(af.fft(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft(a)
    af.ifft_inplace(b)
    display_func(b)
    af.fft_inplace(b)
    display_func(b)

    b = af.fft_r2c(a)
    c = af.fft_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(4, 4)
    display_func(a)

    display_func(af.fft2(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft2(af.fft2(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft2(a)
    af.ifft2_inplace(b)
    display_func(b)
    af.fft2_inplace(b)
    display_func(b)

    b = af.fft2_r2c(a)
    c = af.fft2_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(4, 4, 2)
    display_func(a)

    display_func(af.fft3(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft3(af.fft3(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft3(a)
    af.ifft3_inplace(b)
    display_func(b)
    af.fft3_inplace(b)
    display_func(b)

    b = af.fft3_r2c(a)
    c = af.fft3_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(10, 1)
    b = af.randu(3, 1)
    display_func(af.convolve1(a, b))
    display_func(af.fft_convolve1(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5)
    b = af.randu(3, 3)
    display_func(af.convolve2(a, b))
    display_func(af.fft_convolve2(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5, 3)
    b = af.randu(3, 3, 2)
    display_func(af.convolve3(a, b))
    display_func(af.fft_convolve3(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))


    b = af.randu(3, 1)
    x = af.randu(10, 1)
    a = af.randu(2, 1)
    display_func(af.fir(b, x))
    display_func(af.iir(b, a, x))

    display_func(af.medfilt1(a))
    display_func(af.medfilt2(a))
    display_func(af.medfilt(a))
Пример #10
0
def f_interp_p_3d(self, dt):
    """
    Performs 3D interpolation in the p-space to solve for the equation:
    
    df/dt + A_p1 df/dp1 + A_p2 df/dp2 + A_p3 df/dp3 = 0

    Similar to the above function, this is done by backtracing the 
    characteristic curves and interpolating at the origin of the characteristics.
    
    Parameters
    ----------

    dt : double
         Time-step size to evolve the system

    NOTE: This function currently makes use of a Strang split approx1, approx2 with
          reorders to apply along the intended axes. With implementation of approx3
          complete this would be changed to make use of a single call of approx3.
          Ref:https://github.com/arrayfire/arrayfire/issues/1837
    """
    # Following Strang Splitting:
    if(self.performance_test_flag == True):
        tic = af.time()
    
    (A_p1, A_p2, A_p3) = af.broadcast(self._A_p, self.f, self.time_elapsed,
                                      self.q1_center, self.q2_center,
                                      self.p1_center, self.p2_center, self.p3_center,
                                      self.fields_solver, self.physical_system.params
                                     )
    
    # Using the add method wrapped with af.broadcast
    p1_new = add(self.p1_center, - dt * A_p1)
    p2_new = add(self.p2_center, - dt * A_p2)

    # Since the interpolation function are being performed in velocity space,
    # the arrays used in the computation need to be in p_expanded form.
    # Hence we will need to convert the same:
    p1_new = self._convert_to_p_expanded(p1_new)
    p2_new = self._convert_to_p_expanded(p2_new)

    # Transforming interpolant to go from [0, N_p - 1]:
    p1_lower_boundary = self.p1_start + 0.5 * self.dp1
    p2_lower_boundary = self.p2_start + 0.5 * self.dp2

    p1_interpolant = (p1_new - p1_lower_boundary) / self.dp1
    p2_interpolant = (p2_new - p2_lower_boundary) / self.dp2

    if(self.physical_system.params.p_dim == 3):        
        
        p3_new = add(self.p3_center, - 0.5 * dt * A_p3)
        p3_new = self._convert_to_p_expanded(p3_new)
        p3_lower_boundary = self.p3_start + 0.5 * self.dp3
    
        # Reordering from (N_p1, N_p2, N_p3, N_s * N_q) --> (N_p3, N_p1, N_p2, N_s * N_q)
        p3_interpolant = af.reorder((p3_new - p3_lower_boundary) / self.dp3, 2, 0, 1, 3)

    # We perform the 3d interpolation by performing individual 1d + 2d interpolations: 
    self.f = self._convert_to_p_expanded(self.f)
    
    if(self.physical_system.params.p_dim == 3):
        
        # Reordering from (N_p1, N_p2, N_p3, N_s * N_q) --> (N_p3, N_p1, N_p2, N_s * N_q)
        self.f = af.approx1(af.reorder(self.f, 2, 0, 1, 3),
                            p3_interpolant, 
                            af.INTERP.CUBIC_SPLINE
                           )

        # Reordering back from (N_p1, N_p2, N_p3, N_s * N_q) --> (N_p3, N_p1, N_p2, N_s * N_q)
        self.f = af.reorder(self.f, 1, 2, 0, 3)

    self.f = af.approx2(self.f,
                        p1_interpolant,
                        p2_interpolant,
                        af.INTERP.BICUBIC_SPLINE
                       )

    if(self.physical_system.params.p_dim == 3):
        
        # Reordering from (N_p1, N_p2, N_p3, N_s * N_q) --> (N_p3, N_p1, N_p2, N_s * N_q)
        self.f = af.approx1(af.reorder(self.f, 2, 0, 1, 3),
                            p3_interpolant, 
                            af.INTERP.CUBIC_SPLINE
                           )

        # Reordering back from (N_p1, N_p2, N_p3, N_s * N_q) --> (N_p3, N_p1, N_p2, N_s * N_q)
        self.f = af.reorder(self.f, 1, 2, 0, 3)

    self.f = self._convert_to_q_expanded(self.f)
    af.eval(self.f)

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

    return
Пример #11
0
def apply_shearing_box_bcs_f(self, boundary):
    """
    Applies the shearing box boundary conditions along boundary specified 
    for the distribution function
    
    Parameters
    ----------
    boundary: str
              Boundary along which the boundary condition is to be applied.
    """

    N_g = self.N_ghost
    q = self.physical_system.params.q
    omega = self.physical_system.params.omega

    L_q1 = self.q1_end - self.q1_start
    L_q2 = self.q2_end - self.q2_start

    if (boundary == 'left'):
        sheared_coordinates = self.q2_center[:, :, :
                                             N_g] - q * omega * L_q1 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q2_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q2_end,
                                            sheared_coordinates - L_q2,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q2_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q2_start,
                sheared_coordinates + L_q2, sheared_coordinates)

        # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
        # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)

        self.f[:, :, :N_g] = af.reorder(
            af.approx2(af.reorder(self.f[:, :, :N_g], 2, 3, 0, 1),
                       af.reorder(self.q1_center[:, :, :N_g], 2, 3, 0, 1),
                       af.reorder(sheared_coordinates, 2, 3, 0, 1),
                       af.INTERP.BICUBIC_SPLINE,
                       xp=af.reorder(self.q1_center[:, :, :N_g], 2, 3, 0, 1),
                       yp=af.reorder(self.q2_center[:, :, :N_g], 2, 3, 0, 1)),
            2, 3, 0, 1)

    elif (boundary == 'right'):
        sheared_coordinates = self.q2_center[:, :,
                                             -N_g:] + q * omega * L_q1 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q2_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q2_end,
                                            sheared_coordinates - L_q2,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q2_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q2_start,
                sheared_coordinates + L_q2, sheared_coordinates)

        # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
        # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)

        self.f[:, :, -N_g:] = af.reorder(
            af.approx2(af.reorder(self.f[:, :, -N_g:], 2, 3, 0, 1),
                       af.reorder(self.q1_center[:, :, -N_g:], 2, 3, 0, 1),
                       af.reorder(sheared_coordinates, 2, 3, 0, 1),
                       af.INTERP.BICUBIC_SPLINE,
                       xp=af.reorder(self.q1_center[:, :, -N_g:], 2, 3, 0, 1),
                       yp=af.reorder(self.q2_center[:, :, -N_g:], 2, 3, 0, 1)),
            2, 3, 0, 1)

    elif (boundary == 'bottom'):

        sheared_coordinates = self.q1_center[:, :, :, :
                                             N_g] - q * omega * L_q2 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q1_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q1_end,
                                            sheared_coordinates - L_q1,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q1_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q1_start,
                sheared_coordinates + L_q1, sheared_coordinates)

        # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
        # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)

        self.f[:, :, :, :N_g] = af.reorder(
            af.approx2(af.reorder(self.f[:, :, :, :N_g], 2, 3, 0, 1),
                       af.reorder(sheared_coordinates, 2, 3, 0, 1),
                       af.reorder(self.q2_center[:, :, :, :N_g], 2, 3, 0, 1),
                       af.INTERP.BICUBIC_SPLINE,
                       xp=af.reorder(self.q1_center[:, :, :, :N_g], 2, 3, 0,
                                     1),
                       yp=af.reorder(self.q2_center[:, :, :, :N_g], 2, 3, 0,
                                     1)), 2, 3, 0, 1)

    elif (boundary == 'top'):

        sheared_coordinates = self.q1_center[:, :, :,
                                             -N_g:] + q * omega * L_q2 * self.time_elapsed

        # Applying periodic boundary conditions to the points which are out of domain:
        while (af.sum(sheared_coordinates > self.q1_end) != 0):
            sheared_coordinates = af.select(sheared_coordinates > self.q1_end,
                                            sheared_coordinates - L_q1,
                                            sheared_coordinates)

        while (af.sum(sheared_coordinates < self.q1_start) != 0):
            sheared_coordinates = af.select(
                sheared_coordinates < self.q1_start,
                sheared_coordinates + L_q1, sheared_coordinates)

        # Reordering from (N_p, N_s, N_q1, N_q2) --> (N_q1, N_q2, N_p, N_s)
        # and reordering back from (N_q1, N_q2, N_p, N_s) --> (N_p, N_s, N_q1, N_q2)

        self.f[:, :, :, -N_g:] = af.reorder(
            af.approx2(af.reorder(self.f[:, :, :, -N_g:], 2, 3, 0, 1),
                       af.reorder(sheared_coordinates, 2, 3, 0, 1),
                       af.reorder(self.q2_center[:, :, :, -N_g:], 2, 3, 0, 1),
                       af.INTERP.BICUBIC_SPLINE,
                       xp=af.reorder(self.q1_center[:, :, :, -N_g:], 2, 3, 0,
                                     1),
                       yp=af.reorder(self.q2_center[:, :, :, -N_g:], 2, 3, 0,
                                     1)), 2, 3, 0, 1)

    else:
        raise Exception('Invalid choice for boundary')

    return
Пример #12
0
def simple_signal(verbose=False):
    display_func = _util.display_func(verbose)

    signal = af.randu(10)
    x_new = af.randu(10)
    x_orig = af.randu(10)
    display_func(af.approx1(signal, x_new, xp=x_orig))

    signal = af.randu(3, 3)
    x_new = af.randu(3, 3)
    x_orig = af.randu(3, 3)
    y_new = af.randu(3, 3)
    y_orig = af.randu(3, 3)

    display_func(af.approx2(signal, x_new, y_new, xp=x_orig, yp=y_orig))

    a = af.randu(8, 1)
    display_func(a)

    display_func(af.fft(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft(af.fft(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft(a)
    af.ifft_inplace(b)
    display_func(b)
    af.fft_inplace(b)
    display_func(b)

    b = af.fft_r2c(a)
    c = af.fft_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(4, 4)
    display_func(a)

    display_func(af.fft2(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft2(af.fft2(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft2(a)
    af.ifft2_inplace(b)
    display_func(b)
    af.fft2_inplace(b)
    display_func(b)

    b = af.fft2_r2c(a)
    c = af.fft2_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(4, 4, 2)
    display_func(a)

    display_func(af.fft3(a))
    display_func(af.dft(a))
    display_func(af.real(af.ifft3(af.fft3(a))))
    display_func(af.real(af.idft(af.dft(a))))

    b = af.fft3(a)
    af.ifft3_inplace(b)
    display_func(b)
    af.fft3_inplace(b)
    display_func(b)

    b = af.fft3_r2c(a)
    c = af.fft3_c2r(b)
    display_func(b)
    display_func(c)

    a = af.randu(10, 1)
    b = af.randu(3, 1)
    display_func(af.convolve1(a, b))
    display_func(af.fft_convolve1(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    a = af.randu(5, 5)
    b = af.randu(3, 3)
    display_func(af.convolve2(a, b))
    display_func(af.fft_convolve2(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    c = af.convolve2NN(a, b)
    display_func(c)
    in_dims = c.dims()
    incoming_grad = af.constant(1, in_dims[0], in_dims[1])
    g = af.convolve2GradientNN(incoming_grad, a, b, c)
    display_func(g)

    a = af.randu(5, 5, 3)
    b = af.randu(3, 3, 2)
    display_func(af.convolve3(a, b))
    display_func(af.fft_convolve3(a, b))
    display_func(af.convolve(a, b))
    display_func(af.fft_convolve(a, b))

    b = af.randu(3, 1)
    x = af.randu(10, 1)
    a = af.randu(2, 1)
    display_func(af.fir(b, x))
    display_func(af.iir(b, a, x))

    display_func(af.medfilt1(a))
    display_func(af.medfilt2(a))
    display_func(af.medfilt(a))