def getArrayDescription(array, context, dataset): if not array: return None if array.GetNumberOfComponents() == 9: adapter = dsa.WrapDataObject(dataset) name = array.GetName() npArray = adapter.GetPointData().GetArray(name) eigenvalues = algorithms.eigenvalue(npArray) merge = np.column_stack((npArray, eigenvalues[:, np.newaxis, :])) n = npArray.shape[0] array.SetNumberOfComponents(12) array.SetNumberOfTuples(n) for i in range(n): array.SetTypedTuple(i, merge[i].ravel()) pMd5 = digest(array) context.cacheDataArray(pMd5, { 'array': array, 'mTime': array.GetMTime(), 'ts': time.time() }) root = {} root['hash'] = pMd5 root['vtkClass'] = 'vtkDataArray' root['name'] = array.GetName() root['dataType'] = getJSArrayType(array) root['numberOfComponents'] = array.GetNumberOfComponents() root['size'] = array.GetNumberOfComponents() * array.GetNumberOfTuples() root['ranges'] = [] if root['numberOfComponents'] > 1: for i in range(root['numberOfComponents']): root['ranges'].append(getRangeInfo(array, i)) root['ranges'].append(getRangeInfo(array, -1)) else: root['ranges'].append(getRangeInfo(array, 0)) return root
# Various numerical ops implemented in VTK g = algs.gradient(elev) assert algs.all(g[0] == (1, 0, 0)) v = algs.make_vector(elev, g[:,0], elev) assert algs.all(algs.gradient(v) == [[1, 0, 1], [0, 0, 0], [0, 0, 0]]) v = algs.make_vector(elev, g[:,0], elev2) assert algs.all(algs.curl(v) == [1, 0, 0]) v = algs.make_vector(elev, elev2, 2*elev3) g = algs.gradient(v) assert g.DataSet is v.DataSet assert algs.all(algs.det(g) == 2) assert algs.all(algs.eigenvalue(g) == [2, 1, 1]) assert algs.all(randomVec[:,0] == randomVec[:,0]) int_array1 = numpy.array([1, 0, 1], dtype=numpy.int) int_array2 = numpy.array([0, 1, 0], dtype=numpy.int) assert algs.all(algs.bitwise_or(int_array1, int_array2) == 1) assert algs.all(algs.bitwise_or(int_array1, dsa.NoneArray) == int_array1) assert algs.all(algs.bitwise_or(dsa.NoneArray, int_array1) == int_array1) comp_array1 = dsa.VTKCompositeDataArray([int_array1, int_array2]) comp_array2 = dsa.VTKCompositeDataArray([int_array2, int_array1]) comp_array3 = dsa.VTKCompositeDataArray([int_array2, dsa.NoneArray]) assert algs.all(algs.bitwise_or(comp_array1, comp_array2) == 1) assert algs.all(algs.bitwise_or(comp_array1, dsa.NoneArray) == comp_array1) assert algs.all(algs.bitwise_or(dsa.NoneArray, comp_array1) == comp_array1)
# Various numerical ops implemented in VTK g = algs.gradient(elev) assert algs.all(g[0] == (1, 0, 0)) v = algs.make_vector(elev, g[:,0], elev) assert algs.all(algs.gradient(v) == [[1, 0, 0], [0, 0, 0], [1, 0, 0]]) v = algs.make_vector(elev, g[:,0], elev2) assert algs.all(algs.curl(v) == [1, 0, 0]) v = algs.make_vector(elev, elev2, 2*elev3) g = algs.gradient(v) assert g.DataSet is v.DataSet assert algs.all(algs.det(g) == 2) assert algs.all(algs.eigenvalue(g) == [2, 1, 1]) assert algs.all(randomVec[:,0] == randomVec[:,0]) ssource = vtk.vtkSphereSource() ssource.Update() output = ssource.GetOutput() fd = vtk.vtkFloatArray() fd.SetNumberOfTuples(11) fd.FillComponent(0, 5) fd.SetName("field array") output.GetFieldData().AddArray(fd)
def rans_les_uiuj_err(rans_vtk, les_vtk, thrsh, Ls=1, Us=1): small = np.cbrt(np.finfo(float).tiny) nnode = les_vtk.number_of_points delij = np.zeros([nnode, 3, 3]) for i in range(0, 3): delij[:, i, i] = 1.0 ############### # Get RANS uiuj ############### rans_dsa = dsa.WrapDataObject(rans_vtk) U = rans_dsa.PointData[ 'U'] # NOTE - Getting variables from dsa obj not vtk obj as want to use algs etc later # Velocity gradient tensor and its transpose # J[:,i-1,j-1] is dUidxj # Jt[:,i-1,j-1] is dUjdxi Jt = algs.gradient(U) # Jt is this one as algs uses j,i ordering J = algs.apply_dfunc(np.transpose, Jt, (0, 2, 1)) # Strain and vorticity tensors Sij = 0.5 * (J + Jt) nu_t = rans_dsa.PointData['mu_t'] / rans_dsa.PointData['ro'] tke = rans_dsa.PointData['k'] uiuj_rans = (2.0 / 3.0) * tke * delij - 2.0 * nu_t * Sij # Find Caniso from linear EVM aij = copy.deepcopy(Sij) * 0.0 for i in range(0, 3): for j in range(0, 3): aij[:, i, j] = uiuj_rans[:, i, j] / (2.0 * tke + small) - delij[:, i, j] / 3.0 # Get eigenvalues of aij eig = algs.eigenvalue(aij) eig1 = eig[:, 0] eig2 = eig[:, 1] eig3 = eig[:, 2] # Get coords on barycentric triangle from eigenvalues xc = [1.0, 0.0, 0.5] #x,y coords of corner of triangle yc = [0.0, 0.0, np.cos(np.pi / 6.0)] C1c = eig1 - eig2 C2c = 2 * (eig2 - eig3) C3c = 3 * eig3 + 1 Caniso = 1.0 - C3c rans_vtk.point_arrays['Caniso'] = Caniso ############### # Get LES uiuj ############### # Wrap vista object in dsa wrapper les_dsa = dsa.WrapDataObject(les_vtk) # Copy Reynolds stresses to tensor # uiuj_les = np.zeros([nnode,3,3]) uiuj_les = copy.deepcopy(uiuj_rans) * 0.0 uiuj_les[:, 0, 0] = les_dsa.PointData['uu'] uiuj_les[:, 1, 1] = les_dsa.PointData['vv'] uiuj_les[:, 2, 2] = les_dsa.PointData['ww'] uiuj_les[:, 0, 1] = les_dsa.PointData['uv'] uiuj_les[:, 0, 2] = les_dsa.PointData['uw'] uiuj_les[:, 1, 2] = les_dsa.PointData['vw'] uiuj_les[:, 1, 0] = uiuj_les[:, 0, 1] uiuj_les[:, 2, 0] = uiuj_les[:, 0, 2] uiuj_les[:, 2, 1] = uiuj_les[:, 1, 2] tke_les = 0.5 * (uiuj_les[:, 0, 0] + uiuj_les[:, 1, 1] + uiuj_les[:, 2, 2]) # Calc divergence of DNS and RANS uiuj div_RANS = np.zeros([nnode, 3]) div_LES = np.zeros([nnode, 3]) for i in range(3): for j in range(3): div_RANS[:, i] += algs.gradient(uiuj_rans[:, i, j])[:, i] div_LES[:, i] += algs.gradient(uiuj_les[:, i, j])[:, i] div_err = 1.0 * div_LES - rans_dsa.PointData['ro'] * div_RANS err = np.sqrt(div_err[:, 0]**2 + div_err[:, 1]**2 + div_err[:, 2]**2) # err = err/(tke_les+small) err = err * Ls / Us**2 # # Calc Frobenius norm distance between RANS and LES # err = np.zeros(nnode) # for i in range(3): # for j in range(3): # err[:] += ( (uiuj_rans[:,i,j]-uiuj_les[:,i,j]) )**2. # err = np.sqrt(err) # err = err/(tke_les+small) index = algs.where(err > thrsh) err_bool = np.zeros(nnode, dtype=int) err_bool[index] = 1 y_raw = les_vtk.point_arrays['raw'] y_targ = les_vtk.point_arrays['target'] les_vtk.point_arrays['raw'] = np.append(y_raw, err.reshape(-1, 1), axis=1) les_vtk.point_arrays['target'] = np.append(y_targ, err_bool.reshape(-1, 1), axis=1)
def make_targets(les_vtk, y_type, Ls=1, Us=1, ros=1): from tqdm import tqdm small = np.cbrt(np.finfo(float).tiny) Ps = 0.5 * ros * Us**2 les_nnode = les_vtk.number_of_points delij = np.zeros([les_nnode, 3, 3]) for i in range(0, 3): delij[:, i, i] = 1.0 # Wrap vista object in dsa wrapper les_dsa = dsa.WrapDataObject(les_vtk) if (y_type == 'classification'): ntarg = 5 y_targ = np.zeros([les_nnode, ntarg], dtype=int) print('Classifier targets:') elif (y_type == 'regression'): ntarg = 2 y_targ = np.zeros([les_nnode, ntarg], dtype=float) print('regressor targets:') y_raw = np.zeros([les_nnode, ntarg]) target_labels = np.empty(ntarg, dtype='object') targ = 0 # Copy Reynolds stresses to tensor uiuj = np.zeros([les_nnode, 3, 3]) uiuj[:, 0, 0] = les_dsa.PointData['uu'] uiuj[:, 1, 1] = les_dsa.PointData['vv'] uiuj[:, 2, 2] = les_dsa.PointData['ww'] uiuj[:, 0, 1] = les_dsa.PointData['uv'] uiuj[:, 0, 2] = les_dsa.PointData['uw'] uiuj[:, 1, 2] = les_dsa.PointData['vw'] uiuj[:, 1, 0] = uiuj[:, 0, 1] uiuj[:, 2, 0] = uiuj[:, 0, 2] uiuj[:, 2, 1] = uiuj[:, 1, 2] # resolved TKE tke = 0.5 * (uiuj[:, 0, 0] + uiuj[:, 1, 1] + uiuj[:, 2, 2]) # Velocity vector U = algs.make_vector(les_dsa.PointData['U'], les_dsa.PointData['V'], les_dsa.PointData['W']) # Velocity gradient tensor and its transpose # J[:,i-1,j-1] is dUidxj # Jt[:,i-1,j-1] is dUjdxi Jt = algs.gradient(U) # Jt is this one as algs uses j,i ordering J = algs.apply_dfunc(np.transpose, Jt, (0, 2, 1)) # Strain and vorticity tensors Sij = 0.5 * (J + Jt) Oij = 0.5 * (J - Jt) # Anisotropy tensor and eigenvalues aij = copy.deepcopy(Sij) * 0.0 inv2 = np.zeros(les_nnode) inv3 = np.zeros(les_nnode) for i in range(0, 3): for j in range(0, 3): aij[:, i, j] = uiuj[:, i, j] / (2.0 * tke + small) - delij[:, i, j] / 3.0 # Get eigenvalues of aij eig = algs.eigenvalue(aij) eig1 = eig[:, 0] eig2 = eig[:, 1] eig3 = eig[:, 2] # Get coords on barycentric triangle from eigenvalues xc = [1.0, 0.0, 0.5] #x,y coords of corner of triangle yc = [0.0, 0.0, np.cos(np.pi / 6.0)] C1c = eig1 - eig2 C2c = 2 * (eig2 - eig3) C3c = 3 * eig3 + 1 x0 = C1c * xc[0] + C2c * xc[1] + C3c * xc[2] y0 = C1c * yc[0] + C2c * yc[1] + C3c * yc[2] if (y_type == 'Classification'): # Target 1: Negative eddy viscosity ######################################### print('1: Negative eddy viscosity') A = np.zeros(les_nnode) B = np.zeros(les_nnode) for i in range(0, 3): for j in range(0, 3): A += -uiuj[:, i, j] * Sij[:, i, j] + ( 2.0 / 3.0) * tke * delij[:, i, j] * Sij[:, i, j] B += 2.0 * Sij[:, i, j] * Sij[:, i, j] Str = algs.sqrt(B) # magnitude of Sij strain tensor (used later) nu_t = A / (B + small) nu_t = nu_t / (Us * Ls) y_raw[:, targ] = nu_t index = algs.where(nu_t < 0.0) y_targ[index, targ] = 1 target_labels[targ] = 'Negative eddy viscosity' targ += 1 # Target 2: Deviation from plane shear ################################################# print('2: Deviation from plane shear turbulence') # Get distance from plane shear line p1 = (1 / 3, 0) p2 = (0.5, np.sqrt(3) / 2) dist = abs((p2[1] - p1[1]) * x0 - (p2[0] - p1[0]) * y0 + p2[0] * p1[1] - p2[1] * p1[0]) / np.sqrt((p2[1] - p1[1])**2 + (p2[0] - p1[0])**2) y_raw[:, targ] = dist index = algs.where(dist > 0.25) y_targ[index, targ] = 1 target_labels[targ] = 'Deviation from plane shar turbulence' targ += 1 # Target 3: Anisotropy of turbulence ########################################## print('3: Anisotropy of turbulence') Caniso = 1.0 - C3c y_raw[:, targ] = Caniso index = algs.where(Caniso > 0.5) y_targ[index, targ] = 1 target_labels[targ] = 'Stress anisotropy' targ += 1 # Target 4: Negative Pk ############################################ print('4: Negative Pk') A = np.zeros(les_nnode) for i in range(0, 3): for j in range(0, 3): A[:] += (-uiuj[:, i, j] * J[:, i, j]) A = A * Ls / Us**3 y_raw[:, targ] = A index = algs.where(A < -0.0005) y_targ[index, targ] = 1 target_labels[targ] = 'Negative Pk' targ += 1 # Target 5: 2-eqn Cmu constant ############################################ print('5: 2-equation Cmu constant') A = np.zeros(les_nnode) for i in range(0, 3): for j in range(0, 3): A[:] += aij[:, i, j] * Sij[:, i, j] Cmu = nu_t**2.0 * (Str / (tke + small))**2.0 y_raw[:, targ] = Cmu allow_err = 0.25 #i.e. 10% err Cmu_dist = algs.abs(Cmu - 0.09) # index = algs.where(Cmu_dist>allow_err*0.09) index = algs.where(Cmu > 1.1 * 0.09) y_targ[index, targ] = 1 target_labels[targ] = 'Cmu != 0.09' targ += 1 # ab = ((uiuj[:,1,1]-uiuj[:,0,0])*U[:,0]*U[:,1] + uiuj[:,0,1]*(U[:,0]**2-U[:,1]**2))/(U[:,0]**2+U[:,1]**2) # y_raw[:,err] = ab # # Target 3: Non-linearity # ############################### # print('3: Non-linearity') # # # Build cevm equation in form A*nut**3 + B*nut**2 + C*nut + D = 0 # B, A = build_cevm(Sij,Oij) # B = B/(tke +1e-12) # A = A/(tke**2.0 +1e-12) # # C = np.zeros_like(A) # D = np.zeros_like(A) # for i in range(0,3): # for j in range(0,3): # C += -2.0*Sij[:,i,j]*Sij[:,i,j] # D += (2.0/3.0)*tke*Sij[:,i,j]*delij[:,i,j] - uiuj[:,i,j]*Sij[:,i,j] # # nu_t_cevm = np.empty_like(nu_t) # for i in tqdm(range(0,les_nnode)): # # Find the roots of the cubic equation (i.e. potential values for nu_t_cevm) # roots = np.roots([A[i],B[i],C[i],D[i]]) # roots_orig = roots # # # Remove complex solutions (with imaginary part > a small number, to allow for numerical error) # #roots = roots.real[abs(roots.imag)<1e-5] #NOTE - Matches nu_t much better without this?! # # # Out of remaining solutions(s), pick one that is closest to linear nu_t # if(roots.size==0): # nu_t_cevm[i] = nu_t[i] # else: # nu_t_cevm[i] = roots.real[np.argmin( np.abs(roots - np.full(roots.size,nu_t[i])) )] # # normdiff = algs.abs(nu_t_cevm - nu_t) / (algs.abs(nu_t_cevm) + algs.abs(nu_t) + 1e-12) # y_raw[:,err] = nu_t_cevm # # index = algs.where(normdiff>0.15) # y_targ[index,err] = 1 # error_labels[err] = 'Non-linearity' # err += 1 elif (y_type == 'regression'): # Target 3: Anisotropy of turbulence ########################################## print('1: Anisotropy of turbulence') Caniso = 1.0 - C3c y_raw[:, targ] = Caniso y_targ[:, targ] = Caniso target_labels[targ] = 'Stress anisotropy' targ += 1 return y_raw, y_targ, target_labels