-
Notifications
You must be signed in to change notification settings - Fork 29
/
ModelPreconditioners.jl
256 lines (201 loc) · 8.55 KB
/
ModelPreconditioners.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
export judiIllumination, judiDepthScaling, judiTopmute
export DepthScaling, TopMute
"""
DepthScaling{T, N, K}
Depth scaling operator in `N` dimensions scaling by `depth^K`.
Constructor
===========
judiDepthScaling(model::AbstractModel; K=.5)
"""
struct DepthScaling{T, N, K} <: ModelPreconditioner{T, T}
m::Integer
depth::Array{T, N}
end
function judiDepthScaling(model::AbstractModel; K=.5f0)
N = length(size(model))
depth = reshape(range(0f0, stop=(size(model)[end] - 1) * spacing(model)[end], length=size(model)[end]), ones(Int64, N-1)..., :)
return DepthScaling{Float32, N, K}(prod(size(model)), depth)
end
matvec(D::DepthScaling{T, N, K}, x::Vector{T}) where {T, N, K} = vec(reshape(x, :, size(D.depth, N)) .* D.depth[:]'.^K)
matvec(D::DepthScaling{T, N, K}, x::AbstractArray{T, N}) where {T, N, K} = x .* D.depth.^K
matvec(D::DepthScaling{T, N, K}, x::PhysicalParameter{T}) where {T, N, K} = PhysicalParameter(x.n, x.d, x.o, x.data .* D.depth.^K)
matvec(D::DepthScaling{T, N, K}, x::judiWeights{T}) where {T, N, K} = judiWeights{T}(x.nsrc, [matvec(D, x.data[s]) for s=1:x.nsrc])
# Diagonal operator, self-adjoint
matvec_T(D::DepthScaling{T, N, K}, x) where {T, N, K} = matvec(D, x)
# Real diagonal operator
conj(I::DepthScaling{T}) where T = I
adjoint(I::DepthScaling{T}) where T = I
transpose(I::DepthScaling{T}) where T = I
inv(I::DepthScaling{T, N, K}) where {T, N, K} = DepthScaling{Float32, N, -K}(I.m, I.depth)
"""
TopMute{T, N, Nw}
Mute top of the model in `N` dimensions
Constructor
===========
judiTopmute(model; taperwidht=10)
judiTopmute(n, wb, taperwidth) # Legacy
"""
struct TopMute{T, N, Nw} <: ModelPreconditioner{T, T}
m::Integer
wb::Array{Int64, Nw}
taperwidth::Int64
TopMute(m::Integer, wb::Array{T, Nw}, taperwidth::Integer) where {T, Nw} = new{Float32, Nw+1, Nw}(m, wb, taperwidth)
end
judiTopmute(n::NTuple{N, Integer}, wb::Array{T, Nw}, taperwidth::Integer) where {T, N, Nw} = TopMute(prod(n), wb, taperwidth)
judiTopmute(n::NTuple{N, Integer}, wb::Integer, taperwidth::Integer) where {N} = TopMute(prod(n), wb*ones(Int64, n[1:end-1]), taperwidth)
function judiTopmute(model::AbstractModel; taperwidth=10)
wb = find_water_bottom(model.m.data)
return TopMute(prod(size(model)), wb, taperwidth)
end
function matvec(D::TopMute{T, N}, x::Array{T, N}) where {T, N}
out = 1 .* x
taper = D.taperwidth < 2 ? 1 : _taper(Val(:reflection), D.taperwidth)
for i in CartesianIndices(D.wb)
out[i, 1:D.wb[i]-D.taperwidth] .= 0
s = max(D.wb[i]-D.taperwidth+1, 1)
ni = length(s:D.wb[i])
tap = D.taperwidth < 2 ? 1 : taper[end-ni+1:end]
out[i, s:D.wb[i]] .*= tap
end
out
end
matvec(D::TopMute{T, N}, x::PhysicalParameter{T}) where {T, N} = PhysicalParameter(x, matvec(D, x.data))
matvec(D::TopMute{T, N}, x::judiWeights{T}) where {T, N} = judiWeights{T}(x.nsrc, [matvec(D, x.data[s]) for s=1:x.nsrc])
matvec(D::TopMute{T, N}, x::Vector{T}) where {T, N} = vec(matvec(D, reshape(x, size(D.wb)..., :)))
matvec_T(D::TopMute{T, N}, x) where {T, N} = matvec(D, x)
# Real diagonal operator
conj(I::TopMute{T, N}) where {T, N} = I
adjoint(I::TopMute{T, N}) where {T, N} = I
transpose(I::TopMute{T, N}) where {T, N} = I
inv(::TopMute{T, N}) where {T, N} = throw(MethodError(inv, "Topmute masks contains zeros cannot be inverted"))
"""
judiIllumination(model; mode="u", k=1, recompute=true)
# Arguments
- `model`: JUDI Model structure
- `mode`: Type of ilumination, choicees of ("u", "v", "uv")
- `k`: Power of the illumination, real number
- `recompute`: Flag whether to recompute the illumination at each new propagation (Defaults to true)
judiIllumination(F; mode="u", k=1, recompute=true)
# Arguments
- `F`: JUDI propagator
- `mode`: Type of ilumination, choicees of ("u", "v", "uv")
- `k`: Power of the illumination, real positive number
- `recompute`: Flag whether to recompute the illumination at each new propagation (Defaults to true)
Diagonal approximation of the FWI Hessian as the energy of the wavefield. The diagonal contains the sum over time
of the wavefield chosen as `mode`.
Options for the mode are "u" for the forward wavefield illumination, "v" for the adjoint wavefield illumination, and
"uv" for the pointwise product of the forward and adjoint wavefields illuminations. Additionally, the parameter "k" provides control on the scaling of
the daiagonal raising it to the power `k`.
Example
========
I = judiIllumination(model)
Construct the diagonal operator such that I*x = x ./ |||u||_2^2
"""
struct judiIllumination{DDT, M, K, R} <: ModelPreconditioner{DDT, DDT}
name::String
illums
m::Integer
end
function judiIllumination(model::AbstractModel; mode="u", k=1, recompute=true)
n = prod(size(model))
# Initialize the illumination as the identity
illum = Dict(s=>PhysicalParameter(size(model), spacing(model), origin(model), ones(Float32, size(model))) for s in split(mode, ""))
I = judiIllumination{Float32, Symbol(mode), k, recompute}("Illumination", illum, n)
init_illum(model, I)
return I
end
judiIllumination(F::judiPropagator; kw...) = judiIllumination(F.model; kw...)
# Real diagonal operator
conj(I::judiIllumination{T}) where T = I
adjoint(I::judiIllumination{T}) where T = I
transpose(I::judiIllumination{T}) where T = I
# Inverse
inv(I::judiIllumination{T, M, K, R}) where {T, M, K, R} = judiIllumination{T, M, -K, R}(I.name, I.illums, I.m)
# Mul
function matvec(I::judiIllumination{T, M, K, R}, x::Vector{T}) where {T, M, K, R}
illum = (.*(values(I.illums)...)).^(1/length(I.illums))
inds = findall(illum[:] .> eps(T))
out = T(0) * x
out[inds] .= illum[:][inds].^K .* x[inds]
return out
end
function matvec(I::judiIllumination{T, M, K, R}, x::PhysicalParameter{T}) where {T, M, K, R}
illum = (.*(values(I.illums)...)).^(1/length(I.illums))
inds = findall(illum .> eps(T))
out = T(0) * x
out[inds] .= illum[inds].^K .* x[inds]
return out
end
# Functor
function (I::judiIllumination{T, M, K, R})(mode::String) where {T, M, K, R}
illum = Dict(s=>similar(first(values(I.illums))) for s in split(mode, ""))
for k ∈ keys(illum)
if k ∈ keys(I.illums)
illum[k] = deepcopy(I.illums[k])
else
fill!(illum[k], 1)
end
end
judiIllumination{T, Symbol(mode), K, R}(I.name, illum, I.m)
end
# Assignment
function set_val(I::judiIllumination{T, M, K, R}, mode, v) where {T, M, K, R}
key = mode ∈ [:forward, :born] ? "u" : "v"
if key in keys(I.illums)
# For safety when propagation doesn't reach all the model
v[v .== 0] .= 1f0
combine!(I.illums[key], v)
end
end
# status
function is_updated(I::judiIllumination{T, M, K, R}) where {T, M, K, R}
updated = true
for (k, v) in I.illums
im, iM = extrema(v)
updated = (im == iM == 1) && updated
end
return ~updated
end
################## Illumination tracker. ####################
# We carry a global tracker that associate an illumination operator
# with its model so that we can extract it after propagation
_illums = Dict()
init_illum(model::AbstractModel, Tm::judiIllumination) = (_illums[objectid(model)] = [Tm, false])
function update_illum(vals::Tuple, F::judiPropagator{D, O}) where {D, O}
if length(vals) == 3
update_illum(F.model, vals[2], :forward)
update_illum(F.model, vals[3], :adjoint)
else
update_illum(F.model, vals[2], O)
end
return vals[1]
end
update_illum(vals, ::judiPropagator) = vals
function update_illum(vals::Tuple, model::AbstractModel, ::Any)
length(vals) == 2 && (return vals)
update_illum(model, vals[3], :forward)
update_illum(model, vals[4], :adjoint)
return vals[1:2]
end
function update_illum(model::AbstractModel, i::PhysicalParameter, mode)
set_val(_illums[objectid(model)][1], mode, i)
_illums[objectid(model)][2] = is_updated(_illums[objectid(model)][1])
end
function _compute_illum(::judiIllumination{T, M, K, R}, status, mode) where {T, M, K, R}
if status && ~R
return false
elseif (mode ∈ [:forward, :born] && M ∈ [:u, :uv]) || (mode == :adjoint && M ∈ [:v, :uv]) || (mode == :adjoint_born)
return true
else
return false
end
end
function compute_illum(model::AbstractModel, mode::Symbol)
objectid(model) ∉ keys(_illums) && (return false)
return _compute_illum(_illums[objectid(model)]..., mode)
end
function _track_illum(old_m::AbstractModel, new_m::AbstractModel)
if (objectid(old_m) ∈ keys(_illums)) && (objectid(new_m) ∉ keys(_illums))
_illums[objectid(new_m)] = _illums[objectid(old_m)]
end
end