def eval_div(gfr, gfi=None): ''' evaluate divergence ''' fes = getattr(gfr, getFESpace)() ordering = fes.GetOrdering() mesh = getattr(fes, getMesh)() vdim = 1 sdim = mesh.SpaceDimension() p = fes.GetOrder(0) l2_coll = mfem.L2_FECollection(p, 1) l2s = FiniteElementSpace(mesh, l2_coll, vdim, ordering) div = DiscreteLinearOperator(fes, l2s) itp = mfem.DivergenceInterpolator() div.AddDomainInterpolator(itp) div.Assemble() div.Finalize() br = GridFunction(rts) div.Mult(gfr, br) if gfi is not None: bi = GridFunction(rts) div.Mult(gfi, bi) else: bi = None ### needs to return rts to prevent rts to be collected. return br, bi, (rt_coll, l2s)
def save_scaled_jacobian(filename, mesh, sd=-1): sj = get_scaled_jacobian(mesh, sd=sd) fec = mfem.L2_FECollection(0, mesh.Dimension()) fes = mfem.FiniteElementSpace(mesh, fec) vec = mfem.Vector(sj) gf = mfem.GridFunction(fes, vec.GetData()) gf.Save(filename)
b.AddDomainIntegrator(mfem.DomainLFIntegrator(rhs)) # 8. The solution vector x and the associated finite element grid function # will be maintained over the AMR iterations. x = mfem.ParGridFunction(fespace) # 9. Connect to GLVis. if visualization: sout = mfem.socketstream("localhost", 19916) sout.precision(8) # 10. As in Example 6p, we set up a Zienkiewicz-Zhu estimator that will be # used to obtain element error indicators. The integrator needs to # provide the method ComputeElementFlux. We supply an L2 space for the # discontinuous flux and an H(div) space for the smoothed flux. flux_fec = mfem.L2_FECollection(order, dim) flux_fes = mfem.ParFiniteElementSpace(pmesh, flux_fec, sdim) smooth_flux_fec = mfem.RT_FECollection(order - 1, dim) smooth_flux_fes = mfem.ParFiniteElementSpace(pmesh, smooth_flux_fec) estimator = mfem.L2ZienkiewiczZhuEstimator(integ, x, flux_fes, smooth_flux_fes) # 11. As in Example 6p, we also need a refiner. This time the refinement # strategy is based on a fixed threshold that is applied locally to each # element. The global threshold is turned off by setting the total error # fraction to zero. We also enforce a maximum refinement ratio between # adjacent elements. refiner = mfem.ThresholdRefiner(estimator) refiner.SetTotalErrorFraction(0.0) # purely local threshold refiner.SetLocalErrorGoal(max_elem_error) refiner.PreferConformingRefinement() refiner.SetNCLimit(nc_limit)
trial_order = order trace_order = order - 1 test_order = order # reduced order, full order is (order + dim - 1) if (dim == 2 and (order % 2 == 0 or (pmesh.MeshGenerator() & 2 and order > 1))): test_order = test_order + 1 if (test_order < trial_order): if myid == 0: print( "Warning, test space not enriched enough to handle primal trial space" ) x0_fec = mfem.H1_FECollection(trial_order, dim) xhat_fec = mfem.RT_Trace_FECollection(trace_order, dim) test_fec = mfem.L2_FECollection(test_order, dim) x0_space = mfem.ParFiniteElementSpace(pmesh, x0_fec) xhat_space = mfem.ParFiniteElementSpace(pmesh, xhat_fec) test_space = mfem.ParFiniteElementSpace(pmesh, test_fec) glob_true_s0 = x0_space.GlobalTrueVSize() glob_true_s1 = xhat_space.GlobalTrueVSize() glob_true_s_test = test_space.GlobalTrueVSize() if myid == 0: print('\n'.join([ "nNumber of Unknowns", " Trial space, X0 : " + str(glob_true_s0) + " (order " + str(trial_order) + ")", " Interface space, Xhat : " + str(glob_true_s1) + " (order " + str(trace_order) + ")", " Test space, Y : " +
meshfile = expanduser(join(path, 'data', 'star.mesh')) mesh = mfem.Mesh(meshfile, 1, 1) dim = mesh.Dimension() ref_levels = int(np.floor(np.log(10000. / mesh.GetNE()) / np.log(2.) / dim)) for x in range(ref_levels): mesh.UniformRefinement() pmesh = mfem.ParMesh(MPI.COMM_WORLD, mesh) par_ref_levels = 2 for l in range(par_ref_levels): pmesh.UniformRefinement() hdiv_coll = mfem.RT_FECollection(order, dim) l2_coll = mfem.L2_FECollection(order, dim) R_space = mfem.ParFiniteElementSpace(pmesh, hdiv_coll) W_space = mfem.ParFiniteElementSpace(pmesh, l2_coll) dimR = R_space.GlobalTrueVSize() dimW = W_space.GlobalTrueVSize() if verbose: print("***********************************************************") print("dim(R) = " + str(dimR)) print("dim(W) = " + str(dimW)) print("dim(R+W) = " + str(dimR + dimW)) print("***********************************************************") block_offsets = intArray([0, R_space.GetVSize(), W_space.GetVSize()])
true_size = fespace.TrueVSize() true_offset = mfem.intArray(3) true_offset[0] = 0 true_offset[1] = true_size true_offset[2] = 2 * true_size vx = mfem.BlockVector(true_offset) v_gf = mfem.ParGridFunction(fespace) x_gf = mfem.ParGridFunction(fespace) x_ref = mfem.ParGridFunction(fespace) pmesh.GetNodes(x_ref) w_fec = mfem.L2_FECollection(order + 1, dim) w_fespace = mfem.ParFiniteElementSpace(pmesh, w_fec) w_gf = mfem.ParGridFunction(w_fespace) # 8. Set the initial conditions for v_gf, x_gf and vx, and define the # boundary conditions on a beam-like mesh (see description above). class InitialVelocity(mfem.VectorPyCoefficient): def EvalValue(self, x): dim = len(x) s = 0.1 / 64. v = np.zeros(len(x)) v[-1] = s * x[0]**2 * (8.0 - x[0]) v[0] = -s * x[0]**2 return v
def do_integration(expr, solvars, phys, mesh, kind, attrs, order, num): from petram.helper.variables import (Variable, var_g, NativeCoefficientGenBase, CoefficientVariable) from petram.phys.coefficient import SCoeff st = parser.expr(expr) code= st.compile('<string>') names = code.co_names g = {} #print solvars.keys() for key in phys._global_ns.keys(): g[key] = phys._global_ns[key] for key in solvars.keys(): g[key] = solvars[key] l = var_g.copy() ind_vars = ','.join(phys.get_independent_variables()) if kind == 'Domain': size = max(max(mesh.attributes.ToList()), max(attrs)) else: size = max(max(mesh.bdr_attributes.ToList()), max(attrs)) arr = [0]*(size) for k in attrs: arr[k-1] = 1 flag = mfem.intArray(arr) s = SCoeff(expr, ind_vars, l, g, return_complex=False) ## note L2 does not work for boundary....:D if kind == 'Domain': fec = mfem.L2_FECollection(order, mesh.Dimension()) else: fec = mfem.H1_FECollection(order, mesh.Dimension()) fes = mfem.FiniteElementSpace(mesh, fec) one = mfem.ConstantCoefficient(1) gf = mfem.GridFunction(fes) gf.Assign(0.0) if kind == 'Domain': gf.ProjectCoefficient(mfem.RestrictedCoefficient(s, flag)) else: gf.ProjectBdrCoefficient(mfem.RestrictedCoefficient(s, flag), flag) b = mfem.LinearForm(fes) one = mfem.ConstantCoefficient(1) if kind == 'Domain': itg = mfem.DomainLFIntegrator(one) b.AddDomainIntegrator(itg) else: itg = mfem.BoundaryLFIntegrator(one) b.AddBoundaryIntegrator(itg) b.Assemble() from petram.engine import SerialEngine en = SerialEngine() ans = mfem.InnerProduct(en.x2X(gf), en.b2B(b)) if not np.isfinite(ans): print("not finite", ans, arr) print(size, mesh.bdr_attributes.ToList()) from mfem.common.chypre import LF2PyVec, PyVec2PyMat, Array2PyVec, IdentityPyMat #print(list(gf.GetDataArray())) print(len(gf.GetDataArray()), np.sum(gf.GetDataArray())) print(np.sum(list(b.GetDataArray()))) return ans