def get_phase(num_of_vort, pos, x_pts, y_pts, grid_x, grid_y, grid_len_x, grid_len_y): """ Gets phase distribution of N dipoles.""" # Phase initialisation theta_tot = cp.empty((x_pts, y_pts)) # Scale pts: x_tilde = 2 * cp.pi * ((grid_x - grid_x.min()) / grid_len_x) y_tilde = 2 * cp.pi * ((grid_y - grid_y.min()) / grid_len_y) switch = False for i in range(num_of_vort // 2): theta_k = cp.zeros((x_pts, y_pts)) if i % 24 == 0 and i > 0: switch ^= True if switch: x_p, y_p = next(pos) x_m, y_m = next(pos) else: x_m, y_m = next(pos) x_p, y_p = next(pos) # Scaling vortex positions: x_m_tilde = 2 * cp.pi * ((x_m - grid_x.min()) / grid_len_x) y_m_tilde = 2 * cp.pi * ((y_m - grid_y.min()) / grid_len_y) x_p_tilde = 2 * cp.pi * ((x_p - grid_x.min()) / grid_len_x) y_p_tilde = 2 * cp.pi * ((y_p - grid_y.min()) / grid_len_y) # Aux variables Y_minus = y_tilde - y_m_tilde X_minus = x_tilde - x_m_tilde Y_plus = y_tilde - y_p_tilde X_plus = x_tilde - x_p_tilde heav_xp = cp.asarray(np.heaviside(cp.asnumpy(X_plus), 1.)) heav_xm = cp.asarray(np.heaviside(cp.asnumpy(X_minus), 1.)) for nn in cp.arange(-5, 6): theta_k += cp.arctan(cp.tanh((Y_minus + 2 * cp.pi * nn) / 2) * cp.tan((X_minus - cp.pi) / 2)) \ - cp.arctan(cp.tanh((Y_plus + 2 * cp.pi * nn) / 2) * cp.tan((X_plus - cp.pi) / 2)) \ + cp.pi * (heav_xp - heav_xm) theta_k -= y_tilde * (x_p_tilde - x_m_tilde) / (2 * cp.pi) theta_tot += theta_k return theta_tot
def get_phase(num_of_vort, pos, grid_x, grid_y): """ num_of_vort: number of vortices to imprint pos: iterable of positions to imprint the vortices grid_x: X-meshgrid grid_y: Y-meshgrid """ # Constructing necessary grid parameters: x_pts, y_pts = len(grid_x[:, 0]), len(grid_y[0, :]) dx, dy = grid_x[0, 1] - grid_x[0, 0], grid_y[1, 0] - grid_y[0, 0] grid_len_x, grid_len_y = x_pts * dx, y_pts * dy # Phase initialisation theta_tot = cp.empty((x_pts, y_pts)) # Scale pts: x_tilde = 2 * cp.pi * ((grid_x - grid_x.min()) / grid_len_x) y_tilde = 2 * cp.pi * ((grid_y - grid_y.min()) / grid_len_y) for _ in range(num_of_vort // 2): theta_k = cp.zeros((x_pts, y_pts)) x_m, y_m = next(pos) x_p, y_p = next(pos) # Scaling vortex positions: x_m_tilde = 2 * cp.pi * ((x_m - grid_x.min()) / grid_len_x) y_m_tilde = 2 * cp.pi * ((y_m - grid_y.min()) / grid_len_y) x_p_tilde = 2 * cp.pi * ((x_p - grid_x.min()) / grid_len_x) y_p_tilde = 2 * cp.pi * ((y_p - grid_y.min()) / grid_len_y) # Aux variables Y_minus = y_tilde - y_m_tilde X_minus = x_tilde - x_m_tilde Y_plus = y_tilde - y_p_tilde X_plus = x_tilde - x_p_tilde heav_xp = cp.asarray(np.heaviside(cp.asnumpy(X_plus), 1.)) heav_xm = cp.asarray(np.heaviside(cp.asnumpy(X_minus), 1.)) for nn in cp.arange(-5, 6): theta_k += cp.arctan(cp.tanh((Y_minus + 2 * cp.pi * nn) / 2) * cp.tan((X_minus - cp.pi) / 2)) \ - cp.arctan(cp.tanh((Y_plus + 2 * cp.pi * nn) / 2) * cp.tan((X_plus - cp.pi) / 2)) \ + cp.pi * (heav_xp - heav_xm) theta_k -= y_tilde * (x_p_tilde - x_m_tilde) / (2 * cp.pi) theta_tot += theta_k return theta_tot
def ff(self, r1, r2, r3): exp = 10**-18 res = (r2/2)*(r3**2-r1**2)*np.arcsinh(r2/(np.sqrt(r1**2+r3**2)+exp))+\ (r3/2)*(r2**2-r1**2)*np.arcsinh(r3/(np.sqrt(r1**2+r2**2)+exp))-\ r1*r2*r3*np.arctan(r2*r3/(r1*np.sqrt(r1**2+r2**2+r3**2)+exp))+\ (1/6)*(2*r1**2-r2**2-r3**2)*np.sqrt(r1**2+r2**2+r3**2) return res
def atan(x: Array, /) -> Array: """ Array API compatible wrapper for :py:func:`np.arctan <numpy.arctan>`. See its docstring for more information. """ if x.dtype not in _floating_dtypes: raise TypeError("Only floating-point dtypes are allowed in atan") return Array._new(np.arctan(x._array))
def calc_theta_tx(self, tx_ant): x, y = cp.meshgrid(tx_ant.x_position, tx_ant.y_position) # ブロードキャストを使って30x30に各受信点からの角度(shape=7)を格納 rx_x = self.x_position[cp.newaxis, cp.newaxis, :] # shape=1,1,7 rx_y = self.y_position[cp.newaxis, cp.newaxis, :] # shape=1,1,7 x_diff = rx_x - x[:, :, cp.newaxis] # shape=30,30,7 y_diff = rx_y - y[:, :, cp.newaxis] # shape=30,30,7 theta = cp.arctan(y_diff / x_diff) return theta
def segmentMasks(width, height, returnCupy=False): if cupy: xOrigin = (width - 1) / 2 yOrigin = (height - 1) / 2 xIndices, yIndices = cupy.ogrid[0:height, 0:width] xIndices = xIndices - xOrigin yIndices = yIndices - yOrigin window = yIndices / xIndices window = cupy.arctan(window) window = window * 180 / cupy.pi q1 = (window > -22.5) & (window <= 22.5) q2 = (window > 22.5) & (window <= 67.5) q3 = (window > 67.5) | (window <= -67.5) q4 = (window > -67.5) & (window <= -22.5) q1 = q1.astype(int) q2 = q2.astype(int) q3 = q3.astype(int) q4 = q4.astype(int) if returnCupy: return (q1, q2, q3, q4) else: return (cupy.asnumpy(q1), cupy.asnumpy(q2), cupy.asnumpy(q3), cupy.asnumpy(q4)) else: xOrigin = (width - 1) / 2 yOrigin = (height - 1) / 2 xIndices, yIndices = numpy.ogrid[0:height, 0:width] xIndices = xIndices - xOrigin yIndices = yIndices - yOrigin window = yIndices / xIndices window = numpy.arctan(window) window = window * 180 / numpy.pi q1 = (window > -22.5) & (window <= 22.5) q2 = (window > 22.5) & (window <= 67.5) q3 = (window > 67.5) | (window <= -67.5) q4 = (window > -67.5) & (window <= -22.5) q1 = q1.astype(int) q2 = q2.astype(int) q3 = q3.astype(int) q4 = q4.astype(int) return (q1, q2, q3, q4)
def arccot__(z): return cp.arctan(1.0/z)
def RenderingUserViewLF_AllinOne5K(LF=None, LFDisparity=None, FB=None, viewpoint=None, DIR=None): sphereW = Params.WIDTH sphereH = Params.HEIGHT CENTERx = viewpoint.lon CENTERy = viewpoint.lat # output view is 3:4 ratio new_imgW = cp.floor(viewpoint.diag * 4 / 5 + 0.5) new_imgH = cp.floor(viewpoint.diag * 3 / 5 + 0.5) new_imgW = int(new_imgW) new_imgH = int(new_imgH) OutView = cp.zeros((new_imgH, new_imgW, 3)) TYwarp, TXwarp = cp.mgrid[0:new_imgH, 0:new_imgW] TX = TXwarp TY = TYwarp TX = (TX - 0.5 - new_imgW / 2) TY = (TY - 0.5 - new_imgH / 2) #의심 TX = TX + 1 TY = TY + 1 r = (viewpoint.diag / 2) / cp.tan(viewpoint.fov / 2) R = cp.sqrt(TY**2 + r**2) # Calculate LF_n ANGy = cp.arctan(-TY / r) ANGy = ANGy + CENTERy if (FB == 1): ANGn = cp.cos(ANGy) * cp.arctan(TX / r) ANGn = ANGn + CENTERx Pn = (Params.LFU_W / 2 - viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) elif (FB == 2): ANGn = cp.cos(ANGy) * cp.arctan(-TX / r) ANGn = ANGn - CENTERx Pn = (Params.LFU_W / 2 + viewpoint.pos_y ) * cp.tan(ANGn) + viewpoint.pos_x + (3 * Params.LFU_W / 2) X = cp.sin(ANGy) * R Y = -cp.cos(ANGy) * R Z = TX ANGx = cp.arctan2(Z, -Y) RZY = cp.sqrt(Z**2 + Y**2) ANGy = cp.arctan(X / RZY) #or ANGy = atan2(X, RZY); RATIO = 1 ANGy = ANGy * RATIO ANGx = ANGx + CENTERx ANGx[abs(ANGy) > pi / 2] = ANGx[abs(ANGy) > pi / 2] + pi ANGx[ANGx > pi] = ANGx[ANGx > pi] - 2 * pi ANGy[ANGy > pi / 2] = pi / 2 - (ANGy[ANGy > pi / 2] - pi / 2) ANGy[ANGy < -pi / 2] = -pi / 2 + (ANGy[ANGy < -pi / 2] + pi / 2) Px = (ANGx + pi) / (2 * pi) * sphereW + 0.5 Py = ((-ANGy) + pi / 2) / pi * sphereH + 0.5 if (DIR == 2): Px = Px + Params.WIDTH / 4 elif (DIR == 3): Px = Px + Params.WIDTH / 2 elif (DIR == 4): Px = Px - Params.WIDTH / 4 Px[Px < 1] = Px[Px < 1] + Params.WIDTH Px[Px > Params.WIDTH] = Px[Px > Params.WIDTH] - Params.WIDTH INDxx = cp.argwhere(Px < 1) Px[INDxx] = Px[INDxx] + sphereW Pn0 = cp.floor(Pn) Pn1 = cp.ceil(Pn) Pnr = Pn - Pn0 Px0 = cp.floor(Px) Px1 = cp.ceil(Px) Pxr = Px - Px0 Py0 = cp.floor(Py) Py1 = cp.ceil(Py) Pyr = Py - Py0 Pnr = cp.rint((Pnr * 10000)) Pnr = Pnr / 10000 Pxr = cp.rint((Pxr * 10000)) Pxr = Pxr / 10000 Pyr = cp.rint((Pyr * 10000)) Pyr = Pyr / 10000 #210->012 rgb #cv2 사용 안하면 그대로 OutView[:, :, 2] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 1) OutView[:, :, 1] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 2) OutView[:, :, 0] = inter8_mat5K(LF, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1, 3) OutFlow = inter8_mat_flow5K(LFDisparity, Pnr, Pn0, Pn1, Pxr, Px0, Px1, Pyr, Py0, Py1) Py = cp.pad(Py, [(1, 1), (0, 0)], mode='edge') Py = cp.ceil((Py * 10000)) Py = Py / 10000 My = 2 / (Py[2:cp.size(Py, 0), :] - Py[0:(cp.size(Py, 0) - 2), :]) My[0, :] = My[0, :] / 2 My[cp.size(My, 0) - 1, :] = My[cp.size(My, 0) - 1, :] / 2 OutFlow = My * OutFlow return OutView, OutFlow
def slope_aspect_gpu(dem, resolution_x, resolution_y, ve_factor=1, output_units="radian"): """ Procedure can return terrain slope and aspect in radian units (default) or in alternative units (if specified). Slope is defined as 0 for Hz plane and pi/2 for vertical plane. Aspect iz defined as geographic azimuth: clockwise increasing, 0 or 2pi for the North direction. Currently applied finite difference method. Parameters ---------- dem : input dem 2D cupy array resolution_x : dem resolution in X direction resolution_y : DEM resolution in Y direction ve_factor : vertical exaggeration factor (must be greater than 0) output_units : percent, degree, radians Returns ------- {"slope": slope_out, "aspect": aspect_out} : dictionaries with 2D numpy arrays """ if ve_factor <= 0: raise Exception( "rvt.vis.slope_aspect: ve_factor must be a positive number!") if resolution_x < 0 or resolution_y < 0: raise Exception( "rvt.vis.slope_aspect: resolution must be a positive number!") dem = dem.astype(np.float32) if ve_factor != 1: dem = dem * ve_factor # add frame of 0 (additional row up bottom and column left right) dem = cp.pad(dem, pad_width=1, mode="constant", constant_values=0) # derivatives in X and Y direction dzdx = ((cp.roll(dem, 1, axis=1) - cp.roll(dem, -1, axis=1)) / 2) / resolution_x dzdy = ((cp.roll(dem, -1, axis=0) - cp.roll(dem, 1, axis=0)) / 2) / resolution_y tan_slope = cp.sqrt(dzdx**2 + dzdy**2) # Compute slope if output_units == "percent": slope_out = tan_slope * 100 elif output_units == "degree": slope_out = cp.rad2deg(np.arctan(tan_slope)) elif output_units == "radian": slope_out = cp.arctan(tan_slope) else: raise Exception( "rvt.vis.calculate_slope: Wrong function input 'output_units'!") # compute Aspect # aspect identifies the down slope direction of the maximum rate of change in value from each cell to its neighbors: # 0 # 270 90 # 180 dzdy[ dzdy == 0] = 10e-9 # important for numeric stability - where dzdy is zero, make tangens to really high value aspect_out = cp.arctan2(dzdx, dzdy) # atan2 took care of the quadrants if output_units == "degree": aspect_out = cp.rad2deg(aspect_out) # remove the frame (padding) slope_out = slope_out[1:-1, 1:-1] aspect_out = aspect_out[1:-1, 1:-1] # edges to -1 slope_out[:, 0] = -1 slope_out[0, :] = -1 slope_out[:, -1] = -1 slope_out[-1, :] = -1 aspect_out[:, 0] = -1 aspect_out[0, :] = -1 aspect_out[:, -1] = -1 aspect_out[-1, :] = -1 return {"slope": slope_out, "aspect": aspect_out}
def arctan(x): return cp.arctan(x)
def calc_phi_tx(self, distance, tx_ant): phi = cp.arctan(distance / (tx_ant.z_position + self.z_position)) return phi
def phase_cupy(complex_arr): return(cp.arctan(cp.imag(complex_arr)/cp.real(complex_arr)))