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_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_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_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_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_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_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) )
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) MSE = np.square(np.subtract(wcomp, pred_sol)).mean() RMSE = math.sqrt(MSE) Error_array[i, :] = RMSE #Error training_error = np.average(Error_array) print(f"RMS error is {training_error:.4f}")
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)) # We are now calculating the approximation error to see how close is our reduced solution to the full-order solution/simulation using the **k-fold Cross-Validation** strategy by passing the number of splits to the `ReducedOrderModel.kfold_cv_error(n_splits)` method, which operates as follows: # # 1. Split the dataset (parameters/snapshots) into $k$-number of groups/folds. # 2. Use $k-1$ groups to calculate the reduced space and leave one group for testing. # 3. Use the approximation/interpolation method to predict each snapshot in the testing group. # 4. Calculate the error for each snapshot in the testing group by taking the difference between the predicted and the original snapshot. # 5. Average the errors for predicting snapshots of the testing group/fold. # 6. Repeat this procedure using different groups for testing and the remaining $k-1$ groups to calculate the reduced space. # 7. In the end, we will have $k$-number errors for predicting each group/fold that we can average them to have one value for the error. # In[7]: errors = rom.kfold_cv_error(n_splits=5)
# 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. # The `fit()` function computes the reduced model, meaning that the original snapshots in the database are projected onto the POD space and the RBF interpolator is created. # In[8]: rom = ROM(db, pod, rbf) rom.fit() # ## Online phase # In the *online* phase we can query our model in order to predict the solution for a new parameter $\mu_\text{new}$ that is not in the training set. We just need to pass the new parameters as input of the `predict()` function. # In[9]: new_mu = [8, 1] pred_sol = rom.predict(new_mu) # We can so plot the predicted solution for a fixed parameter... # In[10]: plt.figure(figsize=(7, 5)) plt.triplot(triang, 'b-', lw=0.1) plt.tripcolor(triang, pred_sol) plt.colorbar() # ... or interactively touch the input parameters to visualize the corresponding (approximated) output. For a fancy result, we need a bit of IPython black magic ([https://ipywidgets.readthedocs.io/en/latest/]()). # In[11]: from ipywidgets import interact