def computeDiff(array, varname): """Compute the difference of the field varname defined in array. Usage: computeDiff(array, varname) """ import KCore if isinstance(array[0], list): A = [] for a in array: b = Converter.extractVars(a, [varname]) posc = KCore.isNamePresent(a, 'cellN') if (posc != -1): celln = Converter.extractVars(a, ['cellN']) b = Converter.addVars([b, celln]) A.append(b) b = [] for i in A: b0 = post.computeDiff(i, varname) b0[0] = 'diff' + b0[0] b.append(b0) return b else: b = Converter.extractVars(array, [varname]) posc = KCore.isNamePresent(array, 'cellN') if (posc != -1): celln = Converter.extractVars(array, ['cellN']) b = Converter.addVars([b, celln]) b = post.computeDiff(b, varname) b[0] = 'diff' + b[0] return b
def computeQCriterion(array): vars0 = [ 'gradxVelocityX', 'gradyVelocityX', 'gradzVelocityX', 'gradxVelocityY', 'gradyVelocityY', 'gradzVelocityY', 'gradxVelocityZ', 'gradyVelocityZ', 'gradzVelocityZ' ] # Compute velocity presvx = KCore.isNamePresent(array, 'VelocityX') if presvx == -1: ret = P.computeVariables(array, ['VelocityX', 'VelocityY', 'VelocityZ']) a = C.addVars([array, ret]) else: a = array # compute gradient of velocity presgx = KCore.isNamePresent(a, 'gradxVelocityX') if presgx == -1: gradU = P.computeGrad(a, 'VelocityX') gradV = P.computeGrad(a, 'VelocityY') gradW = P.computeGrad(a, 'VelocityZ') grads = C.addVars([gradU, gradV, gradW]) else: grads = C.extractVars(a, vars0) grads = C.initVars( grads, 'QCriterion=-0.5*({gradxVelocityX}*{gradxVelocityX}+{gradyVelocityY}*{gradyVelocityY}+{gradzVelocityZ}*{gradzVelocityZ}+2*{gradyVelocityX}*{gradxVelocityY}+2*{gradzVelocityX}*{gradxVelocityZ}+2*{gradzVelocityY}*{gradyVelocityZ})' ) ret = C.extractVars(grads, ['QCriterion']) return ret
def setF(a, ind, f): """Set the mesh size factor for a given index.""" pos = KCore.isNamePresent(a, 'f') if pos == -1: C._initVars(a, 'f', 0.) pos = KCore.isNamePresent(a, 'f') if ind == -1: ind = a[1].shape[1] - 1 a[1][pos, ind] = f
def setH(a, ind, h): """Set the mesh step for a given index.""" pos = KCore.isNamePresent(a, 'h') if pos == -1: C._initVars(a, 'h', 0.) pos = KCore.isNamePresent(a, 'h') if ind == -1: ind = a[1].shape[1] - 1 a[1][pos, ind] = h
def buildTag1__(array, F, varStrings): import KCore pos = [] for i in varStrings: i = i.replace('centers:', '') p = KCore.isNamePresent(array, i) + 1 if p == 0: raise ValueError("selectCells: can't find %s in array." % i) else: pos.append(p) n = array[1] nsize = n.shape[1] l = len(varStrings) tag = numpy.zeros(nsize, numpy.float64) if (l == 0): if (F() == True): tag[:] = 1 else: for i in xrange(nsize): x = [n[pos[j] - 1, i] for j in xrange(l)] if (F(*x) == True): tag[i] = 1 tag = tag.reshape(1, tag.size) if (len(array) == 5): out = ['__tag__', tag, array[2], array[3], array[4]] else: out = ['__tag__', tag, array[2], array[3]] return out
def optimizeOverlap(nodes1, centers1, nodes2, centers2, prio1=0, prio2=0, isDW=0): """Optimize the overlap of grids defined by nodes1 and nodes2 centers1 and centers2 define the coordinates of cell centers, modified by the double wall algorithm + cellN variable. Usage: optimizeOverlap(nodes1, centers1, nodes2, centers2,prio1=0, prio2=0,isDW=0)""" import KCore import Converter as C posv1 = KCore.isNamePresent(centers1, 'vol') posv2 = KCore.isNamePresent(centers2, 'vol') if posv1 == -1: try: import Generator as G except: raise ImportError( "optimizeOverlap requires Converter and Generator modules.") vol1 = G.getVolumeMap(nodes1) centers1 = C.addVars([centers1, vol1]) if posv2 == -1: try: import Generator as G except: raise ImportError( "optimizeOverlap requires Converter and Generator modules.") vol2 = G.getVolumeMap(nodes2) centers2 = C.addVars([centers2, vol2]) # extCenters1 = C.node2ExtCenter(nodes1) extCenters2 = C.node2ExtCenter(nodes2) hook1 = C.createHook([extCenters1], 'extractMesh') hook2 = C.createHook([extCenters2], 'extractMesh') res = optimizeOverlap__(extCenters1, centers1, extCenters2, centers2, prio1, prio2, isDW, hook1, hook2) if posv1 == -1: res[0] = C.rmVars(res[0], 'vol') if posv2 == -1: res[1] = C.rmVars(res[1], 'vol') return res
def computeSkinFriction(array, tangent=0): pres1 = KCore.isNamePresent(array, 'sx') if pres1 == -1: try: import Generator as G except: raise ImportError( "computeExtraVariable: skinFriction requires Generator module." ) n = G.getNormalMap(array) n = C.center2Node(n) else: n = C.extractVars(array, ['sx', 'sy', 'sz']) n = C.normalize(n, ['sx', 'sy', 'sz']) a = C.addVars([array, n]) # Array must contain ShearStress a = C.initVars( a, 'SkinFrictionX={ShearStressXX}*{sx}+{ShearStressXY}*{sy}+{ShearStressXZ}*{sz}' ) a = C.initVars( a, 'SkinFrictionY={ShearStressYX}*{sx}+{ShearStressYY}*{sy}+{ShearStressYZ}*{sz}' ) a = C.initVars( a, 'SkinFrictionZ={ShearStressZX}*{sx}+{ShearStressZY}*{sy}+{ShearStressZZ}*{sz}' ) a = C.extractVars(a, ['SkinFrictionX', 'SkinFrictionY', 'SkinFrictionZ']) if (tangent == 1): a = C.addVars([a, n]) a = C.initVars( a, 'SkinFrictionTangentialX={SkinFrictionX} - {sx}*({SkinFrictionX}*{sx}+{SkinFrictionY}*{sy}+{SkinFrictionZ}*{sz})' ) a = C.initVars( a, 'SkinFrictionTangentialY={SkinFrictionY} - {sy}*({SkinFrictionX}*{sx}+{SkinFrictionY}*{sy}+{SkinFrictionZ}*{sz})' ) a = C.initVars( a, 'SkinFrictionTangentialZ={SkinFrictionZ} - {sz}*({SkinFrictionX}*{sx}+{SkinFrictionY}*{sy}+{SkinFrictionZ}*{sz})' ) a = C.extractVars(a, [ 'SkinFrictionTangentialX', 'SkinFrictionTangentialY', 'SkinFrictionTangentialZ' ]) return a
def computeVorticity(array): presvx = KCore.isNamePresent(array, 'VelocityX') if presvx == -1: ret = P.computeVariables(array, ['VelocityX', 'VelocityY', 'VelocityZ']) a = C.addVars([array, ret]) rot = P.computeCurl(a, ['VelocityX', 'VelocityY', 'VelocityZ']) else: rot = P.computeCurl(array, ['VelocityX', 'VelocityY', 'VelocityZ']) if isinstance(rot[0], list): for r in rot: r[0] = 'VorticityX,VorticityY,VorticityZ' else: rot[0] = 'VorticityX,VorticityY,VorticityZ' return rot
# - empty (numpy like) - import KCore # Return un numpy aligne (fortran, double) a = KCore.empty((1200, 3), 64) print a.shape print a.flags
import KCore a = KCore.tester() print a #import Converter as C #b = C.converter.convertHO2LO(a, 0) #print b #C.convertArrays2File(b, 'out.plt') #import sys; sys.exit() import Converter.PyTree as C import Converter.Internal as Internal import Converter a = C.convertArrays2ZoneNode('triangle', [a]) C.convertPyTree2File(a, 'out.cgns') #fc = C.getFields(Internal.__GridCoordinates__, a, api=2)[0] #print fc #fcp = Converter.converter.convertHO2LO(fc, 0) #print fcp #C.setFields([fcp], a, 'nodes', True) #print a #C.convertPyTree2File(a, 'out2.cgns') b = C.convertHO2LO(a, mode=0) print Internal.getZoneDim(b) C.convertPyTree2File(b, 'out2.cgns') import sys
# - indiceFace2Connect (array) - import KCore import Geom as D import Converter as C import Transform as T import Generator as G import numpy # Maillage non structure a = G.cartTetra((0, 0, 0), (1., 1, 1), (10, 10, 1)) a = G.close(a) # liste des faces elt = 0 faces = [elt * 3 + 0, elt * 3 + 1, elt * 3 + 2] connect = KCore.indiceFace2Connect(a, faces) print connect C.convertArrays2File([a], 'out.plt')
import numpy ns = 200 a = G.cart((0, 0, 0), (1, 1, 1), (ns, ns, ns)) # Get an array2 from a x1 = Internal.getNodeFromName2(a, 'CoordinateX') x2 = Internal.getNodeFromName2(a, 'CoordinateY') x3 = Internal.getNodeFromName2(a, 'CoordinateZ') b = ['x,y,z', [x1[1], x2[1], x3[1]], ns, ns, ns] #KCore.tester(b) a = G.cartHexa((0, 0, 0), (1, 1, 1), (ns, ns, ns)) # Get an array2 from a x1 = Internal.getNodeFromName2(a, 'CoordinateX') x2 = Internal.getNodeFromName2(a, 'CoordinateY') x3 = Internal.getNodeFromName2(a, 'CoordinateZ') c1 = Internal.getNodeFromName2(a, 'ElementConnectivity') c1 = c1[1].reshape((8, (ns - 1) * (ns - 1) * (ns - 1))) # cool b = ['x,y,z', [x1[1], x2[1], x3[1]], [c1], 'HEXA'] KCore.tester(b) a = G.cartNGon((0, 0, 0), (1, 1, 1), (ns, ns, ns)) C.convertPyTree2File(a, 'out.cgns') # Get an array2 from a x1 = Internal.getNodeFromName2(a, 'CoordinateX') x2 = Internal.getNodeFromName2(a, 'CoordinateY') x3 = Internal.getNodeFromName2(a, 'CoordinateZ') c1 = Internal.getNodesFromName2(a, 'ElementConnectivity') b = ['x,y,z', [x1[1], x2[1], x3[1]], [c1], 'HEXA'] KCore.tester(b)
def moveCamera(posCams, posEyes=None, dirCams=None, moveEye=False, N=100, speed=1., pos=-1): """Move posCam and posEye following check points.""" # Set d, array of posCams and N nbre of points import KCore import Geom import KCore.Vector as Vector if len(posCams) == 5 and isinstance(posCams[0], str): # input struct array N = posCams[2] d = posCams pinc = KCore.isNamePresent(posCams, 'incEye') if pinc >= 0: pinc = posCams[1][pinc] else: pinc = None else: # list N = max(N, 3) Np = len(posCams) pOut = [] P0 = posCams[0] pOut.append(P0) for i in range(1, Np): P1 = posCams[i] sub = Vector.sub(P1, P0) if Vector.norm(sub) > 1.e-10: pOut.append(P1) P0 = P1 if len(pOut) == 1: d = Geom.polyline(pOut * N) elif len(pOut) == 2: p = Geom.polyline(pOut) d = Geom.spline(p, 2, N) else: p = Geom.polyline(pOut) d = Geom.spline(p, 3, N) pinc = None # Set e, array of posEye of N pts if posEyes is not None: if len(posEyes) == 5 and isinstance(posEyes[0], str): # input struct array Neye = posEyes[2] if Neye != N: import Generator dis = Geom.getDistribution(d) posEyes = Generator.map(posEyes, dis, 1) e = posEyes else: # list Np = len(posEyes) pOut = [] P0 = posEyes[0] pOut.append(P0) for i in range(1, Np): P1 = posEyes[i] sub = Vector.sub(P1, P0) if Vector.norm(sub) > 1.e-10: pOut.append(P1) P0 = P1 if len(pOut) == 1: e = Geom.polyline(pOut * N) elif len(pOut) == 2: p = Geom.polyline(pOut) e = Geom.spline(p, 2, N) else: p = Geom.polyline(pOut) e = Geom.spline(p, 3, N) else: e = None # Set dc, array of dirCams of N pts if dirCams is not None: if len(dirCams) == 5 and isinstance(dirCams[0], str): # input struct array Ndc = dirCams[2] if Ndc != N: import Generator dis = Geom.getDistribution(d) dirCams = Generator.map(dirCams, dis, 1) dc = dirCams else: # list p = Geom.polyline(dirCams) if len(dirCams) == 2: dc = Geom.spline(p, 2, N) else: dc = Geom.spline(p, 3, N) else: dc = None if pos == -1: i = 0 while i < N - 1: time.sleep(__timeStep__ * speed * 0.06) if i > N - 11: inc = N - i - 1 else: inc = 10 posCam = (d[1][0, i], d[1][1, i], d[1][2, i]) if e is not None: posEye = (e[1][0, i], e[1][1, i], e[1][2, i]) if dc is not None: dirCam = (dc[1][0, i], dc[1][1, i], dc[1][2, i]) setState(posCam=posCam, posEye=posEye, dirCam=dirCam) else: setState(posCam=posCam, posEye=posEye) elif moveEye: posEye = (d[1][0, i + inc], d[1][1, i + inc], d[1][2, i + inc]) setState(posCam=posCam, posEye=posEye) else: setState(posCam=posCam) i += 1 else: i = pos i = min(pos, N - 1) if pinc is not None: inc = int(pinc[i]) else: inc = 10 inc = min(inc, N - i - 1) posCam = (d[1][0, i], d[1][1, i], d[1][2, i]) if e is not None: posEye = (e[1][0, i], e[1][1, i], e[1][2, i]) if dc is not None: dirCam = (dc[1][0, i], dc[1][1, i], dc[1][2, i]) setState(posCam=posCam, posEye=posEye, dirCam=dirCam) else: setState(posCam=posCam, posEye=posEye) elif moveEye: posEye = (d[1][0, i + inc], d[1][1, i + inc], d[1][2, i + inc]) setState(posCam=posCam, posEye=posEye) else: setState(posCam=posCam)
def enforceh__(a, N, h): pos = KCore.isNamePresent(a, 'h') if pos == -1: pos = KCore.isNamePresent(a, 'f') if pos == -1: raise ValueError("h or f is not present.") else: hl = numpy.copy(a[1][pos]) factorMoy = moyenne(a, hl) if h > 0: href = h N = D.getLength(a) / (factorMoy * href) + 1 N = int(round(N)) else: href = D.getLength(a) / ((N - 1.) * factorMoy) hl[:] = hl[:] * href else: hl = a[1][pos] npts = hl.size # Calcul h1s, h2s, i1s, i2s h1s = [] h2s = [] Ps = [] i1s = [] i2s = [] h1 = -1 h2 = -1 i1 = -1 i2 = -1 for i in range(npts): hi = hl[i] if hi == 0. and i == npts - 1: hi = h1 if hi > 1.e-12: if i == 0: i1 = 0 h1 = hi if i > 0: if i1 == -1: i1 = 0 i2 = i h1 = hi h2 = hi if h1 > 0: i2 = i h2 = hi sub = T.subzone(a, (i1 + 1, 1, 1), (i2 + 1, 1, 1)) Li = D.getLength(sub) #print 'h1',Li,h1,h2 Pi = Li * 1. / (0.5 * (h1 + h2) + 1.e-12) i1s.append(i1) i2s.append(i2) h1s.append(h1 / Li) h2s.append(h2 / Li) Ps.append(Pi) # loop i1 = i2 h1 = h2 Pt = 0. for x in range(len(h1s)): Pi = Ps[x] Pt += Pi for x in range(len(h1s)): Pi = Ps[x] Pi = Pi / Pt * N #print Ps[x], Pi Ps[x] = int(round(Pi)) + 1 out = [] for x in range(len(h1s)): i1 = i1s[x] i2 = i2s[x] h1 = h1s[x] h2 = h2s[x] # subzone #print i1, i2, h1, h2 sub = T.subzone(a, (i1 + 1, 1, 1), (i2 + 1, 1, 1)) d = buildDistrib(h1, h2, Ps[x]) # remap c = G.map(sub, d) out.append(c) out = T.join(out) return out
def computeShearStress(array, gamma=1.4, rgp=287.053, Cs=110.4, mus=1.76e-5, Ts=273.15): vars0 = [ 'gradxVelocityX', 'gradyVelocityX', 'gradzVelocityX', 'gradxVelocityY', 'gradyVelocityY', 'gradzVelocityY', 'gradxVelocityZ', 'gradyVelocityZ', 'gradzVelocityZ' ] vars1 = ['ViscosityMolecular'] + vars0 presvx = KCore.isNamePresent(array, 'VelocityX') if presvx == -1: ret = P.computeVariables(array, ['VelocityX', 'VelocityY', 'VelocityZ']) a = C.addVars([array, ret]) else: a = array presmu = KCore.isNamePresent(array, 'ViscosityMolecular') if presmu == -1: mu = P.computeVariables(array, ['ViscosityMolecular'], gamma=gamma, rgp=rgp, Cs=Cs, mus=mus, Ts=Ts) mu = C.node2Center(mu) presgx = KCore.isNamePresent(a, 'gradxVelocityX') if presgx == -1: gradU = P.computeGrad(a, 'VelocityX') gradV = P.computeGrad(a, 'VelocityY') gradW = P.computeGrad(a, 'VelocityZ') tau = C.addVars([gradU, gradV, gradW, mu]) else: grads = C.extractVars(a, vars0) tau = C.addVars([grads, mu]) tau = C.initVars( tau, 'ShearStressXX=-2./3.*{ViscosityMolecular}*({gradxVelocityX}+{gradyVelocityY}+{gradzVelocityZ})+{ViscosityMolecular}*2*{gradxVelocityX}' ) tau = C.initVars( tau, 'ShearStressXY={ViscosityMolecular}*({gradyVelocityX}+{gradxVelocityY})' ) tau = C.initVars( tau, 'ShearStressXZ={ViscosityMolecular}*({gradzVelocityX}+{gradxVelocityZ})' ) tau = C.initVars( tau, 'ShearStressYX={ViscosityMolecular}*({gradxVelocityY}+{gradyVelocityX})' ) tau = C.initVars( tau, 'ShearStressYY=-2./3.*{ViscosityMolecular}*({gradxVelocityX}+{gradyVelocityY}+{gradzVelocityZ})+{ViscosityMolecular}*2*{gradyVelocityY}' ) tau = C.initVars( tau, 'ShearStressYZ={ViscosityMolecular}*({gradzVelocityY}+{gradyVelocityZ})' ) tau = C.initVars( tau, 'ShearStressZX={ViscosityMolecular}*({gradxVelocityZ}+{gradzVelocityX})' ) tau = C.initVars( tau, 'ShearStressZY={ViscosityMolecular}*({gradyVelocityZ}+{gradzVelocityY})' ) tau = C.initVars( tau, 'ShearStressZZ=-2./3.*{ViscosityMolecular}*({gradxVelocityX}+{gradyVelocityY}+{gradzVelocityZ})+{ViscosityMolecular}*2*{gradzVelocityZ}' ) ret = C.extractVars(tau, [ 'ShearStressXX', 'ShearStressXY', 'ShearStressXZ', 'ShearStressYX', 'ShearStressYY', 'ShearStressYZ', 'ShearStressZX', 'ShearStressZY', 'ShearStressZZ' ]) return ret
# - indiceStruct2Unstr (array) - import KCore import Geom as D import Converter as C import Transform as T import Generator as G import numpy # Maillage structure de 6 blocks a = D.sphere6((0, 0, 0), 1., N=20) # Maillage QUAD equivalent b = C.convertArray2Hexa(a) b = T.join(b) b = G.close(b) # Indices des vertex du maillage structure dont on cherche la correspondance npts = a[0][1].shape[1] indicesS = numpy.arange(0, npts, dtype=numpy.int32) # Liste des indices des vertex correspondants dans le maillage QUAD b indicesU = KCore.indiceStruct2Unstr(a[0], b, indicesS, 1.e-14) print indicesU # Liste de tous les indices correspondants indicesU = KCore.indiceStruct2Unstr2(a, b, 1.e-14) print indicesU
# - isNamePresent (array) - import KCore import Converter as C a = C.array('x,y,z , densitY',1,1,1) pos = KCore.isNamePresent(a, 'Y') print pos, 'must be -1' pos = KCore.isNamePresent(a, 'densitY') print pos, 'must be 3.'