def create(self, pc): self.diag = None kspNS = PETSc.KSP() kspNS.create(comm=PETSc.COMM_WORLD) pcNS = kspNS.getPC() kspNS.setType('gmres') pcNS.setType('python') pcNS.setPythonContext( NSprecond.PCDdirect( MixedFunctionSpace([self.Fspace[0], self.Fspace[1]]), self.Q, self.F, self.L)) kspNS.setTolerances(1e-3) kspNS.setFromOptions() self.kspNS = kspNS kspM = PETSc.KSP() kspM.create(comm=PETSc.COMM_WORLD) pcM = kspM.getPC() kspM.setType('gmres') pcM.setType('python') kspM.setTolerances(1e-3) pcM.setPythonContext( MP.Direct(MixedFunctionSpace([self.Fspace[2], self.Fspace[3]]))) kspM.setFromOptions() self.kspM = kspM
b = bb.array() zeros = 0*b bb = IO.arrayToVec(b) x = IO.arrayToVec(zeros) ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) ksp.setTolerances(1e-5) ksp.setType('gmres') pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) if Solver == "LSC": pc.setPythonContext(NSprecond.LSCnew(W,A,L,Bd,dBt)) elif Solver == "PCD": pc.setPythonContext(NSprecond.PCD(W, A, Mass, F, L)) ksp.setOperators(A) OptDB = PETSc.Options() # OptDB['ksp_gmres_restart'] = 200 # OptDB['pc_factor_mat_ordering_type'] = 'rcm' # OptDB['pc_factor_mat_solver_package'] = 'umfpack' ksp.setFromOptions() start = time.time() ksp.solve(bb, x)
# PP = assemble(prec) # bcc.apply(PP) # P = CP.Assemble(PP) b = bb.array() zeros = 0 * b bb = IO.arrayToVec(b) x = IO.arrayToVec(zeros) ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) ksp.setTolerances(1e-5) ksp.setType('gmres') pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) pc.setPythonContext(NSprecond.LSCnew(W, A, L, Bd, dBt)) ksp.setOperators(A) OptDB = PETSc.Options() OptDB['ksp_gmres_restart'] = 200 # OptDB['pc_factor_mat_ordering_type'] = 'rcm' # OptDB['pc_factor_mat_solver_package'] = 'umfpack' ksp.setFromOptions() start = time.time() ksp.solve(bb, x) print time.time() - start print ksp.its outerit += ksp.its # r = bb.duplicate() # A.MUlt(x, r)
FF = assemble(MU * inner(grad(p), grad(q)) * dx + inner(inner(grad(p), u_k), q) * dx) F = CP.Assemble(FF) b = bb.array() zeros = 0 * b bb = IO.arrayToVec(b) x = IO.arrayToVec(zeros) ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) ksp.setTolerances(1e-8) ksp.setType('gmres') pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) pc.setPythonContext(NSprecond.PCD(W, A, Mass, F, L)) ksp.setOperators(A, P) OptDB = PETSc.Options() # OptDB['pc_factor_shift_amount'] = 1 ksp.setFromOptions() start = time.time() ksp.solve(bb, x) print time.time() - start print ksp.its outerit += ksp.its # r = bb.duplicate() # A.MUlt(x, r) # r.aypx(-1, bb)
def solve(A, b, u, P, IS, Fspace, IterType, OuterTol, InnerTol, Mass=0, L=0, F=0): # u = b.duplicate() if IterType == "Full": kspOuter = PETSc.KSP().create() kspOuter.setTolerances(OuterTol) kspOuter.setType('fgmres') reshist = {} def monitor(ksp, its, fgnorm): reshist[its] = fgnorm print "OUTER:", fgnorm kspOuter.setMonitor(monitor) pcOutter = kspOuter.getPC() pcOutter.setType(PETSc.PC.Type.KSP) kspOuter.setOperators(A) kspOuter.max_it = 500 kspInner = pcOutter.getKSP() kspInner.max_it = 100 reshist1 = {} def monitor(ksp, its, fgnorm): reshist1[its] = fgnorm print "INNER:", fgnorm # kspInner.setMonitor(monitor) kspInner.setType('gmres') kspInner.setTolerances(InnerTol) pcInner = kspInner.getPC() pcInner.setType(PETSc.PC.Type.PYTHON) pcInner.setPythonContext(MHDprecond.D(Fspace, P, Mass, F, L)) PP = PETSc.Mat().createPython([A.size[0], A.size[0]]) PP.setType('python') p = PrecondMulti.P(Fspace, P, Mass, L, F) PP.setPythonContext(p) kspInner.setOperators(PP) tic() scale = b.norm() b = b / scale print b.norm() kspOuter.solve(b, u) u = u * scale print toc() # print s.getvalue() NSits = kspOuter.its del kspOuter Mits = kspInner.its del kspInner # print u.array return u, NSits, Mits NS_is = IS[0] M_is = IS[1] kspNS = PETSc.KSP().create() kspM = PETSc.KSP().create() kspNS.setTolerances(OuterTol) kspNS.setOperators(A.getSubMatrix(NS_is, NS_is), P.getSubMatrix(NS_is, NS_is)) kspM.setOperators(A.getSubMatrix(M_is, M_is), P.getSubMatrix(M_is, M_is)) # print P.symmetric A.destroy() P.destroy() if IterType == "MD": kspNS.setType('gmres') kspNS.max_it = 500 pcNS = kspNS.getPC() pcNS.setType(PETSc.PC.Type.PYTHON) pcNS.setPythonContext( NSprecond.PCDdirect(MixedFunctionSpace([Fspace[0], Fspace[1]]), Mass, F, L)) elif IterType == "CD": kspNS.setType('minres') pcNS = kspNS.getPC() pcNS.setType(PETSc.PC.Type.PYTHON) pcNS.setPythonContext( StokesPrecond.Approx(MixedFunctionSpace([Fspace[0], Fspace[1]]))) reshist = {} def monitor(ksp, its, fgnorm): reshist[its] = fgnorm print fgnorm # kspNS.setMonitor(monitor) uNS = u.getSubVector(NS_is) bNS = b.getSubVector(NS_is) # print kspNS.view() scale = bNS.norm() bNS = bNS / scale print bNS.norm() kspNS.solve(bNS, uNS) uNS = uNS * scale NSits = kspNS.its kspNS.destroy() # for line in reshist.values(): # print line kspM.setFromOptions() kspM.setType(kspM.Type.MINRES) kspM.setTolerances(InnerTol) pcM = kspM.getPC() pcM.setType(PETSc.PC.Type.PYTHON) pcM.setPythonContext(MP.Direct(MixedFunctionSpace([Fspace[2], Fspace[3]]))) uM = u.getSubVector(M_is) bM = b.getSubVector(M_is) scale = bM.norm() bM = bM / scale print bM.norm() kspM.solve(bM, uM) uM = uM * scale Mits = kspM.its kspM.destroy() u = IO.arrayToVec(np.concatenate([uNS.array, uM.array])) return u, NSits, Mits
fp = assemble(MU*inner(grad(qQ), grad(pQ))*dx+inner((u_k[0]*grad(pQ)[0]+u_k[1]*grad(pQ)[1]),qQ)*dx + (1/2)*div(u_k)*inner(pQ,qQ)*dx - (1/2)*(u_k[0]*n[0]+u_k[1]*n[1])*inner(pQ,qQ)*ds) # print "hi" L = CP.Assemble(L) Mass = CP.Assemble(Mass) fp = CP.Assemble(fp) kspNS = PETSc.KSP().create() kspM = PETSc.KSP().create() kspNS.setOperators(A.getSubMatrix(NS_is,NS_is)) kspNS.setType('gmres') kspNS.setTolerances(1e-6) pcNS = kspNS.getPC() pcNS.setType(PETSc.PC.Type.PYTHON) pcNS.setPythonContext(NSprecond.PCDdirect(MixedFunctionSpace([Velocity,Pressure]), A.getSubMatrix(NS_is,NS_is), Mass, fp, L)) kspNS.setOperators(A.getSubMatrix(NS_is,NS_is)) uNS = u.getSubVector(NS_is) bNS = b.getSubVector(NS_is) kspNS.solve(bNS, uNS) print kspNS.its kspM.setFromOptions() kspM.setType(kspM.Type.MINRES) kspM.setTolerances(1e-6) pcM = kspM.getPC() pcM.setType(PETSc.PC.Type.PYTHON) pcM.setPythonContext(MP.Direct(MixedFunctionSpace([Magnetic,Lagrange]),P.getSubMatrix(M_is,M_is))) kspM.setOperators(A.getSubMatrix(M_is,M_is)) uM = u.getSubVector(M_is)