coords=[0.,0.,0., 1.,1.,0., 1.,1.25,0., 1.,0.,0., 1.,1.5,0., 2.,0.,0., 2.,1.,0., 1.,2.,0., 0.,2.,0., 3.,1.,0., 3.,2.,0., 0.,1.,0., 1.,3.,0., 2.,2.,0., 2.,3.,0., 0.,0.,1., 1.,1.,1., 1.,1.25,1., 1.,0.,1., 1.,1.5,1., 2.,0.,1., 2.,1.,1., 1.,2.,1., 0.,2.,1., 3.,1.,1., 3.,2.,1., 0.,1.,1., 1.,3.,1., 2.,2.,1., 2.,3.,1., 0.,0.,2., 1.,1.,2., 1.,1.25,2., 1.,0.,2., 1.,1.5,2., 2.,0.,2., 2.,1.,2., 1.,2.,2., 0.,2.,2., 3.,1.,2., 3.,2.,2., 0.,1.,2., 1.,3.,2., 2.,2.,2., 2.,3.,2., 0.,0.,3., 1.,1.,3., 1.,1.25,3., 1.,0.,3., 1.,1.5,3., 2.,0.,3., 2.,1.,3., 1.,2.,3., 0.,2.,3., 3.,1.,3., 3.,2.,3., 0.,1.,3., 1.,3.,3., 2.,2.,3., 2.,3.,3.] conn=[0,11,1,3,15,26,16,18, 1,2,4,7,13,6,-1,1,16,21,6,-1,6,21,28,13,-1,13,7,22,28,-1,7,4,19,22,-1,4,2,17,19,-1,2,1,16,17,-1,16,21,28,22,19,17, 1,6,5,3,16,21,20,18, 13,10,9,6,28,25,24,21, 11,8,7,4,2,1,-1,11,26,16,1,-1,1,16,17,2,-1,2,17,19,4,-1,4,19,22,7,-1,7,8,23,22,-1,8,11,26,23,-1,26,16,17,19,22,23, 7,12,14,13,22,27,29,28, 15,26,16,18,30,41,31,33, 16,17,19,22,28,21,-1,16,31,36,21,-1,21,36,43,28,-1,28,22,37,43,-1,22,19,34,37,-1,19,17,32,34,-1,17,16,31,32,-1,31,36,43,37,34,32, 16,21,20,18,31,36,35,33, 28,25,24,21,43,40,39,36, 26,23,22,19,17,16,-1,26,41,31,16,-1,16,31,32,17,-1,17,32,34,19,-1,19,34,37,22,-1,22,23,38,37,-1,23,26,41,38,-1,41,31,32,34,37,38, 22,27,29,28,37,42,44,43, 30,41,31,33,45,56,46,48, 31,32,34,37,43,36,-1,31,46,51,36,-1,36,51,58,43,-1,43,37,52,58,-1,37,34,49,52,-1,34,32,47,49,-1,32,31,46,47,-1,46,51,58,52,49,47, 31,36,35,33,46,51,50,48, 43,40,39,36,58,55,54,51, 41,38,37,34,32,31,-1,41,56,46,31,-1,31,46,47,32,-1,32,47,49,34,-1,34,49,52,37,-1,37,38,53,52,-1,38,41,56,53,-1,56,46,47,49,52,53, 37,42,44,43,52,57,59,58] mesh3D = mc.MEDCouplingUMesh("mesh3D",3); mesh3D.allocateCells(18); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[0:8]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[8:51]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[51:59]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[59:67]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[67:110]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[110:118]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[118:126]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[126:169]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[169:177]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[177:185]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[185:228]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[228:236]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[236:244]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[244:287]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[287:295]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[295:303]); mesh3D.insertNextCell(mc.NORM_POLYHED,conn[303:346]); mesh3D.insertNextCell(mc.NORM_HEXA8,conn[346:354]); myCoords = mc.DataArrayDouble(coords,60,3); myCoords.setInfoOnComponents(["X [m]","Y [m]","Z [m]"]) mesh3D.setCoords(myCoords); mesh3D.orientCorrectlyPolyhedrons() mesh3D.sortCellsInMEDFileFrmt() mesh3D.checkConsistencyLight() renum = mc.DataArrayInt(60) ; renum[:15]=range(15,30) ; renum[15:30]=range(15) ; renum[30:45]=range(45,60) ; renum[45:]=range(30,45) mesh3D.renumberNodes(renum,60) # Scale coordinates from meters to centimeters mesh3D.getCoords()[:] *= 100. mesh3D.getCoords().setInfoOnComponents(["X [cm]","Y [cm]","Z [cm]"])
import os import MEDCoupling as mc import MEDLoader as ml # Connectivity coords = [ -0.3, -0.3, 0., 0.2, -0.3, 0., 0.7, -0.3, 0., -0.3, 0.2, 0., 0.2, 0.2, 0., 0.7, 0.2, 0., -0.3, 0.7, 0., 0.2, 0.7, 0., 0.7, 0.7, 0. ] nodalConnOfCell = [0, 3, 4, 1, 1, 4, 2, 4, 5, 2, 6, 7, 4, 3, 7, 8, 5, 4] # Making mesh mesh = mc.MEDCouplingUMesh("My2DMesh", 2) # Allocating resources mesh.allocateCells(5) #You can put more than 5 if you want but not less. # Building cells print("Cella n.1 formata dai nodi --> ", nodalConnOfCell[:4]) mesh.insertNextCell(mc.NORM_QUAD4, nodalConnOfCell[:4]) print("Cella n.2 formata dai nodi --> ", nodalConnOfCell[4:7]) mesh.insertNextCell(mc.NORM_TRI3, nodalConnOfCell[4:7]) print("Cella n.3 formata dai nodi --> ", nodalConnOfCell[7:10]) mesh.insertNextCell(mc.NORM_TRI3, nodalConnOfCell[7:10]) print("Cella n.4 formata dai nodi --> ", nodalConnOfCell[10:14]) mesh.insertNextCell(mc.NORM_QUAD4, nodalConnOfCell[10:14])
def mesh_square_with_hexagons(xmin=0,xmax=1,ymin=0,ymax=1,ny=14,mesh_name="squareWithHexagons"): radius=(ymax-ymin)/(ny*math.sqrt(3.)) r = math.sqrt(3.)/2*radius nx = int( 2*(xmax-xmin)/(3.*radius) ) print "Meshing a square with hexagons nx=",nx,"ny=",ny, "ncells=",nx*ny # Building the coordinates of the initial hexagon, centered at 0,0 d = mc.DataArrayDouble(6,2) d[:,0] = radius d[:,1] = range(6) d[:,1] *= math.pi/3. d = d.fromPolarToCart() d.setInfoOnComponents(["X [m]","Y [m]"]) print "Uniform array ?", d.magnitude().isUniform(radius,1e-12) # translations of the first cell translationToPerform = [[xmin+(1.5*j+1)*radius,ymin+(2*i+(j%2)+1)*r] for i in range(ny) for j in range(nx)] ds = len(translationToPerform)*[None] for pos,t in enumerate(translationToPerform): ds[pos] = d[:] # Perform a deep copy of d and place it at position 'pos' in ds ds[pos] += t # Adding a vector to a set of coordinates does a translation pass d2 = mc.DataArrayDouble.Aggregate(ds) # Build an unstructured mesh representing the final pattern mesh = mc.MEDCouplingUMesh(mesh_name,2) mesh.setCoords(d2) print "Mesh dimension is", mesh.getMeshDimension() print "Spatial dimension is", mesh.getCoords().getNumberOfComponents() mesh.allocateCells(nx*ny) for i in xrange(nx*ny): cell_connec = range(6*i,6*(i+1)) mesh.insertNextCell(mc.NORM_POLYGON, cell_connec) pass # Identifying duplicate nodes oldNbOfNodes=mesh.getNumberOfNodes() arr, areNodesMerged, newNbOfNodes=mesh.mergeNodes(1e-10) print "oldNbOfNodes=",oldNbOfNodes,"newNbOfNodes",newNbOfNodes # Crée les éléments 1D pour pouvoir imposer les conditions aux limites mesh_1d = mesh.computeSkin() # Identifie les segments de chaque côté pour créer les groupes tol = 1e-10 # PB: getCellsInBoundingBox renvoie aussi les segments qui touchent la bounding box # => On boucle sur les coordonnées des barycentres barycenters = mesh_1d.computeIsoBarycenterOfNodesPerCell() ids_left = [] ids_right = [] ids_bottom = [] ids_top = [] for i, coord in enumerate(barycenters): x, y = coord if abs(x-xmin-radius/4) < tol:# ids_left.append(i) elif abs(x-xmin-(1.5*nx+0.25)*radius) < tol: ids_right.append(i) elif abs(y-ymin) < tol or abs(y-ymin-r) < tol or abs(y-ymin-r/2) < tol: ids_bottom.append(i) elif abs(y-ymin-2*r*ny) < tol or abs(y-ymin-2*r*ny-r) < tol or abs(y-ymin-2*r*ny-r/2) < tol: ids_top.append(i) else: raise ValueError("Pb with boundary construction : barycenter does not belong to any border group") arr_left = mc.DataArrayInt(ids_left) arr_right = mc.DataArrayInt(ids_right) arr_bottom = mc.DataArrayInt(ids_bottom) arr_top = mc.DataArrayInt(ids_top) arr_left.setName("Left") arr_right.setName("Right") arr_bottom.setName("Bottom") arr_top.setName("Top") # Trie les cellules par type conformément à la convention MED fichier o2n = mesh.sortCellsInMEDFileFrmt() meshMEDFile=ML.MEDFileUMesh.New() # Ecrit le maillage 2D meshMEDFile.setMeshAtLevel(0,mesh) # Ecrit le maillage 1D meshMEDFile.setMeshAtLevel(-1,mesh_1d) # Ecrit les groupes meshMEDFile.addGroup(-1, arr_left) meshMEDFile.addGroup(-1, arr_right) meshMEDFile.addGroup(-1, arr_bottom) meshMEDFile.addGroup(-1, arr_top) # Check that everything is coherent (will throw if not) mesh.checkConsistencyLight() filename = mesh_name+".med" # Write the result into a VTU file that can be read with ParaView #mesh.writeVTK(mesh_name+".vtu") # Write the result into a MED file that can be read with Salomé meshMEDFile.write(filename,2) # 2 stands for write from scratch
def createBrickWallMesh(xmin=0., xmax=1., nx=15, ymin=0., ymax=1., ny=15, mesh_name="squareWithBrickWall"): dx = (xmax - xmin) / nx dy = (ymax - ymin) / ny print "Creating BrickWall mesh with nx=", nx, "ny=", ny, "nb cells=", nx * ny # Building the initial rectangular cell, centered at 0,0 d = mc.DataArrayDouble(4, 2) d[0, 0] = -dx / 2 d[0, 1] = dy / 2 d[1, 0] = dx / 2 d[1, 1] = dy / 2 d[2, 0] = dx / 2 d[2, 1] = -dy / 2 d[3, 0] = -dx / 2 d[3, 1] = -dy / 2 d.setInfoOnComponents(["X [m]", "Y [m]"]) print "Uniform array ?", d.magnitude().isUniform( 0.5 * math.sqrt(dx * dx + dy * dy), 1e-10) # translation of the first cell translationToPerform = [[(0.5 * (1 + j % 2) + i) * dx, (0.5 + j) * dy] for i in range(nx) for j in range(ny)] ds = len(translationToPerform) * [None] for pos, t in enumerate(translationToPerform): ds[pos] = d[:] # Perform a deep copy of d and place it at position 'pos' in ds ds[pos] += t # Adding a vector to a set of coordinates does a translation pass d2 = mc.DataArrayDouble.Aggregate(ds) # Build an unstructured mesh representing the final pattern mesh = mc.MEDCouplingUMesh(mesh_name, 2) mesh.setCoords(d2) print "Mesh dimension is", mesh.getMeshDimension() print "Spatial dimension is", mesh.getCoords().getNumberOfComponents() mesh.allocateCells(nx * ny) for i in xrange(nx * ny): cell_connec = range(4 * i, 4 * (i + 1)) mesh.insertNextCell(mc.NORM_POLYGON, cell_connec) pass # Identifying duplicate nodes oldNbOfNodes = mesh.getNumberOfNodes() arr, areNodesMerged, newNbOfNodes = mesh.mergeNodes(1e-10) print "oldNbOfNodes=", oldNbOfNodes, "newNbOfNodes", newNbOfNodes # Crée des polygones pour rendre conforme les mailles mesh.conformize2D(1e-10) # Check that everything is coherent (will throw if not) mesh.checkConsistencyLight() # Crée les éléments 1D pour pouvoir imposer les conditions aux limites mesh_1d = mesh.computeSkin() # Identifie les segments de chaque côté pour créer les groupes tol = 1e-10 # PB: getCellsInBoundingBox renvoie aussi les segments qui touchent la bounding box # => On boucle sur les coordonnées des barycentres barycenters = mesh_1d.computeIsoBarycenterOfNodesPerCell() ids_left = [] ids_right = [] ids_bottom = [] ids_top = [] for i, coord in enumerate(barycenters): x, y = coord if abs(y - ymin) < tol: ids_bottom.append(i) elif abs(y - ymax) < tol: ids_top.append(i) elif abs(x - xmax) < tol or abs(x - xmax - dx / 4) < tol or abs(x - xmax - dx / 2) < tol: ids_right.append(i) elif abs(x - xmin) < tol or abs(x - xmin - dx / 4) < tol or abs(x - xmin - dx / 2) < tol: ids_left.append(i) else: raise ValueError( "Pb with boundary construction : barycenter does not belong to any border group" ) arr_left = mc.DataArrayInt(ids_left) arr_right = mc.DataArrayInt(ids_right) arr_bottom = mc.DataArrayInt(ids_bottom) arr_top = mc.DataArrayInt(ids_top) arr_left.setName("Left") arr_right.setName("Right") arr_bottom.setName("Bottom") arr_top.setName("Top") # Trie les cellules par type conformément à la convention MED fichier o2n = mesh.sortCellsInMEDFileFrmt() meshMEDFile = ML.MEDFileUMesh.New() # Ecrit le maillage 2D meshMEDFile.setMeshAtLevel(0, mesh) # Ecrit le maillage 1D meshMEDFile.setMeshAtLevel(-1, mesh_1d) # Ecrit les groupes meshMEDFile.addGroup(-1, arr_left) meshMEDFile.addGroup(-1, arr_right) meshMEDFile.addGroup(-1, arr_bottom) meshMEDFile.addGroup(-1, arr_top) filename = mesh_name + str(nx * ny) + ".med" # Write the result into a VTU file that can be read with ParaView #mesh.writeVTK(mesh_name+str(nx*ny)+".vtu") # Write the result into a MED file that can be read with Salomé meshMEDFile.write(filename, 2) # 2 stands for write from scratch
def mesh_disk_with_squares(xcenter=0.,ycenter=0., Radius=1.,n=17,mesh_name="diskWithSquares"): xmin=-Radius xmax=Radius ymin=-Radius ymax=Radius dx = (xmax-xmin)/n dy=(ymax-ymin)/n # Building the initial rectangular cell, centered at 0,0 d = mc.DataArrayDouble(4,2) d[0,0] = -dx/2 d[0,1] = dy/2 d[1,0] = dx/2 d[1,1] = dy/2 d[2,0] = dx/2 d[2,1] = -dy/2 d[3,0] = -dx/2 d[3,1] = -dy/2 d.setInfoOnComponents(["X [m]","Y [m]"]) print "Uniform array ?", d.magnitude().isUniform(0.5*math.sqrt(dx*dx+dy*dy),1e-10) # translation of the first cell translationToPerform = [] for i in range(n) : for j in range(n): if (xcenter-xmin-(0.5+i)*dx)**2+(ycenter-ymin-(0.5+j)*dy)**2<Radius*Radius : translationToPerform.append([xmin+(0.5+i)*dx,ymin+(0.5+j)*dy] ) ncells= len(translationToPerform) print "Meshing a disk with squares ",n," nb of cells=",ncells ds = ncells*[None] for pos,t in enumerate(translationToPerform): ds[pos] = d[:] # Perform a deep copy of d and place it at position 'pos' in ds ds[pos] += t # Adding a vector to a set of coordinates does a translation pass d2 = mc.DataArrayDouble.Aggregate(ds) # Build an unstructured mesh representing the final pattern mesh = mc.MEDCouplingUMesh(mesh_name,2) mesh.setCoords(d2) print "Mesh dimension is", mesh.getMeshDimension() print "Spatial dimension is", mesh.getCoords().getNumberOfComponents() mesh.allocateCells(ncells) for i in xrange(ncells): cell_connec = range(4*i,4*(i+1)) mesh.insertNextCell(mc.NORM_POLYGON, cell_connec) pass # Identifying duplicate nodes oldNbOfNodes=mesh.getNumberOfNodes() arr, areNodesMerged, newNbOfNodes=mesh.mergeNodes(1e-10) print "oldNbOfNodes=",oldNbOfNodes,"newNbOfNodes",newNbOfNodes # Check that everything is coherent (will throw if not) mesh.checkConsistencyLight() # Crée les éléments 1D pour pouvoir imposer les conditions aux limites mesh_1d = mesh.computeSkin() # Trie les cellules par type conformément à la convention MED fichier o2n = mesh.sortCellsInMEDFileFrmt() meshMEDFile=ML.MEDFileUMesh.New() # Ecrit le maillage 2D meshMEDFile.setMeshAtLevel(0,mesh) # Ecrit le maillage 1D meshMEDFile.setMeshAtLevel(-1,mesh_1d) # Ecrit les groupes arr_circle = mc.DataArrayInt(range(mesh_1d.getNumberOfCells())) arr_circle.setName("Circle") meshMEDFile.addGroup(-1, arr_circle) filename = mesh_name+".med" # Write the result into a VTU file that can be read with ParaView #mesh.writeVTK(mesh_name+".vtu") # Write the result into a MED file that can be read with Salomé meshMEDFile.write(filename,2) # 2 stands for write from scratch
def mesh_disk_with_hexagons(xcenter=0., ycenter=0., Radius=1., ny=16, mesh_name="diskWithHexagons"): xmin = -Radius xmax = Radius ymin = -Radius ymax = Radius hradius = (ymax - ymin) / (ny * math.sqrt(3.)) r = math.sqrt(3.) / 2 * hradius nx = int(2 * (xmax - xmin) / (3. * hradius)) # Building the coordinates of the initial hexagon, centered at 0,0 d = mc.DataArrayDouble(6, 2) d[:, 0] = hradius d[:, 1] = range(6) d[:, 1] *= math.pi / 3. d = d.fromPolarToCart() d.setInfoOnComponents(["X [m]", "Y [m]"]) print "Uniform array ?", d.magnitude().isUniform(hradius, 1e-12) # translations of the first cell that are inside the circle translationToPerform = [] for i in range(ny): for j in range(nx): if (xcenter - xmin - (1.5 * j + 1) * hradius)**2 + ( ycenter - ymin - (2 * i + (j % 2) + 1) * r)**2 < Radius * Radius: translationToPerform.append([ xmin + (1.5 * j + 1) * hradius, ymin + (2 * i + (j % 2) + 1) * r ]) ncells = len(translationToPerform) print "Meshing a disk with hexagons nx=", nx, "ny=", ny, "nb of cells=", ncells ds = ncells * [None] for pos, t in enumerate(translationToPerform): ds[pos] = d[:] # Perform a deep copy of d and place it at position 'pos' in ds ds[pos] += t # Adding a vector to a set of coordinates does a translation pass # Identifying duplicate tuples d2 = mc.DataArrayDouble.Aggregate(ds) oldNbOfTuples = d2.getNumberOfTuples() c, cI = d2.findCommonTuples(1e-12) tmp = c[cI[0]:cI[0 + 1]] print tmp a = cI.deltaShiftIndex() b = a - 1 myNewNbOfTuples = oldNbOfTuples - sum(b.getValues()) o2n, newNbOfTuples = mc.DataArrayInt.ConvertIndexArrayToO2N( oldNbOfTuples, c, cI) print "Have I got the right number of tuples?" print "myNewNbOfTuples = %d, newNbOfTuples = %d" % (myNewNbOfTuples, newNbOfTuples) assert (myNewNbOfTuples == newNbOfTuples) print "Old number of tuple was ", oldNbOfTuples # Extracting the unique set of tuples d3 = d2.renumberAndReduce(o2n, newNbOfTuples) n2o = o2n.invertArrayO2N2N2O(newNbOfTuples) d3_bis = d2[n2o] print "Are d3 and d3_bis equal ? %s" % (str(d3.isEqual(d3_bis, 1e-12))) # Now build an unstructured mesh representing the final pattern mesh = mc.MEDCouplingUMesh(mesh_name, 2) mesh.setCoords(d3) print "Mesh dimension is", mesh.getMeshDimension() print "Spatial dimension is", mesh.getCoords().getNumberOfComponents() mesh.allocateCells(ncells) for i in xrange(ncells): cell_connec = o2n[6 * i:6 * (i + 1)] mesh.insertNextCell(mc.NORM_POLYGON, cell_connec.getValues()) pass # Check that everything is coherent (will throw if not) mesh.checkConsistencyLight() # Crée les éléments 1D pour pouvoir imposer les conditions aux limites mesh_1d = mesh.computeSkin() # Trie les cellules par type conformément à la convention MED fichier o2n = mesh.sortCellsInMEDFileFrmt() meshMEDFile = ML.MEDFileUMesh.New() # Ecrit le maillage 2D meshMEDFile.setMeshAtLevel(0, mesh) # Ecrit le maillage 1D meshMEDFile.setMeshAtLevel(-1, mesh_1d) # Ecrit les groupes arr_circle = mc.DataArrayInt(range(mesh_1d.getNumberOfCells())) arr_circle.setName("Circle") meshMEDFile.addGroup(-1, arr_circle) filename = mesh_name + ".med" # Write the result into a VTU file that can be read with ParaView #mesh.writeVTK(mesh_name".vtu") # Write the result into a MED file that can be read with Salomé meshMEDFile.write(filename, 2) # 2 stands for write from scratch