Example #1
0
    def test_compute_inner_product_mats(self):
        """Test computation of matrix of inner products."""
        num_row_vecs_list = [
            1,
            int(round(self.total_num_vecs_in_mem / 2.)),
            self.total_num_vecs_in_mem, self.total_num_vecs_in_mem * 2,
            _parallel.get_num_procs() + 1
        ]
        num_col_vecs_list = num_row_vecs_list
        num_states = 6

        row_vec_path = join(self.test_dir, 'row_vec_%03d.txt')
        col_vec_path = join(self.test_dir, 'col_vec_%03d.txt')

        for num_row_vecs in num_row_vecs_list:
            for num_col_vecs in num_col_vecs_list:
                # generate vecs
                _parallel.barrier()
                row_vec_array = _parallel.call_and_bcast(
                    np.random.random, (num_states, num_row_vecs))
                col_vec_array = _parallel.call_and_bcast(
                    np.random.random, (num_states, num_col_vecs))
                row_vec_handles = [
                    V.VecHandleArrayText(row_vec_path % i)
                    for i in range(num_row_vecs)
                ]
                col_vec_handles = [
                    V.VecHandleArrayText(col_vec_path % i)
                    for i in range(num_col_vecs)
                ]

                # Save vecs
                if _parallel.is_rank_zero():
                    for i, h in enumerate(row_vec_handles):
                        h.put(row_vec_array[:, i])
                    for i, h in enumerate(col_vec_handles):
                        h.put(col_vec_array[:, i])
                _parallel.barrier()

                # If number of rows/cols is 1, check case of passing a handle
                if len(row_vec_handles) == 1:
                    row_vec_handles = row_vec_handles[0]
                if len(col_vec_handles) == 1:
                    col_vec_handles = col_vec_handles[0]

                # Test IP computation.
                product_true = np.dot(row_vec_array.T, col_vec_array)
                product_computed = self.my_vec_ops.compute_inner_product_mat(
                    row_vec_handles, col_vec_handles)
                row_vecs = [row_vec_array[:, i] for i in range(num_row_vecs)]
                col_vecs = [col_vec_array[:, i] for i in range(num_col_vecs)]
                np.testing.assert_allclose(product_computed, product_true)

                # Test symm IP computation
                product_true = np.dot(row_vec_array.T, row_vec_array)
                product_computed = \
                    self.my_vec_ops.compute_symmetric_inner_product_mat(
                        row_vec_handles)
                row_vecs = [row_vec_array[:, i] for i in range(num_row_vecs)]
                np.testing.assert_allclose(product_computed, product_true)
Example #2
0
    def test_compute_modes(self):
        """Test computing modes in serial and parallel."""
        atol = 1e-6
        direct_mode_path = join(self.test_dir, 'direct_mode_%03d.txt')
        adjoint_mode_path = join(self.test_dir, 'adjoint_mode_%03d.txt')

        # starts with the correct decomposition.
        self.my_BPOD.R_sing_vecs = self.R_sing_vecs_true
        self.my_BPOD.L_sing_vecs = self.L_sing_vecs_true
        self.my_BPOD.sing_vals = self.sing_vals_true

        direct_mode_handles = [
            V.VecHandleArrayText(direct_mode_path % i) for i in self.mode_nums
        ]
        adjoint_mode_handles = [
            V.VecHandleArrayText(adjoint_mode_path % i) for i in self.mode_nums
        ]

        self.my_BPOD.compute_direct_modes(
            self.mode_nums,
            direct_mode_handles,
            direct_vec_handles=self.direct_vec_handles)
        self.my_BPOD.compute_adjoint_modes(
            self.mode_nums,
            adjoint_mode_handles,
            adjoint_vec_handles=self.adjoint_vec_handles)

        for mode_index, mode_handle in enumerate(direct_mode_handles):
            mode = mode_handle.get()
            np.testing.assert_allclose(
                mode,
                self.direct_mode_array[:, self.mode_nums[mode_index]],
                atol=atol)

        for mode_index, mode_handle in enumerate(adjoint_mode_handles):
            mode = mode_handle.get()
            np.testing.assert_allclose(
                mode,
                self.adjoint_mode_array[:, self.mode_nums[mode_index]],
                atol=atol)

        for direct_mode_index, direct_handle in \
            enumerate(direct_mode_handles):
            direct_mode = direct_handle.get()
            for adjoint_mode_index, adjoint_handle in \
                enumerate(adjoint_mode_handles):
                adjoint_mode = adjoint_handle.get()
                IP = self.my_BPOD.vec_space.inner_product(
                    direct_mode, adjoint_mode)
                if self.mode_nums[direct_mode_index] != \
                    self.mode_nums[adjoint_mode_index]:
                    self.assertAlmostEqual(IP, 0., places=6)
                else:
                    self.assertAlmostEqual(IP, 1., places=6)
