def testEQ(self): for n in range(1, 6): mesh = tum.regularsquaremesh(n) mq = pmmu.MeshQuadratures(mesh, puq.legendrequadrature(6)) for i in range(6 * n ** 2): if i % 3 == 1: self.assertAlmostEquals(sum(mq.quadweights(i)), math.sqrt(2) / n) else: self.assertAlmostEquals(sum(mq.quadweights(i)), 1.0 / n)
def test2DMesh(self): for n in range(1,6,2): mesh = tum.regularsquaremesh(n, "TAG") # Count the number of faces. There are 2 * n^2 elements, 3 faces per element self.assertEqual(mesh.nfaces, 6 * n**2) # # Count the number of elements. There are 2 * n^2. self.assertEqual(mesh.nelements, 2 * n**2) # Check that every face is either a boundary or an internal face self.assertEqual((mesh.boundary + mesh.internal - ss.eye(mesh.nfaces, mesh.nfaces)).nnz, 0) # Check that the 2 ways of determining the boundary give the same faces self.assertEqual((mesh.boundary - mesh.entityfaces["TAG"]).nnz, 0)
def solvesquare(N, pdeg, g, basisrule, solve, k=20, dovolumes = True): impbd = pcbd.generic_boundary_data([-1j*k,1],[-1j*k,1],g) bdytag = "BDY" bnddata={bdytag:impbd} mesh = tum.regularsquaremesh(N, bdytag) p = 1 if pdeg < 1 else pdeg alpha = ((p*1.0)**2 * N) / k beta = k / (p * 1.0*N) problem=psp.Problem(mesh,k, bnddata) computation = psc.Computation(problem, basisrule, pcp.HelmholtzSystem, 15, alpha = alpha, beta = beta) solution = computation.solution(solve, dovolumes=dovolumes) return solution
def examplepic(n, k=20): origin = np.array([-0.2,-0.2]) bounds=np.array([[0,1],[0,1]],dtype='d') npoints=np.array([k * 20,k * 20], dtype=int) g = pcb.FourierHankel(origin, [0], k) pom.output2dfn(bounds, g.values, npoints, alpha=1.0, cmap=mp.cm.get_cmap('binary')) rt = PlaneWaveFromSourceRule(origin) mesh = tum.regularsquaremesh(n) pom.showmesh(mesh) etob = {} psp.Problem(mesh,k, None).populateBasis(etob, rt) pom.showdirections(mesh, etob) mp.show()
def bubblematerial(c = 1, N = 20): # slowness, gradslowness = bubble(c) slowness, gradslowness = hump(c) x0 = np.vstack((np.linspace(0,1,N), np.zeros(N))).T p0 = np.vstack((np.zeros(N), np.ones(N))).T wfs, idxs = prw.wavefront(x0, p0, slowness, gradslowness, 0.1, 1.2/c, 0.1) mp.subplot(1,2,1) plotwavefront(wfs, idxs) mesh = ptum.regularsquaremesh(12, "BDY") etob = prb.getetob(wfs, idxs, mesh, "BDY") mp.subplot(1,2,2) pom.showmesh(mesh) pom.showdirections(mesh, etob,scale=20)
def variableNhConvergence(Ns, nfn, bdycond, basisrule, process, k = 20, scale = 4.0, pdeg = 1): #bnddata={bdytag:pcbd.generic_boundary_data([-1j*k,1],[-1j*k,1],g=g)} bdytag = "BDY" bnddata={bdytag:bdycond} entityton ={1:nfn} for n in Ns: mesh = tum.regularsquaremesh(n, bdytag) alpha = ((pdeg*1.0)**2 * n) / k beta = k / (pdeg * 1.0*n) problem=psp.VariableNProblem(entityton, mesh,k, bnddata) computation = psc.Computation(problem, basisrule, pcp.HelmholtzSystem, 15, alpha = alpha, beta = beta) solution = computation.solution(psc.DirectSolver().solve, dovolumes=True) process(n, solution)
def testRaytraceMesh(self): c = 0.5 for (Nmesh, Nrt) in [(10,10), (20,4), (4, 20)]: mesh = ptum.regularsquaremesh(Nmesh, "BDY") x0 = np.vstack((np.linspace(-0.2,1.2,Nrt), np.ones(Nrt)*(-0.2))).transpose() p0 = np.vstack((np.zeros(Nrt), np.ones(Nrt))).transpose() slowness = lambda x: np.ones(len(x))/c gradslowness = lambda x: np.zeros_like(x) wfs, fidxs = prw.wavefront(x0, p0, slowness, gradslowness, 1/(c * Nrt), 1.4 / c, 1/(c * Nrt)) phases = prw.nodesToPhases(wfs, fidxs, mesh, ["BDY"]) self.assertEqual(len(phases), (Nmesh+1)**2) for vp in phases: self.assertGreaterEqual(len(vp), 1) for p in vp: np.testing.assert_array_almost_equal(p, [0,1/c])
def test2D(self): for n in range(1,10): mesh = tum.regularsquaremesh(n, "BDY") faces = mesh.entityfaces["BDY"] tracer = pre.HomogenousTrace(mesh, []) for f in faces.tocsr().indices: if np.dot(mesh.normals[f], (-1,0)) > 0.5: # look for a face on the left side of the cube point = mesh.directions[f][0] + np.sum(mesh.directions[f][1:-1], axis=0)/2.0 # pick a point in the middle rt = prc.RayTracing(mesh, [prc.TracePoint(f, point, [1,0])], tracer) etods = rt.getDirections() lengths = np.array(map(len, etods)) self.assertEquals(len(lengths.nonzero()[0]), 2*n) # one strip contains 2n triangles self.assertEquals(sum(lengths), 4*n) # each triangle should be painted twice etods = prc.RayTracing(mesh, [prc.TracePoint(f, point, [math.sqrt(2),1])], tracer, -1, 100).getDirections() self.assertLessEqual(sum(map(len, etods)), 100) # we should have managed to paint 100 elements break
def testDubiner(self): # The Dubiner basis is L^2-orthogonal, so we can test that the SVN reduction doesn't do much to it # ... On reflection, this doesn't test very much. But it at least exercises the code, so leaving it in k = 10 N = 3 bounds=np.array([[0.1,0.1],[0.9,0.9]],dtype='d') npoints=np.array([20,20]) sp = pug.StructuredPoints(bounds, npoints) for n in range(1,4): mesh = tum.regularsquaremesh(n) dubrule = pcbr.ReferenceBasisRule(pcbr.Dubiner(N)) problem = psp.Problem(mesh, k, {}) dubbasis = psp.constructBasis(problem, dubrule) refquad = puq.trianglequadrature(N+1) svnrule = pcbred.SVDBasisReduceRule(refquad, dubrule) svnbasis = psp.constructBasis(problem, svnrule) # The bases should be equivalent: testEquivalentBases(dubbasis, svnbasis, mesh, sp)
import numpy as np np.set_printoptions(threshold='nan') import pypwdg.parallel.main from numpy import array,sqrt k = 20 n = 6 g = pcb.FourierHankel([-1,-1], [0], k) bdytag = "BDY" bnddata={bdytag:pcbd.dirichlet(g)} bounds=array([[0,1],[0,1]],dtype='d') npoints=array([200,200]) mesh = tum.regularsquaremesh(n, bdytag) #print mesh.nodes #print mesh.elements problem = psp.Problem(mesh, k, bnddata) basisrule = pcb.planeWaveBases(2,k,9) nquad = 10 system = pcp.HelmholtzSystem compinfo = psc.ComputationInfo(problem, basisrule, 10) computation = psc.Computation(compinfo, system) solblockdiagonal = computation.solution(psi.DiagonalBlockOperator(mesh), psi.GMRESSolver('ctor', psi.ItCounter())) soldomaindecomp = computation.solution(psd.SchwarzOperator(mesh), psi.GMRESSolver('ctor', psi.ItCounter())) mortarrule = pcbr.ReferenceBasisRule(pcbr.Legendre1D(6)) s = -1j*k mc = psm.MortarComputation(problem, basisrule, mortarrule, nquad, system, system.boundaryclass, s)
#direction=np.array([[1.0,1.0]])/np.sqrt(2) #g = pcb.PlaneWaves(direction, k) g = pcb.FourierHankel([-0.5,-0.5], [0], k) impbd = pcbd.generic_boundary_data([-1j*k,1],[-1j*k,1],g) #bnddata={7:pcbd.dirichlet(g), # 8:pcbd.dirichlet(g)} bounds=np.array([[0,1],[0,1]],dtype='d') npoints=np.array([500,500]) n = 6 bnddata={'BDY':impbd} mesh = tum.regularsquaremesh(n, 'BDY') quadpoints = 10 npws = [] conds = [] errs = [] problem = psp.Problem(mesh, k, bnddata) for npw in range(7,45,2): quadpoints = npw # Original basis: pw = pcbv.PlaneWaveVariableN(pcb.uniformdirs(2,npw)) basisrule = pw computation = psc.Computation(psc.ComputationInfo(problem, basisrule, quadpoints),pcp.HelmholtzSystem) solution = computation.solution(psc.DirectOperator(), psc.DirectSolver()) op = psc.DirectOperator()
def testMEQ(self): for n in range(1, 6): mesh = tum.regularsquaremesh(n) mq = pmmu.MeshElementQuadratures(mesh, puq.trianglequadrature(5)) for i in range(2 * n ** 2): self.assertAlmostEquals(sum(mq.quadweights(i)), 1.0 / (2 * n ** 2))
def testReference(self): meshes = [tum.regularsquaremesh(2)] meshes = tum.examplemeshes2d() structuredpoints = pug.StructuredPoints([[0.01,0.01],[0.99,0.99]], [20,30]) rules = [pcbr.ReferenceBasisRule(pcbr.Dubiner(p)) for p in range(3)] pcbtt.basisDerivatives(rules, meshes, structuredpoints, k=1.0)