def bubblexnortho(n,h,kappa2,alpha,p,nel): xwr = gauss(r_jacobi(n+2*alpha+2)) xwl = gauss(r_jacobi(n+2*alpha)) X = linspace(-mpf(0.5)*h,mpf(0.5)*h,nel+1) Chat = [ppoly() for i in range(n)] for k in range(n): C = polyaff(conv(bw(alpha),mon(k)),-mpf(0.5)*h,mpf(0.5)*h,mpf(-1),mpf(1)) els,G,x,phi = fea1dh(X, lagrangecheby(p), kappa2, 0, [mpf(0),mpf(0)], rhsbubble(C, h, kappa2, xwl)) ppsol = ppolyfea1sol(els,G,x,phi) Chat[k] = ppolyaxpy(mpf(-1),polytoppoly(C,ppsol.intv),ppsol) h1ni = mpf(1)/sqrt(ppolyh1norm2(Chat[k],xwl)) Chat[k] = ppolyscale(Chat[k],h1ni) # Execute Gram-Schmidt D = [] for k in range(n): Dk = Chat[k] for l in range(k): aCkDl = ppolyh1inner(Chat[k], D[l], xwl, kappa2) aDlDl = ppolyh1inner(D[l], D[l], xwl, kappa2) Dk = ppolyaxpy(-aCkDl/aDlDl, D[l], Dk) D.append(Dk) Dinner = zeros(1,n) for k in range(n): Dinner[k] = ppolyh1inner(D[k], D[k], xwl, kappa2) return D,Dinner
def iapbuba(els,G,x,phi,kappa2,rhs,iapp): nel = len(els) uh = ppolyfea1sol(els,G,x,phi) duh = ppolyder(uh) res = ppolyaxpy(-kappa2,uh,ppolyder(duh)) # u_h''-kappa2 u_h p = phi.cols # Precompute bubble functions mp.dps = 30 alpha = p/2+1 h = (els[0][1]-els[0][0]) # ASSUME UNIFORM ELEMENTS nbub = iapp xwl = gauss(r_jacobi(nbub+2*alpha)) P = prebub(nbub,h,kappa2,alpha,xwl) bub = [] for i in range(P.rows): bub.append(polytoppoly(P[i,:], [(-0.5*h, 0.5*h)])) degbub = len(bub[0].poly[0]) # for the quadrature of the residual xwl = gauss(r_jacobi(max(degbub,p))) #nprint(loc) mp.dps = 15 h1norm2 = [] eh = ppoly() t1 = time.time() k = 0 for el in els: # Translate bubbles to the element tbub = [] for b in bub: bt = ppolytrans(b, 0.5*(el[0]+el[1])) tbub.append(bt) # Execute bubble kernel erhs = lambda phii, el0, el1 : rhs(phii, el0, el1, el0, el1) +\ 0.5*(el1-el0) * quadpqab(phii,res.poly[k],el0,el1, xwl) G,x = iapbubaker(tbub, erhs) eeh = ppolyiapbubsol(G,x,tbub) h1norm2.append(ppolyh1norm2(eeh,xwl)) ppolyext(eh, eeh) k += 1 return eh,h1norm2,time.time()-t1
def ppolyfea1sol(els,G,x,phi): pp = ppoly(els) e = 0 for el in els: q = zeros(1) for p in range(phi.rows): i = G[e][p] if i != -1: q = polyaxpy(x[i],polyaff(phi[p,:],el[0],el[1],-1,1),q) pp.poly[e] = q e += 1 return pp
def bubblexn(n,h,kappa2,alpha,p,nel): xwr = gauss(r_jacobi(n+2*alpha+2)) xwl = gauss(r_jacobi(n+2*alpha)) X = linspace(-mpf(0.5)*h,mpf(0.5)*h,nel+1) Chat = [ppoly() for i in range(n)] for k in range(n): C = polyaff(conv(bw(alpha),mon(k)),-mpf(0.5)*h,mpf(0.5)*h,mpf(-1),mpf(1)) els,G,x,phi = fea1dh(X, lagrangecheby(p), kappa2, 0, [mpf(0),mpf(0)], rhsbubble(C, h, kappa2, xwl)) ppsol = ppolyfea1sol(els,G,x,phi) Chat[k] = ppolyaxpy(mpf(-1),polytoppoly(C,ppsol.intv),ppsol) h1ni = mpf(1)/sqrt(ppolyh1norm2(Chat[k],xwl)) Chat[k] = ppolyscale(Chat[k],h1ni) return Chat
def ppolyfea1solpp(els,G,x,phi): pp = ppoly() r0 = phi[0].intv[0][0] r1 = phi[0].intv[-1][1] e = 0 for el in els: qq = ppolyaff(ppolyzero(phi[0].intv), r0, r1, el[0], el[1]) for p in range(len(phi)): if G[e][p] != -1: qq = ppolyaxpy(x[G[e][p]], ppolyaff(phi[p],el[0],el[1],r0,r1), qq) ppolyext(pp,qq) e += 1 return pp
def iapneu(els,G,x,phi,kappa2,rhs,duX,iapnel,iapp): nel = len(els) uh = ppolyfea1sol(els,G,x,phi) duh = ppolyder(uh) res = ppolyaxpy(-kappa2,uh,ppolyder(duh)) # u_h''-kappa2 u_h xwl = gauss(r_jacobi(phi.cols+iapp)) h1norm2 = [] eh = ppoly() k = 0 for el in els: psi = lagrangecheby(iapp) # Approximate derivatives on the boundaries if duX is None: A = 0 if k == 0 else 0.5*(polyval(duh.poly[k],el[0])-polyval(duh.poly[k-1],el[0])) B = 0 if k == nel-1 else 0.5*(polyval(duh.poly[k],el[1])-polyval(duh.poly[k+1],el[1])) else: A = duX[k]-polyval(duh.poly[k],el[0]) B = duX[k+1]-polyval(duh.poly[k],el[1]) #print "Ae={0:s}, Be={0:s}".format(nstr(Ae),nstr(Be)) erhs = lambda phii, r0, r1, el0, el1 : rhs(phii, r0, r1, el0, el1) +\ 0.5*(el1-el0) * quadpq(phii,polyaff(res.poly[k],mpf(-1),mpf(1),el0,el1), xwl) Xs = linspace(el[0],el[1],iapnel+1) subels = zip(Xs[0:iapnel], Xs[1:iapnel+1]) eels,eG,ex,epsi = fea1dapel1(subels, psi, kappa2, 1, [A, B], erhs) eeh = ppolyfea1sol(eels,eG,ex,epsi) h1norm2.append(ppolyh1norm2(eeh,xwl)) ppolyext(eh, eeh) k += 1 return eh, h1norm2
def bubble(n,h,kappa2,alpha,p,nel): xwr = gauss(r_jacobi(n+2*alpha+2)) xwl = gauss(r_jacobi(n+2*alpha)) P = prebub(n,h,kappa2,alpha,xwl) X = linspace(-mpf(0.5)*h,mpf(0.5)*h,nel+1) Chat = [ppoly() for i in range(n)] for k in range(n): els,G,x,phi = fea1dh(X, lagrangecheby(p), kappa2, 0, [mpf(0),mpf(0)], rhsbubble(P[k,:], h, kappa2, xwl)) ppsol = ppolyfea1sol(els,G,x,phi) Chat[k] = ppolyaxpy(mpf(-1),polytoppoly(P[k,:],ppsol.intv),ppsol) h1ni = mpf(1)/sqrt(ppolyh1norm2(Chat[k],xwl)) Chat[k] = ppolyscale(Chat[k],h1ni) #xx,yy = polyvalres(P[k,:],-mpf(0.5)*h,mpf(0.5)*h,100) #pl.plot(xx,yy,label=r"$C_{0:d}$".format(k),linewidth=0.8) #xx,yy = ppolyvalres(ppsol,100) #pl.plot(xx,yy,label=r"$C^h_{0:d}$".format(k),linewidth=0.8) return Chat
def iapbub(els,G,x,phi,kappa2,rhs,iapnel,iapp,sobolev=True): nel = len(els) uh = ppolyfea1sol(els,G,x,phi) duh = ppolyder(uh) res = ppolyaxpy(-kappa2,uh,ppolyder(duh)) # u_h''-kappa2 u_h p = phi.cols #print ">>>> iapbub on", els # Precompute bubble functions mp.dps = 20 alpha = p/2+1 h = (els[0][1]-els[0][0]) # ASSUME UNIFORM ELEMENTS nbub = iapp if sobolev == True: bub = bubble(nbub,h,kappa2,alpha,p,iapnel) else: bub = bubblexn(nbub,h,kappa2,alpha,p,iapnel) degbub = len(bub[0].poly[0]) # Determine local matrix xw = gauss(r_jacobi(degbub)) dbub = [ppolyder(b) for b in bub] loc = zeros(nbub) for k,l in product(range(nbub),range(nbub)): loc[k,l] = kappa2*quadppqq(bub[k], bub[l], xw) + quadppqq(dbub[k], dbub[l], xw) # for the quadrature of the residual xwl = gauss(r_jacobi(max(degbub,p))) #nprint(loc) mp.dps = 15 h1norm2 = [] eh = ppoly() t1 = time.time() k = 0 for el in els: # Translate bubbles to the element tbub = [] for b in bub: bt = ppolytrans(b, 0.5*(el[0]+el[1])) tbub.append(bt) # Execute bubble kernel erhs = lambda phii, el0, el1 : rhs(phii, el0, el1, el0, el1) +\ 0.5*(el1-el0) * quadpqab(phii,res.poly[k],el0,el1, xwl) G,x = iapbubker(tbub, loc, erhs) eeh = ppolyiapbubsol(G,x,tbub) h1norm2.append(ppolyh1norm2(eeh,xwl)) ppolyext(eh, eeh) k += 1 return eh,h1norm2,time.time()-t1
def ppolyiapbubsol(G,x,bub): pp = ppoly(bub[0].intv) for i in G[0]: pp = ppolyaxpy(x[G[0][i]], bub[i], pp) return pp
def ppolytrans(pp, a): qq = ppoly() for i in range(len(pp.intv)): qq.intv.append((pp.intv[i][0]+a, pp.intv[i][1]+a)) qq.poly.append(polycomp(pp.poly[i], poly([1, -a]))) return qq
def iapbubspread(els,G,x,phi,kappa2,rhs,iapnel,iapp,sobolev=True): nel = len(els) uh = ppolyfea1sol(els,G,x,phi) duh = ppolyder(uh) res = ppolyaxpy(-kappa2,uh,ppolyder(duh)) # u_h''-kappa2 u_h p = phi.cols #print ">>>> iapbub on", els # Precompute bubble functions mp.dps = 20 alpha = p/2+1 # ASSUME UNIFORM ELEMENTS !!! h1 = (els[0][1]-els[0][0]) # element size h = iapnel*h1 # spread size nbub = iapp spread = (iapnel-1)/2 if sobolev == True: bub1 = bubble(nbub,h1,kappa2,alpha,p,iapnel) bub = bubble(nbub,h,kappa2,alpha,p,iapnel) else: bub1 = bubblexn(nbub,h1,kappa2,alpha,p,iapnel) bub = bubblexn(nbub,h,kappa2,alpha,p,iapnel) degbub = len(bub[0].poly[0]) # Determine local matrices xw = gauss(r_jacobi(degbub)) dbub1 = [ppolyder(b) for b in bub1] dbub = [ppolyder(b) for b in bub] loc1 = zeros(nbub) loc = zeros(nbub) for k,l in product(range(nbub),range(nbub)): loc1[k,l] = kappa2*quadppqq(bub1[k], bub1[l], xw) + quadppqq(dbub1[k], dbub1[l], xw) loc[k,l] = kappa2*quadppqq(bub[k], bub[l], xw) + quadppqq(dbub[k], dbub[l], xw) # for the quadrature of the residual xwl = gauss(r_jacobi(max(degbub,p))) #nprint(loc) mp.dps = 15 h1norm2s = [] eh = ppoly() t1 = time.time() k = 0 for el in els: spreadels = [] # Only use spreaded bubbles if the whole spread interval is in the domain if k-spread < 0 or k+spread >= nel: # Fall back to 0 spread case # Translate bubbles to the element tbub = [] for b in bub1: bt = ppolytrans(b, 0.5*(el[0]+el[1])) tbub.append(bt) erhs = lambda phii, el0, el1 : rhs(phii, el0, el1, el0, el1) +\ 0.5*(el1-el0) * quadpqab(phii,res.poly[k],el0,el1, xwl) G,x = iapbubker(tbub, loc1, erhs) eeh = ppolyiapbubsol(G,x,tbub) h1norm2s.append(ppolyh1norm2(eeh,xwl)) ppolyext(eh, eeh) else: # Multiple element support tbub = [] for b in bub: bt = ppolytrans(b, 0.5*(el[0]+el[1])) tbub.append(bt) # xx,yy = ppolyvalres(bt, 100) # pl.plot(xx,yy,label="spread",linewidth=1.5) erhs = lambda phii, l, el0, el1 : rhs(phii, el0, el1, el0, el1) +\ 0.5*(el1-el0) * quadpqab(phii,res.poly[k-spread+l],el0,el1, xwl) G,x = iapbubspreadker(tbub, loc, erhs) eeh = ppolyiapbubsol(G,x,tbub) # We are only concerened with the restriction to the current element h1norm2s.append(h1norm2ab(eeh.poly[spread],eeh.intv[spread][0],eeh.intv[spread][1],xwl)) eh.intv.append(eeh.intv[spread]) eh.poly.append(eeh.poly[spread]) #xx,yy = polyvalres(eeh.poly[spread], eeh.intv[spread][0], eeh.intv[spread][1], 1000) #pl.plot(xx,yy,label="spread",linewidth=2) #xx,yy = ppolyvalres(eeh, 1000) #pl.plot(xx,yy,label="spread",linewidth=2) k += 1 return eh,h1norm2s,time.time()-t1