Example #3
0
    def setUp(self):
        self.test_dir = 'DELETE_ME_test_files_pod'
        if not os.access('.', os.W_OK):
            raise RuntimeError('Cannot write to current directory')
        if not os.path.isdir(self.test_dir) and _parallel.is_rank_zero():
            os.mkdir(self.test_dir)
        self.mode_indices = [2, 4, 3, 6]
        self.num_vecs = 10
        self.num_states = 30
        self.vec_array = _parallel.call_and_bcast(
            np.random.random, (self.num_states, self.num_vecs))
        self.correlation_mat_true = self.vec_array.conj().transpose().dot(
            self.vec_array)

        self.eigen_vals_true, self.eigen_vecs_true = \
            _parallel.call_and_bcast(util.eigh, self.correlation_mat_true)

        self.mode_array = np.dot(
            self.vec_array,
            np.dot(self.eigen_vecs_true, np.diag(self.eigen_vals_true**-0.5)))
        self.vec_path = join(self.test_dir, 'vec_%03d.txt')
        self.vec_handles = [
            V.VecHandleArrayText(self.vec_path % i)
            for i in range(self.num_vecs)
        ]
        for vec_index, handle in enumerate(self.vec_handles):
            handle.put(self.vec_array[:, vec_index])

        self.my_POD = PODHandles(np.vdot, verbosity=0)
        _parallel.barrier()
Example #4
0
    def test_compute_modes(self):
        mode_path = join(self.test_dir, 'mode_%03d.txt')
        mode_handles = [
            V.VecHandleArrayText(mode_path % i) for i in self.mode_indices
        ]
        # starts with the CORRECT decomposition.
        self.my_POD.eigen_vecs = self.eigen_vecs_true
        self.my_POD.eigen_vals = self.eigen_vals_true

        self.my_POD.compute_modes(self.mode_indices,
                                  mode_handles,
                                  vec_handles=self.vec_handles)

        for mode_index, mode_handle in enumerate(mode_handles):
            mode = mode_handle.get()
            np.testing.assert_allclose(
                mode.squeeze(), self.mode_array[:,
                                                self.mode_indices[mode_index]])

        for mode_index1, handle1 in enumerate(mode_handles):
            mode1 = handle1.get()
            for mode_index2, handle2 in enumerate(mode_handles):
                mode2 = handle2.get()
                IP = self.my_POD.vec_space.inner_product(mode1, mode2)
                if self.mode_indices[mode_index1] != \
                    self.mode_indices[mode_index2]:
                    self.assertAlmostEqual(IP, 0.)
                else:
                    self.assertAlmostEqual(IP, 1.)
Example #5
0
 def test_derivs(self):
     """Test can take derivs"""
     dt = 0.1
     true_derivs = []
     num_vecs = len(self.basis_vec_handles)
     for i in range(num_vecs):
         true_derivs.append((self.A_on_basis_vec_handles[i].get() -
             self.basis_vec_handles[i].get()).squeeze()/dt)
     deriv_handles = [V.VecHandleArrayText(join(self.test_dir,
         'deriv_test%d'%i))
         for i in range(num_vecs)]
     LGP.compute_derivs_handles(self.basis_vec_handles,
         self.A_on_basis_vec_handles, deriv_handles, dt)
     derivs_loaded = [v.get() for v in deriv_handles]
     derivs_loaded = list(map(np.squeeze, derivs_loaded))
     list(map(np.testing.assert_allclose, derivs_loaded, true_derivs))
