class Mumps(): A = None ctx = None x = None def __init__(self, A, **kwagrs): self.ctx = DMumpsContext() self.A = A.tocsc() self.ctx.set_icntl(14, 60) self.ctx.set_centralized_sparse(A) # print 'Factoring' self.ctx.set_silent() # print 'Done' self.ctx.run(job=4) # Factorization def solve(self,b): # print 'Solving' self.x = b.copy() self.ctx.set_rhs(self.x) self.ctx.run(job=3) # Solve # print 'Done' return self.x def clean(self): self.ctx.destroy()
def solve(self): mesh = self.mesh NE = mesh.number_of_edges() NC = mesh.number_of_cells() itype = mesh.itype ftype = mesh.ftype A = self.get_left_matrix() b = self.get_right_vector() x = np.r_[self.uh, self.ph] #把self.uh,self.ph组合在一起 b = b - A @ x # Modify matrix bdIdx = np.zeros((A.shape[0], ), dtype=itype) bdIdx[NE] = 1 Tbd = spdiags(bdIdx, 0, A.shape[0], A.shape[1]) T = spdiags(1 - bdIdx, 0, A.shape[0], A.shape[1]) AD = T @ A @ T + Tbd scipy.sparse.save_npz('AD.npz', AD) b[NE] = self.ph[0] with open("b.csv", "w", newline="") as datacsv: csvwriter = csv.writer(datacsv, dialect=("excel")) csvwriter.writerow(['b']) csvwriter.writerows([b]) # solve x[:] = spsolve(AD, b) # solve from mumps import DMumpsContext ctx = DMumpsContext() if ctx.myid == 0: ctx.set_centralized_sparse(AD.tocoo()) x = b.copy() ctx.set_rhs(x) ctx.set_silent() ctx.run(job=6) self.uh[:] = x[:NE] self.ph[:] = x[NE:] print('ph', self.ph) print('pI', self.pI) return x
def solve(self): mesh = self.mesh NE = mesh.number_of_edges() NC = mesh.number_of_cells() itype = mesh.itype ftype = mesh.ftype A = self.get_left_matrix() b = self.get_right_vector() x = np.r_[self.uh, self.ph] #把self.uh,self.ph组合在一起 b = b - A @ x # Modify matrix bdIdx = np.zeros((A.shape[0], ), dtype=itype) bdIdx[NE] = 1 Tbd = spdiags(bdIdx, 0, A.shape[0], A.shape[1]) T = spdiags(1 - bdIdx, 0, A.shape[0], A.shape[1]) AD = T @ A @ T + Tbd b[NE] = self.ph[0] K = AD.todense() # solve x[:] = spsolve(AD, b) # solve from mumps import DMumpsContext ctx = DMumpsContext() if ctx.myid == 0: ctx.set_centralized_sparse(AD.tocoo()) x = b.copy() ctx.set_rhs(x) ctx.set_silent() ctx.run(job=6) self.uh[:] = x[:NE] self.ph[:] = x[NE:] print('ph', self.ph) print('pI', self.pI) return x
def picard_iteration(self, maxit=10): GD = self.GD e0 = 1.0 k = 0 while e0 > 1e-10: # 构建总系统 A, F, isBdDof = self.get_total_system() # 处理边界条件, 这里是 0 边界 gdof = len(isBdDof) bdIdx = np.zeros(gdof, dtype=np.int_) bdIdx[isBdDof] = 1 Tbd = diags(bdIdx) T = diags(1 - bdIdx) A = T @ A @ T + Tbd F[isBdDof] = 0.0 # 求解 if False: x = spsolve(A, F) else: ctx = DMumpsContext() if ctx.myid == 0: ctx.set_centralized_sparse(A) x = F.copy() ctx.set_rhs(x) # Modified in place ctx.run(job=6) # Analysis + Factorization + Solve ctx.destroy() # Cleanup vgdof = self.vspace.number_of_global_dofs() pgdof = self.pspace.number_of_global_dofs() cgdof = self.cspace.number_of_global_dofs() e0 = 0.0 start = 0 end = vgdof self.cv[:] = x[start:end] start = end end += pgdof e0 += np.sum((self.cp - x[start:end])**2) self.cp[:] = x[start:end] start = end end += pgdof e0 += np.sum((self.cs - x[start:end])**2) self.cs[:] = x[start:end] e0 = np.sqrt(e0) # 误差的 l2 norm k += 1 for i in range(GD): start = end end += cgdof self.cu[:, i] = x[start:end] print(e0) if k >= maxit: print('picard iteration arrive max iteration with error:', e0) break
A = space.linear_elasticity_matrix(lam, mu) #A = space.recovery_linear_elasticity_matrix(lam, mu) F = space.source_vector(pde.source, dim=2) bc = NeumannBC(space, pde.neumann, threshold=pde.is_neumann_boundary) F = bc.apply(F) bc = DirichletBC(space, pde.dirichlet, threshold=pde.is_dirichlet_boundary) A, F = bc.apply(A, F, uh) ctx = DMumpsContext() ctx.set_silent() if ctx.myid == 0: ctx.set_centralized_sparse(A) x = F.copy() ctx.set_rhs(x) # Modified in place ctx.run(job=6) # Analysis + Factorization + Solve ctx.destroy() # Cleanup uh.T.flat[:] = x # uh.T.flat[:] = spsolve(A, F) # (2, gdof ).flat uI = space.interpolation(pde.displacement, dim=2) e = uh - uI #error = np.sqrt(np.mean(e**2)) error = sum(np.sqrt(np.sum(e**2, axis=-1))) / sum( np.sqrt(np.sum(uI**2, axis=-1))) print('error:\n', error) bc = mesh.entity_barycenter('edge')