6
surface_ids=[] # surface ids of the edges
9
region_ids=[] # region ids of the elements
10
edge_attribute1=[] # first edge attribute
15
def read_nodefile(filename):
16
f = open(filename, 'r')
21
def read_elefile(filename):
22
f = open(filename, 'r')
27
def read_edgefile(filename):
28
f = open(filename, 'r')
29
global edges, surface_ids, edge_attribute1
30
[edges, surface_ids, edge_attribute1]=pedges(f)
33
def save_nodefile(nodes, dim, filename):
34
f = open(filename, 'w')
36
f.write(`len(nodes)`+' '+`dim`+' 0 0\n')
39
f.write(`k`+' ' + ''.join([`n`+' ' for n in node])+'\n')
41
f.write('# Created with triangle.py')
44
def save_elefile(eles, par1, filename):
45
f = open(filename, 'w')
46
f.write(`len(eles)`+' '+`len(eles[0])`+' 1\n')
47
if len(par1)!=len(eles):
48
print "Error in save_elefile(): Length of element list is ", len(eles), " but length of region_id list is: ", len(par1)
52
f.write(`k`+' ' + ''.join([`e`+' ' for e in ele])+`par1[k-1]`+'\n')
54
f.write('# Created with triangle.py')
58
def save_edgefile(edges, atr1, filename):
59
f = open(filename, 'w')
60
f.write(`len(edges)`+' 1\n')
61
if len(atr1)!=len(edges):
62
print "Error in save_edgefile(): Length of edge list is ", len(edges), " but length of atr1 list is: ", len(par1)
66
f.write(`k`+' ' + ''.join([`e`+' ' for e in edge])+`atr1[k-1]`+'\n')
68
f.write('# Create with triangly.py')
71
def save_edgefile2(edges, atr1, atr2, filename):
72
f = open(filename, 'w')
73
f.write(`len(edges)`+' 2\n')
74
if len(atr1)!=len(edges) or len(atr2)!=len(edges):
75
print "Error in save_edgefile(): Length of edge list is ", len(edges), " but length of atr1, atr2 list is: ", len(atr1), ', ', len(atr2)
79
f.write(`k`+' ' + ''.join([`e`+' ' for e in edge])+`atr1[k-1]`+' '+ `atr2[k-1]`+'\n')
81
f.write('# Create with triangle.py')
85
""" Parse the .node file
86
and returns the correspondind
104
nbnodes = int(parts[0])
108
print "Error dim in .node file!"
112
nodes.append(([float(x) for x in parts[1:]]))
114
nodes.append(([float(x) for x in parts[1:3]]))
115
if (len(nodes) != nbnodes):
116
print "Error: actual number of nodes does not corresponds " + str(len(nodes)), nbnodes
122
""" Parse the .edge file and returns the corresponding
143
nbedges = int(parts[0])
144
boundary = int(parts[1])
147
print "Error: too many attributes in .edge file!"
150
edges.append(([int(x) for x in parts[1:3]]))
152
surface_ids.append(int(parts[3]))
154
attribute1.append(int(parts[4]))
156
if (len(edges) != nbedges):
157
print "Error: actual number of edges does not corresponds " + str(len(edges)), str(nbedges)
160
return [edges, surface_ids, attribute1]
163
""" Parse the .ele file and returns the corresponding
178
nbeles = int(parts[0])
179
nodesperele = int(parts[1])
180
region = int(parts[2])
184
eles.append(([int(x) for x in parts[1:nodesperele+1]]))
186
region_ids.append(int(parts[len(parts)-1]))
188
if (len(eles) != nbeles):
189
print "Error: actual number of eles does not corresponds " + str(len(eles)), str(nbeles)
194
# Returns array of edge indices whose boundary marker ids equal bid
195
def edges_with_bid(bid):
199
for i in range(0,len(surface_ids)):
200
if surface_ids[i] == bid:
204
# Returns array of ele indices which have the given edge id
205
def ele_with_edgeids(edgeids):
206
global edges, eles, nodes
208
nodeids=edges[edgeids][0:len(edges[edgeids])] # get nodeids of edgeid
209
# loop through all elements and check if they include all these nodes
212
if Set(nodeids).issubset(Set(ele[0:len(ele)])):
216
print "There are ", len(ret), " elements adjacent to face with id ", edgeids, ". Only 2 were expected."
220
# Returns array of ele indices which have the given node ids
221
def ele_with_nodeids(nodeids):
224
# loop through all elements and check if they include all these nodes
227
if Set(nodeids).issubset(Set(ele[0:len(ele)])):
231
print "There are ", len(ret), " elements adjacent to nodes with ids ", nodeids, ". Only 2 were expected."
235
# returns the edges with nodeids
236
def edgeid_from_nodeids(nodeids):
241
if Set(nodeids).issubset(Set(edge[0:len(edge)])):
246
# returns the edge with boundary_id and wit node nodeid but not with id edgeid
247
def get_partner_edge(edgein, nodeid, boundary_id):
249
cands=edges_with_bid(boundary_id)
251
if edge!=edgein and (nodeid in edges[edge]):
254
def get_boundaryid_from_edgeid(edgeid):
256
return surface_ids[edgeid]
258
def has_ele_edge_on_boundaryid(elein, nodeid, boundary_id):
260
for n in eles[elein]:
263
edge=edgeid_from_nodeids([n,nodeid])
264
if edge!=[] and get_boundaryid_from_edgeid(edge[0])==boundary_id:
266
print "Found boundary edge with id", edgeid_from_nodeids([n,nodeid])[0]+1
270
def get_eles_on_ele_side(elein, nodeid, edgein, boundary_id):
272
if not has_ele_edge_on_boundaryid(elein, nodeid, boundary_id):
273
print "Error in get_eles_on_ele_side: Given Element has no edge with secified boundary id"
276
thirdnode=Set(eles[elein]).difference(Set(edges[edgein])).pop()
279
print "Found third node of current element wit index: ", thirdnode
280
print "Looking for neighbour with nodes ", thirdnode, nodeid
281
partner_eleins=ele_with_nodeids([thirdnode, nodeid])
282
if partner_eleins[0]==elein:
283
partner_elein=partner_eleins[1]
285
partner_elein=partner_eleins[0]
287
print "Found neighbour with eleid", partner_elein+1
288
ret.append(partner_elein)
289
if has_ele_edge_on_boundaryid(partner_elein, nodeid, boundary_id):
291
print "Reached the end of the element loop"
294
print "New elemnt has no desired boundary edge, loop again"
296
thirdnode=Set(eles[elein]).difference(Set([nodeid, thirdnode])).pop()
299
# deletes the node with id nodeid and updates the elelist and edgelist
300
def delete_nodeid(nodeid):
301
global eles, edges, nodes
302
for index, ele in enumerate(eles):
304
print "Error in delete_nodeid(", nodeid, "). This node is still in use in element ", index+1, ": ", ele, "."
306
eles[index]=[x if x<nodeid else x-1 for x in eles[index]]
307
for index, edge in enumerate(edges):
309
print "Error in delete_nodeid(", nodeid, "). This node is still in use in edge ", index+1, "."
311
edges[index]=[x if x<nodeid else x-1 for x in edges[index]]
314
# deletes the ele with id eleid
315
def delete_eleid(eleid):
316
global eles, region_ids
318
region_ids.pop(eleid-1)
320
# deletes the edge with id edgeid
321
def delete_edgeid(edgeid):
322
global edges, surface_ids
324
surface_ids.pop(nodeid-1)
325
edge_attribute1.pop(nodeid-1)