def SetParameters(self, u): u_alpha_gf = mfem.GridFunction(self.fespace) u_alpha_gf.SetFromTrueDofs(u) for i in range(u_alpha_gf.Size()): u_alpha_gf[i] = self.kappa + self.alpha * u_alpha_gf[i] self.K = mfem.BilinearForm(self.fespace) u_coeff = mfem.GridFunctionCoefficient(u_alpha_gf) self.K.AddDomainIntegrator(mfem.DiffusionIntegrator(u_coeff)) self.K.Assemble() self.K.FormSystemMatrix(self.ess_tdof_list, self.Kmat) self.T = None
def __init__(self, spaces, ess_bdr, block_offsets, rel_tol, abs_tol, iter, mu): # Array<Vector *> -> tuple super(RubberOperator, self).__init__(spaces[0].GetVSize() + spaces[1].GetVSize()) rhs = (None, None) self.spaces = spaces self.mu = mfem.ConstantCoefficient(mu) self.block_offsets = block_offsets Hform = mfem.BlockNonlinearForm(spaces) Hform.AddDomainIntegrator( mfem.IncompressibleNeoHookeanIntegrator(self.mu)) Hform.SetEssentialBC(ess_bdr, rhs) self.Hform = Hform a = mfem.BilinearForm(self.spaces[1]) one = mfem.ConstantCoefficient(1.0) a.AddDomainIntegrator(mfem.MassIntegrator(one)) a.Assemble() a.Finalize() pressure_mass = a.LoseMat() self.j_prec = JacobianPreconditioner(spaces, pressure_mass, block_offsets) j_gmres = mfem.GMRESSolver() j_gmres.iterative_mode = False j_gmres.SetRelTol(1e-12) j_gmres.SetAbsTol(1e-12) j_gmres.SetMaxIter(300) j_gmres.SetPrintLevel(0) j_gmres.SetPreconditioner(self.j_prec) self.j_solver = j_gmres newton_solver = mfem.NewtonSolver() # Set the newton solve parameters newton_solver.iterative_mode = True newton_solver.SetSolver(self.j_solver) newton_solver.SetOperator(self) newton_solver.SetPrintLevel(1) newton_solver.SetRelTol(rel_tol) newton_solver.SetAbsTol(abs_tol) newton_solver.SetMaxIter(iter) self.newton_solver = newton_solver
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)
# VectorFunctionCoefficient 'x_init' which in turn is based on the # function 'InitDisplacement'. b = mfem.LinearForm(fespace) print('r.h.s ...') integrator = mfem.DGElasticityDirichletLFIntegrator(init_x, lambda_c, mu_c, alpha, kappa) b.AddBdrFaceIntegrator(integrator , dir_bdr) b.Assemble() # 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)
return 0.0 # Inflow boundary condition (zero for the problems considered in this example) class inflow_coeff(mfem.PyCoefficient): def EvalValue(self, x): return 0 # 6. Set up and assemble the bilinear and linear forms corresponding to the # DG discretization. The DGTraceIntegrator involves integrals over mesh # interior faces. velocity = velocity_coeff(dim) inflow = inflow_coeff() u0 = u0_coeff() m = mfem.BilinearForm(fes) m.AddDomainIntegrator(mfem.MassIntegrator()) k = mfem.BilinearForm(fes) k.AddDomainIntegrator(mfem.ConvectionIntegrator(velocity, -1.0)) k.AddInteriorFaceIntegrator( mfem.TransposeIntegrator(mfem.DGTraceIntegrator(velocity, 1.0, -0.5))) k.AddBdrFaceIntegrator( mfem.TransposeIntegrator(mfem.DGTraceIntegrator(velocity, 1.0, -0.5))) b = mfem.LinearForm(fes) b.AddBdrFaceIntegrator( mfem.BoundaryFlowIntegrator(inflow, velocity, -1.0, -0.5)) m.Assemble() m.Finalize() skip_zeros = 0
x = mfem.BlockVector(block_offsets) rhs = mfem.BlockVector(block_offsets) fform = mfem.LinearForm() fform.Update(R_space, rhs.GetBlock(0), 0) fform.AddDomainIntegrator(mfem.VectorFEDomainLFIntegrator(fcoeff)) fform.AddBoundaryIntegrator(mfem.VectorFEBoundaryFluxLFIntegrator(fnatcoeff)) fform.Assemble() gform = mfem.LinearForm() gform.Update(W_space, rhs.GetBlock(1), 0) gform.AddDomainIntegrator(mfem.DomainLFIntegrator(gcoeff)) gform.Assemble() mVarf = mfem.BilinearForm(R_space) bVarf = mfem.MixedBilinearForm(R_space, W_space) mVarf.AddDomainIntegrator(mfem.VectorFEMassIntegrator(k)) mVarf.Assemble() mVarf.Finalize() M = mVarf.SpMat() bVarf.AddDomainIntegrator(mfem.VectorFEDivergenceIntegrator()) bVarf.Assemble() bVarf.Finalize() B = bVarf.SpMat() B *= -1 BT = mfem.Transpose(B) darcyOp = mfem.BlockOperator(block_offsets)
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.OperatorPtr() B = mfem.Vector() X = mfem.Vector() a.FormLinearSystem(ess_tdof_list, x, b, A, X, B) print("Size of linear system: " + str(A.Height())) # 10. Solve AA = mfem.OperatorHandle2SparseMatrix(A) M = mfem.GSSmoother(AA) mfem.PCG(AA, 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.Print('refined.mesh', 8) x.Save('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)
def __init__(self, fespace, ess_bdr, visc, mu, K): mfem.PyTimeDependentOperator.__init__(self, 2 * fespace.GetVSize(), 0.0) rel_tol = 1e-8 skip_zero_entries = 0 ref_density = 1.0 self.z = mfem.Vector(self.Height() / 2) self.fespace = fespace self.viscosity = visc M = mfem.BilinearForm(fespace) S = mfem.BilinearForm(fespace) H = mfem.NonlinearForm(fespace) self.M = M self.H = H self.S = S rho = mfem.ConstantCoefficient(ref_density) M.AddDomainIntegrator(mfem.VectorMassIntegrator(rho)) M.Assemble(skip_zero_entries) M.EliminateEssentialBC(ess_bdr) M.Finalize(skip_zero_entries) M_solver = mfem.CGSolver() M_prec = mfem.DSmoother() M_solver.iterative_mode = False M_solver.SetRelTol(rel_tol) M_solver.SetAbsTol(0.0) M_solver.SetMaxIter(30) M_solver.SetPrintLevel(0) M_solver.SetPreconditioner(M_prec) M_solver.SetOperator(M.SpMat()) self.M_solver = M_solver self.M_prec = M_prec model = mfem.NeoHookeanModel(mu, K) H.AddDomainIntegrator(mfem.HyperelasticNLFIntegrator(model)) H.SetEssentialBC(ess_bdr) self.model = model visc_coeff = mfem.ConstantCoefficient(visc) S.AddDomainIntegrator(mfem.VectorDiffusionIntegrator(visc_coeff)) S.Assemble(skip_zero_entries) S.EliminateEssentialBC(ess_bdr) S.Finalize(skip_zero_entries) self.backward_euler_oper = BackwardEulerOperator(M, S, H) J_prec = mfem.DSmoother(1) J_minres = mfem.MINRESSolver() J_minres.SetRelTol(rel_tol) J_minres.SetAbsTol(0.0) J_minres.SetMaxIter(300) J_minres.SetPrintLevel(-1) J_minres.SetPreconditioner(J_prec) self.J_solver = J_minres self.J_prec = J_prec newton_solver = mfem.NewtonSolver() newton_solver.iterative_mode = False newton_solver.SetSolver(self.J_solver) newton_solver.SetOperator(self.backward_euler_oper) newton_solver.SetPrintLevel(1) #print Newton iterations newton_solver.SetRelTol(rel_tol) newton_solver.SetAbsTol(0.0) newton_solver.SetMaxIter(10) self.newton_solver = newton_solver
# and the stiffness matrix on the continuous trial space, S0. ess_bdr = mfem.intArray(mesh.bdr_attributes.Max()) ess_bdr.Assign(1) B0 = mfem.MixedBilinearForm(x0_space,test_space); B0.AddDomainIntegrator(mfem.DiffusionIntegrator(one)) B0.Assemble() B0.EliminateTrialDofs(ess_bdr, x.GetBlock(x0_var), F) B0.Finalize() Bhat = mfem.MixedBilinearForm(xhat_space,test_space) Bhat.AddTraceFaceIntegrator(mfem.TraceJumpIntegrator()) Bhat.Assemble() Bhat.Finalize() Sinv = mfem.BilinearForm(test_space) Sum = mfem.SumIntegrator() Sum.AddIntegrator(mfem.DiffusionIntegrator(one)) Sum.AddIntegrator(mfem.MassIntegrator(one)) Sinv.AddDomainIntegrator(mfem.InverseIntegrator(Sum)) Sinv.Assemble() Sinv.Finalize() S0 = mfem.BilinearForm(x0_space) S0.AddDomainIntegrator(mfem.DiffusionIntegrator(one)) S0.Assemble() S0.EliminateEssentialBC(ess_bdr) S0.Finalize() matB0 = B0.SpMat() matBhat = Bhat.SpMat()