Skip to content

Commit e66b86f

Browse files
committed
Use 3D array mask instead of function
1 parent 6f8c3b7 commit e66b86f

3 files changed

Lines changed: 22 additions & 12 deletions

File tree

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "OceanTransportMatrixBuilder"
22
uuid = "c2b4a04e-6049-4fc4-aa6a-5508a29a1e1c"
33
authors = ["Benoit Pasquier <briochemc@gmail.com> and contributors"]
4-
version = "0.6.1"
4+
version = "0.7.0"
55

66
[deps]
77
Distances = "b4f34e82-e78d-54a5-968a-f98e89d6e8f7"

src/extratools.jl

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,29 +31,28 @@ where the lumping should occur.
3131
Outside of this region, no lumping.
3232
Default is `f=Returns(true)`, i.e. lump everywhere.
3333
"""
34-
function lump_and_spray(wet3D, vol; f=Returns(true), di=2, dj=2, dk=1)
34+
function lump_and_spray(wet3D, vol, 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)
38+
C = CartesianIndices(nxyz)
3839
LUMPidx = zeros(Int, nxyz .+ (di - 1, dj - 1, dk - 1))
3940

4041
# loop once over all grid cells and assign lumped indices
4142
c = 1 # index / counter
4243
neighbours = CartesianIndices((0:di-1, 0:dj-1, 0:dk-1))
43-
C = CartesianIndices(nxyz)
4444
for 𝑖 in eachindex(C)
4545
C𝑖 = C[𝑖]
46-
i, j, k = C𝑖.I
47-
# assign index if not indexed yet
48-
if LUMPidx[C𝑖] == 0
49-
if f(i,j,k) # to all lumped cells if in region
50-
LUMPidx[C𝑖 .+ neighbours] .= c
51-
else # to only this cell if outside region
52-
LUMPidx[C𝑖] = c
53-
end
54-
c += 1
46+
LUMPidx[C𝑖] 0 && mask[C𝑖] && continue # skip if index already assigned
47+
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
50+
LUMPidx[C𝑖] = c
5551
end
52+
c += 1
5653
end
54+
55+
# Remove ghost cells outside of original grid
5756
LUMP = sparse(LUMPidx[C][:], 1:length(C), 1)
5857

5958
# Find wet points in coarsened grid
@@ -74,6 +73,13 @@ function lump_and_spray(wet3D, vol; f=Returns(true), di=2, dj=2, dk=1)
7473
SPRAY = copy(LUMP')
7574
SPRAY.nzval .= 1
7675

76+
# Print some info
77+
nwet = size(LUMP, 2)
78+
nwet_c = size(LUMP, 1)
79+
@info """LUMP and SPRAY:
80+
Matrix size reduction: $(round(100(1 - nwet_c/nwet)))% ($nwet -> $nwet_c)
81+
"""
82+
7783
return LUMP, SPRAY, vol_c
7884
end
7985

test/local_full.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ end
130130
v = v3D[wet3D]
131131

132132
@info "coarsening grid"
133+
SOmask = lat.data .< -35
134+
NAmask = @. (lat.data > 50) & ((lon.data < 100) | (250 < lon.data))
135+
mymask = repeat(.!SOmask .& .!NAmask, 1, 1, size(wet3D, 3))
136+
LUMP, SPRAY, v_c = OceanTransportMatrixBuilder.lump_and_spray(wet3D, v, mymask; di=2, dj=2, dk=1)
133137
LUMP, SPRAY, v_c = OceanTransportMatrixBuilder.lump_and_spray(wet3D, v; di=2, dj=2, dk=1)
134138

135139
# surface mask

0 commit comments

Comments
 (0)