def __init__(self, fespace, alpha, kappa, u): mfem.PyTimeDependentOperator.__init__(self, fespace.GetTrueVSize(), 0.0) rel_tol = 1e-8 self.alpha = alpha self.kappa = kappa self.T = None self.K = None self.M = None self.fespace = fespace self.ess_tdof_list = intArray() self.Mmat = mfem.SparseMatrix() self.Kmat = mfem.SparseMatrix() self.M_solver = mfem.CGSolver() self.M_prec = mfem.DSmoother() self.T_solver = mfem.CGSolver() self.T_prec = mfem.DSmoother() self.z = mfem.Vector(self.Height()) self.M = mfem.BilinearForm(fespace) self.M.AddDomainIntegrator(mfem.MassIntegrator()) self.M.Assemble() self.M.FormSystemMatrix(self.ess_tdof_list, self.Mmat) self.M_solver.iterative_mode = False self.M_solver.SetRelTol(rel_tol) self.M_solver.SetAbsTol(0.0) self.M_solver.SetMaxIter(30) self.M_solver.SetPrintLevel(0) self.M_solver.SetPreconditioner(self.M_prec) self.M_solver.SetOperator(self.Mmat) self.T_solver.iterative_mode = False self.T_solver.SetRelTol(rel_tol) self.T_solver.SetAbsTol(0.0) self.T_solver.SetMaxIter(100) self.T_solver.SetPrintLevel(0) self.T_solver.SetPreconditioner(self.T_prec) self.SetParameters(u)
# 9. Set up the bilinear form a(.,.) on the DG finite element space # corresponding to the linear elasticity integrator with coefficients # lambda and mu as defined above. The additional interior face integrator # ensures the weak continuity of the displacement field. The additional # boundary face integrator works together with the boundary integrator # added to the linear form b(.) to impose weakly the Dirichlet boundary # conditions. a = mfem.BilinearForm(fespace) a.AddDomainIntegrator(mfem.ElasticityIntegrator(lambda_c, mu_c)) a.AddInteriorFaceIntegrator(mfem.DGElasticityIntegrator(lambda_c, mu_c, alpha, kappa)) a.AddBdrFaceIntegrator(mfem.DGElasticityIntegrator(lambda_c, mu_c, alpha, kappa), dir_bdr) print('matrix ...') a.Assemble() A = mfem.SparseMatrix() B = mfem.Vector() X = mfem.Vector() a.FormLinearSystem(ess_tdof_list, x, b, A, X, B); print('...done') A.PrintInfo(sys.stdout) # 11. Define a simple symmetric Gauss-Seidel preconditioner and use it to # solve the system Ax=b with PCG for the symmetric formulation, or GMRES # for the non-symmetric. M = mfem.GSSmoother(A) rtol = 1e-6 if (alpha == -1.0): mfem.PCG(A, M, B, X, 3, 5000, rtol*rtol, 0.0) else:
import numpy as np from scipy.sparse import csr_matrix import mfem.ser as mfem smat = csr_matrix([[ 1, 2, 3, ], [0, 0, 1], [2, 0, 0]]) print mfem.SparseMatrix(smat) print smat mmat = mfem.SparseMatrix(smat) mmat.Print()
import sys import numpy as np from scipy.sparse import csr_matrix if len(sys.argv) < 2: import mfem.ser as mfem elif sys.argv[1] == 'par': import mfem.par as mfem smat = csr_matrix([[ 1, 2, 3, ], [0, 0, 1], [2, 0, 0]]) mmat = mfem.SparseMatrix(smat) offset1 = mfem.intArray([0, 3, 6]) offset2 = mfem.intArray([0, 3, 6]) m = mfem.BlockMatrix(offset1, offset2) m.SetBlock(0, 0, mmat) print m._offsets print m._linked_mat m = mfem.BlockMatrix(offset1) m.SetBlock(0, 1, mmat) m.SetBlock(1, 0, mmat) print m._offsets print m._linked_mat
def run(order=1, static_cond=False, meshfile=def_meshfile, visualization=False): mesh = mfem.Mesh(meshfile, 1, 1) dim = mesh.Dimension() # 3. Refine the mesh to increase the resolution. In this example we do # 'ref_levels' of uniform refinement. We choose 'ref_levels' to be the # largest number that gives a final mesh with no more than 50,000 # elements. ref_levels = int(np.floor( np.log(50000. / mesh.GetNE()) / np.log(2.) / dim)) for x in range(ref_levels): mesh.UniformRefinement() #5. Define a finite element space on the mesh. Here we use vector finite # elements, i.e. dim copies of a scalar finite element space. The vector # dimension is specified by the last argument of the FiniteElementSpace # constructor. For NURBS meshes, we use the (degree elevated) NURBS space # associated with the mesh nodes. if order > 0: fec = mfem.H1_FECollection(order, dim) elif mesh.GetNodes(): fec = mesh.GetNodes().OwnFEC() prinr("Using isoparametric FEs: " + str(fec.Name())) else: order = 1 fec = mfem.H1_FECollection(order, dim) fespace = mfem.FiniteElementSpace(mesh, fec) print('Number of finite element unknowns: ' + str(fespace.GetTrueVSize())) # 5. Determine the list of true (i.e. conforming) essential boundary dofs. # In this example, the boundary conditions are defined by marking all # the boundary attributes from the mesh as essential (Dirichlet) and # converting them to a list of true dofs. ess_tdof_list = mfem.intArray() if mesh.bdr_attributes.Size() > 0: ess_bdr = mfem.intArray([1] * mesh.bdr_attributes.Max()) ess_bdr = mfem.intArray(mesh.bdr_attributes.Max()) ess_bdr.Assign(1) fespace.GetEssentialTrueDofs(ess_bdr, ess_tdof_list) #6. Set up the linear form b(.) which corresponds to the right-hand side of # the FEM linear system, which in this case is (1,phi_i) where phi_i are # the basis functions in the finite element fespace. b = mfem.LinearForm(fespace) one = mfem.ConstantCoefficient(1.0) b.AddDomainIntegrator(mfem.DomainLFIntegrator(one)) b.Assemble() #7. Define the solution vector x as a finite element grid function # corresponding to fespace. Initialize x with initial guess of zero, # which satisfies the boundary conditions. x = mfem.GridFunction(fespace) x.Assign(0.0) #8. Set up the bilinear form a(.,.) on the finite element space # corresponding to the Laplacian operator -Delta, by adding the Diffusion # domain integrator. a = mfem.BilinearForm(fespace) a.AddDomainIntegrator(mfem.DiffusionIntegrator(one)) #9. Assemble the bilinear form and the corresponding linear system, # applying any necessary transformations such as: eliminating boundary # conditions, applying conforming constraints for non-conforming AMR, # static condensation, etc. if static_cond: a.EnableStaticCondensation() a.Assemble() A = mfem.SparseMatrix() B = mfem.Vector() X = mfem.Vector() a.FormLinearSystem(ess_tdof_list, x, b, A, X, B) print("Size of linear system: " + str(A.Size())) # 10. Solve M = mfem.GSSmoother(A) mfem.PCG(A, M, B, X, 1, 200, 1e-12, 0.0) # 11. Recover the solution as a finite element grid function. a.RecoverFEMSolution(X, b, x) # 12. Save the refined mesh and the solution. This output can be viewed later # using GLVis: "glvis -m refined.mesh -g sol.gf". mesh.PrintToFile('refined.mesh', 8) x.SaveToFile('sol.gf', 8) #13. Send the solution by socket to a GLVis server. if (visualization): sol_sock = mfem.socketstream("localhost", 19916) sol_sock.precision(8) sol_sock.send_solution(mesh, x)
import numpy as np from scipy.sparse import csr_matrix import mfem.ser as mfem smat = csr_matrix([[ 1, 2, 3, ], [0, 0, 1], [2, 0, 0]]) print(mfem.SparseMatrix(smat)) print(smat) mmat = mfem.SparseMatrix(smat) mmat.Print()