def meshTri(P0, P1, P2, N): C01 = (0.5 * (P0[0] + P1[0]), 0.5 * (P0[1] + P1[1]), 0.5 * (P0[2] + P1[2])) C12 = (0.5 * (P1[0] + P2[0]), 0.5 * (P1[1] + P2[1]), 0.5 * (P1[2] + P2[2])) C02 = (0.5 * (P0[0] + P2[0]), 0.5 * (P0[1] + P2[1]), 0.5 * (P0[2] + P2[2])) C = (1. / 3. * (P0[0] + P1[0] + P2[0]), 1. / 3. * (P0[1] + P1[1] + P2[1]), 1. / 3. * (P0[2] + P1[2] + P2[2])) l1 = D.line(P0, C01, N) l2 = D.line(C01, C, N) l3 = D.line(C, C02, N) l4 = D.line(C02, P0, N) m1 = G.TFI([l1, l2, l3, l4]) m1 = T.reorder(m1, (-1, 2, 3)) l1 = D.line(C01, P1, N) l2 = D.line(P1, C12, N) l3 = D.line(C12, C, N) l4 = D.line(C, C01, N) m2 = G.TFI([l1, l2, l3, l4]) m2 = T.reorder(m2, (-1, 2, 3)) l1 = D.line(C, C12, N) l2 = D.line(C12, P2, N) l3 = D.line(P2, C02, N) l4 = D.line(C02, C, N) m3 = G.TFI([l1, l2, l3, l4]) m3 = T.reorder(m3, (-1, 2, 3)) return [m1, m2, m3]
def meshCircle(center, R, N): coeff = R * math.sqrt(2.) * 0.25 x = center[0] y = center[1] z = center[2] c = D.circle(center, R, tetas=-45., tetae=45., N=N) l1 = D.line((x + coeff, y - coeff, z), (x + coeff, y + coeff, z), N=N) l2 = D.line((x + coeff, y - coeff, z), (x + 2 * coeff, y - 2 * coeff, z), N=N) l3 = D.line((x + coeff, y + coeff, z), (x + 2 * coeff, y + 2 * coeff, z), N=N) m1 = G.TFI([c, l1, l2, l3]) c = D.circle(center, R, tetas=45., tetae=45. + 90., N=N) l1 = D.line((x + coeff, y + coeff, z), (x - coeff, y + coeff, z), N=N) l2 = D.line((x + coeff, y + coeff, z), (x + 2 * coeff, y + 2 * coeff, z), N=N) l3 = D.line((x - coeff, y + coeff, z), (x - 2 * coeff, y + 2 * coeff, z), N=N) m2 = G.TFI([c, l1, l2, l3]) c = D.circle(center, R, tetas=45. + 90, tetae=45. + 180., N=N) l1 = D.line((x - coeff, y + coeff, z), (x - coeff, y - coeff, z), N=N) l2 = D.line((x - coeff, y + coeff, z), (x - 2 * coeff, y + 2 * coeff, z), N=N) l3 = D.line((x - coeff, y - coeff, z), (x - 2 * coeff, y - 2 * coeff, z), N=N) m3 = G.TFI([c, l1, l2, l3]) c = D.circle(center, R, tetas=45. + 180, tetae=45. + 270., N=N) l1 = D.line((x - coeff, y - coeff, z), (x + coeff, y - coeff, z), N=N) l2 = D.line((x - coeff, y - coeff, z), (x - 2 * coeff, y - 2 * coeff, z), N=N) l3 = D.line((x + coeff, y - coeff, z), (x + 2 * coeff, y - 2 * coeff, z), N=N) m4 = G.TFI([c, l1, l2, l3]) h = 2 * coeff / (N - 1) m5 = G.cart((x - coeff, y - coeff, z), (h, h, h), (N, N, 1)) m5 = T.reorder(m5, (-1, 2, 3)) return [m1, m2, m3, m4, m5]
def TFI(): if CTK.t == []: return if CTK.__MAINTREE__ <= 0: CTK.TXT.insert('START', 'Fail on a temporary tree.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return nzs = CPlot.getSelectedZones() if len(nzs) == 0: CTK.TXT.insert('START', 'Selection is empty.\n') CTK.TXT.insert('START', 'Error: ', 'Error') return surf = getSurfaces() zones = [] for nz in nzs: nob = CTK.Nb[nz] + 1 noz = CTK.Nz[nz] z = CTK.t[2][nob][2][noz] dim = Internal.getZoneDim(z) if dim[3] == 'BAR': zp = C.convertBAR2Struct(z) zones.append(zp) else: zones.append(z) try: CTK.saveTree() mesh = G.TFI(zones) if surf != []: mesh = T.projectOrthoSmooth(mesh, surf) CTK.t = C.addBase2PyTree(CTK.t, 'MESHES') bases = Internal.getNodesFromName1(CTK.t, 'MESHES') nob = C.getNobOfBase(bases[0], CTK.t) CTK.add(CTK.t, nob, -1, mesh) CTK.TXT.insert('START', 'TFI mesh created.\n') #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render() except Exception as e: Panels.displayErrors([0, str(e)], header='Error: TFI') CTK.TXT.insert('START', 'TFI mesh failed.\n') CTK.TXT.insert('START', 'Error: ', 'Error')
# - TFI 3D structure - import Generator.PyTree as G import Converter.PyTree as C import Transform.PyTree as T import KCore.test as test xo = 0.; yo = 0.; zo = 0. nx = 21; ny = 21; nz = 21 hx = 1./(nx-1); hy = 1./(ny-1); hz = 1./(nz-1) # grilles z = cste fzmin = G.cart((xo,yo,zo), (hx,hy,1.), (nx,ny,1)) fzmax = T.translate(fzmin, (0.,0.,1.)) # grilles x = cste fxmin = G.cart((xo,yo,zo),(1,hy,hz),(1,ny,nz)) fxmin = T.reorder(fxmin,(3,1,2)) fxmax = T.translate(fxmin, (1.,0.,0.)) # grilles y = cste fymin = G.cart((xo,yo,zo),(hx,1.,hz),(nx,1,nz)) fymin = T.reorder(fymin,(1,3,2)) fymax = T.translate(fymin, (0.,1.,0.)) r = [fxmin,fxmax,fymin,fymax,fzmin,fzmax] m = G.TFI(r) test.testT(m)
P0 = (0, 0, 1) P1 = (-2, 2, 1) P2 = (-3, 3, 1) P3 = (2, 5, 1) P4 = (0, 7, 1) pts = D.polyline([P0, P1, P2, P3, P4]) b1 = D.bezier(pts) P0 = (5, 0, 1) P1 = (3, 2, 1) P2 = (2, 3, 1) P3 = (6, 5, 1) P4 = (5, 7, 1) pts = D.polyline([P0, P1, P2, P3, P4]) b2 = D.bezier(pts) # Discretisation reguliere de chaque ligne Ni = 20 Nj = 10 r = G.cart((0, 0, 0), (1. / (Ni - 1), 1, 1), (Ni, 1, 1)) q = G.cart((0, 0, 0), (1. / (Nj - 1), 1, 1), (Nj, 1, 1)) r1 = G.map(d1, r) r2 = G.map(d2, r) r3 = G.map(b1, q) r4 = G.map(b2, q) # TTM m = G.TFI([r1, r2, r3, r4]) m = G.TTM(m) test.testT(m, 1)
# - TFI (pyTree) - import Converter.PyTree as C import Generator.PyTree as G import Geom.PyTree as D # Geometry P0 = (0, 0, 0) P1 = (5, 0, 0) P2 = (0, 7, 0) P3 = (5, 7, 0) Ni = 20 Nj = 10 d1 = D.line(P0, P1, Ni) d2 = D.line(P2, P3, Ni) d3 = D.line(P0, P2, Nj) d4 = D.line(P1, P3, Nj) m = G.TFI([d1, d2, d3, d4]) C.convertPyTree2File(m, 'out.cgns')
def apply3D(density, npts, factor, ntype): nzs = CPlot.getSelectedZones() nz = nzs[0] nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] zone = CTK.t[2][nob][2][noz] ret = getEdges3D(zone, 0.) if ret is None: return True (m, r, f, ue, uf, ind) = ret out = [] # Applique la fonction sur m i = m[0] dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] if ntype == 0: # uniformize if density > 0: npts = D.getLength(i)*density if factor > 0: npts = np*factor[0] npts = int(max(npts, 2)) distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1)) b = G.map(i, distrib) elif ntype == 1: # refine if factor < 0: factor = (npts-1.)/(np-1) else: npts = factor*(np-1)+1 b = G.refine(i, factor, 1) elif ntype == 2: # stretch (factor=h) h = factor l = D.getLength(i) a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') N = dims[1] val = C.getValue(a, 's', ind) Xc = CPlot.getActivePoint() valf = val Pind = C.getValue(i, 'GridCoordinates', ind) if ind < N-1: # cherche avec indp1 Pindp1 = C.getValue(i, 'GridCoordinates', ind+1) v1 = Vector.sub(Pindp1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind+1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if ind > 0 and val == valf: # cherche avec indm1 Pindm1 = C.getValue(i, 'GridCoordinates', ind-1) v1 = Vector.sub(Pindm1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind-1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if h < 0: distrib = G.enforcePoint(distrib, valf) else: if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1) elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1) else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1) b = G.map(i, distrib) elif ntype == 3: source = factor b = G.map(i, source, 1) elif ntype == 4: # smooth (factor=eps, npts=niter) niter = npts eps = factor a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') bornes = P.exteriorFaces(distrib) distrib = T.smooth(distrib, eps=eps, niter=niter, fixedConstraints=[bornes]) b = G.map(i, distrib, 1) dimb = Internal.getZoneDim(b) npts = dimb[1] out.append(b) # Raffine les edges si necessaires if npts != np: ret = getEdges3D(zone, 2.) if ret is None: return True (m, r, f, ue, uf, ind) = ret for i in r: dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] factor = (npts-1.)/(np-1) # npts de m b = G.refine(i, factor, 1) out.append(b) # Garde les autres out += ue outf = [] # Rebuild les faces for i in f: # trouve les edges de la face edges = P.exteriorFacesStructured(i) match = [] for e in edges: dime = Internal.getZoneDim(e) np = dime[1]-1 P0 = C.getValue(e, Internal.__GridCoordinates__, 0) P1 = C.getValue(e, Internal.__GridCoordinates__, np) for ei in out: # retrouve les edges par leurs extremites dimei = Internal.getZoneDim(ei) npi = dimei[1]-1 Q0 = C.getValue(ei, Internal.__GridCoordinates__, 0) Q1 = C.getValue(ei, Internal.__GridCoordinates__, npi) t1 = Vector.norm2(Vector.sub(P0,Q0)) t2 = Vector.norm2(Vector.sub(P1,Q1)) if (t1 < 1.e-12 and t2 < 1.e-12): match.append(ei) if len(match) == 4: # OK fn = G.TFI(match) # Projection du patch interieur #dimsf = Internal.getZoneDim(fn) #fns = T.subzone(fn, (2,2,1), (dimsf[1]-1,dimsf[2]-1,1)) #fns = T.projectOrtho(fns, [i]) #fn = T.patch(fn, fns, position=(2,2,1)) #fn = T.projectOrtho(fn, [i]) outf.append(fn) else: return True outf += uf try: b = G.TFI(outf) CTK.replace(CTK.t, nob, noz, b) return False except Exception as e: Panels.displayErrors([0,str(e)], header='Error: apply3D') return True
def apply2D(density, npts, factor, ntype=0): nzs = CPlot.getSelectedZones() nz = nzs[0] nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] zone = CTK.t[2][nob][2][noz] ret = getEdges2D(zone, 0.) if ret is None: return True (m, r, u, ind) = ret out = [] # Applique la fonction sur m[0] (edge a modifier) i = m[0] dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] if ntype == 0: # uniformize if density > 0: npts = D.getLength(i)*density if factor > 0: npts = np*factor[0] npts = int(max(npts, 2)) distrib = G.cart((0,0,0), (1./(npts-1.),1,1), (npts,1,1)) b = G.map(i, distrib) elif ntype == 1: # refine if factor < 0: factor = (npts-1.)/(np-1) else: npts = factor*(np-1)+1 b = G.refine(i, factor, 1) elif ntype == 2: # stretch (factor=h) h = factor l = D.getLength(i) a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') N = dims[1] val = C.getValue(a, 's', ind) Xc = CPlot.getActivePoint() valf = val Pind = C.getValue(i, 'GridCoordinates', ind) if ind < N-1: # cherche avec indp1 Pindp1 = C.getValue(i, 'GridCoordinates', ind+1) v1 = Vector.sub(Pindp1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind+1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if ind > 0 and val == valf: # cherche avec indm1 Pindm1 = C.getValue(i, 'GridCoordinates', ind-1) v1 = Vector.sub(Pindm1, Pind) v2 = Vector.sub(Xc, Pind) if Vector.dot(v1,v2) >= 0: val2 = C.getValue(a, 's', ind-1) alpha = Vector.norm(v2)/Vector.norm(v1) valf = val+alpha*(val2-val) if h < 0: distrib = G.enforcePoint(distrib, valf) else: if val == 0: distrib = G.enforcePlusX(distrib, h/l, N/10, 1) elif val == 1: distrib = G.enforceMoinsX(distrib, h/l, N/10, 1) else: distrib = G.enforceX(distrib, valf, h/l, N/10, 1) b = G.map(i, distrib) elif ntype == 3: # copyDistrib (factor=source=edge pour l'instant) source = factor b = G.map(i, source, 1) elif ntype == 4: # smooth (factor=eps, npts=niter) niter = npts eps = factor a = D.getCurvilinearAbscissa(i) distrib = C.cpVars(a, 's', a, 'CoordinateX') C._initVars(distrib, 'CoordinateY', 0.) C._initVars(distrib, 'CoordinateZ', 0.) distrib = C.rmVars(distrib, 's') bornes = P.exteriorFaces(distrib) distrib = T.smooth(distrib, eps=eps, niter=niter, fixedConstraints=[bornes]) b = G.map(i, distrib, 1) dimb = Internal.getZoneDim(b) npts = dimb[1] out.append(b) # Raffine les edges si necessaires if npts != np: ret = getEdges2D(zone, 2.) if ret is None: return True (m, r, u, ind) = ret for i in r: dims = Internal.getZoneDim(i) np = dims[1]*dims[2]*dims[3] factor = (npts-1.)/(np-1) # npts de m b = G.refine(i, factor, 1) out.append(b) # Garde les autres out += u #tp = C.newPyTree(['Base']) #tp[2][1][2] += out #C.convertPyTree2File(tp, 'edges.cgns') # Rebuild try: b = G.TFI(out) # Projection du patch interieur #dimsb = Internal.getZoneDim(b) #bs = T.subzone(b, (2,2,1), (dimsb[1]-1,dimsb[2]-1,1)) #bs = T.projectOrtho(bs, [zone]) #b = T.patch(b, bs, position=(2,2,1)) #tp = C.newPyTree(['Base']) #tp[2][1][2] += [b, zone] #C.convertPyTree2File(tp, 'face.cgns') b = T.projectOrtho(b, [zone]) CTK.replace(CTK.t, nob, noz, b) return False except Exception as e: Panels.displayErrors([0,str(e)], header='Error: apply2D') return True
# test des fonctions (pyTree) # - TFI tri # - TFI penta import Generator.PyTree as G import Geom.PyTree as D import KCore.test as test #------------ # 1- TFI tri #------------ l1 = D.line((0,0,0),(0,1,0), 15) l2 = D.line((0,0,0),(1,0,0), 15) l3 = D.line((1,0,0),(0,1,0), 15) tri = G.TFI([l1,l2,l3]) test.testT(tri,1) #-------------- # 2- TFI penta #-------------- n = 5 P1 = (0,0,0) P2 = (1,0,0) P3 = (0,1,0) P4 = (0,0,1) P5 = (1,0,1) P6 = (0,1,1) # Tmin l1 = D.line(P1,P3, n) l2 = D.line(P1,P2, n)