def test_compute_proj_coeffs(self): rtol = 1e-10 atol = 1e-12 # Compute POD using modred. (The properties defining a projection onto # POD modes require manipulations involving the correct decomposition # and modes, so we cannot isolate the mode computation from those # computations.) POD = pod.PODHandles(inner_product=np.vdot, verbosity=0) POD.compute_decomp(self.vec_handles) mode_idxs = range(POD.eigvals.size) mode_handles = [VecHandlePickle(self.mode_path % i) for i in mode_idxs] POD.compute_modes(mode_idxs, mode_handles, vec_handles=self.vec_handles) # Compute true projection coefficients by computing the inner products # between modes and snapshots. proj_coeffs_true = POD.vec_space.compute_inner_product_array( mode_handles, self.vec_handles) # Compute projection coefficients using POD object, which avoids # actually manipulating handles and computing their inner products, # instead using elements of the decomposition for a more efficient # computations. proj_coeffs = POD.compute_proj_coeffs() # Test values np.testing.assert_allclose(proj_coeffs, proj_coeffs_true, rtol=rtol, atol=atol)
def test_compute_decomp(self): """Test computation of the correlation array and SVD arrays.""" rtol = 1e-10 atol = 1e-12 # Compute POD using modred POD = pod.PODHandles(inner_product=np.vdot, verbosity=0) eigvals, eigvecs = POD.compute_decomp(self.vec_handles) # Test correlation array values by simply recomputing them. Here simply # take all inner products, rather than assuming a symmetric inner # product. np.testing.assert_allclose(POD.correlation_array, POD.vec_space.compute_inner_product_array( self.vec_handles, self.vec_handles), rtol=rtol, atol=atol) # Check POD eigenvectors and eigenvalues np.testing.assert_allclose(self.vecs_array.conj().T.dot( self.vecs_array.dot(eigvecs)), eigvecs.dot(np.diag(eigvals)), rtol=rtol, atol=atol) # Check that returned values match internal values np.testing.assert_equal(eigvals, POD.eigvals) np.testing.assert_equal(eigvecs, POD.eigvecs)
def test_compute_modes(self): rtol = 1e-10 atol = 1e-12 # Compute POD using modred. (The properties defining a POD mode require # manipulations involving the correct decomposition, so we cannot # isolate the mode computation from the decomposition step.) POD = pod.PODHandles(np.vdot, verbosity=0) POD.compute_decomp(self.vec_handles) # Select a subset of modes to compute. Compute at least half # the modes, and up to all of them. Make sure to use unique # values. (This may reduce the number of modes computed.) num_modes = parallel.call_and_bcast( np.random.randint, POD.eigvals.size // 2, POD.eigvals.size + 1) mode_idxs = np.unique(parallel.call_and_bcast( np.random.randint, 0, POD.eigvals.size, num_modes)) # Create handles for the modes mode_handles = [VecHandlePickle(self.mode_path % i) for i in mode_idxs] # Compute modes POD.compute_modes(mode_idxs, mode_handles, vec_handles=self.vec_handles) # Test modes np.testing.assert_allclose( POD.vec_space.compute_inner_product_array( mode_handles, self.vec_handles).dot( POD.vec_space.compute_inner_product_array( self.vec_handles, mode_handles)), np.diag(POD.eigvals[mode_idxs]), rtol=rtol, atol=atol)
def test_init(self): """Test arguments passed to the constructor are assigned properly""" # Get default data member values # Set verbosity to false, to avoid printing warnings during tests def my_load(): pass def my_save(): pass def my_IP(): pass data_members_default = { 'put_array': util.save_array_text, 'get_array':util.load_array_text, 'verbosity': 0, 'eigvecs': None, 'eigvals': None, 'correlation_array': None, 'vec_handles': None, 'vecs': None, 'vec_space': VectorSpaceHandles(inner_product=my_IP, verbosity=0)} for k,v in util.get_data_members( pod.PODHandles(my_IP, verbosity=0)).items(): self.assertEqual(v, data_members_default[k]) my_POD = pod.PODHandles(my_IP, verbosity=0) data_members_modified = copy.deepcopy(data_members_default) data_members_modified['vec_space'] = VectorSpaceHandles( inner_product=my_IP, verbosity=0) for k,v in util.get_data_members(my_POD).items(): self.assertEqual(v, data_members_modified[k]) my_POD = pod.PODHandles(my_IP, get_array=my_load, verbosity=0) data_members_modified = copy.deepcopy(data_members_default) data_members_modified['get_array'] = my_load for k,v in util.get_data_members(my_POD).items(): self.assertEqual(v, data_members_modified[k]) my_POD = pod.PODHandles(my_IP, put_array=my_save, verbosity=0) data_members_modified = copy.deepcopy(data_members_default) data_members_modified['put_array'] = my_save for k,v in util.get_data_members(my_POD).items(): self.assertEqual(v, data_members_modified[k]) max_vecs_per_node = 500 my_POD = pod.PODHandles( my_IP, max_vecs_per_node=max_vecs_per_node, verbosity=0) data_members_modified = copy.deepcopy(data_members_default) data_members_modified['vec_space'].max_vecs_per_node = max_vecs_per_node data_members_modified['vec_space'].max_vecs_per_proc = ( max_vecs_per_node * parallel.get_num_nodes() / parallel.get_num_procs()) for k,v in util.get_data_members(my_POD).items(): self.assertEqual(v, data_members_modified[k])
def test_puts_gets(self): # Generate some random data correlation_array_true = parallel.call_and_bcast( np.random.random, ((self.num_vecs, self.num_vecs))) eigvals_true = parallel.call_and_bcast( np.random.random, self.num_vecs) eigvecs_true = parallel.call_and_bcast( np.random.random, ((self.num_states, self.num_vecs))) proj_coeffs_true = parallel.call_and_bcast( np.random.random, ((self.num_vecs, self.num_vecs))) # Create a POD object and store the data in it POD_save = pod.PODHandles(None, verbosity=0) POD_save.correlation_array = correlation_array_true POD_save.eigvals = eigvals_true POD_save.eigvecs = eigvecs_true POD_save.proj_coeffs = proj_coeffs_true # Write the data to disk eigvecs_path = join(self.test_dir, 'eigvecs.txt') eigvals_path = join(self.test_dir, 'eigvals.txt') correlation_array_path = join(self.test_dir, 'correlation.txt') proj_coeffs_path = join(self.test_dir, 'proj_coeffs.txt') POD_save.put_decomp(eigvals_path, eigvecs_path) POD_save.put_correlation_array(correlation_array_path) POD_save.put_proj_coeffs(proj_coeffs_path) parallel.barrier() # Create a new POD object and use it to load the data POD_load = pod.PODHandles(None, verbosity=0) POD_load.get_decomp(eigvals_path, eigvecs_path) POD_load.get_correlation_array(correlation_array_path) POD_load.get_proj_coeffs(proj_coeffs_path) # Check that the loaded data is correct np.testing.assert_equal(POD_load.eigvals, eigvals_true) np.testing.assert_equal(POD_load.eigvecs, eigvecs_true) np.testing.assert_equal( POD_load.correlation_array, correlation_array_true) np.testing.assert_equal(POD_load.proj_coeffs, proj_coeffs_true)