def analytichconvergence(maxN, k = 20, scale = 4.0): fileroot = "hconv.k%s.scale%s"%(k,scale) bounds=np.array([[0,1],[0,1]],dtype='d') npoints=np.array([k * scale * 10,k * scale * 10], dtype=int) S = harmonic1(scale) g = HarmonicDerived(k, S) nfn = NormOfGradient(S) bdycond = pcbd.dirichlet(g) npw = 15 pdeg = 2 Ns = genNs(math.pow(2,1.0/3),1,maxN+1) pw = pcbv.PlaneWaveVariableN(pcb.circleDirections(npw)) fo = pof.ErrorFileOutput(fileroot + 'uniformpw%s'%npw, str(Ns), g, bounds, npoints) variableNhConvergence(Ns, nfn, bdycond, pw, fo.process, k, scale) poly = pcbr.ReferenceBasisRule(pcbr.Dubiner(pdeg)) fo = pof.ErrorFileOutput(fileroot + 'poly%s'%pdeg, str(Ns), g, bounds, npoints) variableNhConvergence(Ns, nfn, bdycond, poly, fo.process, k, scale, pdeg) for err in [0, 0.02, 0.2]: rt = PlaneWaveFromDirectionsRule(S, err) fo = pof.ErrorFileOutput(fileroot + 'rt-err%s'%err, str(Ns), g, bounds, npoints) variableNhConvergence(Ns, nfn, bdycond, rt, fo.process, k, scale) for p in [1,2,3,4]: poly = pcbr.ReferenceBasisRule(pcbr.Dubiner(p)) polyrt = pcb.ProductBasisRule(poly, rt) fo = pof.ErrorFileOutput(fileroot + 'poly%srt-err%s'%(p,err), str(Ns), g, bounds, npoints) variableNhConvergence(Ns, nfn, bdycond, polyrt, fo.process, k, scale, p)
def testL2Prod(self): N = 20 k = 10 qxw = puq.squarequadrature(N) D = 1 g = pcb.PlaneWaves(pcb.circleDirections(40)[15], k) t1 = prp.findpw(prp.L2Prod(g.values, qxw, k), D, maxtheta = 1) self.assertAlmostEqual(t1, (2 * math.pi * 15) / 40)
def testDirections(self): n = 4 d1 = pcb.cubeDirections(n) self.assertEqual(len(d1), n*n) d1r = pcb.cubeRotations(d1) self.assertEqual(len(d1r), 6 * n * n) d2 = pcb.circleDirections(n) self.assertEqual(len(d2), n)
def fullyvariable(): npw1 = 15 npw2 = 15 b1 = pcb.ProductBasisRule(pcbv.PlaneWaveVariableN(pcb.circleDirections(npw1)),pcbr.ReferenceBasisRule(pcbr.Dubiner(2))) b2= pcbv.PlaneWaveVariableN(pcb.circleDirections(npw2)) basisDict={11:b2,12:b1} basisrule = pcb.GeomIdBasisRule(basisDict) #basisrule = pcbv.PlaneWaveVariableN(pcb.circleDirections(npw*3)) entityton = {11:1.0, 12:QuadBubble(1.0, 2.0)} problem = psp.VariableNProblem(entityton, mesh, k, bnddata) computation = psc.Computation(problem, basisrule, pcp.HelmholtzSystem, quadpoints) solution = computation.solution(psc.DirectSolver().solve, dovolumes=True) pos.standardoutput(computation, solution, quadpoints, bounds, npoints, 'twocirclesFV')
def elementwiseconstant(): npw = 12 basisrule = pcbv.PlaneWaveVariableN(pcb.circleDirections(npw)) entityton = {11:1.0,12:1.5} problem = psp.VariableNProblem(entityton, mesh, k, bnddata) computation = psc.Computation(problem, basisrule, pcp.HelmholtzSystem, quadpoints) solution = computation.solution(psc.DirectSolver().solve) pos.standardoutput(computation, solution, quadpoints, bounds, npoints, 'twocirclesEWC')
def testEdge(self): N = 10 k = 10 D = 1 x,w = puq.legendrequadrature(3*N) x = np.hstack((x, np.zeros_like(x))) g = pcb.PlaneWaves(pcb.circleDirections(40)[15], k) bc = pcbd.generic_boundary_data([1j*k,1],[1j*k,1],g) # t1 = prp.findpw(prp.ImpedanceProd(bc, (x,w), [0,1], k), D, maxtheta = 1) t1 = prp.findpw(prp.L2Prod(bc.values, (x,w), k), D, maxtheta = 1) self.assertAlmostEqual(t1, (2 * math.pi * 15) / 40)
def testConstrainedOptimisation(self): k = 4 g = pcb.PlaneWaves(numpy.array([[3.0/5,4.0/5]]), k).values # g = pcb.FourierBessel(numpy.array([-2,-1]), numpy.array([5]),k) npw = 3 nq = 8 ini = pcb.circleDirections(npw) triquad = puq.trianglequadrature(nq) linearopt = puo.LeastSquaresFit(g, triquad) pwpg = puo.PWPenaltyBasisGenerator(k, 1, 2) basis = puo.optimalbasis3(linearopt.optimise, pwpg.finalbasis, ini) (coeffs, l2err) = linearopt.optimise(basis) self.assertAlmostEqual(sum(l2err),0)
def analyticconvergencepwprod(maxN, k = 20, scale = 4.0): fileroot = "hconv.k%s.scale%s"%(k,scale) bounds=np.array([[0,1],[0,1]],dtype='d') npoints=np.array([k * scale * 10,k * scale * 10], dtype=int) S = harmonic1(scale) g = HarmonicDerived(k, S) nfn = NormOfGradient(S) bdycond = pcbd.dirichlet(g) npw = 15 Ns = genNs(math.pow(2,1.0/2),1,maxN+1) pw = pcbv.PlaneWaveVariableN(pcb.circleDirections(npw)) for p in [1,2,3,4]: poly = pcbr.ReferenceBasisRule(pcbr.Dubiner(p)) polypw = pcb.ProductBasisRule(poly, pw) fo = FileOutput(fileroot + 'pw%spoly%s'%(npw,p), str(Ns), g, bounds, npoints) variableNhConvergence(Ns, nfn, bdycond, polypw, fo.process, k, scale, p)
alpha=k*p**2*1./h delta=.5 alpha=.5 beta=.5 delta=.5 entityton ={6:nfun} problem=psp.VariableNProblem(entityton, mesh,k, bnddata) etods = prc.tracemesh(problem, {8:lambda x:direction}) etob = [[pcb.PlaneWaves(ds, k)] if len(ds) else [] for ds in etods] pob.vtkbasis(mesh,etob,'waveguide_rays.vtu',None) b0=pcbv.PlaneWaveVariableN(pcb.circleDirections(10)) b=pcb.PlaneWaveFromDirectionsRule(etods) b1=pcb.ProductBasisRule(b,pcbr.ReferenceBasisRule(pcbr.Dubiner(p))) b2=pcbr.ReferenceBasisRule(pcbr.Dubiner(p)) origins=np.array([[0,0],[0,5],[5,1],[0,1]]) h=pcb.FourierHankelBasisRule(origins,[0]) h2=pcb.ProductBasisRule(h,pcbr.ReferenceBasisRule(pcbr.Dubiner(p))) bh=pcb.UnionBasisRule([h2,b1]) b3=pcb.ProductBasisRule(b0,b2) basisrule = b3 #cbred.SVDBasisReduceRule(puq.trianglequadrature(quadpoints), bh, threshold=1E-5)
def augmentparams(params, npw, dim): if params==None: params = np.zeros((0,dim)) if len(params) < npw: params = np.vstack((params, pcb.circleDirections(npw - len(params)))) return normalise(params, npw)
print "ns: ",ns print "pw1s: ", pw1s print "pp1s: ", pp1s print "pw2s: ", pw2s print "pp2s: ", pp2s MAXDOF = 500000 for ni, n in enumerate(ns): mesh = tum.regularsquaremesh(n, bdytag) problem=psp.VariableNProblem(entityton, mesh,k, bnddata) for pi,p in enumerate(pw1s): if p * n * n * 2 > MAXDOF: break basisrule = pcbv.PlaneWaveVariableN(pcb.circleDirections(p)) pwerr[ni,pi] = geterr(problem, basisrule) # basisrule = pcbr.ReferenceBasisRule(pcbr.Dubiner(9)) #basisrule = pcb.planeWaveBases(2, k, 20) #basisrule = pcb.ProductBasisRule(pcbv.PlaneWaveVariableN(pcb.circleDirections(13)), pcbr.ReferenceBasisRule(pcbr.Dubiner(1))) # pom.output2dfn(bounds, entityton[1], npoints) # pom.output2dfn(bounds, g.values, npoints) for pi,p in enumerate(pp1s): if p * (p+1) * n * n > MAXDOF: break basisrule = pcb.ProductBasisRule(PlaneWaveFromDirectionsRule(S), pcbr.ReferenceBasisRule(pcbr.Dubiner(p))) polydirerr[ni,pi] = geterr(problem, basisrule) basisrule = pcbr.ReferenceBasisRule(pcbr.Dubiner(p))
from pypwdg.core.vandermonde import LocalVandermondes #mesh_dict=gmsh_reader('../../examples/2D/squarescatt.msh') mesh=gmshMesh('squarescatt.msh',dim=2) #mesh.partition(4) #print cubemesh.nodes boundaryentities = [10,11] #SM = StructureMatrices(mesh, boundaryentities) Nq = 20 Np = 15 dirs = circleDirections(Np) elttobasis = [[PlaneWaves(dirs, k)]] * mesh.nelements params={'alpha':.5, 'beta':.5,'delta':.5} bnddata={11:dirichlet(g), 10:zero_impedance(k)} quad=legendrequadrature(Nq) mqs = MeshQuadratures(mesh, quad) lv = LocalVandermondes(mesh, elttobasis, mqs, usecache=True) bndvs=[] for data in bnddata.values(): bndv = LocalVandermondes(mesh, [[data]] * mesh.nelements, mqs) bndvs.append(bndv)
if __name__ == '__main__': N = 20 k = 20 qxw = puq.squarequadrature(N) D = math.sqrt(2) D = 1 # # mesh = tum.regularsquaremesh() # e = 0 # mqs = pmmu.MeshQuadratures(mesh, puq.legendrequadrature(N)) # qx = np.vstack([mqs.quadpoints(f) for f in mesh.etof[e]]) # qw = np.concatenate([mqs.quadweights(f) for f in mesh.etof[e]]) # qxw = (qx,qw) qrp(qxw, pcb.circleDirections(4), k) g = pcb.PlaneWaves(pcb.circleDirections(40)[15], k) g = pcb.FourierHankel([-1,-0.5], [10], k) g = pcb.BasisReduce(pcb.PlaneWaves(pcb.circleDirections(20)[[5,8]], k), [3,1]) g = pcb.BasisReduce(pcb.BasisCombine([pcb.FourierHankel([-1,-0.5], [0], k), pcb.FourierHankel([-0.2,0.5], [0], k)]), [1,1]) # g = pcb.FourierBessel([0.25,0.25],[20], k) t1 = pap.findpw(pap.L2Prod(g.values, qxw, k), D, maxtheta = 1) g1 = project(g, qxw, k, t1) t2 = pap.findpw(pap.L2Prod(g1.values, qxw, k), D, maxtheta = 1) g2 = project(g1, qxw, k, t2) print t1 print t2 theta, proj, projd, projdd = pwproduniform(g.values, qxw, k, 500)
zeroimp = pcbd.zero_impedance(averagek) entityton = {5: veldata} mesh = ptum.regularrectmesh( [xmin, xmax], [ymin, ymax], int(((xmax - xmin) / (ymax - ymin)) / effectiveh), int(1 / effectiveh) ) print mesh.nelements bnddata = {1: zeroimp, 2: sourceimp, 3: zeroimp, 4: zeroimp} # bdndata = {0:impbd, 1:pcbd.zero_impedance} problem = psp.VariableNProblem(entityton, mesh, averagek, bnddata) alpha = ((pdeg * 1.0) ** 2 / trueh) / averagek beta = averagek / (pdeg * 1.0 * trueh) pw = pcbv.PlaneWaveVariableN(pcb.circleDirections(npw)) if usepoly: poly = pcbr.ReferenceBasisRule(pcbr.Dubiner(pdeg)) polypw = pcb.ProductBasisRule(poly, pw) basisrule = polypw else: basisrule = pw computation = psc.Computation( problem, basisrule, pcp.HelmholtzSystem, quadpoints, alpha=alpha, beta=beta, usecache=False ) solution = computation.solution(psc.DirectSolver().solve, dovolumes=True) pos.standardoutput( computation, solution, quadpoints,
""" import test.utils.mesh as tum import pypwdg.mesh.meshutils as pmmu import pypwdg.utils.quadrature as puq import pypwdg.core.bases as pcb import numpy as np import scipy.linalg as sl import math import matplotlib.pyplot as mp def geterrs(g, (qx, qw), auxbasis, k, n): sqw = np.sqrt(qw).reshape(-1, 1) auxv = auxbasis.values(qx) * sqw print auxv.shape pws = pcb.PlaneWaves(pcb.circleDirections(n), k) pwv = pws.values(qx) * sqw gv = g.values(qx) * sqw errs = np.empty(n) for i in range(n): bv = np.hstack((auxv, pwv[:, i].reshape(-1, 1))) coeffs = sl.lstsq(bv, gv)[0] resids = np.dot(bv, coeffs) - gv errs[i] = math.sqrt(np.vdot(resids, resids)) return errs if __name__ == "__main__": k = 10 N = 40