]> SALOME platform Git repositories - tools/solverlab.git/blob - CDMATH/tests/ressources/scripts/convert_typ_to_med.py
Salome HOME
update for MEDCoupling 64 bits
[tools/solverlab.git] / CDMATH / tests / ressources / scripts / convert_typ_to_med.py
1 import MEDLoader as ml\r
2 import os\r
3 \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
8 \r
9     som = [map(float, line.split()) for line in lines[2:2 + nb_som]]\r
10 \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
14 \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
19 \r
20     pts = []\r
21     for p in som: pts.extend(p)\r
22     co = ml.DataArrayDouble(pts, len(pts) / 2, 2)\r
23     mesh.setCoords(co)\r
24 \r
25     mf, d, di, r, ri = mesh.buildDescendingConnectivity()\r
26     mf.setName("mesh")\r
27     mm = ml.MEDFileUMesh.New()\r
28     mm.setMeshAtLevel(0, mesh)\r
29     mm.setMeshAtLevel(-1, mf)\r
30 \r
31     g = []\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
38 \r
39     mm.write("{}/mesh.med".format(nom_med), 2)\r
40 \r
41 def read_between(infile, patern1, patern2):\r
42     with open(infile) as fic:\r
43         copy = False\r
44         string = ''\r
45         for line in fic:\r
46             if any([line.strip().lower().startswith(patern.lower()) for patern in patern1]):\r
47                 copy = True\r
48             elif any([line.strip().lower().startswith(patern.lower()) for patern in patern2]):\r
49                 copy = False\r
50             elif copy:\r
51                 string += line\r
52     return string\r
53 \r
54 def connectivity_from_string(string):\r
55     indicies = map(int, string.split())\r
56     tab = []\r
57     off = 0\r
58     while off < len(indicies):\r
59         off_old = off + 1\r
60         off += indicies[off] + 1\r
61         tab.append(indicies[off_old:off])\r
62     return tab\r
63 \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
69 \r
70     som = [map(float, line.split()) for line in lines[17:17 + nb_som]]\r
71 \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
77 \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
82 \r
83     mesh = ml.MEDCouplingUMesh("mesh", 3)\r
84     mesh.allocateCells(len(cel))\r
85 \r
86     for e in range(len(cel)):\r
87         con = []\r
88         for face in cel[e]:\r
89             con.extend(fac[face])\r
90             con.append(-1)\r
91         mesh.insertNextCell(ml.NORM_POLYHED, con[:-1])\r
92     mesh.finishInsertingCells()\r
93 \r
94     pts = []\r
95     for p in som: pts.extend(p)\r
96     co = ml.DataArrayDouble(pts, len(pts) / 3, 3)\r
97     mesh.setCoords(co)\r
98 \r
99     mf, d, di, r, ri = mesh.buildDescendingConnectivity()\r
100     mf.setName("mesh")\r
101     mm = ml.MEDFileUMesh.New()\r
102     mm.setMeshAtLevel(0, mesh)\r
103     mm.setMeshAtLevel(-1, mf)\r
104 \r
105     g = []\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
112 \r
113     mm.write("{}/mesh.med".format(nom_med), 2)\r
114 \r
115 # maillages 3D\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
125     for n in d:\r
126         print t, n\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