Esempio n. 1
0
    def test_n_samples(self):
        #
        # Sampled Case
        #
        meshes = {1: Mesh1D(), 2: QuadMesh()}
        elements = {1: QuadFE(1, 'Q2'), 2: QuadFE(2, 'Q2')}

        # Use function to set data
        fns = {
            1: {
                1: lambda x: 2 * x[:, 0]**2,
                2: lambda x, y: 2 * x[:, 0] + 2 * y[:, 0]
            },
            2: {
                1: lambda x: x[:, 0]**2 + x[:, 1],
                2: lambda x, y: x[:, 0] * y[:, 0] + x[:, 1] * y[:, 1]
            }
        }

        # n_samples = 2
        parms = {1: {1: [{}, {}], 2: [{}, {}]}, 2: {1: [{}, {}], 2: [{}, {}]}}

        for dim in [1, 2]:
            mesh = meshes[dim]
            element = elements[dim]
            dofhandler = DofHandler(mesh, element)
            dofhandler.distribute_dofs()
            basis = Basis(dofhandler)
            for n_variables in [1, 2]:
                fn = fns[dim][n_variables]
                parm = parms[dim][n_variables]
                #
                # Deterministic
                #
                f = Nodal(f=fn,
                          mesh=mesh,
                          basis=basis,
                          element=element,
                          dim=dim,
                          n_variables=n_variables)
                self.assertEqual(f.n_samples(), 1)

                #
                # Sampled
                #
                f = Nodal(f=fn,
                          parameters=parm,
                          basis=basis,
                          mesh=mesh,
                          element=element,
                          dim=dim,
                          n_variables=n_variables)
                self.assertEqual(f.n_samples(), 2)
Esempio n. 2
0
    def test_eval_x(self):
        #
        # Evaluate Nodal function at a given set of x-values
        #

        # Meshes and elements
        meshes = {1: Mesh1D(resolution=(2, )), 2: QuadMesh(resolution=(2, 1))}
        elements = {1: QuadFE(1, 'Q2'), 2: QuadFE(2, 'Q2')}

        # Use function to set data
        fns = {
            1: {
                1: lambda x: 2 * x[:, 0]**2,
                2: lambda x, y: 2 * x[:, 0] + 2 * y[:, 0]
            },
            2: {
                1: lambda x: x[:, 0]**2 + x[:, 1],
                2: lambda x, y: x[:, 0] * y[:, 0] + x[:, 1] * y[:, 1]
            }
        }

        # n_samples = 2
        parms = {1: {1: [{}, {}], 2: [{}, {}]}, 2: {1: [{}, {}], 2: [{}, {}]}}

        n_points = 1
        for dim in [1, 2]:
            mesh = meshes[dim]
            element = elements[dim]
            dofhandler = DofHandler(mesh, element)
            dofhandler.distribute_dofs()
            #dofhandler.get_region_dofs()
            basis = Basis(dofhandler)
            #
            # Define random points in domain
            #
            if dim == 1:
                x_min, x_max = mesh.bounding_box()

                x = x_min + 0.5 * (x_max - x_min) * np.random.rand(n_points)
                x = x[:, np.newaxis]

                y = x_min + (x_max - x_min) * np.random.rand(n_points)
                y = y[:, np.newaxis]
            elif dim == 2:
                x_min, x_max, y_min, y_max = mesh.bounding_box()

                x = np.zeros((n_points, 2))
                x[:, 0] = x_min + (x_max - x_min) * np.random.rand(n_points)
                x[:, 1] = y_min + (y_max - y_min) * np.random.rand(n_points)

                y = np.zeros((n_points, 2))
                y[:, 0] = x_min + (x_max - x_min) * np.random.rand(n_points)
                y[:, 1] = y_min + (y_max - y_min) * np.random.rand(n_points)

            for n_variables in [1, 2]:
                fn = fns[dim][n_variables]
                parm = parms[dim][n_variables]
                #
                # Deterministic
                #
                f = Nodal(f=fn,
                          basis=basis,
                          mesh=mesh,
                          element=element,
                          dim=dim,
                          n_variables=n_variables)

                if n_variables == 1:
                    xx = x
                    fe = fn(x)
                elif n_variables == 2:
                    xx = (x, y)
                    fe = fn(*xx)
                fx = f.eval(x=xx)
                self.assertTrue(np.allclose(fx, fe))

                #
                # Sampled
                #
                f = Nodal(f=fn,
                          parameters=parm,
                          basis=basis,
                          mesh=mesh,
                          element=element,
                          dim=dim,
                          n_variables=n_variables)
                self.assertEqual(f.n_samples(), 2)

                fx = f.eval(x=xx)
                self.assertTrue(np.allclose(fx[:, 0], fe))
                self.assertTrue(np.allclose(fx[:, 1], fe))