예제 #1
0
one = mfem.ConstantCoefficient(1.0)
bdr = BdrCoefficient()
rhs = RhsCoefficient()

integ = mfem.DiffusionIntegrator(one)
a.AddDomainIntegrator(integ)
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
예제 #2
0
pcg.Mult(b, x)

LSres = mfem.HypreParVector(test_space)
tmp = mfem.HypreParVector(test_space)

B.Mult(x, LSres)
LSres -= trueF
matSinv.Mult(LSres, tmp)

res = sqrt(mfem.InnerProduct(LSres, tmp))

if (myid == 0):
    print("\n|| B0*x0 + Bhat*xhat - F ||_{S^-1} = " + str(res))

x0.Distribute(x.GetBlock(x0_var))

# 13. Save the refined mesh and the solution in parallel. This output can
#     be viewed later using GLVis: "glvis -np <np> -m mesh -g sol".
smyid = '{:0>6d}'.format(myid)
mesh_name = "mesh." + smyid
sol_name = "sol." + smyid
pmesh.PrintToFile(mesh_name, 8)
x0.SaveToFile(sol_name, 8)

# 14. Send the solution by socket to a GLVis server.
if visualization:
    sol_sock = mfem.socketstream("localhost", 19916)
    sol_sock.send_text("parallel " + str(num_procs) + " " + str(myid))
    sol_sock.precision(8)
    sol_sock.send_solution(pmesh, x0)
예제 #3
0
def ex19_main(args):
    ser_ref_levels = args.refine_serial
    par_ref_levels = args.refine_parallel
    order = args.order
    visualization = args.visualization
    mu = args.shear_modulus
    newton_rel_tol = args.relative_tolerance
    newton_abs_tol = args.absolute_tolerance
    newton_iter = args.newton_iterations

    if myid == 0: parser.print_options(args)

    meshfile = expanduser(join(path, 'data', args.mesh))
    mesh = mfem.Mesh(meshfile, 1, 1)
    dim = mesh.Dimension()

    for lev in range(ser_ref_levels):
        mesh.UniformRefinement()

    pmesh = mfem.ParMesh(MPI.COMM_WORLD, mesh)
    del mesh
    for lev in range(par_ref_levels):
        pmesh.UniformRefinement()

    #  4. Define the shear modulus for the incompressible Neo-Hookean material
    c_mu = mfem.ConstantCoefficient(mu)

    #  5. Define the finite element spaces for displacement and pressure
    #     (Taylor-Hood elements). By default, the displacement (u/x) is a second
    #     order vector field, while the pressure (p) is a linear scalar function.
    quad_coll = mfem.H1_FECollection(order, dim)
    lin_coll = mfem.H1_FECollection(order - 1, dim)

    R_space = mfem.ParFiniteElementSpace(pmesh, quad_coll, dim,
                                         mfem.Ordering.byVDIM)
    W_space = mfem.ParFiniteElementSpace(pmesh, lin_coll)

    spaces = [R_space, W_space]
    glob_R_size = R_space.GlobalTrueVSize()
    glob_W_size = W_space.GlobalTrueVSize()

    #   6. Define the Dirichlet conditions (set to boundary attribute 1 and 2)
    ess_bdr_u = mfem.intArray(R_space.GetMesh().bdr_attributes.Max())
    ess_bdr_p = mfem.intArray(W_space.GetMesh().bdr_attributes.Max())
    ess_bdr_u.Assign(0)
    ess_bdr_u[0] = 1
    ess_bdr_u[1] = 1
    ess_bdr_p.Assign(0)
    ess_bdr = [ess_bdr_u, ess_bdr_p]

    if myid == 0:
        print("***********************************************************")
        print("dim(u) = " + str(glob_R_size))
        print("dim(p) = " + str(glob_W_size))
        print("dim(u+p) = " + str(glob_R_size + glob_W_size))
        print("***********************************************************")

    block_offsets = intArray([0, R_space.TrueVSize(), W_space.TrueVSize()])
    block_offsets.PartialSum()
    xp = mfem.BlockVector(block_offsets)

    #  9. Define grid functions for the current configuration, reference
    #     configuration, final deformation, and pressure
    x_gf = mfem.ParGridFunction(R_space)
    x_ref = mfem.ParGridFunction(R_space)
    x_def = mfem.ParGridFunction(R_space)
    p_gf = mfem.ParGridFunction(W_space)

    #x_gf.MakeRef(R_space, xp.GetBlock(0), 0)
    #p_gf.MakeRef(W_space, xp.GetBlock(1), 0)

    deform = InitialDeformation(dim)
    refconfig = ReferenceConfiguration(dim)

    x_gf.ProjectCoefficient(deform)
    x_ref.ProjectCoefficient(refconfig)
    p_gf.Assign(0.0)

    #  12. Set up the block solution vectors
    x_gf.GetTrueDofs(xp.GetBlock(0))
    p_gf.GetTrueDofs(xp.GetBlock(1))

    #  13. Initialize the incompressible neo-Hookean operator
    oper = RubberOperator(spaces, ess_bdr, block_offsets, newton_rel_tol,
                          newton_abs_tol, newton_iter, mu)
    #  14. Solve the Newton system
    oper.Solve(xp)

    #  15. Distribute the shared degrees of freedom
    x_gf.Distribute(xp.GetBlock(0))
    p_gf.Distribute(xp.GetBlock(1))

    #  16. Compute the final deformation
    mfem.subtract_vector(x_gf, x_ref, x_def)

    #  17. Visualize the results if requested
    if (visualization):
        vis_u = mfem.socketstream("localhost", 19916)
        visualize(vis_u, pmesh, x_gf, x_def, "Deformation", True)
        MPI.COMM_WORLD.Barrier()
        vis_p = mfem.socketstream("localhost", 19916)
        visualize(vis_p, pmesh, x_gf, p_gf, "Deformation", True)

    #  14. Save the displaced mesh, the final deformation, and the pressure
    nodes = x_gf
    owns_nodes = 0
    nodes, owns_nodes = pmesh.SwapNodes(nodes, owns_nodes)

    smyid = '.' + '{:0>6d}'.format(myid)
    pmesh.PrintToFile('deformed.mesh' + smyid, 8)
    p_gf.SaveToFile('pressure.sol' + smyid, 8)
    x_def.SaveToFile("deformation.sol" + smyid, 8)
