-
Notifications
You must be signed in to change notification settings - Fork 29
/
fwi_example_NLopt.jl
63 lines (49 loc) · 1.82 KB
/
fwi_example_NLopt.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
# 2D FWI on Overthrust model with L-BFGS using NLopt library
# Author: Philipp Witte, [email protected]
# Date: December 2017
#
using Statistics, Random, LinearAlgebra
using JUDI, HDF5, NLopt, SegyIO
# Load starting model
n,d,o,m0 = read(h5open("$(JUDI.JUDI_DATA)/overthrust_model.h5","r"), "n", "d", "o", "m0")
model0 = Model((n[1],n[2]), (d[1],d[2]), (o[1],o[2]), m0)
# Bound constraints
v0 = sqrt.(1f0 ./m0)
vmin = ones(Float32, model0.n) .* 1.3f0
vmax = ones(Float32, model0.n) .* 6.5f0
# Slowness squared [s^2/km^2]
mmin = vec((1f0 ./ vmax).^2)
mmax = vec((1f0 ./ vmin).^2)
# Load data
block = segy_read("$(JUDI.JUDI_DATA)/overthrust_shot_records.segy")
d_obs = judiVector(block)
# Set up wavelet
src_geometry = Geometry(block; key="source")
wavelet = ricker_wavelet(src_geometry.t[1], src_geometry.dt[1], 0.008f0) # 8 Hz wavelet
q = judiVector(src_geometry, wavelet)
############################### FWI ###########################################
# optimization parameters
batchsize = 16
count = 0
# NLopt objective function
println("No. ", "fval ", "norm(gradient)")
function f!(x,grad)
# Update model
model0.m .= convert(Array{Float32, 2}, reshape(x, model0.n))
# Seclect batch and calculate gradient
i = randperm(d_obs.nsrc)[1:batchsize]
fval, gradient = fwi_objective(model0, q[i], d_obs[i])
# Reset gradient in water column to zero
gradient = reshape(gradient, model0.n)
gradient[:, 1:21] .= 0f0
grad[1:end] = vec(gradient)
global count; count += 1
println(count, " ", fval, " ", norm(grad))
return convert(Float64, fval)
end
# Optimization parameters
opt = Opt(:LD_LBFGS, prod(model0.n))
lower_bounds!(opt, mmin); upper_bounds!(opt, mmax)
min_objective!(opt, f!)
maxeval!(opt, parse(Int, get(ENV, "NITER", "10")))
(minf, minx, ret) = optimize(opt, vec(model0.m.data))