Exemple #1
0
def basisDerivatives(basisrules, meshes, structuredpoints, k):
    # Tests that we get the correct directional derivatives 
    N = 5
    h = 1E-5
    for mesh in meshes:
        ei = pcbu.ElementInfo(mesh, k)
        for e in range(mesh.nelements):
            _, points = pug.elementToStructuredPoints(structuredpoints, mesh, e)
            # generate some random normals
            if points is not None and len(points):
                nn = nr.random((len(points),2))
                n = nn / np.sqrt(np.sum(nn**2, axis=1)).reshape(-1,1)
                for basisrule in basisrules:
                    basis = basisrule.populate(ei.info(e))[0]
                    
                    vd = basis.derivs(points)
                    vl = basis.laplacian(points)
                    vp = basis.values(points)
                    
                    vph = [basis.values(points + dx) for dx in [[h,0],[0,h], [-h,0], [0,-h]]]
                    vdh = np.dstack([vph[0] - vph[2], vph[1] - vph[3]])/(2*h)
                    vlh = (sum(vph) - 4*vp) / (h**2)
                    
                    vdn = basis.derivs(points, n)
                    vdnh = (basis.values(points + n * h) - basis.values(points - n * h)) / (2*h) # compute the derivatives in mesh coordinates using finite differences
                    
                    np.testing.assert_allclose(vdnh, vdn, rtol=1E-5, atol=1E-5)
                    np.testing.assert_allclose(vdh, vd, rtol=1E-5, atol=1E-5)
                    np.testing.assert_allclose(vlh, vl, rtol=1E-3, atol=1E-4)
Exemple #2
0
def outputMeshPartition(bounds, npoints, mesh, nparts = 0):
    ''' Show the mesh partitions'''
    bounds=np.array(bounds,dtype='d')
    points = pug.StructuredPoints(bounds.transpose(), npoints)
    parts = getPartitions(mesh) if nparts==0 else mesh.basicinfo.partition(nparts)
    v = np.ones(points.length)*-1
    for i,p in enumerate(parts):
        for eid in p:
            epoints, _ = pug.elementToStructuredPoints(points, mesh, eid) 
            v[np.array(epoints)] += (i+1)
    image(v, npoints, bounds, colorbar=False)
    showmesh(mesh)
Exemple #3
0
    def evaluate(self, structuredpoints):
        outputshape = structuredpoints.length if len(self.x.shape) == 1 else (structuredpoints.length, self.x.shape[1])
        vals = numpy.zeros(outputshape, dtype=numpy.complex128)
        pointcount = numpy.zeros(structuredpoints.length, dtype=int)
        for e in self.mesh.partition:
            pointidxs, points = elementToStructuredPoints(structuredpoints, self.mesh, e)
            if len(pointidxs):
                v = self.elttobasis.getValues(e, points)
                (vidx0,vidx1) = self.elttobasis.getIndices()[e:e+2]       
                vals[pointidxs] += numpy.dot(v, self.x[vidx0: vidx1])
                pointcount[pointidxs]+=1
        return self.filter(vals), pointcount
    




        
        
Exemple #4
0
    def compareStructuredAndUnstructured(self, meshes, dim):
        """ Compare the structured and unstructured element-to-point routines
        
            Since the unstructured routine picks an arbitrary element for points that lie on boundaries
            but the structured routine will say that such points lie in both elements, this routine
            can at best say that the two routines are compatible.
            """
        vertices = np.array([[0] * dim, [1] * dim])
        sp = pug.StructuredPoints(vertices, np.array([5] * dim))
        for mesh in meshes:
            points = sp.getPoints(vertices)[1]
            #            print points
            etopunstructured = pug.pointsToElement(points, mesh)
            pointscheck = etopunstructured == -1

            for e in range(mesh.nelements):
                ps = pug.elementToStructuredPoints(sp, mesh, e)[0]
                self.assertFalse((etopunstructured[ps] == -1).any())
                pointscheck[ps] |= etopunstructured[ps] == e

            self.assertTrue(pointscheck.all())