def test_inputs(self): training = np.loadtxt("tests/bessel/bessel_training.txt") with self.assertRaises(ValueError): Nsamples = 10 x = np.linspace(0, 1, 101) sliced_training = training[:, :Nsamples] arby.ReducedOrderModel(sliced_training, x) with self.assertRaises(ValueError): Nsamples = training.shape[1] wrong_Nsamples = 11 x = np.linspace(0, 1, wrong_Nsamples) arby.ReducedOrderModel(training, x) with self.assertRaises(ValueError): wrong_Ntrain = 11 x = np.linspace(0, 1, 101) nu = np.linspace(0, 10, wrong_Ntrain) arby.ReducedOrderModel(training, x, nu) with self.assertRaises(ValueError): x = np.linspace(0, 1, 101) bessel = arby.ReducedOrderModel(training, x) bessel.Nsamples += 1 bessel.basis
def test_projectors(self): """Test that projectors works as true projectors.""" npoints = 101 nu_train = np.linspace(1, 10, num=npoints) x = np.linspace(0, 1, 1001) # build traning space training = np.array([BesselJ(nn, x) for nn in nu_train]) # build reduced basis bessel = arby.ReducedOrderModel( training_space=training, physical_interval=x, parameter_interval=nu_train, greedy_tol=1e-12, ) # compute a random index to test Proj_operator^2 = Proj_operator random_index = np.random.randint(0, npoints) proj_fun = bessel.project(training[random_index], bessel.basis) proj2_fun = bessel.project(proj_fun, bessel.basis) self.assertTrue(np.allclose(proj_fun, proj2_fun, rtol=1e-5, atol=1e-8)) # compute a random index to test Interpolant^2 = Interpolant random_index = np.random.randint(0, npoints) # build interpolant matrix bessel.build_eim() interp_fun = bessel.interpolate(training[random_index]) interp2_fun = bessel.interpolate(interp_fun) self.assertTrue( np.allclose(interp_fun, interp2_fun, rtol=1e-5, atol=1e-8))
def test_regression_EIM(self): "Test that EIM matches ROMpy's for the same training data" rompy_eim_nodes = np.array([0, 100, 2, 36, 9, 72, 1, 20, 89, 4]) # Compute eim nodes for Bessel functions eim_bessel = arby.ReducedOrderModel(basis=self.basis) eim_bessel.build_eim() # compare self.assertTrue((rompy_eim_nodes == eim_bessel.eim_nodes_).all())
def test_greedy_already_selected(self): """Test greedy stopping condition.""" npoints = 101 # Sample parameter nu and physical variable x nu = np.linspace(0, 10, num=npoints) x = np.linspace(0, 1, 101) # build traning space training = np.array([BesselJ(nn, x) for nn in nu]) # build reduced basis with exagerated greedy_tol bessel = arby.ReducedOrderModel(training, x, greedy_tol=1e-20) bessel.basis
def test_regression_reduced_basis(self): "Test that the reduced basis matches ROMpy's for the same " "training data" nbasis, npoints = self.basis.shape physical_inteval = np.linspace(0, 1, npoints) # build reduced basis rb_bessel = arby.ReducedOrderModel( self.training, physical_inteval, greedy_tol=1e-14 ) # compare self.assertTrue( np.allclose(rb_bessel.basis, self.basis, rtol=1e-5, atol=1e-8) )
def test_basis_shape(self): """Test correct shape for reduced basis.""" npoints = 101 # Sample parameter nu and physical variable x nu = np.linspace(0, 10, num=npoints) x = np.linspace(0, 1, 101) # build traning space training = np.array([BesselJ(nn, x) for nn in nu]) # build reduced basis bessel = arby.ReducedOrderModel(training, x, greedy_tol=1e-12) # Assert that basis has correct shape self.assertEqual(bessel.basis.ndim, 2) self.assertEqual(bessel.basis.shape[1], npoints)
def test_surrogate(self): """Test surrogate accuracy.""" npoints = 101 nu_train = np.linspace(1, 10, num=npoints) nu_validation = np.linspace(1, 10, num=1001) x = np.linspace(0, 1, 1001) # build traning space training = np.array([BesselJ(nn, x) for nn in nu_train]) # build reduced basis bessel = arby.ReducedOrderModel( training_space=training, physical_interval=x, parameter_interval=nu_train, greedy_tol=1e-15, ) bessel_test = [BesselJ(nn, x) for nn in nu_validation] bessel_surrogate = [bessel.surrogate(nn) for nn in nu_validation] self.assertTrue( np.allclose(bessel_test, bessel_surrogate, rtol=1e-5, atol=1e-5))
def test_projection_error(self): """Test auto-consistency for projection error function.""" npoints = 101 nu_train = np.linspace(1, 10, num=npoints) x = np.linspace(0, 1, 1001) # build traning space training = np.array([BesselJ(nn, x) for nn in nu_train]) # build reduced basis bessel = arby.ReducedOrderModel( training_space=training, physical_interval=x, parameter_interval=nu_train, greedy_tol=1e-12, ) # Check that projection errors of basis elements onto the basis is # zero computed_errors = [ bessel.projection_error(basis_element, bessel.basis) for _, basis_element in enumerate(bessel.basis) ] expected_errors = [0.0] * bessel.Nbasis_ self.assertTrue( np.allclose(computed_errors, expected_errors, rtol=1e-5, atol=1e-8))