def prepare_inputs(*inputs, device): out = [bh.array(k) for k in inputs] for o in out: # force allocation on target device tmp = o * 1 # noqa: F841 bh.flush() return out
def gauss(a): """ Performe Gausian elimination on matrix a without pivoting """ dw = False if dw: def loop_body(a): c = get_iterator(1) x1 = a[c:, c - 1:] # x2 = (a[c:, c - 1, None] / a[c - 1, c - 1:c, None])#[:, None] x2 = a[c:, c - 1] / a[c - 1, c - 1:c] # x2 = a[c:, c - 1] / a[c - 1, c - 1:c] # x2 = (a[c:, c - 1] / a[c - 1, c - 1:c])[:, None] x3 = a[c - 1, c - 1:] x1 = x1 - x2 * x3 loop.do_while(loop_body, a.shape[0] - 2, a) a /= np.diagonal(a)[:, None] return a else: for c in range(1, a.shape[0]): x1 = a[c:, c - 1:] x2 = (a[c:, c - 1] / a[c - 1, c - 1:c])[:, None] x3 = a[c - 1, c - 1:] x1 = x1 - x2 * x3 # a[c:, c - 1:] = a[c:, c - 1:] - (a[c:, c - 1] / a[c - 1, c - 1:c]) * a[c - 1, c - 1:] np.flush() a /= np.diagonal(a)[:, None] return a
def sign_is(z): bh.flush() x = np.real(z) y = np.imag(z) out = z / (np.sqrt(x*x+y*y)+(z==0)) bh.flush() return out
def lu(a): """ Performe LU decomposition on the matrix a so A = L*U """ dw = True if dw: def loop_body(l, u): c = get_iterator() l[c:, c - 1] = (u[c:, c - 1] / u[c - 1, c - 1:c]) u[c:, c - 1:] = u[c:, c - 1:] - l[c:, c - 1][:, None] * u[c - 1, c - 1:] u = a.copy() l = np.zeros_like(a) np.diagonal(l)[:] = 1.0 loop.do_while(loop_body, u.shape[0] - 2, l, u) return (l, u) else: u = a.copy() l = np.zeros_like(a) np.diagonal(l)[:] = 1.0 for c in range(1, u.shape[0]): l[c:, c - 1] = (u[c:, c - 1] / u[c - 1, c - 1:c]) u[c:, c - 1:] = u[c:, c - 1:] - l[c:, c - 1][:, None] * u[c - 1, c - 1:] np.flush() return (l, u)
def gauss(a): """ Performe Gausian elimination on matrix a without pivoting """ for c in xrange(1,a.shape[0]): a[c:,c-1:] = a[c:,c-1:] - (a[c:,c-1]/a[c-1,c-1:c])[:,None] * a[c-1,c-1:] np.flush(a) a /= np.diagonal(a)[:,None] return a
def thesign(z): bh.flush() z_abs = np.absolute(z) z_zero = (z==0) divisor = z_abs + z_zero del z_abs del z_zero sign = z / divisor del divisor bh.flush() return sign
def lu(a): """ Performe LU decomposition on the matrix a so A = L*U """ u = a.copy() l = np.zeros_like(a) np.diagonal(l)[:] = 1.0 for c in xrange(1,u.shape[0]): l[c:,c-1] = (u[c:,c-1]/u[c-1,c-1:c]) u[c:,c-1:] = u[c:,c-1:] - l[c:,c-1][:,None] * u[c-1,c-1:] np.flush(u) return (l,u)
def simulate(m, x, y, z, vx, vy, vz, timesteps): temporaries = (np.ones( (size, size), dtype="float64"), np.ones((size, size), dtype="float64"), np.ones((size, size), dtype="float64"), np.ones((size, size), dtype="float64")) start = time.time() for i in range(timesteps): ret = move(m, x, y, z, vx, vy, vz, dt, temporaries) np.flush() print(x, y, z) end = time.time() print("Simulation time:", end - start)
def solve(a, b): """ Solve a linear matrix equation, or system of linear scalar equations using Gausian elimination. :param a: Coefficient matrix :type a: array_like, shape (M, M) :param b: Ordinate or "dependent variable" values :type b: array_like, shape (M,) or (M, N) :return: Solution to the system a x = b :rtype: ndarray, shape (M,) or (M, N) depending on b :raises: :py:exc:`LinAlgError` If `a` is singular or not square. **Examples:** Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``: >>> import bohrium as np >>> a = np.array([[3.,1.], [1.,2.]]) >>> b = np.array([9.,8.]) >>> x = np.linalg.solve(a, b) >>> x array([ 2., 3.]) Check that the solution is correct: >>> (np.dot(a, x) == b).all() True """ if not (len(a.shape) == 2 and a.shape[0] == a.shape[1]): raise la.LinAlgError("a is not square") w = gauss(np.hstack((a,b[:,np.newaxis]))) lc = w.shape[1]-1 x = w[:,lc].copy() for c in xrange(lc-1,0,-1): x[:c] -= w[:c,c] * x[c:c+1] np.flush(x) return x
def solve(a, b): """ Solve a linear matrix equation, or system of linear scalar equations using Gausian elimination. :param a: Coefficient matrix :type a: array_like, shape (M, M) :param b: Ordinate or "dependent variable" values :type b: array_like, shape (M,) or (M, N) :return: Solution to the system a x = b :rtype: ndarray, shape (M,) or (M, N) depending on b :raises: :py:exc:`LinAlgError` If `a` is singular or not square. **Examples:** Solve the system of equations ``3 * x0 + x1 = 9`` and ``x0 + 2 * x1 = 8``: >>> import bohrium as np >>> a = np.array([[3.,1.], [1.,2.]]) >>> b = np.array([9.,8.]) >>> x = np.linalg.solve(a, b) >>> x array([ 2., 3.]) Check that the solution is correct: >>> (np.dot(a, x) == b).all() True """ if not (len(a.shape) == 2 and a.shape[0] == a.shape[1]): raise la.LinAlgError("a is not square") w = gauss(np.hstack((a, b[:, np.newaxis]))) lc = w.shape[1] - 1 x = w[:, lc].copy() for c in range(lc - 1, 0, -1): x[:c] -= w[:c, c] * x[c:c + 1] np.flush() return x
def main(): B = util.Benchmark() nx = B.size[0] ny = B.size[1] nz = B.size[2] ITER = B.size[3] NO_OBST = 1 omega = 1.0 density = 1.0 deltaU = 1e-7 t1 = 1/3.0 t2 = 1/18.0 t3 = 1/36.0 B.start() F = np.ones((19, nx, ny, nz), dtype=np.float64) F[:] = density/19.0 FEQ = np.ones((19, nx, ny, nz), dtype=np.float64) FEQ[:] = density/19.0 T = np.zeros((19, nx, ny, nz), dtype=np.float64) #Create the scenery. BOUND = np.zeros((nx, ny, nz), dtype=np.float64) BOUNDi = np.ones((nx, ny, nz), dtype=np.float64) """ if not NO_OBST: for i in xrange(nx): for j in xrange(ny): for k in xrange(nz): if ((i-4)**2+(j-5)**2+(k-6)**2) < 6: BOUND[i,j,k] += 1.0 BOUNDi[i,j,k] += 0.0 BOUND[:,0,:] += 1.0 BOUNDi[:,0,:] *= 0.0 """ if util.Benchmark().bohrium: np.flush() for ts in xrange(0, ITER): ##Propagate / Streaming step T[:] = F #nearest-neighbours F[1,:,:,0] = T[1,:,:,-1] F[1,:,:,1:] = T[1,:,:,:-1] F[2,:,:,:-1] = T[2,:,:,1:] F[2,:,:,-1] = T[2,:,:,0] F[3,:,0,:] = T[3,:,-1,:] F[3,:,1:,:] = T[3,:,:-1,:] F[4,:,:-1,:] = T[4,:,1:,:] F[4,:,-1,:] = T[4,:,0,:] F[5,0,:,:] = T[5,-1,:,:] F[5,1:,:,:] = T[5,:-1,:,:] F[6,:-1,:,:] = T[6,1:,:,:] F[6,-1,:,:] = T[6,0,:,:] #next-nearest neighbours F[7,0 ,0 ,:] = T[7,-1 , -1,:] F[7,0 ,1:,:] = T[7,-1 ,:-1,:] F[7,1:,0 ,:] = T[7,:-1, -1,:] F[7,1:,1:,:] = T[7,:-1,:-1,:] F[8,0 ,:-1,:] = T[8,-1 ,1:,:] F[8,0 , -1,:] = T[8,-1 ,0 ,:] F[8,1:,:-1,:] = T[8,:-1,1:,:] F[8,1:, -1,:] = T[8,:-1,0 ,:] F[9,:-1,0 ,:] = T[9,1:, -1,:] F[9,:-1,1:,:] = T[9,1:,:-1,:] F[9,-1 ,0 ,:] = T[9,0 , 0,:] F[9,-1 ,1:,:] = T[9,0 ,:-1,:] F[10,:-1,:-1,:] = T[10,1:,1:,:] F[10,:-1, -1,:] = T[10,1:,0 ,:] F[10,-1 ,:-1,:] = T[10,0 ,1:,:] F[10,-1 , -1,:] = T[10,0 ,0 ,:] F[11,0 ,:,0 ] = T[11,0 ,:, -1] F[11,0 ,:,1:] = T[11,0 ,:,:-1] F[11,1:,:,0 ] = T[11,:-1,:, -1] F[11,1:,:,1:] = T[11,:-1,:,:-1] F[12,0 ,:,:-1] = T[12, -1,:,1:] F[12,0 ,:, -1] = T[12, -1,:,0 ] F[12,1:,:,:-1] = T[12,:-1,:,1:] F[12,1:,:, -1] = T[12,:-1,:,0 ] F[13,:-1,:,0 ] = T[13,1:,:, -1] F[13,:-1,:,1:] = T[13,1:,:,:-1] F[13, -1,:,0 ] = T[13,0 ,:, -1] F[13, -1,:,1:] = T[13,0 ,:,:-1] F[14,:-1,:,:-1] = T[14,1:,:,1:] F[14,:-1,:, -1] = T[14,1:,:,0 ] F[14,-1 ,:,:-1] = T[14,0 ,:,1:] F[14,-1 ,:, -1] = T[14,0 ,:,0 ] F[15,:,0 ,0 ] = T[15,:, -1, -1] F[15,:,0 ,1:] = T[15,:, -1,:-1] F[15,:,1:,0 ] = T[15,:,:-1, -1] F[15,:,1:,1:] = T[15,:,:-1,:-1] F[16,:,0 ,:-1] = T[16,:, -1,1:] F[16,:,0 , -1] = T[16,:, -1,0 ] F[16,:,1:,:-1] = T[16,:,:-1,1:] F[16,:,1:, -1] = T[16,:,:-1,0 ] F[17,:,:-1,0 ] = T[17,:,1:, -1] F[17,:,:-1,1:] = T[17,:,1:,:-1] F[17,:, -1,0 ] = T[17,:,0 , -1] F[17,:, -1,1:] = T[17,:,0 ,:-1] F[18,:,:-1,:-1] = T[18,:,1:,1:] F[18,:,:-1, -1] = T[18,:,1:,0 ] F[18,:,-1 ,:-1] = T[18,:,0 ,1:] F[18,:,-1 , -1] = T[18,:,0 ,0 ] #Densities bouncing back at next timestep BB = np.empty(F.shape) T[:] = F T[1:,:,:,:] *= BOUND[np.newaxis,:,:,:] BB[2 ,:,:,:] += T[1 ,:,:,:] BB[1 ,:,:,:] += T[2 ,:,:,:] BB[4 ,:,:,:] += T[3 ,:,:,:] BB[3 ,:,:,:] += T[4 ,:,:,:] BB[6 ,:,:,:] += T[5 ,:,:,:] BB[5 ,:,:,:] += T[6 ,:,:,:] BB[10,:,:,:] += T[7 ,:,:,:] BB[9 ,:,:,:] += T[8 ,:,:,:] BB[8 ,:,:,:] += T[9 ,:,:,:] BB[7 ,:,:,:] += T[10,:,:,:] BB[14,:,:,:] += T[11,:,:,:] BB[13,:,:,:] += T[12,:,:,:] BB[12,:,:,:] += T[13,:,:,:] BB[11,:,:,:] += T[14,:,:,:] BB[18,:,:,:] += T[15,:,:,:] BB[17,:,:,:] += T[16,:,:,:] BB[16,:,:,:] += T[17,:,:,:] BB[15,:,:,:] += T[18,:,:,:] # Relax calculate equilibrium state (FEQ) with equivalent speed and density to F DENSITY = np.add.reduce(F) #UX = F[5,:,:,:].copy() UX = np.ones(F[5,:,:,:].shape, dtype=np.float64) UX[:,:,:] = F[5,:,:,:] UX += F[7,:,:,:] UX += F[8,:,:,:] UX += F[11,:,:,:] UX += F[12,:,:,:] UX -= F[6,:,:,:] UX -= F[9,:,:,:] UX -= F[10,:,:,:] UX -= F[13,:,:,:] UX -= F[14,:,:,:] UX /=DENSITY #UY = F[3,:,:,:].copy() UY = np.ones(F[3,:,:,:].shape, dtype=np.float64) UY[:,:,:] = F[3,:,:,:] UY += F[7,:,:,:] UY += F[9,:,:,:] UY += F[15,:,:,:] UY += F[16,:,:,:] UY -= F[4,:,:,:] UY -= F[8,:,:,:] UY -= F[10,:,:,:] UY -= F[17,:,:,:] UY -= F[18,:,:,:] UY /=DENSITY #UZ = F[1,:,:,:].copy() UZ = np.ones(F[1,:,:,:].shape, dtype=np.float64) UZ[:,:,:] = F[1,:,:,:] UZ += F[11,:,:,:] UZ += F[13,:,:,:] UZ += F[15,:,:,:] UZ += F[17,:,:,:] UZ -= F[2,:,:,:] UZ -= F[12,:,:,:] UZ -= F[14,:,:,:] UZ -= F[16,:,:,:] UZ -= F[18,:,:,:] UZ /=DENSITY UX[0,:,:] += deltaU #Increase inlet pressure #Set bourderies to zero. UX[:,:,:] *= BOUNDi UY[:,:,:] *= BOUNDi UZ[:,:,:] *= BOUNDi DENSITY[:,:,:] *= BOUNDi U_SQU = UX**2 + UY**2 + UZ**2 # Calculate equilibrium distribution: stationary FEQ[0,:,:,:] = (t1*DENSITY)*(1.0-3.0*U_SQU/2.0) # nearest-neighbours T1 = 3.0/2.0*U_SQU tDENSITY = t2*DENSITY FEQ[1,:,:,:]=tDENSITY*(1.0 + 3.0*UZ + 9.0/2.0*UZ**2 - T1) FEQ[2,:,:,:]=tDENSITY*(1.0 - 3.0*UZ + 9.0/2.0*UZ**2 - T1) FEQ[3,:,:,:]=tDENSITY*(1.0 + 3.0*UY + 9.0/2.0*UY**2 - T1) FEQ[4,:,:,:]=tDENSITY*(1.0 - 3.0*UY + 9.0/2.0*UY**2 - T1) FEQ[5,:,:,:]=tDENSITY*(1.0 + 3.0*UX + 9.0/2.0*UX**2 - T1) FEQ[6,:,:,:]=tDENSITY*(1.0 - 3.0*UX + 9.0/2.0*UX**2 - T1) # next-nearest neighbours T1 = 3.0*U_SQU/2.0 tDENSITY = t3*DENSITY U8 = UX+UY FEQ[7,:,:,:] =tDENSITY*(1.0 + 3.0*U8 + 9.0/2.0*(U8)**2 - T1) U9 = UX-UY FEQ[8,:,:,:] =tDENSITY*(1.0 + 3.0*U9 + 9.0/2.0*(U9)**2 - T1) U10 = -UX+UY FEQ[9,:,:,:] =tDENSITY*(1.0 + 3.0*U10 + 9.0/2.0*(U10)**2 - T1) U8 *= -1.0 FEQ[10,:,:,:]=tDENSITY*(1.0 + 3.0*U8 + 9.0/2.0*(U8)**2 - T1) U12 = UX+UZ FEQ[11,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1) U12 *= 1.0 FEQ[14,:,:,:]=tDENSITY*(1.0 + 3.0*U12 + 9.0/2.0*(U12)**2 - T1) U13 = UX-UZ FEQ[12,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1) U13 *= -1.0 FEQ[13,:,:,:]=tDENSITY*(1.0 + 3.0*U13 + 9.0/2.0*(U13)**2 - T1) U16 = UY+UZ FEQ[15,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1) U17 = UY-UZ FEQ[16,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1) U17 *= -1.0 FEQ[17,:,:,:]=tDENSITY*(1.0 + 3.0*U17 + 9.0/2.0*(U17)**2 - T1) U16 *= -1.0 FEQ[18,:,:,:]=tDENSITY*(1.0 + 3.0*U16 + 9.0/2.0*(U16)**2 - T1) F *= (1.0-omega) F += omega * FEQ #Densities bouncing back at next timestep F[1:,:,:,:] *= BOUNDi[np.newaxis,:,:,:] F[1:,:,:,:] += BB[1:,:,:,:] del BB del T1 del UX, UY, UZ del U_SQU del DENSITY, tDENSITY del U8, U9, U10, U12, U13, U16, U17 if util.Benchmark().bohrium: np.flush() B.stop() B.pprint() if B.outputfn: B.tofile(B.outputfn, {'res': UX}) """
def run(*inputs, device='cpu'): isoneutral_diffusion_pre(*inputs) bh.flush() return inputs[-7:]
def sor_compute(full): cells = full[1:-1, 1:-1] black1 = full[1:-1:2, 1:-1:2] black2 = full[2:-1:2, 2:-1:2] red1 = full[1:-1:2, 2:-1:2] red2 = full[2:-1:2, 1:-1:2] black1_up = full[0:-3:2, 1:-1:2] black2_up = red1 black1_right = red1 black2_right = full[2:-1:2, 3::2] black1_left = full[1:-2:2, 0:-2:2] black2_left = red2 black1_down = red2 black2_down = full[3::2, 2:-1:2] red1_up = full[0:-2:2, 2:-1:2] red2_up = black1 red1_right = full[1:-2:2, 3::2] red2_right = black2 red1_left = black1 red2_left = full[2:-1:2, 0:-3:2] red1_down = black2 red2_down = full[3::2, 1:-2:2] epsilon = full.shape[0]**2 * 0.002 delta = epsilon + 1 i = 0 while epsilon < delta: i += 1 diff = black1.copy() black1 += black1_up black1 += black1_right black1 += black1_left black1 += black1_down black1 *= 0.2 diff -= black1 np.absolute(diff, diff) delta = np.add.reduce(np.add.reduce(diff)) diff = black2.copy() black2 += black2_up black2 += black2_right black2 += black2_left black2 += black2_down black2 *= 0.2 diff -= black2 np.absolute(diff, diff) delta += np.add.reduce(np.add.reduce(diff)) diff = red1.copy() red1 += red1_up red1 += red1_right red1 += red1_left red1 += red1_down red1 *= 0.2 diff -= red1 delta += np.add.reduce(np.add.reduce(diff)) diff = red2.copy() red2 += red2_up red2 += red2_right red2 += red2_left red2 += red2_down red2 *= 0.2 diff -= red2 delta += np.add.reduce(np.add.reduce(diff)) np.flush() return cells
def run(sa, ct, p, device='cpu'): out = gsw_dHdT(sa, ct, p) bh.flush() return out
def sor_compute(full): cells = full[1:-1, 1:-1] black1 = full[1:-1:2, 1:-1:2] black2 = full[2:-1:2, 2:-1:2] red1 = full[1:-1:2, 2:-1:2] red2 = full[2:-1:2, 1:-1:2] black1_up = full[0:-3:2, 1:-1:2] black2_up = red1 black1_right = red1 black2_right = full[2:-1:2, 3::2] black1_left = full[1:-2:2, 0:-2:2] black2_left = red2 black1_down = red2 black2_down = full[3::2, 2:-1:2] red1_up = full[0:-2:2, 2:-1:2] red2_up = black1 red1_right = full[1:-2:2, 3::2] red2_right = black2 red1_left = black1 red2_left = full[2:-1:2, 0:-3:2] red1_down = black2 red2_down = full[3::2, 1:-2:2] epsilon=full.shape[0]**2*0.002 delta=epsilon+1 i=0 while epsilon<delta: i+=1 diff = black1.copy() black1 += black1_up black1 += black1_right black1 += black1_left black1 += black1_down black1 *= 0.2 diff -= black1 np.absolute(diff, diff) delta = np.add.reduce(np.add.reduce(diff)) diff = black2.copy() black2 += black2_up black2 += black2_right black2 += black2_left black2 += black2_down black2 *= 0.2 diff -= black2 np.absolute(diff, diff) delta += np.add.reduce(np.add.reduce(diff)) diff = red1.copy() red1 += red1_up red1 += red1_right red1 += red1_left red1 += red1_down red1 *= 0.2 diff -= red1 delta += np.add.reduce(np.add.reduce(diff)) diff = red2.copy() red2 += red2_up red2 += red2_right red2 += red2_left red2 += red2_down red2 *= 0.2 diff -= red2 delta += np.add.reduce(np.add.reduce(diff)) np.flush() return cells
def run(*inputs, device='cpu'): outputs = integrate_tke(*inputs) bh.flush() return outputs
def flush(self, ignore_no_flush_arg=False): """Executes the queued instructions when running through Bohrium. Set `ignore_no_flush_arg=True` to flush even when the --no-flush argument is used""" if bh_is_loaded_as_np: if ignore_no_flush_arg or not self.args.no_flush: bohrium.flush()