Ejemplo n.º 1
0
            def adjoin_GaNi(a, b):
                print('Gani')
                a.dct()

                #    print(a.val)
                a.grad_transpose()

                #   print(a.val)
                a.idct()

                adjoin = Chebyshev_element(name='adjoin',
                                           N=N,
                                           shape=(),
                                           multype='scal')
                adjoin.val = np.multiply(a.val, b.val)
                adjoin.dct()
                #     print(adjoin.val)

                adjoin.idct()
                #    print('ab')

                #   print(adjoin.val)
                adjoin.integrate()
                #scalar = np.sum(adjoin.val)
                return adjoin.integral  #scalar  # #
Ejemplo n.º 2
0
    def test_interpolate(self):
        print('\nChecking enlarge and decrease')
        for dim, Ni in itertools.product([2], [5, 15, 55]):
            N = np.array(dim * [
                Ni,
            ], dtype=np.int)
            dN = tuple(np.array(N * 2 - 1, dtype=np.int))
            x0 = Chebyshev_element(name='x', N=N, shape=(), Fourier=False)
            x0.set_nodal_coord()
            x0.val = load(x0.coord, 'continuous')

            a = copy.deepcopy(x0)
            #  print(x0.val)
            x0.interpolate()
            #  print(x0.val)
            x0.dct()
            #  print(x0.val)
            x0.shrink()
            #  print(x0.val)
            x0.idct()
            #    print(x0.val)
            self.assertTrue(np.allclose(
                a.val,
                x0.val,
                atol=1e-12,
            ),
                            msg="Enlarge and shrink does not preserve values")
Ejemplo n.º 3
0
    def test_trans_dct(self):
        print('\nChecking transposed DCT')
        Ns = np.arange(4, 15, 1)
        for dim, Ni in itertools.product([2], Ns):
            N = np.array(dim * [
                Ni,
            ], dtype=np.int)
            print(N)
            u = Chebyshev_element(name='u', N=N, shape=(), multype='scal')
            u.set_nodal_coord()
            u.val = np.random.rand(N[0], N[1]) * 1
            u.Fourier = True
            u.idct()

            v = Chebyshev_element(name='u', N=N, shape=(), multype='scal')
            v.set_nodal_coord()
            v.val = np.random.rand(N[0], N[1]) * 1
            v.Fourier = True
            v.idct()

            def transform(a, b):
                b.dct()
                b.idct()
                scal = Chebyshev_element(name='scal',
                                         N=N,
                                         shape=(),
                                         multype='scal')
                scal.set_nodal_coord()
                scal.val = np.multiply(a.val, b.val)
                return scal.val

            def transposed_transform(a, b):
                # a.Fourier=True
                a.idct_trans()
                a.dct_trans()
                scal2 = Chebyshev_element(name='scal2',
                                          N=N,
                                          shape=(),
                                          multype='scal')
                scal2.set_nodal_coord()
                scal2.val = np.multiply(a.val, b.val)
                return scal2.val

            forward = transform(copy.deepcopy(u), copy.deepcopy(v))
            transposed = transposed_transform(copy.deepcopy(u),
                                              copy.deepcopy(v))

            #  print('  sum((u,Fv)   ={})'.format(np.sum(forward)))
            #  print('  sum((Ftu,v)   ={})'.format(np.sum(transposed)))
            #  print(' dif sum((Ftu,v)-(u,Fv))   ={})'.format(np.sum(transposed-forward)))

            self.assertTrue(np.isclose(np.sum(forward),
                                       np.sum(transposed),
                                       rtol=1e-10,
                                       atol=1e-08,
                                       equal_nan=False),
                            msg="Transposed DCT does not work correctly")
Ejemplo n.º 4
0
            def tester(a, b):
                a.dct()
                #  print(a.val)
                a.idct()
                adjoin = Chebyshev_element(name='test',
                                           N=N,
                                           shape=(),
                                           multype='scal')
                adjoin.val = np.multiply(a.val, b.val)
                adjoin.dct()
                # print(adjoin.val)

                adjoin.idct()
                #    print('ab')

                #   print(adjoin.val)
                adjoin.integrate()
                #scalar = np.sum(adjoin.val)
                return adjoin.integral  #scalar  # #
