示例#1
0
def get_five_point_central_kernels():
    # 4th-order, kernel size = 5x5x5; diff_f = (-f_(i+2) + 8*f_(i+1) - 8*f_(i-1) + f_(i-2)) / 12
    five_point_diff = np.array([1, -8, 0, 8, -1]) / 12  # 5-point differential
    kernelx = np.zeros((1, 1, 5, 5, 5))
    kernelx[0, 0, :, 2, 2] = five_point_diff
    kernely = np.swapaxes(kernelx, 2, 3)
    kernelz = np.swapaxes(kernelx, 2, 4)

    return cast(kernelx, device=lbnz.get_device()), cast(
        kernely, device=lbnz.get_device()), cast(kernelz,
                                                 device=lbnz.get_device())
示例#2
0
def get_five_point_sobel_kernels():
    # get 5x5 sobel-way built kernels
    smooth_5_point = np.array([[1, 4, 6, 4, 1]])  # 1-D 5-point smoothing
    smooth_5x5 = (np.transpose(smooth_5_point) * smooth_5_point).reshape(
        (1, 5, 5))  # 2-D 5x5 smoothing
    five_point_diff = (np.array([1, -8, 0, 8, -1]) / 12).reshape(
        5, 1, 1)  # 5-point differential
    # add the denominator to normalize the kernel parameters
    sobel_5x5_x = (smooth_5x5 * five_point_diff / 256).reshape(1, 1, 5, 5, 5)
    sobel_5x5_y = np.swapaxes(sobel_5x5_x, 2, 3)
    sobel_5x5_z = np.swapaxes(sobel_5x5_x, 2, 4)

    return cast(sobel_5x5_x, device=lbnz.get_device()), cast(
        sobel_5x5_y, device=lbnz.get_device()), cast(sobel_5x5_z,
                                                     device=lbnz.get_device())
示例#3
0
def get_two_point_upwind_kernels(direction):
    # 'p' denotes 'plus', i.e. diff_u = (u_i+1) - u_i
    # 'm' denotes 'minus', i.e. diff_u = u_i - (u_i-1)

    upwind_x = np.zeros((1, 1, 2, 2, 2))
    upwind_diff = np.array([-1, 1])

    if direction == 'p':
        upwind_x[0, 0, :, 0, 0] = upwind_diff
    elif direction == 'm':
        upwind_x[0, 0, :, 1, 1] = upwind_diff

    upwind_y = np.swapaxes(upwind_x, 2, 3)
    upwind_z = np.swapaxes(upwind_x, 2, 4)

    return cast(upwind_x), cast(upwind_y), cast(upwind_z)
示例#4
0
def get_four_point_upwind_kernels(direction):
    # 'p' denotes 'plus', i.e. diff_u = -(u_i+2) + 4 * (u_i+1) - 3 * u_i
    # 'm' denotes 'minus', i.e. diff_u = 3 * u_i - 4 * (u_i-1) + (u_i-2)

    upwind_x = np.zeros((1, 1, 4, 4, 4))

    if direction == 'p':
        upwind_diff = np.array([-2, -3, 6, -1]) / 6
        upwind_x[0, 0, :, 1, 1] = upwind_diff
    elif direction == 'm':
        upwind_diff = np.array([1, -6, 3, 2]) / 6
        upwind_x[0, 0, :, -2, -2] = upwind_diff

    upwind_y = np.swapaxes(upwind_x, 2, 3)
    upwind_z = np.swapaxes(upwind_x, 2, 4)

    return cast(upwind_x, device=lbnz.get_device()), cast(
        upwind_y, device=lbnz.get_device()), cast(upwind_z,
                                                  device=lbnz.get_device())
示例#5
0
 def data(self) -> Tensor:
     return cast(np.meshgrid(
         np.linspace(self.west, self.east, num=self.L),
         np.linspace(self.south, self.north, num=self.W),
         np.linspace(self.lower, self.upper, num=self.H), indexing='ij'
     ), device=self.default_device).view(1, 3, self.L, self.W, self.H)
示例#6
0
 def mk_one(self) -> Tensor:
     return cast(np.ones(self.shape), device=self.default_device)
示例#7
0
 def mk_zero(self) -> Tensor:
     return cast(np.zeros(self.shape), device=self.default_device)
示例#8
0
 def setUp(self):
     self.zero = lbnz.cast(np.zeros([1, 1, 2, 2, 2]))
     self.one = lbnz.cast(np.ones([1, 1, 2, 2, 2]))
示例#9
0
 def data(self) -> Tensor:
     return cast(np.meshgrid(
         np.linspace(self.west, self.east, num=self.L),
         np.linspace(self.south, self.north, num=self.W),
     ), device=self.default_device).view(1, 2, self.L, self.W)
示例#10
0
    f[:, :, :,
      0, :] = 3 * f[:, :, :, 1, :] - 3 * f[:, :, :, 2, :] + f[:, :, :, 3, :]
    f[:, :, :,
      -1, :] = 3 * f[:, :, :, -2, :] - 3 * f[:, :, :, -3, :] + f[:, :, :,
                                                                 -4, :]
    f[:, :, :, :,
      0] = 3 * f[:, :, :, :, 1] - 3 * f[:, :, :, :, 2] + f[:, :, :, :, 3]
    f[:, :, :, :,
      -1] = 3 * f[:, :, :, :, -2] - 3 * f[:, :, :, :, -3] + f[:, :, :, :, -4]

    return f


centralx = cast([[[
    [[0, 0, 0], [0, -1, 0], [0, 0, 0]],
    [[0, 0, 0], [0, 0, 0], [0, 0, 0]],
    [[0, 0, 0], [0, 1, 0], [0, 0, 0]],
]]],
                device=lbnz.get_device()) / 2
centraly = cast(
    [[[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, -1, 0], [0, 0, 0], [0, 1, 0]],
       [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]],
    device=lbnz.get_device()) / 2
centralz = cast(
    [[[[[0, 0, 0], [0, 0, 0], [0, 0, 0]], [[0, 0, 0], [-1, 0, 1], [0, 0, 0]],
       [[0, 0, 0], [0, 0, 0], [0, 0, 0]]]]],
    device=lbnz.get_device()) / 2


def central(f):
    f = fill_boundary_central(f)