Ejemplo n.º 1
0
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)    
Ejemplo n.º 2
0
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
Ejemplo n.º 3
0
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)    
Ejemplo n.º 4
0
def convergence(N):
    x,w= pu.squarequadrature(N*2)
    Nv = poisson(N*2,x)[0].flatten()
    Nvl2 = math.sqrt(sum(Nv**2 * w))
    e = np.zeros(N)
    for n in range(N):
        nv = poisson(n,x)[0].flatten()
        e[n] = math.sqrt(sum((Nv - nv)**2 * w)) / Nvl2
    print e
    p = mp.semilogy(np.arange(N)*2, e)
    mp.xlabel("polynomial degree", fontsize=14)
    mp.ylabel("relative error", fontsize=14)
Ejemplo n.º 5
0
    def testBoundaryQuad(self):
        from pypyr.utils import squarequadrature, trianglequadrature
        tag = "TAG"
        N = 1
        n = 3
        bq = BoundaryQuadrature()
        buildcubemesh(N, bq, boundarytag = tag)
        s = 0
        sx = 0
        for x,w, ns in bq.getQuadratures(tag, squarequadrature(n), trianglequadrature(n)):
            if len(x):
                s+=sum(w)
                sx += numpy.dot(x[:,0], w)
                x0 = x[0]
#                print ns, len(x), len(w)
                for xp,np in zip(x, ns):
                     self.assertEqual(numpy.dot(xp - x0, np), 0)
                     self.assertTrue(numpy.dot(xp - numpy.array([0.5,0.5,0.5]), np) > 0)
                    
#                     print xp, np, numpy.dot(xp - numpy.array([0.5,0.5,0.5]), np)
                     
        self.assertAlmostEqual(s, 6)
        self.assertAlmostEqual(sx, 3)
Ejemplo n.º 6
0
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