Ejemplo n.º 5
0
    def test_integral_w_projection(self):
        print('\nChecking integral with projection')
        solution = [0, 0.8, 8.3317]
        for dim, Ni in itertools.product([2, 3], [100, 155]):
            N = np.array(dim * [
                Ni,
            ], dtype=np.int)
            dN = tuple(np.array(N * 2 - 1, dtype=np.int))

            integrant = lambda x: (x[0]**4) + (x[1]**3) if x.__len__(
            ) == 2 else (x[0]**4) + (x[1]**3) + np.cos(x[2])
            u = Chebyshev_element(name='u', N=N, shape=(), multype='scal')
            u.set_nodal_coord()
            u.val = integrant(u.coord)

            u.integrate()
            i_1 = u.integral
            u.dct()
            u.enlarge()
            u.idct()
            u.integrate()
            i_2 = u.integral
            u.dct()
            u.shrink()
            u.idct()
            i_3 = u.integral

            self.assertTrue(np.isclose(i_1,
                                       i_2,
                                       rtol=1e-05,
                                       atol=1e-08,
                                       equal_nan=False),
                            msg="Integral does not work correctly")
            self.assertTrue(np.isclose(i_1,
                                       i_3,
                                       rtol=1e-05,
                                       atol=1e-08,
                                       equal_nan=False),
                            msg="Integral does not work correctly")
Ejemplo n.º 6
0
    def test_dct(self):
        print('\nChecking discrete cosine transform...')
        for dim, Ni in itertools.product([2, 3], [2, 4, 8]):
            N = tuple(np.array(dim * [
                Ni,
            ], dtype=np.int))
            values = np.random.random_sample(N)
            Element = Chebyshev_element(name='x', N=N, shape=())
            Element.val = values
            u1 = copy.deepcopy(Element.val)

            Element.dct()
            Element.idct()

            u2 = copy.deepcopy(Element.val)

            self.assertTrue(
                np.allclose(
                    u1,
                    u2,
                    atol=1e-15,
                ),
                msg="dct does not preserve values for dim {} and  N{}".format(
                    dim, Ni))
Ejemplo n.º 7
0
    def test_grad_transpose(self):
        print('\nChecking adjoin to gradient')
        Ns = np.arange(4, 25, 1)
        for dim, Ni in itertools.product([2], Ns):
            N = np.array(dim * [
                Ni,
            ], dtype=np.int)
            print(N)
            # u_val = lambda x: (x[0]**0 )*(x[1]**0 )

            u = Chebyshev_element(name='u', N=N, shape=(2, ), multype='scal')
            u.set_nodal_coord()
            u.val[0] = np.random.rand(N[0], N[1]) * 1
            u.val[1] = np.random.rand(N[0], N[1]) * 1
            u.Fourier = True
            u.idct()

            # u.Fourier = True

            v = Chebyshev_element(name='u', N=N, shape=(), multype='scal')
            v.set_nodal_coord()
            v.val = np.random.rand(N[0], N[1]) * 1  #u_val(u.coord)
            v.Fourier = True
            v.idct()

            #v.Fourier = True
            # u.grad_transpose()
            # print(u.val)

            def grad_forward(a, b):
                b.dct()
                b.grad()
                b.idct()

                scal = Chebyshev_element(name='scal',
                                         N=N,
                                         shape=(),
                                         multype='scal')
                scal.set_nodal_coord()
                scal.val = np.multiply(a.val, b.val)
                return scal.val

            def transposed_grad(a, b):
                # a.Fourier = True
                a.idct_trans()
                #   a.Fourier = True
                a.grad_transpose()
                #  a.Fourier = False
                a.dct_trans()
                scal2 = Chebyshev_element(name='scal2',
                                          N=N,
                                          shape=(),
                                          multype='scal')
                scal2.set_nodal_coord()
                scal2.val = np.multiply(a.val, b.val)
                return scal2.val

            forward = grad_forward(copy.deepcopy(u), copy.deepcopy(v))
            #print(forward)
            transposed = transposed_grad(copy.deepcopy(u), copy.deepcopy(v))
            #print(transposed)
            #print('  sum((u,iFGFv)   ={})'.format(np.sum(forward)))
            #print('  sum((FtGtiFtu,v)   ={})'.format(np.sum(transposed)))
            #    print(' dif sum((Ftu,v)-(u,Fv))   ={})'.format(np.sum(transposed)-np.sum(forward)))

            self.assertTrue(np.isclose(np.sum(forward),
                                       np.sum(transposed),
                                       rtol=1e-10,
                                       atol=1e-08,
                                       equal_nan=False),
                            msg="Transposed DCT does not work correctly")