예제 #4
0
    if (init_vis):
        out.send_text("window_size 400 400")
        out.send_text("window_title '" + field_name)
        if (pmesh.SpaceDimension() == 2):
            out.send_text("view 0 0")
            out.send_text("keys jl")
        out.send_text("keys cm")  # show colorbar and mesh
        out.send_text(
            "autoscale value")  # update value-range; keep mesh-extents fixed
        out.send_text("pause")
    out.flush()


oper = HyperelasticOperator(fespace, ess_bdr, visc, mu, K)
if (visualization):
    vis_v = mfem.socketstream("localhost", 19916)
    vis_v.precision(8)
    visualize(vis_v, pmesh, x_gf, v_gf, "Velocity", True)

    MPI.COMM_WORLD.Barrier()
    vis_w = mfem.socketstream("localhost", 19916)
    oper.GetElasticEnergyDensity(x_gf, w_gf)
    vis_w.precision(8)
    visualize(vis_w, pmesh, x_gf, w_gf, "Elastic energy density", True)

ee0 = oper.ElasticEnergy(x_gf)
ke0 = oper.KineticEnergy(v_gf)

if myid == 0:
    print("initial elastic energy (EE) = " + str(ee0))
    print("initial kinetic energy (KE) = " + str(ke0))
예제 #5
0
 def NewWindow(self):
     self.socks.append(mfem.socketstream(self.host, self.port))
     self.output = self.socks[-1]
     self.output.precision(8)
     self.socks
     self.sid = self.sid + 1