def createMesh(nb_segs_x, x_start=0, x_end=1., y_start=0., mesh_name="Mesh"): mesh_dim = 2 nb_nodes = nb_segs_x + 1 dx = (x_end - x_start) / nb_segs_x mesh = MC.MEDCouplingIMesh(mesh_name, mesh_dim, [nb_nodes, nb_nodes], [x_start, y_start], [dx] * mesh_dim) return mesh
def create2DGrid(xmin, xmax, nx, ymin, ymax, ny, mesh_name="Mesh_rectangle_with_rectangles"): mesh_dim = 2 dx = (xmax - xmin) / nx dy = (ymax - ymin) / ny mesh = mc.MEDCouplingIMesh(mesh_name, mesh_dim, [nx + 1, ny + 1], [xmin, ymin], [dx, dy]) return mesh
def create3DGrid(xmin, xmax, nx, ymin, ymax, ny, zmin, zmax, nz, mesh_name="Mesh_cube_with_cuboids"): mesh_dim = 3 dx = (xmax - xmin) / nx dy = (ymax - ymin) / ny dz = (zmax - zmin) / nz mesh = mc.MEDCouplingIMesh(mesh_name, mesh_dim, [nx + 1, ny + 1, nz + 1], [xmin, ymin, zmin], [dx, dy, dz]) return mesh
def mesh_rectangle_with_rectangles(xmin, xmax, nx, ymin, ymax, ny, mesh_name="Mesh_rectangle_with_rectangles"): mesh_dim = 2 dx = (xmax - xmin) / nx dy = (ymax - ymin) / ny mesh = mc.MEDCouplingIMesh(mesh_name, mesh_dim, [nx + 1, ny + 1], [xmin, ymin], [dx, dy]).buildUnstructured() #--------------- Boundary groups -----------------# # 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 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) < tol: ids_left.append(i) elif abs(x - xmax) < tol: ids_right.append(i) elif abs(y - ymin) < tol: ids_bottom.append(i) elif abs(y - ymax) < 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