def f_interp(config, dt, x, vel_x, f): x_new = x - vel_x*dt step_size = af.sum(x[1,0] - x[0,0]) f_interp = af.constant(0, N_positions + 2*ghost_zones, N_velocity) f_interp = af.Array.as_type(f_interp, af.Dtype.f64) # Interpolating: x_temp = x_new[ghost_zones:-ghost_zones, :].copy() while(af.sum(x_temp<left_boundary)!=0): x_temp = af.select(x_temp<left_boundary, x_temp + length, x_temp ) while(af.sum(x_temp>right_boundary)!=0): x_temp = af.select(x_temp>right_boundary, x_temp - length, x_temp ) x_temp = af.Array.as_type(x_temp, af.Dtype.f64) x_interpolant = x_temp/step_size + ghost_zones x_interpolant = af.Array.as_type(x_interpolant, af.Dtype.f64) f = af.Array.as_type(f, af.Dtype.f64) f_interp[ghost_zones:-ghost_zones, :] = af.approx1(f, x_interpolant,\ af.INTERP.CUBIC_SPLINE ) f_interp = af.Array.as_type(f_interp, af.Dtype.f64) af.eval(f_interp) return f_interp
#!/usr/bin/python ####################################################### # Copyright (c) 2015, ArrayFire # All rights reserved. # # 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)
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))
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
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))
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
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))