def smooth1D(niter, eps): fail = False nzs = CPlot.getSelectedZones() for nz in nzs: nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] z = CTK.t[2][nob][2][noz] dims = Internal.getZoneDim(z) try: if dims[0] == 'Unstructured': a = C.convertBAR2Struct(z) else: a = z a = D.getCurvilinearAbscissa(a) 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(a, distrib) CTK.replace(CTK.t, nob, noz, b) except Exception as e: fail = True Panels.displayErrors([0,str(e)], header='Error: smooth1D') return fail
c2 = C.addBC2Zone(c2, 'overlap1', 'BCOverlap', 'imax') c2 = C.initVars(c2, 'centers:celln', 1.) c2 = C.initVars(c2, 'Density', dens, ['CoordinateX', 'CoordinateY']) a = T.patch(c1, c2, (1, 1, 1)) t = C.newPyTree(['Base', 3]) t[2][1][2].append(a) test.testT(t, 1) # multizone a = D.sphere6((0, 0, 0), 1, N=20) t = C.newPyTree(['Base', 3]) t[2][1][2] += a k = 0 t2 = T.subzone(t, (1, 1, k + 1), (-1, -1, k + 1)) t2 = T.smooth(t2, eps=0.5, niter=20, projConstraints=Internal.getNodesFromType(t2, 'Zone_t')) t = T.patch(t, t2, (1, 1, k + 1)) test.testT(t, 3) # cas 3D c1 = G.cart((0, 0, 0), (0.01, 0.01, 1), (201, 101, 20)) c1 = C.addBC2Zone(c1, 'wall1', 'BCWall', 'imin') c1 = C.addBC2Zone(c1, 'match1', 'BCMatch', 'imax', c1, 'imin', [1, 2, 3]) c1 = C.addBC2Zone(c1, 'overlap1', 'BCOverlap', 'jmax') c1 = C.initVars(c1, 'centers:celln', 1.) c1 = C.initVars(c1, 'Density', dens, ['CoordinateX', 'CoordinateY']) c2 = G.cart((0, 0, 0), (0.01, 0.01, 1), (51, 81, 20)) c2 = T.rotate(c2, (0, 0, 0), (0, 0, 1), 0.2) c2 = C.addBC2Zone(c2, 'wall1', 'BCWall', 'imin')
def smooth(): 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 nzs == []: CTK.TXT.insert('START', 'Selection is empty.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return smooth = CTK.varsFromWidget(VARS[0].get(), type=2) if len(smooth) != 1: CTK.TXT.insert('START', 'Smooth iter is incorrect.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return smooth = smooth[0] eps = CTK.varsFromWidget(VARS[4].get(), type=1) if len(eps) != 1: CTK.TXT.insert('START', 'Eps is incorrect.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return eps = eps[0] ntype = VARS[8].get() if ntype == 'Volume': ntype = 0 elif ntype == 'Scale': ntype = 1 elif ntype == 'Taubin': ntype = 2 else: ntype = 0 # Constraint strength strength = CTK.varsFromWidget(VARS[2].get(), type=1) if len(strength) != 1: CTK.TXT.insert('START', 'Constraint strength is incorrect.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return strength = strength[0] # Constraints fixedConstraints = []; projConstraints = [] name = VARS[1].get() names = name.split(';') for v in names: v = v.lstrip(); v = v.rstrip() sname = v.split('/', 1) base = Internal.getNodeFromName1(CTK.t, sname[0]) if base is not None: nodes = Internal.getNodesFromType1(base, 'Zone_t') for z in nodes: if (z[0] == sname[1]): fixedConstraints.append(z) CTK.saveTree() # Get all zones zones = [] for nz in nzs: nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] z = CTK.t[2][nob][2][noz] zones.append(z) # Mesh unique try: A = C.convertArray2Tetra(zones) A = T.join(A); A = G.close(A) except Exception as e: Panels.displayErrors([0,str(e)], header='Error: smooth') CTK.TXT.insert('START', 'Some zones are invalid for smoothing.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return # pb surfacique ou volumique? dims = Internal.getZoneDim(A) pbDim = 3 if dims[3] == 'TRI': pbDim = 2 # Keep external faces if VARS[3].get() == 1 or pbDim == 3: try: ext = P.exteriorFaces(A) except: ext = [] if VARS[3].get() == 1 and ext != []: fixedConstraints.append(ext) # Keep sharp edges if VARS[5].get() == 1: angle = CTK.varsFromWidget(VARS[6].get(), type=1) if len(angle) != 1: CTK.TXT.insert('START', 'Sharp edges angle is incorrect.\n') CTK.TXT.insert('START', 'Error: ', 'Error'); return angle = angle[0] if pbDim == 2: try: sharp = P.sharpEdges(A, angle) fixedConstraints += sharp except: pass else: try: sharp = P.sharpEdges(ext, angle) fixedConstraints += sharp except: pass # Project on surface Pj = VARS[7].get() if pbDim == 2: projSurf = A else: # projSurf = ext # Pour l'instant, on ne sait pas projeter un maillage volumique # sur une surface Pj = 0 # Smooth fail = False try: if Pj == 0: zones = T.smooth(zones, eps=eps, niter=smooth, type=ntype, fixedConstraints=fixedConstraints, projConstraints=projConstraints, delta=strength) else: for s in xrange(smooth): zones = T.smooth(zones, eps=eps, niter=2, type=ntype, fixedConstraints=fixedConstraints, projConstraints=projConstraints, delta=strength) zones = T.projectOrtho(zones, [projSurf]) except Exception as e: fail = True Panels.displayErrors([0,str(e)], header='Error: smooth') if fail == False: c = 0 for nz in nzs: nob = CTK.Nb[nz]+1 noz = CTK.Nz[nz] a = zones[c]; c += 1 CTK.replace(CTK.t, nob, noz, a) CTK.TXT.insert('START', 'Mesh smoothed.\n') else: CTK.TXT.insert('START', 'Smooth fails.\n') CTK.TXT.insert('START', 'Error: ', 'Error') #C._fillMissingVariables(CTK.t) (CTK.Nb, CTK.Nz) = CPlot.updateCPlotNumbering(CTK.t) CTK.TKTREE.updateApp() CPlot.render()
c1 = G.cart((0, 0, 0), (0.01, 0.01, 1), (201, 101, 20)) C._addBC2Zone(c1, 'wall1', 'BCWall', 'imin') C._addBC2Zone(c1, 'match1', 'BCMatch', 'imax', c1, 'imin', [1, 2, 3]) C._addBC2Zone(c1, 'overlap1', 'BCOverlap', 'jmax') C._initVars(c1, 'centers:celln', 1.) C._initVars(c1, 'Density', dens, ['CoordinateX', 'CoordinateY']) c2 = G.cart((0, 0, 0), (0.01, 0.01, 1), (51, 81, 20)) c2 = T.rotate(c2, (0, 0, 0), (0, 0, 1), 0.2) C._addBC2Zone(c2, 'wall1', 'BCWall', 'imin') C._addBC2Zone(c2, 'overlap1', 'BCOverlap', 'imax') C._initVars(c2, 'centers:celln', 1.) C._initVars(c2, 'Density', dens, ['CoordinateX', 'CoordinateY']) a = T.patch(c1, c2, (1, 1, 1)) t = C.newPyTree(['Base', a]) test.testT(t, 2) # multi-zone 3D a = D.sphere6( (0, 0, 0), 1, N=20, ) d = G.cart((0.1, 0., 0.), (0.1, 1, 1), (5, 1, 1)) a = G.addNormalLayers(a, d) t = C.newPyTree(['Base', 3, a]) t2 = T.subzone(t, (1, 1, 2), (-1, -1, 4)) t2 = T.smooth(t2, eps=0.5, niter=20) t = T.patch(t, t2, (1, 1, 2)) test.testT(t, 3)
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
# - smooth (pyTree) - import Transform.PyTree as T import Geom.PyTree as D import Converter.PyTree as C import KCore.test as test # TRI a = D.sphere((0.,0.,0.), 1) b = C.convertArray2Tetra(a) b = C.addVars(b,'Density'); b = C.initVars(b,'centers:cellN',1.) b = T.smooth(b, eps=0.5, niter=10) test.testT(b,1) # QUAD b = C.convertArray2Hexa(a) b = C.addVars(b,'Density'); b = C.initVars(b,'centers:cellN',1.) b = T.smooth(b, eps=0.5, niter=10) test.testT(b,2)