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
def Maxwell(V, Q, F, params, HiptmairMatrices, Hiptmairtol, mesh): parameters['reorder_dofs_serial'] = False W = FunctionSpace(mesh, MixedElement([V, Q])) IS = MO.IndexSet(W) print params (b, r) = TrialFunctions(W) (c, s) = TestFunctions(W) a11 = params[1]*params[2]*inner(curl(b), curl(c))*dx('everywhere') a21 = inner(b,grad(s))*dx('everywhere') a12 = inner(c,grad(r))*dx('everywhere') L = inner(c, F)*dx('everywhere') a = a11+a12+a21 def boundary(x, on_boundary): return on_boundary bcb = DirichletBC(W.sub(0), Expression(("1.0","0.0"), degree=4), boundary) bcr = DirichletBC(W.sub(1), Expression(("0.0"), degree=4), boundary) bc = [bcb, bcr] A, b = assemble_system(a, L, bc) A, b = CP.Assemble(A, b) u = b.duplicate() ksp = PETSc.KSP().create() ksp.setTolerances(1e-8) ksp.max_it = 200 pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) ksp.setType('minres') pc.setPythonContext(MP.Hiptmair(W, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6],Hiptmairtol)) scale = b.norm() b = b/scale ksp.setOperators(A,A) del A start_time = time.time() ksp.solve(b,u) print ("{:40}").format("Maxwell solve, time: "), " ==> ",("{:4f}").format(time.time() - start_time),("{:9}").format(" Its: "), ("{:4}").format(ksp.its), ("{:9}").format(" time: "), ("{:4}").format(time.strftime('%X %x %Z')[0:5]) u = u*scale b_k = Function(FunctionSpace(mesh, V)) r_k = Function(FunctionSpace(mesh, Q)) b_k.vector()[:] = u.getSubVector(IS[0]).array r_k.vector()[:] = u.getSubVector(IS[1]).array return b_k, r_k
def solve(A, b, u, params, Fspace, SolveType, IterType, OuterTol, InnerTol, HiptmairMatrices, Hiptmairtol, KSPlinearfluids, Fp, kspF): if SolveType == "Direct": ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) pc = ksp.getPC() ksp.setType('preonly') pc.setType('lu') OptDB = PETSc.Options() OptDB['pc_factor_mat_solver_package'] = "mumps" OptDB['pc_factor_mat_ordering_type'] = "rcm" ksp.setFromOptions() scale = b.norm() b = b / scale ksp.setOperators(A, A) del A ksp.solve(b, u) # Mits +=dodim u = u * scale MO.PrintStr("Number iterations = " + str(ksp.its), 60, "+", "\n\n", "\n\n") return u, ksp.its, 0 elif SolveType == "Direct-class": ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) pc = ksp.getPC() ksp.setType('gmres') pc.setType('none') ksp.setFromOptions() scale = b.norm() b = b / scale ksp.setOperators(A, A) del A ksp.solve(b, u) # Mits +=dodim u = u * scale MO.PrintStr("Number iterations = " + str(ksp.its), 60, "+", "\n\n", "\n\n") return u, ksp.its, 0 else: # u = b.duplicate() if IterType == "Full": ksp = PETSc.KSP() ksp.create(comm=PETSc.COMM_WORLD) pc = ksp.getPC() ksp.setType('fgmres') pc.setType('python') OptDB = PETSc.Options() OptDB['ksp_gmres_restart'] = 200 # FSpace = [Velocity,Magnetic,Pressure,Lagrange] reshist = {} def monitor(ksp, its, fgnorm): reshist[its] = fgnorm print its, " OUTER:", fgnorm # ksp.setMonitor(monitor) ksp.max_it = 1000 W = Fspace FFSS = [W.sub(0), W.sub(1), W.sub(2), W.sub(3)] pc.setPythonContext( MHDprec.InnerOuterMAGNETICapprox( FFSS, kspF, KSPlinearfluids[0], KSPlinearfluids[1], Fp, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6], Hiptmairtol)) #OptDB = PETSc.Options() # OptDB['pc_factor_mat_solver_package'] = "mumps" # OptDB['pc_factor_mat_ordering_type'] = "rcm" # ksp.setFromOptions() scale = b.norm() b = b / scale ksp.setOperators(A, A) del A ksp.solve(b, u) # Mits +=dodim u = u * scale MO.PrintStr("Number iterations = " + str(ksp.its), 60, "+", "\n\n", "\n\n") return u, ksp.its, 0 IS = MO.IndexSet(Fspace, '2by2') M_is = IS[1] NS_is = IS[0] kspNS = PETSc.KSP().create() kspM = PETSc.KSP().create() kspNS.setTolerances(OuterTol) kspNS.setOperators(A[0]) kspM.setOperators(A[1]) # print P.symmetric if IterType == "MD": kspNS.setType('gmres') kspNS.max_it = 500 pcNS = kspNS.getPC() pcNS.setType(PETSc.PC.Type.PYTHON) pcNS.setPythonContext( NSpreconditioner.NSPCD( MixedFunctionSpace([Fspace.sub(0), Fspace.sub(1)]), kspF, KSPlinearfluids[0], KSPlinearfluids[1], Fp)) elif IterType == "CD": kspNS.setType('minres') pcNS = kspNS.getPC() pcNS.setType(PETSc.PC.Type.PYTHON) Q = KSPlinearfluids[1].getOperators()[0] Q = 1. / params[2] * Q KSPlinearfluids[1].setOperators(Q, Q) pcNS.setPythonContext( StokesPrecond.MHDApprox( MixedFunctionSpace([Fspace.sub(0), Fspace.sub(1)]), kspF, KSPlinearfluids[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.Hiptmair(MixedFunctionSpace([Fspace.sub(2), Fspace.sub(3)]), HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6], Hiptmairtol)) 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])) MO.PrintStr("Number of M iterations = " + str(Mits), 60, "+", "\n\n", "\n\n") MO.PrintStr("Number of NS/S iterations = " + str(NSits), 60, "+", "\n\n", "\n\n") return u, NSits, Mits
p22 = inner(grad(p), grad(q)) * dx pp = p11 + p22 PP, Pb = assemble_system(pp, L1, bcs) P = CP.Assemble(PP) if (Solving == 'Direct'): ksp = PETSc.KSP().create() ksp.setOperators(A) ksp.setFromOptions() ksp.setType(ksp.Type.MINRES) ksp.setTolerances(1e-8) pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) pc.setPythonContext(MP.Approx(W, P)) # print 'Solving with:', ksp.getType() # Solve! tic() ksp.solve(bb, x) SolTime[xx - 1] = toc() print "time to solve: ", SolTime[xx - 1] OuterIt[xx - 1] = ksp.its r = bb.duplicate() A.mult(x, r) r.aypx(-1, bb) rnorm = r.norm() PETSc.Sys.Print('error norm = %g' % rnorm, comm=PETSc.COMM_WORLD) del A, P
Hiptmairtol = 1e-3 tic() kspVector, kspScalar, kspCGScalar, diag = HiptmairSetup.HiptmairKSPsetup(VectorLaplacian, ScalarLaplacian, Prec, CGtol) del A, VectorLaplacian, ScalarLaplacian print ("{:40}").format("Hiptmair Setup time:"), " ==> ",("{:4f}").format(toc()), ("{:9}").format(" time: "), ("{:4}").format(time.strftime('%X %x %Z')[0:5]) reshist = {} def monitor(ksp, its, rnorm): print rnorm reshist[its] = rnorm ksp.setMonitor(monitor) pc.setPythonContext(MaxwellPrecond.Hiptmair(W, kspScalar, kspCGScalar, kspVector, G, P, Prec,Hiptmairtol)) scale = b.norm() b = b/scale start_time = time.time() ksp.solve(b, x) TimeSave[xx-1] = time.time() - start_time x = x*scale print ksp.its print TimeSave[xx-1] ItsSave[xx-1] = len(reshist) CGits, Hits, CGtime, HiptmairTime = pc.getPythonContext().ITS() ItsHipt[xx-1] =float(Hits)/len(reshist) ItsCG[xx-1] =float(CGits)/len(reshist) print " \n\n\n\n" Ve = FunctionSpace(mesh,"N1curl",2)
def Maxwell(V, Q, F, b0, r0, params, boundaries, HiptmairMatrices, Hiptmairtol): parameters['reorder_dofs_serial'] = False W = V * Q IS = MO.IndexSet(W) (b, r) = TrialFunctions(W) (c, s) = TestFunctions(W) a11 = params[1] * params[2] * inner(curl(b), curl(c)) * dx('everywhere') a21 = inner(b, grad(s)) * dx('everywhere') a12 = inner(c, grad(r)) * dx('everywhere') L = inner(c, F) * dx('everywhere') a = a11 + a12 + a21 def boundary(x, on_boundary): return on_boundary bcb = DirichletBC(W.sub(0), b0, boundary) bcr = DirichletBC(W.sub(1), r0, boundary) bc = [bcb, bcr] A, b = assemble_system(a, L, bc) A, b = CP.Assemble(A, b) u = b.duplicate() # ksp = PETSc.KSP() # ksp.create(comm=PETSc.COMM_WORLD) # pc = ksp.getPC() # ksp.setType('preonly') # pc.setType('lu') # OptDB = PETSc.Options() # if __version__ != '1.6.0': # OptDB['pc_factor_mat_solver_package'] = "mumps" # OptDB['pc_factor_mat_ordering_type'] = "rcm" # ksp.setFromOptions() ksp = PETSc.KSP().create() ksp.setTolerances(1e-8) ksp.max_it = 200 pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) ksp.setType('minres') pc.setPythonContext( MP.Hiptmair(W, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6], Hiptmairtol)) scale = b.norm() b = b / scale ksp.setOperators(A, A) del A start_time = time.time() ksp.solve(b, u) print("{:40}").format("Maxwell solve, time: "), " ==> ", ( "{:4f}").format(time.time() - start_time), ( "{:9}").format(" Its: "), ("{:4}").format( ksp.its), ("{:9}").format(" time: "), ("{:4}").format( time.strftime('%X %x %Z')[0:5]) u = u * scale b_k = Function(V) r_k = Function(Q) b_k.vector()[:] = u.getSubVector(IS[0]).array r_k.vector()[:] = u.getSubVector(IS[1]).array return b_k, r_k
def Maxwell(V, Q, F, b0, r0, params, mesh, HiptmairMatrices, Hiptmairtol): parameters['reorder_dofs_serial'] = False W = V * Q W = FunctionSpace(mesh, MixedElement([V, Q])) IS = MO.IndexSet(W) (b, r) = TrialFunctions(W) (c, s) = TestFunctions(W) if params[0] == 0.0: a11 = params[1] * inner(curl(b), curl(c)) * dx else: a11 = params[1] * params[0] * inner(curl(b), curl(c)) * dx a21 = inner(b, grad(s)) * dx a12 = inner(c, grad(r)) * dx # print F L = inner(c, F) * dx a = a11 + a12 + a21 def boundary(x, on_boundary): return on_boundary # class b0(Expression): # def __init__(self): # self.p = 1 # def eval_cell(self, values, x, ufc_cell): # values[0] = 0.0 # values[1] = 1.0 # def value_shape(self): # return (2,) bcb = DirichletBC(W.sub(0), b0, boundary) bcr = DirichletBC(W.sub(1), r0, boundary) bc = [bcb, bcr] A, b = assemble_system(a, L, bc) A, b = CP.Assemble(A, b) u = b.duplicate() # ksp = PETSc.KSP() # ksp.create(comm=PETSc.COMM_WORLD) # pc = ksp.getPC() # ksp.setType('preonly') # pc.setType('lu') # OptDB = PETSc.Options() # OptDB['pc_factor_mat_solver_package'] = "pastix" # OptDB['pc_factor_mat_ordering_type'] = "rcm" # ksp.setFromOptions() ksp = PETSc.KSP().create() ksp.setTolerances(1e-8) ksp.max_it = 200 pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) ksp.setType('minres') pc.setPythonContext( MP.Hiptmair(W, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6], Hiptmairtol)) scale = b.norm() b = b / scale ksp.setOperators(A, A) del A start_time = time.time() ksp.solve(b, u) print("{:40}").format("Maxwell solve, time: "), " ==> ", ( "{:4f}").format(time.time() - start_time), ( "{:9}").format(" Its: "), ("{:4}").format( ksp.its), ("{:9}").format(" time: "), ("{:4}").format( time.strftime('%X %x %Z')[0:5]) u = u * scale b_k = Function(FunctionSpace(mesh, V)) r_k = Function(FunctionSpace(mesh, Q)) b_k.vector()[:] = u.getSubVector(IS[0]).array r_k.vector()[:] = u.getSubVector(IS[1]).array return b_k, r_k
def solve(A,b,u,IS,Fspace,IterType,OuterTol,InnerTol,HiptmairMatrices,KSPlinearfluids,kspF,Fp,MatrixLinearFluids,kspFp): if IterType == "Full": kspOuter = PETSc.KSP().create() kspOuter.setTolerances(OuterTol) kspOuter.setType('fgmres') u_is = PETSc.IS().createGeneral(range(Fspace[0].dim())) p_is = PETSc.IS().createGeneral(range(Fspace[0].dim(),Fspace[0].dim()+Fspace[1].dim())) Bt = A.getSubMatrix(u_is,p_is) 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,A) kspOuter.max_it = 500 kspInner = pcOutter.getKSP() kspInner.max_it = 100 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(MHDstabPrecond.InnerOuterWITHOUT(Fspace,kspF, KSPlinearfluids[0], KSPlinearfluids[1],Fp, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6],1e-6,A)) PP = PETSc.Mat().createPython([A.size[0], A.size[0]]) PP.setType('python') p = PrecondMulti.MultiApply(Fspace,A,HiptmairMatrices[6],MatrixLinearFluids[1],MatrixLinearFluids[0],kspFp, HiptmairMatrices[3]) 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)) kspM.setOperators(A.getSubMatrix(M_is,M_is)) del A uNS = u.getSubVector(NS_is) bNS = b.getSubVector(NS_is) kspNS.setType('gmres') pcNS = kspNS.getPC() kspNS.setTolerances(OuterTol) pcNS.setType(PETSc.PC.Type.PYTHON) if IterType == "MD": pcNS.setPythonContext(NSpreconditioner.NSPCD(MixedFunctionSpace([Fspace[0],Fspace[1]]), kspF, KSPlinearfluids[0], KSPlinearfluids[1],Fp)) else: pcNS.setPythonContext(StokesPrecond.MHDApprox(MixedFunctionSpace([Fspace[0],Fspace[1]]), kspF, KSPlinearfluids[1])) scale = bNS.norm() bNS = bNS/scale start_time = time.time() kspNS.solve(bNS, uNS) print ("{:25}").format("NS, time: "), " ==> ",("{:4f}").format(time.time() - start_time),("{:9}").format(" Its: "), ("{:4}").format(kspNS.its), ("{:9}").format(" time: "), ("{:4}").format(time.strftime('%X %x %Z')[0:5]) uNS = scale*uNS 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.Hiptmair(MixedFunctionSpace([Fspace[2],Fspace[3]]), HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6],1e-6)) # x = x*scale uM = u.getSubVector(M_is) bM = b.getSubVector(M_is) scale = bM.norm() bM = bM/scale start_time = time.time() kspM.solve(bM, uM) print ("{:25}").format("Maxwell solve, time: "), " ==> ",("{:4f}").format(time.time() - start_time),("{:9}").format(" Its: "), ("{:4}").format(kspM.its), ("{:9}").format(" time: "), ("{:4}").format(time.strftime('%X %x %Z')[0:5]) uM = uM*scale Mits = kspM.its kspM.destroy() u = IO.arrayToVec(np.concatenate([uNS.array, uM.array])) return u,NSits,Mits
pcNS.setType(PETSc.PC.Type.LU) uNS = u.getSubVector(NS_is) bNS = b.getSubVector(NS_is) kspNS.solve(bNS, uNS) NSits = kspNS.its kspNS.destroy() kspM.setFromOptions() kspM.setType(kspM.Type.MINRES) kspM.setTolerances(1e-5) pcM = kspM.getPC() pcM.setType(PETSc.PC.Type.PYTHON) pcM.setPythonContext(MP.Direct(MixedFunctionSpace([FSpaces[2],FSpaces[3]]))) uM = u.getSubVector(M_is) bM = b.getSubVector(M_is) kspM.solve(bM, uM) Mits = kspM.its kspM.destroy() u = IO.arrayToVec(np.concatenate([uNS.array, uM.array])) print NSits print Mits tic() # ksp.solve(b, u) time = toc() print time
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
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) bM = b.getSubVector(M_is) kspM.solve(bM, uM) print kspM.its time = toc() u = IO.arrayToVec(np.concatenate([uNS.array, uM.array])) print time SolutionTime = SolutionTime +time # OptDB = PETSc.Options() # # OptDB["ksp_type"] = "gmres" # # OptDB["pc_type"] = "ilu" # # OptDB["pc_factor_shift_amount"] = .1
def Maxwell(u, V, Q, BC, f, params, HiptmairMatrices, Hiptmairtol, InitialTol, Neumann=None): # parameters['linear_algebra_backend'] = 'uBLAS' dim = f.shape()[0] W = V * Q (b, r) = TrialFunctions(W) (c, s) = TestFunctions(W) def boundary(x, on_boundary): return on_boundary bcb = DirichletBC(W.sub(0), BC[0], boundary) bcr = DirichletBC(W.sub(1), BC[1], boundary) if params[0] == 0: a11 = params[1] * inner(curl(c), curl(b)) * dx else: if dim == 2: a11 = params[1] * params[0] * inner(curl(c), curl(b)) * dx elif dim == 3: a11 = params[1] * params[0] * inner(curl(c), curl(b)) * dx a12 = inner(c, grad(r)) * dx a21 = inner(b, grad(s)) * dx Lmaxwell = inner(c, f) * dx maxwell = a11 + a12 + a21 bcs = [bcb, bcr] tic() AA, bb = assemble_system(maxwell, Lmaxwell, bcs) A, bb = CP.Assemble(AA, bb) del AA x = bb.duplicate() u_is = PETSc.IS().createGeneral(range(V.dim())) p_is = PETSc.IS().createGeneral(range(V.dim(), V.dim() + Q.dim())) ksp = PETSc.KSP().create() ksp.setTolerances(InitialTol) pc = ksp.getPC() pc.setType(PETSc.PC.Type.PYTHON) # ksp.setType('preonly') # pc.setType('lu') # [G, P, kspVector, kspScalar, kspCGScalar, diag] reshist = {} def monitor(ksp, its, rnorm): print rnorm reshist[its] = rnorm # ksp.setMonitor(monitor) if V.__str__().find("N1curl2") == -1: ksp.setOperators(A) pc.setPythonContext( MP.Hiptmair(W, HiptmairMatrices[3], HiptmairMatrices[4], HiptmairMatrices[2], HiptmairMatrices[0], HiptmairMatrices[1], HiptmairMatrices[6], Hiptmairtol)) else: p = params[1] * params[0] * inner(curl(c), curl(b)) * dx + inner( c, b) * dx + inner(grad(r), grad(s)) * dx P = assemble(p) for bc in bcs: bc.apply(P) P = CP.Assemble(P) ksp.setOperators(A, P) pc.setType(PETSc.PC.Type.LU) # pc.setPythonContext(MP.Direct(W)) scale = bb.norm() bb = bb / scale start_time = time.time() ksp.solve(bb, x) print("{:40}").format("Maxwell solve, time: "), " ==> ", ( "{:4f}").format(time.time() - start_time), ( "{:9}").format(" Its: "), ("{:4}").format( ksp.its), ("{:9}").format(" time: "), ("{:4}").format( time.strftime('%X %x %Z')[0:5]) x = x * scale ksp.destroy() X = IO.vecToArray(x) x = X[0:V.dim()] ua = Function(V) ua.vector()[:] = x p = X[V.dim():] pa = Function(Q) pa.vector()[:] = p del ksp, pc return ua, pa