Esempio n. 1
0
g2.AddInputData(output)

g2.Update()

sphere = dsa.CompositeDataSet(g2.GetOutput())

vn = algs.vertex_normal(sphere)
compare(algs.mag(vn) - 1, 1E-6)

sn = algs.surface_normal(sphere)
compare(algs.mag(sn) - 1, 1E-6)

dot = algs.dot(vn, vn)
assert dot.DataSet is sphere
compare(dot - 1, 1E-6)
assert algs.all(algs.cross(vn, vn) == [0, 0, 0])

fd = sphere.FieldData['field array']
assert algs.all(fd == 5)
assert algs.shape(fd) == (22,)

assert vn.DataSet is sphere

# --------------------------------------

na = dsa.NoneArray

# Test operators
assert (1 + na - 1 - randomVec) is na

# Test slicing and indexing
Esempio n. 2
0
g2.AddInputData(output)

g2.Update()

sphere = dsa.CompositeDataSet(g2.GetOutput())

vn = algs.vertex_normal(sphere)
assert algs.all(algs.mag(vn) - 1 < 1E-6)

sn = algs.surface_normal(sphere)
assert algs.all(algs.mag(sn) - 1 < 1E-6)

dot = algs.dot(vn, vn)
assert dot.DataSet is sphere
assert algs.all(dot == 1)
assert algs.all(algs.cross(vn, vn) == [0, 0, 0])

fd = sphere.FieldData['field array']
assert algs.all(fd == 5)
assert algs.shape(fd) == (22,)

assert vn.DataSet is sphere

# --------------------------------------

na = dsa.NoneArray

# Test operators
assert (1 + na - 1 - randomVec) is na

# Test slicing and indexing
radius = algs.sqrt(x**2 + y**2)
theta = algs.arctan2(y, x)

# Cylindrical Direction Vectors
# radVec
radVec = coordVec.copy()
radVec[:, 2] = radVec[:, 2] * 0
radVec = algs.norm(radVec)

# zVec
zVec = np.repeat([[0, 0, 1]], radVec[:, 0].size, axis=0)
zVec = algs.make_vector(*zVec.T)

# thetaVec
thetaVec = algs.cross(zVec, radVec)
thetaVec = algs.make_vector(*thetaVec.T)
thetaVec = algs.norm(thetaVec)

############################################
#      CREATING CARTESIAN VECTOR DATA
############################################
VelMeanVec = algs.make_vector(input0.PointData['Mean_X_Velocity'].Arrays[0],
                              input0.PointData['Mean_Y_Velocity'].Arrays[0],
                              input0.PointData['Mean_Z_Velocity'].Arrays[0])

VelRMSEVec = algs.make_vector(input0.PointData['RMSE_X_Velocity'].Arrays[0],
                              input0.PointData['RMSE_Y_Velocity'].Arrays[0],
                              input0.PointData['RMSE_Z_Velocity'].Arrays[0])

