def systems_and_preconditioners(mesh, X, z, order=1):
    # Create a real analogue of our vector space for the wrapper preconditioner.
    dirichlet = 'top|right|bottom|left'
    X_real = ng.H1(mesh, order=order, dirichlet=dirichlet, complex=False)

    # Test and trial functions for the original space.
    u, v = X.TnT()

    # Real trial and test functions.
    ur, vr = X_real.TnT()

    # Create a real analogue of the bilinear form a.
    a_real = ng.BilinearForm(X_real)
    a_real += ng.SymbolicBFI(ng.grad(ur) * ng.grad(vr))
    a_real.Assemble()

    # Initialize petsc prior to conswtructing preconditioners.
    petsc.Initialize()

    # Create a bilinear form and a preconditioner for each z.
    zbas = []
    precs = []
    for k in range(len(z)):
        #  Create a bilinear form for the given z-value.
        zba = ng.BilinearForm(X)
        zba += ng.SymbolicBFI(z[k] * u * v - ng.grad(u) * ng.grad(v))

        # Create a preconditioner for the given z-value.
        mat_convert = petsc.PETScMatrix(a_real.mat, freedofs=X_real.FreeDofs())
        real_pc = petsc.PETSc2NGsPrecond(mat=mat_convert,
                                         name="real_pc",
                                         petsc_options={"pc_type": "gamg"})

        prec = WrapperPrec(real_pc)

        # Assemble the given bilinear form.
        zba.Assemble()

        # Tack the bilinear forms and preconditioners onto their respective lists.
        zbas += [zba]
        precs += [prec]

    return zbas, precs
示例#2
0
    gfu = GridFunction(fes)
    rbm_vecs = list()
    upart = gfu
    for RBM in RBMS:
        if dim == 3:
            ucf = CoefficientFunction((RBM[0], RBM[1], RBM[2]))
        else:
            ucf = CoefficientFunction((RBM[0], RBM[1]))
        upart.Set(ucf)
        v = gfu.vec.CreateVector()
        v.data = gfu.vec
        rbm_vecs.append(v)
    return rbm_vecs


petsc.Initialize()

mat_wrap = petsc.PETScMatrix(a.mat,
                             freedofs=V.FreeDofs(),
                             format=petsc.PETScMatrix.AIJ)
opts = {"ksp_type": "cg", "ksp_atol": 1e-30, "ksp_rtol": 1e-8, "pc_type": "ml"}
ksp = petsc.KSP(mat=mat_wrap,
                name="someksp",
                petsc_options=opts,
                finalize=False)
ksp.GetMatrix().SetNearNullSpace(rb_modes(V))

# import ngs_amg
# mat_wrap = petsc.FlatPETScMatrix(a.mat, freedofs=V.FreeDofs())
# ngs_amg_opts = {"energy" : "alg", "comp_sm" : True, "force_comp_sm" : True, "max_cv" : 500, "ass_frac" : 0.15, "skip_ass" : 3}
# ngs_pc = ngs_amg.AMG_EL2(blf=a, freedofs=V.FreeDofs(), **ngs_amg_opts)
示例#3
0
else:
    ngm = NGMesh.Receive(comm)
mesh = Mesh(ngm)

V = H1(mesh, order=1, dirichlet='.*')
u, v = V.TnT()
a = BilinearForm(V)
a += SymbolicBFI(InnerProduct(grad(u), grad(v)))
f = LinearForm(V)
f += SymbolicLFI(v)
f.Assemble()
gfu = GridFunction(V)
# c = Preconditioner(a, 'bddc')
a.Assemble()

ngp.Initialize()

wrapped_mat = ngp.PETScMatrix(a.mat,
                              freedofs=V.FreeDofs(),
                              format=ngp.PETScMatrix.IS_AIJ)

# gives access to the petsc4py mat
p4p_mat = wrapped_mat.GetPETScMat()
if comm.size > 1:
    p4p_mat.convert("mpiaij")

# this is a wrapper around a KSP
wrapped_ksp = ngp.KSP(mat=wrapped_mat,
                      name="someksp",
                      petsc_options={
                          "ksp_type": "cg",
示例#4
0
I = Id(mesh.dim)
F = I + u.Deriv()   # attention: row .. component, col .. derivative
C = F * F.trans

factor = Parameter(1.0)

a = BilinearForm(V, symmetric=False)
a += SymbolicEnergy(  NeoHook (C).Compile(False) )
a += SymbolicEnergy(  (-factor * InnerProduct(force,u) ).Compile(False) )

gfu = GridFunction(V)
gfu2 = GridFunction(V)

poo = dict()
#poo = {"info" : ""}
petsc.Initialize(**poo)

petsc_options = {"pc_type" : "none",
                 "ksp_type" : "cg",
                 "ksp_atol" : 1e-30,
                 "ksp_rtol" : 1e-8,
                 "ksp_max_it" : 1000,
                 # "ksp_view" : "",
                 # "ksp_monitor" : "",
                 # "ksp_converged_reason" : "",
                 # "snes_view" : "",
                 # "snes_monitor" : "",
                 # "snes_converged_reason" : "",
                 "snes_max_it" : 50,
                 "snes_linesearch_type" : "basic" }
snes = petsc.SNES(a, name="mysnes", petsc_options=petsc_options, mode = petsc.SNES.JACOBI_MAT_MODE.FLAT)