-
Notifications
You must be signed in to change notification settings - Fork 2
/
coloring.jl
80 lines (73 loc) · 2.43 KB
/
coloring.jl
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
# Greedy algorithm for coloring a grid such that no two cells with the same node
# have the same color
function create_coloring(g::Grid)
# Contains the elements that each node contain
cell_containing_node = Dict{Int, Set{Int}}()
for (cellid, cell) in enumerate(g.cells)
for v in cell.nodes
if !haskey(cell_containing_node, v)
cell_containing_node[v] = Set{Int}()
end
push!(cell_containing_node[v], cellid)
end
end
I, J, V = Int[], Int[], Bool[]
for (node, cells) in cell_containing_node
for cell1 in cells # All these cells have a neighboring node
for cell2 in cells
if cell1 != cell2
push!(I, cell1)
push!(J, cell2)
push!(V, true)
end
end
end
end
incidence_matrix = sparse(I, J, V)
# cell -> color of cell
cell_colors = Dict{Int, Int}()
# color -> list of cells
final_colors = Vector{Int}[]
occupied_colors = Set{Int}()
# Zero represents no color set yet
for cellid in 1:length(g.cells)
cell_colors[cellid] = 0
end
total_colors = 0
for cellid in 1:length(g.cells)
empty!(occupied_colors)
# loop over neighbors
for r in nzrange(incidence_matrix, cellid)
cell_neighbour = incidence_matrix.rowval[r]
color = cell_colors[cell_neighbour]
if color != 0
push!(occupied_colors, color)
end
end
# occupied colors now contains all the colors we are not allowed to use
free_color = 0
for attempt_color in 1:total_colors
if attempt_color ∉ occupied_colors
free_color = attempt_color
break
end
end
if free_color == 0 # no free color found, need to bump max colors
total_colors += 1
free_color = total_colors
push!(final_colors, Int[])
end
cell_colors[cellid] = free_color
push!(final_colors[free_color], cellid)
end
return cell_colors, final_colors
end
function vtk_cell_data_colors(vtkfile, grid, cell_colors)
color_vector = zeros(getncells(grid))
for (i, cells_color) in enumerate(cell_colors)
for cell in cells_color
color_vector[cell] = i
end
end
vtk_cell_data(vtkfile, color_vector, "coloring")
end