Salome HOME
Updated and tested the python scripts for the conversion of Conference FVCA to MED...
[tools/solverlab.git] / CDMATH / tests / ressources / ConversionScripts / convert_2Dmsh_to_med.py
1 #!/usr/bin/env python
2 # -*- coding: UTF8 -*-
3
4 import sys
5
6 import medcoupling as MC
7 import MEDLoader as ML
8
9 if len(sys.argv) != 2:
10   print( "USAGE: convert_2Dgmsh_to_med.py file.typ2")
11   sys.exit(-1)
12
13 filename = sys.argv[1]
14
15 print( "Converting ", filename)
16
17 # type de maille en fonction du nombre de noeuds.
18 # cf INTERP_KERNEL/CellModel.cxx
19 d_cell_types = {3: MC.NORM_TRI3,
20                 4: MC.NORM_QUAD4,
21                 5: MC.NORM_POLYGON,
22                 6: MC.NORM_POLYGON}
23
24 mesh_dim = 2
25
26 read_vertices = False
27 coords = []
28
29 read_cells = False
30 read_new_cell_connectivity = False
31
32
33 vertex_line_number=False
34 cell_line_number=False
35
36 nb_vertices = 0
37 nb_cells = 0
38 nb_nodes_in_cell = 0
39 cell_connectivity = []
40
41 mesh = MC.MEDCouplingUMesh.New()
42 mesh.setMeshDimension(mesh_dim)
43 mesh.setName("mesh_from_typ")
44
45 with open(filename, 'r', encoding="iso-8859-1") as f:
46   for line in f:
47     # remove end of line character
48     line = line[:-1]
49     infos = line.split()
50     #print(infos)
51     if vertex_line_number:
52       print("Reading number of vertices")
53       nb_vertices = int(infos[0])
54       vertex_line_number = False
55       read_vertices = True
56       print("Start reading vertex coordinates")
57       continue
58     elif cell_line_number:
59       print("Reading number of cells")
60       nb_cells = int(infos[0])
61       mesh.allocateCells(nb_cells)
62       cell_line_number = False
63       read_cells = True
64       print("Start reading cell connectivity")
65       continue
66     elif infos and infos[0] == "Vertices":
67       vertex_line_number = True
68     elif infos and ( infos[0] == "cells" or infos[0] == "Control"):
69       cell_line_number = True
70       read_vertices = False
71     elif read_vertices:
72       # read node coords
73       coords_i = [float(v) for v in infos]
74       coords += coords_i
75     elif read_cells:
76       values = [int(v) for v in infos]
77       nb_nodes_in_cell = int(values[0])
78       cell_connectivity = values[1:]
79       #print( "nb_nodes_in_cell: ", nb_nodes_in_cell)
80       #print "cell_connectivity: ", cell_connectivity
81       # start numbering at 0
82       cell_connectivity = [v-1 for v in cell_connectivity]
83       mesh.insertNextCell(d_cell_types[nb_nodes_in_cell], nb_nodes_in_cell, cell_connectivity)
84       nb_nodes_in_cell = 0
85       cell_connectivity = []
86       
87   meshCoords = MC.DataArrayDouble.New()
88   #pdb.set_trace()
89   meshCoords.setValues(coords, nb_vertices, mesh_dim)
90   mesh.setCoords(meshCoords)
91
92
93 # Merge les noeuds confondus (à faire avant le conformize2D)
94 arr, areNodesMerged, newNbOfNodes = mesh.mergeNodes(1e-10)
95
96 # Crée des polyèdres pour rendre conforme les mailles
97 mesh.convertAllToPoly()
98 mesh.conformize2D(1e-10)
99 mesh.unPolyze()
100
101 # Crée les éléments 1D pour pouvoir imposer les conditions aux limites
102 mesh_1d = mesh.computeSkin()
103
104 # Identifie les faces de chaque côté pour créer les groupes
105 tol = 1e-10
106
107 barycenters = mesh_1d.computeIsoBarycenterOfNodesPerCell()
108 ids_left = []
109 ids_right = []
110 ids_bottom = []
111 ids_top = []
112 for i, coord in enumerate(barycenters):
113   x, y = coord
114   if abs(x) < tol:
115     ids_left.append(i)
116   elif abs(x-1) < tol:
117     ids_right.append(i)
118   elif abs(y) < tol:
119     ids_bottom.append(i)
120   elif abs(y-1) < tol:
121     ids_top.append(i)
122
123 arr_left = MC.DataArrayInt64(ids_left)
124 arr_right = MC.DataArrayInt64(ids_right)
125 arr_bottom = MC.DataArrayInt64(ids_bottom)
126 arr_top = MC.DataArrayInt64(ids_top)
127
128 arr_left.setName("Left")
129 arr_right.setName("Right")
130 arr_bottom.setName("Bottom")
131 arr_top.setName("Top")
132
133 # Trie les cellules par type conformément à la convention MED fichier
134 o2n = mesh.sortCellsInMEDFileFrmt()
135 o2n = mesh_1d.sortCellsInMEDFileFrmt()
136 meshMEDFile = ML.MEDFileUMesh.New()
137 # Ecrit le maillage 2D
138 meshMEDFile.setMeshAtLevel(0,mesh)
139 # Ecrit le maillage 1D
140 meshMEDFile.setMeshAtLevel(-1,mesh_1d)
141 # Ecrit les groupes
142 meshMEDFile.addGroup(-1, arr_left)
143 meshMEDFile.addGroup(-1, arr_right)
144 meshMEDFile.addGroup(-1, arr_bottom)
145 meshMEDFile.addGroup(-1, arr_top)
146 med_filename = filename.replace(".typ2", ".med")
147 meshMEDFile.write(med_filename,2) # 2 stands for write from scratch
148
149 print( "...done converting ", filename, " to ", med_filename)