u_rot[i] = np.dot(rotij, u[i]) grad_u_rot[i] = rotij @ (grad_u[i] @ rotij.T) grad_k_rot[i] = np.dot(rotij, grad_k[i]) grad_p_rot[i] = np.dot(rotij, grad_p[i]) sij_rot[i] = rotij @ (sij_full[i] @ rotij.T) rij_rot[i] = rotij @ (rij_full[i] @ rotij.T) bij_les_rot[i] = rotij @ (bij_les_full[i] @ rotij.T) # For Tij, go through every basis too for j in range(tb_t.shape[1]): tb_t_rot[i, j] = rotij @ (tb_t_full[i, j] @ rotij.T) # Collapse matrix form from 3 x 3 to 9 x 1 tb_t_rot = tb_t_rot.reshape((tb_t.shape[0], tb_t.shape[1], 9)) bij_les_rot = bij_les_rot.reshape((-1, 9)) # Contract rotated symmetric tensors from 9 full components back to 6 components tb_t_rot = contractSymmetricTensor(tb_t_rot) bij_les_rot = contractSymmetricTensor(bij_les_rot) # Change Tij back to shape (n_samples, n_outputs, n_bases) tb_rot = np.swapaxes(tb_t_rot, 1, 2) """ Calculate Rotated Feature Sets """ if fs == 'grad(TKE)': fs_data_rot, labels = getInvariantFeatureSet(sij_rot, rij_rot, grad_k_rot, k=k, eps=epsilon) elif fs == 'grad(p)': fs_data_rot, labels = getInvariantFeatureSet(sij_rot, rij_rot, grad_p=grad_p_rot, u=u_rot, grad_u=grad_u_rot) elif fs == 'grad(TKE)_grad(p)': fs_data_rot, labels = getInvariantFeatureSet(sij_rot, rij_rot, grad_k=grad_k_rot, grad_p=grad_p_rot, k=k, eps=epsilon,
if bij_novelty == 'excl': print( "Since bij_novelty is 'excl', removing NaN and making y_pred_test realizable..." ) nan_mask = np.isnan(y_pred_test).any(axis=1) ccx_test = ccx_test[~nan_mask] ccy_test = ccy_test[~nan_mask] ccz_test = ccz_test[~nan_mask] y_pred_test = y_pred_test[~nan_mask] for _ in range(2): y_pred_test = makeRealizable(y_pred_test) # Rotate field y_pred_test = expandSymmetricTensor(y_pred_test).reshape((-1, 3, 3)) y_pred_test = rotateData(y_pred_test, anglez=fieldrot) y_pred_test = contractSymmetricTensor(y_pred_test) t1 = t.time() print('\nFinished bij prediction in {:.4f} s'.format(t1 - t0)) """ Postprocess Machine Learning Predictions """ # Filter the result if requested. # This requires: # 1. Remove any component outside bound and set to NaN # 2. Interpolate to 2D slice mesh grid with nearest method # 3. Use 2D Gaussian filter to smooth the mesh grid while ignoring NaN, for every component # 4. Make whatever between bound and limits realizable if filter: cc2_test = ccz_test if slicedir == 'vertical' else ccy_test ccx_test_mesh, cc2_test_mesh, _, y_predtest_mesh = fieldSpatialSmoothing( y_pred_test,
# cc_test = np.vstack((cc1_test, cc2_test, np.zeros_like(cc1_test))).T # cc_test = rotateData(cc_test, anglez=fieldrot) # cc1_test = cc_test[:, 0] # cc2_test = cc_test[:, 1] # del cc_test x_test = list_data_test[1] x_test[x_test > 1e10] = 1e10 x_test[x_test < -1e10] = 1e10 y_test_unrot = list_data_test[2] tb_test = list_data_test[3] del list_data_test # Rotate field y_test = expandSymmetricTensor(y_test_unrot).reshape((-1, 3, 3)) y_test = rotateData(y_test, anglez=fieldrot) y_test = contractSymmetricTensor(y_test.reshape((-1, 9))) """ Predict """ t0 = t.time() score_test = regressor.score(x_test, y_test, tb=tb_test) y_pred_test_unrot = regressor.predict(x_test, tb=tb_test) # Rotate field y_pred_test = expandSymmetricTensor(y_pred_test_unrot).reshape( (-1, 3, 3)) y_pred_test = rotateData(y_pred_test, anglez=fieldrot) y_pred_test = contractSymmetricTensor(y_pred_test.reshape((-1, 9))) t1 = t.time() print('\nFinished bij prediction in {:.4f} s'.format(t1 - t0)) """ Posprocess Machine Learning Predictions
# Read slices and store them in case.slice_* dict case.readSlices(properties=property, slicenames=slicenames, slicenames_sub=slicenames_sub) # Initialize lists to store multiple slices coor and value data list_x, list_y, list_z = [], [], [] list_rgb, list_bij, list_rij = [], [], [] # Go through specified slices for i, slicename in enumerate(case.slicenames): """ Process Uninterpolated Anisotropy Tensor """ # Retrieve Rij of this slice rij = case.slices_val[slicename] rij = expandSymmetricTensor(rij).reshape((-1, 3, 3)) # Rotate Rij if requested, rotateData only accept full matrix form if rotate_data: rij = rotateData(rij, anglez=rot_z) rij = contractSymmetricTensor(rij) rij_tmp = rij.copy() # Get bij from Rij and its corresponding eigenval and eigenvec bij, eig_val, eig_vec = processReynoldsStress(rij_tmp, realization_iter=0, make_anisotropic=make_anisotropic, to_old_grid_shape=False) # bij was (n_samples, 3, 3) contract it bij = contractSymmetricTensor(bij) # Get barycentric map coor and normalized RGB xy_bary, rgb = getBarycentricMapData(eig_val, c_offset=c_offset, c_exp=c_exp) """ Interpolation """ # 1st coor is always x not matter vertical or horizontal slice # 2nd coor is y if horizontal slice otherwise z, take appropriate confinebox limit coor2_lim = confinebox[i][2:4] if case.slices_orient[slicename] == 'horizontal' else confinebox[i][4:]
bij_les = invariants['bij_LES'] uuprime2_les = case.readPickleData(time, 'uuPrime2_LES')[0] cc = case.readPickleData(time, filenames='CC') fs_data = case.readPickleData(time, filenames=('FS_' + fs)) # Also read LES TKE production G related fields g_les = case.readPickleData(time, 'G_LES') gradu_les = case.readPickleData(time, 'grad(U)_LES') k_les = case.readPickleData(time, 'k_LES') div_uuprime2_les = case.readPickleData(time, 'div(uuPrime2)_LES') list_data_test = case.readPickleData(time, 'list_data_test_seed' + str(seed)) cc_test = list_data_test[0] ccx_test, ccy_test, ccz_test = cc_test[:, 0], cc_test[:, 1], cc_test[:, 2] x_test, y_test, tb_test = list_data_test[1:4] # Contract symmetric tensors to 6 components in case of 9 components y_test = contractSymmetricTensor(y_test) tb_test = transposeTensorBasis(tb_test) tb_test = contractSymmetricTensor(tb_test) tb_test = transposeTensorBasis(tb_test) # Sort g_les, gradu_les, k_les that were based on cc to be based on cc_test by "interpolation" g_les = griddata(cc[:, :2], g_les, cc_test[:, :2], method=interp_method) # k_*_sorted are based on cc that is a sorted RANS grid coor array k_les_sorted, k_sorted = k_les.copy(), k.copy() k_les = griddata(cc[:, :2], k_les, cc_test[:, :2], method=interp_method) k = griddata(cc[:, :2], k, cc_test[:, :2], method=interp_method) for i in range(9): gradu_les[:, i] = griddata(cc[:, :2], gradu_les[:, i], cc_test[:, :2], method=interp_method)
if bij_novelty == 'excl': print( "Since bij_novelty is 'excl', removing NaN and making y_pred realizable..." ) nan_mask = np.isnan(y_pred).any(axis=1) ccx_test = ccx_test[~nan_mask] ccy_test = ccy_test[~nan_mask] ccz_test = ccz_test[~nan_mask] y_pred = y_pred[~nan_mask] for _ in range(2): y_pred = makeRealizable(y_pred) # Rotate field y_pred = expandSymmetricTensor(y_pred).reshape((-1, 3, 3)) y_pred = rotateData(y_pred, anglez=fieldrot) y_pred = contractSymmetricTensor(y_pred) t1 = t.time() print('\nFinished bij prediction in {:.4f} s'.format(t1 - t0)) """ Postprocess Machine Learning Predictions """ # Filter the result if requested. # This requires: # 1. Remove any component outside bound and set to NaN # 2. Interpolate to 2D slice mesh grid with nearest method # 3. Use 2D Gaussian filter to smooth the mesh grid while ignoring NaN, for every component # 4. Make whatever between bound and limits realizable if filter: cc2_test = ccz_test if slicedir == 'vertical' else ccy_test ccx_test_mesh, cc2_test_mesh, _, y_pred_mesh = fieldSpatialSmoothing( y_pred,
if 'TBDT' in estimator_name: y_pred_test, g_test = regressor.predict(x_test, tb_test, tree_struct) y_pred_train, g_train = regressor.predict(x_train, tb_train, tree_struct) else: # This is ensemble of all tree's predictions _, y_pred_test, g_test = regressor.predict(x_test, tb_test, tree_struct) _, y_pred_train, g_train = regressor.predict(x_train, tb_train, tree_struct) # Take median y_pred_test = np.median(y_pred_test, axis=2) y_pred_train = np.median(y_pred_train, axis=2) y_pred_train = np.swapaxes(y_pred_train, 0, 1) y_pred_test = np.swapaxes(y_pred_test, 0, 1) y_train = np.swapaxes(y_train, 0, 1) y_test = np.swapaxes(y_test, 0, 1) y_pred_train = contractSymmetricTensor(y_pred_train) y_pred_test = contractSymmetricTensor(y_pred_test) y_train = contractSymmetricTensor(y_train) y_test = contractSymmetricTensor(y_test) score_train = r2_score(y_train, y_pred_train) score_test = r2_score(y_test, y_pred_test) """ Postprocess Machine Learning Predictions """ t0 = t.time() _, eigval_test, _ = processReynoldsStress(y_test, make_anisotropic=False, realization_iter=0) _, eigval_train, _ = processReynoldsStress(y_train, make_anisotropic=False, realization_iter=0) y_pred_test3, eigval_pred_test, _ = processReynoldsStress(y_pred_test, make_anisotropic=False, realization_iter=realize_iter) y_pred_train3, eigval_pred_train, _ = processReynoldsStress(y_pred_train, make_anisotropic=False, realization_iter=realize_iter)