Esempio n. 1
0
    def disc(self):
        dim = int(self.read_next_non_whitespace().strip())
        center = np.array(next(self.fstream).split(), dtype=float)
        r = float(next(self.fstream).strip())
        z_axis = np.array(next(self.fstream).split(), dtype=float)
        x_axis = np.array(next(self.fstream).split(), dtype=float)
        degen = next(self.fstream).strip() != '0'
        angles = [float(next(self.fstream).strip()) for i in range(4)]
        param_u = np.array(next(self.fstream).split(), dtype=float)
        param_v = np.array(next(self.fstream).split(), dtype=float)
        swap = next(self.fstream).strip() != '0'

        if degen:
            result = SurfaceFactory.disc(r=r,
                                         center=center,
                                         xaxis=x_axis,
                                         normal=z_axis,
                                         type='radial')
        else:
            if not (np.allclose(np.diff(angles), pi / 2, atol=1e-10)):
                raise RuntimeError(
                    'Unknown square parametrization of disc elementary surface'
                )
            result = SurfaceFactory.disc(r=r,
                                         center=center,
                                         xaxis=x_axis,
                                         normal=z_axis,
                                         type='square')
        result.reparam(param_u, param_v)
        if swap:
            result.swap()
        return result
Esempio n. 2
0
    def test_disc(self):
        # radial disc
        surf = sf.disc()
        x = surf.evaluate([0, 1], [0, pi / 4, pi / 2, pi])
        self.assertTrue(np.allclose(x[0, 0], [0, 0]))
        self.assertTrue(np.allclose(x[1, 0], [1, 0]))
        self.assertTrue(np.allclose(x[1, 1], [1 / sqrt(2), 1 / sqrt(2)]))
        self.assertTrue(np.allclose(x[1, 2], [0, 1]))
        self.assertAlmostEqual(surf.area(), pi, places=3)

        # radial disc of size different from 1
        surf = sf.disc(4)
        # test evaluation at 25 points for radius=4
        v = np.linspace(surf.start('v'), surf.end('v'), 25)
        u = np.linspace(surf.start('u'), surf.end('u'), 5)
        x = surf.evaluate(u, v)
        for circles, i in zip(x, range(5)):
            for pt in circles:
                self.assertAlmostEqual(norm(pt, 2),
                                       4.0 * i / 4)  # check radius
        self.assertAlmostEqual(surf.area(), 4 * 4 * pi, places=3)

        # square disc
        surf = sf.disc(3, type='square')
        # evaluate on all 4 edges, 5 pts on each edge
        u = np.linspace(0, 1, 5)
        v = np.linspace(0, 1, 5)
        x = surf.evaluate(u, v)
        for pt in np.array(x[0, :, :]):  # umin edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[-1, :, :]):  # umax edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[:, 0, :]):  # vmin edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[:, -1, :]):  # vmax edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        self.assertAlmostEqual(surf.area(), 3 * 3 * pi, places=2)

        # test xaxis
        surf = sf.disc(r=3, xaxis=(0, 1, 0))
        u = surf.end('u')
        self.assertTrue(np.allclose(surf(u, 0), [0, 3]))

        # test normal
        surf = sf.disc(r=2, normal=(1, 1, 0), xaxis=(0, 0, 1))
        u = surf.end('u')
        self.assertTrue(np.allclose(surf(u, 0), [0, 0, 2]))
    def test_disc(self):
        # radial disc
        surf = SurfaceFactory.disc()
        x = surf.evaluate([0, 1], [0, pi / 4, pi / 2, pi])
        self.assertTrue(np.allclose(x[0,0], [0,0]))
        self.assertTrue(np.allclose(x[1,0], [1,0]))
        self.assertTrue(np.allclose(x[1,1], [1/sqrt(2),1/sqrt(2)]))
        self.assertTrue(np.allclose(x[1,2], [0,1]))
        self.assertAlmostEqual(surf.area(), pi, places=3)

        # radial disc of size different from 1
        surf = SurfaceFactory.disc(4)
        # test evaluation at 25 points for radius=4
        v = np.linspace(surf.start('v'), surf.end('v'),25)
        u = np.linspace(surf.start('u'), surf.end('u'), 5)
        x = surf.evaluate(u, v)
        for circles, i in zip(x, range(5)):
            for pt in circles:
                self.assertAlmostEqual(norm(pt, 2), 4.0 * i / 4)  # check radius
        self.assertAlmostEqual(surf.area(), 4*4*pi, places=3)

        # square disc
        surf = SurfaceFactory.disc(3, type='square')
        # evaluate on all 4 edges, 5 pts on each edge
        u = np.linspace(0, 1, 5)
        v = np.linspace(0, 1, 5)
        x = surf.evaluate(u, v)
        for pt in np.array(x[0, :, :]):  # umin edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[-1, :, :]):  # umax edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[:, 0, :]):  # vmin edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        for pt in np.array(x[:, -1, :]):  # vmax edge
            self.assertAlmostEqual(np.linalg.norm(pt, 2), 3.0)  # check radius
        self.assertAlmostEqual(surf.area(), 3*3*pi, places=2)

        # test xaxis
        surf = SurfaceFactory.disc(r=3, xaxis=(0,1,0))
        u = surf.end('u')
        self.assertTrue(np.allclose(surf(u,0), [0,3]))

        # test normal
        surf = SurfaceFactory.disc(r=2, normal=(1,1,0), xaxis=(0,0,1))
        u = surf.end('u')
        self.assertTrue(np.allclose(surf(u,0), [0,0,2]))
