class MUMPSSolver(LinearSolver): """ Interface to MUMPS solver. """ name = 'ls.mumps' __metaclass__ = SolverMeta _parameters = [] def __init__(self, conf, **kwargs): try: from mumps import DMumpsContext except ImportError: self.mumps = None msg = 'cannot import MUMPS!' raise ImportError(msg) LinearSolver.__init__(self, conf, **kwargs) self.mumps = DMumpsContext() self.mumps_presolved = False @standard_call def __call__(self, rhs, x0=None, conf=None, eps_a=None, eps_r=None, i_max=None, mtx=None, status=None, **kwargs): context = self.mumps if not self.mumps_presolved: self.presolve(mtx) out = rhs.copy() context.set_rhs(out) context.run(job=3) # Solve return out def presolve(self, mtx): is_new, mtx_digest = _is_new_matrix(mtx, self.mtx_digest) if is_new: mtx_coo = mtx.tocoo() context = self.mumps if not self.conf.verbose: context.set_silent() context.set_shape(mtx_coo.shape[0]) context.set_centralized_assembled(mtx_coo.row + 1, mtx_coo.col + 1, mtx_coo.data) context.run(job=1) # Analyze context.run(job=2) # Factorize self.mumps_presolved = True self.mtx_digest = mtx_digest def __del__(self): if self.mumps is not None: self.mumps.destroy()
def __init__(self, conf, **kwargs): try: from mumps import DMumpsContext except ImportError: self.mumps = None msg = 'cannot import MUMPS!' raise ImportError(msg) LinearSolver.__init__(self, conf, **kwargs) self.mumps = DMumpsContext() self.mumps_presolved = False
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): 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
return (1-s)**2 if args.reload[0] is not None: n = int(float(args.reload[1])*3600*24/args.DT) with open(args.reload[0], 'rb') as f: simulator = pickle.load(f) simulator.add_time(n) #writer = VTKMeshWriter(simulation=simulator.run) #writer.run() writer = VTKMeshWriter() simulator.run(ctx=ctx, writer=writer) else: ctx = DMumpsContext() ctx.set_silent() with open(args.mesh, 'rb') as f: mesh = pickle.load(f) # 导入地质网格模型 mesh.fluid_relative_permeability_0 = water mesh.fluid_relative_permeability_1 = oil simulator = TwoFluidsWithGeostressSimulator(mesh, args) writer = VTKMeshWriter(simulation=simulator.run, args=(ctx, None)) writer.run() #writer = VTKMeshWriter() #simulator.run(ctx=ctx, writer=writer) ctx.destroy()
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
mesh = pde.init_mesh(n=n) space = LagrangeFiniteElementSpace(mesh, p=p, q=4) uh = space.function(dim=2) 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(
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()