-
Notifications
You must be signed in to change notification settings - Fork 2
/
meshColoring.py
63 lines (51 loc) · 2.06 KB
/
meshColoring.py
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
import numpy as np
import os.path
from mesh import *
# last color is reserved
def MeshColoring(name, mesh, nColors=23, maxNeighbors=50):
nNodes = mesh.lclNNodes
nElms = mesh.nElements
elmNodeIds = mesh.lclElmNodeIds
# Try to load the coloring result first.
if os.path.exists('{}.npz'.format(name)):
colorData = np.load('{}.npz'.format(name), allow_pickle=True)
mesh.elmColors = colorData['elmColors']
mesh.colorGroups = colorData['colorGroups']
return 0
# If failed, generate the color group and save in file.
# 1. Find the neighbors.
nodeElmNeighbors = np.empty((nNodes, maxNeighbors), dtype=int)
nodeElmNeighborsCount = np.zeros(nNodes, dtype=int)
for iElm in range(nElms):
for iNode in elmNodeIds[iElm]:
nodeElmNeighbors[iNode,nodeElmNeighborsCount[iNode]] = iElm
nodeElmNeighborsCount[iNode] += 1
neighbors = [[] for _ in range(nElms)]
for iElm in range(nElms):
for iNode in elmNodeIds[iElm]:
neighbors[iElm].extend(nodeElmNeighbors[iNode,:nodeElmNeighborsCount[iNode]])
# Clear the redundent neighbors.
neighbors = np.array(neighbors)
for i in range(nElms):
neighbors[i] = np.unique(neighbors[i])
# 2. Label the colors.
elmColors = np.empty(nElms)
neighborColors = [[] for _ in range(nElms)]
for i in range(nElms):
for iColor in range(nColors+1):
if iColor not in neighborColors[i]:
break
if iColor == nColors:
print('Colors are used up! {} colors are not enough.'.format(nColors))
return -1
elmColors[i] = iColor
for iNeighbor in neighbors[i]:
neighborColors[iNeighbor].append(iColor)
# Put each group together.
nColors = int(elmColors.max()) + 1
colorGroups = np.array([np.where(elmColors == iColor)[0] for iColor in range(nColors)])
# 3. Save the result into files.
mesh.elmColors = elmColors
mesh.colorGroups = colorGroups
np.savez(name, elmColors=elmColors, colorGroups=colorGroups)
return 0