@@ -31,32 +31,56 @@ where the lumping should occur.
3131Outside of this region, no lumping.
3232Default is `f=Returns(true)`, i.e. lump everywhere.
3333"""
34- function lump_and_spray (wet3D, vol, mask= trues (size (wet3D)); di= 2 , dj= 2 , dk= 1 )
34+ function lump_and_spray (wet3D, vol, T, mask= trues (size (wet3D)); di= 2 , dj= 2 , dk= 1 )
3535
3636 # extend the grid to avoid lumping cells outside of bounds
3737 nxyz = size (wet3D)
3838 C = CartesianIndices (nxyz)
3939 LUMPidx = zeros (Int, nxyz .+ (di - 1 , dj - 1 , dk - 1 ))
4040
41+ wet = wet3D[:]
42+ i, j = findnz (T)
43+ L = LinearIndices (nxyz .+ (di - 1 , dj - 1 , dk - 1 ))
44+ wet3Dext = falses (nxyz .+ (di - 1 , dj - 1 , dk - 1 ))
45+ wet3Dext[C[wet3D]] .= true
46+ Lwet = L[wet3Dext] # careful here, as L is larger than wet3D
47+
48+ connectivitymatrix = sparse (Lwet[i], Lwet[j], true , length (LUMPidx), length (LUMPidx))
49+
4150 # loop once over all grid cells and assign lumped indices
42- c = 1 # index / counter
51+ c = 2 # index / counter (we start at 2 because 1 is reserved for dry cells)
4352 neighbours = CartesianIndices ((0 : di- 1 , 0 : dj- 1 , 0 : dk- 1 ))
4453 for 𝑖 in eachindex (C)
4554 C𝑖 = C[𝑖]
46- LUMPidx[C𝑖] ≠ 0 && mask[C𝑖] && continue # skip if index already assigned
55+ # skip if index already assigned and in mask
56+ # (if assigned but not in mask, must reassign)
57+ LUMPidx[C𝑖] > 0 && mask[C𝑖] && continue
4758 if mask[C𝑖] # if in region, assign all neighbours also in region
48- LUMPidx[C𝑖 .+ neighbours] .= c
49- else # to only this cell if outside region
59+ # list of neighbours
60+ L𝑖 = L[C𝑖 .+ neighbours][:]
61+ # First, assign dry index to dry cells
62+ localwet = wet3Dext[L𝑖]
63+ dryidx = L𝑖[.! localwet]
64+ LUMPidx[dryidx] .= 1
65+ # Then build a simple connectivity graph of the remaining cells to be lumped
66+ wetidx = L𝑖[localwet]
67+ localconnectivitymatrix = view (connectivitymatrix, wetidx, wetidx)
68+ g = SimpleGraph (localconnectivitymatrix)
69+ # and split it in connected components
70+ for comp in connected_components (g)
71+ LUMPidx[wetidx[comp]] .= c
72+ c += 1
73+ end
74+ else # if outside region, assign new index
5075 LUMPidx[C𝑖] = c
76+ c += 1
5177 end
52- c += 1
5378 end
5479
5580 # Remove ghost cells outside of original grid
5681 LUMP = sparse (LUMPidx[C][:], 1 : length (C), 1 )
5782
5883 # Find wet points in coarsened grid
59- wet = wet3D[:]
6084 wet_c = LUMP * wet .> 0
6185
6286 # Extract only indices of wet grd points
@@ -84,6 +108,18 @@ function lump_and_spray(wet3D, vol, mask=trues(size(wet3D)); di=2, dj=2, dk=1)
84108end
85109
86110
111+ """
112+ diconnected_cells(wet3D, T; di=2, dj=2, dk=1)
113+
114+ """
115+ function connected_cells (wet3D, T; di= 2 , dj= 2 , dk= 1 )
116+ i, j = findnz (SPRAY * LUMP)
117+ connected_by_grid = sparse (i, j, true , size (T)... )
118+ i, j = findnz (T)
119+ connected_by_T = sparse (i, j, true , size (T)... )
120+ connected = connected_by_grid .& connected_by_T
121+ connected = ((connected^ di)^ dj)^ dk
122+ end
87123
88124
89125
0 commit comments