def initialize(self, d_idx, d_normal_tmp, d_normal): idx = declare('int') idx = 3 * d_idx d_normal_tmp[idx] = 0.0 d_normal_tmp[idx + 1] = 0.0 d_normal_tmp[idx + 2] = 0.0 d_normal[idx] = 0.0 d_normal[idx + 1] = 0.0 d_normal[idx + 2] = 0.0
def loop(self, d_idx, s_idx, s_V, d_gradv, d_h, s_h, XIJ, DWIJ, VIJ): alp, bet, d = declare('int', 3) d = self.dim Vj = 1.0/s_V[s_idx] for alp in range(d): for bet in range(d): d_gradv[d_idx*d*d + d*bet + alp] += -Vj * VIJ[alp] * DWIJ[bet]
def loop(self, d_idx, s_idx, d_ai, d_gradai, d_cwij, d_bi, d_gradbi, s_ai, s_gradai, s_bi, s_gradbi, WIJ, DWIJ, XIJ, HIJ): alp, gam, d = declare('int', 3) res = declare('matrix(3)') dbxij = declare('matrix(3)') dbxji = declare('matrix(3)') d = self.dim ai = d_ai[d_idx] aj = s_ai[s_idx] eps = 1.0e-04 * HIJ bxij = 0.0 bxji = 0.0 for alp in range(d): bxij += d_bi[d * d_idx + alp] * XIJ[alp] bxji -= s_bi[d * s_idx + alp] * XIJ[alp] for gam in range(d): temp = 0.0 temp1 = 0.0 for alp in range(d): temp += d_gradbi[d * d * d_idx + d * gam + alp] * XIJ[alp] temp1 -= s_gradbi[d * d * s_idx + d * gam + alp] * XIJ[alp] dbxij[gam] = temp dbxji[gam] = temp1 d_cwij[d_idx] = 1.0 / (ai * (1 + bxij)) for gam in range(d): temp = ((ai * DWIJ[gam] + d_gradai[d * d_idx + gam] * WIJ) * (1 + bxij)) temp += ai * (dbxij[gam] + d_bi[d * d_idx + gam]) * WIJ temp += ((aj * DWIJ[gam] - s_gradai[d * s_idx + gam] * WIJ) * (1 + bxji)) temp -= aj * (dbxji[gam] + s_bi[d * s_idx + gam]) * WIJ res[gam] = 0.5 * temp res_mag = 0.0 dwij_mag = 0.0 for i in range(d): res_mag += abs(res[i]) dwij_mag += abs(DWIJ[i]) change = abs(res_mag - dwij_mag) / (dwij_mag + eps) if change < self.tol: for i in range(d): DWIJ[i] = res[i]
def initialize(self, d_idx, d_uho, d_Buh, d_vho, d_Bvh, d_who, d_Bwh): i = declare('int') for i in range(3): d_uho[4 * d_idx + i] = 0.0 d_Buh[4 * d_idx + i] = 0.0 d_vho[4 * d_idx + i] = 0.0 d_Bvh[4 * d_idx + i] = 0.0 d_who[4 * d_idx + i] = 0.0 d_Bwh[4 * d_idx + i] = 0.0
def identity(a=[0.0, 0.0], n=3): """Initialize an identity matrix. """ i, j = declare('int', 2) for i in range(n): for j in range(n): if i == j: a[n * i + j] = 1.0 else: a[n * i + j] = 0.0
def initialize(self, d_idx, d_ctr, d_diag, d_col_idx): # Make only the diagonals zero as the rest are not summed. d_diag[d_idx] = 0.0 d_ctr[d_idx] = 0 # Initialize col_idx to -1 so as to use it in cond while constructing # pressure coeff matrix. i = declare('int') for i in range(100): d_col_idx[d_idx*100 + i] = -1
def velocity(i, x, y, gamma, u, v, nv): tmp = declare('matrix(2)') xi = x[i] yi = y[i] u[i] = 0.0 v[i] = 0.0 for j in range(nv): point_vortex(xi, yi, x[j], y[j], gamma[j], tmp) u[i] += tmp[0] v[i] += tmp[1]
def initialize(self, d_idx, d_uo, d_Bu, d_vo, d_Bv, d_wo, d_Bw): i = declare('int') for i in range(3): d_uo[4 * d_idx + i] = 0.0 d_Bu[4 * d_idx + i] = 0.0 d_vo[4 * d_idx + i] = 0.0 d_Bv[4 * d_idx + i] = 0.0 d_wo[4 * d_idx + i] = 0.0 d_Bw[4 * d_idx + i] = 0.0
def loop(self, d_idx, s_idx, d_ai, d_gradai, d_cwij, d_bi, d_gradbi, s_ai, s_gradai, s_bi, s_gradbi, d_h, s_h, WIJ, DWIJ, XIJ, HIJ, RIJ, DWI, DWJ, WI, WJ, SPH_KERNEL): alp, gam, d = declare('int', 3) dbxij = declare('matrix(3)') dbxji = declare('matrix(3)') dwij, dwji = declare('matrix(3)', 2) SPH_KERNEL.gradient(XIJ, RIJ, d_h[d_idx], dwij) SPH_KERNEL.gradient(XIJ, RIJ, s_h[s_idx], dwji) wij = SPH_KERNEL.kernel(XIJ, RIJ, d_h[d_idx]) wji = SPH_KERNEL.kernel(XIJ, RIJ, s_h[s_idx]) d = self.dim ai = d_ai[d_idx] aj = s_ai[s_idx] bxij = 0.0 bxji = 0.0 for alp in range(d): bxij += d_bi[d * d_idx + alp] * XIJ[alp] bxji -= s_bi[d * s_idx + alp] * XIJ[alp] for gam in range(d): temp = 0.0 temp1 = 0.0 for alp in range(d): temp += d_gradbi[d * d * d_idx + d * gam + alp] * XIJ[alp] temp1 -= s_gradbi[d * d * s_idx + d * gam + alp] * XIJ[alp] dbxij[gam] = temp dbxji[gam] = temp1 d_cwij[d_idx] = (ai * (1 + bxij)) WI = (ai * (1 + bxij)) * WIJ WJ = (aj * (1 + bxji)) * WIJ for gam in range(d): temp = ((ai * dwij[gam] + d_gradai[d * d_idx + gam] * wij) * (1 + bxij)) temp += ai * (dbxij[gam] + d_bi[d * d_idx + gam]) * wij temp1 = ((-aj * dwji[gam] + s_gradai[d * s_idx + gam] * wji) * (1 + bxji)) temp1 += aj * (dbxji[gam] + s_bi[d * s_idx + gam]) * wji DWIJ[gam] = 0.5 * (temp - temp1) DWI[gam] = temp DWJ[gam] = temp1
def loop(self, d_idx, d_h, s_h, s_x, s_y, s_z, d_x, d_y, d_z, s_rho, s_m, s_idx, XIJ, DWIJ, WIJ, s_p, d_Bp): Vj = s_m[s_idx] / s_rho[s_idx] pj = s_p[s_idx] i4 = declare('int') i4 = 4 * d_idx d_Bp[i4 + 0] += pj * WIJ * Vj d_Bp[i4 + 1] += pj * DWIJ[0] * Vj d_Bp[i4 + 2] += pj * DWIJ[1] * Vj d_Bp[i4 + 3] += pj * DWIJ[2] * Vj
def stage2(self, d_idx, d_uhat, d_vhat, d_what, d_x, d_y, d_z, d_rho, d_arho, d_sigma, d_asigma, dt): d_rho[d_idx] += dt * d_arho[d_idx] i = declare('int') for i in range(9): d_sigma[d_idx * 9 + i] += dt * d_asigma[d_idx * 9 + i] d_x[d_idx] += dt * d_uhat[d_idx] d_y[d_idx] += dt * d_vhat[d_idx] d_z[d_idx] += dt * d_what[d_idx]
def loop(self, d_p0, s_m, s_idx, d_rho, d_idx, d_auhat, d_avhat, d_awhat, XIJ, RIJ, SPH_KERNEL, HIJ): rhoi2 = d_rho[d_idx] * d_rho[d_idx] tmp = -d_p0[d_idx] * s_m[s_idx] / rhoi2 dwijhat = declare('matrix(3)') SPH_KERNEL.gradient(XIJ, RIJ, self.hij_fac * HIJ, dwijhat) d_auhat[d_idx] += tmp * dwijhat[0] d_avhat[d_idx] += tmp * dwijhat[1] d_awhat[d_idx] += tmp * dwijhat[2]
def initialize(self, d_rho, d_rho0, d_rhs, d_diag, d_idx, d_coeff, d_ctr, d_col_idx, d_row_idx): i = declare('int') if d_rho[d_idx]/d_rho0[d_idx] < 0.98: d_rhs[d_idx] = 0.0 d_diag[d_idx] = 1.0 d_ctr[d_idx] = 1 for i in range(100): d_coeff[d_idx*100 + i] = 0.0 d_col_idx[d_idx*100 + i] = -1 d_row_idx[d_idx*100 + i] = d_idx
def loop(self, d_idx, d_h, s_h, s_x, s_y, s_z, d_x, d_y, d_z, s_rho, s_m, WIJ, DWIJ, s_temp_prop, d_p_sph, s_idx): i4 = declare('int') Vj = s_m[s_idx] / s_rho[s_idx] pj = s_temp_prop[s_idx] i4 = 4 * d_idx d_p_sph[i4 + 0] += pj * WIJ * Vj d_p_sph[i4 + 1] += pj * DWIJ[0] * Vj d_p_sph[i4 + 2] += pj * DWIJ[1] * Vj d_p_sph[i4 + 3] += pj * DWIJ[2] * Vj
def loop_all(self, d_idx, d_rho, d_x, d_y, d_z, s_m, s_rhotmp, s_x, s_y, s_z, d_h, s_h, SPH_KERNEL, NBRS, N_NBRS): i, s_idx = declare('int', 2) xij = declare('matrix(3)') tmp_w = 0.0 x = d_x[d_idx] y = d_y[d_idx] z = d_z[d_idx] d_rho[d_idx] = 0.0 for i in range(N_NBRS): s_idx = NBRS[i] xij[0] = x - s_x[s_idx] xij[1] = y - s_y[s_idx] xij[2] = z - s_z[s_idx] rij = sqrt(xij[0] * xij[0] + xij[1] * xij[1] + xij[2] * xij[2]) hij = (d_h[d_idx] + s_h[s_idx]) * 0.5 wij = SPH_KERNEL.kernel(xij, rij, hij) tmp_w += wij * s_m[s_idx] / s_rhotmp[s_idx] d_rho[d_idx] += wij * s_m[s_idx] d_rho[d_idx] /= tmp_w
def loop(self, d_idx, s_idx, d_rho, s_rho, d_u, d_v, d_w, d_uhat, d_vhat, d_what, s_u, s_v, s_w, s_uhat, s_vhat, s_what, d_au, d_av, d_aw, s_m, DWIJ): rhoi = d_rho[d_idx] rhoj = s_rho[s_idx] i, j = declare('int', 2) ui, uj, uidif, ujdif, res = declare('matrix(3)', 5) Aij = declare('matrix(9)') for i in range(3): res[i] = 0.0 for j in range(3): Aij[3 * i + j] = 0.0 ui[0] = d_u[d_idx] ui[1] = d_v[d_idx] ui[2] = d_w[d_idx] uj[0] = s_u[s_idx] uj[1] = s_v[s_idx] uj[2] = s_w[s_idx] uidif[0] = d_uhat[d_idx] - d_u[d_idx] uidif[1] = d_vhat[d_idx] - d_v[d_idx] uidif[2] = d_what[d_idx] - d_w[d_idx] ujdif[0] = s_uhat[s_idx] - s_u[s_idx] ujdif[1] = s_vhat[s_idx] - s_v[s_idx] ujdif[2] = s_what[s_idx] - s_w[s_idx] for i in range(3): for j in range(3): Aij[3 * i + j] = (ui[i] * uidif[j] / rhoi + uj[i] * ujdif[j] / rhoj) mat_vec_mult(Aij, DWIJ, 3, res) d_au[d_idx] += s_m[s_idx] * res[0] d_av[d_idx] += s_m[s_idx] * res[1] d_aw[d_idx] += s_m[s_idx] * res[2]
def loop(self, d_idx, d_xn, d_yn, d_zn, d_x, d_y, d_z, d_disp): d_xn[d_idx] = self.xn d_yn[d_idx] = self.yn d_zn[d_idx] = self.zn xij = declare('matrix(3)') xij[0] = d_x[d_idx] - self.xo xij[1] = d_y[d_idx] - self.yo xij[2] = d_z[d_idx] - self.zo d_disp[d_idx] = abs(xij[0] * d_xn[d_idx] + xij[1] * d_yn[d_idx] + xij[2] * d_yn[d_idx])
def llxf(rhol=0.0, rhor=1.0, pl=0.0, pr=1.0, ul=0.0, ur=1.0, gamma=1.4, niter=20, tol=1e-6, result=[0.0, 0.0]): """Lax Friedrichs approximate Riemann solver for the Euler equations to determine the intermediate states. Parameters ---------- rhol, rhor: double: left and right density. pl, pr: double: left and right pressure. ul, ur: double: left and right speed. gamma: double: Ratio of specific heats. niter: int: Max number of iterations to try for iterative schemes. tol: double: Error tolerance for convergence. result: list: List of length 2. Result will contain computed pstar, ustar Returns ------- Returns 0 if the value is computed correctly else 1 if the iterations either did not converge or if there is an error. """ gamma1, csl, csr, cslr, el, El, er, Er, pstar = declare('double', 9) gamma1 = 1. / (gamma - 1.0) # Lagrangian sound speeds csl = sqrt(gamma * pl * rhol) csr = sqrt(gamma * pr * rhor) cslr = max(csr, csl) # Total energy on either side el = pl * gamma1 / rhol El = el + 0.5 * ul * ul er = pr * gamma1 / rhor Er = er + 0.5 * ur * ur # the intermediate states # cdef double ustar = 0.5 * ( ul + ur - 1./cslr * (pr - pl) ) pstar = 0.5 * (pl + pr - cslr * (ur - ul)) result[0] = pstar result[1] = 1. / pstar * (0.5 * ((pl * ul + pr * ur) - cslr * (Er - El))) return 0
def map_cid_to_idx(i, x, y, z, num_particles, cell_size, xmin, ymin, zmin, pids, keys, cids, cid_to_idx): cid = cids[i] if i != 0 and cids[i] == cids[i - 1]: return c = declare('matrix(3, "int")') pid = pids[i] find_cell_id(x[pid] - xmin, y[pid] - ymin, z[pid] - zmin, cell_size, c) nbr_boxes = declare('matrix(27, "ulong")') nbr_boxes_length = neighbor_boxes(c[0], c[1], c[2], nbr_boxes) for j in range(nbr_boxes_length): key = nbr_boxes[j] idx = find_idx(keys, num_particles, key) cid_to_idx[27 * cid + j] = idx
def initialize(self, d_idx, d_tag, d_orig_idx, d_dijpj0, d_dijpj1, d_dijpj2, d_dii0, d_dii1, d_dii2, d_piter): idx = declare('int') if d_tag[d_idx] == 2: idx = d_orig_idx[d_idx] d_dijpj0[d_idx] = d_dijpj0[idx] d_dijpj1[d_idx] = d_dijpj1[idx] d_dijpj2[d_idx] = d_dijpj2[idx] d_dii0[d_idx] = d_dii0[idx] d_dii1[d_idx] = d_dii1[idx] d_dii2[d_idx] = d_dii2[idx] d_piter[d_idx] = d_piter[idx]
def post_loop(self, d_idx, d_A, d_po, d_Bp): a_mat = declare('matrix(16)') aug_mat = declare('matrix(20)') b_p = declare('matrix(4)') res_p = declare('matrix(4)') i, n, i16, i4 = declare('int', 4) i16 = 16 * d_idx i4 = 4 * d_idx for i in range(16): a_mat[i] = d_A[i16 + i] for i in range(20): aug_mat[i] = 0.0 for i in range(4): b_p[i] = d_Bp[i4 + i] res_p[i] = 0.0 n = self.dim + 1 augmented_matrix(a_mat, b_p, n, 1, 4, aug_mat) gj_solve(aug_mat, n, 1, res_p) for i in range(4): d_po[i4 + i] = res_p[i]
def post_loop(self, d_idx, d_normal_tmp, d_h): idx = declare('int') idx = 3 * d_idx mag = sqrt(d_normal_tmp[idx]**2 + d_normal_tmp[idx + 1]**2 + d_normal_tmp[idx + 2]**2) if mag > 0.25 / d_h[d_idx]: d_normal_tmp[idx] /= mag d_normal_tmp[idx + 1] /= mag d_normal_tmp[idx + 2] /= mag else: d_normal_tmp[idx] = 0.0 d_normal_tmp[idx + 1] = 0.0 d_normal_tmp[idx + 2] = 0.0
def post_loop(self, d_idx, d_normal, d_h): idx = declare('int') idx = 3 * d_idx mag = sqrt(d_normal[idx]**2 + d_normal[idx + 1]**2 + d_normal[idx + 2]**2) if mag > 1e-3: d_normal[idx] /= mag d_normal[idx + 1] /= mag d_normal[idx + 2] /= mag else: d_normal[idx] = 0.0 d_normal[idx + 1] = 0.0 d_normal[idx + 2] = 0.0
def velocity(x, y, gamma, u, v, xc, yc, gc, nv): i, gid, nb = declare('int', 3) j, ti, nt, jb = declare('int', 4) ti = LID_0 nt = LDIM_0 gid = GID_0 i = gid * nt + ti idx = declare('int') tmp = declare('matrix(2)') uj, vj = declare('double', 2) nb = GDIM_0 if i < nv: xi = x[i] yi = y[i] uj = 0.0 vj = 0.0 for jb in range(nb): idx = jb * nt + ti if idx < nv: xc[ti] = x[idx] yc[ti] = y[idx] gc[ti] = gamma[idx] else: gc[ti] = 0.0 local_barrier() if i < nv: for j in range(nt): point_vortex(xi, yi, xc[j], yc[j], gc[j], tmp) uj += tmp[0] vj += tmp[1] local_barrier() if i < nv: u[i] = uj v[i] = vj
def initialize(self, d_idx, d_uag, d_pag, d_uta, d_pta): i6, i, N = declare('int', 3) N = 6 i6 = N * d_idx d_uta[d_idx] = 0.0 d_pta[d_idx] = 0.0 for i in range(N): d_uta[d_idx] += d_uag[i6+i] d_pta[d_idx] += d_pag[i6+i] d_uta[d_idx] /= N d_pta[d_idx] /= N
def post_loop(self, d_idx, d_moment, d_prop, d_p_sph): a_mat = declare('matrix(16)') aug_mat = declare('matrix(20)') b = declare('matrix(4)') res = declare('matrix(4)') i, n, i16, i4 = declare('int', 4) i16 = 16 * d_idx i4 = 4 * d_idx for i in range(16): a_mat[i] = d_moment[i16 + i] for i in range(20): aug_mat[i] = 0.0 for i in range(4): b[i] = d_p_sph[4 * d_idx + i] res[i] = 0.0 n = self.dim + 1 augmented_matrix(a_mat, b, n, 1, 4, aug_mat) gj_solve(aug_mat, n, 1, res) for i in range(4): d_prop[i4 + i] = res[i]
def loop(self, d_idx, d_m_mat, d_dw_gamma, d_cwij, DWIJ, HIJ): i, j, n, nt = declare('int', 4) n = self.dim nt = n + 1 temp = declare('matrix(12)') # The augmented matrix res = declare('matrix(3)') dwij = declare('matrix(3)') eps = 1.0e-04 * HIJ for i in range(n): dwij[i] = (DWIJ[i] - d_dw_gamma[3 * d_idx + i]) / d_cwij[d_idx] for j in range(n): temp[nt * i + j] = d_m_mat[9 * d_idx + 3 * i + j] temp[nt * i + n] = dwij[i] gj_solve(temp, n, 1, res) res_mag = 0.0 dwij_mag = 0.0 for i in range(n): res_mag += abs(res[i]) dwij_mag += abs(dwij[i]) change = abs(res_mag - dwij_mag) / (dwij_mag + eps) if change < self.tol: for i in range(n): DWIJ[i] = res[i]
def loop_all(self, d_idx, d_m_mat, s_m, s_rho, d_x, d_y, d_z, d_h, s_x, s_y, s_z, s_h, SPH_KERNEL, NBRS, N_NBRS): x = d_x[d_idx] y = d_y[d_idx] z = d_z[d_idx] h = d_h[d_idx] i, j, s_idx, n = declare('int', 4) xij = declare('matrix(3)') dwij = declare('matrix(3)') n = self.dim for k in range(N_NBRS): s_idx = NBRS[k] xij[0] = x - s_x[s_idx] xij[1] = y - s_y[s_idx] xij[2] = z - s_z[s_idx] hij = (h + s_h[s_idx]) * 0.5 r = sqrt(xij[0] * xij[0] + xij[1] * xij[1] + xij[2] * xij[2]) SPH_KERNEL.gradient(xij, r, hij, dwij) V = s_m[s_idx] / s_rho[s_idx] if r > 1.0e-12: for i in range(n): for j in range(n): xj = xij[j] d_m_mat[9 * d_idx + 3 * i + j] -= V * dwij[i] * xj
def mat_vec_mult(a=[1.0, 0.0], b=[1.0, 0.0], n=3, result=[0.0, 0.0]): """Multiply a square matrix with a vector. Parameters ---------- a: list b: list n: int : number of rows/columns result: list """ i, j = declare('int', 2) for i in range(n): s = 0.0 for j in range(n): s += a[n * i + j] * b[j] result[i] = s
def post_loop(self, d_idx, d_A, d_uho, d_Buh, d_vho, d_Bvh, d_who, d_Bwh): a_mat = declare('matrix(16)') aug_mat = declare('matrix(20)') b_uh = declare('matrix(4)') res_uh = declare('matrix(4)') b_vh = declare('matrix(4)') res_vh = declare('matrix(4)') b_wh = declare('matrix(4)') res_wh = declare('matrix(4)') i, n, i16, i4 = declare('int', 4) i16 = 16 * d_idx i4 = 4 * d_idx for i in range(16): a_mat[i] = d_A[i16 + i] for i in range(20): aug_mat[i] = 0.0 for i in range(4): b_uh[i] = d_Buh[i4 + i] res_uh[i] = 0.0 b_vh[i] = d_Bvh[i4 + i] res_vh[i] = 0.0 b_wh[i] = d_Bwh[i4 + i] res_wh[i] = 0.0 n = self.dim + 1 augmented_matrix(a_mat, b_uh, n, 1, 4, aug_mat) gj_solve(aug_mat, n, 1, res_uh) for i in range(4): d_uho[i4 + i] = res_uh[i] augmented_matrix(a_mat, b_vh, n, 1, 4, aug_mat) gj_solve(aug_mat, n, 1, res_vh) for i in range(4): d_vho[i4 + i] = res_vh[i] augmented_matrix(a_mat, b_wh, n, 1, 4, aug_mat) gj_solve(aug_mat, n, 1, res_wh) for i in range(4): d_who[i4 + i] = res_wh[i]