Esempio n. 4
0
 def test_center(self):
     # make an ellipse at (2,1)
     surf = sf.disc(3)
     print(surf)
     surf.scale((3, 1))
     surf += (2, 1)
     center = surf.center()
     self.assertEqual(len(center), 2)
     self.assertAlmostEqual(center[0], 2.0)
     self.assertAlmostEqual(center[1], 1.0)
Esempio n. 5
0
def cylinder(r=1, h=1, center=(0,0,0), axis=(0,0,1), xaxis=(1,0,0)):
    """  Create a solid cylinder

    :param float r: Radius
    :param float h: Height
    :param array-like center: The center of the bottom circle
    :param array-like axis: Cylinder axis
    :param array-like xaxis: direction of sem, i.e. parametric start point u=0
    :return: The cylinder
    :rtype: Volume
    """
    return extrude(SurfaceFactory.disc(r, center, axis, xaxis=xaxis), h*np.array(axis))
Esempio n. 6
0
def cylinder(r=1, h=1, center=(0, 0, 0), axis=(0, 0, 1), xaxis=(1, 0, 0)):
    """  Create a solid cylinder

    :param float r: Radius
    :param float h: Height
    :param array-like center: The center of the bottom circle
    :param array-like axis: Cylinder axis
    :param array-like xaxis: direction of sem, i.e. parametric start point u=0
    :return: The cylinder
    :rtype: Volume
    """
    return extrude(SurfaceFactory.disc(r, center, axis, xaxis=xaxis),
                   h * np.array(axis))
Esempio n. 7
0
    def disc(self):
        dim      = int(     self.read_next_non_whitespace().strip())
        center   = np.array(next(self.fstream).split(), dtype=float)
        r        = float(   next(self.fstream).strip())
        z_axis   = np.array(next(self.fstream).split(), dtype=float)
        x_axis   = np.array(next(self.fstream).split(), dtype=float)
        degen    =          next(self.fstream).strip() != '0'
        angles   = [float(  next(self.fstream).strip()) for i in range(4)]
        param_u  = np.array(next(self.fstream).split(), dtype=float)
        param_v  = np.array(next(self.fstream).split(), dtype=float)
        swap     =          next(self.fstream).strip() != '0'

        if degen:
            result = SurfaceFactory.disc(r=r, center=center, xaxis=x_axis, normal=z_axis, type='radial')
        else:
            if not(np.allclose(np.diff(angles), pi/2, atol=1e-10)):
                raise RuntimeError('Unknown square parametrization of disc elementary surface')
            result = SurfaceFactory.disc(r=r, center=center, xaxis=x_axis, normal=z_axis, type='square')
        result.reparam(param_u, param_v)
        if swap:
            result.swap()
        return result
Esempio n. 8
0
    def test_write_and_read_surface(self):
        # write disc to file and test if its there
        disc = SurfaceFactory.disc(type='square')
        with G2('disc.g2') as myfile:
            myfile.write(disc)
        self.assertTrue(os.path.isfile('disc.g2'))

        # read the written file and compare that it's the same thing
        with G2('disc.g2') as myfile:
            read_disc = myfile.read()
        self.assertEqual(len(read_disc), 1)
        read_disc = read_disc[0]
        self.assertTrue(np.all(disc.shape == read_disc.shape))

        # clean up after us
        os.remove('disc.g2')
Esempio n. 9
0
    def test_write_and_read_surface(self):
        # write disc to file and test if its there
        disc = SurfaceFactory.disc(type='square')
        with G2('disc.g2') as myfile:
            myfile.write(disc)
        self.assertTrue(os.path.isfile('disc.g2'))

        # read the written file and compare that it's the same thing
        with G2('disc.g2') as myfile:
            read_disc = myfile.read()
        self.assertEqual(len(read_disc), 1)
        read_disc = read_disc[0]
        self.assertTrue(np.all(disc.shape == read_disc.shape))

        # clean up after us
        os.remove('disc.g2')
Esempio n. 10
0
 def test_write_surface(self):
     srf = SurfaceFactory.disc(type='square').rebuild([4, 4], [7, 7])
     with SVG('output.svg') as myfile:
         myfile.write(srf)
     self.assertTrue(os.path.isfile('output.svg'))
     os.remove('output.svg')
Esempio n. 11
0
 def test_write_surface(self):
     srf = SurfaceFactory.disc(type='square').rebuild([4,4], [7,7])
     with SVG('output.svg') as myfile:
         myfile.write(srf)
     self.assertTrue(os.path.isfile('output.svg'))
     os.remove('output.svg')