def stokes2(k, meshevents, v, points): vortelts1 = pe.HcurlElements(k) vortelts2 = pe.HcurlElements(k) velelts1 = pe.HdivElements(k) velelts2 = pe.HdivElements(k) pressureelts1 = pe.L2Elements(k) quadrule = pu.pyramidquadrature(k+1) Asys = pa.SymmetricSystem(vortelts1, quadrule, meshevents, []) # Bsys = pa.AsymmetricSystem(velelts1, vortelts2, quadrule, meshevents, [bdytag], []) BsysT = pa.AsymmetricSystem(vortelts2, velelts1, quadrule, meshevents, [], [bdytag]) Csys = pa.AsymmetricSystem(pressureelts1,velelts2, quadrule, meshevents, [], [bdytag]) A = Asys.systemMatrix(False) BT = BsysT.systemMatrix(True, False) C = Csys.systemMatrix(False, True) vv = lambda x: np.tile(v,(len(x), 1))[:,np.newaxis,:] vn = lambda x,n: np.tensordot(n,v,([1],[1])) # vt = lambda x,n: (v - vn(x,n)*n)[:,np.newaxis,:] vc = lambda x,n: np.cross(v, n)[:, np.newaxis, :] BTI, BTE, BTGs = BsysT.processBoundary(BT, {bdytag:vv}) CI, CE, CGs = Csys.processBoundary(C, {bdytag:vv}) P = Csys.loadVector(lambda x: np.ones((len(x),1,1))) # print "P ",P Gt = Asys.boundaryLoad({bdytag: vc}, pu.squarequadrature(k+1), pu.trianglequadrature(k+1), False) # print "Gt ",Gt print A.shape, BT.shape, C.shape, BTI.shape, BTE[bdytag].shape, BTGs[bdytag].shape, CI.shape AL = Gt[bdytag] + BTE[bdytag] * BTGs[bdytag] # print "AL ",AL CL = -CE[bdytag] * CGs[bdytag] nvort = A.get_shape()[0] nvel = BTI.get_shape()[1] # S = ss.bmat([[A, -BTI, None],[-BTI.transpose(), None, CI.transpose()],[None, CI, None]]) # L = np.vstack((AL,np.zeros((nvel,1)), CL)) S = ss.bmat([[A, -BTI, None, None],[-BTI.transpose(), None, CI.transpose(), None],[None, CI, None, P], [None,None,P.transpose(), None]]) L = np.vstack((AL,np.zeros((nvel,1)), CL, np.zeros((1,1)))) print "solving" X = ps.solve(S, L) U = X[nvort:(nvort + nvel)] # print "X",X # print "U", U # print "BTGs", BTGs # u = BsysT.evaluate(points, U, BTGs, False) # uu = Asys.evaluate(points, np.eye(nvort)[-2], {}, False) # uu = BsysT.evaluate(points, U, {}, False) uu = BsysT.evaluate(points, np.zeros_like(U), BTGs, False) # print np.hstack((points, u)) # print u return u, uu
def mixedpoissondual(k,N, g, f, points): tag = "B1" hdiveltsA = HdivElements(k) hdiveltsB = HdivElements(k) hdiveltsBt = HdivElements(k) l2eltsB = L2Elements(k) l2eltsBt = L2Elements(k) quadrule = pyramidquadrature(k+1) meshevents = lambda m: buildcubemesh(N,m,tag) Asystem = SymmetricSystem(hdiveltsA, quadrule, meshevents, []) Bsystem = AsymmetricSystem(l2eltsB, hdiveltsB, quadrule, meshevents, [],[]) Btsystem = AsymmetricSystem(hdiveltsBt, l2eltsBt, quadrule, meshevents, [],[]) A = Asystem.systemMatrix(False) # Bt = Btsystem.systemMatrix(True, False) B = Bsystem.systemMatrix(False, True) print A.shape, B.shape F = Bsystem.loadVector(f, False) gn = lambda x,n: g(x).reshape(-1,1,1) * n.reshape(-1,1,3) G = Btsystem.boundaryLoad({tag:gn}, squarequadrature(k+1), trianglequadrature(k+1), False) u,p = ps.directsolve(A, B, G[tag], F) um,pm = ps.mixedcg(A, B, G[tag], F) # print numpy.hstack((UU[:len(u)], u)) print math.sqrt(numpy.sum((u - um)**2)/len(u)) print math.sqrt(numpy.sum((p - pm)**2)/len(p)) return Btsystem.evaluate(points, p, {}, False)
def poissonneumann(k,N,g,f, points): tag = "B1" elements = H1Elements(k) quadrule = pyramidquadrature(k+1) system = SymmetricSystem(elements, quadrule, lambda m: buildcubemesh(N,m,tag), []) SM = system.systemMatrix(True) S = SM[1:,:][:,1:] # the first basis fn is guaranteed to be associated with an external degree, so is a linear comb of all the others F = 0 if f is None else system.loadVector(f) G = 0 if g is None else system.boundaryLoad({tag:g}, squarequadrature(k+1), trianglequadrature(k+1), False) U = numpy.concatenate((numpy.array([0]), spsolve(S, G[tag][1:]-F[1:])))[:,numpy.newaxis] return system.evaluate(points, U, {}, False)
def testSymmetry(self): tag = "B1" for k in range(1,3): quadrule = pyramidquadrature(k+1) for N in range(1,3): for elements in [H1Elements(k), HcurlElements(k), HdivElements(k)]: system = SymmetricSystem(elements, quadrule, lambda m: buildcubemesh(N, m, tag), [tag]) for deriv in [False, True]: SM = system.systemMatrix(deriv) g = lambda x: np.zeros((len(x),1)) S, SIBs, Gs = system.processBoundary(SM, {tag:g}) np.testing.assert_array_almost_equal(SM.todense(), SM.transpose().todense()) np.testing.assert_array_almost_equal(S.todense(), S.transpose().todense())
def laplacedirichlet(k, N, g, points): tag = "B1" elements = H1Elements(k) quadrule = pyramidquadrature(k+1) system = SymmetricSystem(elements, quadrule, lambda m: buildcubemesh(N, m, tag), [tag]) SM = system.systemMatrix(True) S, SIBs, Gs = system.processBoundary(SM, {tag:g}) SG = SIBs[tag] * Gs[tag] print S.shape U = spsolve(S, -SG)[:,numpy.newaxis] return system.evaluate(points, U, Gs, False)
def testSymmetry(self): tag = "B1" for k in range(1,3): quadrule = pyramidquadrature(k+1) for N in range(1,3): hdivelt1 = HdivElements(k) l2elt1 = L2Elements(k) hdivelt2 = HdivElements(k) l2elt2 = L2Elements(k) system = AsymmetricSystem(hdivelt1, l2elt1, quadrule, lambda m:buildcubemesh(N,m,tag), [], []) systemt = AsymmetricSystem(l2elt2, hdivelt2, quadrule, lambda m:buildcubemesh(N,m,tag), [], []) SM = system.systemMatrix(True, False) StM = system.transpose().systemMatrix(False, True) SMt = systemt.systemMatrix(False, True) np.testing.assert_array_almost_equal(SM.todense(), SMt.transpose().todense()) np.testing.assert_array_almost_equal(StM.todense(), SMt.todense())
def laplaceeigs(k,N,n): import scipy.linalg as sl tag = "B1" elements = H1Elements(k) quadrule = pyramidquadrature(k+1) system = SymmetricSystem(elements, quadrule, lambda m: buildcubemesh(N, m, tag), [tag]) SM = system.systemMatrix(True) S, SIBs, Gs = system.processBoundary(SM, {tag:lambda p: numpy.zeros((len(p),1))}) print S.shape MM = system.systemMatrix(False) M, _, _ = system.processBoundary(MM, {tag:lambda p: numpy.zeros((len(p),1))}) MLU = ssl.splu(M) L = A = ssl.LinearOperator( M.shape, matvec=lambda x: MLU.solve(S* x), dtype=float) return ssl.eigen(L, k=n, which='SM', return_eigenvectors=False)
def poissondirichlet(k,N,g,f, points): tag = "B1" elements = H1Elements(k) quadrule = pyramidquadrature(k+1) system = SymmetricSystem(elements, quadrule, lambda m: buildcubemesh(N, m, tag), [tag]) SM = system.systemMatrix(True) S, SIBs, Gs = system.processBoundary(SM, {tag:g}) SG = SIBs[tag] * Gs[tag] if f: F = system.loadVector(f) else: F = 0 print SM.shape, S.shape, SIBs[tag].shape, Gs[tag].shape, F.shape, SG.shape t = Timer().start() U = spsolve(S, -F-SG)[:,numpy.newaxis] t.split("spsolve").show() return system.evaluate(points, U, Gs, False)
def stokespressure(k, meshevents, pressures, points, countdofs = False, avpressure = False): vortelts1 = pe.HcurlElements(k) vortelts2 = pe.HcurlElements(k) velelts1 = pe.HdivElements(k) velelts2 = pe.HdivElements(k) pressureelts1 = pe.L2Elements(k) quadrule = pu.pyramidquadrature(k+1) Asys = pa.SymmetricSystem(vortelts1, quadrule, meshevents, []) # Bsys = pa.AsymmetricSystem(velelts1, vortelts2, quadrule, meshevents, [bdytag], []) BsysT = pa.AsymmetricSystem(vortelts2, velelts1, quadrule, meshevents, [], [closedbdytag]) Csys = pa.AsymmetricSystem(pressureelts1,velelts2, quadrule, meshevents, [], [closedbdytag]) A = Asys.systemMatrix(False) BT = BsysT.systemMatrix(True, False) C = Csys.systemMatrix(False, True) v0 = lambda x: np.zeros_like(x)[:,np.newaxis,:] vt = lambda x,n: np.zeros_like(x)[:,np.newaxis,:] BTI, BTE, BTGs = BsysT.processBoundary(BT, {closedbdytag:v0}) CI, CE, CGs = Csys.processBoundary(C, {closedbdytag:v0}) Pav = Csys.loadVector(lambda x: np.ones((len(x),1,1))) # print "P ",P alltags = pressures.keys() + [closedbdytag] Gt = Asys.boundaryLoad(dict([(tag,vt) for tag in alltags]), pu.squarequadrature(k+1), pu.trianglequadrature(k+1), False) Pv = Csys.transpose().boundaryLoad(pressures, pu.squarequadrature(k+1), pu.trianglequadrature(k+1), False) # print "Gt ",Gt print A.shape, BT.shape, C.shape, BTI.shape, map(np.shape, BTE.values()), map(np.shape, BTGs.values()), map(np.shape, Gt.values()), map(np.shape, Pv.values()), CI.shape AL = sum(Gt.values()) + BTE[closedbdytag] * BTGs[closedbdytag] BL = sum(Pv.values()) # print "AL ",AL CL = -CE[closedbdytag] * CGs[closedbdytag] nvort = A.get_shape()[0] nvel = BTI.get_shape()[1] npress = C.get_shape()[0] print nvel if avpressure: S = ss.bmat([[A, -BTI, None, None],[-BTI.transpose(), None, CI.transpose(), None],[None, CI, None, Pav], [None,None,Pav.transpose(), None]]) L = np.vstack((AL,BL, CL, np.zeros((1,1)))) else: S = ss.bmat([[A, -BTI, None],[-BTI.transpose(), None, CI.transpose()],[None, CI, None]]) L = np.vstack((AL,BL, CL)) X = ps.solve(S, L) U = X[nvort:(nvort + nvel)] P = X[(nvort+nvel):(nvort+nvel+npress)] # print "X",X # print "U", U # print "BTGs", BTGs # u = BsysT.evaluate(points, U, BTGs, False) # uu = Asys.evaluate(points, np.eye(nvort)[-2], {}, False) # uu = BsysT.evaluate(points, U, {}, False) p = Csys.transpose().evaluate(points, P, {}, False) # print np.hstack((points, u)) # print u if countdofs: return u, len(X) return u, p