3 #### Maillage d'un rectangle par une grille décalée (mur de briques)
4 ### input : xmin, xmax, nx, ymin, ymax, ny
5 ### output : squareWithBrickWall.vtu squareWithBrickWall.med
7 import medcoupling as mc
11 def createBrickWallMesh( xmin=0., xmax=1., nx=15, ymin=0., ymax=1., ny=15,mesh_name="squareWithBrickWall"):
16 print( "Creating BrickWall mesh with nx=",nx,"ny=",ny, "nb cells=",nx*ny )
18 # Building the initial rectangular cell, centered at 0,0
19 d = mc.DataArrayDouble(4,2)
29 d.setInfoOnComponents(["X [m]","Y [m]"])
31 print( "Uniform array ?", d.magnitude().isUniform(0.5*math.sqrt(dx*dx+dy*dy),1e-10) )
33 # translation of the first cell
34 translationToPerform = [[(0.5*(1+j%2)+i)*dx,(0.5+j)*dy] for i in range(nx) for j in range(ny)]
36 ds = len(translationToPerform)*[None]
37 for pos,t in enumerate(translationToPerform):
38 ds[pos] = d[:] # Perform a deep copy of d and place it at position 'pos' in ds
39 ds[pos] += t # Adding a vector to a set of coordinates does a translation
42 d2 = mc.DataArrayDouble.Aggregate(ds)
43 # Build an unstructured mesh representing the final pattern
44 mesh = mc.MEDCouplingUMesh(mesh_name,2)
46 print( "Mesh dimension is", mesh.getMeshDimension() )
47 print( "Spatial dimension is", mesh.getCoords().getNumberOfComponents() )
48 mesh.allocateCells(nx*ny)
49 for i in range(nx*ny):
50 cell_connec = [4*i,4*i+1,4*i+2,4*i+3]
51 mesh.insertNextCell(mc.NORM_QUAD4, cell_connec)
54 # Identifying duplicate nodes
55 oldNbOfNodes=mesh.getNumberOfNodes()
56 arr, areNodesMerged, newNbOfNodes=mesh.mergeNodes(1e-10)
57 print( "oldNbOfNodes=",oldNbOfNodes,"newNbOfNodes",newNbOfNodes )
59 # Crée des polygones pour rendre conforme les mailles
60 mesh.conformize2D(1e-10)
61 # Check that everything is coherent (will throw if not)
62 mesh.checkConsistencyLight()
64 # Crée les éléments 1D pour pouvoir imposer les conditions aux limites
65 mesh_1d = mesh.computeSkin()
67 # Identifie les segments de chaque côté pour créer les groupes
70 # PB: getCellsInBoundingBox renvoie aussi les segments qui touchent la bounding box
71 # => On boucle sur les coordonnées des barycentres
73 barycenters = mesh_1d.computeIsoBarycenterOfNodesPerCell()
78 for i, coord in enumerate(barycenters):
80 if abs(y-ymin) < tol :
82 elif abs(y-ymax) < tol :
84 elif abs(x-xmax) < tol or abs(x-xmax-dx/4) < tol or abs(x-xmax-dx/2) < tol:
86 elif abs(x-xmin) < tol or abs(x-xmin-dx/4) < tol or abs(x-xmin-dx/2) < tol:
89 raise ValueError("Pb with boundary construction : barycenter does not belong to any border group")
91 arr_left = mc.DataArrayInt64(ids_left)
92 arr_right = mc.DataArrayInt64(ids_right)
93 arr_bottom = mc.DataArrayInt64(ids_bottom)
94 arr_top = mc.DataArrayInt64(ids_top)
96 arr_left.setName("Left")
97 arr_right.setName("Right")
98 arr_bottom.setName("Bottom")
99 arr_top.setName("Top")
101 # Trie les cellules par type conformément à la convention MED fichier
102 o2n = mesh.sortCellsInMEDFileFrmt()
103 meshMEDFile=ML.MEDFileUMesh.New()
104 # Ecrit le maillage 2D
105 meshMEDFile.setMeshAtLevel(0,mesh)
106 # Ecrit le maillage 1D
107 meshMEDFile.setMeshAtLevel(-1,mesh_1d)
109 meshMEDFile.addGroup(-1, arr_left)
110 meshMEDFile.addGroup(-1, arr_right)
111 meshMEDFile.addGroup(-1, arr_bottom)
112 meshMEDFile.addGroup(-1, arr_top)
114 filename = mesh_name+str(nx*ny)+".med"
115 # Write the result into a VTU file that can be read with ParaView
116 #mesh.writeVTK(mesh_name+str(nx*ny)+".vtu")
117 # Write the result into a MED file that can be read with Salomé
118 meshMEDFile.write(filename,2) # 2 stands for write from scratch
120 if __name__ == '__main__':
121 createBrickWallMesh(0.,1.,5 ,0.,1.,5)
122 createBrickWallMesh(0.,1.,15,0.,1.,15)
123 createBrickWallMesh(0.,1.,31,0.,1.,31)
124 createBrickWallMesh(0.,1.,61,0.,1.,61)
125 createBrickWallMesh(0.,1.,101,0.,1.,101)