Example #6
0
    def test_lin_combine(self):
        num_vecs_list = [1, 15, 40]
        num_states = 20
        # Test cases where number of modes:
        #   less, equal, more than num_states
        #   less, equal, more than num_vecs
        #   less, equal, more than total_num_vecs_in_mem
        num_modes_list = [1, 8, 10, 20, 25, 45, \
            int(np.ceil(self.total_num_vecs_in_mem / 2.)),\
            self.total_num_vecs_in_mem, self.total_num_vecs_in_mem * 2]
        mode_path = join(self.test_dir, 'mode_%03d.txt')
        vec_path = join(self.test_dir, 'vec_%03d.txt')

        for num_vecs in num_vecs_list:
            for num_modes in num_modes_list:
                #generate data and then broadcast to all procs
                #print '----- new case ----- '
                #print 'num_vecs =',num_vecs
                #print 'num_states =',num_states
                #print 'num_modes =',num_modes
                #print 'max_vecs_per_node =',max_vecs_per_node
                #print 'index_from =',index_from
                vec_handles = [
                    V.VecHandleArrayText(vec_path % i) for i in range(num_vecs)
                ]
                vec_array, mode_indices, build_coeff_mat, true_modes = \
                    _parallel.call_and_bcast(self.generate_vecs_modes,
                    num_states, num_vecs, num_modes)

                if _parallel.is_rank_zero():
                    for vec_index, vec_handle in enumerate(vec_handles):
                        vec_handle.put(vec_array[:, vec_index])
                _parallel.barrier()
                mode_handles = [
                    V.VecHandleArrayText(mode_path % mode_num)
                    for mode_num in mode_indices
                ]

                # If there are more vecs than mat has rows
                build_coeff_mat_too_small = \
                    np.zeros((build_coeff_mat.shape[0]-1,
                        build_coeff_mat.shape[1]))
                self.assertRaises(ValueError, self.my_vec_ops.\
                    lin_combine, mode_handles,
                    vec_handles, build_coeff_mat_too_small, mode_indices)

                # Test the case that only one mode is desired,
                # in which case user might pass in an int
                if len(mode_indices) == 1:
                    mode_indices = mode_indices[0]
                    mode_handles = mode_handles[0]

                # Saves modes to files
                self.my_vec_ops.lin_combine(mode_handles, vec_handles,
                                            build_coeff_mat, mode_indices)

                # Change back to list so is iterable
                if not isinstance(mode_indices, list):
                    mode_indices = [mode_indices]

                _parallel.barrier()
                #print 'mode_indices',mode_indices
                for mode_index in mode_indices:
                    computed_mode = V.VecHandleArrayText(mode_path %
                                                         mode_index).get()
                    #print 'mode number',mode_num
                    #print 'true mode',true_modes[:,\
                    #    mode_num-index_from]
                    #print 'computed mode',computed_mode
                    np.testing.assert_allclose(computed_mode,
                                               true_modes[:, mode_index])

                _parallel.barrier()

        _parallel.barrier()
