def FEM_field_V1(coeff, x, T, p, bc): t = T[1:-1] D = bsp.collocation_matrix(t, p - 1, x, bc, normalize=True) eva = np.dot(D, coeff) return eva
def evaluate_field_V0(vec, x, p, Nbase, T, bc): """ Evaluates the 1d FEM field in the space V0 at the points x. Parameters ---------- vec : np.array coefficient vector x : np.array evaluation points p : int spline degree Nbase : int number of spline functions T : np.array knot vector bc : boolean boundary conditions (True = periodic, False = homogeneous Dirichlet, None = no boundary conditions) Returns ------- eva: np.arrray the function values at the points x """ if bc == True: N = bsp.collocation_matrix(T, p, x, bc) elif bc == False: N = bsp.collocation_matrix(T, p, x, bc)[:, 1:-1] else: N = bsp.collocation_matrix(T, p, x, False) eva = np.dot(N, vec) return eva
def evaluate_field_V1(vec, x, p, Nbase, T, bc): """ Evaluates the 1d FEM field in the space V1 at the points x. Parameters ---------- vec : np.array coefficient vector x : np.array evaluation points p : int spline degree Nbase : int number of spline functions T : np.array knot vector bc : boolean boundary conditions (True = periodic, False = homogeneous Dirichlet, None = no boundary conditions) Returns ------- eval: np.arrray the function values at the points x """ t = T[1:-1] if bc == True: D = bsp.collocation_matrix(t, p - 1, x, bc, normalize=True) else: D = bsp.collocation_matrix(t, p - 1, x, False, normalize=True) eva = D.dot(vec) return eva
def __init__(self, T, p, bc): self.p = p self.T = T self.bc = bc self.greville = bsp.greville(T, p, bc) self.breakpoints = bsp.breakpoints(T, p) self.Nbase = len(self.breakpoints) - 1 + p - bc*p self.pts_loc, self.wts_loc = np.polynomial.legendre.leggauss(p) self.interpolation_V0 = bsp.collocation_matrix(T, p, self.greville, bc) self.histopolation_V1 = histo(T, p, bc, self.greville)
def histo(T, p, bc, grev): el_b = bsp.breakpoints(T, p) Nel = len(el_b) - 1 t = T[1:-1] Nbase = Nel + p - bc*p if bc == True: pts_loc, wts_loc = np.polynomial.legendre.leggauss(p - 1) ne = Nbase D = np.zeros((ne, ne)) if p%2 != 0: grid = el_b pts, wts = bsp.quadrature_grid(grid, pts_loc, wts_loc) col_quad = bsp.collocation_matrix(t, p - 1, pts.flatten(), bc, normalize=True) for ie in range(Nel): for il in range(p): i = (ie + il)%ne for k in range(p - 1): D[ie, i] += wts[ie, k]*col_quad[ie*(p - 1) + k, i] return D else: grid = np.linspace(0., el_b[-1], 2*Nel + 1) pts, wts = bsp.quadrature_grid(grid, pts_loc, wts_loc) col_quad = bsp.collocation_matrix(t, p - 1, pts.flatten(), bc, normalize=True) for iee in range(2*Nel): for il in range(p): ie = int(iee/2) ie_grev = int(np.ceil(iee/2) - 1) i = (ie + il)%ne for k in range(p - 1): D[ie_grev, i] += wts[iee, k]*col_quad[iee*(p - 1) + k, i] return D else: ng = len(grev) col_quad = bsp.collocation_matrix(T, p, grev, bc) D = np.zeros((ng - 1, Nbase - 1)) for i in range(ng - 1): for j in range(max(i - p + 1, 1), min(i + p + 3, Nbase)): s = 0. for k in range(j): s += col_quad[i, k] - col_quad[i + 1, k] D[i, j - 1] = s return D
def FEM_field_V0(coeff, x, T, p, bc): N = bsp.collocation_matrix(T, p, x, bc) eva = np.dot(N, coeff) return eva
T1 = T0[1:-1] M0 = fem.mass_V0(T0, p, bc) M1 = fem.mass_V1(T0, p, bc) MB = fem.mass_V0_B(T0, p, bc, B_background_z) G = sc.sparse.csc_matrix(fem.GRAD(T0, p, bc)) if bc == False: M0 = M0[1:-1, 1:-1] MB = MB[1:-1, 1:-1] G = G[:, 1:-1] D = sc.sparse.csr_matrix( bsp.collocation_matrix(T1, p - 1, eva_points_Bx, bc, normalize=True)) print('matrix assembly done!') #==================================================================================== #=================== coefficients for pp-forms ====================================== if p == 3: pp_0 = np.asfortranarray( [[1 / 6, -1 / (2 * dz), 1 / (2 * dz**2), -1 / (6 * dz**3)], [2 / 3, 0., -1 / dz**2, 1 / (2 * dz**3)], [1 / 6, 1 / (2 * dz), 1 / (2 * dz**2), -1 / (2 * dz**3)], [0., 0., 0., 1 / (6 * dz**3)]]) pp_1 = np.asfortranarray([[1 / 2, -1 / dz, 1 / (2 * dz**2)], [1 / 2, 1 / dz, -1 / dz**2], [0., 0., 1 / (2 * dz**2)]]) / dz elif p == 2: pp_0 = np.asfortranarray([[1 / 2, -1 / dz, 1 / (2 * dz**2)],
#==================================================================================== #===== spline knot vector, global mass matrices (in V0 and V1) and gradient matrix == Tz = bsp.make_knots(el_b, p, False) tz = Tz[1:-1] M0, C0 = utils_opt.matrixAssembly_V0(p, Nbase0, Tz, False) M1 = utils_opt.matrixAssembly_V1(p, Nbase0, Tz, False) Mb = utils_opt.matrixAssembly_backgroundField(p, Nbase0, Tz, False, B_background_z) G = utils_opt.GRAD_1d(p, Nbase0, False) D = bsp.collocation_matrix(tz, p - 1, eva_points_Bx, False, normalize=True) print('matrix assembly done!') #==================================================================================== #===== reserve memory for unknowns ================================================== ex = np.empty(Nbase0) ey = np.empty(Nbase0) bx = np.empty(Nbase1) by = np.empty(Nbase1) yx = np.empty(Nbase0) yy = np.empty(Nbase0) uj = np.empty(4 * Nbase0_0 + 2 * Nbase1_0) z_old = np.empty(Np)
def histopolation_matrix_1d(p, Nbase, T, grev, bc): """ Computest the 1d histopolation matrix. Parameters ---------- p : int spline degree Nbase : int number of spline functions T : np.array knot vector grev : np.array greville points bc : boolean boundary conditions (True = periodic, False = homogeneous Dirichlet, None = no boundary conditions) Returns ------- D : 2d np.array histopolation matrix """ el_b = bsp.breakpoints(T, p) Nel = len(el_b) - 1 t = T[1:-1] if bc == True: pts_loc, wts_loc = np.polynomial.legendre.leggauss(p - 1) ne = Nbase - p D = np.zeros((ne, ne)) if p % 2 != 0: grid = el_b pts, wts = bsp.quadrature_grid(grid, pts_loc, wts_loc) col_quad = bsp.collocation_matrix(t, p - 1, pts.flatten(), bc, normalize=True) for ie in range(Nel): for il in range(p): i = (ie + il) % ne for k in range(p - 1): D[ie, i] += wts[ie, k] * col_quad[ie * (p - 1) + k, i] return D else: grid = np.linspace(0., el_b[-1], 2 * Nel + 1) pts, wts = bsp.quadrature_grid(grid, pts_loc, wts_loc) col_quad = bsp.collocation_matrix(t, p - 1, pts.flatten(), bc, normalize=True) for iee in range(2 * Nel): for il in range(p): ie = int(iee / 2) ie_grev = int(np.ceil(iee / 2) - 1) i = (ie + il) % ne for k in range(p - 1): D[ie_grev, i] += wts[iee, k] * col_quad[iee * (p - 1) + k, i] return D else: ng = len(grev) col_quad = bsp.collocation_matrix(T, p, grev, bc) D = np.zeros((ng - 1, Nbase - 1)) for i in range(ng - 1): for j in range(max(i - p + 1, 1), min(i + p + 3, Nbase)): s = 0. for k in range(j): s += col_quad[i, k] - col_quad[i + 1, k] D[i, j - 1] = s return D