Ejemplo n.º 1
0
    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)),
        )
Ejemplo n.º 2
0
 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)
Ejemplo n.º 3
0
 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)
Ejemplo n.º 4
0
 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
Ejemplo n.º 5
0
 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
Ejemplo n.º 6
0
 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)
Ejemplo n.º 7
0
 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)
Ejemplo n.º 8
0
 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)
Ejemplo n.º 9
0
 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)
Ejemplo n.º 10
0
 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)
Ejemplo n.º 11
0
 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
Ejemplo n.º 12
0
 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)
Ejemplo n.º 13
0
 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)
Ejemplo n.º 14
0
    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
Ejemplo n.º 15
0
 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)
Ejemplo n.º 16
0
 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)
Ejemplo n.º 17
0
 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)
Ejemplo n.º 18
0
 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)
Ejemplo n.º 19
0
    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
Ejemplo n.º 20
0
 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)
     )
Ejemplo n.º 21
0
                       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.
Ejemplo n.º 22
0
 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)))
Ejemplo n.º 23
0
    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)
Ejemplo n.º 24
0
 def test_constructor_arg_wrong(self):
     with self.assertRaises(RuntimeError):
         Database(np.random.uniform(size=(9, 3)),
                  np.random.uniform(size=(10, 8)))
Ejemplo n.º 25
0
 def test_constructor(self):
     pod = POD()
     rbf = RBF()
     db = Database(param, snapshots.T)
     rom = ROM(db, pod, rbf)
Ejemplo n.º 26
0
 def test_constructor_error(self):
     with self.assertRaises(RuntimeError):
         Database(np.eye(5))
Ejemplo n.º 27
0
                       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))
Ejemplo n.º 28
0
 def test_constructor_empty(self):
     a = Database()