mesh.py 7.99 KB
Newer Older
Jonathan Lambrechts's avatar
Jonathan Lambrechts committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
from . import gmshType
import time

class Mesh() :
  class element():
    def __init__(self, etype, vertices, partition, tag):
      self.etype = etype
      self.vertices = vertices
      self.partition = partition
      self.tag = tag

    def genFaces(self) :
      v = self.vertices
      order = self.etype.order
      if self.etype.baseType == gmshType.TYPE_TRI :
        return [Mesh.element(gmshType.TYPE_LIN.mshType[order], [v[i], v[(i + 1) % 3]] + v[3 + i * (order-1):3 + (i+1) * (order-1)], self.partition, -1) for i in range(3)]
      elif self.etype.baseType == gmshType.TYPE_QUA :
        return [Mesh.element(gmshType.TYPE_LIN.mshType[order], [v[i], v[(i + 1) % 4]] + v[4 + i * (order-1):4 + (i+1) * (order-1)], self.partition, -1) for i in range(4)]
      elif self.etype.baseType == gmshType.TYPE_LIN :
        return [Mesh.element(gmshType.MSH_PNT, [v[i]], self.partition, -1) for i in range(2)]

  class entity():
    def __init__(self, tag, dim, physicals) :
      self.tag = tag
      self.dimension = dim
      self.physicals = physicals
      self.elements = []
      
  def __init__(self, filename = None) :
    self.vertices = []
    self.physicals = [{}, {}, {}, {}]
    self.entities = []
    self.maxeid = 0
    self.useFormat3 = False
    if filename :
      self.read(filename)

  def write(self, filename, format3=None) :
    output = open(filename, "w")
    if format3 == None :
      format3 = self.useFormat3
    if format3 :
      output.write("$MeshFormat\n3 0 8\n$EndMeshFormat\n")
      output.write("$PhysicalNames\n%i\n" % (len(self.physicals[0]) + len(self.physicals[1]) + len(self.physicals[2]) + len(self.physicals[3])))
      for dim, plist in enumerate(self.physicals) :
        for name, tag in plist.items() :
          output.write("%i %i \"%s\"\n" % (dim, tag, name))
      output.write("$EndPhysicalNames\n")
      output.write("$Entities\n%i\n" % (len(self.entities)))
      for e in self.entities :
        output.write("%i %i %i %s\n" % (e.tag, e.dimension, len(e.physicals), " ".join(repr(ip) for ip in e.physicals)))
      output.write("$EndEntities\n")
      output.write("$Nodes\n%d\n" % len(self.vertices))
      for (x, y, z, j) in self.vertices :
        output.write("%i %.16g %.16g %.16g 0\n" % (j, x, y, z))
      output.write("$EndNodes\n")
      output.write("$Elements\n%d\n" % sum ([len(e.elements) for e in self.entities]))
      for entity in self.entities :
        for e in entity.elements :
          ntag = len(e.vertices) + ((1 + len(e.partition)) if e.partition else 0)
          spart = " ".join(repr(i) for i in [len(e.partition)] + e.partition) if e.partition else ""
          output.write("%i %i %i %i %s %s\n" % (e.tag, e.etype.tag, entity.tag, ntag, " ".join(repr(v[3]) for v in e.vertices), spart))
      output.write("$EndElements\n")
    else :
      output.write("$MeshFormat\n2.2 0 8\n$EndMeshFormat\n")
      output.write("$PhysicalNames\n%i\n" % (len(self.physicals[0]) + len(self.physicals[1]) + len(self.physicals[2]) + len(self.physicals[3])))
      for dim, plist in enumerate(self.physicals) :
        for name, tag in plist.items() :
          output.write("%i %i \"%s\"\n" % (dim, tag, name))
      output.write("$EndPhysicalNames\n")
      output.write("$Nodes\n%d\n" % len(self.vertices))
      for (x, y, z, j) in self.vertices :
        output.write("%i %.16g %.16g %.16g\n" % (j, x, y, z))
      output.write("$EndNodes\n")
      output.write("$Elements\n%d\n" % sum ([len(e.elements) for e in self.entities]))
      for entity in self.entities :
        for e in entity.elements :
          ntag = 2 + ((1 + len(e.partition)) if e.partition else 0)
          spart = " ".join(repr(i) for i in [len(e.partition)] + e.partition) if e.partition else ""
          ptag = entity.physicals[0] if entity.physicals else 0
          output.write("%i %i %i %i %i %s %s\n" % (e.tag, e.etype.tag, ntag, ptag, entity.tag, spart, " ".join(repr(v[3]) for v in e.vertices)))
      output.write("$EndElements\n")

  def read(self, filename):
    fin = open(filename, "r");
    l = fin.readline()
    vmap = {}
    entitymap = {}
    while l != "" :
      w = l.split()
      if w[0] == "$MeshFormat":
        l = fin.readline().split()
        if float(l[0]) == 3.:
          self.useFormat3 = True
        elif int(float(l[0])) == 2 :
          self.useFormat3 = False 
        else :
          print("error : cannot read mesh format " + l[0])
        l = fin.readline()
      elif w[0] == "$PhysicalNames" :
        n = int(fin.readline())
        for i in range(n) :
          dim, tag, name = fin.readline().split()
          self.physicals[int(dim)][name[1:-1]] = int(tag)
        fin.readline()
      elif w[0] == "$Entities" and self.useFormat3:
        n = int(fin.readline())
        for i in range(n) :
          l = fin.readline().split()
          j, dim, nphys = int(l[0]), int(l[1]), int(l[2])
          self.entities.append(Mesh.entity(j, dim, [int(ip) for ip in l[3:3+nphys]]))
          entitymap[(dim, j)] = self.entities[-1]
        fin.readline()
      elif w[0] == "$Nodes" :
        n = int(fin.readline())
        for i in range(n) :
          if self.useFormat3 :
            (j, x, y, z, t) = fin.readline().split()
          else :
            (j, x, y, z) = fin.readline().split()
          self.vertices.append([float(x), float(y), float(z), int(j)])
          vmap[int(j)] = self.vertices[-1]
      elif w[0] == "$Elements" :
        n = int(fin.readline())
        for i in range(n) :
          l = fin.readline().split()
          if self.useFormat3 :
            j, t, e, nf = int(l[0]), int(l[1]), int(l[2]), int(l[3])
            nv = gmshType.Type[t].numVertices
            vertices = [vmap[int(i)] for i in l[4:4+nv]]
            partition = [int(i) for i in l[5 + nv : 5 + nv + int(l[4 + nv])]] if nf > nv else []
          else :
            j, t, nf, p, e = int(l[0]), int(l[1]), int(l[2]), int(l[3]), int(l[4])
            vertices = [vmap[int(i)] for i in l[3 + nf:]]
            partition = [int(i) for i in l[6 : 6 + int(l[5])]] if nf > 2 else []
          edim = gmshType.Type[t].baseType.dimension
          entity = entitymap.get((edim, e), None)
          if not self.useFormat3 and not entity:
            entity = Mesh.entity(e, edim, [p])
            self.entities.append(entity)
            entitymap[(edim, e)] = entity
          entity.elements.append(Mesh.element(gmshType.Type[t], vertices, partition, j)) 
          self.maxeid = max(self.maxeid, j)
      l = fin.readline()

  def getPhysicalNumber(self, dim, name) :
    t = self.physicals[dim].get(name, None)
    if not t :
      self.physicals[dim][name] = max(self.physicals[dim].values()) + 1 if self.physicals[dim] else 1
      t = self.physicals[dim][name]
    return t

  def getPhysicalName(self, dim, number) :
    for name, num in self.physicals[dim].items() :
      if num == number :
        return name
    return ""

  def newVertex(self, x, y, z) :
    self.vertices.append((x, y, z, len(self.vertices) + 1))
    return self.vertices[-1]
 
  def newElement(self, t, partition, entity, vertices) :
    self.maxeid += 1
    entity.elements.append(Mesh.element(t, vertices, partition, self.maxeid))
    return entity.elements[-1]

  def newEntity(self, entityDim, physicalTag) :
    self.entities.append(Mesh.entity(len(self.entities) + 1, entityDim, [self.getPhysicalNumber(entityDim, physicalTag)]))
    return self.entities[-1]

  def genFaces(self, dim, physicalTag) :
    allf = {}
    for entity in self.entities :
      if entity.dimension == dim - 1 :
        for e in entity.elements :
          allf[tuple(sorted([v[3] for v in e.vertices]))] = e
    for entity in self.entities :
      if entity.dimension == dim  :
        vbentity = self.newEntity(dim - 1, physicalTag)
        for e in entity.elements :
          e.faces = e.genFaces()
          for i, f in enumerate(e.faces) :
            fs = tuple(sorted([v[3] for v in f.vertices]))
            if not fs in allf :
              f.tag = self.maxeid + 1
              allf[fs] = f
              self.maxeid += 1
              vbentity.elements.append(f)
            else :
              e.faces[i] = allf[fs]