1 import MEDLoader as ml
\r
4 def read_typ2(fichier, nom_med):
\r
5 with open(fichier, "r") as fic: lines = fic.readlines()
\r
6 nb_som = int(lines[1].strip())
\r
7 nb_cel = int(lines[3 + nb_som].strip())
\r
9 som = [map(float, line.split()) for line in lines[2:2 + nb_som]]
\r
11 # les connectivites ne partent pas de 0...
\r
12 tmp_cel = [map(int, line.split()[1:]) for line in lines[4 + nb_som:]]
\r
13 cel = [[p - 1 for p in c] for c in tmp_cel]
\r
15 mesh = ml.MEDCouplingUMesh("mesh", 2)
\r
16 mesh.allocateCells(len(cel))
\r
17 for p in cel: mesh.insertNextCell(ml.NORM_POLYGON, len(p), p)
\r
18 mesh.finishInsertingCells()
\r
21 for p in som: pts.extend(p)
\r
22 co = ml.DataArrayDouble(pts, len(pts) / 2, 2)
\r
25 mf, d, di, r, ri = mesh.buildDescendingConnectivity()
\r
27 mm = ml.MEDFileUMesh.New()
\r
28 mm.setMeshAtLevel(0, mesh)
\r
29 mm.setMeshAtLevel(-1, mf)
\r
32 nb_vois = ri.deltaShiftIndex()
\r
33 for i in range(mf.getNumberOfCells()):
\r
34 if nb_vois[i] == 1: g.append(i)
\r
35 grp = ml.DataArrayIdType.New(g)
\r
36 grp.setName("boundary")
\r
37 mm.addGroup(-1, grp)
\r
39 mm.write("{}/mesh.med".format(nom_med), 2)
\r
41 def read_between(infile, patern1, patern2):
\r
42 with open(infile) as fic:
\r
46 if any([line.strip().lower().startswith(patern.lower()) for patern in patern1]):
\r
48 elif any([line.strip().lower().startswith(patern.lower()) for patern in patern2]):
\r
54 def connectivity_from_string(string):
\r
55 indicies = map(int, string.split())
\r
58 while off < len(indicies):
\r
60 off += indicies[off] + 1
\r
61 tab.append(indicies[off_old:off])
\r
64 def read_typ3(fichier, nom_med):
\r
65 with open(fichier, "r") as fic: lines = fic.readlines()
\r
66 nb_som = int(lines[9].strip())
\r
67 nb_cel = int(lines[11].strip())
\r
68 nb_fac = int(lines[13].strip())
\r
70 som = [map(float, line.split()) for line in lines[17:17 + nb_som]]
\r
72 # les connectivites ne partent pas de 0...
\r
73 s = read_between(fichier, ["Volumes->Faces"], ["Volumes->Vertices"])
\r
74 tmp_cel = connectivity_from_string(s)
\r
75 # tmp_cel = [map(int, line.split()[1:]) for line in lines[18 + nb_som:18 + nb_som + nb_cel]]
\r
76 cel = [[p - 1 for p in c] for c in tmp_cel]
\r
78 s = read_between(fichier, ["Faces->Vertices"], ["Faces->Control volumes", "Faces->volumes"])
\r
79 tmp_fac = connectivity_from_string(s)
\r
80 # tmp_fac = [map(int, line.split()[1:]) for line in lines[21 + nb_som + 2 * nb_cel + nb_fac:21 + nb_som + 2 * nb_cel + 2 * nb_fac]]
\r
81 fac = [[p - 1 for p in c] for c in tmp_fac]
\r
83 mesh = ml.MEDCouplingUMesh("mesh", 3)
\r
84 mesh.allocateCells(len(cel))
\r
86 for e in range(len(cel)):
\r
89 con.extend(fac[face])
\r
91 mesh.insertNextCell(ml.NORM_POLYHED, con[:-1])
\r
92 mesh.finishInsertingCells()
\r
95 for p in som: pts.extend(p)
\r
96 co = ml.DataArrayDouble(pts, len(pts) / 3, 3)
\r
99 mf, d, di, r, ri = mesh.buildDescendingConnectivity()
\r
101 mm = ml.MEDFileUMesh.New()
\r
102 mm.setMeshAtLevel(0, mesh)
\r
103 mm.setMeshAtLevel(-1, mf)
\r
106 nb_vois = ri.deltaShiftIndex()
\r
107 for i in range(mf.getNumberOfCells()):
\r
108 if nb_vois[i] == 1: g.append(i)
\r
109 grp = ml.DataArrayIdType.New(g)
\r
110 grp.setName("boundary")
\r
111 mm.addGroup(-1, grp)
\r
113 mm.write("{}/mesh.med".format(nom_med), 2)
\r
116 meshes = (("meshAA-random", "RandMesh", ("4", "8", "16", "32")),
\r
117 #("meshBB_well", "WellMesh_", ("1", "2", "3", "4", "5", "6", "7")),
\r
118 #("meshB_tetra", "tet.", ("00", "0", "1", "2", "3", "4", "5", "6")),
\r
119 ("meshC_voro", "vmesh_", ("1", "2", "3", "4", "5")),
\r
120 ("meshD_kershaw", "dkershaw", ("08", "16", "32", "64")),
\r
121 ("meshF_dbls", "dbls_", ("10", "20", "30", "40")))
\r
122 # ("meshH_locrafgrid", "locrafgrid_", ("1", "2", "3", "4", "5")),
\r
123 # ("meshI_checkerboard", "checkerboard_", ("2x2x2", "4x4x4", "8x8x8", "16x16x16", "32x32x32")))
\r
124 for t, m, d in meshes:
\r
127 folder = "{}/jdd_{}".format(t, n)
\r
128 os.system("mkdir -p {}".format(folder))
\r
129 read_typ3("Meshes_3D/{}/{}{}.msh".format(t, m, n), folder)
\r