Example #7
0
    def setUp(self):
        if not os.access('.', os.W_OK):
            raise RuntimeError('Cannot write to current directory')

        self.test_dir = 'DELETE_ME_test_files_bpod'
        if not os.path.isdir(self.test_dir):
            _parallel.call_from_rank_zero(os.mkdir, self.test_dir)

        self.mode_nums = [2, 3, 0]
        self.num_direct_vecs = 10
        self.num_adjoint_vecs = 12
        self.num_inputs = 1
        self.num_outputs = 1
        self.num_states = 20

        #A = np.mat(np.random.random((self.num_states, self.num_states)))
        A = np.mat(
            _parallel.call_and_bcast(util.drss, self.num_states, 1, 1)[0])
        B = np.mat(
            _parallel.call_and_bcast(np.random.random,
                                     (self.num_states, self.num_inputs)))
        C = np.mat(
            _parallel.call_and_bcast(np.random.random,
                                     (self.num_outputs, self.num_states)))
        self.direct_vecs = [B]
        A_powers = np.identity(A.shape[0])
        for t in range(self.num_direct_vecs - 1):
            A_powers = A_powers.dot(A)
            self.direct_vecs.append(A_powers.dot(B))
        self.direct_vec_array = np.array(self.direct_vecs).squeeze().T

        A_adjoint = A.H
        C_adjoint = C.H
        A_adjoint_powers = np.identity(A_adjoint.shape[0])
        self.adjoint_vecs = [C_adjoint]
        for t in range(self.num_adjoint_vecs - 1):
            A_adjoint_powers = A_adjoint_powers.dot(A_adjoint)
            self.adjoint_vecs.append(A_adjoint_powers.dot(C_adjoint))
        self.adjoint_vec_array = np.array(self.adjoint_vecs).squeeze().T

        self.direct_vec_path = join(self.test_dir, 'direct_vec_%03d.txt')
        self.adjoint_vec_path = join(self.test_dir, 'adjoint_vec_%03d.txt')

        self.direct_vec_handles = [
            V.VecHandleArrayText(self.direct_vec_path % i)
            for i in range(self.num_direct_vecs)
        ]
        self.adjoint_vec_handles = [
            V.VecHandleArrayText(self.adjoint_vec_path % i)
            for i in range(self.num_adjoint_vecs)
        ]

        if _parallel.is_rank_zero():
            for i, handle in enumerate(self.direct_vec_handles):
                handle.put(self.direct_vecs[i])
            for i, handle in enumerate(self.adjoint_vec_handles):
                handle.put(self.adjoint_vecs[i])

        self.Hankel_mat_true = np.dot(self.adjoint_vec_array.T,
                                      self.direct_vec_array)

        self.L_sing_vecs_true, self.sing_vals_true, self.R_sing_vecs_true = \
            _parallel.call_and_bcast(util.svd, self.Hankel_mat_true, tol=1e-10)

        self.direct_mode_array = self.direct_vec_array * \
            np.mat(self.R_sing_vecs_true) * \
            np.mat(np.diag(self.sing_vals_true ** -0.5))
        self.adjoint_mode_array = self.adjoint_vec_array * \
            np.mat(self.L_sing_vecs_true) *\
            np.mat(np.diag(self.sing_vals_true ** -0.5))

        self.my_BPOD = BPODHandles(np.vdot, verbosity=0)
        _parallel.barrier()
    def generate_data_set(self, num_basis_vecs, num_adjoint_basis_vecs,
                          num_states, num_inputs, num_outputs):
        """Generates random data, saves, and computes true reduced A,B,C."""
        self.basis_vec_handles = [
            V.VecHandleArrayText(self.basis_vec_path % i)
            for i in range(self.num_basis_vecs)
        ]
        self.adjoint_basis_vec_handles = [
            V.VecHandleArrayText(self.adjoint_basis_vec_path % i)
            for i in range(self.num_adjoint_basis_vecs)
        ]
        self.A_on_basis_vec_handles = \
            [V.VecHandleArrayText(self.A_on_basis_vec_path%i)
                for i in range(self.num_basis_vecs)]
        self.B_on_standard_basis_handles = [
            V.VecHandleArrayText(self.B_on_basis_path % i)
            for i in range(self.num_inputs)
        ]
        self.C_on_basis_vec_handles = [
            V.VecHandleArrayText(self.C_on_basis_vec_path % i)
            for i in range(self.num_basis_vecs)
        ]

        self.basis_vec_array = _parallel.call_and_bcast(
            np.random.random, (num_states, num_basis_vecs))
        self.adjoint_basis_vec_array = _parallel.call_and_bcast(
            np.random.random, (num_states, num_adjoint_basis_vecs))
        self.A_array = _parallel.call_and_bcast(np.random.random,
                                                (num_states, num_states))
        self.B_array = _parallel.call_and_bcast(np.random.random,
                                                (num_states, num_inputs))
        self.C_array = _parallel.call_and_bcast(np.random.random,
                                                (num_outputs, num_states))

        self.basis_vecs = [
            self.basis_vec_array[:, i].squeeze() for i in range(num_basis_vecs)
        ]
        self.adjoint_basis_vecs = [
            self.adjoint_basis_vec_array[:, i].squeeze()
            for i in range(num_adjoint_basis_vecs)
        ]
        self.A_on_basis_vecs = [
            np.dot(self.A_array, basis_vec).squeeze()
            for basis_vec in self.basis_vecs
        ]
        self.B_on_basis = [
            self.B_array[:, i].squeeze() for i in range(self.num_inputs)
        ]
        self.C_on_basis_vecs = [
            np.array(np.dot(self.C_array, basis_vec).squeeze(), ndmin=1)
            for basis_vec in self.basis_vecs
        ]

        if _parallel.is_rank_zero():
            for handle, vec in zip(self.basis_vec_handles, self.basis_vecs):
                handle.put(vec)
            for handle, vec in zip(self.adjoint_basis_vec_handles,
                                   self.adjoint_basis_vecs):
                handle.put(vec)
            for handle, vec in zip(self.A_on_basis_vec_handles,
                                   self.A_on_basis_vecs):
                handle.put(vec)
            for handle, vec in zip(self.B_on_standard_basis_handles,
                                   self.B_on_basis):
                handle.put(vec)
            for handle, vec in zip(self.C_on_basis_vec_handles,
                                   self.C_on_basis_vecs):
                handle.put(vec)
        _parallel.barrier()

        self.A_true = np.dot(self.adjoint_basis_vec_array.T,
                             np.dot(self.A_array, self.basis_vec_array))
        self.B_true = np.dot(self.adjoint_basis_vec_array.T, self.B_array)
        self.C_true = np.dot(self.C_array, self.basis_vec_array)
        self.proj_mat = np.linalg.inv(
            np.dot(self.adjoint_basis_vec_array.T, self.basis_vec_array))
        self.A_true_nonorth = np.dot(self.proj_mat, self.A_true)
        self.B_true_nonorth = np.dot(self.proj_mat, self.B_true)