def test_constructor_arg(self): Database(np.random.uniform(size=(10, 3)), np.random.uniform(size=(10, 8))) Database( np.random.uniform(size=(10, 3)), np.random.uniform(size=(10, 8)), np.random.uniform(size=(10, 8)), )
def test_predict_01(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf).fit() pred_sol = rom.predict([-0.293344, -0.23120537]) np.testing.assert_allclose(pred_sol, pred_sol_tst, rtol=1e-4, atol=1e-5)
def test_test_error(self): pod = POD(method='svd', rank=-1) rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf).fit() error = rom.test_error(db) np.testing.assert_almost_equal(error, 0, decimal=6)
def test_getitem_space(self): org = Database(np.random.uniform(size=(10, 3)), np.random.uniform(size=(10, 8)), space=np.random.uniform(size=(10, 8))) new = org[2::2] assert new.parameters.shape[0] == new.snapshots.shape[ 0] == new.space.shape[0] == 4
def test_predict_04(self): pod = POD(method='svd', rank=3) gpr = GPR() db = Database(param, snapshots.T) rom = ROM(db, pod, gpr).fit() pred_sol = rom.predict(db.parameters) assert pred_sol.shape == db.snapshots.shape
def test_optimal_mu(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf).fit() opt_mu = rom.optimal_mu() np.testing.assert_allclose(opt_mu, [[-0.17687147, -0.21820951]], rtol=1e-4)
def test_loo_error_singular_values(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf).fit() valid_svalues = rom.reduction.singular_values rom.loo_error() np.testing.assert_allclose(valid_svalues, rom.reduction.singular_values)
def test_save(self): fname = 'ezyrb.tmp' pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) rom.fit() rom.save(fname)
def test_predict_02(self): np.random.seed(117) pod = POD(method='svd', rank=4) gpr = GPR() db = Database(param, snapshots.T) rom = ROM(db, pod, gpr).fit() pred_sol = rom.predict([-.45, -.45]) np.testing.assert_allclose(pred_sol, pred_sol_gpr, rtol=1e-4, atol=1e-5)
def test_kfold_cv_error_02(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) err = rom.kfold_cv_error(n_splits=3) np.testing.assert_allclose( err, np.array([0.468199, 0.271776, 0.919509]), rtol=1e-4)
def test_predict_03(self): pod = POD(method='svd', rank=3) gpr = GPR() db = Database(param, snapshots.T) #rom = ROM(db, pod, RBF()).fit() #pred_sol = rom.predict([-.45, -.45]) #print(pred_sol) rom = ROM(db, pod, gpr).fit() pred_sol = rom.predict(db.parameters[2]) assert pred_sol.shape == db.snapshots[0].shape
def test_loo_error_01(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) err = rom.loo_error() np.testing.assert_allclose( err, np.array([0.540029, 1.211744, 0.271776, 0.919509]), rtol=1e-4)
def test_kfold_cv_error_01(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) err = rom.kfold_cv_error(n_splits=4) np.testing.assert_allclose( err, np.array([0.54002856, 1.21174449, 0.27177608, 0.91950896]), rtol=1e-4)
def test_with_db_predict(self): reg = Linear() pod = POD() db = Database(np.array([1, 2, 3])[:,None], np.array([1, 5, 3])[:,None]) rom = ReducedOrderModel(db, pod, reg) rom.fit() assert rom.predict([1]) == 1 assert rom.predict([2]) == 5 assert rom.predict([3]) == 3
def test_kfold_cv_error_03(self): pod = POD() gpr = GPR() db = Database(param, snapshots.T) rom = ROM(db, pod, gpr) err = rom.kfold_cv_error(n_splits=3, normalizer=False) np.testing.assert_allclose( err, np.array([0.664149, 1.355502, 0.379874]), rtol=1e-3)
def test_loo_error(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) err = rom.loo_error() np.testing.assert_allclose( err, np.array([421.299091, 344.571787, 48.711501, 300.490491]), rtol=1e-4)
def test_loo_error_02(self): pod = POD() gpr = GPR() db = Database(param, snapshots.T) rom = ROM(db, pod, gpr) err = rom.loo_error(normalizer=False) np.testing.assert_allclose( err[0], np.array(0.639247), rtol=1e-3)
def test_predict_scaler_01(self): from sklearn.preprocessing import StandardScaler scaler = StandardScaler() pod = POD() rbf = RBF() db = Database(param, snapshots.T, scaler_snapshots=scaler) rom = ROM(db, pod, rbf).fit() pred_sol = rom.predict(db.parameters[0]) np.testing.assert_allclose(pred_sol, db._snapshots[0], rtol=1e-4, atol=1e-5) pred_sol = rom.predict(db.parameters[0:2]) np.testing.assert_allclose(pred_sol, db._snapshots[0:2], rtol=1e-4, atol=1e-5)
def test_with_db_predict(self): reg = RadiusNeighborsRegressor(radius=0.5) pod = POD() db = Database( np.array([1, 2, 3])[:, None], np.array([1, 5, 3])[:, None]) rom = ReducedOrderModel(db, pod, reg) rom.fit() assert rom.predict([1]) == 1 assert rom.predict([2]) == 5 assert rom.predict([3]) == 3
def test_load2(self): fname = 'ezyrb2.tmp' pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf) rom.fit() rom.save(fname, save_db=False) new_rom = ROM.load(fname) new_param = [-0.293344, -0.23120537] np.testing.assert_array_almost_equal( rom.predict(new_param), new_rom.predict(new_param) )
ncols=4, figsize=(16, 6), sharey=True, sharex=True) ax = ax.flatten() for i in range(8): ax[i].triplot(triang, 'b-', lw=0.1) cm = ax[i].tripcolor(triang, snapshots[i]) fig.colorbar(cm, ax=ax[i]) ax[i].set_title('($\mu_0={:5.2f}, \mu_1={:5.2f})$'.format(*param[i])) # First of all, we create a `Database` object from the parameters and the snapshots. # In[5]: db = Database(param, snapshots) # Then we need a reduction object. In this case we use the proper orthogonal decomposition so we create a `POD` object. We use here all the default parameters, but for the complete list of available arguments we refer to original documentation of [POD](https://mathlab.github.io/EZyRB/pod.html) class. # In[6]: pod = POD('svd') # Then we instantiate the `RBF` class for interpolating the solution manifold. Also in this case, [RBF](https://mathlab.github.io/EZyRB/rbf.html) documentation is the perfect starting point to explore such class. # In[7]: rbf = RBF() # Few lines of code and our reduced model is created! # To complete everything, we create the `ReducedOrderModel` (aliased to `ROM` in this tutorial) object by passing the already created objects. For clarity, we puntualize that we need to pass the **instances** and not the classes. Simply changing such line (with different objects) allows to test different frameworks in a very modular way.
def test_constructor_arg_wrong_space_width(self): with self.assertRaises(RuntimeError): Database(np.random.uniform(size=(10, 3)), np.random.uniform(size=(10, 9)), space=np.random.uniform(size=(10, 8)))
A = (kappa * A1) - (u * A2) return A, b, TleftBC #POD offline snapshots = np.load('Snapshots/Snapshot75b.npy') print(f'Total number of columns in snapshot matrix = {len(snapshots[0])}') limit = int(0.8 * len(snapshots[0])) snapshots = snapshots[:, 0:limit] snapshots = np.transpose(snapshots) param = np.load('Param/Param75b.npy') param_snap = param[:, 0:limit] param_snap = np.transpose(param_snap) db = Database(param_snap, snapshots) pod = POD('svd') rbf = RBF() rom = ROM(db, pod, rbf) rom.fit() #POD Online param_train = np.transpose(param[:, limit:len(param[0])]) Error_array = np.zeros((len(param[0]) - limit, 1)) train_limit = len(param[0]) - limit for i in range(0, train_limit): param_i = param_train[i, :] [A, b, TleftBC] = buildadvdefop(param_i[0], param_i[1], param_i[2]) wcomp = np.linalg.solve(A, b).T.squeeze() pred_sol = rom.predict(param_i)
def test_constructor_arg_wrong(self): with self.assertRaises(RuntimeError): Database(np.random.uniform(size=(9, 3)), np.random.uniform(size=(10, 8)))
def test_constructor(self): pod = POD() rbf = RBF() db = Database(param, snapshots.T) rom = ROM(db, pod, rbf)
def test_constructor_error(self): with self.assertRaises(RuntimeError): Database(np.eye(5))
figsize=(16, 8), sharey=True, sharex=True) ax = ax.flatten() for i in range(9): ax[i].tricontourf(data.triang, data.snapshots['vx'][i], levels=16) ax[i].set_title('Original snapshot at inlet velocity = {}'.format( *data.params[i].round(2))) # In this step, we perform the model order reduction to obtain a reduced space from the full order space. We refer to [Tutorial 1](https://github.com/mathLab/EZyRB/blob/master/tutorials/tutorial-1.ipynb) for the description of the basic workflow, here we just quickly describe the steps implemented in the next cell. # # We start by passing the matrices of the parameters and snapshots to the `Database()` class. It must be said that at this time we create the ROM for the `vx` field. We also instantiate the `POD` and `RBF` object to have a benchmark ROM. # In[5]: db = Database(data.params, data.snapshots['vx']) rom = ROM(db, POD(), RBF()) rom.fit() # Three lines for a data-driven reduced order model, not bad! # # Just to have a visual check that everything is going well, we plot the approximation for new parameters in the range $[1, 80]$. # In[6]: new_params = np.random.uniform(size=(2)) * 79. + 1. fig, ax = plt.subplots(nrows=1, ncols=2, figsize=(16, 3)) for i, param in enumerate(new_params): ax[i].tricontourf(data.triang, rom.predict([param])) ax[i].set_title('Predicted snapshots at inlet velocity = {}'.format(param))
def test_constructor_empty(self): a = Database()