############################################
Esempio n. 4
0
def make_features(rans_vtk, Ls=1, Us=1, ros=1, nondim='local'):
    from cfd2ml.utilities import build_cevm

    small = np.cbrt(np.finfo(float).tiny)
    Ps = 0.5 * ros * Us**2

    rans_nnode = rans_vtk.number_of_points

    delij = np.zeros([rans_nnode, 3, 3])
    for i in range(0, 3):
        delij[:, i, i] = 1.0

    # Wrap vista object in dsa wrapper
    rans_dsa = dsa.WrapDataObject(rans_vtk)

    print('Feature:')
    nfeat = 15
    feat = 0
    q = np.empty([rans_nnode, nfeat])
    feature_labels = np.empty(nfeat, dtype='object')

    # Feature 1: non-dim Q-criterion
    ################################
    print('1: non-dim Q-criterion...')
    # Velocity vector
    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)
    Oij = 0.5 * (J - Jt)

    # Frob. norm of Sij and Oij  (Snorm and Onorm are actually S^2 and O^2, sqrt needed to get norms)
    Snorm = algs.sum(2.0 * Sij**2, axis=1)  # sum i axis
    Snorm = algs.sum(Snorm,
                     axis=1)  # sum previous summations i.e. along j axis
    Onorm = algs.sum(2.0 * Oij**2, axis=1)  # sum i axis
    Onorm = algs.sum(Onorm,
                     axis=1)  # sum previous summations i.e. along j axis

    # Store q1
    q[:, feat] = (Onorm - 0.5 * Snorm) / (Onorm + 0.5 * Snorm + small)
    feature_labels[feat] = 'Normalised strain'
    feat += 1

    # clean up
    Snorm = algs.sqrt(Snorm)  #revert to revert to real Snorm for use later
    Onorm = algs.sqrt(Onorm)  #revert to revert to real Onorm for use later

    # Feature 2: Turbulence intensity
    #################################
    print('2: Turbulence intensity')
    tke = rans_dsa.PointData['k']
    UiUi = algs.mag(U)**2.0
    #    q[:,feat] = tke/(0.5*UiUi+tke+small)
    q[:, feat] = tke / (0.5 * UiUi + small)
    feature_labels[feat] = 'Turbulence intensity'
    feat += 1

    # Feature 3: Turbulence Reynolds number
    #######################################
    print('3: Turbulence Reynolds number')
    nu = rans_dsa.PointData['mu_l'] / rans_dsa.PointData['ro']
    Red = (algs.sqrt(tke) * rans_dsa.PointData['d']) / (50.0 * nu)
    q[:, feat] = algs.apply_dfunc(np.minimum, Red, 2.0)
    #Red = 0.09**0.25*algs.sqrt(tke)*rans_dsa.PointData['d']/nu
    #q[:,feat] = algs.apply_dfunc(np.minimum, Red, 100.0)
    feature_labels[feat] = 'Turbulence Re'
    feat += 1

    # Feature 4: Pressure gradient along streamline
    ###############################################
    print('4: Stream-wise pressure gradient')
    A = np.zeros(rans_nnode)
    B = np.zeros(rans_nnode)

    dpdx = algs.gradient(rans_dsa.PointData['p'])
    ro = rans_dsa.PointData['ro']
    Umag = algs.mag(U)

    for k in range(0, 3):
        A += U[:, k] * dpdx[:, k]

    if nondim == 'global':
        A = A / Umag
        q[:, feat] = A * Ls / Ps
    elif nondim == 'local':
        for i in range(0, 3):
            for j in range(0, 3):
                B += U[:, i] * U[:, i] * dpdx[:, j] * dpdx[:, j]
        q[:, feat] = A / (algs.sqrt(B) + algs.abs(A) + small)

    feature_labels[feat] = 'Stream-wise Pgrad'
    feat += 1

    # Feature 5: Ratio of turb time scale to mean strain time scale
    ###############################################################
    print('5: Ratio of turb time scale to mean strain time scale')
    #    A = 1.0/rans_dsa.PointData['w']  #Turbulent time scale (eps = k*w therefore also A = k/eps)
    #    B = 1.0/Snorm
    #    q[:,feat] = A/(A+B+small)
    q[:, feat] = Snorm / (rans_dsa.PointData['w'] + small)
    feature_labels[feat] = 'turb/strain time-scale'
    feat += 1

    # Feature 6: Viscosity ratio
    ############################
    print('6: Viscosity ratio')
    nu_t = rans_dsa.PointData['mu_t'] / ro
    #    q[:,feat] = nu_t/(100.0*nu + nu_t)
    q[:, feat] = nu_t / (nu + small)
    feature_labels[feat] = 'Viscosity ratio'
    feat += 1

    # Feature 7: Vortex stretching
    ##############################
    print('7: Vortex stretching')
    A = np.zeros(rans_nnode)
    B = np.zeros(rans_nnode)

    vortvec = algs.vorticity(U)

    for j in range(0, 3):
        for i in range(0, 3):
            for k in range(0, 3):
                A += vortvec[:, j] * J[:, i, j] * vortvec[:, k] * J[:, i, k]

    B = Snorm

    #    q[:,feat] = algs.sqrt(A)/(algs.sqrt(A)+B+small)
    q[:, feat] = algs.sqrt(A)  #/(algs.sqrt(A)+B+small)
    feature_labels[feat] = 'Vortex stretching'
    feat += 1

    # Feature 8: Marker of Gorle et al. (deviation from parallel shear flow)
    ########################################################################
    print('8: Marker of Gorle et al. (deviation from parallel shear flow)')
    if nondim == 'global':
        g = np.zeros([rans_nnode, 3])
        m = np.zeros(rans_nnode)
        s = U / Umag
        for j in range(3):
            for i in range(3):
                g[:, j] += s[:, i] * J[:, i, j]
            m += g[:, j] * s[:, j]
        m = np.abs(m)
        q[:, feat] = m * Ls / Us
    elif nondim == 'local':
        A = np.zeros(rans_nnode)
        B = np.zeros(rans_nnode)
        for i in range(0, 3):
            for j in range(0, 3):
                A += U[:, i] * U[:, j] * J[:, i, j]
        for n in range(0, 3):
            for i in range(0, 3):
                for j in range(0, 3):
                    for m in range(0, 3):
                        B += U[:, n] * U[:, n] * U[:,
                                                   i] * J[:, i,
                                                          j] * U[:,
                                                                 m] * J[:, m,
                                                                        j]
        q[:, feat] = algs.abs(A) / (algs.sqrt(B) + algs.abs(A) + small)
    feature_labels[feat] = 'Deviation from parallel shear'
    feat += 1

    # Feature 9: Ratio of convection to production of k
    ####################################################
    print('9: Ratio of convection to production of k')
    uiuj = (2.0 / 3.0) * tke * delij - 2.0 * nu_t * Sij
    dkdx = algs.gradient(tke)
    A = np.zeros(rans_nnode)
    B = np.zeros(rans_nnode)
    for i in range(0, 3):
        A += U[:, i] * dkdx[:, i]
    for j in range(0, 3):
        for l in range(0, 3):
            B += uiuj[:, j, l] * Sij[:, j, l]
    q[:, feat] = A / (algs.abs(B) + small)
    feature_labels[feat] = 'Convection/production of k'
    feat += 1

    # Feature 10: Ratio of total Reynolds stresses to normal Reynolds stresses
    ##########################################################################
    print('10: Ratio of total Reynolds stresses to normal Reynolds stresses')
    # Frob. norm of uiuj
    A = algs.sum(uiuj**2, axis=1)  # sum i axis
    A = algs.sum(A, axis=1)  # sum previous summations i.e. along j axis
    A = algs.sqrt(A)
    B = tke
    q[:, feat] = A / (B + small)
    feature_labels[feat] = 'total/normal stresses'
    feat += 1

    # Feature 11: Cubic eddy viscosity comparision
    ##############################################
    print('11: Cubic eddy viscosity comparision')

    # Add quadratic and cubic terms to linear evm
    cevm_2nd, cevm_3rd = build_cevm(Sij, Oij)
    uiujSij = np.zeros(rans_nnode)
    for i in range(0, 3):
        for j in range(0, 3):
            uiujSij += uiuj[:, i, j] * Sij[:, i, j]
    uiujcevmSij = uiujSij + (cevm_2nd / tke) * nu_t**2.0 + (
        cevm_3rd / tke**2.0) * nu_t**3.0
    q[:, feat] = (uiujcevmSij -
                  uiujSij) / (0.5 *
                              (np.abs(uiujcevmSij) + np.abs(uiujSij)) + small)
    feature_labels[feat] = 'CEV comparison'
    feat += 1

    # Feature 12: Streamline normal pressure gradient
    #################################################
    print('12: Stream-normal pressure gradient')
    A = algs.cross(U, dpdx)
    A = np.sqrt(A[:, 0]**2 + A[:, 1]**2 + A[:, 2]**2)

    if nondim == 'global':
        A = A / Umag
        q[:, feat] = A * Ls / Ps
    elif nondim == 'local':
        B = np.zeros(rans_nnode)
        for i in range(0, 3):
            for j in range(0, 3):
                B += U[:, i] * U[:, i] * dpdx[:, j] * dpdx[:, j]
        q[:, feat] = A / (A + algs.sqrt(B) + small)

    feature_labels[feat] = 'Stream-normal Pgrad'
    feat += 1

    # Feature 13: Streamline curvature
    ##################################
    print('13: Streamline curvature')
    #    A = np.zeros([rans_nnode,3])
    #
    #    # Gradient of Gamma
    #    Gamma = U#/algs.mag(U)
    #    dGammadx = algs.gradient(Gamma)
    #
    #    for i in range(0,3):
    #        for j in range(0,3):
    #            A[:,i] += U[:,j]*dGammadx[:,j,i]
    #    A = algs.mag(A/algs.mag(U)*algs.mag(U))
    #
    #    q[:,feat] = A
    #    feature_labels[feat] = 'Streamline curvature'
    #    feat += 1
    D2 = 0.5 * (Snorm**2 + Onorm**2)
    #    cr1 = 1.0
    #    cr2 = 12.0
    #    cr3 = 1.0
    cr2 = 12
    cr3 = 1 / np.pi
    rstar = Snorm / (Onorm + small)

    dSijdx1 = algs.gradient(Sij[:, :, 0])
    dSijdx2 = algs.gradient(Sij[:, :, 1])
    dSijdx3 = algs.gradient(Sij[:, :, 2])

    DSijDt = np.zeros([rans_nnode, 3, 3])
    for i in range(3):
        for j in range(3):
            DSijDt[:, i,
                   j] = U[:,
                          0] * dSijdx1[:, j,
                                       i] + U[:,
                                              1] * dSijdx2[:, j,
                                                           i] + U[:,
                                                                  2] * dSijdx3[:,
                                                                               j,
                                                                               i]

    rhat = np.zeros(rans_nnode)
    for i in range(3):
        for j in range(3):
            for k in range(3):
                rhat += (2 * Oij[:, i, k] * Sij[:, j, k] /
                         D2**2) * DSijDt[:, i, j]


#    fr1 = -((2*rstar)/(1+rstar))*(cr3*algs.arctan(cr2*rhat))
#    fr1 = ( (1+cr1)*((2*rstar)/(1+rstar))*(1-cr3*algs.arctan(cr2*rhat)) ) - cr1
    rhathat = algs.arctan(0.25 * rhat) * 2 / np.pi
    q[:, feat] = rhathat  #fr1
    feature_labels[feat] = 'Streamline curvature'
    feat += 1

    # Feature 14: Anisotropy of pressure hessian
    ############################################
    print('14: Anisotropy of pressure hessian')
    # Calculate pressure hessian
    Hij = algs.gradient(dpdx)
    Hij = algs.apply_dfunc(np.transpose, Hij, (0, 2, 1))
    aniso = np.zeros(rans_nnode)
    iso = np.zeros(rans_nnode)
    # Frob. norm of Hij
    for i in range(3):
        for j in range(3):
            aniso += (Hij[:, i, j] - Hij[:, i, j] * delij[:, i, j])**2
        iso += Hij[:, i, i]**2

    aniso = np.sqrt(aniso)
    iso = np.sqrt(iso)
    q[:, feat] = (aniso) / (iso + small)

    feature_labels[feat] = 'Anisotropy of pressure hessian'
    feat += 1

    # Feature 15: White noise
    #########################
    print('15: White noise')
    q[:, feat] = np.random.uniform(low=-1.0, high=1.0, size=rans_nnode)
    feature_labels[feat] = 'White noise'
    feat += 1

    return q, feature_labels